sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

max_open_files is accepted everywhere but never applied

Open dzerik opened this issue 4 months ago • 1 comments

max_open_files is accepted by the builder, the CLI, the C ABI, Python and Go — and never applied. RLIMIT_NOFILE appears nowhere under crates/.

Reproduction

Verified on main (0.8.5, x86-64).

$ sandlock run -r /usr -r /lib -r /lib64 -r /bin -- /bin/sh -c 'ulimit -n'
524288

$ sandlock run -r /usr -r /lib -r /lib64 -r /bin --max-open-files 10 -- /bin/sh -c 'ulimit -n'
524288

$ ulimit -n     # the host, for comparison
524288

The flag is accepted without complaint and changes nothing. Going further — asking for two hundred descriptors under a stated cap of ten:

$ sandlock run -r /usr -r /lib -r /lib64 -r /bin -r /etc --max-open-files 10 -- \
    /usr/bin/python3 -c "
import resource
print('RLIMIT_NOFILE inside the sandbox:', resource.getrlimit(resource.RLIMIT_NOFILE))
fds = [open('/etc/hostname') for _ in range(200)]
print('descriptors opened:', len(fds))
"
RLIMIT_NOFILE inside the sandbox: (524288, 524288)
descriptors opened: 200

Trace

The value flows all the way through and then stops:

  • crates/sandlock-core/src/sandbox/builder.rs:112 — field
  • crates/sandlock-core/src/sandbox/builder.rs:482 — setter
  • crates/sandlock-core/src/sandbox/builder.rs:869 — copied into the sandbox
  • crates/sandlock-core/src/sandbox.rs:443, :579 — carried
  • crates/sandlock-core/src/profile.rs:286 — settable from a profile
$ rg -n 'RLIMIT_NOFILE|NOFILE' crates/
(no matches)

Every other limit in the group reaches an enforcement point; this one has none.

Why it matters more than a no-op usually would

The Go SDK documents it as enforced, go/sandbox.go:243:

MaxOpenFiles uint32   // RLIMIT_NOFILE; 0 = inherit system default

A caller who sets it believes a descriptor limit is in place. Since max_open_files reads as a hardening control, a silent no-op is worse than an absent field: the sandbox looks more constrained than it is.

Why the tests do not catch it

Nothing asserts the limit takes effect — the same shape as the restrict_max_processes and restrict_max_memory gaps closed in #104 by adding tests that verify enforcement rather than acceptance. A test in that style (open descriptors past the cap, expect EMFILE) would have caught this; the reproduction above is essentially that test.

Options

  1. Implement it — setrlimit(RLIMIT_NOFILE) in the child before exec, next to where the other limits land.
  2. Remove the field and the flag, and note it in the release notes.
  3. Keep it and document it as accepted-but-unenforced.

I would go with (1), and would rather not leave (3) standing for something that reads as a hardening control. Either way the Go doc comment needs correcting.

Happy to take this if you want it implemented.

dzerik avatar Jul 29 '26 13:07 dzerik

Yes, please implement it. Thank you.

congwang-mk avatar Jul 29 '26 22:07 congwang-mk