Skip to content

Commit 614ce12

Browse files
committed
[1.1] nsenter: cloned_binary: remove bindfd logic entirely
(This is a cherry-pick of b999376.) While the ro-bind-mount trick did eliminate the memory overhead of copying the runc binary for each "runc init" invocation, on machines with very significant container churn, creating a temporary mount namespace on every container invocation can trigger severe lock contention on namespace_sem that makes containers fail to spawn. The only reason we added bindfd in commit 16612d7 ("nsenter: cloned_binary: try to ro-bind /proc/self/exe before copying") was due to a Kubernetes e2e test failure where they had a ridiculously small memory limit. It seems incredibly unlikely that real workloads are running without 10MB to spare for the very short time that runc is interacting with the container. In addition, since the original cloned_binary implementation, cgroupv2 is now almost universally used on modern systems. Unlike cgroupv1, the cgroupv2 memcg implementation does not migrate memory usage when processes change cgroups (even cgroupv1 only did this if you had memory.move_charge_at_immigrate enabled). In addition, because we do the /proc/self/exe clone before synchronising the bootstrap data read, we are guaranteed to do the clone before "runc init" is moved into the container cgroup -- meaning that the memory used by the /proc/self/exe clone is charged against the root cgroup, and thus container workloads should not be affected at all with memfd cloning. The long-term fix for this problem is to block the /proc/self/exe re-opening attack entirely in-kernel, which is something I'm working on[1]. Though it should also be noted that because the memfd is completely separate to the host binary, even attacks like Dirty COW against the runc binary can be defended against with the memfd approach. Of course, once we have in-kernel protection against the /proc/self/exe re-opening attack, we won't have that protection anymore... [1]: https://lwn.net/Articles/934460/ Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 2655e7c commit 614ce12

File tree

1 file changed

+0
-67
lines changed

1 file changed

+0
-67
lines changed

libcontainer/nsenter/cloned_binary.c

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -396,61 +396,6 @@ static int seal_execfd(int *fd, int fdtype)
396396
return -1;
397397
}
398398

399-
static int try_bindfd(void)
400-
{
401-
int fd, ret = -1;
402-
char template[PATH_MAX] = { 0 };
403-
char *prefix = getenv("_LIBCONTAINER_STATEDIR");
404-
405-
if (!prefix || *prefix != '/')
406-
prefix = "/tmp";
407-
if (snprintf(template, sizeof(template), "%s/runc.XXXXXX", prefix) < 0)
408-
return ret;
409-
410-
/*
411-
* We need somewhere to mount it, mounting anything over /proc/self is a
412-
* BAD idea on the host -- even if we do it temporarily.
413-
*/
414-
fd = mkstemp(template);
415-
if (fd < 0)
416-
return ret;
417-
close(fd);
418-
419-
/*
420-
* For obvious reasons this won't work in rootless mode because we haven't
421-
* created a userns+mntns -- but getting that to work will be a bit
422-
* complicated and it's only worth doing if someone actually needs it.
423-
*/
424-
ret = -EPERM;
425-
if (mount("/proc/self/exe", template, "", MS_BIND, "") < 0)
426-
goto out;
427-
if (mount("", template, "", MS_REMOUNT | MS_BIND | MS_RDONLY, "") < 0)
428-
goto out_umount;
429-
430-
/* Get read-only handle that we're sure can't be made read-write. */
431-
ret = open(template, O_PATH | O_CLOEXEC);
432-
433-
out_umount:
434-
/*
435-
* Make sure the MNT_DETACH works, otherwise we could get remounted
436-
* read-write and that would be quite bad (the fd would be made read-write
437-
* too, invalidating the protection).
438-
*/
439-
if (umount2(template, MNT_DETACH) < 0) {
440-
if (ret >= 0)
441-
close(ret);
442-
ret = -ENOTRECOVERABLE;
443-
}
444-
445-
out:
446-
/*
447-
* We don't care about unlink errors, the worst that happens is that
448-
* there's an empty file left around in STATEDIR.
449-
*/
450-
unlink(template);
451-
return ret;
452-
}
453-
454399
static ssize_t fd_to_fd(int outfd, int infd)
455400
{
456401
ssize_t total = 0;
@@ -485,18 +430,6 @@ static int clone_binary(void)
485430
size_t sent = 0;
486431
int fdtype = EFD_NONE;
487432

488-
/*
489-
* Before we resort to copying, let's try creating an ro-binfd in one shot
490-
* by getting a handle for a read-only bind-mount of the execfd.
491-
*/
492-
execfd = try_bindfd();
493-
if (execfd >= 0)
494-
return execfd;
495-
496-
/*
497-
* Dammit, that didn't work -- time to copy the binary to a safe place we
498-
* can seal the contents.
499-
*/
500433
execfd = make_execfd(&fdtype);
501434
if (execfd < 0 || fdtype == EFD_NONE)
502435
return -ENOTRECOVERABLE;

0 commit comments

Comments
 (0)