Skip to content

Commit d1f332b

Browse files
committed
Enable support for clone3() and for CLONE_NEWTIME
1 parent 4be9595 commit d1f332b

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

cmdline.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,18 @@ void logParams(nsjconf_t* nsjconf) {
231231
"max_conns:%u, max_conns_per_ip:%u, time_limit:%" PRId64
232232
", personality:%#lx, daemonize:%s, clone_newnet:%s, "
233233
"clone_newuser:%s, clone_newns:%s, clone_newpid:%s, clone_newipc:%s, clone_newuts:%s, "
234-
"clone_newcgroup:%s, clone_newtime:%s, keep_caps:%s, disable_no_new_privs:%s, max_cpus:%zu",
234+
"clone_newcgroup:%s, clone_newtime:%s, keep_caps:%s, disable_no_new_privs:%s, "
235+
"max_cpus:%zu",
235236
nsjconf->hostname.c_str(), nsjconf->chroot.c_str(),
236237
nsjconf->exec_file.empty() ? nsjconf->argv[0].c_str() : nsjconf->exec_file.c_str(),
237238
nsjconf->bindhost.c_str(), nsjconf->port, nsjconf->max_conns, nsjconf->max_conns_per_ip,
238239
nsjconf->tlimit, nsjconf->personality, logYesNo(nsjconf->daemonize),
239240
logYesNo(nsjconf->clone_newnet), logYesNo(nsjconf->clone_newuser),
240241
logYesNo(nsjconf->clone_newns), logYesNo(nsjconf->clone_newpid),
241242
logYesNo(nsjconf->clone_newipc), logYesNo(nsjconf->clone_newuts),
242-
logYesNo(nsjconf->clone_newcgroup), logYesNo(nsjconf->clone_newtime), logYesNo(nsjconf->keep_caps),
243-
logYesNo(nsjconf->disable_no_new_privs), nsjconf->max_cpus);
243+
logYesNo(nsjconf->clone_newcgroup), logYesNo(nsjconf->clone_newtime),
244+
logYesNo(nsjconf->keep_caps), logYesNo(nsjconf->disable_no_new_privs),
245+
nsjconf->max_cpus);
244246

