Skip to content
Snippets Groups Projects
Commit bfdbb708 authored by Bob Lantz's avatar Bob Lantz
Browse files

Fall back to chroot() if setns() fails for mnt namespace

fixes #347
parent 16a384ab
No related branches found
No related tags found
No related merge requests found
...@@ -140,9 +140,9 @@ int main(int argc, char *argv[]) ...@@ -140,9 +140,9 @@ int main(int argc, char *argv[])
fflush(stdout); fflush(stdout);
break; break;
case 'a': case 'a':
/* Attach to pid's network namespace and mount namespace*/ /* Attach to pid's network namespace and mount namespace */
pid = atoi(optarg); pid = atoi(optarg);
sprintf(path, "/proc/%d/ns/net", pid ); sprintf(path, "/proc/%d/ns/net", pid);
nsid = open(path, O_RDONLY); nsid = open(path, O_RDONLY);
if (nsid < 0) { if (nsid < 0) {
perror(path); perror(path);
...@@ -152,15 +152,16 @@ int main(int argc, char *argv[]) ...@@ -152,15 +152,16 @@ int main(int argc, char *argv[])
perror("setns"); perror("setns");
return 1; return 1;
} }
sprintf(path, "/proc/%d/ns/mnt", pid ); /* Plan A: call setns() to attach to mount namespace */
sprintf(path, "/proc/%d/ns/mnt", pid);
nsid = open(path, O_RDONLY); nsid = open(path, O_RDONLY);
if (nsid < 0) { if (nsid < 0 || setns(nsid, 0) != 0) {
perror(path); /* Plan B: chroot into pid's root file system */
return 1; sprintf(path, "/proc/%d/root", pid);
} if (chroot(path) < 0) {
if (setns(nsid, 0) != 0) { perror(path);
perror("setns"); return 1;
return 1; }
} }
break; break;
case 'g': case 'g':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment