Bug 280809 - jail_attach(2) fails to document reason for EPERM
Summary: jail_attach(2) fails to document reason for EPERM
Status: Closed FIXED
Alias: None
Product: Documentation
Classification: Unclassified
Component: Manual Pages (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Many People
Assignee: Olivier Certner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-08-13 20:23 UTC by Karlo Miličević
Modified: 2025-10-10 17:16 UTC (History)
4 users (show)

See Also:


Attachments
Documents this behavior in lib/libc/sys/jail.2 (498 bytes, patch)
2024-08-13 20:23 UTC, Karlo Miličević
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Karlo Miličević 2024-08-13 20:23:24 UTC
Created attachment 252746 [details]
Documents this behavior in lib/libc/sys/jail.2

In jail_attach(2) the only reason listed for EPERM is if the user not root while attempting to attach to a jail.
Running into that errno in my program I was puzzled because uid was 0 - so that could not have been the problem.
After help from other people I found out that there might be other, undocumented, reasons for getting that errno.

Specifically, the issue was that the process was holding a directory FD open while trying to attach, which is prohibited.

A sentence describing this behavior should be added to "[EPERM]" in jail_attach(2) "ERRORS" section.
Comment 1 Olivier Certner freebsd_committer freebsd_triage 2024-08-13 20:51:55 UTC
Funnily, I stumbled into the exact same problem today in a different context...

That behavior is very indirectly documented through the paragraph:
> Further jail(), jail_set(), and jail_attach() call chroot(2) internally,
> so they can fail for all the same reasons.  Please consult the chroot(2)
> manual page for details.

I agree that the jail_attach(2) manpage should mention the other "common" reason for getting EPERM (another reason is for the attach to be rejected by the MAC framework, and there are still potentially others).
Comment 2 Olivier Certner freebsd_committer freebsd_triage 2024-08-13 21:45:07 UTC
(Just to avoid giving you false hopes with my prompt response, I'll be AFK for the next two weeks, so don't expect a prompt response from me.  But if nobody beats me to it, I'll handle it in September.)
Comment 3 crest 2024-08-19 11:15:23 UTC
There is the kern.chroot_allow_open_directories sysctl to allow chroot() with directory file descriptors, but no such "backdoor" exists for jail_attach(), because jails are supposed to provide secure namespace isolation. Removing this restriction would allow trivial jail escapes through fchdir() and openat().

Allowing jailed processes to safely share directory file descriptors either through inheritance or file descriptor passing could be very useful, but is totally unsafe right now. Sharing directory file descriptors through inheritance across jails is (and always has been) impossible because jail_attach() doesn't allow processes with having one or more such file descriptors to attach to a jail. Despite this it's possible to share file descriptors across jails via file descriptor passing over unix domain sockets.

Passing directory file descriptors to a directory outside the receivers jail root into a jail is an instant file system escape because the jail root is enforced by comparing vnodes and if you `parent_fd = openat(dir_fd, "..", O_DIRECTORY)` you won't encounter the jail root vnode. By looping until "." == ".." the jailed process can find the hosts root filesystem. While the unix socket could be brought into a jail via jail_attach() the more likely real world situation would be intentionally sharing a nullfs mount across jails (e.g. a PostgreSQL jail <-> a FastCGI jail).

One way I can think of to make it safe would be to add a O_RESOLVE_BENEATH like write-once flag to file descriptors and allow only directory file descriptors with this flag to be shared across jails. It would have to be inherited too (e.g. openat(dir_fd, "<subdir>", O_DIRECTORY) on a sub-directory). If the flag could be automatically set by jail_attach() and file descriptor passing across jails would work without extra syscalls to set the new flag with the caveat that ".." inaccessible on such file descriptors and those derived from them.
Comment 4 Karlo Miličević 2024-08-26 18:47:25 UTC
(In reply to Olivier Certner from comment #1)

Ah! I totally missed that paragraph. I guess I should read more carefully.

(In reply to crest from comment #3)

Could you add "root vnode pointer" to every directory FD to limit their scope?
That way, when you reference ".." you would check whether the directory FD equals that pointer and if so, not go above.
Also, O_RESOLVE_BENEATH would then just mean that when you open that directory FD you would make the directory itself be the "root vnode pointer" instead of its jail/chroot root directory.
Opening directories with openat copies the "root vnode pointer" unless overriden by something like O_RESOLVE_BENEATH.

((I have no experience with VFS code, so take this idea with a grain of salt!))

This reminds me slightly of how sockets have vnet pointers.


Should I close this issue or does someone else do that?
The reason is documented already, as stated by Olivier Cartner.
Comment 5 commit-hook freebsd_committer freebsd_triage 2025-09-15 09:30:55 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ef2f36be8fce8cf7a62ce7d33ab1c4a181fac3e2

commit ef2f36be8fce8cf7a62ce7d33ab1c4a181fac3e2
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-09-01 09:31:23 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-09-15 09:29:44 +0000

    jail.2: Mention EPERM is returned on open directories

    The manual page does not directly mention this reason for getting EPERM,
    instead referring the reader to chroot(2).  We have had some questions
    about it recently, in part due to a bug (fixed), and this case is not an
    obvious permission/problem, so let's be more explicit.

    PR:             280809
    Reviewed by:    jamie
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52319

 lib/libsys/jail.2 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 6 Olivier Certner freebsd_committer freebsd_triage 2025-09-15 09:33:34 UTC
Will MFC it (stable/15, stable/14) in a few days.  Closing.
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-09-18 08:53:05 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7140f620a5695b023e76e482dab50133479f0a0b

commit 7140f620a5695b023e76e482dab50133479f0a0b
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-09-01 09:31:23 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-09-18 08:50:31 +0000

    jail.2: Mention EPERM is returned on open directories

    The manual page does not directly mention this reason for getting EPERM,
    instead referring the reader to chroot(2).  We have had some questions
    about it recently, in part due to a bug (fixed), and this case is not an
    obvious permission/problem, so let's be more explicit.

    PR:             280809
    Reviewed by:    jamie
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52319

    (cherry picked from commit ef2f36be8fce8cf7a62ce7d33ab1c4a181fac3e2)

 lib/libsys/jail.2 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Comment 8 crest 2025-09-18 12:01:25 UTC
Afaik the easily exploitable bug you mentioned is not (yet) fixed anywhere but 15-current.
Comment 9 Olivier Certner freebsd_committer freebsd_triage 2025-09-18 12:39:24 UTC
(In reply to crest from comment #8)

The bug I'm talking about was a pam_xdg(8)'s file descriptor leak, which is fixed everywhere supported, and not at all what you seem to have in mind.  You're basically jumping on your own first comment, which is off-topic for this bug.  

Additionally, you're wrong: https://reviews.freebsd.org/D50371 was merged into stable/14 also.  Could you please do a minimum of research before making such claims (you intervened on this very Phab revision)? If you have something more to say on your specific topic, the proper bug is PR 262179, as you should know.
Comment 10 commit-hook freebsd_committer freebsd_triage 2025-10-10 17:16:47 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6e888a22743f67d3b130dd0a9b9849bcee160d2f

commit 6e888a22743f67d3b130dd0a9b9849bcee160d2f
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-09-01 09:31:23 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-10-10 17:15:55 +0000

    jail.2: Mention EPERM is returned on open directories

    The manual page does not directly mention this reason for getting EPERM,
    instead referring the reader to chroot(2).  We have had some questions
    about it recently, in part due to a bug (fixed), and this case is not an
    obvious permission/problem, so let's be more explicit.

    PR:             280809
    Reviewed by:    jamie
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52319

    (cherry picked from commit ef2f36be8fce8cf7a62ce7d33ab1c4a181fac3e2)

 lib/libc/sys/jail.2 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)