245247
for (const auto& p : nsjconf->mountpts) {
246248
LOG_I(

config.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ message NsJailConfig {
177177
optional bool clone_newuts = 52 [default = true];
178178
/* Disable for kernel versions < 4.6 as it's not supported there */
179179
optional bool clone_newcgroup = 53 [default = true];
180-
/* Supported with kernel versions >= 5.3 */
180+
/* Supported with kernel versions >= 5.3 */
181181
optional bool clone_newtime = 86 [default = false];
182182

183183
/* Mappings for UIDs and GIDs. See the description for 'msg IdMap'

mnt.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ bool initNs(nsjconf_t* nsjconf) {
453453
return initNsInternal(nsjconf);
454454
}
455455

456-
pid_t pid = subproc::cloneProc(CLONE_FS | SIGCHLD);
456+
pid_t pid = subproc::cloneProc(CLONE_FS, SIGCHLD);
457457
if (pid == -1) {
458458
return false;
459459
}

pid.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bool initNs(nsjconf_t* nsjconf) {
4848
* first clone/fork will work, and the rest will fail with ENOMEM (see 'man pid_namespaces'
4949
* for details on this behavior)
5050
*/
51-
pid_t pid = subproc::cloneProc(CLONE_FS);
51+
pid_t pid = subproc::cloneProc(CLONE_FS, 0);
5252
if (pid == -1) {
5353
PLOG_E("Couldn't create a dummy init process");
5454
return false;

subproc.cc

+51-20
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,20 @@ static const std::string cloneFlagsToStr(uintptr_t flags) {
100100
NS_VALSTR_STRUCT(CLONE_IO),
101101
};
102102

103-
uintptr_t knownFlagMask = CSIGNAL;
103+
uintptr_t knownFlagMask = 0;
104104
for (const auto& i : cloneFlags) {
105105
if (flags & i.flag) {
106-
res.append(i.name).append("|");
106+
if (!res.empty()) {
107+
res.append("|");
108+
}
109+
res.append(i.name);
107110
}
108111
knownFlagMask |= i.flag;
109112
}
110113

111114
if (flags & ~(knownFlagMask)) {
112-
util::StrAppend(&res, "%#tx|", flags & ~(knownFlagMask));
115+
util::StrAppend(&res, "|%#tx", flags & ~(knownFlagMask));
113116
}
114-
res.append(util::sigName(flags & CSIGNAL).c_str());
115117
return res;
116118
}
117119

@@ -444,8 +446,8 @@ pid_t runChild(nsjconf_t* nsjconf, int netfd, int fd_in, int fd_out, int fd_err)
444446
LOG_F("Launching new process failed");
445447
}
446448

447-
flags |= SIGCHLD;
448-
LOG_D("Creating new process with clone flags:%s", cloneFlagsToStr(flags).c_str());
449+
LOG_D("Creating new process with clone flags:%s and exit_signal:SIGCHLD",
450+
cloneFlagsToStr(flags).c_str());
449451

450452
int sv[2];
451453
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) {
@@ -455,7 +457,7 @@ pid_t runChild(nsjconf_t* nsjconf, int netfd, int fd_in, int fd_out, int fd_err)
455457
int child_fd = sv[0];
456458
int parent_fd = sv[1];
457459

458-
pid_t pid = cloneProc(flags);
460+
pid_t pid = cloneProc(flags, SIGCHLD);
459461
if (pid == 0) {
460462
close(parent_fd);
461463
subprocNewProc(nsjconf, netfd, fd_in, fd_out, fd_err, child_fd);
@@ -464,21 +466,20 @@ pid_t runChild(nsjconf_t* nsjconf, int netfd, int fd_in, int fd_out, int fd_err)
464466
}
465467
close(child_fd);
466468
if (pid == -1) {
469+
auto saved_errno = errno;
470+
PLOG_W("clone(flags=%s) failed", cloneFlagsToStr(flags).c_str());
467471
if (flags & CLONE_NEWCGROUP) {
468-
auto saved_errno = errno;
469-
PLOG_E(
472+
LOG_W(
470473
"nsjail tried to use the CLONE_NEWCGROUP clone flag, which is "
471-
"supported under kernel versions >= 4.6 only. Try disabling this flag");
472-
errno = saved_errno;
474+
"supported under kernel versions >= 4.6 only");
475+
} else if (flags & CLONE_NEWTIME) {
476+
LOG_W(
477+
"nsjail tried to use the CLONE_NEWTIME clone flag, which is "
478+
"supported under kernel versions >= 5.13 only");
473479
}
474-
PLOG_E(
475-
"clone(flags=%s) failed. You probably need root privileges if your system "
476-
"doesn't support CLONE_NEWUSER. Alternatively, you might want to recompile "
477-
"your kernel with support for namespaces or check the current value of the "
478-
"kernel.unprivileged_userns_clone sysctl",
479-
cloneFlagsToStr(flags).c_str());
480480
close(parent_fd);
481-
return -1;
481+
errno = saved_errno;
482+
return pid;
482483
}
483484
addProc(nsjconf, pid, netfd);
484485

@@ -517,9 +518,39 @@ static int cloneFunc(void* arg __attribute__((unused))) {
517518
* update the internal PID/TID caches, what can lead to invalid values being returned by getpid()
518519
* or incorrect PID/TIDs used in raise()/abort() functions
519520
*/
520-
pid_t cloneProc(uintptr_t flags) {
521+
pid_t cloneProc(uintptr_t flags, int exit_signal) {
522+
exit_signal &= CSIGNAL;
523+
521524
if (flags & CLONE_VM) {
522525
LOG_E("Cannot use clone(flags & CLONE_VM)");
526+
errno = 0;
527+
return -1;
528+
}
529+
530+
#if defined(__NR_clone3)
531+
struct clone_args ca = {
532+
.flags = (uint64_t)flags,
533+
.pidfd = 0,
534+
.child_tid = 0,
535+
.parent_tid = 0,
536+
.exit_signal = (uint64_t)exit_signal,
537+
.stack = 0,
538+
.stack_size = 0,
539+
.tls = 0,
540+
.set_tid = 0,
541+
.set_tid_size = 0,
542+
.cgroup = 0,
543+
};
544+
545+
pid_t ret = util::syscall(__NR_clone3, (uintptr_t)&ca, sizeof(ca));
546+
if (ret != -1 || errno != ENOSYS) {
547+
return ret;
548+
}
549+
#endif /* defined(__NR_clone3) */
550+
551+
if (flags & CLONE_NEWTIME) {
552+
LOG_E("CLONE_NEWTIME was requested but clone3() is not supported");
553+
errno = 0;
523554
return -1;
524555
}
525556

@@ -532,7 +563,7 @@ pid_t cloneProc(uintptr_t flags) {
532563
*/
533564
void* stack = &cloneStack[sizeof(cloneStack) / 2];
534565
/* Parent */
535-
return clone(cloneFunc, stack, flags, NULL, NULL, NULL);
566+
return clone(cloneFunc, stack, flags | exit_signal, NULL, NULL, NULL);
536567
}
537568
/* Child */
538569
return 0;

subproc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void killAndReapAll(nsjconf_t* nsjconf);
4141
/* Returns the exit code of the first failing subprocess, or 0 if none fail */
4242
int reapProc(nsjconf_t* nsjconf);
4343
int systemExe(const std::vector<std::string>& args, char** env);
44-
pid_t cloneProc(uintptr_t flags);
44+
pid_t cloneProc(uintptr_t flags, int exit_signal);
4545

4646
} // namespace subproc
4747

0 commit comments

Comments
 (0)