Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsenter: cloned_binary: "memfd" cleanups #1984

Merged
merged 5 commits into from
Mar 7, 2019
Merged

nsenter: cloned_binary: "memfd" cleanups #1984

merged 5 commits into from
Mar 7, 2019

Commits on Feb 26, 2019

  1. nsenter: cloned_binary: detect and handle short copies

    For a variety of reasons, sendfile(2) can end up doing a short-copy so
    we need to just loop until we hit the binary size. Since /proc/self/exe
    is tautologically our own binary, there's no chance someone is going to
    modify it underneath us (or changing the size).
    
    Signed-off-by: Aleksa Sarai <asarai@suse.de>
    cyphar committed Feb 26, 2019
    Configuration menu
    Copy the full SHA
    5b775bf View commit details
    Browse the repository at this point in the history

Commits on Mar 1, 2019

  1. nsenter: cloned_binary: expand and add pre-3.11 fallbacks

    In order to get around the memfd_create(2) requirement, 0a8e411
    ("nsenter: clone /proc/self/exe to avoid exposing host binary to
    container") added an O_TMPFILE fallback. However, this fallback was
    flawed in two ways:
    
     * It required O_TMPFILE which is relatively new (having been added to
       Linux 3.11).
    
     * The fallback choice was made at compile-time, not runtime. This
       results in several complications when it comes to running binaries
       on different machines to the ones they were built on.
    
    The easiest way to resolve these things is to have fallbacks work in a
    more procedural way (though it does make the code unfortunately more
    complicated) and to add a new fallback that uses mkotemp(3).
    
    Signed-off-by: Aleksa Sarai <asarai@suse.de>
    cyphar committed Mar 1, 2019
    Configuration menu
    Copy the full SHA
    2429d59 View commit details
    Browse the repository at this point in the history
  2. nsenter: cloned_binary: use the runc statedir for O_TMPFILE

    Writing a file to tmpfs actually incurs a memcg penalty, and thus the
    benefit of being able to disable memfd_create(2) with
    _LIBCONTAINER_DISABLE_MEMFD_CLONE is fairly minimal -- though it should
    be noted that quite a few distributions don't use tmpfs for /tmp (and
    instead have it as a regular directory or subvolume of the host
    filesystem).
    
    Since runc must have write access to the state directory anyway (and the
    state directory is usually not on a tmpfs) we can use that instead of
    /tmp -- avoiding potential memcg costs with no real downside.
    
    Signed-off-by: Aleksa Sarai <asarai@suse.de>
    cyphar committed Mar 1, 2019
    Configuration menu
    Copy the full SHA
    af9da0a View commit details
    Browse the repository at this point in the history
  3. nsenter: cloned_binary: try to ro-bind /proc/self/exe before copying

    The usage of memfd_create(2) and other copying techniques is quite
    wasteful, despite attempts to minimise it with _LIBCONTAINER_STATEDIR.
    memfd_create(2) added ~10M of memory usage to the cgroup associated with
    the container, which can result in some setups getting OOM'd (or just
    hogging the hosts' memory when you have lots of created-but-not-started
    containers sticking around).
    
    The easiest way of solving this is by creating a read-only bind-mount of
    the binary, opening that read-only bindmount, and then umounting it to
    ensure that the host won't accidentally be re-mounted read-write. This
    avoids all copying and cleans up naturally like the other techniques
    used. Unfortunately, like the O_TMPFILE fallback, this requires being
    able to create a file inside _LIBCONTAINER_STATEDIR (since bind-mounting
    over the most obvious path -- /proc/self/exe -- is a *very bad idea*).
    
    Unfortunately detecting this isn't fool-proof -- on a system with a
    read-only root filesystem (that might become read-write during "runc
    init" execution), we cannot tell whether we have already done an ro
    remount. As a partial mitigation, we store a _LIBCONTAINER_CLONED_BINARY
    environment variable which is checked *alongside* the protection being
    present.
    
    Signed-off-by: Aleksa Sarai <asarai@suse.de>
    cyphar committed Mar 1, 2019
    Configuration menu
    Copy the full SHA
    16612d7 View commit details
    Browse the repository at this point in the history
  4. nsenter: cloned_binary: userspace copy fallback if sendfile fails

    There are some circumstances where sendfile(2) can fail (one example is
    that AppArmor appears to block writing to deleted files with sendfile(2)
    under some circumstances) and so we need to have a userspace fallback.
    It's fairly trivial (and handles short-writes).
    
    Signed-off-by: Aleksa Sarai <asarai@suse.de>
    cyphar committed Mar 1, 2019
    Configuration menu
    Copy the full SHA
    2d4a37b View commit details
    Browse the repository at this point in the history