git: 193586db78ee - releng/15.2 - xdr: Fix xdr_string

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 06:07:26 UTC
The branch releng/15.2 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=193586db78eef824fdde210ff6b6f412741df419

commit 193586db78eef824fdde210ff6b6f412741df419
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-09-29 12:12:59 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-13 06:07:09 +0000

    xdr: Fix xdr_string
    
    Forcing maxsize to RPC_MAXDATASIZE breaks code that deliberately uses
    XDR with longer strings than permitted by SunRPC.
    
    MFC after:      1 week
    Fixes:          6448ec89e739 (" * limit size of buffers to RPC_MAXDATASIZE")
    Fixes:          e17d7ab869bb ("xdr_string: don't leak strings with xdr_free")
    Sponsored by:   Klara, Inc.
    Sponsored by:   NetApp, Inc.
    Reviewed by:    kevans, brooks
    Differential Revision:  https://reviews.freebsd.org/D59994
    
    (cherry picked from commit d2018cedb414ef17e77b7fc2fa19fd81328da46b)
    
    xdr: Limit xdr_wrapstring
    
    The previous commit (re)introduced the possibility that a malicious or
    corrupt message can cause the receiver to allocate a very large amount
    of memory before realizing that the message is much shorter than
    claimed.  Revert the change to xdr_wrapstring() to avoid this.
    
    Approved by:    re (cperciva)
    Fixes:          d2018cedb414 ("xdr: Fix xdr_string")
    Reported by:    bjk@
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D60898
    
    (cherry picked from commit 838ff5548b4f83707f34cfd28769e35393da3f05)
    (cherry picked from commit 1a57c6bc8d81d6eca4f43babde45207e6fda27b9)
---
 lib/libc/xdr/xdr.c | 4 ++--
 sys/xdr/xdr.c      | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/libc/xdr/xdr.c b/lib/libc/xdr/xdr.c
index 47aafea4bc30..82f69882a30d 100644
--- a/lib/libc/xdr/xdr.c
+++ b/lib/libc/xdr/xdr.c
@@ -702,7 +702,7 @@ xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
 		 * be invalid.  Otherwise, if it's very small, we might
 		 * fail to free the string.
 		 */
-		maxsize = RPC_MAXDATASIZE;
+		maxsize = ~0U;
 		/* FALLTHROUGH */
 	case XDR_ENCODE:
 		size = strlen(sp);
@@ -764,7 +764,7 @@ xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
 bool_t
 xdr_wrapstring(XDR *xdrs, char **cpp)
 {
-	return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
+	return (xdr_string(xdrs, cpp, RPC_MAXDATASIZE));
 }
 
 /*
diff --git a/sys/xdr/xdr.c b/sys/xdr/xdr.c
index f983a474abdd..72ffd5ceb76b 100644
--- a/sys/xdr/xdr.c
+++ b/sys/xdr/xdr.c
@@ -626,7 +626,7 @@ xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
 		 * be invalid.  Otherwise, if it's very small, we might
 		 * fail to free the string.
 		 */
-		maxsize = RPC_MAXDATASIZE;
+		maxsize = ~0U;
 		/* FALLTHROUGH */
 	case XDR_ENCODE:
 		size = strlen(sp);
@@ -687,7 +687,7 @@ xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
 bool_t
 xdr_wrapstring(XDR *xdrs, char **cpp)
 {
-	return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
+	return (xdr_string(xdrs, cpp, RPC_MAXDATASIZE));
 }
 
 /*