Skip to content

Consider using discourse/ruby-landlock to narrow file permissions #13

Open

Description

@flavorjones

A worker runs as the same user as the supervisor, and that user must be able to write the socket directory. So a worker that achieves code execution can unlink work.sock and bind its own, and every later request arrives at its listener with the caller's descriptors attached.

Supervisor#verify_sockets records each socket's inode and stops the cell when it changes. That is detection, not prevention: the attacker still serves whatever arrives before the next check.

Every prevention available to an unprivileged container was ruled out, and the reasons are in that method's comment. A tighter directory mode is undone by the owner, the sticky bit grants the owner what it withholds from others, the immutable flag needs CAP_LINUX_IMMUTABLE, a uid per worker needs CAP_SETUID, and an abstract-namespace socket cannot be reached across network: none.

Landlock is the one that was not ruled out. It lets an unprivileged process apply an irreversible filesystem restriction to itself and its children, with no capability. A worker would give up write and unlink access to the socket directory immediately after the fork, before it touches an untrusted byte. Being irreversible is the property that matters here: there is nothing for the attacker to undo.

discourse/ruby-landlock is a binding to evaluate.

Worth considering at the same time, since a ruleset costs the same whether it names one directory or several:

  • the slot workspace, where a worker can read a concurrent request's staged files
  • /hotcell, which holds config.rb and the operations the supervisor loads at boot

Open questions: the kernel version floor, whether the runtime's seccomp profile permits the Landlock syscalls, what a cell does when they are unavailable, and whether a restriction that tight breaks a toolchain that writes where we did not expect.

Raised by the pre-release security review.

