Fix for unintended FUSE ACL cache
Subsystems (4)
cover Fix for unintended FUSE ACL cache
Christian,
I bet you did not miss fuse acls...
Ghosts from the past have come back to haunt me now.
Let me know what you think of this fix and whether you prefer a fuse gate to forget_all_cached_acls() instead of the suggested vfs fix.
Thanks,
Amir.
Amir Goldstein (2):
fs: preserve ACL_DONT_CACHE state in forget_cached_acl() selftests/fuse: add ACL_DONT_CACHE regression test
fs/posix_acl.c | 7 + .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 3 files changed, 365 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c
Signature · 1 line
2.54.0
PATCH 1/2 fs: preserve ACL_DONT_CACHE state in forget_cached_acl()
The ACL_DONT_CACHE state is meant to be a constant state for the inode for filesystems that want to opt out of posix acl caching.
Commit facd61053cff1 ("fuse: fixes after adapting to new posix acl api") used this facility to opt out of posix acl caching for fuse inodes with fuse server that does not negotiate FUSE_POSIX_ACL (fc->posix_acl).
The commit also takes care to gate the forget_all_cached_acls() call in fuse_set_acl() on fc->posix_acl because there is no need for it, but there are other placed in fuse code which call forget_all_cached_acls() unconditional to fc->posix_acl and those cause the loss of the ACL_DONT_CACHE state.
This is not only a functional bug. Properly timed, a get_acl() from this fuse filesystem can return a stale cached value, as was observed in tests, because set_acl() does not invalidate the unintentional acl cache.
We could fix this in fuse, but it actually makes no sense for the vfs helper forget_cached_acl() to invalidate the ACL_DONT_CACHE state, so let it not do that to fix fuse and future users of ACL_DONT_CACHE.
--- fs/posix_acl.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/posix_acl.c +7 -0
@@ -93,6 +93,13 @@ static void __forget_cached_acl(struct posix_acl **p) {struct posix_acl *old; +/* +* ACL_DONT_CACHE is expected to be a "const" value and xchg it with +* ACL_NOT_CACHED would enable acl caching for the inode - +* clearly not what the caller has intended. +*/ +if (READ_ONCE (*p) ==ACL_DONT_CACHE ) +return ; old =xchg (p,ACL_NOT_CACHED );if (!is_uncached_acl (old))posix_acl_release (old);
Signature · 1 line
2.54.0
Christian Brauner
maintainerRe: [PATCH 1/2] fs: preserve ACL_DONT_CACHE state in forget_cached_acl()
Quoted 30 lines
The
ACL_DONT_CACHEstate is meant to be a constant state for the inode for filesystems that want to opt out of posix acl caching.Commit
facd61053cff1("fuse: fixes after adapting to new posix acl api") used this facility to opt out of posix acl caching for fuse inodes with fuse server that does not negotiateFUSE_POSIX_ACL(fc->posix_acl).The commit also takes care to gate the
forget_all_cached_acls()call infuse_set_acl()onfc->posix_aclbecause there is no need for it, but there are other placed in fuse code which callforget_all_cached_acls()unconditional tofc->posix_acland those cause the loss of theACL_DONT_CACHEstate.This is not only a functional bug. Properly timed, a
get_acl()from this fuse filesystem can return a stale cached value, as was observed in tests, becauseset_acl()does not invalidate the unintentional acl cache.We could fix this in fuse, but it actually makes no sense for the vfs helper
forget_cached_acl()to invalidate theACL_DONT_CACHEstate, so let it not do that to fix fuse and future users ofACL_DONT_CACHE.Fixes facd61053cff1 ("fuse: fixes after adapting to new posix acl api")Signed-off-by Amir Goldstein <[email protected]>fs/posix_acl.c +0
-0
fs/posix_acl.c static void __forget_cached_acl(struct posix_acl **p) · line 102
Quoted 10 lines
fs/posix_acl.c +7
-0@@ -93,6 +93,13 @@ static void __forget_cached_acl(struct posix_acl **p) {struct posix_acl *old; +/* +* ACL_DONT_CACHE is expected to be a "const" value and xchg it with +* ACL_NOT_CACHED would enable acl caching for the inode - +* clearly not what the caller has intended. +*/ +if (READ_ONCE (*p) ==ACL_DONT_CACHE ) +return ;
Still on vacation this week but I took a glimpse:
If this isn't what the caller intended, having ACL_DONT_CACHE end up should be treated like a bug. So shouldn't this then be a WARN_ON_ONCE() and return?
Signature · 1 line
Christian Brauner <[email protected]>
Re: [PATCH 1/2] fs: preserve ACL_DONT_CACHE state in forget_cached_acl()
Quoted 44 lines · Christian Brauner
Quoted 31 lines
The
ACL_DONT_CACHEstate is meant to be a constant state for the inode for filesystems that want to opt out of posix acl caching.Commit
facd61053cff1("fuse: fixes after adapting to new posix acl api") used this facility to opt out of posix acl caching for fuse inodes with fuse server that does not negotiateFUSE_POSIX_ACL(fc->posix_acl).The commit also takes care to gate the
forget_all_cached_acls()call infuse_set_acl()onfc->posix_aclbecause there is no need for it, but there are other placed in fuse code which callforget_all_cached_acls()unconditional tofc->posix_acland those cause the loss of theACL_DONT_CACHEstate.This is not only a functional bug. Properly timed, a
get_acl()from this fuse filesystem can return a stale cached value, as was observed in tests, becauseset_acl()does not invalidate the unintentional acl cache.We could fix this in fuse, but it actually makes no sense for the vfs helper
forget_cached_acl()to invalidate theACL_DONT_CACHEstate, so let it not do that to fix fuse and future users ofACL_DONT_CACHE.Fixes facd61053cff1 ("fuse: fixes after adapting to new posix acl api")Signed-off-by Amir Goldstein <[email protected]>fs/posix_acl.c +0
-0fs/posix_acl.c static void __forget_cached_acl(struct posix_acl **p) · line 102
Quoted 10 lines
fs/posix_acl.c +7
-0@@ -93,6 +93,13 @@ static void __forget_cached_acl(struct posix_acl **p) {struct posix_acl *old; +/* +* ACL_DONT_CACHE is expected to be a "const" value and xchg it with +* ACL_NOT_CACHED would enable acl caching for the inode - +* clearly not what the caller has intended. +*/ +if (READ_ONCE (*p) ==ACL_DONT_CACHE ) +return ;Still on vacation this week but I took a glimpse:
Quoted 3 lines
If this isn't what the caller intended, having
ACL_DONT_CACHEend up should be treated like a bug. So shouldn't this then be aWARN_ON_ONCE()and return?
The caller has requested to *forget* any *cached* *acl*. The value ACL_DONT_CACHE means that there are no cached acls to forget so the call is a success.
Therefore, WARN_ON is not called for IMO.
Sorry to interrupt vacation.
Let's continue when you get back.
Thanks,
Amir.
Re: [PATCH 1/2] fs: preserve ACL_DONT_CACHE state in forget_cached_acl()
In reply to Amir GoldsteinQuoted 39 lines · Amir Goldstein
The
ACL_DONT_CACHEstate is meant to be a constant state for the inode for filesystems that want to opt out of posix acl caching.Commit
facd61053cff1("fuse: fixes after adapting to new posix acl api") used this facility to opt out of posix acl caching for fuse inodes with fuse server that does not negotiateFUSE_POSIX_ACL(fc->posix_acl).The commit also takes care to gate the
forget_all_cached_acls()call infuse_set_acl()onfc->posix_aclbecause there is no need for it, but there are other placed in fuse code which callforget_all_cached_acls()unconditional tofc->posix_acland those cause the loss of theACL_DONT_CACHEstate.This is not only a functional bug. Properly timed, a
get_acl()from this fuse filesystem can return a stale cached value, as was observed in tests, becauseset_acl()does not invalidate the unintentional acl cache.We could fix this in fuse, but it actually makes no sense for the vfs helper
forget_cached_acl()to invalidate theACL_DONT_CACHEstate, so let it not do that to fix fuse and future users ofACL_DONT_CACHE.Fixes facd61053cff1 ("fuse: fixes after adapting to new posix acl api")Cc [email protected] Signed-off-by: Amir Goldstein<[email protected]>
---
fs/posix_acl.c| 7 +++++++ 1 file changed, 7 insertions(+)fs/posix_acl.c index renamed from fs/posix_acl.c +0
-0
b4bfe4ddf64ea..3dc62c1c27087100644 ---a/fs/posix_acl.c+++b/fs/posix_acl.c@@ -93,6 +93,13 @@ static void__forget_cached_acl(struct posix_acl **p)
{
structposix_acl*old;+ /* + *
ACL_DONT_CACHEis expected to be a "const" value and xchg it with + *ACL_NOT_CACHEDwould enable acl caching for the inode - + * clearly not what the caller has intended. + */ + if (READ_ONCE(*p)==ACL_DONT_CACHE) + return;
fs/posix_acl.c index
Quoted 2 lines
fs/posix_acl.c index renamed from fs/posix_acl.c +0
-0old = xchg(p, ACL_NOT_CACHED); if (!is_uncached_acl(old)) posix_acl_release(old);
Nice catch! FWIW feel free to add my
Cheers,
Signature · 1 line
Luís
PATCH 2/2 selftests/fuse: add ACL_DONT_CACHE regression test
In reply to Amir GoldsteinAdd a test that reproduces the stale ACL bug fixed by:
"fs: preserve ACL_DONT_CACHE state in forget_cached_acl()"
A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises inodes with i_acl = ACL_DONT_CACHE. Before the fix, calling forget_all_cached_acls() (e.g. from fuse_update_get_attr() on a statx(AT_STATX_FORCE_SYNC)) would silently replace ACL_DONT_CACHE with ACL_NOT_CACHED, enabling the kernel ACL cache. A subsequent getxattr would populate the cache, and because fuse_set_acl() skips forget_all_cached_acls() for !fc->posix_acl, later ACL changes were not visible to callers — getxattr returned stale data.
The test mounts a minimal libfuse3 lowlevel filesystem (no FUSE_POSIX_ACL negotiated) and:
1. Issues two getxattrs — both must reach the daemon, proving ACL_DONT_CACHE suppresses caching before any trigger.
2. Calls statx(AT_STATX_FORCE_SYNC) to trigger forget_all_cached_acls().
3. Issues another getxattr (populates the cache on a buggy kernel).
4. Switches the daemon to a different-sized ACL (ACL_B).
5. Issues a final getxattr — expects ACL_B (44 bytes) and daemon call count 4; a buggy kernel returns stale ACL_A (28 bytes).
fuse_acl_cache_test is only built when libfuse3 is detected via pkg-config.
--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c
tools/testing/selftests/filesystems/fuse/Makefile +10 -0
@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)
@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +348 -0
348 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Hi Amir,
Quoted 12 lines · Amir Goldstein
Add a test that reproduces the stale ACL bug fixed by:
"fs: preserveACL_DONT_CACHEstate inforget_cached_acl()"A FUSE mount that does not negotiate
FUSE_POSIX_ACLinitialises inodes withi_acl=ACL_DONT_CACHE. Before the fix, callingforget_all_cached_acls()(e.g. fromfuse_update_get_attr()on astatx(AT_STATX_FORCE_SYNC)) would silently replaceACL_DONT_CACHEwithACL_NOT_CACHED, enabling the kernel ACL cache. A subsequent getxattr would populate the cache, and becausefuse_set_acl()skipsforget_all_cached_acls()for !fc->posix_acl, later ACL changes were not visible to callers — getxattr returned stale data.
Quoted 9 lines
The test mounts a minimal libfuse3 lowlevel filesystem (no
FUSE_POSIX_ACLnegotiated) and:
1. Issues two getxattrs — both must reach the daemon, provingACL_DONT_CACHEsuppresses caching before any trigger.
2. Callsstatx(AT_STATX_FORCE_SYNC)to triggerforget_all_cached_acls().
3. Issues another getxattr (populates the cache on a buggy kernel).
4. Switches the daemon to a different-sized ACL (ACL_B).
5. Issues a final getxattr — expectsACL_B(44 bytes) and daemon call count 4; a buggy kernel returns staleACL_A(28 bytes).
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
Quoted 98 lines
Signed-off-by Amir Goldstein <[email protected]>--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.ctools/testing/selftests/filesystems/fuse/Makefile +10
-0@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +60
-0@@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Test: FUSE ACL caching bug triggered by AT_STATX_FORCE_SYNC +* +* A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises every inode +* with i_acl = i_default_acl = ACL_DONT_CACHE. When a fresh stat is needed +* (e.g. AT_STATX_FORCE_SYNC), fuse_update_get_attr() calls +* forget_all_cached_acls() before issuing FUSE_GETATTR. On an unfixed kernel, +* __forget_cached_acl() replaces ACL_DONT_CACHE with ACL_NOT_CACHED, +* inadvertently enabling the kernel ACL cache for that inode. The next +* getxattr populates the cache. Because fuse_set_acl() skips +* forget_all_cached_acls() for !fc->posix_acl mounts, any subsequent change to +* the ACL leaves the stale kernel entry in place, and the next getxattr returns +* wrong data without ever reaching the FUSE daemon. +* +* Fix (fs/posix_acl.c): __forget_cached_acl() returns early when *p is +* ACL_DONT_CACHE, preserving the "never cache" invariant for the inode's +* lifetime. +* +* Test outline: +* 1. Mount a minimal FUSE fs (no FUSE_POSIX_ACL negotiated). +* 2. lgetxattr -> daemon called, ACL_A returned, NOT cached (ACL_DONT_CACHE). +* 3. statx(AT_STATX_FORCE_SYNC) -> forget_all_cached_acls() called. +* Buggy: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled). +* Fixed: ACL_DONT_CACHE preserved. +* 4. lgetxattr -> daemon called, ACL_A returned. +* Buggy: result now cached (ACL_NOT_CACHED -> cached). +* Fixed: result still not cached. +* 5. Daemon switches to ACL_B internally (different size). +* 6. lgetxattr -> should return ACL_B (44 bytes). +* Buggy: cache hit, returns stale ACL_A (28 bytes). FAIL. +* Fixed: no cache, daemon called, returns ACL_B (44 bytes). PASS. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <linux/limits.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/xattr.h> +#include <unistd.h> + +#define FUSE_USE_VERSION 31 +#include <fuse_lowlevel.h> + +#include "kselftest_harness.h" + +/* ---- ACL binary encoding ------------------------------------------------ */ +/* +* POSIX ACL v2 xattr format (little-endian): +* header: u32 version (= 0x00000002) +* entry: u16 tag | u16 perm | u32 id +* +* Entries must appear in tag-ascending order; named USER/GROUP entries
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 70
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+* require a MASK entry. Both ACLs pass posix_acl_from_xattr() validation. +*/ + +/* ACL_A: 3 entries (USER_OBJ:rwx, GROUP_OBJ:r-x, OTHER:r-x) = 28 bytes */ +static const uint8_t acl_a[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +};
Wouldn't it be OK to add a dependency on libacl to make this more readable?
Quoted 210 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +209
-0+ +/* +* ACL_B: 5 entries — adds USER uid=1 and MASK = 44 bytes. +* A named USER entry requires a MASK; all tags in ascending order. +*/ +static const uint8_t acl_b[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x02 ,0x00 ,0x07 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,/* USER uid=1 rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x10 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* MASK rwx */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +}; + +/* ---- Shared state (daemon thread <-> test thread) ----------------------- */ + +#define FILE_INO 2 +#define FILE_NAME "testfile" + +struct daemon_state { +pthread_mutex_t lock; +const uint8_t *acl; +size_t acl_size; +int getxattr_count; +}; + +/* +* Global: callbacks are stateless fns so we use a single global. +* Safe because only one test instance runs at a time. +*/ +static struct daemon_state g_ds = { + .lock =PTHREAD_MUTEX_INITIALIZER , +}; + +/* ---- FUSE lowlevel callbacks -------------------------------------------- */ + +static void fs_lookup (fuse_req_t req,fuse_ino_t parent,const char *name) +{ +if (parent !=FUSE_ROOT_ID ||strcmp (name,FILE_NAME )) { +fuse_reply_err (req,ENOENT ); +return ; + } +struct fuse_entry_param e = {}; + +/* +* Long attr/entry timeouts so that normal stat() calls do not +* expire and trigger forget_all_cached_acls() on their own; +* only the explicit AT_STATX_FORCE_SYNC should trigger it. +*/ + e.ino =FILE_INO ; + e.generation =1 ; + e.attr_timeout =10.0 ; + e.entry_timeout =10.0 ; + e.attr.st_ino =FILE_INO ; + e.attr.st_mode =S_IFREG |0644 ; + e.attr.st_nlink =1 ; +fuse_reply_entry (req, &e); +} + +static void fs_getattr (fuse_req_t req,fuse_ino_t ino, +struct fuse_file_info *fi) +{ +struct stat st = {}; + + (void )fi; +if (ino ==FUSE_ROOT_ID ) { + st.st_ino =FUSE_ROOT_ID ; + st.st_mode =S_IFDIR |0755 ; + st.st_nlink =2 ; + }else if (ino ==FILE_INO ) { + st.st_ino =FILE_INO ; + st.st_mode =S_IFREG |0644 ; + st.st_nlink =1 ; + }else { +fuse_reply_err (req,ENOENT ); +return ; + } +fuse_reply_attr (req, &st,10 ); +} + +static void fs_getxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +size_t size) +{ +if (ino !=FILE_INO || +strcmp (name,"system.posix_acl_access" ) !=0 ) { +fuse_reply_err (req,ENODATA ); +return ; + } + +pthread_mutex_lock (&g_ds.lock); +const uint8_t *acl = g_ds.acl; +size_t acl_size = g_ds.acl_size; + g_ds.getxattr_count++; +pthread_mutex_unlock (&g_ds.lock); + +if (size ==0 ) +fuse_reply_xattr (req, acl_size); +else if (size < acl_size) +fuse_reply_err (req,ERANGE ); +else +fuse_reply_buf (req, (const char *)acl, acl_size); +} + +static const struct fuse_lowlevel_ops fs_ops = { + .lookup = fs_lookup, + .getattr = fs_getattr, + .getxattr = fs_getxattr, +}; + +/* ---- Daemon thread ------------------------------------------------------- */ + +static void *run_daemon (void *arg) +{ +fuse_session_loop ((struct fuse_session *)arg); +return NULL ; +} + +/* ---- kselftest harness --------------------------------------------------- */ + +FIXTURE (acl_cache) { +struct fuse_session *se; +char mountpoint[PATH_MAX ]; +char file_path[PATH_MAX ]; +pthread_t thread; +}; + +FIXTURE_SETUP (acl_cache) +{ +char *fuse_argv[] = {"fuse_acl_cache_test" ,NULL }; +struct fuse_args args =FUSE_ARGS_INIT (1 , fuse_argv); + + g_ds.acl = acl_a; + g_ds.acl_size =sizeof (acl_a); + g_ds.getxattr_count =0 ; + +strcpy (self->mountpoint,"/tmp/acl_cache_test_XXXXXX" ); +if (!mkdtemp (self->mountpoint)) +SKIP (return,"mkdtemp: %s" ,strerror (errno)); + +snprintf (self->file_path,sizeof (self->file_path), +"%s/" FILE_NAME , self->mountpoint); + + self->se =fuse_session_new (&args, &fs_ops,sizeof (fs_ops),NULL ); +if (!self->se) { +rmdir (self->mountpoint); +SKIP (return,"fuse_session_new failed" ); + } + +if (fuse_session_mount (self->se, self->mountpoint)) { +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"fuse_session_mount failed " +"(missing fusermount3 or insufficient privileges)" ); + } + +if (pthread_create (&self->thread,NULL , run_daemon, self->se)) { +fuse_session_unmount (self->se); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"pthread_create: %s" ,strerror (errno)); + } + +fuse_opt_free_args (&args); +} + +FIXTURE_TEARDOWN (acl_cache) +{ +fuse_session_exit (self->se); +fuse_session_unmount (self->se); +pthread_join (self->thread,NULL ); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +} + +static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} + +TEST_F(acl_cache, stale_after_force_sync) +{ +char buf[512 ]; +ssize_t sz; +int count; + +/* +* Step 1: two getxattr calls before any statx(FORCE_SYNC). +* i_acl == ACL_DONT_CACHE. __get_acl's cmpxchg(p, ACL_NOT_CACHED, +* sentinel) finds *p != ACL_NOT_CACHED on every call, so the sentinel +* is never placed and the result is never cached. Both calls must +* reach the daemon, proving ACL_DONT_CACHE suppresses caching. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,2 ); + TH_LOG("step 1 OK: both pre-trigger getxattrs reached daemon (count=%d), "
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 289
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+ "ACL_DONT_CACHE is working", count); + +/* +* Step 2: statx(AT_STATX_FORCE_SYNC). +* fuse_update_get_attr() calls forget_all_cached_acls() before sending +* FUSE_GETATTR. +* Buggy kernel: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled) +* Fixed kernel: ACL_DONT_CACHE preserved (no effect) +*/ +ASSERT_EQ (do_force_statx (self->file_path),0 );
Why not calling statx(2) instead? Are you using a libc that doesn't include a wrapper?
(Also, I wonder if having the global state protected with a mutex is really needed in the scope of this test... but yeah, that's the correct thing to do, of course.)
Cheers,
Signature · 1 line
Luís
Quoted 63 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +59
-0+TH_LOG ("step 2 OK: statx(AT_STATX_FORCE_SYNC) succeeded" ); + +/* +* Step 3: getxattr — cache population attempt after the trigger. +* Buggy: *p == ACL_NOT_CACHED -> sentinel placed -> fuse_get_inode_acl +* called -> ACL_A parsed and stored in the kernel cache. +* Fixed: *p == ACL_DONT_CACHE -> sentinel placement skipped -> +* fuse_get_inode_acl called but result not cached. +* Either way the correct ACL_A is returned here. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,3 ); +TH_LOG ("step 3 OK: post-trigger getxattr reached daemon (count=%d), " +"returned correct ACL_A (%zd bytes)" , count, sz); + +/* +* Step 4: switch daemon to ACL_B (different size: 44 vs 28 bytes). +* Simulates an ACL change that fuse_set_acl() would NOT invalidate for +* !fc->posix_acl mounts (it skips forget_all_cached_acls in that case). +* On a fixed kernel the ACL was never cached, so this is moot. +*/ +pthread_mutex_lock (&g_ds.lock); + g_ds.acl = acl_b; + g_ds.acl_size =sizeof (acl_b); +pthread_mutex_unlock (&g_ds.lock); +TH_LOG ("step 4: daemon switched to ACL_B (%zu bytes)" ,sizeof (acl_b)); + +/* +* Step 5: getxattr — the decisive check. +* Buggy kernel: cache hit -> stale ACL_A (28 bytes), count stays 3. +* Fixed kernel: no cache -> daemon called -> ACL_B (44 bytes), count 4. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +if (sz == (ssize_t )sizeof (acl_a)) +TH_LOG ("step 5 BUG: stale ACL_A (%zd bytes) from kernel cache " +"(count=%d); ACL_DONT_CACHE corrupted by " +"forget_all_cached_acls()" , sz, count); +else +TH_LOG ("step 5 OK: daemon reached (count=%d), " +"fresh ACL_B (%zd bytes)" , count, sz); + +EXPECT_EQ (sz, (ssize_t )sizeof (acl_b)); +EXPECT_EQ (count,4 ); +} + +TEST_HARNESS_MAIN Signature · 1 line
2.54.0
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Quoted 27 lines · Luis Henriques
Hi Amir,
Quoted 12 lines · Amir Goldstein
Add a test that reproduces the stale ACL bug fixed by:
"fs: preserveACL_DONT_CACHEstate inforget_cached_acl()"A FUSE mount that does not negotiate
FUSE_POSIX_ACLinitialises inodes withi_acl=ACL_DONT_CACHE. Before the fix, callingforget_all_cached_acls()(e.g. fromfuse_update_get_attr()on astatx(AT_STATX_FORCE_SYNC)) would silently replaceACL_DONT_CACHEwithACL_NOT_CACHED, enabling the kernel ACL cache. A subsequent getxattr would populate the cache, and becausefuse_set_acl()skipsforget_all_cached_acls()for !fc->posix_acl, later ACL changes were not visible to callers — getxattr returned stale data.Quoted 9 lines
The test mounts a minimal libfuse3 lowlevel filesystem (no
FUSE_POSIX_ACLnegotiated) and:
1. Issues two getxattrs — both must reach the daemon, provingACL_DONT_CACHEsuppresses caching before any trigger.
2. Callsstatx(AT_STATX_FORCE_SYNC)to triggerforget_all_cached_acls().
3. Issues another getxattr (populates the cache on a buggy kernel).
4. Switches the daemon to a different-sized ACL (ACL_B).
5. Issues a final getxattr — expectsACL_B(44 bytes) and daemon call count 4; a buggy kernel returns staleACL_A(28 bytes).
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Quoted 10 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.
Thanks for the review!
Amir.
Quoted 409 lines
Quoted 99 lines
Signed-off-by Amir Goldstein <[email protected]>--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.ctools/testing/selftests/filesystems/fuse/Makefile +10
-0@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +60
-0@@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Test: FUSE ACL caching bug triggered by AT_STATX_FORCE_SYNC +* +* A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises every inode +* with i_acl = i_default_acl = ACL_DONT_CACHE. When a fresh stat is needed +* (e.g. AT_STATX_FORCE_SYNC), fuse_update_get_attr() calls +* forget_all_cached_acls() before issuing FUSE_GETATTR. On an unfixed kernel, +* __forget_cached_acl() replaces ACL_DONT_CACHE with ACL_NOT_CACHED, +* inadvertently enabling the kernel ACL cache for that inode. The next +* getxattr populates the cache. Because fuse_set_acl() skips +* forget_all_cached_acls() for !fc->posix_acl mounts, any subsequent change to +* the ACL leaves the stale kernel entry in place, and the next getxattr returns +* wrong data without ever reaching the FUSE daemon. +* +* Fix (fs/posix_acl.c): __forget_cached_acl() returns early when *p is +* ACL_DONT_CACHE, preserving the "never cache" invariant for the inode's +* lifetime. +* +* Test outline: +* 1. Mount a minimal FUSE fs (no FUSE_POSIX_ACL negotiated). +* 2. lgetxattr -> daemon called, ACL_A returned, NOT cached (ACL_DONT_CACHE). +* 3. statx(AT_STATX_FORCE_SYNC) -> forget_all_cached_acls() called. +* Buggy: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled). +* Fixed: ACL_DONT_CACHE preserved. +* 4. lgetxattr -> daemon called, ACL_A returned. +* Buggy: result now cached (ACL_NOT_CACHED -> cached). +* Fixed: result still not cached. +* 5. Daemon switches to ACL_B internally (different size). +* 6. lgetxattr -> should return ACL_B (44 bytes). +* Buggy: cache hit, returns stale ACL_A (28 bytes). FAIL. +* Fixed: no cache, daemon called, returns ACL_B (44 bytes). PASS. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <linux/limits.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/xattr.h> +#include <unistd.h> + +#define FUSE_USE_VERSION 31 +#include <fuse_lowlevel.h> + +#include "kselftest_harness.h" + +/* ---- ACL binary encoding ------------------------------------------------ */ +/* +* POSIX ACL v2 xattr format (little-endian): +* header: u32 version (= 0x00000002) +* entry: u16 tag | u16 perm | u32 id +* +* Entries must appear in tag-ascending order; named USER/GROUP entries tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 70
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+* require a MASK entry. Both ACLs pass posix_acl_from_xattr() validation. +*/ + +/* ACL_A: 3 entries (USER_OBJ:rwx, GROUP_OBJ:r-x, OTHER:r-x) = 28 bytes */ +static const uint8_t acl_a[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +};Wouldn't it be OK to add a dependency on libacl to make this more readable?
Quoted 210 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +209
-0+ +/* +* ACL_B: 5 entries — adds USER uid=1 and MASK = 44 bytes. +* A named USER entry requires a MASK; all tags in ascending order. +*/ +static const uint8_t acl_b[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x02 ,0x00 ,0x07 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,/* USER uid=1 rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x10 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* MASK rwx */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +}; + +/* ---- Shared state (daemon thread <-> test thread) ----------------------- */ + +#define FILE_INO 2 +#define FILE_NAME "testfile" + +struct daemon_state { +pthread_mutex_t lock; +const uint8_t *acl; +size_t acl_size; +int getxattr_count; +}; + +/* +* Global: callbacks are stateless fns so we use a single global. +* Safe because only one test instance runs at a time. +*/ +static struct daemon_state g_ds = { + .lock =PTHREAD_MUTEX_INITIALIZER , +}; + +/* ---- FUSE lowlevel callbacks -------------------------------------------- */ + +static void fs_lookup (fuse_req_t req,fuse_ino_t parent,const char *name) +{ +if (parent !=FUSE_ROOT_ID ||strcmp (name,FILE_NAME )) { +fuse_reply_err (req,ENOENT ); +return ; + } +struct fuse_entry_param e = {}; + +/* +* Long attr/entry timeouts so that normal stat() calls do not +* expire and trigger forget_all_cached_acls() on their own; +* only the explicit AT_STATX_FORCE_SYNC should trigger it. +*/ + e.ino =FILE_INO ; + e.generation =1 ; + e.attr_timeout =10.0 ; + e.entry_timeout =10.0 ; + e.attr.st_ino =FILE_INO ; + e.attr.st_mode =S_IFREG |0644 ; + e.attr.st_nlink =1 ; +fuse_reply_entry (req, &e); +} + +static void fs_getattr (fuse_req_t req,fuse_ino_t ino, +struct fuse_file_info *fi) +{ +struct stat st = {}; + + (void )fi; +if (ino ==FUSE_ROOT_ID ) { + st.st_ino =FUSE_ROOT_ID ; + st.st_mode =S_IFDIR |0755 ; + st.st_nlink =2 ; + }else if (ino ==FILE_INO ) { + st.st_ino =FILE_INO ; + st.st_mode =S_IFREG |0644 ; + st.st_nlink =1 ; + }else { +fuse_reply_err (req,ENOENT ); +return ; + } +fuse_reply_attr (req, &st,10 ); +} + +static void fs_getxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +size_t size) +{ +if (ino !=FILE_INO || +strcmp (name,"system.posix_acl_access" ) !=0 ) { +fuse_reply_err (req,ENODATA ); +return ; + } + +pthread_mutex_lock (&g_ds.lock); +const uint8_t *acl = g_ds.acl; +size_t acl_size = g_ds.acl_size; + g_ds.getxattr_count++; +pthread_mutex_unlock (&g_ds.lock); + +if (size ==0 ) +fuse_reply_xattr (req, acl_size); +else if (size < acl_size) +fuse_reply_err (req,ERANGE ); +else +fuse_reply_buf (req, (const char *)acl, acl_size); +} + +static const struct fuse_lowlevel_ops fs_ops = { + .lookup = fs_lookup, + .getattr = fs_getattr, + .getxattr = fs_getxattr, +}; + +/* ---- Daemon thread ------------------------------------------------------- */ + +static void *run_daemon (void *arg) +{ +fuse_session_loop ((struct fuse_session *)arg); +return NULL ; +} + +/* ---- kselftest harness --------------------------------------------------- */ + +FIXTURE (acl_cache) { +struct fuse_session *se; +char mountpoint[PATH_MAX ]; +char file_path[PATH_MAX ]; +pthread_t thread; +}; + +FIXTURE_SETUP (acl_cache) +{ +char *fuse_argv[] = {"fuse_acl_cache_test" ,NULL }; +struct fuse_args args =FUSE_ARGS_INIT (1 , fuse_argv); + + g_ds.acl = acl_a; + g_ds.acl_size =sizeof (acl_a); + g_ds.getxattr_count =0 ; + +strcpy (self->mountpoint,"/tmp/acl_cache_test_XXXXXX" ); +if (!mkdtemp (self->mountpoint)) +SKIP (return,"mkdtemp: %s" ,strerror (errno)); + +snprintf (self->file_path,sizeof (self->file_path), +"%s/" FILE_NAME , self->mountpoint); + + self->se =fuse_session_new (&args, &fs_ops,sizeof (fs_ops),NULL ); +if (!self->se) { +rmdir (self->mountpoint); +SKIP (return,"fuse_session_new failed" ); + } + +if (fuse_session_mount (self->se, self->mountpoint)) { +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"fuse_session_mount failed " +"(missing fusermount3 or insufficient privileges)" ); + } + +if (pthread_create (&self->thread,NULL , run_daemon, self->se)) { +fuse_session_unmount (self->se); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"pthread_create: %s" ,strerror (errno)); + } + +fuse_opt_free_args (&args); +} + +FIXTURE_TEARDOWN (acl_cache) +{ +fuse_session_exit (self->se); +fuse_session_unmount (self->se); +pthread_join (self->thread,NULL ); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +} + +static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} + +TEST_F(acl_cache, stale_after_force_sync) +{ +char buf[512 ]; +ssize_t sz; +int count; + +/* +* Step 1: two getxattr calls before any statx(FORCE_SYNC). +* i_acl == ACL_DONT_CACHE. __get_acl's cmpxchg(p, ACL_NOT_CACHED, +* sentinel) finds *p != ACL_NOT_CACHED on every call, so the sentinel +* is never placed and the result is never cached. Both calls must +* reach the daemon, proving ACL_DONT_CACHE suppresses caching. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,2 ); + TH_LOG("step 1 OK: both pre-trigger getxattrs reached daemon (count=%d), "tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 289
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+ "ACL_DONT_CACHE is working", count); + +/* +* Step 2: statx(AT_STATX_FORCE_SYNC). +* fuse_update_get_attr() calls forget_all_cached_acls() before sending +* FUSE_GETATTR. +* Buggy kernel: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled) +* Fixed kernel: ACL_DONT_CACHE preserved (no effect) +*/ +ASSERT_EQ (do_force_statx (self->file_path),0 );Why not calling statx(2) instead? Are you using a libc that doesn't include a wrapper?
(Also, I wonder if having the global state protected with a mutex is really needed in the scope of this test... but yeah, that's the correct thing to do, of course.)
Cheers,
--
LuísQuoted 63 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +59
-1+TH_LOG ("step 2 OK: statx(AT_STATX_FORCE_SYNC) succeeded" ); + +/* +* Step 3: getxattr — cache population attempt after the trigger. +* Buggy: *p == ACL_NOT_CACHED -> sentinel placed -> fuse_get_inode_acl +* called -> ACL_A parsed and stored in the kernel cache. +* Fixed: *p == ACL_DONT_CACHE -> sentinel placement skipped -> +* fuse_get_inode_acl called but result not cached. +* Either way the correct ACL_A is returned here. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,3 ); +TH_LOG ("step 3 OK: post-trigger getxattr reached daemon (count=%d), " +"returned correct ACL_A (%zd bytes)" , count, sz); + +/* +* Step 4: switch daemon to ACL_B (different size: 44 vs 28 bytes). +* Simulates an ACL change that fuse_set_acl() would NOT invalidate for +* !fc->posix_acl mounts (it skips forget_all_cached_acls in that case). +* On a fixed kernel the ACL was never cached, so this is moot. +*/ +pthread_mutex_lock (&g_ds.lock); + g_ds.acl = acl_b; + g_ds.acl_size =sizeof (acl_b); +pthread_mutex_unlock (&g_ds.lock); +TH_LOG ("step 4: daemon switched to ACL_B (%zu bytes)" ,sizeof (acl_b)); + +/* +* Step 5: getxattr — the decisive check. +* Buggy kernel: cache hit -> stale ACL_A (28 bytes), count stays 3. +* Fixed kernel: no cache -> daemon called -> ACL_B (44 bytes), count 4. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +if (sz == (ssize_t )sizeof (acl_a)) +TH_LOG ("step 5 BUG: stale ACL_A (%zd bytes) from kernel cache " +"(count=%d); ACL_DONT_CACHE corrupted by " +"forget_all_cached_acls()" , sz, count); +else +TH_LOG ("step 5 OK: daemon reached (count=%d), " +"fresh ACL_B (%zd bytes)" , count, sz); + +EXPECT_EQ (sz, (ssize_t )sizeof (acl_b)); +EXPECT_EQ (count,4 ); +} + +TEST_HARNESS_MAIN --2.54.0
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Hi!
On Wed, Jul 15 2026, Amir Goldstein wrote:
[...]
Quoted 8 lines
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Cool, thanks. I'll consider adapting my own tests to have a similar format as this one.
Quoted 6 lines
Quoted 6 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.
Quoted 7 lines
Quoted 3 lines
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.
Oh! as usual my comment wasn't clear :-)
In fact, my comment wasn't really very relevant in the context of this patch as, in my opinion, I think it's OK for new tests to depend on fuse3.
I was talking about my own patch[1] which was converting the existing test, which depends on fuse2, to use fuse3. That's what I was referring to when talking about breaking CI: we could have a scenario where a test was being built and executed and, because it's now using fuse3, it can't be built any more.
[1] https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Thanks for the review!
I had two extra comments below, not sure you saw them ;-)
Cheers,
Signature · 1 line
Luís
Quoted 411 lines
Amir.
Quoted 409 lines
Quoted 99 lines
Signed-off-by Amir Goldstein <[email protected]>--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.ctools/testing/selftests/filesystems/fuse/Makefile +10
-0@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +60
-0@@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Test: FUSE ACL caching bug triggered by AT_STATX_FORCE_SYNC +* +* A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises every inode +* with i_acl = i_default_acl = ACL_DONT_CACHE. When a fresh stat is needed +* (e.g. AT_STATX_FORCE_SYNC), fuse_update_get_attr() calls +* forget_all_cached_acls() before issuing FUSE_GETATTR. On an unfixed kernel, +* __forget_cached_acl() replaces ACL_DONT_CACHE with ACL_NOT_CACHED, +* inadvertently enabling the kernel ACL cache for that inode. The next +* getxattr populates the cache. Because fuse_set_acl() skips +* forget_all_cached_acls() for !fc->posix_acl mounts, any subsequent change to +* the ACL leaves the stale kernel entry in place, and the next getxattr returns +* wrong data without ever reaching the FUSE daemon. +* +* Fix (fs/posix_acl.c): __forget_cached_acl() returns early when *p is +* ACL_DONT_CACHE, preserving the "never cache" invariant for the inode's +* lifetime. +* +* Test outline: +* 1. Mount a minimal FUSE fs (no FUSE_POSIX_ACL negotiated). +* 2. lgetxattr -> daemon called, ACL_A returned, NOT cached (ACL_DONT_CACHE). +* 3. statx(AT_STATX_FORCE_SYNC) -> forget_all_cached_acls() called. +* Buggy: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled). +* Fixed: ACL_DONT_CACHE preserved. +* 4. lgetxattr -> daemon called, ACL_A returned. +* Buggy: result now cached (ACL_NOT_CACHED -> cached). +* Fixed: result still not cached. +* 5. Daemon switches to ACL_B internally (different size). +* 6. lgetxattr -> should return ACL_B (44 bytes). +* Buggy: cache hit, returns stale ACL_A (28 bytes). FAIL. +* Fixed: no cache, daemon called, returns ACL_B (44 bytes). PASS. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <linux/limits.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/xattr.h> +#include <unistd.h> + +#define FUSE_USE_VERSION 31 +#include <fuse_lowlevel.h> + +#include "kselftest_harness.h" + +/* ---- ACL binary encoding ------------------------------------------------ */ +/* +* POSIX ACL v2 xattr format (little-endian): +* header: u32 version (= 0x00000002) +* entry: u16 tag | u16 perm | u32 id +* +* Entries must appear in tag-ascending order; named USER/GROUP entries tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 70
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+* require a MASK entry. Both ACLs pass posix_acl_from_xattr() validation. +*/ + +/* ACL_A: 3 entries (USER_OBJ:rwx, GROUP_OBJ:r-x, OTHER:r-x) = 28 bytes */ +static const uint8_t acl_a[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +};Wouldn't it be OK to add a dependency on libacl to make this more readable?
Quoted 210 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +209
-0+ +/* +* ACL_B: 5 entries — adds USER uid=1 and MASK = 44 bytes. +* A named USER entry requires a MASK; all tags in ascending order. +*/ +static const uint8_t acl_b[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x02 ,0x00 ,0x07 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,/* USER uid=1 rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x10 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* MASK rwx */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +}; + +/* ---- Shared state (daemon thread <-> test thread) ----------------------- */ + +#define FILE_INO 2 +#define FILE_NAME "testfile" + +struct daemon_state { +pthread_mutex_t lock; +const uint8_t *acl; +size_t acl_size; +int getxattr_count; +}; + +/* +* Global: callbacks are stateless fns so we use a single global. +* Safe because only one test instance runs at a time. +*/ +static struct daemon_state g_ds = { + .lock =PTHREAD_MUTEX_INITIALIZER , +}; + +/* ---- FUSE lowlevel callbacks -------------------------------------------- */ + +static void fs_lookup (fuse_req_t req,fuse_ino_t parent,const char *name) +{ +if (parent !=FUSE_ROOT_ID ||strcmp (name,FILE_NAME )) { +fuse_reply_err (req,ENOENT ); +return ; + } +struct fuse_entry_param e = {}; + +/* +* Long attr/entry timeouts so that normal stat() calls do not +* expire and trigger forget_all_cached_acls() on their own; +* only the explicit AT_STATX_FORCE_SYNC should trigger it. +*/ + e.ino =FILE_INO ; + e.generation =1 ; + e.attr_timeout =10.0 ; + e.entry_timeout =10.0 ; + e.attr.st_ino =FILE_INO ; + e.attr.st_mode =S_IFREG |0644 ; + e.attr.st_nlink =1 ; +fuse_reply_entry (req, &e); +} + +static void fs_getattr (fuse_req_t req,fuse_ino_t ino, +struct fuse_file_info *fi) +{ +struct stat st = {}; + + (void )fi; +if (ino ==FUSE_ROOT_ID ) { + st.st_ino =FUSE_ROOT_ID ; + st.st_mode =S_IFDIR |0755 ; + st.st_nlink =2 ; + }else if (ino ==FILE_INO ) { + st.st_ino =FILE_INO ; + st.st_mode =S_IFREG |0644 ; + st.st_nlink =1 ; + }else { +fuse_reply_err (req,ENOENT ); +return ; + } +fuse_reply_attr (req, &st,10 ); +} + +static void fs_getxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +size_t size) +{ +if (ino !=FILE_INO || +strcmp (name,"system.posix_acl_access" ) !=0 ) { +fuse_reply_err (req,ENODATA ); +return ; + } + +pthread_mutex_lock (&g_ds.lock); +const uint8_t *acl = g_ds.acl; +size_t acl_size = g_ds.acl_size; + g_ds.getxattr_count++; +pthread_mutex_unlock (&g_ds.lock); + +if (size ==0 ) +fuse_reply_xattr (req, acl_size); +else if (size < acl_size) +fuse_reply_err (req,ERANGE ); +else +fuse_reply_buf (req, (const char *)acl, acl_size); +} + +static const struct fuse_lowlevel_ops fs_ops = { + .lookup = fs_lookup, + .getattr = fs_getattr, + .getxattr = fs_getxattr, +}; + +/* ---- Daemon thread ------------------------------------------------------- */ + +static void *run_daemon (void *arg) +{ +fuse_session_loop ((struct fuse_session *)arg); +return NULL ; +} + +/* ---- kselftest harness --------------------------------------------------- */ + +FIXTURE (acl_cache) { +struct fuse_session *se; +char mountpoint[PATH_MAX ]; +char file_path[PATH_MAX ]; +pthread_t thread; +}; + +FIXTURE_SETUP (acl_cache) +{ +char *fuse_argv[] = {"fuse_acl_cache_test" ,NULL }; +struct fuse_args args =FUSE_ARGS_INIT (1 , fuse_argv); + + g_ds.acl = acl_a; + g_ds.acl_size =sizeof (acl_a); + g_ds.getxattr_count =0 ; + +strcpy (self->mountpoint,"/tmp/acl_cache_test_XXXXXX" ); +if (!mkdtemp (self->mountpoint)) +SKIP (return,"mkdtemp: %s" ,strerror (errno)); + +snprintf (self->file_path,sizeof (self->file_path), +"%s/" FILE_NAME , self->mountpoint); + + self->se =fuse_session_new (&args, &fs_ops,sizeof (fs_ops),NULL ); +if (!self->se) { +rmdir (self->mountpoint); +SKIP (return,"fuse_session_new failed" ); + } + +if (fuse_session_mount (self->se, self->mountpoint)) { +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"fuse_session_mount failed " +"(missing fusermount3 or insufficient privileges)" ); + } + +if (pthread_create (&self->thread,NULL , run_daemon, self->se)) { +fuse_session_unmount (self->se); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"pthread_create: %s" ,strerror (errno)); + } + +fuse_opt_free_args (&args); +} + +FIXTURE_TEARDOWN (acl_cache) +{ +fuse_session_exit (self->se); +fuse_session_unmount (self->se); +pthread_join (self->thread,NULL ); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +} + +static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} + +TEST_F(acl_cache, stale_after_force_sync) +{ +char buf[512 ]; +ssize_t sz; +int count; + +/* +* Step 1: two getxattr calls before any statx(FORCE_SYNC). +* i_acl == ACL_DONT_CACHE. __get_acl's cmpxchg(p, ACL_NOT_CACHED, +* sentinel) finds *p != ACL_NOT_CACHED on every call, so the sentinel +* is never placed and the result is never cached. Both calls must +* reach the daemon, proving ACL_DONT_CACHE suppresses caching. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,2 ); + TH_LOG("step 1 OK: both pre-trigger getxattrs reached daemon (count=%d), "tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 289
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+ "ACL_DONT_CACHE is working", count); + +/* +* Step 2: statx(AT_STATX_FORCE_SYNC). +* fuse_update_get_attr() calls forget_all_cached_acls() before sending +* FUSE_GETATTR. +* Buggy kernel: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled) +* Fixed kernel: ACL_DONT_CACHE preserved (no effect) +*/ +ASSERT_EQ (do_force_statx (self->file_path),0 );Why not calling statx(2) instead? Are you using a libc that doesn't include a wrapper?
(Also, I wonder if having the global state protected with a mutex is really needed in the scope of this test... but yeah, that's the correct thing to do, of course.)
Cheers,
--
LuísQuoted 63 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +59
-1+TH_LOG ("step 2 OK: statx(AT_STATX_FORCE_SYNC) succeeded" ); + +/* +* Step 3: getxattr — cache population attempt after the trigger. +* Buggy: *p == ACL_NOT_CACHED -> sentinel placed -> fuse_get_inode_acl +* called -> ACL_A parsed and stored in the kernel cache. +* Fixed: *p == ACL_DONT_CACHE -> sentinel placement skipped -> +* fuse_get_inode_acl called but result not cached. +* Either way the correct ACL_A is returned here. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,3 ); +TH_LOG ("step 3 OK: post-trigger getxattr reached daemon (count=%d), " +"returned correct ACL_A (%zd bytes)" , count, sz); + +/* +* Step 4: switch daemon to ACL_B (different size: 44 vs 28 bytes). +* Simulates an ACL change that fuse_set_acl() would NOT invalidate for +* !fc->posix_acl mounts (it skips forget_all_cached_acls in that case). +* On a fixed kernel the ACL was never cached, so this is moot. +*/ +pthread_mutex_lock (&g_ds.lock); + g_ds.acl = acl_b; + g_ds.acl_size =sizeof (acl_b); +pthread_mutex_unlock (&g_ds.lock); +TH_LOG ("step 4: daemon switched to ACL_B (%zu bytes)" ,sizeof (acl_b)); + +/* +* Step 5: getxattr — the decisive check. +* Buggy kernel: cache hit -> stale ACL_A (28 bytes), count stays 3. +* Fixed kernel: no cache -> daemon called -> ACL_B (44 bytes), count 4. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +if (sz == (ssize_t )sizeof (acl_a)) +TH_LOG ("step 5 BUG: stale ACL_A (%zd bytes) from kernel cache " +"(count=%d); ACL_DONT_CACHE corrupted by " +"forget_all_cached_acls()" , sz, count); +else +TH_LOG ("step 5 OK: daemon reached (count=%d), " +"fresh ACL_B (%zd bytes)" , count, sz); + +EXPECT_EQ (sz, (ssize_t )sizeof (acl_b)); +EXPECT_EQ (count,4 ); +} + +TEST_HARNESS_MAIN --2.54.0
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Quoted 37 lines · Luis Henriques
Hi!
On Wed, Jul 15 2026, Amir Goldstein wrote:
[...]
Quoted 8 lines
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Cool, thanks. I'll consider adapting my own tests to have a similar format as this one.
Quoted 6 lines
Quoted 6 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Quoted 7 lines
Quoted 3 lines
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.Oh! as usual my comment wasn't clear :-)
In fact, my comment wasn't really very relevant in the context of this patch as, in my opinion, I think it's OK for new tests to depend on fuse3.
Quoted 8 lines
I was talking about my own patch[1] which was converting the existing test, which depends on fuse2, to use fuse3. That's what I was referring to when talking about breaking CI: we could have a scenario where a test was being built and executed and, because it's now using fuse3, it can't be built any more.
CC test author
I think your concern is exaggerated
This test is 1 year old. Who runs this test and doesn't have fuse3?
Also, when speaking of regressions, test suites are usually the last of concerns - they always regularly break after kernel changes, because of "over expectations".
Test suit runners usually keep the test suite uptodate.
They are almost never deployed binary rpm packages where kernel regressions cause a problem.
And also, it's not critical, so you can always try and see if someone shouts (and then you can also shout back).
Quoted 4 lines
Quoted 2 lines
Thanks for the review!
I had two extra comments below, not sure you saw them ;-)
I did not :)
Quoted 120 lines
Cheers,
--
LuísQuoted 115 lines
Amir.
Quoted 113 lines
Quoted 99 lines
Signed-off-by Amir Goldstein <[email protected]>--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.ctools/testing/selftests/filesystems/fuse/Makefile +10
-0@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +60
-0@@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Test: FUSE ACL caching bug triggered by AT_STATX_FORCE_SYNC +* +* A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises every inode +* with i_acl = i_default_acl = ACL_DONT_CACHE. When a fresh stat is needed +* (e.g. AT_STATX_FORCE_SYNC), fuse_update_get_attr() calls +* forget_all_cached_acls() before issuing FUSE_GETATTR. On an unfixed kernel, +* __forget_cached_acl() replaces ACL_DONT_CACHE with ACL_NOT_CACHED, +* inadvertently enabling the kernel ACL cache for that inode. The next +* getxattr populates the cache. Because fuse_set_acl() skips +* forget_all_cached_acls() for !fc->posix_acl mounts, any subsequent change to +* the ACL leaves the stale kernel entry in place, and the next getxattr returns +* wrong data without ever reaching the FUSE daemon. +* +* Fix (fs/posix_acl.c): __forget_cached_acl() returns early when *p is +* ACL_DONT_CACHE, preserving the "never cache" invariant for the inode's +* lifetime. +* +* Test outline: +* 1. Mount a minimal FUSE fs (no FUSE_POSIX_ACL negotiated). +* 2. lgetxattr -> daemon called, ACL_A returned, NOT cached (ACL_DONT_CACHE). +* 3. statx(AT_STATX_FORCE_SYNC) -> forget_all_cached_acls() called. +* Buggy: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled). +* Fixed: ACL_DONT_CACHE preserved. +* 4. lgetxattr -> daemon called, ACL_A returned. +* Buggy: result now cached (ACL_NOT_CACHED -> cached). +* Fixed: result still not cached. +* 5. Daemon switches to ACL_B internally (different size). +* 6. lgetxattr -> should return ACL_B (44 bytes). +* Buggy: cache hit, returns stale ACL_A (28 bytes). FAIL. +* Fixed: no cache, daemon called, returns ACL_B (44 bytes). PASS. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <linux/limits.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/xattr.h> +#include <unistd.h> + +#define FUSE_USE_VERSION 31 +#include <fuse_lowlevel.h> + +#include "kselftest_harness.h" + +/* ---- ACL binary encoding ------------------------------------------------ */ +/* +* POSIX ACL v2 xattr format (little-endian): +* header: u32 version (= 0x00000002) +* entry: u16 tag | u16 perm | u32 id +* +* Entries must appear in tag-ascending order; named USER/GROUP entries tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 70
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+* require a MASK entry. Both ACLs pass posix_acl_from_xattr() validation. +*/ + +/* ACL_A: 3 entries (USER_OBJ:rwx, GROUP_OBJ:r-x, OTHER:r-x) = 28 bytes */ +static const uint8_t acl_a[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +};Wouldn't it be OK to add a dependency on libacl to make this more readable?
It would be ok, but TBH, the only fact relevant to this test is that acl_a and acl_b
are different. That is pretty easy to understand even without libacl...
Quoted 215 lines
Quoted 215 lines
Quoted 215 lines
Quoted 215 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +214
-0+ +/* +* ACL_B: 5 entries — adds USER uid=1 and MASK = 44 bytes. +* A named USER entry requires a MASK; all tags in ascending order. +*/ +static const uint8_t acl_b[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x02 ,0x00 ,0x07 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,/* USER uid=1 rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x10 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* MASK rwx */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +}; + +/* ---- Shared state (daemon thread <-> test thread) ----------------------- */ + +#define FILE_INO 2 +#define FILE_NAME "testfile" + +struct daemon_state { +pthread_mutex_t lock; +const uint8_t *acl; +size_t acl_size; +int getxattr_count; +}; + +/* +* Global: callbacks are stateless fns so we use a single global. +* Safe because only one test instance runs at a time. +*/ +static struct daemon_state g_ds = { + .lock =PTHREAD_MUTEX_INITIALIZER , +}; + +/* ---- FUSE lowlevel callbacks -------------------------------------------- */ + +static void fs_lookup (fuse_req_t req,fuse_ino_t parent,const char *name) +{ +if (parent !=FUSE_ROOT_ID ||strcmp (name,FILE_NAME )) { +fuse_reply_err (req,ENOENT ); +return ; + } +struct fuse_entry_param e = {}; + +/* +* Long attr/entry timeouts so that normal stat() calls do not +* expire and trigger forget_all_cached_acls() on their own; +* only the explicit AT_STATX_FORCE_SYNC should trigger it. +*/ + e.ino =FILE_INO ; + e.generation =1 ; + e.attr_timeout =10.0 ; + e.entry_timeout =10.0 ; + e.attr.st_ino =FILE_INO ; + e.attr.st_mode =S_IFREG |0644 ; + e.attr.st_nlink =1 ; +fuse_reply_entry (req, &e); +} + +static void fs_getattr (fuse_req_t req,fuse_ino_t ino, +struct fuse_file_info *fi) +{ +struct stat st = {}; + + (void )fi; +if (ino ==FUSE_ROOT_ID ) { + st.st_ino =FUSE_ROOT_ID ; + st.st_mode =S_IFDIR |0755 ; + st.st_nlink =2 ; + }else if (ino ==FILE_INO ) { + st.st_ino =FILE_INO ; + st.st_mode =S_IFREG |0644 ; + st.st_nlink =1 ; + }else { +fuse_reply_err (req,ENOENT ); +return ; + } +fuse_reply_attr (req, &st,10 ); +} + +static void fs_getxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +size_t size) +{ +if (ino !=FILE_INO || +strcmp (name,"system.posix_acl_access" ) !=0 ) { +fuse_reply_err (req,ENODATA ); +return ; + } + +pthread_mutex_lock (&g_ds.lock); +const uint8_t *acl = g_ds.acl; +size_t acl_size = g_ds.acl_size; + g_ds.getxattr_count++; +pthread_mutex_unlock (&g_ds.lock); + +if (size ==0 ) +fuse_reply_xattr (req, acl_size); +else if (size < acl_size) +fuse_reply_err (req,ERANGE ); +else +fuse_reply_buf (req, (const char *)acl, acl_size); +} + +static const struct fuse_lowlevel_ops fs_ops = { + .lookup = fs_lookup, + .getattr = fs_getattr, + .getxattr = fs_getxattr, +}; + +/* ---- Daemon thread ------------------------------------------------------- */ + +static void *run_daemon (void *arg) +{ +fuse_session_loop ((struct fuse_session *)arg); +return NULL ; +} + +/* ---- kselftest harness --------------------------------------------------- */ + +FIXTURE (acl_cache) { +struct fuse_session *se; +char mountpoint[PATH_MAX ]; +char file_path[PATH_MAX ]; +pthread_t thread; +}; + +FIXTURE_SETUP (acl_cache) +{ +char *fuse_argv[] = {"fuse_acl_cache_test" ,NULL }; +struct fuse_args args =FUSE_ARGS_INIT (1 , fuse_argv); + + g_ds.acl = acl_a; + g_ds.acl_size =sizeof (acl_a); + g_ds.getxattr_count =0 ; + +strcpy (self->mountpoint,"/tmp/acl_cache_test_XXXXXX" ); +if (!mkdtemp (self->mountpoint)) +SKIP (return,"mkdtemp: %s" ,strerror (errno)); + +snprintf (self->file_path,sizeof (self->file_path), +"%s/" FILE_NAME , self->mountpoint); + + self->se =fuse_session_new (&args, &fs_ops,sizeof (fs_ops),NULL ); +if (!self->se) { +rmdir (self->mountpoint); +SKIP (return,"fuse_session_new failed" ); + } + +if (fuse_session_mount (self->se, self->mountpoint)) { +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"fuse_session_mount failed " +"(missing fusermount3 or insufficient privileges)" ); + } + +if (pthread_create (&self->thread,NULL , run_daemon, self->se)) { +fuse_session_unmount (self->se); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"pthread_create: %s" ,strerror (errno)); + } + +fuse_opt_free_args (&args); +} + +FIXTURE_TEARDOWN (acl_cache) +{ +fuse_session_exit (self->se); +fuse_session_unmount (self->se); +pthread_join (self->thread,NULL ); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +} + +static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} + +TEST_F(acl_cache, stale_after_force_sync) +{ +char buf[512 ]; +ssize_t sz; +int count; + +/* +* Step 1: two getxattr calls before any statx(FORCE_SYNC). +* i_acl == ACL_DONT_CACHE. __get_acl's cmpxchg(p, ACL_NOT_CACHED, +* sentinel) finds *p != ACL_NOT_CACHED on every call, so the sentinel +* is never placed and the result is never cached. Both calls must +* reach the daemon, proving ACL_DONT_CACHE suppresses caching. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,2 ); +TH_LOG ("step 1 OK: both pre-trigger getxattrs reached daemon (count=%d), " +"ACL_DONT_CACHE is working" , count); + +/* +* Step 2: statx(AT_STATX_FORCE_SYNC). +* fuse_update_get_attr() calls forget_all_cached_acls() before sending
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 289
Quoted 5 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +5
-0+* FUSE_GETATTR. +* Buggy kernel: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled) +* Fixed kernel: ACL_DONT_CACHE preserved (no effect) +*/ +ASSERT_EQ (do_force_statx (self->file_path),0 );Why not calling statx(2) instead? Are you using a libc that doesn't include a wrapper?
No reason. Will change to statx()
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(Also, I wonder if having the global state protected with a mutex is really needed in the scope of this test... but yeah, that's the correct thing to do, of course.)
When writing a test I try to optimize for code clarity and development speed nothing else.
Obviously I assist agents when writing tests - would be stupid not to, so when I review I stick to validating correctness and readability.
That's the true reason for syscall(SYS_statx,
I was something correct, I didn't bother to fix it, but your comment was in place.
Thanks for the review.
Amir.
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Quoted 44 lines · Amir Goldstein
Quoted 43 lines · Luis Henriques
Hi!
On Wed, Jul 15 2026, Amir Goldstein wrote:
[...]
Quoted 8 lines
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Cool, thanks. I'll consider adapting my own tests to have a similar format as this one.
Quoted 6 lines
Quoted 6 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Quoted 7 lines
Quoted 3 lines
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.Oh! as usual my comment wasn't clear :-)
In fact, my comment wasn't really very relevant in the context of this patch as, in my opinion, I think it's OK for new tests to depend on fuse3.
I was talking about my own patch[1] which was converting the existing test, which depends on fuse2, to use fuse3. That's what I was referring to when talking about breaking CI: we could have a scenario where a test was being built and executed and, because it's now using fuse3, it can't be built any more.
Quoted 6 lines
Quoted 2 lines
CC test author
I think your concern is exaggerated
This test is 1 year old. Who runs this test and doesn't have fuse3?
A quick note on why it used fuse2 to begin with: back then there was no fuse3-based test anywhere in the selftests tree. Amir actually pointed me to tools/testing/selftests/memfd [1] as the reference to follow, and that one (both its Makefile and fuse_mnt.c) is built on libfuse2, so I simply copied that pattern for consistency.
Thanks,
Chen Linxuan
[1] https://lore.kernel.org/all/CAOQ4uxjKFXOKQxPpxtS6G_nR0tpw95w0GiO68UcWg_OBhmSY=Q@mail.gmail.com/
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
Quoted 52 lines · Chen Linxuan
Quoted 44 lines · Amir Goldstein
Quoted 43 lines · Luis Henriques
Hi!
On Wed, Jul 15 2026, Amir Goldstein wrote:
[...]
Quoted 8 lines
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Cool, thanks. I'll consider adapting my own tests to have a similar format as this one.
Quoted 6 lines
Quoted 6 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Quoted 7 lines
Quoted 3 lines
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.Oh! as usual my comment wasn't clear :-)
In fact, my comment wasn't really very relevant in the context of this patch as, in my opinion, I think it's OK for new tests to depend on fuse3.
I was talking about my own patch[1] which was converting the existing test, which depends on fuse2, to use fuse3. That's what I was referring to when talking about breaking CI: we could have a scenario where a test was being built and executed and, because it's now using fuse3, it can't be built any more.
Quoted 6 lines
Quoted 2 lines
CC test author
I think your concern is exaggerated
This test is 1 year old. Who runs this test and doesn't have fuse3?
Quoted 5 lines
A quick note on why it used fuse2 to begin with: back then there was no fuse3-based test anywhere in the selftests tree. Amir actually pointed me to
tools/testing/selftests/memfd[1] as the reference to follow, and that one (both itsMakefileandfuse_mnt.c) is built on libfuse2, so I simply copied that pattern for consistency.
Ah! I wasn't aware there was yet another selftest relying on fuse2. Thanks a lot for pointing that out. It now makes sense -- that's a very old test.
Cheers,
Signature · 1 line
Luís
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
In reply to Amir GoldsteinQuoted 55 lines · Amir Goldstein
Quoted 37 lines · Luis Henriques
Hi!
On Wed, Jul 15 2026, Amir Goldstein wrote:
[...]
Quoted 8 lines
Quoted 4 lines
A question: in my own tests I have been following a different approach, where the filesystem is mounted from a shell script that does the actual testing. I wonder if you think your approach (i.e. writing the actual test in C, using the kselftest macros) is the preferred one.
I always prefer a standalone C program for a selftest. For fstests naturally the tests are done by the shell scripts, so it really depends.
Cool, thanks. I'll consider adapting my own tests to have a similar format as this one.
Quoted 6 lines
Quoted 6 lines
Anyway, the test looks good to me, and I just have a few minor comments below.
Quoted 2 lines
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Quoted 7 lines
Quoted 3 lines
Yeah, I've sent a patch recently converting the only existing test to also user fuse3 instead. Not sure that's acceptable though, since there's a chance of breaking some CI.
I mean if the test is not built it wont run, so I don't know why CI would fail. Obviously some systems do not have libfuse. That's fine by me
not everyone needs to run all the tests.Oh! as usual my comment wasn't clear :-)
In fact, my comment wasn't really very relevant in the context of this patch as, in my opinion, I think it's OK for new tests to depend on fuse3.
Quoted 8 lines
I was talking about my own patch[1] which was converting the existing test, which depends on fuse2, to use fuse3. That's what I was referring to when talking about breaking CI: we could have a scenario where a test was being built and executed and, because it's now using fuse3, it can't be built any more.
CC test author
I think your concern is exaggerated
This test is 1 year old. Who runs this test and doesn't have fuse3?Also, when speaking of regressions, test suites are usually the last of concerns - they always regularly break after kernel changes, because of "over expectations".
Quoted 7 lines
Test suit runners usually keep the test suite uptodate.
They are almost never deployed binary rpm packages where kernel regressions cause a problem.
And also, it's not critical, so you can always try and see if someone shouts (and then you can also shout back).
OK, understood. And I agree, of course (otherwise, why would I send the patch to convert it to fuse3? :-)
Also, I'll make sure I'll CC the test author in next rev (I don't think I did included him in v1). And thank you, Amir!
Cheers,
Signature · 1 line
Luís
Quoted 374 lines
Quoted 3 lines
Quoted 1 line
Thanks for the review!
I had two extra comments below, not sure you saw them ;-)
I did not :)
Quoted 120 lines
Cheers,
--
LuísQuoted 115 lines
Amir.
Quoted 113 lines
Quoted 99 lines
Signed-off-by Amir Goldstein <[email protected]>--- .../selftests/filesystems/fuse/Makefile | 10 + .../filesystems/fuse/fuse_acl_cache_test.c | 348 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.ctools/testing/selftests/filesystems/fuse/Makefile +10
-0@@ -5,6 +5,13 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES) TEST_GEN_PROGS := fusectl_test TEST_GEN_FILES := fuse_mnt +# fuse_acl_cache_test requires libfuse3; add it only when the library is present. +ACL_CFLAGS := $(shell pkg-config fuse3 --cflags 2>/dev/null) +ACL_LDLIBS := $(shell pkg-config fuse3 --libs 2>/dev/null) +ifneq ($(ACL_CFLAGS),) +TEST_GEN_PROGS += fuse_acl_cache_test +endif + include ../../lib.mk VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)@@ -19,3 +26,6 @@ endif $(OUTPUT)/fuse_mnt: CFLAGS += $(VAR_CFLAGS) $(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS) + +$(OUTPUT)/fuse_acl_cache_test: CFLAGS += $(ACL_CFLAGS) +$(OUTPUT)/fuse_acl_cache_test: LDLIBS += $(ACL_LDLIBS)tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +60
-0@@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Test: FUSE ACL caching bug triggered by AT_STATX_FORCE_SYNC +* +* A FUSE mount that does not negotiate FUSE_POSIX_ACL initialises every inode +* with i_acl = i_default_acl = ACL_DONT_CACHE. When a fresh stat is needed +* (e.g. AT_STATX_FORCE_SYNC), fuse_update_get_attr() calls +* forget_all_cached_acls() before issuing FUSE_GETATTR. On an unfixed kernel, +* __forget_cached_acl() replaces ACL_DONT_CACHE with ACL_NOT_CACHED, +* inadvertently enabling the kernel ACL cache for that inode. The next +* getxattr populates the cache. Because fuse_set_acl() skips +* forget_all_cached_acls() for !fc->posix_acl mounts, any subsequent change to +* the ACL leaves the stale kernel entry in place, and the next getxattr returns +* wrong data without ever reaching the FUSE daemon. +* +* Fix (fs/posix_acl.c): __forget_cached_acl() returns early when *p is +* ACL_DONT_CACHE, preserving the "never cache" invariant for the inode's +* lifetime. +* +* Test outline: +* 1. Mount a minimal FUSE fs (no FUSE_POSIX_ACL negotiated). +* 2. lgetxattr -> daemon called, ACL_A returned, NOT cached (ACL_DONT_CACHE). +* 3. statx(AT_STATX_FORCE_SYNC) -> forget_all_cached_acls() called. +* Buggy: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled). +* Fixed: ACL_DONT_CACHE preserved. +* 4. lgetxattr -> daemon called, ACL_A returned. +* Buggy: result now cached (ACL_NOT_CACHED -> cached). +* Fixed: result still not cached. +* 5. Daemon switches to ACL_B internally (different size). +* 6. lgetxattr -> should return ACL_B (44 bytes). +* Buggy: cache hit, returns stale ACL_A (28 bytes). FAIL. +* Fixed: no cache, daemon called, returns ACL_B (44 bytes). PASS. +*/ + +#define _GNU_SOURCE +#include <errno.h> +#include <fcntl.h> +#include <linux/limits.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/syscall.h> +#include <sys/xattr.h> +#include <unistd.h> + +#define FUSE_USE_VERSION 31 +#include <fuse_lowlevel.h> + +#include "kselftest_harness.h" + +/* ---- ACL binary encoding ------------------------------------------------ */ +/* +* POSIX ACL v2 xattr format (little-endian): +* header: u32 version (= 0x00000002) +* entry: u16 tag | u16 perm | u32 id +* +* Entries must appear in tag-ascending order; named USER/GROUP entries tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 70
Quoted 10 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +10
-0+* require a MASK entry. Both ACLs pass posix_acl_from_xattr() validation. +*/ + +/* ACL_A: 3 entries (USER_OBJ:rwx, GROUP_OBJ:r-x, OTHER:r-x) = 28 bytes */ +static const uint8_t acl_a[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +};Wouldn't it be OK to add a dependency on libacl to make this more readable?
It would be ok, but TBH, the only fact relevant to this test is that
acl_aandacl_b
are different. That is pretty easy to understand even without libacl...Quoted 215 lines
Quoted 215 lines
Quoted 215 lines
Quoted 215 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +214
-0+ +/* +* ACL_B: 5 entries — adds USER uid=1 and MASK = 44 bytes. +* A named USER entry requires a MASK; all tags in ascending order. +*/ +static const uint8_t acl_b[] = { +0x02 ,0x00 ,0x00 ,0x00 ,/* v2 header */ +0x01 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* USER_OBJ rwx */ +0x02 ,0x00 ,0x07 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 ,/* USER uid=1 rwx */ +0x04 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* GROUP_OBJ r-x */ +0x10 ,0x00 ,0x07 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* MASK rwx */ +0x20 ,0x00 ,0x05 ,0x00 ,0xff ,0xff ,0xff ,0xff ,/* OTHER r-x */ +}; + +/* ---- Shared state (daemon thread <-> test thread) ----------------------- */ + +#define FILE_INO 2 +#define FILE_NAME "testfile" + +struct daemon_state { +pthread_mutex_t lock; +const uint8_t *acl; +size_t acl_size; +int getxattr_count; +}; + +/* +* Global: callbacks are stateless fns so we use a single global. +* Safe because only one test instance runs at a time. +*/ +static struct daemon_state g_ds = { + .lock =PTHREAD_MUTEX_INITIALIZER , +}; + +/* ---- FUSE lowlevel callbacks -------------------------------------------- */ + +static void fs_lookup (fuse_req_t req,fuse_ino_t parent,const char *name) +{ +if (parent !=FUSE_ROOT_ID ||strcmp (name,FILE_NAME )) { +fuse_reply_err (req,ENOENT ); +return ; + } +struct fuse_entry_param e = {}; + +/* +* Long attr/entry timeouts so that normal stat() calls do not +* expire and trigger forget_all_cached_acls() on their own; +* only the explicit AT_STATX_FORCE_SYNC should trigger it. +*/ + e.ino =FILE_INO ; + e.generation =1 ; + e.attr_timeout =10.0 ; + e.entry_timeout =10.0 ; + e.attr.st_ino =FILE_INO ; + e.attr.st_mode =S_IFREG |0644 ; + e.attr.st_nlink =1 ; +fuse_reply_entry (req, &e); +} + +static void fs_getattr (fuse_req_t req,fuse_ino_t ino, +struct fuse_file_info *fi) +{ +struct stat st = {}; + + (void )fi; +if (ino ==FUSE_ROOT_ID ) { + st.st_ino =FUSE_ROOT_ID ; + st.st_mode =S_IFDIR |0755 ; + st.st_nlink =2 ; + }else if (ino ==FILE_INO ) { + st.st_ino =FILE_INO ; + st.st_mode =S_IFREG |0644 ; + st.st_nlink =1 ; + }else { +fuse_reply_err (req,ENOENT ); +return ; + } +fuse_reply_attr (req, &st,10 ); +} + +static void fs_getxattr (fuse_req_t req,fuse_ino_t ino,const char *name, +size_t size) +{ +if (ino !=FILE_INO || +strcmp (name,"system.posix_acl_access" ) !=0 ) { +fuse_reply_err (req,ENODATA ); +return ; + } + +pthread_mutex_lock (&g_ds.lock); +const uint8_t *acl = g_ds.acl; +size_t acl_size = g_ds.acl_size; + g_ds.getxattr_count++; +pthread_mutex_unlock (&g_ds.lock); + +if (size ==0 ) +fuse_reply_xattr (req, acl_size); +else if (size < acl_size) +fuse_reply_err (req,ERANGE ); +else +fuse_reply_buf (req, (const char *)acl, acl_size); +} + +static const struct fuse_lowlevel_ops fs_ops = { + .lookup = fs_lookup, + .getattr = fs_getattr, + .getxattr = fs_getxattr, +}; + +/* ---- Daemon thread ------------------------------------------------------- */ + +static void *run_daemon (void *arg) +{ +fuse_session_loop ((struct fuse_session *)arg); +return NULL ; +} + +/* ---- kselftest harness --------------------------------------------------- */ + +FIXTURE (acl_cache) { +struct fuse_session *se; +char mountpoint[PATH_MAX ]; +char file_path[PATH_MAX ]; +pthread_t thread; +}; + +FIXTURE_SETUP (acl_cache) +{ +char *fuse_argv[] = {"fuse_acl_cache_test" ,NULL }; +struct fuse_args args =FUSE_ARGS_INIT (1 , fuse_argv); + + g_ds.acl = acl_a; + g_ds.acl_size =sizeof (acl_a); + g_ds.getxattr_count =0 ; + +strcpy (self->mountpoint,"/tmp/acl_cache_test_XXXXXX" ); +if (!mkdtemp (self->mountpoint)) +SKIP (return,"mkdtemp: %s" ,strerror (errno)); + +snprintf (self->file_path,sizeof (self->file_path), +"%s/" FILE_NAME , self->mountpoint); + + self->se =fuse_session_new (&args, &fs_ops,sizeof (fs_ops),NULL ); +if (!self->se) { +rmdir (self->mountpoint); +SKIP (return,"fuse_session_new failed" ); + } + +if (fuse_session_mount (self->se, self->mountpoint)) { +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"fuse_session_mount failed " +"(missing fusermount3 or insufficient privileges)" ); + } + +if (pthread_create (&self->thread,NULL , run_daemon, self->se)) { +fuse_session_unmount (self->se); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +SKIP (return,"pthread_create: %s" ,strerror (errno)); + } + +fuse_opt_free_args (&args); +} + +FIXTURE_TEARDOWN (acl_cache) +{ +fuse_session_exit (self->se); +fuse_session_unmount (self->se); +pthread_join (self->thread,NULL ); +fuse_session_destroy (self->se); +rmdir (self->mountpoint); +} + +static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} + +TEST_F(acl_cache, stale_after_force_sync) +{ +char buf[512 ]; +ssize_t sz; +int count; + +/* +* Step 1: two getxattr calls before any statx(FORCE_SYNC). +* i_acl == ACL_DONT_CACHE. __get_acl's cmpxchg(p, ACL_NOT_CACHED, +* sentinel) finds *p != ACL_NOT_CACHED on every call, so the sentinel +* is never placed and the result is never cached. Both calls must +* reach the daemon, proving ACL_DONT_CACHE suppresses caching. +*/ + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + + sz =lgetxattr (self->file_path,"system.posix_acl_access" , + buf,sizeof (buf)); +ASSERT_EQ (sz, (ssize_t )sizeof (acl_a)); + +pthread_mutex_lock (&g_ds.lock); + count = g_ds.getxattr_count; +pthread_mutex_unlock (&g_ds.lock); + +ASSERT_EQ (count,2 ); +TH_LOG ("step 1 OK: both pre-trigger getxattrs reached daemon (count=%d), " +"ACL_DONT_CACHE is working" , count); + +/* +* Step 2: statx(AT_STATX_FORCE_SYNC). +* fuse_update_get_attr() calls forget_all_cached_acls() before sending Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 289
Quoted 5 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +5
-0+* FUSE_GETATTR. +* Buggy kernel: ACL_DONT_CACHE -> ACL_NOT_CACHED (cache enabled) +* Fixed kernel: ACL_DONT_CACHE preserved (no effect) +*/ +ASSERT_EQ (do_force_statx (self->file_path),0 );Why not calling statx(2) instead? Are you using a libc that doesn't include a wrapper?
No reason. Will change to
statx()Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(Also, I wonder if having the global state protected with a mutex is really needed in the scope of this test... but yeah, that's the correct thing to do, of course.)
When writing a test I try to optimize for code clarity and development speed nothing else.
Obviously I assist agents when writing tests - would be stupid not to, so when I review I stick to validating correctness and readability.
That's the true reason for syscall(SYS_statx,
I was something correct, I didn't bother to fix it, but your comment was in place.Thanks for the review.
Amir.
Re: [PATCH 2/2] selftests/fuse: add ACL_DONT_CACHE regression test
In reply to Amir GoldsteinQuoted 29 lines · Amir Goldstein
Add a test that reproduces the stale ACL bug fixed by:
"fs: preserveACL_DONT_CACHEstate inforget_cached_acl()"A FUSE mount that does not negotiate
FUSE_POSIX_ACLinitialises inodes withi_acl=ACL_DONT_CACHE. Before the fix, callingforget_all_cached_acls()(e.g. fromfuse_update_get_attr()on astatx(AT_STATX_FORCE_SYNC)) would silently replaceACL_DONT_CACHEwithACL_NOT_CACHED, enabling the kernel ACL cache. A subsequent getxattr would populate the cache, and becausefuse_set_acl()skipsforget_all_cached_acls()for !fc->posix_acl, later ACL changes were not visible to callers — getxattr returned stale data.The test mounts a minimal libfuse3 lowlevel filesystem (no
FUSE_POSIX_ACLnegotiated) and:
1. Issues two getxattrs — both must reach the daemon, provingACL_DONT_CACHEsuppresses caching before any trigger.
2. Callsstatx(AT_STATX_FORCE_SYNC)to triggerforget_all_cached_acls().
3. Issues another getxattr (populates the cache on a buggy kernel).
4. Switches the daemon to a different-sized ACL (ACL_B).
5. Issues a final getxattr — expectsACL_B(44 bytes) and daemon call count 4; a buggy kernel returns staleACL_A(28 bytes).
fuse_acl_cache_testis only built when libfuse3 is detected via pkg-config.Signed-off-by Amir Goldstein <[email protected]>---
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c line 252
Quoted 8 lines
tools/testing/selftests/filesystems/fuse/fuse_acl_cache_test.c new file +8
-0+static int do_force_statx (const char *path) +{ +struct statx stx; + +return syscall (SYS_statx,AT_FDCWD , path, +AT_STATX_FORCE_SYNC ,STATX_BASIC_STATS , &stx); +} +
Christian,
If no other comments require me sending v2, please change this to plain statx() on commit.
Thanks Luis for pointing this out.
Amir.
Quoted 8 lines · Amir Goldstein
Fix for unintended FUSE ACL cache
Christian,
I bet you did not miss fuse acls...
Ghosts from the past have come back to haunt me now.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/2] fs: preserve ACL_DONT_CACHE state in forget_cached_acl()
https://git.kernel.org/vfs/vfs/c/85f9e659787f [2/2] selftests/fuse: add ACL_DONT_CACHE regression test https://git.kernel.org/vfs/vfs/c/67eca5c355be
Quoted 5 lines · Christian Brauner
Quoted 4 lines · Amir Goldstein
Fix for unintended FUSE ACL cache
Christian,
Quoted 7 lines
Quoted 4 lines
I bet you did not miss fuse acls...
Ghosts from the past have come back to haunt me now.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Maybe I'm just being too eager, but I don't see these patches in that tree. Is there a delay between this email and the actual branch update, or did this patch got dropped somehow?
Cheers,
Signature · 1 line
Luís
Quoted 16 lines
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes[1/2] fs: preserve
ACL_DONT_CACHEstate inforget_cached_acl()https://git.kernel.org/vfs/vfs/c/85f9e659787f [2/2] selftests/fuse: add ACL_DONT_CACHE regression test https://git.kernel.org/vfs/vfs/c/67eca5c355be