git: f741a23d2ef5 - stable/14 - xdr: Fix xdr_string

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 14:27:41 UTC
The branch stable/14 has been updated by des:

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

commit f741a23d2ef52567ac41442bdd7ae8e78e8e5803
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-09-29 12:12:59 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-12 14:27:00 +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.
    
    Fixes:          d2018cedb414 ("xdr: Fix xdr_string")
    Reported by:    bjk@
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D60898
    
    (cherry picked from commit 838ff5548b4f83707f34cfd28769e35393da3f05)
---
 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 599bda6a45a3..79f76fd81cdc 100644
--- a/lib/libc/xdr/xdr.c
+++ b/lib/libc/xdr/xdr.c
@@ -706,7 +706,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);
@@ -768,7 +768,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 d469837d4d1c..39eb94dabffa 100644
--- a/sys/xdr/xdr.c
+++ b/sys/xdr/xdr.c
@@ -630,7 +630,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);
@@ -691,7 +691,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));
 }
 
 /*