Activity

  1. flavorjones commented on Aug 18, 2026

    @flavorjones
    MemberAuthor

    Detection was built and withdrawn. Recording why, so it is not rebuilt.

    The supervisor recorded each socket's inode at boot and re-checked both in the run loop, stopping the cell on a change or an unlink. wait_for was capped at one second, because a stolen listener never becomes readable and an event-driven loop would never wake to notice.

    An attacker evades it. link preserves the inode under a second name, so the theft does not have to destroy the original socket. Measured:

    supervisor binds, inode        223078
    attacker rebinds, inode        223079  (a check here catches it)
    traffic reaches the impostor:  true
    attacker restores, inode       223078  (matches: true)
    traffic reaches the supervisor: true
    

    The attacker links the socket aside, unlinks the name, binds an impostor, takes what arrives, then unlinks theirs and renames the saved link back. Any burst shorter than the check interval is invisible, and a shorter interval only shortens the burst. renameat2 with RENAME_EXCHANGE does the same thing atomically.

    It also cost more than it bought. An adversarial review found two further problems:

    • inode mapped every SystemCallError to nil, and the check then compared nil == nil and passed. One transient stat failure at boot, or an unlink landing between the bind and the record, gave the cell a baseline that could never mismatch. The control would silently stop existing.
    • During an overlapping restart, two cells share the volume. The new one binds, the old one sees a changed inode and stops, and its shutdown unlinks whatever now holds both names, which is the new cell's sockets. A restart cascade on an ordinary deploy.
    • File.stat has no timeout and follows symlinks, so a worker could aim it at a hung mount and park pid 1 while every in-flight deadline went unenforced.

    So there is no cheap detection worth having, and the gap is stated in docs/DESIGN.md under "Worker isolation" instead. Landlock remains the one prevention that fits an unprivileged container.

  2. flavorjones commented on Aug 21, 2026

    @flavorjones
    MemberAuthor

    Landlock closes this. Everything below is measured against hotcell:example run with --cap-drop ALL --security-opt no-new-privileges:true --network none, and it is enough to implement from.

    The kernel reports Landlock ABI 9. Docker's default seccomp profile permits the syscalls, and
    no-new-privileges is no obstacle: Landlock requires PR_SET_NO_NEW_PRIVS and the gem sets it. That
    answers the seccomp and kernel-floor questions above.

    What a write-class ruleset denies

    Handle the write rights, grant them only on the slot directory, and never name the socket directory:

    Attempt Result
    worker unlinks work.sock EACCES
    worker binds an impostor socket EACCES
    tool unlinks work.sock after execve EACCES
    setsid grandchild unlinks work.sock EACCES

    A child inherits the ruleset across fork and execve, so the escape at Supervisor#become_worker stops
    being a route to this. The escapee still leaves the process group and still runs untimed until
    pids-limit ends it. It no longer holds a stolen listener.

    connect to work.sock stays allowed. Landlock does not mediate pathname AF_UNIX connections. Only the
    unlink and the re-bind are denied.

    Reads and descriptor writes stay outside the handled mask:

    Operation Result
    write through an already-open descriptor allowed
    read-reopen of source.fd_path allowed
    write-reopen of destination.fd_path EACCES

    The last row drives prerequisite 1. The kernel resolves /dev/fd/N to the backing file and checks that
    file's own path ancestry. In deployment that file is in the application container's mount tree, which the
    cell cannot name, so no allowlist can permit the write.

    The dependency

    discourse/ruby-landlock 0.4.1, MIT. It is a native
    extension with no precompiled binaries, so the cell image compiles it. It goes in
    hotcell-server.gemspec and in hotcell-client/lib/hot_cell/install/Gemfile.tt.

    ext/landlock/extconf.rb probes linux/landlock.h without aborting, so the build may succeed off Linux.
    Check that it builds on Darwin before making the dependency unconditional, and gate it by platform if not.
    Landlock.abi_version returns 0 where the syscall is absent and Landlock.supported? returns false, so
    the runtime degrades on its own.

    Change 1: write confinement

    In Supervisor#become_worker, after Process.setpgid 0, 0. The child is single-threaded there, which is
    what Landlock.restrict! requires.

    WRITE_CLASS = %i[write_file truncate make_reg make_dir make_sock make_fifo
                     make_sym remove_file remove_dir refer].freeze
    
    Landlock.restrict! paths: [ { path: File.dirname(slot.home), rights: WRITE_CLASS } ]

    make_sock denies the impostor bind and remove_file denies the unlink. Rights outside the mask are
    unrestricted, so reads and execve are untouched.

    Four things that will otherwise cost a day:

    • Use paths:, never write:. Landlock::WRITE_RIGHTS contains read_file and read_dir, so the
      write: keyword puts reads into the handled mask and denies them across the filesystem. Only paths:
      with an explicit rights list builds a write-only mask.
    • Anchor on File.dirname(slot.home), which is <workspace>/<n>. A rule binds an inode, and
      Slot#make_home creates a new one for every request, so a rule on slot.home is dead after the first
      request. The parent also covers the home.discarded-* trees Slot#sweep removes.
    • Supervisor#prepare_directories does not create <workspace>/<n>. Native.add_path_rule opens the
      path, so add a mkdir_p there.
    • refer is masked out below ABI 2, which denies cross-directory renames. Every rename here is
      same-directory, so ABI 1 is still enough.

    Change 2: verify_landlock!

    In Supervisor, beside verify_ptrace_scope! and called from the same place. Copy its split: raise
    ConfigurationError on Linux when Landlock.abi_version is 0, and log a warning elsewhere. This is the
    answer to "what a cell does when they are unavailable". Record the new precondition next to the
    ptrace_scope one in docs/DEPLOYMENT.md.

    Change 3: scope: [:signal]

    A separate restrict! call, guarded on Landlock.abi_version >= 6. Policy#scope_for raises
    UnsupportedError below ABI 6, so folding it into change 1 would fail the whole policy on kernels 5.13
    to 6.11.

    It closes the accepted risk at Supervisor#trap_signals:

    Direction Result
    worker to supervisor EPERM
    worker to sibling worker EPERM
    worker to its own tool allowed
    supervisor to restricted worker allowed

    The last two rows are the safety argument: a worker keeps control of its own tools, and the deadline kill
    still reaches the worker.

    Half of that risk is not denial of service. A worker can SIGSTOP a sibling and hold that request's
    staged files on the tmpfs, which widens the window docs/DESIGN.md relies on when it bounds file exposure
    to requests in flight.

    Prerequisite 1: stage the ffmpeg poster frame

    previewers/video/ffmpeg.rb writes through destination.fd_path, which the table above shows is denied.
    Move it to the staged pattern Previewers::Pdf uses. Three edits:

    1. Pass destination.path(extension: "jpg") as the output argument in place of destination.fd_path.
    2. Call destination.adopt destination.path(extension: "jpg") after run!.
    3. Replace bytes: produced!(destination.to_io.stat.size, "ffmpeg"). A staged output does not reach the
      descriptor until Output#post, which runs later in Worker#perform, so that expression returns 0 and
      produced! raises UnreadableDocument. Measure the staged file.

    Drop destination.to_io from pass:; only the source descriptor is still needed. The comment above
    run! argues for the direct write and says a staged output leaves the caller's file empty on a mid-write
    failure. Staging is the behaviour now, so rewrite it.

    Prerequisite 2: set TMPDIR

    Operation#tool_environment sets no TMPDIR, so ImageMagick's pixel cache and libvips vips_tmpfile
    spill into /tmp. Point TMPDIR at the slot home.

    Do not grant /tmp in the ruleset instead. Supervisor defaults workspace to
    File.join(Dir.tmpdir, "hotcell-workspace"), so granting /tmp restores cross-slot writes.

    MagickOperation needs separate handling. mini_magick spawns magick itself rather than going through
    run_tool, so tool_environment never reaches it. MiniMagick.cli_env and MiniMagick.tmpdir are the
    levers, and neither is set for this today.

    Prerequisite 3: cover the spill paths

    Twenty test files boot a real cell through HotCell::TestCell and drive the real toolchain, so a missing
    grant on any exercised path fails loudly and needs no new test.

    Spills are the exception, because they are size-dependent branches. The largest fixture is
    activestorage-hotcell-server/test/fixtures/large.png at 962KB and 400x400, far below any memory limit
    that pushes the pixel cache to disk. Force the branch instead of adding a large fixture: a low
    MAGICK_MEMORY_LIMIT for ImageMagick, and VIPS_DISC_THRESHOLD for libvips.
    magick_environment_test.rb already drives a booted cell with ImageMagick environment variables set, and
    is the model to copy.

    Do this whether or not the rest lands. We do not currently test where these tools write intermediate
    files.

    Reproducing any of the above

    docker run --rm --cap-drop ALL --security-opt no-new-privileges:true --network none \
      -v <ruby-landlock>/lib:/gem/lib:ro hotcell:example ruby -I/gem/lib -e '<probe>'
    

    For the destination.fd_path denial the backing file must sit in a mount the policy never grants.
    Bind-mount a host directory and open the file there.

    Deferred: the read side

    Handling read rights as well closes both residuals docs/DESIGN.md records under worker isolation.
    Measured: a sibling slot's files and /proc/self/environ both return EACCES. That reaches what the
    design calls unfixable, and it beats hidepid=2, which needs CAP_SYS_ADMIN.

    It also denies every source.fd_path re-open, for the reason under the second table, so every input would
    have to be staged — which puts large inputs back on the tmpfs under RLIMIT_FSIZE. Separate decision, and
    not a default.

    Correction to the issue text

    There is no Supervisor#verify_sockets. docs/DESIGN.md records that the inode check was tried and
    withdrawn, because an attacker hard-links the original aside, serves from an impostor, and renames it back.

  3. flavorjones commented on Sep 29, 2026

    @flavorjones
    MemberAuthor

    Findings from #23, which is closed into this. In #23 a worker replaces its slot directory with a symlink to a sibling's, and every later request on either slot can fail until the cell restarts.

    Measured with change 1's rights list on Linux 7.2.6, Landlock ABI 10:

    Attempt No Landlock Grant on <workspace>/0 Grant on <workspace>
    create a home in its own slot ok ok ok
    rename a sibling's home within <workspace>/1 ok EACCES ok
    rename its own slot directory aside ok EACCES ok
    open /dev/null for writing ok EACCES EACCES
    open /dev/null for writing, with a write_file + truncate rule on it ok ok ok

    Recommendations:

    1. Keep the anchor on File.dirname(slot.home), and test it. Granting the whole workspace reopens A worker can replace its slot directory, and make_home follows the replacement #23. Add tests that rename the slot directory aside and rename a sibling's home, so that widening the grant fails them.
    2. Grant write_file and truncate on /dev/null. Nothing in hotcell-server opens it after the fork: Log.null opens it in the supervisor, and the worker inherits the descriptor. A tool that redirects output to /dev/null would fail with EACCES, though. The table's last row shows the rule allows it.
    3. Drop prerequisite 2. The worker already sets TMPDIR to the home (worker.rb), and MagickOperation forwards it to magick (CHANGELOG.md).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions