Skip to contentSkip to conversation

SUNRPC: make XDR_QUADLEN() overflow-safe

PATCH In review Hyunsol Mun 4 participants 4 messages Nov 30 – Dec 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SUNRPC: make XDR_QUADLEN() overflow-safe

XDR_QUADLEN() rounds a byte count up by adding three before dividing by four. On 32-bit systems, a length near UINT_MAX wraps the addition and can produce a zero word count. __xdr_inline_decode() then accepts the length without advancing the stream because its bounds checks operate on the wrapped result.

A malformed NFSv4 SETATTR request can supply UINT_MAX as the FATTR4_OWNER length. The zero word count bypasses the XDR bounds check, after which NFSD passes the original length to its name-to-ID mapper and attempts an oversized copy into a fixed-size stack buffer.

Dan Carpenter reported the XDR_QUADLEN() overflow in 2024. The proposed size_add() conversion was not usable because XDR_QUADLEN() is also required in integer constant expressions.

Use DIV_ROUND_UP_POW2(), which avoids the overflowing addition and remains a constant expression. The decoded word count for UINT_MAX is then 0x40000000, so the existing stream bounds check rejects the request.

On nfsd-testing 36fd4049e3c2, an unpatched i386 QEMU server returned NFS4ERR_NOFILEHANDLE for a normal-length control request. The UINT_MAX request reached do_name_to_id(), triggered a double fault, and panicked. With the patch, the control status was unchanged and the malformed request returned NFS4ERR_BADXDR without an Oops, warning, or panic.

The unpatched and patched i386 kernels and the patched x86-64 kernel built with GCC 13.3 and W=1. A patched x86-64 NFSv4.2 mount, write, fsync, readback, unlink, and unmount completed successfully.

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

---
 include/linux/sunrpc/xdr.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/sunrpc/xdr.h +2 -1
index 2b33fc64be..46151478ee 100644
@@ -12,6 +12,7 @@
 #define _SUNRPC_XDR_H_
 
 #include <linux/uio.h>
+#include <linux/math.h>
 #include <asm/byteorder.h>
 #include <linux/unaligned.h>
 #include <linux/scatterlist.h>
@@ -29,7 +30,7 @@ struct rpc_rqst;
 /*
  * Buffer adjustment
  */
-#define XDR_QUADLEN(l)		(((l) + 3) >> 2)
+#define XDR_QUADLEN(l)		DIV_ROUND_UP_POW2(l, XDR_UNIT)
 
 /*
  * Generic opaque `network object.'

base-commit: 36fd4049e3c2bb901118e11be63bf7a7922bea8c

Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 6 lines · Hyunsol Mun

XDR_QUADLEN() rounds a byte count up by adding three before dividing by four. On 32-bit systems, a length near UINT_MAX wraps the addition and can produce a zero word count. __xdr_inline_decode() then accepts the length without advancing the stream because its bounds checks operate on the wrapped result.

Quoted 6 lines

A malformed NFSv4 SETATTR request can supply UINT_MAX as the FATTR4_OWNER length. The zero word count bypasses the XDR bounds check, after which NFSD passes the original length to its name-to-ID mapper and attempts an oversized copy into a fixed-size stack buffer.

[...]

Applied to nfsd-testing, thanks!

[1/1] SUNRPC: make XDR_QUADLEN() overflow-safe

      commit: f61d4acd903a15bff7302f4aaa056f4c43d74858

--
Chuck Lever

#2 lore raw
Quoted 12 lines · Chuck Lever
Quoted 11 lines · Hyunsol Mun

XDR_QUADLEN() rounds a byte count up by adding three before dividing by four. On 32-bit systems, a length near UINT_MAX wraps the addition and can produce a zero word count. __xdr_inline_decode() then accepts the length without advancing the stream because its bounds checks operate on the wrapped result.

A malformed NFSv4 SETATTR request can supply UINT_MAX as the FATTR4_OWNER length. The zero word count bypasses the XDR bounds check, after which NFSD passes the original length to its name-to-ID mapper and attempts an oversized copy into a fixed-size stack buffer.

Quoted 6 lines
Quoted 1 line

[...]

Applied to nfsd-testing, thanks!

[1/1] SUNRPC: make XDR_QUADLEN() overflow-safe

      commit: f61d4acd903a15bff7302f4aaa056f4c43d74858

Hooray!

regards,
dan carpenter

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Hyunsol Mun
Quoted 65 lines · Hyunsol Mun

XDR_QUADLEN() rounds a byte count up by adding three before dividing by four. On 32-bit systems, a length near UINT_MAX wraps the addition and can produce a zero word count. __xdr_inline_decode() then accepts the length without advancing the stream because its bounds checks operate on the wrapped result.

A malformed NFSv4 SETATTR request can supply UINT_MAX as the FATTR4_OWNER length. The zero word count bypasses the XDR bounds check, after which NFSD passes the original length to its name-to-ID mapper and attempts an oversized copy into a fixed-size stack buffer.

Dan Carpenter reported the XDR_QUADLEN() overflow in 2024. The proposed size_add() conversion was not usable because XDR_QUADLEN() is also required in integer constant expressions.

Use DIV_ROUND_UP_POW2(), which avoids the overflowing addition and remains a constant expression. The decoded word count for UINT_MAX is then 0x40000000, so the existing stream bounds check rejects the request.

On nfsd-testing 36fd4049e3c2, an unpatched i386 QEMU server returned NFS4ERR_NOFILEHANDLE for a normal-length control request. The UINT_MAX request reached do_name_to_id(), triggered a double fault, and panicked. With the patch, the control status was unchanged and the malformed request returned NFS4ERR_BADXDR without an Oops, warning, or panic.

The unpatched and patched i386 kernels and the patched x86-64 kernel built with GCC 13.3 and W=1. A patched x86-64 NFSv4.2 mount, write, fsync, readback, unlink, and unmount completed successfully.

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

---
 include/linux/sunrpc/xdr.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/sunrpc/xdr.h +2 -1
index 2b33fc64be..46151478ee 100644
@@ -12,6 +12,7 @@
 #define _SUNRPC_XDR_H_
 
 #include <linux/uio.h>
+#include <linux/math.h>
 #include <asm/byteorder.h>
 #include <linux/unaligned.h>
 #include <linux/scatterlist.h>
@@ -29,7 +30,7 @@ struct rpc_rqst;
 /*
  * Buffer adjustment
  */
-#define XDR_QUADLEN(l)		(((l) + 3) >> 2)
+#define XDR_QUADLEN(l)		DIV_ROUND_UP_POW2(l, XDR_UNIT)
 
 /*
  * Generic opaque `network object.'
Quoted 1 line

base-commit: 36fd4049e3c2bb901118e11be63bf7a7922bea8c