Skip to contentSkip to conversation

SUNRPC: preserve the XDR encode limit after truncation

PATCH New Jiwoong Wi 1 participant 1 message Dec 2

Subsystems (3)
lore
Share & tools
lore

Jiwoong Wi

author
#0 lore raw

PATCH 1/1 SUNRPC: preserve the XDR encode limit after truncation

xdr_restrict_buflen() limits a session reply to its negotiated cached response size. When xdr_truncate_encode() moves the encoder back into the reply pages, it resets xdr->end to the end of the physical page. A following xdr_reserve_space() can then extend the reply beyond buf->buflen without taking the path that checks that limit.

An NFSv4.1 COMPOUND can leave READ data in the reply pages, truncate a partial READDIR entry, and then encode a LOCKT denial containing a large lock owner. The denied result does not pass the successful-result size check. nfsd4_store_cache_entry() consequently copies the overlong reply into a slot sized for the negotiated cache limit.

An instrumented unpatched 7.4-rc4 kernel measured a 3,012-byte cache copy starting 77 bytes into a 2,048-byte slot, crossing the allocation by 1,041 bytes.
A separate uninstrumented run faulted while handling the request.

Reapply buf->buflen after truncating into a page.

On nfsd-testing 86df59b69d8f, an x86-64 kernel was built with GCC 13.3, CONFIG_WERROR=y, Generic KASAN, and lockdep. The existing SunRPC XDR KUnit tests passed. The reproducer returned NFS4ERR_REP_TOO_BIG_TO_CACHE in a 2,036-byte reply for a 2,048-byte cache limit, and replay matched. A shorter READ control still returned NFS4ERR_DENIED with the full 1,024-byte lock owner, and its replay matched. No KASAN, Oops, or panic was observed.

The vulnerability research and validation were conducted by members of the Tobabz team as part of the Best of the Best 15th program.

Fixes 3e19ce762b53 ("rpc: xdr_truncate_encode")
Fixes 47ee52986472 ("nfsd4: adjust buflen to session channel limit")
---
 net/sunrpc/xdr.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
net/sunrpc/xdr.c +5 -4
index 3b66a09629..557b87bd9a 100644
@@ -1381,10 +1381,9 @@ EXPORT_SYMBOL_GPL(xdr_reserve_space_vec);
  * and xdr->p points at offset len from the start of the buffer, and
  * head, tail, and page lengths are adjusted to correspond.
  *
- * If this means moving xdr->p to a different buffer, we assume that
- * the end pointer should be set to the end of the current page,
- * except in the case of the head buffer when we assume the head
- * buffer's current length represents the end of the available buffer.
+ * If this means moving xdr->p to a page, the end pointer is bounded by
+ * both the end of that page and buf->buflen. In the head buffer, the
+ * head buffer's current length is the end of the available buffer.
  *
  * This is *not* safe to use on a buffer that already has inlined page
  * cache pages (as in a zero-copy server read reply), except for the
@@ -1427,6 +1426,8 @@ void xdr_truncate_encode(struct xdr_stream *xdr, size_t len)
 		xdr->p = page_address(*xdr->page_ptr);
 		xdr->end = (void *)xdr->p + PAGE_SIZE;
 		xdr->p = (void *)xdr->p + (new % PAGE_SIZE);
+		/* Preserve any restriction on the encoded reply length. */
+		xdr_restrict_buflen(xdr, buf->buflen);
 		WARN_ON_ONCE(xdr->iov);
 		return;
 	}

base-commit: 86df59b69d8fbacb9ba5aae22291d472f02b15a0

Signature · 1 line
2.50.1 (Apple Git-155)