async-signal-safety of chroot
Hi everyone,
Is chroot async-signal-safe?
It is a somewhat popular pattern to fork, chroot, then exec. However, only AS-safe functions may be called between fork and exec (stated at [1]) but the manual entry for chroot makes no such mention [2]. signal-safety(7) [3] only covers POSIX, so it is also not listed there.
The rather old POSIX edition that last specified chroot also does not mark it AS-safe [4], [5].
Is code using this pattern buggy, or is it just missing documentation? I expect it's the latter but it would be nice to confirm either way.
Thanks,
Trevor
[1]: https://man7.org/linux/man-pages/man2/fork.2.html
[2]: https://man7.org/linux/man-pages/man2/chroot.2.html
[3]: https://man7.org/linux/man-pages/man7/signal-safety.7.html
[4]: https://pubs.opengroup.org/onlinepubs/7908799/xsh/chroot.html
[5]: https://pubs.opengroup.org/onlinepubs/7908799/xsh/sigaction.html
Quoted 10 lines · On Wed, Sep 23, 2026 at 12:53:16AM +0000, [email protected] wrote:
Hi everyone,
Is
chrootasync-signal-safe?It is a somewhat popular pattern to
fork,chroot, thenexec. However, only AS-safe functions may be called betweenforkandexec(stated at [1]) but the manual entry forchrootmakes no such mention [2]. signal-safety(7) [3] only covers POSIX, so it is also not listed there.
Quoted 5 lines
The rather old POSIX edition that last specified chroot also does not mark it AS-safe [4], [5].
Is code using this pattern buggy, or is it just missing documentation? I expect it's the latter but it would be nice to confirm either way.
The restriction to only using AS-safe functions after fork only applies in multi-threaded processes. So unless your process is multi-threaded, that's a non-issue.
Since it's no longer standard, there is no standard governing chroot, but in practice on most systems, including Linux/musl, chroot is going to be AS-safe because it's just a single syscall.
Also, both glibc and musl relax the restriction on only calling AS-safe functions in the child after a multi-threaded parent forks. In glibc this is sort of a best-effort thing, but in musl we went through a detailed process of ensuring that everything is made consistent for the child. The only thing that might not be consistent is dynamic linker state, which might make it impossible to dlopen in the child, but this is treated as a reportable error condition not a crash or deadlock or anything.
The changes to lift the restrictions on the child happened in commit 167390f05564e0a4d3fcb4329377fd7743267560
Rich
Quoted 1 line · Rich Felker
Since it's no longer standard, there is no standard governing chroot,
On this claim only, I believe you're technically mistaken: ISO/IEC 23360-1-2:2021 (Linux Standard Base: Core specification generic part) is a current International Standard. It incorporates the definition of chroot() from SUSv2 by reference without amendment, so the same async-signal-safety limitations apply.
Quoted 8 lines · John Scott
Quoted 1 line · Rich Felker
Since it's no longer standard, there is no standard governing chroot,
On this claim only, I believe you're technically mistaken: ISO/IEC 23360-1-2:2021 (Linux Standard Base: Core specification generic part) is a current International Standard. It incorporates the definition of
chroot()from SUSv2 by reference without amendment, so the same async-signal-safety limitations apply.
Linux Standard Base is not relevant. It is not a standard musl aims to conform to, and it is largely an ad hoc third party description of a very old version of the glibc ABI and its integration into Red Hat Linux.
* Rich Felker:
Quoted 10 lines · John Scott
Quoted 8 lines · Rich Felker
Quoted 1 line
Since it's no longer standard, there is no standard governing chroot,
On this claim only, I believe you're technically mistaken: ISO/IEC 23360-1-2:2021 (Linux Standard Base: Core specification generic part) is a current International Standard. It incorporates the definition of
chroot()from SUSv2 by reference without amendment, so the same async-signal-safety limitations apply.
Quoted 4 lines
Linux Standard Base is not relevant. It is not a standard musl aims to conform to, and it is largely an ad hoc third party description of a very old version of the glibc ABI and its integration into Red Hat Linux.
And glibc does not aim to conform to Linux Standard Base, either. Only very few architectures meet the symbol version requirements in ISO/IEC 23360-1-2:2021. Any architecture that was added after glibc 2.3 is not compliant (not even with the generic part). This is a defect in the standard.
Thanks,
Florian
Quoted 5 lines · Rich Felker
On Wed, Sep 23, 2026 at 12:53:16AM +0000, [email protected] wrote:
Since it's no longer standard, there is no standard governing chroot, but in practice on most systems, including Linux/musl, chroot is going to be AS-safe because it's just a single syscall.
Thanks Rich and others, that aligns with what I would have expected. In that case this is a documentation request: I have a patch to the manual, will send it in reply to this thread for any feedback.
Quoted 11 lines
Also, both glibc and musl relax the restriction on only calling AS-safe functions in the child after a multi-threaded parent forks. In glibc this is sort of a best-effort thing, but in musl we went through a detailed process of ensuring that everything is made consistent for the child. The only thing that might not be consistent is dynamic linker state, which might make it impossible to dlopen in the child, but this is treated as a reportable error condition not a crash or deadlock or anything.
The changes to lift the restrictions on the child happened in commit
167390f05564e0a4d3fcb4329377fd7743267560
That's great to learn about. I'll include a patch to document this as well.
- Trevor
Quoted 10 lines · Rich Felker
Also, both glibc and musl relax the restriction on only calling AS-safe functions in the child after a multi-threaded parent forks. In glibc this is sort of a best-effort thing, but in musl we went through a detailed process of ensuring that everything is made consistent for the child. The only thing that might not be consistent is dynamic linker state, which might make it impossible to dlopen in the child, but this is treated as a reportable error condition not a crash or deadlock or anything.
Quoted 4 lines
The changes to lift the restrictions on the child happened in commit
167390f05564e0a4d3fcb4329377fd7743267560Rich
A followup question: the usual wisdom has been that fork must always be followed by an exec, and not doing so is somewhere between error-prone and completely unsound. Based per the commit it sounds like there are no more deadlocks, and without the POSIX AS-safe requirement, the soundness question goes away. Does this mean that it is now considered correct and reasonable code to fork without exec for the purpose of e.g. invoking a function in a separate process? Similar to what can be done with clone.
Of course that case may be better served by a way to re-exec the same process for clean state without relying on the exact same hacks, but that unfortunately doesn't exist.
- Trevor
Quoted 11 lines · Trevor Gross
Quoted 10 lines · Rich Felker
Also, both glibc and musl relax the restriction on only calling AS-safe functions in the child after a multi-threaded parent forks. In glibc this is sort of a best-effort thing, but in musl we went through a detailed process of ensuring that everything is made consistent for the child. The only thing that might not be consistent is dynamic linker state, which might make it impossible to dlopen in the child, but this is treated as a reportable error condition not a crash or deadlock or anything.
Quoted 6 lines
Quoted 2 lines
The changes to lift the restrictions on the child happened in commit
167390f05564e0a4d3fcb4329377fd7743267560A followup question: the usual wisdom has been that
forkmust always be followed by anexec, and not doing so is somewhere between error-prone and completely unsound.
I would not say that is "usual wisdom" at all. Traditional unix practice was very much to take maximal advantage of fork. Lots of daemons run connections/sessions this way, shells implement subshells this way, etc.
I would say, however, that it's good practice not to fork except to exec, and to avoid that too (use posix_spawn instead) when possible. There are lots of pitfalls with keeping around a forked child, ranging from leaking potentially sensitive data you didn't intent to copy from the parent -- especially if dropping privileges in the child -- to logic bugs around sole ownership of a resource potentially becoming shared/multiple ownership. Plus there are the portability issues if anything in the parent is using threads, or if you want to port to systems where fork is slow, poorly emulated, or unavailable (Windows, MMU-less microcontrollers, etc.)
Quoted 5 lines
Based per the commit it sounds like
there are no more deadlocks, and without the POSIX AS-safe requirement, the soundness question goes away. Does this mean that it is now considered correct and reasonable code toforkwithoutexecfor the purpose of e.g. invoking a function in a separate process?
This works on musl and maybe mostly on glibc, but unless you know the parent is single-threaded, it's not portable. If you look back at the reason we made it work, it's because one or more interpreted languages (sorry I don't remember which one(s) at the moment) *wrongly assumed it works* and built their own language constructs (exposed to applications written in those languages) around the assumption that it works.
Quoted 2 lines
Similar to
what can be done withclone.
Generally clone is far more dangerous than fork in this regard. There is no formal spec for how it behaves, and you are likely stuck with the same or tighter AS-safety restrictions than fork would have.
musl makes clone roughly as safe as fork (see commit fa4a8abd06a4), and disallows flags that are not compatible with executing any part of libc in the child, but afaik glibc still has an anything-goes approach.
Rich
* Rich Felker:
Quoted 4 lines
musl makes clone roughly as safe as fork (see commit
fa4a8abd06a4), and disallows flags that are not compatible with executing any part of libc in the child, but afaik glibc still has an anything-goes approach.
Yes, there is no filtering in glibc, and no update of the (shared) TCB. Several web browsers longjmp out of the clone callback and patch the TCB with the new TID, so that mutexes work as expected.
Thanks,
Florian
PATCH 1/2 man/man2/chroot.2: Document that chroot is AS-safe
In reply to [email protected]As discussed at [1], this is an async-signal-safe syscall wrapper.
--- man/man2/chroot.2 | 4 ++++ man/man7/signal-safety.7 | 10 ++++++++++ 2 files changed, 14 insertions(+)
man/man2/chroot.2 +4 -0
@@ -150,6 +150,10 @@ inherits its parent's root directory. The root directory is left unchanged by .BR execve (2). .P +.BR chroot() +is async-signal-safe: +it may be safely called within a signal handler. +.P The magic symbolic link, .IR /proc/ pid /root , can be used to discover a process's root directory;
man/man7/signal-safety.7 +10 -0
@@ -280,6 +280,16 @@ T} \f[B]write\f[](2) .TE .P +Functions listed in the following table are not required to be +async-signal-safe by POSIX, but are safe on Linux: +.P +.TS +lb +lw(24n) l. +Function +\f[B]chroot\f[](2) +.TE +.P Notes: .IP \[bu] 3 POSIX.1-2001 and POSIX.1-2001 TC2 required the functions
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH 2/2 man/man2/fork.2: Document glibc and musl relaxed fork->exec AS requirements
In reply to [email protected]--- man/man2/fork.2 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
man/man2/fork.2 +9 -2
@@ -147,10 +147,17 @@ may be helpful for dealing with problems that this can cause. After a .BR fork () in a multithreaded program, -the child can safely call only async-signal-safe functions (see +POSIX specifies that the child can safely call only async-signal-safe +functions (see .BR signal\-safety (7)) -until such time as it calls +until it calls .BR execve (2). +On glibc and musl libc, this is relaxed; +it is safe to call non-async-signal-safe functions in the time between +.BR fork () +and +.BR exec () +\&. .IP \[bu] The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the same
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [musl] [PATCH 2/2] man/man2/fork.2: Document glibc and musl relaxed fork->exec AS requirements
Quoted 33 lines · Trevor Gross
Signed-off-by Trevor Gross <[email protected]>--- man/man2/fork.2 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)man/man2/fork.2 +9
-2@@ -147,10 +147,17 @@ may be helpful for dealing with problems that this can cause. After a .BR fork () in a multithreaded program, -the child can safely call only async-signal-safe functions (see +POSIX specifies that the child can safely call only async-signal-safe +functions (see .BR signal\-safety (7)) -until such time as it calls +until it calls .BR execve (2). +On glibc and musl libc, this is relaxed; +it is safe to call non-async-signal-safe functions in the time between +.BR fork () +and +.BR exec () +\&. .IP \[bu] The child inherits copies of the parent's set of open file descriptors. Each file descriptor in the child refers to the sameSignature · 1 line
2.50.1 (Apple Git-155)
While glibc has taken measures to make this mostly work, especially malloc-after-fork, I don't think glibc has an intentional model for ensuring that arbitrary libc functions work right in the child of a multithreaded fork. Before accepting such a man page patch, we should at least get an official position from glibc on whether they claim this property or at least aim for it.
Rich
Re: [musl] [PATCH 2/2] man/man2/fork.2: Document glibc and musl relaxed fork->exec AS requirements
* Rich Felker:
Quoted 5 lines
Quoted 5 lines
man/man2/fork.2 +5
-0+On glibc and musl libc, this is relaxed; +it is safe to call non-async-signal-safe functions in the time between +.BR fork () +and +.BR exec ()
Quoted 8 lines
Quoted 1 line
+\&.
While glibc has taken measures to make this mostly work, especially malloc-after-fork, I don't think glibc has an intentional model for ensuring that arbitrary libc functions work right in the child of a multithreaded fork. Before accepting such a man page patch, we should at least get an official position from glibc on whether they claim this property or at least aim for it.
Far from everything in glibc is fork-protected, so the statement is not true today as far as glibc is concerned. We try to maintain compatibility with existing application requirements, but the precise goals have not been written down.
Thanks,
Florian
Re: [musl] [PATCH 2/2] man/man2/fork.2: Document glibc and musl relaxed fork->exec AS requirements
Quoted 16 lines · Florian Weimer
* Rich Felker:
Quoted 5 lines
Quoted 5 lines
man/man2/fork.2 +5
-0+On glibc and musl libc, this is relaxed; +it is safe to call non-async-signal-safe functions in the time between +.BR fork () +and +.BR exec ()Quoted 8 lines
Quoted 1 line
+\&.
While glibc has taken measures to make this mostly work, especially malloc-after-fork, I don't think glibc has an intentional model for ensuring that arbitrary libc functions work right in the child of a multithreaded fork. Before accepting such a man page patch, we should at least get an official position from glibc on whether they claim this property or at least aim for it.
Quoted 4 lines
Far from everything in glibc is fork-protected, so the statement is not true today as far as glibc is concerned. We try to maintain compatibility with existing application requirements, but the precise goals have not been written down.
Would the following be more accurate?
On glibc and musl libc, this is relaxed. With musl environments, it is safe to call non-async-signal-safe functions in the time between fork() and exec(). With glibc environments, some functions that are not AS-safe may be called between fork() and exec() may be safe to call; however, there is no exact list maintained.
Open to better wording as well, I'm not sure sure how best to express the glibc case - if that's worth it at all.
Rich, is this reasonable to document for musl? It is a rather helpful property even if not portable, but I'm not sure whether it is more of an implementation detail for improved compatibility vs. something musl is comfortable with users relying on.
- Trevor
Re: [musl] [PATCH 2/2] man/man2/fork.2: Document glibc and musl relaxed fork->exec AS requirements
Quoted 19 lines · Trevor Gross
Quoted 14 lines · Florian Weimer
* Rich Felker:
Quoted 4 lines
Quoted 4 lines
man/man2/fork.2 +4
-0+On glibc and musl libc, this is relaxed; +it is safe to call non-async-signal-safe functions in the time between +.BR fork () +andQuoted 8 lines
Quoted 2 lines
+.BR exec ()
+\&.While glibc has taken measures to make this mostly work, especially malloc-after-fork, I don't think glibc has an intentional model for ensuring that arbitrary libc functions work right in the child of a multithreaded fork. Before accepting such a man page patch, we should at least get an official position from glibc on whether they claim this property or at least aim for it.
Quoted 4 lines
Far from everything in glibc is fork-protected, so the statement is not true today as far as glibc is concerned. We try to maintain compatibility with existing application requirements, but the precise goals have not been written down.
Quoted 7 lines
Would the following be more accurate?
On glibc and musl libc, this is relaxed. With musl environments, it is safe to call non-async-signal-safe functions in the time between
fork()andexec(). With glibc environments, some functions that are not AS-safe may be called betweenfork()andexec()may be safe to call; however, there is no exact list maintained.
At the very least, it shouldn't duplicate the "may be safe to call" clause :p
Quoted 10 lines
Open to better wording as well, I'm not sure sure how best to express the glibc case - if that's worth it at all.
Rich, is this reasonable to document for musl? It is a rather helpful property even if not portable, but I'm not sure whether it is more of an implementation detail for improved compatibility vs. something musl is comfortable with users relying on.
- Trevor