[lxc-devel] [lxc/master] conf: fix misreading issue of proc mountinfo

2xsec on Github lxc-bot at linuxcontainers.org
Mon Jun 4 09:50:18 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 589 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180604/630ff496/attachment.bin>
-------------- next part --------------
From e97d22d263e1fa119146150bbd075eb0ac8e27e8 Mon Sep 17 00:00:00 2001
From: Donghwa Jeong <dh48.jeong at samsung.com>
Date: Mon, 4 Jun 2018 18:40:50 +0900
Subject: [PATCH] conf: fix misreading issue of proc mountinfo

Signed-off-by: Donghwa Jeong <dh48.jeong at samsung.com>
---
 src/lxc/conf.c  | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/lxc/conf.h  |  2 +-
 src/lxc/start.c |  2 +-
 3 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 05d58081e..e7206deed 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -3175,16 +3175,63 @@ void tmp_proc_unmount(struct lxc_conf *lxc_conf)
 	lxc_conf->tmp_umount_proc = false;
 }
 
+static void copy_proc_mountinfo(const char* src, const char* dst)
+{
+	FILE *src_fp;
+	FILE *dst_fp;
+	char buf[4096] = {0};
+
+	if (!src || !dst) {
+		ERROR("Invalid argument value");
+		return;
+	}
+
+	src_fp = fopen(src, "r");
+	if (!src_fp) {
+		SYSERROR("Failed to open \"%s\"", src);
+		return;
+	}
+
+	dst_fp = fopen(dst, "w");
+	if (!dst_fp) {
+		SYSERROR("Failed to open \"%s\"", dst);
+		fclose(src_fp);
+		return;
+	}
+
+	while (fgets(buf, sizeof(buf), src_fp))
+		fputs(buf, dst_fp);
+
+	fclose(src_fp);
+	fclose(dst_fp);
+
+	INFO("\"%s\" is copied to \"%s\"", src, dst);
+}
+
 /* Walk /proc/mounts and change any shared entries to slave. */
-void remount_all_slave(void)
+void remount_all_slave(const char *name)
 {
 	FILE *f;
 	size_t len = 0;
 	char *line = NULL;
+	char path[MAXPATHLEN];
+	bool ret = false;
+
+	if (name) {
+		snprintf(path, MAXPATHLEN, "/tmp/lxc_%s_mountinfo", name);
 
-	f = fopen("/proc/self/mountinfo", "r");
+		copy_proc_mountinfo("/proc/self/mountinfo", path);
+
+		if (!access(path, F_OK | R_OK))
+			ret = true;
+	}
+
+	if (!ret)
+		snprintf(path, MAXPATHLEN, "/proc/self/mountinfo");
+
+	f = fopen(path, "r");
 	if (!f) {
-		SYSERROR("Failed to open \"/proc/self/mountinfo\" to mark all shared");
+		SYSERROR("Failed to open \"%s\" to mark all shared", path);
 		ERROR("Continuing container startup...");
 		return;
 	}
@@ -3214,6 +3261,9 @@ void remount_all_slave(void)
 	}
 	fclose(f);
 	free(line);
+
+	if (ret && remove("/tmp/lxc_mountinfo.txt") < 0)
+		SYSERROR("Failed to remove \"%s\"", path);
 }
 
 static int lxc_execute_bind_init(struct lxc_handler *handler)
@@ -3298,7 +3348,7 @@ int do_rootfs_setup(struct lxc_conf *conf, const char *name, const char *lxcpath
 		return 0;
 	}
 
-	remount_all_slave();
+	remount_all_slave(name);
 
 	ret = run_lxc_hooks(name, "pre-mount", conf, NULL);
 	if (ret < 0) {
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index ea3a71dfb..2a56b80b0 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -414,7 +414,7 @@ extern int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *),
 extern int parse_mntopts(const char *mntopts, unsigned long *mntflags,
 			 char **mntdata);
 extern void tmp_proc_unmount(struct lxc_conf *lxc_conf);
-extern void remount_all_slave(void);
+extern void remount_all_slave(const char *name);
 extern void suggest_default_idmap(void);
 extern FILE *make_anonymous_mount_file(struct lxc_list *mount);
 extern struct lxc_list *sort_cgroup_settings(struct lxc_list *cgroup_settings);
diff --git a/src/lxc/start.c b/src/lxc/start.c
index b222c847a..38b3a34e4 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -1872,7 +1872,7 @@ int __lxc_start(const char *name, struct lxc_handler *handler,
 			}
 			INFO("Unshared CLONE_NEWNS");
 
-			remount_all_slave();
+			remount_all_slave(name);
 			ret = do_rootfs_setup(conf, name, lxcpath);
 			if (ret < 0) {
 				ERROR("Error setting up rootfs mount as root before spawn");


More information about the lxc-devel mailing list