Added use_switchroot option

This commit is contained in:
Eli Zrihen 2021-06-17 14:57:01 +03:00
parent 2e9fd0e2e4
commit dab1713ac9
5 changed files with 64 additions and 22 deletions

View File

@ -76,6 +76,7 @@ struct custom_option custom_opts[] = {
{ { "exec_file", required_argument, NULL, 'x' }, "File to exec (default: argv[0])" },
{ { "execute_fd", no_argument, NULL, 0x0607 }, "Use execveat() to execute a file-descriptor instead of executing the binary path. In such case argv[0]/exec_file denotes a file path before mount namespacing" },
{ { "chroot", required_argument, NULL, 'c' }, "Directory containing / of the jail (default: none)" },
{ { "use_switchroot", no_argument, NULL, 0x600 }, "When creating a mount namespace, use switch_root rather then the default pivot_root (usefull when using rootfs, on which the kernel disallows pivot_root)" },
{ { "rw", no_argument, NULL, 0x601 }, "Mount chroot dir (/) R/W (default: R/O)" },
{ { "user", required_argument, NULL, 'u' }, "Username/uid of processes inside the jail (default: your current uid). You can also use inside_ns_uid:outside_ns_uid:count convention here. Can be specified multiple times" },
{ { "group", required_argument, NULL, 'g' }, "Groupname/gid of processes inside the jail (default: your current gid). You can also use inside_ns_gid:global_ns_gid:count convention here. Can be specified multiple times" },
@ -431,6 +432,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
nsjconf->clone_newnet = true;
nsjconf->clone_newuser = true;
nsjconf->clone_newns = true;
nsjconf->use_switchroot = false;
nsjconf->clone_newpid = true;
nsjconf->clone_newipc = true;
nsjconf->clone_newuts = true;
@ -648,6 +650,9 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
}
nsjconf->caps.push_back(cap);
} break;
case 0x0600:
nsjconf->use_switchroot = true;
break;
case 0x0601:
nsjconf->is_root_rw = true;
break;

View File

@ -185,6 +185,8 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig&
nsjconf->clone_newcgroup = njc.clone_newcgroup();
nsjconf->clone_newtime = njc.clone_newtime();
nsjconf->use_switchroot = njc.use_switchroot();
for (ssize_t i = 0; i < njc.uidmap_size(); i++) {
if (!user::parseId(nsjconf, njc.uidmap(i).inside_id(), njc.uidmap(i).outside_id(),
njc.uidmap(i).count(), false /* is_gid */, njc.uidmap(i).use_newidmap())) {

View File

@ -86,6 +86,9 @@ message NsJailConfig {
/* Initial current working directory for the binary */
optional string cwd = 9 [default = "/"];
/* Defines whether to use switch_root or pivot_root */
optional bool use_switchroot = 88 [default = false];
/* TCP port to listen to. Valid with mode=LISTEN only */
optional uint32 port = 10 [default = 0];
/* Host to bind to for mode=LISTEN. Must be in IPv6 format */

31
mnt.cc
View File

@ -398,6 +398,8 @@ static bool initCloneNs(nsjconf_t* nsjconf) {
PLOG_E("umount2('%s', MNT_DETACH)", tmpdir->c_str());
return false;
}
if (false == nsjconf->use_switchroot) {
/*
* This requires some explanation: It's actually possible to pivot_root('/', '/').
* After this operation has been completed, the old root is mounted over the new
@ -416,6 +418,35 @@ static bool initCloneNs(nsjconf_t* nsjconf) {
PLOG_E("umount2('/', MNT_DETACH)");
return false;
}
} else {
/*
* pivot_root would normally un-mount the old root, however in certain cases this
* operation is forbidden. There are systems (mainly embedded) that keep their root
* file system in RAM, when initially loaded by the kernel (e.g. initramfs),
* and there is no other file system that is mounted on top of it.In such systems,
* there is option to pivot_root!
* For more information, see kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt.
* switch_root alternative:
* Innstead of un-mounting the old rootfs, it is over mounted by moving the new root to it.
* This way, we prevent the process from hacking its way back into the old root.
*/
if (chdir(destdir->c_str()) == -1) {
PLOG_E("chdir('%s')", destdir->c_str());
return false;
}
/* mount moving the new root on top of '/'. This operation is atomic and doesn't involve
un-mounting '/' at any stage */
if (mount(".", "/", NULL, MS_MOVE, NULL) == -1) {
PLOG_E("mount('/', %s, NULL, MS_MOVE, NULL)", destdir->c_str());
return false;
}
if (chroot(".") == -1) {
PLOG_E("chroot('%s')", destdir->c_str());
return false;
}
}
for (const auto& p : nsjconf->mountpts) {
if (!remountPt(p) && p.is_mandatory) {

View File

@ -120,6 +120,7 @@ struct nsjconf_t {
bool clone_newnet;
bool clone_newuser;
bool clone_newns;
bool use_switchroot;
bool clone_newpid;
bool clone_newipc;
bool clone_newuts;