Merge pull request #1575 from brauner/2017-05-18/fix_tmp_mount_proc

utils: fix lxc_mount_proc_if_needed()
This commit is contained in:
Serge Hallyn 2017-05-18 12:45:13 -05:00 committed by GitHub
commit a1387f5c14

View File

@ -1758,9 +1758,8 @@ int safe_mount(const char *src, const char *dest, const char *fstype,
int lxc_mount_proc_if_needed(const char *rootfs) int lxc_mount_proc_if_needed(const char *rootfs)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
char link[20]; int link_to_pid, linklen, mypid, ret;
int link_to_pid, linklen, ret; char link[LXC_NUMSTRLEN64] = {0};
int mypid;
ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs); ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs);
if (ret < 0 || ret >= MAXPATHLEN) { if (ret < 0 || ret >= MAXPATHLEN) {
@ -1768,10 +1767,7 @@ int lxc_mount_proc_if_needed(const char *rootfs)
return -1; return -1;
} }
memset(link, 0, 20); linklen = readlink(path, link, LXC_NUMSTRLEN64);
linklen = readlink(path, link, 20);
mypid = (int)getpid();
INFO("I am %d, /proc/self points to \"%s\"", mypid, link);
ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs); ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
if (ret < 0 || ret >= MAXPATHLEN) { if (ret < 0 || ret >= MAXPATHLEN) {
@ -1784,24 +1780,29 @@ int lxc_mount_proc_if_needed(const char *rootfs)
if (mkdir(path, 0755) && errno != EEXIST) if (mkdir(path, 0755) && errno != EEXIST)
return -1; return -1;
goto domount; goto domount;
} else if (linklen >= LXC_NUMSTRLEN64) {
link[linklen - 1] = '\0';
ERROR("readlink returned truncated content: \"%s\"", link);
return -1;
} }
mypid = getpid();
INFO("I am %d, /proc/self points to \"%s\"", mypid, link);
if (lxc_safe_int(link, &link_to_pid) < 0) if (lxc_safe_int(link, &link_to_pid) < 0)
return -1; return -1;
/* wrong /procs mounted */ /* correct procfs is already mounted */
if (link_to_pid != mypid) { if (link_to_pid == mypid)
/* ignore failure */ return 0;
umount2(path, MNT_DETACH);
goto domount;
}
/* the right proc is already mounted */ ret = umount2(path, MNT_DETACH);
return 0; if (ret < 0)
WARN("failed to umount \"%s\" with MNT_DETACH", path);
domount: domount:
/* rootfs is NULL */ /* rootfs is NULL */
if (!strcmp(rootfs,"")) if (!strcmp(rootfs, ""))
ret = mount("proc", path, "proc", 0, NULL); ret = mount("proc", path, "proc", 0, NULL);
else else
ret = safe_mount("proc", path, "proc", 0, NULL, rootfs); ret = safe_mount("proc", path, "proc", 0, NULL, rootfs);