[13/23] linux: Add fchroot
Checks
| Context |
Check |
Description |
| redhat-pt-bot/TryBot-apply_patch |
success
|
Patch applied to master at the time it was sent
|
| linaro-tcwg-bot/tcwg_glibc_build--master-arm |
fail
|
Patch failed to apply
|
| linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 |
fail
|
Patch failed to apply
|
Commit Message
The fchroot system call (commit
79d27fd718545db4731691e0a77e51d27dc1a41e, Linux 7.3) changes the root
directory of the calling process to the directory referred to by a
file descriptor. The fd argument can also be FD_FAILFS_ROOT, in which
case all subsequent absolute file name lookups fail, unprivileged
processes can do this if no_new_privs is set and the file system
information is not shared with other threads (so it fails with EINVAL
in a multithreaded process created with pthread_create).
The wrapper follows the kernel signature:
int fchroot (int fd, unsigned int flags);
Checked on x86_64-linux-gnu.
---
NEWS | 3 +
sysdeps/unix/sysv/linux/Makefile | 1 +
sysdeps/unix/sysv/linux/Versions | 1 +
sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arc/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 +
sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 +
sysdeps/unix/sysv/linux/bits/unistd_ext.h | 6 +
sysdeps/unix/sysv/linux/csky/libc.abilist | 1 +
sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 +
sysdeps/unix/sysv/linux/i386/libc.abilist | 1 +
.../sysv/linux/loongarch/ilp32/libc.abilist | 1 +
.../sysv/linux/loongarch/lp64/libc.abilist | 1 +
.../sysv/linux/m68k/coldfire/libc.abilist | 1 +
.../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 +
.../sysv/linux/microblaze/be/libc.abilist | 1 +
.../sysv/linux/microblaze/le/libc.abilist | 1 +
.../sysv/linux/mips/mips32/fpu/libc.abilist | 1 +
.../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 +
.../sysv/linux/mips/mips64/n32/libc.abilist | 1 +
.../sysv/linux/mips/mips64/n64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/or1k/libc.abilist | 1 +
.../linux/powerpc/powerpc32/fpu/libc.abilist | 1 +
.../powerpc/powerpc32/nofpu/libc.abilist | 1 +
.../linux/powerpc/powerpc64/be/libc.abilist | 1 +
.../linux/powerpc/powerpc64/le/libc.abilist | 1 +
.../unix/sysv/linux/riscv/rv32/libc.abilist | 1 +
.../unix/sysv/linux/riscv/rv64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/s390/libc.abilist | 1 +
sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 +
sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 +
.../sysv/linux/sparc/sparc32/libc.abilist | 1 +
.../sysv/linux/sparc/sparc64/libc.abilist | 1 +
sysdeps/unix/sysv/linux/syscalls.list | 1 +
sysdeps/unix/sysv/linux/tst-fchroot.c | 114 ++++++++++++++++++
.../unix/sysv/linux/x86_64/64/libc.abilist | 1 +
.../unix/sysv/linux/x86_64/x32/libc.abilist | 1 +
38 files changed, 158 insertions(+)
create mode 100644 sysdeps/unix/sysv/linux/tst-fchroot.c
Comments
* Adhemerval Zanella:
> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> index a541b378d03..6597435ace7 100644
> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
> # define CLOSE_RANGE_CLOEXEC (1U << 2)
> #endif
>
> +/* Change the root directory of the calling process to the directory
> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
> + in which case all subsequent absolute file name lookups fail. FLAGS
> + must be zero. */
> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
> +
> #endif /* __USE_GNU */
CRIU defines _GNU_SOURCE, and it has:
criu/files.c:#include <unistd.h>
criu/files.c:static int fchroot(int fd)
criu/files.c-{
criu/files.c- /*
criu/files.c- * There's no such thing in syscalls. We can emulate
criu/files.c- * it using fchdir()
criu/files.c- */
criu/files.c-
criu/files.c- if (fchdir(fd) < 0) {
criu/files.c- pr_perror("Can't chdir to proc");
criu/files.c- return -1;
criu/files.c- }
criu/files.c-
criu/files.c- pr_debug("Going to chroot into /proc/self/fd/%d\n", fd);
criu/files.c- return chroot(".");
criu/files.c-}
So it breaks the build. I think that's true for
I wonder if we should start defining macros like
#define _LIBC_HAS_FUNCTION_fchroot
in some header file as part of a general push for more system call
wrappers.
Thanks,
Florian
On 05/11/26 09:26, Florian Weimer wrote:
> * Adhemerval Zanella:
>
>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> index a541b378d03..6597435ace7 100644
>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>> #endif
>>
>> +/* Change the root directory of the calling process to the directory
>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>> + in which case all subsequent absolute file name lookups fail. FLAGS
>> + must be zero. */
>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>> +
>> #endif /* __USE_GNU */
>
> CRIU defines _GNU_SOURCE, and it has:
>
> criu/files.c:#include <unistd.h>
>
> criu/files.c:static int fchroot(int fd)
> criu/files.c-{
> criu/files.c- /*
> criu/files.c- * There's no such thing in syscalls. We can emulate
> criu/files.c- * it using fchdir()
> criu/files.c- */
> criu/files.c-
> criu/files.c- if (fchdir(fd) < 0) {
> criu/files.c- pr_perror("Can't chdir to proc");
> criu/files.c- return -1;
> criu/files.c- }
> criu/files.c-
> criu/files.c- pr_debug("Going to chroot into /proc/self/fd/%d\n", fd);
> criu/files.c- return chroot(".");
> criu/files.c-}
>
> So it breaks the build. I think that's true for
I think you ended the sentence here.
>
> I wonder if we should start defining macros like
>
> #define _LIBC_HAS_FUNCTION_fchroot
>
> in some header file as part of a general push for more system call
> wrappers.
Please no, this adds another set of defines we will need to keep tracking
of.
For the CRIU, it would require to add a configure check. I don't think it
should be a blocked for this wrapper.
> Thanks,
> Florian
On Nov 05 2026, Florian Weimer wrote:
> * Adhemerval Zanella:
>
>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> index a541b378d03..6597435ace7 100644
>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>> #endif
>>
>> +/* Change the root directory of the calling process to the directory
>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>> + in which case all subsequent absolute file name lookups fail. FLAGS
>> + must be zero. */
>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>> +
>> #endif /* __USE_GNU */
>
> CRIU defines _GNU_SOURCE, and it has:
>
> criu/files.c:#include <unistd.h>
>
> criu/files.c:static int fchroot(int fd)
BSD also has fchroot without the flags. Should the one above be called
fchroot2, or should the flags be dropped for now?
On 05/11/26 11:31, Andreas Schwab wrote:
> On Nov 05 2026, Florian Weimer wrote:
>
>> * Adhemerval Zanella:
>>
>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> index a541b378d03..6597435ace7 100644
>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>> #endif
>>>
>>> +/* Change the root directory of the calling process to the directory
>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>> + must be zero. */
>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>> +
>>> #endif /* __USE_GNU */
>>
>> CRIU defines _GNU_SOURCE, and it has:
>>
>> criu/files.c:#include <unistd.h>
>>
>> criu/files.c:static int fchroot(int fd)
>
> BSD also has fchroot without the flags. Should the one above be called
> fchroot2, or should the flags be dropped for now?
>
This is annoying, but doable. We can add both fchroot as SYS_fchroot with
flags set to 0 and fchroot2 with Linux signature. I don't have a strong
opinion.
* Andreas Schwab:
> On Nov 05 2026, Florian Weimer wrote:
>
>> * Adhemerval Zanella:
>>
>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> index a541b378d03..6597435ace7 100644
>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>> #endif
>>>
>>> +/* Change the root directory of the calling process to the directory
>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>> + must be zero. */
>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>> +
>>> #endif /* __USE_GNU */
>>
>> CRIU defines _GNU_SOURCE, and it has:
>>
>> criu/files.c:#include <unistd.h>
>>
>> criu/files.c:static int fchroot(int fd)
>
> BSD also has fchroot without the flags.
So does Hurd.
> Should the one above be called fchroot2, or should the flags be
> dropped for now?
It won't fix the fchroot build failure. It's surprisingly rare, though:
<https://codesearch.debian.net/search?q=%28%5E%7Cint%5Cs*%29fchroot%5Cs*%5C%28&literal=0>
I just got lucky, I didn't find it through Debian Code Search, just by
consulting the CRIU source code.
I don't think we should aim for compatibility with Hurd/BSD if Linux has
something else. On the other hand, if POSIX standardizes fchroot in the
one-argument form, we'll have a problem.
Thanks,
Florian
* Adhemerval Zanella Netto:
>> I wonder if we should start defining macros like
>>
>> #define _LIBC_HAS_FUNCTION_fchroot
>>
>> in some header file as part of a general push for more system call
>> wrappers.
>
> Please no, this adds another set of defines we will need to keep tracking
> of.
If it's just a plain #define, I think we could generate that from the
DSO itself.
If we need to identify the header for the declaration or the argument
count, it's less straightforward to auto-generate, but still possible.
Thanks,
Florian
On 05/11/26 13:42, Florian Weimer wrote:
> * Andreas Schwab:
>
>> On Nov 05 2026, Florian Weimer wrote:
>>
>>> * Adhemerval Zanella:
>>>
>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> index a541b378d03..6597435ace7 100644
>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>> #endif
>>>>
>>>> +/* Change the root directory of the calling process to the directory
>>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>>> + must be zero. */
>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>> +
>>>> #endif /* __USE_GNU */
>>>
>>> CRIU defines _GNU_SOURCE, and it has:
>>>
>>> criu/files.c:#include <unistd.h>
>>>
>>> criu/files.c:static int fchroot(int fd)
>>
>> BSD also has fchroot without the flags.
>
> So does Hurd.
>
>> Should the one above be called fchroot2, or should the flags be
>> dropped for now?
>
> It won't fix the fchroot build failure. It's surprisingly rare, though:
What do you mean by it won't fix the build failure? We can provide both,
although it still be confusing that on Linux you have 2 functions.
>
> <https://codesearch.debian.net/search?q=%28%5E%7Cint%5Cs*%29fchroot%5Cs*%5C%28&literal=0>
>
> I just got lucky, I didn't find it through Debian Code Search, just by
> consulting the CRIU source code.
>
> I don't think we should aim for compatibility with Hurd/BSD if Linux has
> something else. On the other hand, if POSIX standardizes fchroot in the
> one-argument form, we'll have a problem.
>
> Thanks,
> Florian
* Adhemerval Zanella Netto:
> On 05/11/26 13:42, Florian Weimer wrote:
>> * Andreas Schwab:
>>
>>> On Nov 05 2026, Florian Weimer wrote:
>>>
>>>> * Adhemerval Zanella:
>>>>
>>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> index a541b378d03..6597435ace7 100644
>>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>>> #endif
>>>>>
>>>>> +/* Change the root directory of the calling process to the directory
>>>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>>>> + must be zero. */
>>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>>> +
>>>>> #endif /* __USE_GNU */
>>>>
>>>> CRIU defines _GNU_SOURCE, and it has:
>>>>
>>>> criu/files.c:#include <unistd.h>
>>>>
>>>> criu/files.c:static int fchroot(int fd)
>>>
>>> BSD also has fchroot without the flags.
>>
>> So does Hurd.
>>
>>> Should the one above be called fchroot2, or should the flags be
>>> dropped for now?
>>
>> It won't fix the fchroot build failure. It's surprisingly rare, though:
>
> What do you mean by it won't fix the build failure? We can provide both,
> although it still be confusing that on Linux you have 2 functions.
With the single-argument version, it's still a static definition folling
an extern declaration, which is a compiler error.
Thanks,
Florian
Florian Weimer <fweimer@redhat.com> writes:
> * Andreas Schwab:
>
>> On Nov 05 2026, Florian Weimer wrote:
>>
>>> * Adhemerval Zanella:
>>>
>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> index a541b378d03..6597435ace7 100644
>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>> #endif
>>>>
>>>> +/* Change the root directory of the calling process to the directory
>>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>>> + must be zero. */
>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>> +
>>>> #endif /* __USE_GNU */
>>>
>>> CRIU defines _GNU_SOURCE, and it has:
>>>
>>> criu/files.c:#include <unistd.h>
>>>
>>> criu/files.c:static int fchroot(int fd)
>>
>> BSD also has fchroot without the flags.
>
> So does Hurd.
And Solaris [1].
I have been wanting this for the 'chroot' command from GNU coreutils for
a while. If my memory serves me correctly, it is only available on
NetBSD and not the other BSDs. For that reason, I didn't bother making
use of it. I was not aware it was supported on Hurd, and only found out
about Solaris recently.
>> Should the one above be called fchroot2, or should the flags be
>> dropped for now?
>
> It won't fix the fchroot build failure. It's surprisingly rare, though:
>
> <https://codesearch.debian.net/search?q=%28%5E%7Cint%5Cs*%29fchroot%5Cs*%5C%28&literal=0>
>
> I just got lucky, I didn't find it through Debian Code Search, just by
> consulting the CRIU source code.
>
> I don't think we should aim for compatibility with Hurd/BSD if Linux has
> something else. On the other hand, if POSIX standardizes fchroot in the
> one-argument form, we'll have a problem.
Yes. I was pleasantly suprised to see the system call added in Linux
7.3. But I find it a bit annoying that a different function signature
than everyone else...
Collin
[1] https://docs.oracle.com/cd/E86824_01/html/E54765/fchroot-2.html
On 05/11/26 14:02, Florian Weimer wrote:
> * Adhemerval Zanella Netto:
>
>> On 05/11/26 13:42, Florian Weimer wrote:
>>> * Andreas Schwab:
>>>
>>>> On Nov 05 2026, Florian Weimer wrote:
>>>>
>>>>> * Adhemerval Zanella:
>>>>>
>>>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>>> index a541b378d03..6597435ace7 100644
>>>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>>>> #endif
>>>>>>
>>>>>> +/* Change the root directory of the calling process to the directory
>>>>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>>>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>>>>> + must be zero. */
>>>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>>>> +
>>>>>> #endif /* __USE_GNU */
>>>>>
>>>>> CRIU defines _GNU_SOURCE, and it has:
>>>>>
>>>>> criu/files.c:#include <unistd.h>
>>>>>
>>>>> criu/files.c:static int fchroot(int fd)
>>>>
>>>> BSD also has fchroot without the flags.
>>>
>>> So does Hurd.
>>>
>>>> Should the one above be called fchroot2, or should the flags be
>>>> dropped for now?
>>>
>>> It won't fix the fchroot build failure. It's surprisingly rare, though:
>>
>> What do you mean by it won't fix the build failure? We can provide both,
>> although it still be confusing that on Linux you have 2 functions.
>
> With the single-argument version, it's still a static definition folling
> an extern declaration, which is a compiler error.
Right, but on this case the usual expected way is a configure check if
the system library already provides it. Having this as a blocker will
make *any* new syscall wrappers really hard to justify.
On 05/11/26 14:26, Collin Funk wrote:
> Florian Weimer <fweimer@redhat.com> writes:
>
>> * Andreas Schwab:
>>
>>> On Nov 05 2026, Florian Weimer wrote:
>>>
>>>> * Adhemerval Zanella:
>>>>
>>>>> diff --git a/sysdeps/unix/sysv/linux/bits/unistd_ext.h b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> index a541b378d03..6597435ace7 100644
>>>>> --- a/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> +++ b/sysdeps/unix/sysv/linux/bits/unistd_ext.h
>>>>> @@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
>>>>> # define CLOSE_RANGE_CLOEXEC (1U << 2)
>>>>> #endif
>>>>>
>>>>> +/* Change the root directory of the calling process to the directory
>>>>> + referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
>>>>> + in which case all subsequent absolute file name lookups fail. FLAGS
>>>>> + must be zero. */
>>>>> +extern int fchroot (int __fd, unsigned int __flags) __THROW;
>>>>> +
>>>>> #endif /* __USE_GNU */
>>>>
>>>> CRIU defines _GNU_SOURCE, and it has:
>>>>
>>>> criu/files.c:#include <unistd.h>
>>>>
>>>> criu/files.c:static int fchroot(int fd)
>>>
>>> BSD also has fchroot without the flags.
>>
>> So does Hurd.
>
> And Solaris [1].
>
> I have been wanting this for the 'chroot' command from GNU coreutils for
> a while. If my memory serves me correctly, it is only available on
> NetBSD and not the other BSDs. For that reason, I didn't bother making
> use of it. I was not aware it was supported on Hurd, and only found out
> about Solaris recently.
It was added on FreeBSD 15 as well [1].
[1] https://man.freebsd.org/cgi/man.cgi?query=fchroot&apropos=0&sektion=0&manpath=FreeBSD+15.1-RELEASE+and+Ports.quarterly&format=html
>
>>> Should the one above be called fchroot2, or should the flags be
>>> dropped for now?
>>
>> It won't fix the fchroot build failure. It's surprisingly rare, though:
>>
>> <https://codesearch.debian.net/search?q=%28%5E%7Cint%5Cs*%29fchroot%5Cs*%5C%28&literal=0>
>>
>> I just got lucky, I didn't find it through Debian Code Search, just by
>> consulting the CRIU source code.
>>
>> I don't think we should aim for compatibility with Hurd/BSD if Linux has
>> something else. On the other hand, if POSIX standardizes fchroot in the
>> one-argument form, we'll have a problem.
>
> Yes. I was pleasantly suprised to see the system call added in Linux
> 7.3. But I find it a bit annoying that a different function signature
> than everyone else...
>
I think we can keep using the BSD/Solaris/Hurd prototype and add a fchroot2
with flags. Seems the lest disruptive way imho.
> Collin
>
> [1] https://docs.oracle.com/cd/E86824_01/html/E54765/fchroot-2.html
On Nov 05 2026, Florian Weimer wrote:
> On the other hand, if POSIX standardizes fchroot
That is unlikely, they removed chroot in the 2001 edition.
* Andreas Schwab:
> On Nov 05 2026, Florian Weimer wrote:
>
>> On the other hand, if POSIX standardizes fchroot
>
> That is unlikely, they removed chroot in the 2001 edition.
Oh. Then I would say we should go with the two-argument Linux version.
Thanks,
Florian
On 06/11/26 11:14, Florian Weimer wrote:
> * Andreas Schwab:
>
>> On Nov 05 2026, Florian Weimer wrote:
>>
>>> On the other hand, if POSIX standardizes fchroot
>>
>> That is unlikely, they removed chroot in the 2001 edition.
>
> Oh. Then I would say we should go with the two-argument Linux version.
With the current fchroot name or do you prefer a fchroot2 ?
* Adhemerval Zanella Netto:
> On 06/11/26 11:14, Florian Weimer wrote:
>> * Andreas Schwab:
>>
>>> On Nov 05 2026, Florian Weimer wrote:
>>>
>>>> On the other hand, if POSIX standardizes fchroot
>>>
>>> That is unlikely, they removed chroot in the 2001 edition.
>>
>> Oh. Then I would say we should go with the two-argument Linux version.
> With the current fchroot name or do you prefer a fchroot2 ?
I prefer fchroot, matching the system call name.
Thanks,
Florian
@@ -16,6 +16,9 @@ Major new features:
<sys/membarrier.h> header. It issues memory barriers on a set of
threads.
+* On Linux, the fchroot function has been added. It changes the root
+ directory to the directory referred to by a file descriptor.
+
Deprecated and removed features, and other changes affecting compatibility:
* The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
@@ -204,6 +204,7 @@ tests += \
tst-epoll \
tst-epoll-ioctls \
tst-fanotify \
+ tst-fchroot \
tst-fdopendir-o_path \
tst-getauxval \
tst-gettid \
@@ -344,6 +344,7 @@ libc {
openat2;
}
GLIBC_2.45 {
+ fchroot;
membarrier;
}
GLIBC_PRIVATE {
@@ -2775,6 +2775,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -3122,6 +3122,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2536,6 +2536,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2828,6 +2828,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2825,6 +2825,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -47,4 +47,10 @@ extern __pid_t gettid (void) __THROW;
# define CLOSE_RANGE_CLOEXEC (1U << 2)
#endif
+/* Change the root directory of the calling process to the directory
+ referred to by the file descriptor FD. FD can also be FD_FAILFS_ROOT,
+ in which case all subsequent absolute file name lookups fail. FLAGS
+ must be zero. */
+extern int fchroot (int __fd, unsigned int __flags) __THROW;
+
#endif /* __USE_GNU */
@@ -2812,6 +2812,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2849,6 +2849,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -3032,6 +3032,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2287,6 +2287,7 @@ GLIBC_2.44 wprintf F
GLIBC_2.44 write F
GLIBC_2.44 writev F
GLIBC_2.44 wscanf F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2296,6 +2296,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2808,6 +2808,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2975,6 +2975,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2861,6 +2861,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2858,6 +2858,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2938,6 +2938,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2936,6 +2936,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2944,6 +2944,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2846,6 +2846,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2286,6 +2286,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -3165,6 +3165,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -3210,6 +3210,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2919,6 +2919,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2995,6 +2995,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2539,6 +2539,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2739,6 +2739,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2956,6 +2956,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2855,6 +2855,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2852,6 +2852,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -3186,6 +3186,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2822,6 +2822,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -10,6 +10,7 @@ epoll_create1 EXTRA epoll_create1 i:i epoll_create1
epoll_ctl EXTRA epoll_ctl i:iiip epoll_ctl
eventfd EXTRA eventfd2 i:ii eventfd
execve - execve i:spp __execve execve
+fchroot EXTRA fchroot i:iU fchroot
flock - flock i:ii __flock flock
fsconfig EXTRA fsconfig i:iUsNi fsconfig
fsmount EXTRA fsmount i:iUU fsmount
new file mode 100644
@@ -0,0 +1,114 @@
+/* Basic tests for Linux fchroot.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/prctl.h>
+#include <unistd.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/xunistd.h>
+
+#define MARKER "fchroot-marker"
+
+static char *temp_dir;
+static char *marker_path;
+
+static void
+do_prepare (int argc, char *argv[])
+{
+ temp_dir = support_create_temp_directory ("tst-fchroot.");
+ marker_path = xasprintf ("%s/%s", temp_dir, MARKER);
+ add_temp_file (marker_path);
+ xclose (xopen (marker_path, O_WRONLY | O_CREAT | O_EXCL, 0600));
+}
+#define PREPARE do_prepare
+
+/* Move the root to failfs. Unprivileged processes can do this if
+ no_new_privs is set. */
+static void
+change_root_failfs (void *closure)
+{
+ TEST_COMPARE (prctl (PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0);
+ if (fchroot (FD_FAILFS_ROOT, 0) != 0)
+ {
+ /* An unprivileged process which is already in a chroot cannot escape
+ it this way. */
+ TEST_COMPARE (errno, EPERM);
+ puts ("info: cannot change the root to failfs");
+ return;
+ }
+
+ /* Absolute file name lookups now fail. */
+ TEST_COMPARE (open ("/", O_RDONLY | O_DIRECTORY), -1);
+ TEST_COMPARE (errno, EOPNOTSUPP);
+}
+
+/* Change the root to TEMP_DIR, which requires CAP_SYS_CHROOT. */
+static void
+change_root (void *closure)
+{
+ int dfd = xopen (temp_dir, O_RDONLY | O_DIRECTORY, 0);
+ TEST_COMPARE (fchroot (dfd, 0), 0);
+ xclose (dfd);
+
+ /* The marker file is now at the root. */
+ TEST_COMPARE (access ("/" MARKER, F_OK), 0);
+}
+
+static int
+do_test (void)
+{
+ int dfd = xopen (temp_dir, O_RDONLY | O_DIRECTORY, 0);
+
+ /* FLAGS must be zero. */
+ TEST_COMPARE (fchroot (dfd, 1), -1);
+ if (errno == ENOSYS)
+ FAIL_UNSUPPORTED ("fchroot is not supported by the kernel");
+ TEST_COMPARE (errno, EINVAL);
+
+ TEST_COMPARE (fchroot (-1, 0), -1);
+ TEST_COMPARE (errno, EBADF);
+
+ {
+ int fd = xopen (marker_path, O_RDONLY, 0);
+ TEST_COMPARE (fchroot (fd, 0), -1);
+ TEST_COMPARE (errno, ENOTDIR);
+ xclose (fd);
+ }
+
+ support_isolate_in_subprocess (change_root_failfs, NULL);
+
+ support_become_root ();
+ if (support_can_chroot ())
+ support_isolate_in_subprocess (change_root, NULL);
+ else
+ {
+ TEST_COMPARE (fchroot (dfd, 0), -1);
+ TEST_COMPARE (errno, EPERM);
+ puts ("info: cannot change the root to a directory");
+ }
+
+ xclose (dfd);
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -2771,6 +2771,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F
@@ -2790,6 +2790,7 @@ GLIBC_2.43 memset_explicit F
GLIBC_2.43 mseal F
GLIBC_2.43 openat2 F
GLIBC_2.43 umaxabs F
+GLIBC_2.45 fchroot F
GLIBC_2.45 membarrier F
GLIBC_2.45 posix_spawn_file_actions_addchdir F
GLIBC_2.45 posix_spawn_file_actions_addfchdir F