Skip to contentSkip to conversation

SUNRPC: copy the GSS init token with read_bytes_from_xdr_buf()

PATCH In review Yiran Qiu 2 participants 2 messages Nov 20

Subsystems (4)
lore
Share & tools
lore

Yiran Qiu

author
#0 lore raw

PATCH 1/1 SUNRPC: copy the GSS init token with read_bytes_from_xdr_buf()

gss_read_proxy_verf() copies the token from the head of rq_arg and then from rq_arg.pages, assuming the rest of the token is in the page list. svcrdma rebuilds a Call that carries one Read chunk as head (inline data before the chunk position), pages (the chunk) and tail (inline data after it), and xdr_stream_remaining() counts the tail. When the token starts at the chunk and continues into the tail, the copy runs past the chunk's pages. If the chunk fills rq_pages, the copy takes the NULL entry at rq_pages[rq_maxpages] and memcpy() reads from page_address(NULL); otherwise the stale contents of the next page go to gssproxy in place of the tail. This runs in svcauth_gss_proxy_init() before any credential is checked, so a client of an NFS/RDMA server that uses gssproxy can oops nfsd threads without authenticating.

Commit 62319c2534b6 ("SUNRPC: fix oversized GSS proxy token copy") fixed the copy from the head and left the copy from the pages as it was. Copy the token with read_bytes_from_xdr_buf(), which follows head, pages and tail.

Found with afb-afl, an LLM-driven kernel audit/repro pipeline; the fix and this changelog were also drafted with LLM assistance. Reproduced on an unmodified KASAN build of mainline, nfsd listening on "rdma 20049" over rxe with use-gss-proxy set. An RPCSEC_GSS INIT Call whose token is a 12-page Read chunk (max_block_size 32768) plus 64 inline bytes gives (gss_read_proxy_verf() is inlined):

  BUG: KASAN: wild-memory-access in svcauth_gss_proxy_init+0x71f/0xe10
  Read of size 64 at addr 0005088000000000 by task nfsd/184
   __asan_memcpy+0x23/0x60
   svcauth_gss_proxy_init+0x71f/0xe10
   svcauth_gss_proc_init+0x22b/0x440

With an 11-page chunk, gssproxy received the chunk followed by 64 bytes that were not the tail. With this patch there is no report and gssproxy receives the chunk followed by the tail, for 12- and 11-page chunks; a chunk without a tail and an inline token are unchanged. Over TCP, tokens of 100 to 30000 bytes reach gssproxy intact with and without this patch. Kerberos and NFS functional tests were not run.

Fixes 5866efa8cbfb ("SUNRPC: Fix svcauth_gss_proxy_init()")

---
Tested on mainline 4405c18c95d2 (v7.4-rc3+) in QEMU. Applies to cel/linux.git nfsd-next and nfsd-testing.
A reproducer and the test logs are available on request.

 net/sunrpc/auth_gss/svcauth_gss.c | 38 +++++++++----------------------
 1 file changed, 11 insertions(+), 27 deletions(-)
net/sunrpc/auth_gss/svcauth_gss.c +11 -27
index 43839e805531..da4043aed247 100644
@@ -1056,9 +1056,8 @@ static int gss_read_proxy_verf(struct svc_rqst *rqstp,
 			       struct gssp_in_token *in_token)
 {
 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
-	unsigned int length, pgto_offs, pgfrom_offs;
-	int pages, i, pgto, pgfrom;
-	size_t to_offs, from_offs;
+	unsigned int base, length;
+	int pages, i;
 	u32 inlen;
 
 	if (dup_netobj(in_handle, &gc->gc_ctx))
@@ -1090,30 +1089,15 @@ static int gss_read_proxy_verf(struct svc_rqst *rqstp,
 		}
 	}
 
-	length = min_t(unsigned int, inlen, (char *)xdr->end - (char *)xdr->p);
-	for (to_offs = 0; to_offs < length; to_offs += PAGE_SIZE)
-		memcpy(page_address(in_token->pages[to_offs >> PAGE_SHIFT]),
-		       (char *)xdr->p + to_offs,
-		       min_t(unsigned int, length - to_offs, PAGE_SIZE));
-	inlen -= length;
-	to_offs = length;
-	from_offs = rqstp->rq_arg.page_base;
-	while (inlen) {
-		pgto = to_offs >> PAGE_SHIFT;
-		pgfrom = from_offs >> PAGE_SHIFT;
-		pgto_offs = to_offs & ~PAGE_MASK;
-		pgfrom_offs = from_offs & ~PAGE_MASK;
-
-		length = min_t(unsigned int, inlen,
-			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
-			       PAGE_SIZE - pgfrom_offs));
-		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
-		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
-		       length);
-
-		to_offs += length;
-		from_offs += length;
-		inlen -= length;
+	base = xdr_stream_pos(xdr);
+	for (i = 0; i < pages; i++) {
+		length = min_t(unsigned int, inlen - i * PAGE_SIZE, PAGE_SIZE);
+		if (read_bytes_from_xdr_buf(&rqstp->rq_arg, base + i * PAGE_SIZE,
+					    page_address(in_token->pages[i]),
+					    length)) {
+			gss_free_in_token_pages(in_token);
+			goto out_denied_free;
+		}
 	}
 	return 0;
 
Signature · 1 line
2.56.1
maintainer
#1 lore raw
Quoted 13 lines · Yiran Qiu

gss_read_proxy_verf() copies the token from the head of rq_arg and then from rq_arg.pages, assuming the rest of the token is in the page list. svcrdma rebuilds a Call that carries one Read chunk as head (inline data before the chunk position), pages (the chunk) and tail (inline data after it), and xdr_stream_remaining() counts the tail. When the token starts at the chunk and continues into the tail, the copy runs past the chunk's pages. If the chunk fills rq_pages, the copy takes the NULL entry at rq_pages[rq_maxpages] and memcpy() reads from page_address(NULL); otherwise the stale contents of the next page go to gssproxy in place of the tail. This runs in svcauth_gss_proxy_init() before any credential is checked, so a client of an NFS/RDMA server that uses gssproxy can oops nfsd threads without authenticating.

Quoted 1 line

[...]

Applied to nfsd-testing, thanks!

[1/1] SUNRPC: copy the GSS init token with read_bytes_from_xdr_buf()

      commit: 6d817b9ca893bc2603446e340030e149e5c8b207

--
Chuck Lever <[email protected]>