Fix premature completion of rpc_pipefs upcalls
Subsystems (2)
Chuck Lever
maintainerv2 cover Fix premature completion of rpc_pipefs upcalls
Farhad Alemi's syzkaller run against 7.1-rc5 hit a KASAN vmalloc-out-of-bounds in rpc_queue_upcall() while NFSD was starting client tracking:
The cld downcall matches a reply to its upcall by xid alone. If a write arrives before nfsdcld has read the upcall, the waiter returns while its struct rpc_pipe_msg is still queued, and the pipe is left holding a list_head into a dead stack frame. The blocklayout device upcall has the same bug, plus two of its own: the waiter can go to sleep after the reply has arrived, and a reply left over from a purged upcall is taken as a fresh one.
Guarding the reply exposes the opposite failure. Once a daemon has read a whole upcall, rpc_pipe_read() unlinks the message from every pipe list, so nothing the pipe purges on close can reach it. A daemon that exits between the read and the write leaves its waiter sleeping uninterruptibly with no way back. bl_resolve_deviceid() holds nn->bl_mutex across that wait, so every later device resolution in the net namespace blocks behind it.
rpc_pipefs pipes are mode 0600, so the writer is a daemon running as root. These patches guard against a broken or hostile daemon, which is unlikely. I consider the series as hardening rather than urgent.
The gss and idmap consumers keep their message in an object that outlives the upcall, and gss also releases what a departing daemon left behind. Neither has either bug.
The application order of the patches matters, so I'm happy to take the lot through the NFSD tree. An Acked-by: from the NFS client maintainers would be great.
Farhad, a run of your reproducer against this series would help.
---
Changes in v2:
- Retire the upcall on accept; drop the racy completion_done() test.
- Retire a purged upcall in bl_pipe_destroy_msg(), not in the waiter.
- New patch 6: set nn->cld_net before the cld pipe can be opened.
- New patch 7: complete a cld upcall when nfsdcld closes the pipe.
- New patch 8: complete a device upcall when blkmapd closes the pipe.
- Link to v1: https://patch.msgid.link/[email protected]
---
Chuck Lever (8):
NFSD: Don't complete a cld upcall the daemon has not read
NFSD: Move the cld upcall message out of the caller's stack frame
NFSD: Complete a cld upcall when copying its reply fails
NFSD: Reject an oversized principal hash from nfsdcld pnfs/blocklayout: Complete a device upcall only on its own reply
NFSD: Set nn->cld_net before registering the cld pipe
NFSD: Complete a cld upcall when the daemon closes the pipe pnfs/blocklayout: Complete a device upcall when the pipe is closed
fs/nfs/blocklayout/blocklayout.h | 5 --- fs/nfs/blocklayout/rpc_pipefs.c | 79 ++++++++++++++++++++++++---------- fs/nfs/netns.h | 5 ++- fs/nfsd/nfs4recover.c | 91 ++++++++++++++++++++++++++++++++-------- 4 files changed, 135 insertions(+), 45 deletions(-)
---
base-commit: e3c3b1a8188b192b125ae1fc9861d9a5d8d43d85
change-id: 20260830-alemi-d9f420956e8d
Best regards,
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerPATCH v2 1/8 NFSD: Don't complete a cld upcall the daemon has not read
cld_pipe_downcall() matches a reply to its upcall on the xid alone. A write that arrives before the daemon has read that upcall completes __cld_pipe_upcall() while its struct rpc_pipe_msg is still queued on the pipe. The waiter returns, its stack frame goes away, and the pipe keeps a list_head that points into it. The next rpc_queue_upcall() walks that pointer:
BUG: KASAN: vmalloc-out-of-bounds in __list_add_valid_or_report Read of size 8 at addr ffffc90007027950 by task syz.0.96/13066 __list_add_valid_or_report+0x6a/0x130 rpc_queue_upcall cld_pipe_upcall nfsd4_cld_grace_start nfsd4_cld_tracking_init
The pipe is mode 0600 in rpc_pipefs, so the trigger is a broken or hostile nfsdcld rather than an unprivileged task.
Record whether the daemon has consumed the whole message and complete only when it has. cld_pipe_destroy_msg() runs on every path by which the pipe releases a message, so it can maintain that flag under cn_lock. nfsdcld sends its -EINPROGRESS downcalls only after reading the upcall, so the new check does not break the GraceStart record stream.
--- fs/nfsd/nfs4recover.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
fs/nfsd/nfs4recover.c +20 -3
@@ -648,6 +648,8 @@ struct cld_upcall {struct list_head cu_list;struct cld_net *cu_net;struct completion cu_done; +/* daemon has read the whole upcall; protected by cn_lock */ +bool cu_inflight;union {struct cld_msg_hdr cu_hdr;struct cld_msg cu_msg;
@@ -808,6 +810,13 @@ cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)spin_lock (&cn->cn_lock);list_for_each_entry (tmp, &cn->cn_list, cu_list) {if (get_unaligned (&tmp->cu_u.cu_hdr.cm_xid) == xid) { +/* +* Completing now would return the waiter +* while its message is still queued on the +* pipe. +*/ +if (!tmp->cu_inflight) +break ; cup = tmp;if (status != -EINPROGRESS )list_del_init (&cup->cu_list);
@@ -816,9 +825,9 @@ cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) }spin_unlock (&cn->cn_lock); -/* couldn't find upcall? */ +/* no upcall to complete? */ if (!cup) { -dprintk ("%s: couldn't find upcall -- xid=%u\n" , __func__, xid); +dprintk ("%s: no completable upcall -- xid=%u\n" , __func__, xid);return -EINVAL ; }
@@ -838,8 +847,16 @@ cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)struct cld_msg *cmsg = msg->data; struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u.cu_msg); +struct cld_net *cn = cup->cu_net; + +/* +* errno >= 0 means the daemon read the whole message and a +* downcall will complete the upcall. +*/ +spin_lock (&cn->cn_lock); + cup->cu_inflight = msg->errno >=0 ; +spin_unlock (&cn->cn_lock); -/* errno >= 0 means we got a downcall */ if (msg->errno >=0 )return ;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 2/8 NFSD: Move the cld upcall message out of the caller's stack frame
In reply to Chuck Lever__cld_pipe_upcall() builds its struct rpc_pipe_msg on the stack and hands it to rpc_queue_upcall(), which links it into a list the pipe owns until the message is released. Only the completion rules keep that frame alive for as long as the pipe refers to it, and the peer that drives those rules is an untrusted userspace daemon. The gss and idmap upcalls do not take that on: each keeps the message in the object it already allocates.
Do the same here and put the message in struct cld_upcall, beside the reply buffer it carries. cld_pipe_destroy_msg() then reaches the upcall from the message directly rather than through msg->data.
--- fs/nfsd/nfs4recover.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
fs/nfsd/nfs4recover.c +10 -10
@@ -650,6 +650,7 @@ struct cld_upcall {struct completion cu_done;/* daemon has read the whole upcall; protected by cn_lock */ bool cu_inflight; +struct rpc_pipe_msg cu_pipe_msg;union {struct cld_msg_hdr cu_hdr;struct cld_msg cu_msg;
@@ -661,22 +662,22 @@ static int __cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn) { int ret; -struct rpc_pipe_msg msg; struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u); +struct rpc_pipe_msg *msg = &cup->cu_pipe_msg; -memset (&msg,0 ,sizeof (msg)); - msg.data = cmsg; - msg.len = nn->client_tracking_ops->msglen; +memset (msg,0 ,sizeof (*msg)); + msg->data = cmsg; + msg->len = nn->client_tracking_ops->msglen; - ret =rpc_queue_upcall (pipe, &msg); + ret =rpc_queue_upcall (pipe, msg);if (ret <0 ) { goto out; }wait_for_completion (&cup->cu_done); -if (msg.errno <0 ) - ret = msg.errno; +if (msg->errno <0 ) + ret = msg->errno; out:return ret; }
@@ -844,9 +845,8 @@ cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)static void cld_pipe_destroy_msg (struct rpc_pipe_msg *msg) { -struct cld_msg *cmsg = msg->data; - struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, - cu_u.cu_msg); + struct cld_upcall *cup = container_of(msg, struct cld_upcall, + cu_pipe_msg);struct cld_net *cn = cup->cu_net;/*
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 3/8 NFSD: Complete a cld upcall when copying its reply fails
In reply to Chuck Levercld_pipe_downcall() removes the matching upcall from cn_list before copying the daemon's reply. When that copy_from_user() faults, the downcall returns -EFAULT without completing the upcall, and a retried write cannot reach it because its xid is no longer on the list. __cld_pipe_upcall() waits on cu_done uninterruptibly, so the nfsd thread that sent the upcall is stuck, and nfsd shutdown hangs behind it. The pipe is mode 0600, so only a broken or hostile nfsdcld can trigger the fault.
Record the fault in the upcall's pipe message and complete it, so the waiter returns the error to its client tracking operation.
--- fs/nfsd/nfs4recover.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/nfsd/nfs4recover.c +4 -1
@@ -835,8 +835,11 @@ cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)if (status == -EINPROGRESS )return __cld_pipe_inprogress_downcall (cmsg, nn); -if (copy_from_user (&cup->cu_u.cu_msg_v2, src, mlen) !=0 ) +if (copy_from_user (&cup->cu_u.cu_msg_v2, src, mlen) !=0 ) { + cup->cu_pipe_msg.errno = -EFAULT ; +complete (&cup->cu_done);return -EFAULT ; + }complete (&cup->cu_done);return mlen;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 4/8 NFSD: Reject an oversized principal hash from nfsdcld
In reply to Chuck Lever__cld_pipe_inprogress_downcall() takes the principal hash length from the daemon's GraceStart downcall without checking it against the cc_princhash payload it describes, which holds at most SHA256_DIGEST_SIZE bytes. The length is stored verbatim in the reclaim record, and nfsd4_cld_check_v2() later hands it to memcmp() against a digest array of SHA256_DIGEST_SIZE bytes on the stack. A length up to U8_MAX reads past the end of that array, and the stray bytes can only make the comparison fail, denying the client its reclaim. The pipe is mode 0600 in rpc_pipefs, so the trigger is a broken or hostile nfsdcld rather than an unprivileged task.
Reject a length larger than the wire field, as the existing check on the client name length does.
--- fs/nfsd/nfs4recover.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/nfsd/nfs4recover.c +5 -0
@@ -730,6 +730,11 @@ __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg, name.len = namelen;if (get_user (princhashlen, &ci->cc_princhash.cp_len))return -EFAULT ; +if (princhashlen >SHA256_DIGEST_SIZE ) { +dprintk ("%s: invalid princhashlen (%u)" , + __func__, princhashlen); +return -EINVAL ; + }if (princhashlen >0 ) { princhashcopy = memdup_user( &ci->cc_princhash.cp_data,
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 5/8 pnfs/blocklayout: Complete a device upcall only on its own reply
In reply to Chuck Leverbl_pipe_downcall() wakes bl_resolve_deviceid() on any write of the right size, without checking that blkmapd has read the upcall being replied to. A write that arrives first returns the waiter while its struct rpc_pipe_msg is still queued on the pipe, which then holds a list_head into a dead stack frame. Reaching the pipe takes root, so the trigger is a broken or hostile blkmapd.
The wait has two further defects. bl_resolve_deviceid() sets TASK_UNINTERRUPTIBLE only after rpc_queue_upcall() has made the message visible, so a reply that lands in between wakes a running task and the schedule() that follows sleeps forever holding bl_mutex. And nn->bl_mount_reply is never reset, so when the pipe purges an unread upcall the waiter takes the previous reply as its own.
Move the message into struct nfs_net, where bl_mutex already limits the pipe to one upcall per net namespace. Accept a reply only while blkmapd has read the whole message and no earlier reply has been taken, and reject any other write with -EINVAL. Wait for that reply on a completion so it cannot slip past the sleep, and check msg->errno before trusting it.
--- fs/nfs/blocklayout/blocklayout.h | 5 ---- fs/nfs/blocklayout/rpc_pipefs.c | 58 +++++++++++++++++++++++++--------------- fs/nfs/netns.h | 5 +++- 3 files changed, 41 insertions(+), 27 deletions(-)
fs/nfs/blocklayout/blocklayout.h +0 -5
@@ -161,11 +161,6 @@ BLK_LSEG2EXT(struct pnfs_layout_segment *lseg)return BLK_LO2EXT (lseg->pls_layout); } -struct bl_pipe_msg { -struct rpc_pipe_msg msg; -wait_queue_head_t *bl_wq; -}; -struct bl_msg_hdr {u8 type;u16 totallen;/* length of entire message, including hdr itself */
fs/nfs/blocklayout/rpc_pipefs.c +37 -21
104 lines not on this page · raw message
fs/nfs/netns.h +4 -1
@@ -10,6 +10,8 @@#include <net/net_namespace.h> #include <net/netns/generic.h> #include <linux/sunrpc/stats.h> +#include <linux/sunrpc/rpc_pipe_fs.h> +#include <linux/completion.h> struct bl_dev_msg {int32_t status;
@@ -21,8 +23,9 @@ struct nfs_netns_client;struct nfs_net {struct cache_detail *nfs_dns_resolve;struct rpc_pipe *bl_device_pipe; +struct rpc_pipe_msg bl_pipe_msg;struct bl_dev_msg bl_mount_reply; -wait_queue_head_t bl_wq; +struct completion bl_done;struct mutex bl_mutex;struct list_head nfs_client_list;struct list_head nfs_volume_list;
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 6/8 NFSD: Set nn->cld_net before registering the cld pipe
In reply to Chuck Leverrpc_mkpipe_dentry() instantiates the pipe's dentry before it returns, so the cld pipe can be opened as soon as nfsd4_cld_register_net() has run. __nfsd4_init_cld_pipe() does not store the new cld_net in nn->cld_net until after that call. cld_pipe_downcall() reaches the cld_net only through that pointer and takes cn_lock without checking it, so a write() that lands in the window oopses.
Store nn->cld_net once @cn is fully initialized and before the pipe is registered, and clear it again when registration fails.
--- fs/nfsd/nfs4recover.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
fs/nfsd/nfs4recover.c +10 -4
@@ -941,18 +941,24 @@ __nfsd4_init_cld_pipe(struct net *net) }spin_lock_init (&cn->cn_lock);INIT_LIST_HEAD (&cn->cn_list); +#ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING + cn->cn_has_legacy = false; +#endif + +/* +* The pipe's methods reach @cn through nn->cld_net, so set +* it before the pipe can be opened. +*/ + nn->cld_net = cn; ret =nfsd4_cld_register_net (net, cn->cn_pipe);if (unlikely (ret)) goto err_destroy_data; -#ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING - cn->cn_has_legacy = false; -#endif - nn->cld_net = cn;return 0 ; err_destroy_data: + nn->cld_net =NULL ;rpc_destroy_pipe_data (cn->cn_pipe); err:kfree (cn);
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 7/8 NFSD: Complete a cld upcall when the daemon closes the pipe
In reply to Chuck LeverOnce nfsdcld has read a whole upcall, rpc_pipe_read() unlinks the message from every pipe list, so the purges in rpc_pipe_release() and rpc_close_pipes() no longer reach it. cld_upcall_ops supplies no .release_pipe callback, and cld_pipe_destroy_msg() returns without completing the upcall because a downcall is expected to follow. A daemon that exits between the read and the write therefore strands its waiter: the nfsd thread sleeps in TASK_UNINTERRUPTIBLE in __cld_pipe_upcall() and never returns, hung task warnings follow, and the server cannot be shut down.
Add a .release_pipe that completes every upcall the daemon has consumed. cu_inflight distinguishes those from upcalls still queued on the pipe, which the framework purges before it calls this callback; cld_pipe_downcall() removes an upcall it accepts from cn_list under the same lock. Neither can be completed twice.
Fail the stranded upcall with -EPIPE rather than the -EAGAIN that makes cld_pipe_upcall() retry. The daemon consumed the request, so whether it acted on it before exiting is unknown.
--- fs/nfsd/nfs4recover.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
fs/nfsd/nfs4recover.c +24 -0
@@ -871,10 +871,34 @@ cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)complete (&cup->cu_done); } +/* +* An upcall the daemon has consumed is off every pipe list, so the +* purge in rpc_pipe_release() and rpc_close_pipes() cannot reach it. +* Release its waiter here instead; no downcall can arrive now. +*/ +static void +cld_release_pipe (struct inode *inode) +{ +struct nfsd_net *nn =net_generic (inode->i_sb->s_fs_info, nfsd_net_id); +struct cld_net *cn = nn->cld_net; +struct cld_upcall *cup; + +spin_lock (&cn->cn_lock); +list_for_each_entry (cup, &cn->cn_list, cu_list) { +if (!cup->cu_inflight) +continue ; + cup->cu_inflight = false; + cup->cu_pipe_msg.errno = -EPIPE ; +complete (&cup->cu_done); + } +spin_unlock (&cn->cn_lock); +} +static const struct rpc_pipe_ops cld_upcall_ops = { .upcall = rpc_pipe_generic_upcall, .downcall = cld_pipe_downcall, .destroy_msg = cld_pipe_destroy_msg, + .release_pipe = cld_release_pipe, }; static int
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v2 8/8 pnfs/blocklayout: Complete a device upcall when the pipe is closed
In reply to Chuck LeverOnce blkmapd has read a whole upcall, rpc_pipe_read() unlinks the message from every pipe list, so the purges in rpc_pipe_release() and rpc_close_pipes() no longer reach it. bl_upcall_ops supplies no .release_pipe callback, and bl_pipe_destroy_msg() returns without completing the upcall because a downcall is expected to follow. A daemon that exits between the read and the write therefore strands its waiter: bl_resolve_deviceid() sleeps in wait_for_completion() and never returns, and it holds nn->bl_mutex across that wait, so every later device resolution in the net namespace blocks behind it.
Add a .release_pipe that completes an upcall blkmapd has consumed and left unanswered. Completing an upcall retires it, so a message the framework has already purged is no longer in flight and is not completed a second time here.
--- fs/nfs/blocklayout/rpc_pipefs.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
fs/nfs/blocklayout/rpc_pipefs.c +21 -0
@@ -151,10 +151,31 @@ static void bl_pipe_destroy_msg(struct rpc_pipe_msg *msg)complete (&nn->bl_done); } +/* +* An upcall blkmapd has consumed is off every pipe list, so the +* purges in rpc_pipe_release() and rpc_close_pipes() cannot reach it. +* No downcall can arrive once the pipe is closed; release its waiter +* here. +*/ +static void bl_release_pipe (struct inode *inode) +{ +struct nfs_net *nn =net_generic (inode->i_sb->s_fs_info, nfs_net_id); +struct rpc_pipe *pipe = nn->bl_device_pipe; + +spin_lock (&pipe->lock); +if (rpc_msg_is_inflight (&nn->bl_pipe_msg)) { + nn->bl_pipe_msg.copied =0 ; + nn->bl_pipe_msg.errno = -EPIPE ; +complete (&nn->bl_done); + } +spin_unlock (&pipe->lock); +} +static const struct rpc_pipe_ops bl_upcall_ops = { .upcall = rpc_pipe_generic_upcall, .downcall = bl_pipe_downcall, .destroy_msg = bl_pipe_destroy_msg, + .release_pipe = bl_release_pipe, }; static int nfs4blocklayout_register_sb(struct super_block *sb,
Signature · 1 line
2.54.0