Bug 291301 - statfs() no longer reporting statfs.f_fsid in jails
Summary: statfs() no longer reporting statfs.f_fsid in jails
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: 14.3-RELEASE
Hardware: Any Any
: --- Affects Many People
Assignee: Dag-Erling Smørgrav
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-11-30 14:29 UTC by vedad
Modified: 2025-12-19 18:40 UTC (History)
1 user (show)

See Also:
des: mfc-stable15+
des: mfc-stable14+
des: mfc-stable13+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description vedad 2025-11-30 14:29:41 UTC
As of FreeBSD 14.3, the statfs() call in jails seems to always return empty (zeroed) statfs.f_fsid, specifically for zfs datasets managed (created and mounted) by the jail.

This used to work fine (ie. an actual value was returned in `statfs.f_fsid`) in FreeBSD 14.2 jails with enforce_statfs=1.

I have tested on FreeBSD 14.3 with both enforce_statfs=1 and enforce_statfs=0, and the returned value is always zeroed.

This is quite annoying for software that needs to gather available/used disk space among available mountpoints.
Comment 1 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2025-12-13 21:43:34 UTC
Hi,

You say this worked in 14.2, but I see no evidence that any of the code involved has changed in the past five years.

At the core are the following six lines from sys/kern/vfs_syscalls.c:

        error = VFS_STATFS(mp, buf);
        if (error != 0)
                goto out;
        if (priv_check_cred_vfs_generation(td->td_ucred)) {
                buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
                prison_enforce_statfs(td->td_ucred, mp, buf);
        }

First, we can verify that VFS_STATFS() (which invokes the underlying filesystem's statfs method) works as intended by invoking `statfs(2)` as root outside a jail.

Next, the priv_check_cred_vfs_generation() call checks if we are permitted to see the raw statfs data (this code violates our style guide: the function returns 0 for success so we should be checking the return value explicitly here instead of using the function as a predicate, but never mind that for now).

If we are not unjailed root, this function returns non-zero and we enter the block, which starts by zeroing out f_fsid and then calling prison_enforce_statfs(), which rewrites mntfromname and mnttoname to be relative to the jail root.

The last time this code changed was in 2020 when priv_check_cred_vfs_generation(td->td_ucred) replaced priv_check(td, PRIV_VFS_GENERATION).  This was purely an optimization; the permission we are checking is the same, but instead of calling a more general function which takes more time to return the answer, we call a more specific function, which shaves a few cycles off from a very frequently invoked code path.

Looking at priv_check_cred_vfs_generation() itself, it hasn't changed since 2020 either.  As long as you're not using MAC, it boils down to this:

        error = EPERM;
        if (!jailed(cred) && cred->cr_uid == 0 && suser_enabled(cred))
                error = 0;
        return (error);

As you can see, it will always return EPERM for a jailed process.

The enforce_statfs permission was never involved in determining whether a jailed process can see a filesystem's fsid.  It only controls the behavior of prison_enforce_statfs(), which isn't called until after f_fsid has been zeroed.

You may argue that the fsid _should_ be available when enforce_statfs is 0, which could be implemented by moving the zeroing of f_fsid into prison_enforce_statfs().  But as far as I can tell it has never been available in the past.  I don't have a 14.2 VM handy, but I have verified that it isn't available in 13.5.
Comment 2 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2025-12-13 22:08:39 UTC
https://reviews.freebsd.org/D54214
Comment 3 vedad 2025-12-14 12:28:24 UTC
Hi,

I'm probably mistaken indeed about it working in 14.2 jails, it might have been a bhyve VM instead.

For the context, garage software gets the cluster capacity wrong due to this issue. I made a small contribution to the software to fix another freebsd-related statfs/statvfs issue almost a year ago (running 14.2):

https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/917/files

but as I stated previously my garage might not have been running within a jail at that time.

That being said, I do believe both enforce_statfs=0 and enforce_statfs=1 should expose fs_id, as enforce_statfs=1 is sufficient to allow jail to manage (create & mount) zfs datasets.

Thanks,
Comment 4 commit-hook freebsd_committer freebsd_triage 2025-12-14 13:47:21 UTC
A commit in branch main references this bug:

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

commit d4f25d0c7957f0f1960028eec82625c2d6405537
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-12-14 13:16:16 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-12-14 13:16:37 +0000

    vfs: Let prison_enforce_statfs zero the fsid

    Currently, we unconditionally zero the fsid before returning a struct
    statfs to a jailed process.  Move this into prison_enforce_statfs() so
    it only happens if enforce_statfs is greater than 1, or enforce_statfs
    is 1 but the mountpoint is outside the jail.

    PR:             291301
    MFC after:      1 week
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D54214

 sys/kern/kern_jail.c    | 3 +++
 sys/kern/vfs_syscalls.c | 5 +----
 2 files changed, 4 insertions(+), 4 deletions(-)
Comment 5 vedad 2025-12-19 13:51:51 UTC
Great, thanks for the quick fix!
Comment 6 commit-hook freebsd_committer freebsd_triage 2025-12-19 18:06:16 UTC
A commit in branch stable/15 references this bug:

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

commit b43159901f11cb379176215cc573971c2148d18f
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-12-14 13:16:16 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-12-19 18:05:43 +0000

    vfs: Let prison_enforce_statfs zero the fsid

    Currently, we unconditionally zero the fsid before returning a struct
    statfs to a jailed process.  Move this into prison_enforce_statfs() so
    it only happens if enforce_statfs is greater than 1, or enforce_statfs
    is 1 but the mountpoint is outside the jail.

    PR:             291301
    MFC after:      1 week
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D54214

    (cherry picked from commit d4f25d0c7957f0f1960028eec82625c2d6405537)

 sys/kern/kern_jail.c    | 3 +++
 sys/kern/vfs_syscalls.c | 5 +----
 2 files changed, 4 insertions(+), 4 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-12-19 18:07:18 UTC
A commit in branch stable/14 references this bug:

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

commit 90e09bfe799dc96e7e6e0071a053852cad152d96
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-12-14 13:16:16 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-12-19 18:06:00 +0000

    vfs: Let prison_enforce_statfs zero the fsid

    Currently, we unconditionally zero the fsid before returning a struct
    statfs to a jailed process.  Move this into prison_enforce_statfs() so
    it only happens if enforce_statfs is greater than 1, or enforce_statfs
    is 1 but the mountpoint is outside the jail.

    PR:             291301
    MFC after:      1 week
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D54214

    (cherry picked from commit d4f25d0c7957f0f1960028eec82625c2d6405537)

 sys/kern/kern_jail.c    | 3 +++
 sys/kern/vfs_syscalls.c | 5 +----
 2 files changed, 4 insertions(+), 4 deletions(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2025-12-19 18:07:20 UTC
A commit in branch stable/13 references this bug:

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

commit b063e9c13a916cea96596de5c32cd0206aaf1c08
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-12-14 13:16:16 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-12-19 18:07:04 +0000

    vfs: Let prison_enforce_statfs zero the fsid

    Currently, we unconditionally zero the fsid before returning a struct
    statfs to a jailed process.  Move this into prison_enforce_statfs() so
    it only happens if enforce_statfs is greater than 1, or enforce_statfs
    is 1 but the mountpoint is outside the jail.

    PR:             291301
    MFC after:      1 week
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D54214

    (cherry picked from commit d4f25d0c7957f0f1960028eec82625c2d6405537)

 sys/kern/kern_jail.c    | 3 +++
 sys/kern/vfs_syscalls.c | 5 +----
 2 files changed, 4 insertions(+), 4 deletions(-)