linux-stable/fs/nfs
Josef Bacik 3abc2d160e nfs: fix UAF in direct writes
[ Upstream commit 17f46b803d ]

In production we have been hitting the following warning consistently

------------[ cut here ]------------
refcount_t: underflow; use-after-free.
WARNING: CPU: 17 PID: 1800359 at lib/refcount.c:28 refcount_warn_saturate+0x9c/0xe0
Workqueue: nfsiod nfs_direct_write_schedule_work [nfs]
RIP: 0010:refcount_warn_saturate+0x9c/0xe0
PKRU: 55555554
Call Trace:
 <TASK>
 ? __warn+0x9f/0x130
 ? refcount_warn_saturate+0x9c/0xe0
 ? report_bug+0xcc/0x150
 ? handle_bug+0x3d/0x70
 ? exc_invalid_op+0x16/0x40
 ? asm_exc_invalid_op+0x16/0x20
 ? refcount_warn_saturate+0x9c/0xe0
 nfs_direct_write_schedule_work+0x237/0x250 [nfs]
 process_one_work+0x12f/0x4a0
 worker_thread+0x14e/0x3b0
 ? ZSTD_getCParams_internal+0x220/0x220
 kthread+0xdc/0x120
 ? __btf_name_valid+0xa0/0xa0
 ret_from_fork+0x1f/0x30

This is because we're completing the nfs_direct_request twice in a row.

The source of this is when we have our commit requests to submit, we
process them and send them off, and then in the completion path for the
commit requests we have

if (nfs_commit_end(cinfo.mds))
	nfs_direct_write_complete(dreq);

However since we're submitting asynchronous requests we sometimes have
one that completes before we submit the next one, so we end up calling
complete on the nfs_direct_request twice.

The only other place we use nfs_generic_commit_list() is in
__nfs_commit_inode, which wraps this call in a

nfs_commit_begin();
nfs_commit_end();

Which is a common pattern for this style of completion handling, one
that is also repeated in the direct code with get_dreq()/put_dreq()
calls around where we process events as well as in the completion paths.

Fix this by using the same pattern for the commit requests.

