When a confined task (such as a container or VM running under container_t) reads from a network filesystem with fscache under SELinux enforcing, every cache read fails and data is refetched from the server. Furthermore, since commit e2d46f2ec332 ("netfs: Change the read result collector to only use one work item"), in-task copy-to-cache writes (such as through memory mappings) are denied as well. Each failure logs an SELinux denial: avc: denied { read } for pid=... comm=... scontext=system_u:system_r:container_t:s0:c355,c657 tcontext=system_u:object_r:cachefiles_var_t:s0 tclass=file Cachefiles opens backing cache files with the cache's credentials, and Documentation/filesystems/caching/cachefiles.rst ("Security Model and SELinux") specifies that it should switch to those credentials when accessing the cache. However, cachefiles_read() and __cachefiles_write() do not switch, so vfs_iocb_iter_read() and vfs_iocb_iter_write() revalidate access against the calling task's SID via security_file_permission(), which gets denied. Switch to the cache's credentials around the read and write operations, matching cachefiles_prepare_write() and cachefiles_issue_write(). Primary access checks remain enforced on the network filesystem inode, so the task gains no unauthorized access. With this change, cache reads and writes succeed under SELinux enforcing with zero denials. Fixes: 047487c947e8 ("cachefiles: Implement the I/O routines") Cc: stable@vger.kernel.org Signed-off-by: Kim Sondrup --- Notes: - Testing: Tested on an NFS server VM and client VM running SELinux enforcing with cachefilesd, mounting with -o vers=4.2,fsc. A podman container (container_t) reads a file twice, dropping the page cache in between. On 7.4.0-rc5 (Fedora Rawhide): 1) Reading 512 MiB with dd (readahead): cache reads failed NFS READs AVC denials without the patch 1022 1022 1024 1022 with the patch 1023 0 1 0 2) Reading 64 MiB through a MADV_RANDOM mapping (netfs_read_folio): first read second read cache writes failed cache reads NFS READs without the patch 16384 16384 0 16384 with the patch 16384 0 16384 0 On CentOS Stream 9 (5.14.0-769.el9): 1024 of 1024 cache reads failed without the patch, 0 with it. On the Fedora debug kernel (PROVE_LOCKING, KASAN, DEBUG_ATOMIC_SLEEP), both tests pass with no KASAN or sleeping-in-atomic reports. - Note on netfs ordering & dependencies: Moving the cache write into the reading task also allows a confined reader to reach a pre-existing netfs bug where a truncated cache write (e.g. from an unaligned RLIMIT_FSIZE) causes a NULL pointer dereference in netfs_retry_write_stream(). That fix stands independently and has been posted to: https://lore.kernel.org/netfs/20261203104718.2-1-kim@sondrup.io/ - AI disclosure: Tracked down, investigated, and drafted with AI assistance (Claude, Gemini). I have reviewed the code change myself; it cleanly follows the existing cachefiles_begin_secure() / cachefiles_end_secure() pattern already used elsewhere in cachefiles. fs/cachefiles/io.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c index d05059822..3f68faf19 100644 --- a/fs/cachefiles/io.c +++ b/fs/cachefiles/io.c @@ -83,6 +83,7 @@ static int cachefiles_read(struct netfs_cache_resources *cres, { struct cachefiles_object *object; struct cachefiles_kiocb *ki; + const struct cred *saved_cred; struct file *file; unsigned int old_nofs; ssize_t ret = -ENOBUFS; @@ -159,8 +160,11 @@ static int cachefiles_read(struct netfs_cache_resources *cres, trace_cachefiles_read(object, file_inode(file), ki->iocb.ki_pos, len - skipped); old_nofs = memalloc_nofs_save(); ret = cachefiles_inject_read_error(); - if (ret == 0) + if (ret == 0) { + cachefiles_begin_secure(object->volume->cache, &saved_cred); ret = vfs_iocb_iter_read(file, &ki->iocb, iter); + cachefiles_end_secure(object->volume->cache, saved_cred); + } memalloc_nofs_restore(old_nofs); switch (ret) { case -EIOCBQUEUED: @@ -310,6 +314,7 @@ int __cachefiles_write(struct cachefiles_object *object, { struct cachefiles_cache *cache; struct cachefiles_kiocb *ki; + const struct cred *saved_cred; unsigned int old_nofs; ssize_t ret; size_t len = iov_iter_count(iter); @@ -351,8 +356,11 @@ int __cachefiles_write(struct cachefiles_object *object, trace_cachefiles_write(object, file_inode(file), ki->iocb.ki_pos, len); old_nofs = memalloc_nofs_save(); ret = cachefiles_inject_write_error(); - if (ret == 0) + if (ret == 0) { + cachefiles_begin_secure(cache, &saved_cred); ret = vfs_iocb_iter_write(file, &ki->iocb, iter); + cachefiles_end_secure(cache, saved_cred); + } memalloc_nofs_restore(old_nofs); switch (ret) { case -EIOCBQUEUED: base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790 prerequisite-patch-id: f31e2ece0968c9e7f898df7223a32b9a39fa7f79 -- 2.55.0