A write to the cache that completes short, rather than returning an error, causes netfs to oops in iov_iter_revert() while attempting to retry the remainder. netfs_write_subrequest_terminated() unconditionally sets NETFS_SREQ_NEED_RETRY whenever a write subrequest transfers fewer bytes than requested, treating server uploads and cache writes identically. However, a write to the cache is a direct I/O write through a rolling buffer. When the retry path calls netfs_reset_iter(), it attempts to revert the iterator over the already-transferred bytes. Because spent buffer elements have already been freed, iov_iter_revert() walks off the start of the buffer, leading to a NULL pointer dereference: BUG: kernel NULL pointer dereference, address: 000000000000011e RIP: 0010:iov_iter_revert+0x12d/0x210 Call Trace: netfs_reset_iter+0x4d/0x90 [netfs] netfs_retry_write_stream+0x14a/0x7e0 [netfs] netfs_retry_writes+0x11b/0x170 [netfs] netfs_collect_write_results+0x189/0x7f0 [netfs] netfs_write_collection+0x1d/0x3e0 [netfs] netfs_write_collection_worker+0x92/0x110 [netfs] This fatal crash kills the kernel worker thread (leaving the kernel tainted with D for DIE) and leaks request references. As a result, subsequent operations such as unmounting the filesystem hang permanently in uninterruptible sleep (D state), requiring a hard reset. This condition is reachable from userspace by any task with a file-size limit. Since commit e2d46f2ec332 ("netfs: Change the read result collector to only use one work item"), the copy to the cache for a read runs in the reading task's context, subjecting the cache write to that task's RLIMIT_FSIZE. While the write truncation occurs in the reading task, the retry and resulting crash occur in the background collection worker. Because lowering RLIMIT_FSIZE requires no privileges, any user on a system where cache writes are permitted can trigger this. When generic_write_check_limits() clamps a write to a non-block-aligned file size limit: *count = min(*count, limit - pos); the write completes short with a positive byte count rather than returning -EFBIG, triggering the retry path in the worker. Writes to the cache cannot be meaningfully retried: direct I/O requires block alignment, so the unaligned remainder cannot be reissued, and any subsequent attempt would hit the same limit. Furthermore, cache writes are best-effort; on an explicit write error, netfs cancels the subrequest and invalidates the cache. Handle a truncated cache write the same way: cancel the subrequest, stop further cache I/O on the request, and mark the cache for invalidation, rather than setting NETFS_SREQ_NEED_RETRY. Writes to the server are unaffected. Fixes: e2d46f2ec332 ("netfs: Change the read result collector to only use one work item") Cc: stable@vger.kernel.org Signed-off-by: Kim Sondrup --- Notes: - Context: I encountered this oops while investigating and testing a separate SELinux credentials issue in cachefiles on the same copy-to-cache path (where an unprivileged container was blocked by SELinux from issuing the cache write, masking the bug). That credentials fix will follow this one. However, this netfs fix stands completely on its own: on systems without SELinux (or where the task is permitted to write to the cache), any user can trigger the crash today simply by setting a non-block-aligned RLIMIT_FSIZE. - Testing: Verified by before/after reproduction on 7.4.0-rc5 (Fedora Rawhide) with ulimit -f 1025 reading an NFS file through a MADV_RANDOM mapping: * Unpatched: triggers the NULL dereference in iov_iter_revert, kills the kworker, taints the kernel with DIE, and leaves subsequent umount permanently wedged in D state. * Patched: runs without an oops, no D-state processes, umount succeeds cleanly, and subsequent reads verify data integrity with matching SHA-256. Compile-tested with W=1 on 7.4-rc5 (clean, no new warnings). - Backport note: This patch reuses netfs cache-write error symbols (NETFS_SREQ_CANCELLED, NETFS_RREQ_CACHE_STOP, NETFS_RREQ_CACHE_ERROR) from the error-path rework that landed between 7.3 and 7.4-rc5. While older trees (6.14..7.3) have the underlying bug, this patch will not cherry-pick cleanly to them and will require an adapted backport mirroring that tree's cache-write error handling. - AI disclosure: This analysis, fix, and commit message were heavily developed with AI assistance (Claude, Gemini). I am a Linux sysadmin and platform engineer rather than a kernel C developer; my primary role was acting as a skeptic, catching blind spots, cross-examining the findings, and validating the (mis)behavior on test VMs. fs/netfs/write_collect.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/fs/netfs/write_collect.c b/fs/netfs/write_collect.c index 6e8ea5342..3c8402b85 100644 --- a/fs/netfs/write_collect.c +++ b/fs/netfs/write_collect.c @@ -599,8 +599,25 @@ void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error) subreq->error = 0; subreq->transferred += transferred_or_error; - if (subreq->transferred < subreq->len) - set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags); + if (subreq->transferred < subreq->len) { + /* + * Writes to the cache are best-effort and cannot be + * resumed if truncated: the remainder is not block-aligned + * for DIO, and the buffer iterator cannot be rewound. + * Cancel and invalidate the cache as we do on error, + * rather than retrying. + */ + if (subreq->source == NETFS_WRITE_TO_CACHE) { + netfs_stat(&netfs_n_wh_write_failed); + __set_bit(NETFS_SREQ_CANCELLED, &subreq->flags); + set_bit(NETFS_RREQ_CACHE_STOP, &wreq->flags); + if (!test_and_set_bit(NETFS_RREQ_CACHE_ERROR, &wreq->flags)) + trace_netfs_rreq(wreq, netfs_rreq_trace_cache_failed); + subreq->transferred = subreq->len; + } else { + set_bit(NETFS_SREQ_NEED_RETRY, &subreq->flags); + } + } } trace_netfs_sreq(subreq, netfs_sreq_trace_terminated); base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790 -- 2.55.0