Before with my 200 node rocksdb stress running this warning would pop
every 10ish minutes.  With my patch the stress test has been running for
several hours without popping.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Cc: stable@vger.kernel.org
Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-04-03 15:19:34 +02:00
..
blocklayout pNFS: Fix the pnfs block driver's calculation of layoutget size 2024-01-25 15:27:23 -08:00
filelayout pNFS/filelayout: Fix coalescing test for single DS 2023-01-24 07:24:30 +01:00
flexfilelayout nfs: fix panic when nfs4_ff_layout_prepare_ds() fails 2024-03-26 18:20:57 -04:00
Kconfig NFS: Replace readdir's use of xxhash() with hash_64() 2022-04-07 16:19:47 -04:00
Makefile nfs: Convert to new fscache volume/cookie API 2022-01-10 11:53:25 +00:00
cache_lib.c
cache_lib.h
callback.c NFSD: Move svc_serv_ops::svo_function into struct svc_serv 2022-02-28 10:26:40 -05:00
callback.h NFSv4.1: Fix uninitialised variable in devicenotify 2022-01-06 14:00:20 -05:00
callback_proc.c pNFS: Avoid a live lock condition in pnfs_update_layout() 2022-06-06 11:53:55 -04:00
callback_xdr.c SUNRPC: Parametrize how much of argsize should be zeroed 2022-09-26 14:02:42 -04:00
client.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
delegation.c NFSv4: Fix a potential state reclaim deadlock 2022-10-27 15:52:10 -04:00
delegation.h
dir.c nfs: Remove redundant null checks before kfree 2022-10-27 15:52:10 -04:00
direct.c nfs: fix UAF in direct writes 2024-04-03 15:19:34 +02:00
dns_resolve.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
dns_resolve.h NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
export.c nfsd: allow reaping files still under writeback 2024-03-26 18:20:23 -04:00
file.c NFS Client Updates for Linux 6.1 2022-10-13 09:58:42 -07:00
fs_context.c nfs: fix possible null-ptr-deref when parsing param 2022-12-31 13:33:04 +01:00
fscache.c mm, netfs, fscache: stop read optimisation when folio removed from pagecache 2024-01-10 17:10:31 +01:00
fscache.h nfs: Convert to release_folio 2022-05-09 23:12:33 -04:00
getroot.c
inode.c nfs: use vfs setgid helper 2023-08-30 16:11:10 +02:00
internal.h pNFS: Fix the pnfs block driver's calculation of layoutget size 2024-01-25 15:27:23 -08:00
io.c
iostat.h
mount_clnt.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
namespace.c NFS: Fix an Oops in nfs_d_automount() 2022-12-31 13:32:18 +01:00
netns.h
nfs.h
nfs2super.c
nfs2xdr.c NFS: Guard against READDIR loop when entry names exceed MAXNAMELEN 2023-09-13 09:42:49 +02:00
nfs3_fs.h
nfs3acl.c
nfs3client.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
nfs3proc.c freezer,sched: Rewrite core freezer logic 2022-09-07 21:53:50 +02:00
nfs3super.c
nfs3xdr.c NFS: Guard against READDIR loop when entry names exceed MAXNAMELEN 2023-09-13 09:42:49 +02:00
nfs4_fs.h NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
nfs4client.c NFSv4.1: fix pnfs MDS=DS session trunking 2023-10-06 14:56:31 +02:00
nfs4file.c NFSv4.2 fix problems with __nfs42_ssc_open 2022-08-19 20:31:57 -04:00
nfs4getroot.c
nfs4idmap.c nfs: remove unnecessary (void*) conversions. 2022-10-03 11:26:36 -04:00
nfs4idmap.h
nfs4namespace.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
nfs4proc.c NFSv4.2: fix nfs4_listxattr kernel BUG at mm/usercopy.c:102 2024-03-26 18:20:56 -04:00
nfs4renewd.c
nfs4session.c
nfs4session.h
nfs4state.c NFSv4: Fix a nfs4_state_manager() race 2023-10-10 22:00:41 +02:00
nfs4super.c
nfs4sysctl.c
nfs4trace.c
nfs4trace.h trace: Relocate event helper files 2024-03-06 14:45:17 +00:00
nfs4xdr.c NFSv4.2: Fix a memory stomp in decode_attr_security_label 2022-12-31 13:32:18 +01:00
nfs42.h NFSv4.2: fix listxattr maximum XDR buffer size 2024-03-26 18:20:56 -04:00
nfs42proc.c nfs42: client needs to strip file mode's suid/sgid bit after ALLOCATE op 2023-10-25 12:03:14 +02:00
nfs42xattr.c NFSv4.2: fix wrong shrinker_id 2023-07-19 16:21:43 +02:00
nfs42xdr.c NFSv4.2: Rework scratch handling for READ_PLUS (again) 2023-09-13 09:43:05 +02:00
nfsroot.c NFS: Fix an off by one in root_nfs_cat() 2024-03-26 18:20:56 -04:00
nfstrace.c
nfstrace.h trace: Relocate event helper files 2024-03-06 14:45:17 +00:00
pagelist.c NFSv4.1 mark qualified async operations as MOVEABLE tasks 2022-05-31 17:09:30 -04:00
pnfs.c pNFS: Fix the pnfs block driver's calculation of layoutget size 2024-01-25 15:27:23 -08:00
pnfs.h NFSv4/flexfiles: Cancel I/O if the layout is recalled or revoked 2022-10-06 09:52:09 -04:00
pnfs_dev.c NFSv4/pnfs: minor fix for cleanup path in nfs4_get_device_info 2023-09-19 12:27:58 +02:00
pnfs_nfs.c pNFS: Fix assignment of xprtdata.cred 2023-09-13 09:42:49 +02:00
proc.c NFS: NFSv2/v3 clients should never be setting NFS_CAP_XATTR 2022-02-25 18:50:13 -05:00
read.c NFSv4.2: Rework scratch handling for READ_PLUS (again) 2023-09-13 09:43:05 +02:00
super.c NFS: Avoid memcpy() run-time warning for struct sockaddr overflows 2022-10-27 15:52:10 -04:00
symlink.c fs: Change the type of filler_t 2022-05-09 16:36:48 -04:00
sysctl.c
sysfs.c NFS: rename nfs_client_kset to nfs_kset 2023-10-10 22:00:35 +02:00
sysfs.h
unlink.c NFSv4.1 mark qualified async operations as MOVEABLE tasks 2022-05-31 17:09:30 -04:00
write.c nfs: fix UAF in direct writes 2024-04-03 15:19:34 +02:00