pynfs tests for setting ACL+MODE
Subsystems (2)
Chuck Lever
maintainerv3 cover pynfs tests for setting ACL+MODE
There are plenty of corner cases when an NFSv4 client requests setting an NFSv4 ACL and POSIX mode bits in the a single SETATTR request. It's even worse for NFS server implementations that have to translate NFSv4 ACLs to POSIX ACLs.
Note that the in-kernel Linux NFS client itself does not support NFSv4 ACLs since Linux is a POSIX ACL ecosystem. I believe it will never send an OPEN(create) or SETATTR that sets an NFSv4 ACL and mode bits simultaneously, relying on only user space tooling to set the ACL. So we must depend on only pynfs for testing this particular NFSD facility.
pynfs didn't have many tests in this particular category when I set out to troubleshoot a recently reported ACL+MODE bug in NFSD. So I've written a handful to exercise this specific case.
Claude and sashiko review found a number of issues in this old series. Resending with fixes.
---
Changes in v3:
- Compare ACE who fields as bytes in acl2mode_rfc8881(), make_test_acl()
- Run testMixed's RFC 8881 mode check before pass_warn(), which raises
- Compare ACE flags in verify_acl()
- Let verify_acl() skip ACEs the server inserts before requested ones
- Let verify_acl() search past a matching ACE whose mask falls short
- Derive the expected mode from the requested ACL, not the returned one
- Fold SATT20/21 into SATT19 and drop the pass_warn() that always fired
- Require MODE in attrsset in SATT30, as SATT19 already does
- Mask the mode with 0o777, not 0o7777, in SATT22 and SATT30
- Warn rather than fail on the SHOULD clauses in SATT29 and SATT32
- Fail OPEN12 and OPEN8b when the REQUIRED suppattr_exclcreat is missing
- Verify the whole ACE, not just who, in testSetattrACLNamedPrincipals
- Fold OPEN9/10 and OPEN15-18 into OPEN14, which covers all create modes
- Rewrite the OPEN(create) patch description
- Correct the SATT30 item and test list in the SETATTR patch description
- Link to v2: https://lore.kernel.org/linux-nfs/[email protected]/
Changes in v2:
- De-duplicate code shared between NFSv4.0 and NFSv4.1
- Remove named principals tests for now
- Replace incorrect usage of binary strings
- Add missing patch descriptions
---
Chuck Lever (9):
Add helper to report unsupported protocol features Add helper to format attribute bitmaps
Add a helper to compute POSIX mode bits from NFSv4 ACLs Add access_mask_to_str() helper to nfs4.0/nfs4acl.py Add make_test_acl() helper to nfs4acl modules Add verify_acl() helper to nfs4acl modules Add verify_mode_and_acl() helper to nfs4acl modules Add tests for SETATTR with MODE and ACL
Add tests for OPEN(create) with ACLs
nfs4.0/nfs4acl.py | 193 ++++++++++ nfs4.0/servertests/st_setattr.py | 728 +++++++++++++++++++++++++++++++++++- nfs4.1/nfs4acl.py | 1 + nfs4.1/nfs4lib.py | 8 + nfs4.1/server41tests/environment.py | 3 + nfs4.1/server41tests/st_open.py | 463 ++++++++++++++++++++++- 6 files changed, 1392 insertions(+), 4 deletions(-)
---
base-commit: cd4701827a8261fedbfb4c6e39029fb9671321a6
change-id: 20261008-acl-mode-f3c35108c597
Best regards,
Signature · 1 line
Chuck Lever
Chuck Lever
maintainerPATCH v3 1/9 Add helper to report unsupported protocol features
From: Chuck Lever <[email protected]>
In some cases, specifications permit a server to not implement a feature (or part of a feature) being tested. That should not count as a test failure. The helper added here can be used to indicate that the test did not pass or fail, but that the server under test does not support the tested feature.
--- nfs4.1/server41tests/environment.py | 3 +++ 1 file changed, 3 insertions(+)
nfs4.1/server41tests/environment.py +3 -0
@@ -277,6 +277,9 @@ debug_fail = False def fail(msg): raise testmod.FailureException(msg) +def unsupported(msg): + raise testmod.UnsupportedException(msg) + def check(res, stat=NFS4_OK, msg=None, warnlist=[]): if type(stat) is str:
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 2/9 Add helper to format attribute bitmaps
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
I'm about to add several new attribute-related tests. Introduce attr_bitmap_to_str() in nfs4lib.py to convert attribute bitmaps to human-readable symbolic strings.
--- nfs4.1/nfs4lib.py | 8 ++++++++ 1 file changed, 8 insertions(+)
nfs4.1/nfs4lib.py +8 -0
@@ -564,6 +564,14 @@ def attr_name(bitnum): """Returns string corresponding to attr bitnum""" return bitnum2attr.get(bitnum, "unknown_%r" % bitnum) +def attr_bitmap_to_str(bitmap): + """Convert an attribute bitmap to a symbolic string representation""" + bits = bitmap2list(bitmap) + if not bits: + return "(none)" + names = [bitnum2attr.get(bit, "unknown_%d" % bit).upper() for bit in bits] + return ", ".join(names) + class NFS4Error(Exception): def __init__(self, status, attrs=0, lock_denied=None, tag=None, check_msg=None): self.status = status
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 3/9 Add a helper to compute POSIX mode bits from NFSv4 ACLs
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
NFSv4 servers are permitted to adjust a file's mode bits when the file's ACL is set. RFC 8881 Section 6.3.2 describes what is expected.
I'm about to add new tests that set an ACL and then retrieve the file's mode to verify that the server is following that section of RFC 8881. Mode bit verification is common, so it is added as a utility function.
To avoid duplicating this code for both NFSv4.0 and NFSv4.1 ACL-related tests (I'm expecting to see a common NFSv4 ACL document published soon), a symlink is added to make ACL utility functions visible to tests for both minor versions.
--- nfs4.0/nfs4acl.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ nfs4.1/nfs4acl.py | 1 + 2 files changed, 61 insertions(+)
nfs4.0/nfs4acl.py +60 -0
@@ -208,6 +208,66 @@ def chk_groups(acl, flags, not_mask): chk_triple(mask, allow, acl[0], flags | GROUP, not_mask) del acl[:1] +def acl2mode_rfc8881(acl): + """ + Compute mode from ACL according to RFC 8881 Section 6.3.2. + + For each special identifier (OWNER@, GROUP@, EVERYONE@), evaluate the + ACL in order considering only ALLOW and DENY ACEs for EVERYONE@ and + the identifier under consideration. Then translate to mode bits: + - Read bit: Set if ACE4_READ_DATA is permitted + - Write bit: Set if BOTH ACE4_WRITE_DATA AND ACE4_APPEND_DATA are permitted + - Execute bit: Set if ACE4_EXECUTE is permitted + + Returns the low-order 9 bits of the mode (user/group/other permissions). + """ + identifiers = [ + (b"OWNER@", MODE4_RUSR, MODE4_WUSR, MODE4_XUSR), + (b"GROUP@", MODE4_RGRP, MODE4_WGRP, MODE4_XGRP), + (b"EVERYONE@", MODE4_ROTH, MODE4_WOTH, MODE4_XOTH) + ] + + mode = 0 + + for who, read_bit, write_bit, exec_bit in identifiers: + # Start with no permissions + allowed_mask = 0 + denied_mask = 0 + + # Evaluate ACL in order, considering only ALLOW/DENY for this + # identifier and EVERYONE@ + for ace in acl: + # Skip non-relevant ACEs + if ace.who not in (who, b"EVERYONE@"): + continue + if ace.type not in (ALLOWED, DENIED): + continue + # Skip inherit-only ACEs (they don't affect current permissions) + if ace.flag & ACE4_INHERIT_ONLY_ACE: + continue + + if ace.type == ALLOWED: + # Add allowed permissions not already denied + allowed_mask |= (ace.access_mask & ~denied_mask) + elif ace.type == DENIED: + # Add denied permissions not already allowed + denied_mask |= (ace.access_mask & ~allowed_mask) + + # Translate permitted mask to mode bits per RFC 8881 Section 6.3.2 + # Read bit: ACE4_READ_DATA must be set + if allowed_mask & ACE4_READ_DATA: + mode |= read_bit + + # Write bit: BOTH ACE4_WRITE_DATA and ACE4_APPEND_DATA must be set + if (allowed_mask & ACE4_WRITE_DATA) and (allowed_mask & ACE4_APPEND_DATA): + mode |= write_bit + + # Execute bit: ACE4_EXECUTE must be set + if allowed_mask & ACE4_EXECUTE: + mode |= exec_bit + + return mode + def printableacl(acl): type_str = ["ACCESS", "DENY"] out = ""
nfs4.1/nfs4acl.py new file +1 -0
@@ -0,0 +1 @@ +../nfs4.0/nfs4acl.py
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 4/9 Add access_mask_to_str() helper to nfs4.0/nfs4acl.py
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
I'm about to add several new ACL-related tests. Introduce access_mask_to_str() to convert ACE access_mask values to human- readable symbolic strings for the display output in these new tests.
--- nfs4.0/nfs4acl.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
nfs4.0/nfs4acl.py +20 -0
@@ -268,6 +268,26 @@ def acl2mode_rfc8881(acl): return mode +def access_mask_to_str(mask): + """Convert an ACE access_mask to a symbolic string representation""" + perms = [ + (ACE4_READ_DATA, "READ_DATA"), + (ACE4_WRITE_DATA, "WRITE_DATA"), + (ACE4_APPEND_DATA, "APPEND_DATA"), + (ACE4_READ_NAMED_ATTRS, "READ_NAMED_ATTRS"), + (ACE4_WRITE_NAMED_ATTRS, "WRITE_NAMED_ATTRS"), + (ACE4_EXECUTE, "EXECUTE"), + (ACE4_DELETE_CHILD, "DELETE_CHILD"), + (ACE4_READ_ATTRIBUTES, "READ_ATTRIBUTES"), + (ACE4_WRITE_ATTRIBUTES, "WRITE_ATTRIBUTES"), + (ACE4_DELETE, "DELETE"), + (ACE4_READ_ACL, "READ_ACL"), + (ACE4_WRITE_ACL, "WRITE_ACL"), + (ACE4_WRITE_OWNER, "WRITE_OWNER"), + (ACE4_SYNCHRONIZE, "SYNCHRONIZE"), + ] + return " | ".join(name for bit, name in perms if mask & bit) or "(none)" + def printableacl(acl): type_str = ["ACCESS", "DENY"] out = ""
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 5/9 Add make_test_acl() helper to nfs4acl modules
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
I'm about to add several new tests that set a test ACL. This is common code for all these tests, so introduce a utility function to construct this ACL for each test.
--- nfs4.0/nfs4acl.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
nfs4.0/nfs4acl.py +22 -0
@@ -78,6 +78,28 @@ def mode2acl(mode, dir=False): nfsace4(DENIED, 0, negate(other), "EVERYONE@") ] +def make_test_acl(): + """Create a test ACL that maps cleanly to POSIX ACLs + + Uses OWNER@, GROUP@, and EVERYONE@ to match POSIX user/group/other + structure, which helps servers that map NFSv4 ACLs to POSIX ACLs. + + Includes both WRITE_DATA and APPEND_DATA for write permission, since + Linux NFS server's conservative NFSv4-to-POSIX mapping requires both + to grant POSIX write permission. + """ + return [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_READ_ACL, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + def acl2mode(acl): """Translate an acl into a 3-digit octal mode""" names = ["OWNER@", "GROUP@", "EVERYONE@"]
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 6/9 Add verify_acl() helper to nfs4acl modules
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
I'm about to add several new tests that verify a test ACL. This is common code for all these tests, so introduce a utility function to verify the test ACL.
--- nfs4.0/nfs4acl.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
nfs4.0/nfs4acl.py +55 -0
@@ -310,6 +310,61 @@ def access_mask_to_str(mask): ] return " | ".join(name for bit, name in perms if mask & bit) or "(none)" +def verify_acl(returned_acl, expected_acl): + """Verify that returned ACL contains expected ACEs + + Server may insert additional ACEs anywhere, but the requested ones + must be present in the requested order, with the requested type, + principal, and flags, and with at least the requested permissions. + + Raises AssertionError if verification fails. + """ + if len(returned_acl) < len(expected_acl): + raise AssertionError( + "Returned ACL has fewer entries than requested: " + "expected at least %d, got %d" % (len(expected_acl), len(returned_acl))) + + # Verify the ACEs we set are present (server may add additional ACEs) + pos = 0 + for i, expected_ace in enumerate(expected_acl): + # RFC 8881 Section 6.2.1.4.1: ACE4_IDENTIFIER_GROUP is ignored on + # the special identifiers + flag_mask = ~0 + if expected_ace.who.endswith(b"@"): + flag_mask = ~ACE4_IDENTIFIER_GROUP + expected_flag = expected_ace.flag & flag_mask + # A server may split one principal's grant across several ACEs, + # so keep searching past a candidate whose mask falls short and + # remember it for the error message + candidate = None + for j in range(pos, len(returned_acl)): + returned_ace = returned_acl[j] + if (returned_ace.type != expected_ace.type or + returned_ace.who != expected_ace.who or + (returned_ace.flag & flag_mask) != expected_flag): + continue + candidate = returned_ace + # Check that requested permissions are present (server may add more) + if (returned_ace.access_mask & expected_ace.access_mask) == expected_ace.access_mask: + break + else: + if candidate is None: + raise AssertionError( + "ACE %d (type %d, flag 0x%x, who %s) not found in returned ACL" % + (i, expected_ace.type, expected_ace.flag, expected_ace.who)) + returned_ace = candidate + missing = expected_ace.access_mask & ~returned_ace.access_mask + raise AssertionError( + "ACE %d access_mask mismatch:\n" + " Expected: %s\n" + " Got: %s\n" + " Missing: %s" % + (i, + access_mask_to_str(expected_ace.access_mask), + access_mask_to_str(returned_ace.access_mask), + access_mask_to_str(missing))) + pos = j + 1 + def printableacl(acl): type_str = ["ACCESS", "DENY"] out = ""
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 7/9 Add verify_mode_and_acl() helper to nfs4acl modules
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
I'm about to add new tests that set a test ACL and mode bits simultaneously. This is common code for the new tests, so introduce a utility function to verify the ACL and mode bits.
--- nfs4.0/nfs4acl.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
nfs4.0/nfs4acl.py +36 -0
@@ -365,6 +365,42 @@ def verify_acl(returned_acl, expected_acl): access_mask_to_str(missing))) pos = j + 1 +def verify_mode_and_acl(attrs_dict, expected_acl, operation="operation"): + """Verify that MODE and ACL attributes match expectations + + This helper encapsulates the common pattern of verifying both ACL + and mode derivation per RFC 8881 Section 6.3.2. + + Args: + attrs_dict: Dictionary of attributes (must contain FATTR4_ACL and FATTR4_MODE) + expected_acl: The ACL that should be present + operation: Name of operation for error messages (default: "operation") + + Raises: + AssertionError: If verification fails + """ + # Check that both attributes are present + if FATTR4_ACL not in attrs_dict: + raise AssertionError( + "ACL attribute not returned after %s" % operation) + if FATTR4_MODE not in attrs_dict: + raise AssertionError( + "MODE attribute not returned after %s" % operation) + + # Verify ACL matches expected + verify_acl(attrs_dict[FATTR4_ACL], expected_acl) + + # Verify mode matches RFC 8881 derivation from the requested ACL. + # Deriving from the returned ACL would only check that the server + # agrees with itself, so a server that widened both would pass. + returned_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(expected_acl) + + if returned_mode != expected_mode: + raise AssertionError( + "MODE (0%o) does not match RFC 8881 Section 6.3.2 derivation " + "from requested ACL (expected 0%o)" % (returned_mode, expected_mode)) + def printableacl(acl): type_str = ["ACCESS", "DENY"] out = ""
Signature · 1 line
2.54.0
Chuck Lever
maintainerPATCH v3 8/9 Add tests for SETATTR with MODE and ACL
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
Add comprehensive tests that verify SETATTR behavior when both mode bits and an ACL are set simultaneously. The new tests cover:
1. SATT19: testSetattrModeWithACL
- Sets MODE and ACL in a single SETATTR, once each with a MODE the ACL widens (0o640, 0o600) and one more restrictive than the ACL grants (0o400)
- Verifies both attributes appear in attrsset, the ACL is stored as given, and the final mode is derived from the ACL rather than taken from the requested MODE
2. SATT22: testSetattrACLThenMode
- Compares setting MODE+ACL together vs separately in sequence
- Creates two files and verifies consistent behavior
These tests ensure that servers correctly handle the interaction between MODE and ACL attributes when setting file attributes, and verify that the ACL specified in the OPEN operation matches the ACL after the OPEN completes.
SETATTR: ACL-to-mode verification tests
These new tests verify:
- Mode is correctly derived from ACL per RFC 8881 §6.3.2
- Write bit requires both ACE4_WRITE_DATA and ACE4_APPEND_DATA
- ACL evaluation order (first ACE takes precedence)
- EVERYONE@ ACEs affect all three permission classes
- Complex ACLs with multiple identifiers
SETATTR: Two additional ACL verification tests
New test SATT28: Verify that the final ACL is the same no matter what the file's previous mode bits were.
New test SATT29: Verify that SETATTR(ACL) preserves high-order mode bits (SUID/SGID/SVTX).
SETATTR: Verify behavior when setting both MODE and ACL
Add three comprehensive tests that verify SETATTR(MODE+ACL) behavior per RFC 8881 §6.4.1.3:
SATT30:
Verify attrsset bitmap and final mode consistency when MODE+ACL set together. The server must report both MODE and ACL in attrsset, and the final mode must match what the ACL derives to per §6.4.1.3.
SATT31:
Test MODE+ACL interaction when they conflict significantly. Regardless of requested MODE, final mode always matches ACL derivation. This shows that ACL wins when both are set (per §6.4.1.3: "possibly changing the final mode").
--- nfs4.0/servertests/st_setattr.py | 728 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 727 insertions(+), 1 deletion(-)
nfs4.0/servertests/st_setattr.py +727 -1
@@ -1,9 +1,10 @@ from xdrdef.nfs4_const import * from .environment import check, get_invalid_utf8strings from nfs4lib import bitmap2list, dict2fattr -from xdrdef.nfs4_type import nfstime4, settime4 +from xdrdef.nfs4_type import nfstime4, settime4, nfsace4 import nfs_ops op = nfs_ops.NFS4ops() +import nfs4acl def _set_mode(t, c, file, stateid=None, msg=" using stateid=0", warnlist=[]):
@@ -783,3 +784,728 @@ def testMixed(t, env): c.init_connection() fh, stateid = c.create_confirm(t.word(), deny=OPEN4_SHARE_DENY_NONE) _set_mixed(t, c, fh) + +def testSetattrModeWithACL(t, env): + """SETATTR with both MODE and ACL attributes + + Per RFC 8881 Section 6.4.1.3, when both MODE and ACL are set together, + both are processed, but the final mode is derived from the ACL and may + differ from the requested MODE. The requested MODE is varied from one + that the ACL widens to one more restrictive than the ACL grants. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT19 + """ + c = env.c1 + c.init_connection() + acl = nfs4acl.make_test_acl() + + for mode in (0o640, 0o600, 0o400): + fh, stateid = c.create_confirm(t.word() + (b"_%o" % mode)) + + # Set both MODE and ACL in a single SETATTR + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE 0%o and ACL" % mode) + + # Check which attributes were actually set by examining the reply bitmask + attrsset = bitmap2list(res.resarray[-1].attrsset) + + # Verify both MODE and ACL were set (processed) + if FATTR4_MODE not in attrsset: + t.fail("MODE not in attrsset, but MODE 0%o was requested" % mode) + if FATTR4_ACL not in attrsset: + t.fail("ACL not in attrsset, but ACL was requested with MODE 0%o" + % mode) + + # Verify ACL was set correctly and the mode derived from it + attrs_dict = c.do_getattrdict(fh, [FATTR4_ACL, FATTR4_MODE]) + try: + nfs4acl.verify_mode_and_acl(attrs_dict, acl, + "SETATTR with MODE 0%o" % mode) + except AssertionError as e: + t.fail(str(e)) + +def testSetattrACLThenMode(t, env): + """SETATTR MODE+ACL together vs ACL then MODE separately + + Per RFC 8881 sections 6.4.1.1-6.4.1.3, when MODE and ACL are set + together in a single SETATTR, the server computes the final mode + from the ACL (the MODE attribute is effectively ignored). When + ACL is set first and MODE is set in a separate operation, the + final mode is the explicitly-set MODE value. + + This test verifies this difference by using a MODE (0o755) that + differs from the ACL-derived mode (0o644). + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT22 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create two test files + fh1, stateid1 = c.create_confirm(t.word() + b"_1") + fh2, stateid2 = c.create_confirm(t.word() + b"_2") + + # make_test_acl() derives to mode 0o644 (rw-r--r--) + acl = nfs4acl.make_test_acl() + acl_derived_mode = acl2mode_rfc8881(acl) + + # Use a different mode to demonstrate the RFC-defined difference + explicit_mode = 0o755 + + # File 1: Set MODE and ACL together + # Per RFC 8881 Section 6.4.1.2, mode is derived from ACL (MODE ignored) + attrs = {FATTR4_MODE: explicit_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh1) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE and ACL together") + + # File 2: Set ACL first, then MODE separately + # Per RFC 8881 Section 6.4.1.1, final mode is the explicit MODE value + ops = c.use_obj(fh2) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with ACL") + + ops = c.use_obj(fh2) + [c.setattr({FATTR4_MODE: explicit_mode})] + res = c.compound(ops) + check(res, msg="SETATTR with MODE after ACL") + + # Verify the modes differ as expected per RFC 8881 + attrs1 = c.do_getattrdict(fh1, [FATTR4_MODE, FATTR4_ACL]) + attrs2 = c.do_getattrdict(fh2, [FATTR4_MODE, FATTR4_ACL]) + + mode1 = attrs1[FATTR4_MODE] & 0o777 + mode2 = attrs2[FATTR4_MODE] & 0o777 + + # File 1 (MODE+ACL together): mode should be derived from ACL + if mode1 != acl_derived_mode: + t.fail("MODE+ACL together: expected ACL-derived mode 0%o, got 0%o" + % (acl_derived_mode, mode1)) + + # File 2 (ACL then MODE): mode should be explicit MODE value + if mode2 != explicit_mode: + t.fail("ACL then MODE: expected explicit mode 0%o, got 0%o" + % (explicit_mode, mode2)) + +def testSetattrACLModeDeriveBasic(t, env): + """SETATTR(ACL) should derive mode per RFC 8881 Section 6.3.2 + + Test basic mode derivation from ACL when setting ACL alone. + The mode's permission bits should match what is computed from the ACL. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT23 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create ACL: OWNER@ gets read+write+execute, others get nothing + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"EVERYONE@") + ] + + # Set ACL only (not MODE) + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with ACL only") + + # Get resulting mode + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + + # Compute expected mode from the requested ACL per RFC 8881 Section 6.3.2 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match RFC 8881 Section 6.3.2 derivation " + "from ACL (expected 0%o)" % (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveWriteBits(t, env): + """SETATTR(ACL) write bit requires BOTH WRITE_DATA and APPEND_DATA + + Per RFC 8881 Section 6.3.2, the write mode bit should only be set if BOTH + ACE4_WRITE_DATA and ACE4_APPEND_DATA are present in the ACL. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT24 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test 1: Only WRITE_DATA (no APPEND_DATA) - write bit should NOT be set + fh1, stateid1 = c.create_confirm(t.word() + b"_1") + acl1 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_WRITE_DATA, # Missing APPEND_DATA + b"OWNER@") + ] + ops = c.use_obj(fh1) + [c.setattr({FATTR4_ACL: acl1})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only WRITE_DATA") + + attrs1 = c.do_getattrdict(fh1, [FATTR4_MODE, FATTR4_ACL]) + mode1 = attrs1[FATTR4_MODE] & 0o777 + expected_mode1 = acl2mode_rfc8881(acl1) + + if mode1 != expected_mode1: + t.fail("Mode (0%o) with only WRITE_DATA does not match expected (0%o). " + "Write bit should NOT be set without APPEND_DATA." % + (mode1, expected_mode1)) + + # Test 2: Only APPEND_DATA (no WRITE_DATA) - write bit should NOT be set + fh2, stateid2 = c.create_confirm(t.word() + b"_2") + acl2 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_APPEND_DATA, # Missing WRITE_DATA + b"OWNER@") + ] + ops = c.use_obj(fh2) + [c.setattr({FATTR4_ACL: acl2})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only APPEND_DATA") + + attrs2 = c.do_getattrdict(fh2, [FATTR4_MODE, FATTR4_ACL]) + mode2 = attrs2[FATTR4_MODE] & 0o777 + expected_mode2 = acl2mode_rfc8881(acl2) + + if mode2 != expected_mode2: + t.fail("Mode (0%o) with only APPEND_DATA does not match expected (0%o). " + "Write bit should NOT be set without WRITE_DATA." % + (mode2, expected_mode2)) + + # Test 3: Both WRITE_DATA and APPEND_DATA - write bit SHOULD be set + fh3, stateid3 = c.create_confirm(t.word() + b"_3") + acl3 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_WRITE_DATA | ACE4_APPEND_DATA, # Both present + b"OWNER@") + ] + ops = c.use_obj(fh3) + [c.setattr({FATTR4_ACL: acl3})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with both WRITE_DATA and APPEND_DATA") + + attrs3 = c.do_getattrdict(fh3, [FATTR4_MODE, FATTR4_ACL]) + mode3 = attrs3[FATTR4_MODE] & 0o777 + expected_mode3 = acl2mode_rfc8881(acl3) + + if mode3 != expected_mode3: + t.fail("Mode (0%o) with WRITE_DATA+APPEND_DATA does not match " + "expected (0%o)" % (mode3, expected_mode3)) + +def testSetattrACLModeDeriveAllowDeny(t, env): + """SETATTR(ACL) with ALLOW/DENY interaction + + Test that ACL evaluation order is correct when mixing ALLOW and DENY ACEs. + Per RFC 8881 Section 6.3.2, evaluate ACEs in order, with earlier ACEs taking + precedence. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT25 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test: ALLOW first, then DENY - the ALLOW should win + fh, stateid = c.create_confirm(t.word()) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"OWNER@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA, # This should be ignored (already allowed) + b"OWNER@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with ALLOW then DENY") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) with ALLOW/DENY does not match expected (0%o). " + "First ACE should take precedence." % (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveEveryone(t, env): + """SETATTR(ACL) with EVERYONE@ affecting all identifiers + + Test that EVERYONE@ ACEs are considered when evaluating permissions + for OWNER@ and GROUP@ per RFC 8881 Section 6.3.2. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT26 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # EVERYONE@ gets read, specific OWNER@ gets nothing extra + # Final result: OWNER@ should have read (from EVERYONE@) + fh, stateid = c.create_confirm(t.word()) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only EVERYONE@") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match expected (0%o). " + "EVERYONE@ should affect all mode bits." % + (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveComplex(t, env): + """SETATTR(ACL) with complex ACL including multiple identifiers + + Test mode derivation with a more realistic ACL including OWNER@, + GROUP@, and EVERYONE@ with various permissions. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT27 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create a complex ACL: + # OWNER@: read + write + execute + # GROUP@: read + execute (no write) + # EVERYONE@: read only + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with complex ACL") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match RFC 8881 Section 6.3.2 derivation " + "from complex ACL (expected 0%o)" % + (returned_mode, expected_mode)) + +def testSetattrACLIndependentOfMode(t, env): + """SETATTR(ACL) outcome should not depend on existing mode bits + + Per RFC 8881 Section 6.4.1.2, when setting ACL without mode, the ACL should + be set as given. The existing mode should not affect the ACL that gets + stored. This test verifies that setting the same ACL on files with + different initial modes results in identical ACLs being stored. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT28 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create the same ACL to use for all files + # OWNER@: read + write, GROUP@: read, EVERYONE@: nothing + test_acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@") + ] + + # Test with various initial mode values + initial_modes = [0o600, 0o644, 0o755, 0o777, 0o400, 0o000] + files = [] + + # Create files with different initial modes + for initial_mode in initial_modes: + fh, stateid = c.create_confirm(t.word() + (b"_%o" % initial_mode)) + + # Set initial mode + ops = c.use_obj(fh) + [c.setattr({FATTR4_MODE: initial_mode})] + res = c.compound(ops) + check(res, msg="Setting initial mode to 0%o" % initial_mode) + + files.append((fh, initial_mode)) + + # Now set the same ACL on all files + for fh, initial_mode in files: + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: test_acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL on file with initial mode 0%o" % initial_mode) + + # Retrieve the ACLs that were actually stored + acls = [] + for fh, initial_mode in files: + attrs = c.do_getattrdict(fh, [FATTR4_ACL]) + acls.append((initial_mode, attrs[FATTR4_ACL])) + + # Helper function to compare ACLs (comparing ACE lists) + def acl_equal(acl1, acl2): + """Compare two ACLs for equality""" + if len(acl1) != len(acl2): + return False + for ace1, ace2 in zip(acl1, acl2): + if (ace1.type != ace2.type or + ace1.flag != ace2.flag or + ace1.access_mask != ace2.access_mask or + ace1.who != ace2.who): + return False + return True + + def acl_to_string(acl): + """Convert ACL to readable string for error messages""" + aces = [] + for ace in acl: + type_str = "ALLOW" if ace.type == ACE4_ACCESS_ALLOWED_ACE_TYPE else "DENY" + aces.append("<%s:%s:0x%x:0x%x>" % + (type_str, ace.who.decode(), ace.access_mask, ace.flag)) + return "[" + ", ".join(aces) + "]" + + # Compare all ACLs - they should all be identical + reference_mode, reference_acl = acls[0] + for initial_mode, acl in acls[1:]: + if not acl_equal(reference_acl, acl): + t.fail("ACLs differ based on initial mode! " + "File with mode 0%o has ACL %s, " + "but file with mode 0%o has ACL %s. " + "RFC 8881 Section 6.4.1.2: ACL should be set as given, " + "independent of existing mode." % + (reference_mode, acl_to_string(reference_acl), + initial_mode, acl_to_string(acl))) + + # Also verify that the mode derived from the ACL is consistent + modes = [] + for initial_mode, acl in acls: + derived_mode = acl2mode_rfc8881(acl) + modes.append(derived_mode) + + if len(set(modes)) != 1: + mode_str = ", ".join("0%o (from initial 0%o)" % (m, acls[i][0]) + for i, m in enumerate(modes)) + t.fail("ACLs derive to different modes: %s. " + "This suggests ACLs were stored differently based on initial mode." % + mode_str) + +def testSetattrACLIndependentModeHighBits(t, env): + """SETATTR(ACL) should preserve high-order mode bits (SUID/SGID/SVTX) + + Per RFC 8881 Section 6.4.1.2, when setting ACL without mode, the three + high-order bits of mode (SUID, SGID, SVTX) SHOULD remain unchanged. + Only the low-order nine permission bits should be modified. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT29 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create ACL to set + test_acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@") + ] + + # Test with different high-order bits set + test_cases = [ + (0o4755, "SUID"), # Set-user-ID + (0o2755, "SGID"), # Set-group-ID + (0o1755, "SVTX"), # Sticky bit + (0o6755, "SUID+SGID"), # Both SUID and SGID + (0o7755, "ALL"), # All three bits + ] + + # Preserving the high-order bits is a SHOULD, so collect violations + # and warn once the MUST checks below have run for every case + not_preserved = [] + for initial_mode, description in test_cases: + fh, stateid = c.create_confirm(t.word() + (b"_%s" % description.encode())) + + # Set initial mode with high-order bits + ops = c.use_obj(fh) + [c.setattr({FATTR4_MODE: initial_mode})] + res = c.compound(ops) + check(res, msg="Setting initial mode 0%o (%s)" % (initial_mode, description)) + + # Verify the mode was set + before_attrs = c.do_getattrdict(fh, [FATTR4_MODE]) + before_mode = before_attrs[FATTR4_MODE] & 0o7777 + + # Set ACL only (not mode) + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: test_acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL on file with %s" % description) + + # Check that high-order bits are preserved + after_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + after_mode = after_attrs[FATTR4_MODE] & 0o7777 + + # Extract high-order bits (SUID, SGID, SVTX) + before_high = before_mode & 0o7000 + after_high = after_mode & 0o7000 + + if before_high != after_high: + not_preserved.append("%s: before=0%o, after=0%o" % + (description, before_mode, after_mode)) + + # Verify low-order bits match ACL derivation + after_low = after_mode & 0o777 + expected_low = acl2mode_rfc8881(test_acl) + + if after_low != expected_low: + t.fail("Low-order mode bits (0%o) don't match RFC 8881 Section 6.3.2 " + "derivation (expected 0%o) for %s" % + (after_low, expected_low, description)) + + if not_preserved: + t.pass_warn("High-order mode bits not preserved (RFC 8881 Section " + "6.4.1.2: SHOULD remain unchanged): %s" % + "; ".join(not_preserved)) + +def testSetattrModeACLattrsset(t, env): + """SETATTR(MODE+ACL) should indicate which attributes were actually set + + Per RFC 8881 Section 6.4.1.3, when both MODE and ACL are set together, the + server processes MODE first, then ACL (which may modify the mode). + The attrsset bitmap indicates which attributes were actually set. + This test verifies the attrsset bitmap and final mode consistency. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT30 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create an ACL that will derive to a specific mode (0754) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + # Request a different mode (0640) + requested_mode = 0o640 + + # Set both MODE and ACL together + attrs = {FATTR4_MODE: requested_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE and ACL together") + + # Check the attrsset bitmap + attrsset = bitmap2list(res.resarray[-1].attrsset) + + # Get the final attributes + final_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + final_mode = final_attrs[FATTR4_MODE] & 0o777 + + # Both attributes were processed, so both must appear in attrsset + if FATTR4_MODE not in attrsset: + t.fail("MODE not in attrsset, but MODE was requested") + if FATTR4_ACL not in attrsset: + t.fail("ACL not in attrsset, but ACL was requested") + + # Per RFC 8881 Section 6.4.1.3, ACL processing modifies the mode, so + # the final mode matches the ACL derivation, not the requested mode + acl_derived_mode = acl2mode_rfc8881(acl) + if final_mode != acl_derived_mode: + t.fail("Final mode (0%o) does not match ACL-derived mode (0%o)" % + (final_mode, acl_derived_mode)) + +def testSetattrModeACLConflict(t, env): + """SETATTR(MODE+ACL) when MODE and ACL would produce different permissions + + Test the interaction when the requested MODE differs significantly from + what the ACL would derive. Per RFC 8881 Section 6.4.1.3, MODE is applied first, + then ACL is applied (possibly changing the final mode). + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT31 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test multiple conflict scenarios + test_cases = [ + # (requested_mode, ACL, description) + (0o777, [ # Request all permissions + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, # But ACL only grants read + b"OWNER@") + ], "MODE=0777 but ACL grants only read"), + + (0o000, [ # Request no permissions + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@") # But ACL grants everything + ], "MODE=0000 but ACL grants rwx"), + + (0o644, [ # Request read for all, write for owner + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_EXECUTE, # But ACL only grants execute + b"OWNER@") + ], "MODE=0644 but ACL grants only execute"), + ] + + for requested_mode, acl, description in test_cases: + fh, stateid = c.create_confirm(t.word() + ("_%o" % requested_mode).encode()) + + # Set both MODE and ACL together + attrs = {FATTR4_MODE: requested_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR MODE+ACL: %s" % description) + + # Get final attributes + final_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + final_mode = final_attrs[FATTR4_MODE] & 0o777 + + # Verify final mode matches derivation from the requested ACL + expected_mode = acl2mode_rfc8881(acl) + + if final_mode != expected_mode: + t.fail("Test case '%s': final mode (0%o) doesn't match " + "ACL-derived mode (0%o). Per RFC 8881 Section 6.4.1.3, " + "ACL should be set as given and mode derived from it." % + (description, final_mode, expected_mode)) + +def testSetattrACLNamedPrincipals(t, env): + """SETATTR(ACL) with named principals should preserve them + + Per RFC 8881 Section 6.4.1.3: "the ACL attribute is set as given." + When an ACL contains named principals (not OWNER@/GROUP@/EVERYONE@), + the server must preserve those exact principals in the stored ACL, + not convert them to special identifiers. For SETATTR(ACL) alone, + Section 6.4.1.2 makes this a SHOULD, so that case only warns. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT32 + """ + c = env.c1 + c.init_connection() + + # First create a file and get its owner and group + temp_fh, temp_stateid = c.create_confirm(t.word() + b"_temp") + attrs = c.do_getattrdict(temp_fh, [FATTR4_OWNER, FATTR4_OWNER_GROUP]) + current_owner = attrs[FATTR4_OWNER] + current_group = attrs[FATTR4_OWNER_GROUP] + + # Test 1: SETATTR(ACL) with named principals only + fh1, stateid1 = c.create_confirm(t.word() + b"_acl_only") + + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + current_owner), # Named principal, not OWNER@ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, ACE4_IDENTIFIER_GROUP, + ACE4_READ_DATA, + current_group) # Named principal, not GROUP@ + ] + + ops = c.use_obj(fh1) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with named principals") + + # Verify ACL was preserved with named principals. Setting the ACL as + # given is a SHOULD in RFC 8881 Section 6.4.1.2, so hold the warning + # until test 2 has run. + returned_attrs = c.do_getattrdict(fh1, [FATTR4_ACL]) + acl_only_warning = None + try: + nfs4acl.verify_acl(returned_attrs[FATTR4_ACL], acl) + except AssertionError as e: + acl_only_warning = ("%s. Server did not set the ACL as given " + "(RFC 8881 Section 6.4.1.2: SHOULD)" % e) + + # Test 2: SETATTR(MODE+ACL) with named principals + fh2, stateid2 = c.create_confirm(t.word() + b"_mode_acl") + + mode = 0o755 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + current_owner), # Named principal + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, ACE4_IDENTIFIER_GROUP, + ACE4_READ_DATA | ACE4_EXECUTE, + current_group), # Named principal + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") # Special identifier is fine + ] + + ops = c.use_obj(fh2) + [c.setattr({FATTR4_MODE: mode, FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR MODE+ACL with named principals") + + # Verify ACL was preserved + returned_attrs = c.do_getattrdict(fh2, [FATTR4_ACL]) + try: + nfs4acl.verify_acl(returned_attrs[FATTR4_ACL], acl) + except AssertionError as e: + t.fail("%s. Server did not set the ACL as given, " + "violating RFC 8881 Section 6.4.1.3" % e) + + if acl_only_warning: + t.pass_warn(acl_only_warning)
Signature · 1 line
2.54.0
Re: [PATCH pynfs v3 8/9] Add tests for SETATTR with MODE and ACL
What about testing FATTR4_MODE_SET_MASKED too? IMO this is a better way to set the mode, because it is atomic and might fit better with ACLs because it only sets intended mode bits.
Aurélien
Quoted 810 lines · Chuck Lever
From: Chuck Lever <[email protected]>
Add comprehensive tests that verify SETATTR behavior when both mode bits and an ACL are set simultaneously. The new tests cover:
1. SATT19: testSetattrModeWithACL
- Sets MODE and ACL in a single SETATTR, once each with a MODE the ACL widens (0o640, 0o600) and one more restrictive than the ACL grants (0o400)
- Verifies both attributes appear in attrsset, the ACL is stored as given, and the final mode is derived from the ACL rather than taken from the requested MODE
2. SATT22: testSetattrACLThenMode
- Compares setting MODE+ACL together vs separately in sequence
- Creates two files and verifies consistent behaviorThese tests ensure that servers correctly handle the interaction between MODE and ACL attributes when setting file attributes, and verify that the ACL specified in the OPEN operation matches the ACL after the OPEN completes.
SETATTR: ACL-to-mode verification tests
These new tests verify:
- Mode is correctly derived from ACL per RFC 8881 §6.3.2
- Write bit requires bothACE4_WRITE_DATAandACE4_APPEND_DATA
- ACL evaluation order (first ACE takes precedence)
- EVERYONE@ ACEs affect all three permission classes
- Complex ACLs with multiple identifiersSETATTR: Two additional ACL verification tests
New test SATT28: Verify that the final ACL is the same no matter what the file's previous mode bits were.
New test SATT29: Verify that SETATTR(ACL) preserves high-order mode bits (SUID/SGID/SVTX).
SETATTR: Verify behavior when setting both MODE and ACL
Add three comprehensive tests that verify SETATTR(MODE+ACL) behavior per RFC 8881 §6.4.1.3:
SATT30:
Verify attrsset bitmap and final mode consistency when MODE+ACL set together. The server must report both MODE and ACL in attrsset, and the final mode must match what the ACL derives to per §6.4.1.3.SATT31:
Test MODE+ACL interaction when they conflict significantly. Regardless of requested MODE, final mode always matches ACL derivation. This shows that ACL wins when both are set (per §6.4.1.3: "possibly changing the final mode").Signed-off-by Chuck Lever <[email protected]>--- nfs4.0/servertests/st_setattr.py | 728 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 727 insertions(+), 1 deletion(-)nfs4.0/servertests/st_setattr.py +727
-2@@ -1,9 +1,10 @@ from xdrdef.nfs4_const import * from .environment import check, get_invalid_utf8strings from nfs4lib import bitmap2list, dict2fattr -from xdrdef.nfs4_type import nfstime4, settime4 +from xdrdef.nfs4_type import nfstime4, settime4, nfsace4 import nfs_ops op = nfs_ops.NFS4ops() +import nfs4acl def _set_mode(t, c, file, stateid=None, msg=" using stateid=0", warnlist=[]):@@ -783,3 +784,728 @@ def testMixed(t, env): c.init_connection() fh, stateid = c.create_confirm(t.word(), deny=OPEN4_SHARE_DENY_NONE) _set_mixed(t, c, fh) + +def testSetattrModeWithACL(t, env): + """SETATTR with both MODE and ACL attributes + + Per RFC 8881 Section 6.4.1.3, when both MODE and ACL are set together, + both are processed, but the final mode is derived from the ACL and may + differ from the requested MODE. The requested MODE is varied from one + that the ACL widens to one more restrictive than the ACL grants. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT19 + """ + c = env.c1 + c.init_connection() + acl = nfs4acl.make_test_acl() + + for mode in (0o640, 0o600, 0o400): + fh, stateid = c.create_confirm(t.word() + (b"_%o" % mode)) + + # Set both MODE and ACL in a single SETATTR + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE 0%o and ACL" % mode) + + # Check which attributes were actually set by examining the reply bitmask + attrsset = bitmap2list(res.resarray[-1].attrsset) + + # Verify both MODE and ACL were set (processed) + if FATTR4_MODE not in attrsset: + t.fail("MODE not in attrsset, but MODE 0%o was requested" % mode) + if FATTR4_ACL not in attrsset: + t.fail("ACL not in attrsset, but ACL was requested with MODE 0%o" + % mode) + + # Verify ACL was set correctly and the mode derived from it + attrs_dict = c.do_getattrdict(fh, [FATTR4_ACL, FATTR4_MODE]) + try: + nfs4acl.verify_mode_and_acl(attrs_dict, acl, + "SETATTR with MODE 0%o" % mode) + except AssertionError as e: + t.fail(str(e)) + +def testSetattrACLThenMode(t, env): + """SETATTR MODE+ACL together vs ACL then MODE separately + + Per RFC 8881 sections 6.4.1.1-6.4.1.3, when MODE and ACL are set + together in a single SETATTR, the server computes the final mode + from the ACL (the MODE attribute is effectively ignored). When + ACL is set first and MODE is set in a separate operation, the + final mode is the explicitly-set MODE value. + + This test verifies this difference by using a MODE (0o755) that + differs from the ACL-derived mode (0o644). + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT22 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create two test files + fh1, stateid1 = c.create_confirm(t.word() + b"_1") + fh2, stateid2 = c.create_confirm(t.word() + b"_2") + + # make_test_acl() derives to mode 0o644 (rw-r--r--) + acl = nfs4acl.make_test_acl() + acl_derived_mode = acl2mode_rfc8881(acl) + + # Use a different mode to demonstrate the RFC-defined difference + explicit_mode = 0o755 + + # File 1: Set MODE and ACL together + # Per RFC 8881 Section 6.4.1.2, mode is derived from ACL (MODE ignored) + attrs = {FATTR4_MODE: explicit_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh1) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE and ACL together") + + # File 2: Set ACL first, then MODE separately + # Per RFC 8881 Section 6.4.1.1, final mode is the explicit MODE value + ops = c.use_obj(fh2) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with ACL") + + ops = c.use_obj(fh2) + [c.setattr({FATTR4_MODE: explicit_mode})] + res = c.compound(ops) + check(res, msg="SETATTR with MODE after ACL") + + # Verify the modes differ as expected per RFC 8881 + attrs1 = c.do_getattrdict(fh1, [FATTR4_MODE, FATTR4_ACL]) + attrs2 = c.do_getattrdict(fh2, [FATTR4_MODE, FATTR4_ACL]) + + mode1 = attrs1[FATTR4_MODE] & 0o777 + mode2 = attrs2[FATTR4_MODE] & 0o777 + + # File 1 (MODE+ACL together): mode should be derived from ACL + if mode1 != acl_derived_mode: + t.fail("MODE+ACL together: expected ACL-derived mode 0%o, got 0%o" + % (acl_derived_mode, mode1)) + + # File 2 (ACL then MODE): mode should be explicit MODE value + if mode2 != explicit_mode: + t.fail("ACL then MODE: expected explicit mode 0%o, got 0%o" + % (explicit_mode, mode2)) + +def testSetattrACLModeDeriveBasic(t, env): + """SETATTR(ACL) should derive mode per RFC 8881 Section 6.3.2 + + Test basic mode derivation from ACL when setting ACL alone. + The mode's permission bits should match what is computed from the ACL. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT23 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create ACL: OWNER@ gets read+write+execute, others get nothing + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"EVERYONE@") + ] + + # Set ACL only (not MODE) + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with ACL only") + + # Get resulting mode + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + + # Compute expected mode from the requested ACL per RFC 8881 Section 6.3.2 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match RFC 8881 Section 6.3.2 derivation " + "from ACL (expected 0%o)" % (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveWriteBits(t, env): + """SETATTR(ACL) write bit requires BOTH WRITE_DATA and APPEND_DATA + + Per RFC 8881 Section 6.3.2, the write mode bit should only be set if BOTH + ACE4_WRITE_DATA and ACE4_APPEND_DATA are present in the ACL. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT24 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test 1: Only WRITE_DATA (no APPEND_DATA) - write bit should NOT be set + fh1, stateid1 = c.create_confirm(t.word() + b"_1") + acl1 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_WRITE_DATA, # Missing APPEND_DATA + b"OWNER@") + ] + ops = c.use_obj(fh1) + [c.setattr({FATTR4_ACL: acl1})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only WRITE_DATA") + + attrs1 = c.do_getattrdict(fh1, [FATTR4_MODE, FATTR4_ACL]) + mode1 = attrs1[FATTR4_MODE] & 0o777 + expected_mode1 = acl2mode_rfc8881(acl1) + + if mode1 != expected_mode1: + t.fail("Mode (0%o) with only WRITE_DATA does not match expected (0%o). " + "Write bit should NOT be set without APPEND_DATA." % + (mode1, expected_mode1)) + + # Test 2: Only APPEND_DATA (no WRITE_DATA) - write bit should NOT be set + fh2, stateid2 = c.create_confirm(t.word() + b"_2") + acl2 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_APPEND_DATA, # Missing WRITE_DATA + b"OWNER@") + ] + ops = c.use_obj(fh2) + [c.setattr({FATTR4_ACL: acl2})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only APPEND_DATA") + + attrs2 = c.do_getattrdict(fh2, [FATTR4_MODE, FATTR4_ACL]) + mode2 = attrs2[FATTR4_MODE] & 0o777 + expected_mode2 = acl2mode_rfc8881(acl2) + + if mode2 != expected_mode2: + t.fail("Mode (0%o) with only APPEND_DATA does not match expected (0%o). " + "Write bit should NOT be set without WRITE_DATA." % + (mode2, expected_mode2)) + + # Test 3: Both WRITE_DATA and APPEND_DATA - write bit SHOULD be set + fh3, stateid3 = c.create_confirm(t.word() + b"_3") + acl3 = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_WRITE_DATA | ACE4_APPEND_DATA, # Both present + b"OWNER@") + ] + ops = c.use_obj(fh3) + [c.setattr({FATTR4_ACL: acl3})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with both WRITE_DATA and APPEND_DATA") + + attrs3 = c.do_getattrdict(fh3, [FATTR4_MODE, FATTR4_ACL]) + mode3 = attrs3[FATTR4_MODE] & 0o777 + expected_mode3 = acl2mode_rfc8881(acl3) + + if mode3 != expected_mode3: + t.fail("Mode (0%o) with WRITE_DATA+APPEND_DATA does not match " + "expected (0%o)" % (mode3, expected_mode3)) + +def testSetattrACLModeDeriveAllowDeny(t, env): + """SETATTR(ACL) with ALLOW/DENY interaction + + Test that ACL evaluation order is correct when mixing ALLOW and DENY ACEs. + Per RFC 8881 Section 6.3.2, evaluate ACEs in order, with earlier ACEs taking + precedence. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT25 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test: ALLOW first, then DENY - the ALLOW should win + fh, stateid = c.create_confirm(t.word()) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"OWNER@"), + nfsace4(ACE4_ACCESS_DENIED_ACE_TYPE, 0, + ACE4_READ_DATA, # This should be ignored (already allowed) + b"OWNER@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with ALLOW then DENY") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) with ALLOW/DENY does not match expected (0%o). " + "First ACE should take precedence." % (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveEveryone(t, env): + """SETATTR(ACL) with EVERYONE@ affecting all identifiers + + Test that EVERYONE@ ACEs are considered when evaluating permissions + for OWNER@ and GROUP@ per RFC 8881 Section 6.3.2. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT26 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # EVERYONE@ gets read, specific OWNER@ gets nothing extra + # Final result: OWNER@ should have read (from EVERYONE@) + fh, stateid = c.create_confirm(t.word()) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with only EVERYONE@") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match expected (0%o). " + "EVERYONE@ should affect all mode bits." % + (returned_mode, expected_mode)) + +def testSetattrACLModeDeriveComplex(t, env): + """SETATTR(ACL) with complex ACL including multiple identifiers + + Test mode derivation with a more realistic ACL including OWNER@, + GROUP@, and EVERYONE@ with various permissions. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT27 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create a complex ACL: + # OWNER@: read + write + execute + # GROUP@: read + execute (no write) + # EVERYONE@: read only + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR with complex ACL") + + attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + returned_mode = attrs[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if returned_mode != expected_mode: + t.fail("Mode (0%o) does not match RFC 8881 Section 6.3.2 derivation " + "from complex ACL (expected 0%o)" % + (returned_mode, expected_mode)) + +def testSetattrACLIndependentOfMode(t, env): + """SETATTR(ACL) outcome should not depend on existing mode bits + + Per RFC 8881 Section 6.4.1.2, when setting ACL without mode, the ACL should + be set as given. The existing mode should not affect the ACL that gets + stored. This test verifies that setting the same ACL on files with + different initial modes results in identical ACLs being stored. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT28 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create the same ACL to use for all files + # OWNER@: read + write, GROUP@: read, EVERYONE@: nothing + test_acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@") + ] + + # Test with various initial mode values + initial_modes = [0o600, 0o644, 0o755, 0o777, 0o400, 0o000] + files = [] + + # Create files with different initial modes + for initial_mode in initial_modes: + fh, stateid = c.create_confirm(t.word() + (b"_%o" % initial_mode)) + + # Set initial mode + ops = c.use_obj(fh) + [c.setattr({FATTR4_MODE: initial_mode})] + res = c.compound(ops) + check(res, msg="Setting initial mode to 0%o" % initial_mode) + + files.append((fh, initial_mode)) + + # Now set the same ACL on all files + for fh, initial_mode in files: + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: test_acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL on file with initial mode 0%o" % initial_mode) + + # Retrieve the ACLs that were actually stored + acls = [] + for fh, initial_mode in files: + attrs = c.do_getattrdict(fh, [FATTR4_ACL]) + acls.append((initial_mode, attrs[FATTR4_ACL])) + + # Helper function to compare ACLs (comparing ACE lists) + def acl_equal(acl1, acl2): + """Compare two ACLs for equality""" + if len(acl1) != len(acl2): + return False + for ace1, ace2 in zip(acl1, acl2): + if (ace1.type != ace2.type or + ace1.flag != ace2.flag or + ace1.access_mask != ace2.access_mask or + ace1.who != ace2.who): + return False + return True + + def acl_to_string(acl): + """Convert ACL to readable string for error messages""" + aces = [] + for ace in acl: + type_str = "ALLOW" if ace.type == ACE4_ACCESS_ALLOWED_ACE_TYPE else "DENY" + aces.append("<%s:%s:0x%x:0x%x>" % + (type_str, ace.who.decode(), ace.access_mask, ace.flag)) + return "[" + ", ".join(aces) + "]" + + # Compare all ACLs - they should all be identical + reference_mode, reference_acl = acls[0] + for initial_mode, acl in acls[1:]: + if not acl_equal(reference_acl, acl): + t.fail("ACLs differ based on initial mode! " + "File with mode 0%o has ACL %s, " + "but file with mode 0%o has ACL %s. " + "RFC 8881 Section 6.4.1.2: ACL should be set as given, " + "independent of existing mode." % + (reference_mode, acl_to_string(reference_acl), + initial_mode, acl_to_string(acl))) + + # Also verify that the mode derived from the ACL is consistent + modes = [] + for initial_mode, acl in acls: + derived_mode = acl2mode_rfc8881(acl) + modes.append(derived_mode) + + if len(set(modes)) != 1: + mode_str = ", ".join("0%o (from initial 0%o)" % (m, acls[i][0]) + for i, m in enumerate(modes)) + t.fail("ACLs derive to different modes: %s. " + "This suggests ACLs were stored differently based on initial mode." % + mode_str) + +def testSetattrACLIndependentModeHighBits(t, env): + """SETATTR(ACL) should preserve high-order mode bits (SUID/SGID/SVTX) + + Per RFC 8881 Section 6.4.1.2, when setting ACL without mode, the three + high-order bits of mode (SUID, SGID, SVTX) SHOULD remain unchanged. + Only the low-order nine permission bits should be modified. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT29 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Create ACL to set + test_acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@") + ] + + # Test with different high-order bits set + test_cases = [ + (0o4755, "SUID"), # Set-user-ID + (0o2755, "SGID"), # Set-group-ID + (0o1755, "SVTX"), # Sticky bit + (0o6755, "SUID+SGID"), # Both SUID and SGID + (0o7755, "ALL"), # All three bits + ] + + # Preserving the high-order bits is a SHOULD, so collect violations + # and warn once the MUST checks below have run for every case + not_preserved = [] + for initial_mode, description in test_cases: + fh, stateid = c.create_confirm(t.word() + (b"_%s" % description.encode())) + + # Set initial mode with high-order bits + ops = c.use_obj(fh) + [c.setattr({FATTR4_MODE: initial_mode})] + res = c.compound(ops) + check(res, msg="Setting initial mode 0%o (%s)" % (initial_mode, description)) + + # Verify the mode was set + before_attrs = c.do_getattrdict(fh, [FATTR4_MODE]) + before_mode = before_attrs[FATTR4_MODE] & 0o7777 + + # Set ACL only (not mode) + ops = c.use_obj(fh) + [c.setattr({FATTR4_ACL: test_acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL on file with %s" % description) + + # Check that high-order bits are preserved + after_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + after_mode = after_attrs[FATTR4_MODE] & 0o7777 + + # Extract high-order bits (SUID, SGID, SVTX) + before_high = before_mode & 0o7000 + after_high = after_mode & 0o7000 + + if before_high != after_high: + not_preserved.append("%s: before=0%o, after=0%o" % + (description, before_mode, after_mode)) + + # Verify low-order bits match ACL derivation + after_low = after_mode & 0o777 + expected_low = acl2mode_rfc8881(test_acl) + + if after_low != expected_low: + t.fail("Low-order mode bits (0%o) don't match RFC 8881 Section 6.3.2 " + "derivation (expected 0%o) for %s" % + (after_low, expected_low, description)) + + if not_preserved: + t.pass_warn("High-order mode bits not preserved (RFC 8881 Section " + "6.4.1.2: SHOULD remain unchanged): %s" % + "; ".join(not_preserved)) + +def testSetattrModeACLattrsset(t, env): + """SETATTR(MODE+ACL) should indicate which attributes were actually set + + Per RFC 8881 Section 6.4.1.3, when both MODE and ACL are set together, the + server processes MODE first, then ACL (which may modify the mode). + The attrsset bitmap indicates which attributes were actually set. + This test verifies the attrsset bitmap and final mode consistency. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT30 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + fh, stateid = c.create_confirm(t.word()) + + # Create an ACL that will derive to a specific mode (0754) + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + + # Request a different mode (0640) + requested_mode = 0o640 + + # Set both MODE and ACL together + attrs = {FATTR4_MODE: requested_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR with MODE and ACL together") + + # Check the attrsset bitmap + attrsset = bitmap2list(res.resarray[-1].attrsset) + + # Get the final attributes + final_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + final_mode = final_attrs[FATTR4_MODE] & 0o777 + + # Both attributes were processed, so both must appear in attrsset + if FATTR4_MODE not in attrsset: + t.fail("MODE not in attrsset, but MODE was requested") + if FATTR4_ACL not in attrsset: + t.fail("ACL not in attrsset, but ACL was requested") + + # Per RFC 8881 Section 6.4.1.3, ACL processing modifies the mode, so + # the final mode matches the ACL derivation, not the requested mode + acl_derived_mode = acl2mode_rfc8881(acl) + if final_mode != acl_derived_mode: + t.fail("Final mode (0%o) does not match ACL-derived mode (0%o)" % + (final_mode, acl_derived_mode)) + +def testSetattrModeACLConflict(t, env): + """SETATTR(MODE+ACL) when MODE and ACL would produce different permissions + + Test the interaction when the requested MODE differs significantly from + what the ACL would derive. Per RFC 8881 Section 6.4.1.3, MODE is applied first, + then ACL is applied (possibly changing the final mode). + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT31 + """ + from nfs4acl import acl2mode_rfc8881 + c = env.c1 + c.init_connection() + + # Test multiple conflict scenarios + test_cases = [ + # (requested_mode, ACL, description) + (0o777, [ # Request all permissions + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, # But ACL only grants read + b"OWNER@") + ], "MODE=0777 but ACL grants only read"), + + (0o000, [ # Request no permissions + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@") # But ACL grants everything + ], "MODE=0000 but ACL grants rwx"), + + (0o644, [ # Request read for all, write for owner + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_EXECUTE, # But ACL only grants execute + b"OWNER@") + ], "MODE=0644 but ACL grants only execute"), + ] + + for requested_mode, acl, description in test_cases: + fh, stateid = c.create_confirm(t.word() + ("_%o" % requested_mode).encode()) + + # Set both MODE and ACL together + attrs = {FATTR4_MODE: requested_mode, FATTR4_ACL: acl} + ops = c.use_obj(fh) + [c.setattr(attrs)] + res = c.compound(ops) + check(res, msg="SETATTR MODE+ACL: %s" % description) + + # Get final attributes + final_attrs = c.do_getattrdict(fh, [FATTR4_MODE, FATTR4_ACL]) + final_mode = final_attrs[FATTR4_MODE] & 0o777 + + # Verify final mode matches derivation from the requested ACL + expected_mode = acl2mode_rfc8881(acl) + + if final_mode != expected_mode: + t.fail("Test case '%s': final mode (0%o) doesn't match " + "ACL-derived mode (0%o). Per RFC 8881 Section 6.4.1.3, " + "ACL should be set as given and mode derived from it." % + (description, final_mode, expected_mode)) + +def testSetattrACLNamedPrincipals(t, env): + """SETATTR(ACL) with named principals should preserve them + + Per RFC 8881 Section 6.4.1.3: "the ACL attribute is set as given." + When an ACL contains named principals (not OWNER@/GROUP@/EVERYONE@), + the server must preserve those exact principals in the stored ACL, + not convert them to special identifiers. For SETATTR(ACL) alone, + Section 6.4.1.2 makes this a SHOULD, so that case only warns. + + FLAGS: setattr acl all + DEPEND: MODE ACL0 + CODE: SATT32 + """ + c = env.c1 + c.init_connection() + + # First create a file and get its owner and group + temp_fh, temp_stateid = c.create_confirm(t.word() + b"_temp") + attrs = c.do_getattrdict(temp_fh, [FATTR4_OWNER, FATTR4_OWNER_GROUP]) + current_owner = attrs[FATTR4_OWNER] + current_group = attrs[FATTR4_OWNER_GROUP] + + # Test 1: SETATTR(ACL) with named principals only + fh1, stateid1 = c.create_confirm(t.word() + b"_acl_only") + + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + current_owner), # Named principal, not OWNER@ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, ACE4_IDENTIFIER_GROUP, + ACE4_READ_DATA, + current_group) # Named principal, not GROUP@ + ] + + ops = c.use_obj(fh1) + [c.setattr({FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR ACL with named principals") + + # Verify ACL was preserved with named principals. Setting the ACL as + # given is a SHOULD in RFC 8881 Section 6.4.1.2, so hold the warning + # until test 2 has run. + returned_attrs = c.do_getattrdict(fh1, [FATTR4_ACL]) + acl_only_warning = None + try: + nfs4acl.verify_acl(returned_attrs[FATTR4_ACL], acl) + except AssertionError as e: + acl_only_warning = ("%s. Server did not set the ACL as given " + "(RFC 8881 Section 6.4.1.2: SHOULD)" % e) + + # Test 2: SETATTR(MODE+ACL) with named principals + fh2, stateid2 = c.create_confirm(t.word() + b"_mode_acl") + + mode = 0o755 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + current_owner), # Named principal + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, ACE4_IDENTIFIER_GROUP, + ACE4_READ_DATA | ACE4_EXECUTE, + current_group), # Named principal + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") # Special identifier is fine + ] + + ops = c.use_obj(fh2) + [c.setattr({FATTR4_MODE: mode, FATTR4_ACL: acl})] + res = c.compound(ops) + check(res, msg="SETATTR MODE+ACL with named principals") + + # Verify ACL was preserved + returned_attrs = c.do_getattrdict(fh2, [FATTR4_ACL]) + try: + nfs4acl.verify_acl(returned_attrs[FATTR4_ACL], acl) + except AssertionError as e: + t.fail("%s. Server did not set the ACL as given, " + "violating RFC 8881 Section 6.4.1.3" % e) + + if acl_only_warning: + t.pass_warn(acl_only_warning) --2.54.0
Signature · 2 lines
Aurélien Couderc <[email protected]> Big Data/Data mining expert, chess enthusiast
Chuck Lever
maintainerRe: [PATCH pynfs v3 8/9] Add tests for SETATTR with MODE and ACL
Quoted 3 lines · Aurélien Couderc
What about testing
FATTR4_MODE_SET_MASKEDtoo? IMO this is a better way to set the mode, because it is atomic and might fit better with ACLs because it only sets intended mode bits.
So, this thread is a repost of last year's pynfs ACL+MODE tests. I was trying to include only fixes and clean-ups.
To be clear, are you asking for an additional pynfs test, or for additional logic in the Linux in-kernel NFS server? Or both?
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Re: [PATCH pynfs v3 8/9] Add tests for SETATTR with MODE and ACL
Quoted 8 lines · Chuck Lever
Quoted 3 lines · Aurélien Couderc
What about testing
FATTR4_MODE_SET_MASKEDtoo? IMO this is a better way to set the mode, because it is atomic and might fit better with ACLs because it only sets intended mode bits.
Quoted 5 lines
So, this thread is a repost of last year's pynfs ACL+MODE tests. I was trying to include only fixes and clean-ups.
To be clear, are you asking for an additional pynfs test, or for additional logic in the Linux in-kernel NFS server? Or both?
First I was thinking only about a pynfs test for FATTR4_MODE_SET_MASKED, but then I found out that Linux does not support it. IMO the answer is therefore "BOTH". But first the test, because we need to test this in ganesha too.
Aurélien
Signature · 2 lines
Aurélien Couderc <[email protected]> Big Data/Data mining expert, chess enthusiast
Chuck Lever
maintainerRe: [PATCH pynfs v3 8/9] Add tests for SETATTR with MODE and ACL
Quoted 12 lines · Aurélien Couderc
Quoted 11 lines · Chuck Lever
Quoted 3 lines · Aurélien Couderc
What about testing
FATTR4_MODE_SET_MASKEDtoo? IMO this is a better way to set the mode, because it is atomic and might fit better with ACLs because it only sets intended mode bits.So, this thread is a repost of last year's pynfs ACL+MODE tests. I was trying to include only fixes and clean-ups.
Quoted 7 lines
Quoted 2 lines
To be clear, are you asking for an additional pynfs test, or for additional logic in the Linux in-kernel NFS server? Or both?
First I was thinking only about a pynfs test for
FATTR4_MODE_SET_MASKED, but then I found out that Linux does not support it. IMO the answer is therefore "BOTH". But first the test, because we need to test this in ganesha too.
I'll make a note of the ganesha interest and look into implementing both.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Chuck Lever
maintainerPATCH v3 9/9 Add tests for OPEN(create) with ACLs
In reply to Chuck LeverFrom: Chuck Lever <[email protected]>
The NFSv4.1 OPEN tests set MODE at create time but never request an ACL. A server that ignores FATTR4_ACL in the createattrs, or that applies the requested MODE after both MODE and ACL are given, passes the suite. RFC 8881 Section 6.4.1.3 requires the ACL to be set as given and the mode to be derived from that ACL per Section 6.3.2.
Add OPEN8a and OPEN8b to check that the server advertises FATTR4_ACL in supported_attrs and in suppattr_exclcreat, OPEN12 and OPEN13 to check suppattr_exclcreat itself, then OPEN11, OPEN14, and OPEN38 through OPEN40 to create files with MODE alone, with an ACL alone, and with MODE and ACL together under UNCHECKED4, GUARDED4, and EXCLUSIVE4_1. The combined cases include a MODE more restrictive than the ACL and a MODE that conflicts with it. Where an ACL is requested, the test verifies the returned ACL against it and compares the resulting mode with the RFC 8881 derivation from the requested ACL.
--- nfs4.1/server41tests/st_open.py | 463 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 460 insertions(+), 3 deletions(-)
nfs4.1/server41tests/st_open.py +460 -3
@@ -1,15 +1,16 @@ from .st_create_session import create_session from xdrdef.nfs4_const import * -from .environment import check, fail, create_file, open_file, close_file, write_file, read_file -from .environment import open_create_file_op +from .environment import check, fail, unsupported, create_file, open_file, close_file, write_file, read_file +from .environment import open_create_file_op, do_getattrdict from xdrdef.nfs4_type import open_owner4, openflag4, createhow4, open_claim4 from xdrdef.nfs4_type import creatverfattr, fattr4, stateid4, locker4, lock_owner4 -from xdrdef.nfs4_type import open_to_lock_owner4 +from xdrdef.nfs4_type import open_to_lock_owner4, nfsace4 import nfs_ops op = nfs_ops.NFS4ops() import threading import nfs4lib +import nfs4acl def expect(res, seqid): """Verify that open result has expected stateid.seqid"""
@@ -195,3 +196,459 @@ def testCloseWithZeroSeqid(t, env): stateid.seqid = 0 res = close_file(sess1, fh, stateid=stateid) check(res) + +def testSuppattrExclcreat(t, env): + """Check that FATTR4_SUPPATTR_EXCLCREAT is supported and valid + + FLAGS: open all + CODE: OPEN12 + """ + sess = env.c1.new_client_session(env.testname(t)) + res = sess.compound([op.putrootfh(), + op.getattr(nfs4lib.list2bitmap([FATTR4_SUPPORTED_ATTRS, + FATTR4_SUPPATTR_EXCLCREAT]))]) + check(res) + attrs_info = res.resarray[-1].obj_attributes + + if FATTR4_SUPPORTED_ATTRS not in attrs_info: + fail("Server did not return FATTR4_SUPPORTED_ATTRS") + + # SUPPATTR_EXCLCREAT is REQUIRED (RFC 8881 Section 5.6), so a server + # that omits it from supported_attrs is non-compliant, not unsupported + supported = attrs_info[FATTR4_SUPPORTED_ATTRS] + if not (supported & (1 << FATTR4_SUPPATTR_EXCLCREAT)): + fail("REQUIRED attribute FATTR4_SUPPATTR_EXCLCREAT not in " + "FATTR4_SUPPORTED_ATTRS") + + # If supported, check that it was returned + if FATTR4_SUPPATTR_EXCLCREAT not in attrs_info: + fail("FATTR4_SUPPATTR_EXCLCREAT not returned even though it " + "appears in FATTR4_SUPPORTED_ATTRS") + +def testSuppattrExclcreatSubset(t, env): + """FATTR4_SUPPATTR_EXCLCREAT must be subset of SUPPORTED_ATTRS + + FLAGS: open all + CODE: OPEN13 + DEPEND: OPEN12 + """ + sess = env.c1.new_client_session(env.testname(t)) + res = sess.compound([op.putrootfh(), + op.getattr(nfs4lib.list2bitmap([FATTR4_SUPPORTED_ATTRS, + FATTR4_SUPPATTR_EXCLCREAT]))]) + check(res) + attrs_info = res.resarray[-1].obj_attributes + + supported = attrs_info[FATTR4_SUPPORTED_ATTRS] + excl_supported = attrs_info[FATTR4_SUPPATTR_EXCLCREAT] + + # SUPPATTR_EXCLCREAT must be a subset of SUPPORTED_ATTRS + # i.e., every bit set in excl_supported must also be set in supported + invalid = excl_supported & ~supported + if invalid != 0: + fail("FATTR4_SUPPATTR_EXCLCREAT contains attributes not in " + "FATTR4_SUPPORTED_ATTRS:\n" + " Invalid attributes: %s" % nfs4lib.attr_bitmap_to_str(invalid)) + +def testACLSupported(t, env): + """Check that server supports FATTR4_ACL attribute + + FLAGS: open acl all + CODE: OPEN8a + """ + sess = env.c1.new_client_session(env.testname(t)) + res = sess.compound([op.putrootfh(), + op.getattr(nfs4lib.list2bitmap([FATTR4_SUPPORTED_ATTRS]))]) + check(res) + attrs_info = res.resarray[-1].obj_attributes + if FATTR4_SUPPORTED_ATTRS not in attrs_info: + fail("Server did not return FATTR4_SUPPORTED_ATTRS") + supported = attrs_info[FATTR4_SUPPORTED_ATTRS] + if not (supported & (1 << FATTR4_ACL)): + unsupported("Server does not support FATTR4_ACL attribute") + +def testACLExclusiveSupported(t, env): + """Check that server supports setting ACL during EXCLUSIVE4_1 create + + FLAGS: open acl all + CODE: OPEN8b + """ + sess = env.c1.new_client_session(env.testname(t)) + res = sess.compound([op.putrootfh(), + op.getattr(nfs4lib.list2bitmap([FATTR4_SUPPATTR_EXCLCREAT]))]) + check(res) + attrs_info = res.resarray[-1].obj_attributes + if FATTR4_SUPPATTR_EXCLCREAT not in attrs_info: + fail("REQUIRED attribute FATTR4_SUPPATTR_EXCLCREAT not returned") + excl_supported = attrs_info[FATTR4_SUPPATTR_EXCLCREAT] + if not (excl_supported & (1 << FATTR4_ACL)): + unsupported("Server does not support setting FATTR4_ACL during " + "EXCLUSIVE4_1 create") + +def testOpenCreateWithACLExclusive(t, env): + """OPEN with EXCLUSIVE4_1 CREATE setting NFSv4 ACL attribute + + FLAGS: open acl all + CODE: OPEN11 + DEPEND: OPEN8b + """ + sess = env.c1.new_client_session(env.testname(t)) + acl = nfs4acl.make_test_acl() + + # Create file with ACL attribute using EXCLUSIVE4_1 mode + # EXCLUSIVE4_1 allows attributes to be set atomically with create + # Don't set MODE with ACL - let the ACL determine permissions + attrs = {FATTR4_ACL: acl} + res = create_file(sess, env.testname(t), attrs=attrs, mode=EXCLUSIVE4_1) + check(res) + expect(res, seqid=1) + + fh = res.resarray[-1].object + stateid = res.resarray[-2].stateid + + # Verify the ACL was set correctly by reading it back + attrs_dict = do_getattrdict(sess, fh, [FATTR4_ACL]) + if FATTR4_ACL not in attrs_dict: + fail("ACL attribute not returned after OPEN with CREATE") + + try: + nfs4acl.verify_acl(attrs_dict[FATTR4_ACL], acl) + except AssertionError as e: + fail(str(e)) + + res = close_file(sess, fh, stateid=stateid) + check(res) + +def testOpenCreateModeWithACL(t, env): + """OPEN(create) setting both MODE and ACL under every create mode + + Per RFC 8881 Section 6.4.1.3, when both MODE and ACL are set together, + both are processed, but the final mode is derived from the ACL and may + differ from the requested MODE. Each create mode is exercised with a + MODE the ACL widens and with one more restrictive than the ACL grants. + + FLAGS: open acl all + CODE: OPEN14 + DEPEND: OPEN8a OPEN8b + """ + sess = env.c1.new_client_session(env.testname(t)) + acl = nfs4acl.make_test_acl() + + cases = [ + (UNCHECKED4, b"unchecked"), + (GUARDED4, b"guarded"), + (EXCLUSIVE4_1, b"exclusive"), + ] + for mode in (0o640, 0o600, 0o400): + for createmode, label in cases: + name = env.testname(t) + b"_%o_%s" % (mode, label) + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + res = create_file(sess, name, attrs=attrs, mode=createmode) + check(res) + expect(res, seqid=1) + + fh = res.resarray[-1].object + stateid = res.resarray[-2].stateid + + # Check which attributes were actually set by examining the + # reply bitmask + attrset = nfs4lib.bitmap2list(res.resarray[-2].attrset) + if FATTR4_MODE not in attrset: + fail("MODE not in attrset, but MODE 0%o was requested (%s)" + % (mode, label)) + if FATTR4_ACL not in attrset: + fail("ACL not in attrset, but ACL was requested (%s)" % label) + + # Verify ACL was set correctly and the mode derived from it + attrs_dict = do_getattrdict(sess, fh, [FATTR4_ACL, FATTR4_MODE]) + try: + nfs4acl.verify_mode_and_acl( + attrs_dict, acl, + "%s CREATE with MODE 0%o" % (label.decode(), mode)) + except AssertionError as e: + fail(str(e)) + + res = close_file(sess, fh, stateid=stateid) + check(res) + +# +# Tests for OPEN CREATE with MODE and ACL per RFC 8881 Section 6.4.3 +# + +def testOpenCreateModeDerivation(t, env): + """OPEN CREATE with MODE should derive ACL from mode + + Per RFC 8881 Section 6.4.3: If just mode is given, the mode MUST be + applied to the inherited/created ACL per Section 6.4.1.1. Test with + UNCHECKED4, GUARDED4, and EXCLUSIVE4_1. + + FLAGS: open acl all + CODE: OPEN38 + DEPEND: OPEN8a OPEN8b + """ + from nfs4acl import acl2mode_rfc8881 + + sess = env.c1.new_client_session(env.testname(t)) + + # Test with UNCHECKED4 + mode = 0o640 + attrs = {FATTR4_MODE: mode} + name1 = env.testname(t) + b"_unchecked" + res = create_file(sess, name1, attrs=attrs, mode=UNCHECKED4) + check(res) + fh1 = res.resarray[-1].object + stateid1 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh1, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + if result_mode != mode: + fail("UNCHECKED4: mode (0%o) doesn't match requested (0%o)" % + (result_mode, mode)) + + acl_derived_mode = acl2mode_rfc8881(attrs_dict[FATTR4_ACL]) + if result_mode != acl_derived_mode: + fail("UNCHECKED4: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, acl_derived_mode)) + + res = close_file(sess, fh1, stateid=stateid1) + check(res) + + # Test with GUARDED4 + mode = 0o750 + attrs = {FATTR4_MODE: mode} + name2 = env.testname(t) + b"_guarded" + res = create_file(sess, name2, attrs=attrs, mode=GUARDED4) + check(res) + fh2 = res.resarray[-1].object + stateid2 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh2, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + if result_mode != mode: + fail("GUARDED4: mode (0%o) doesn't match requested (0%o)" % + (result_mode, mode)) + + acl_derived_mode = acl2mode_rfc8881(attrs_dict[FATTR4_ACL]) + if result_mode != acl_derived_mode: + fail("GUARDED4: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, acl_derived_mode)) + + res = close_file(sess, fh2, stateid=stateid2) + check(res) + + # Test with EXCLUSIVE4_1 + mode = 0o755 + attrs = {FATTR4_MODE: mode} + name3 = env.testname(t) + b"_exclusive" + res = create_file(sess, name3, attrs=attrs, mode=EXCLUSIVE4_1) + check(res) + fh3 = res.resarray[-1].object + stateid3 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh3, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + if result_mode != mode: + fail("EXCLUSIVE4_1: mode (0%o) doesn't match requested (0%o)" % + (result_mode, mode)) + + acl_derived_mode = acl2mode_rfc8881(attrs_dict[FATTR4_ACL]) + if result_mode != acl_derived_mode: + fail("EXCLUSIVE4_1: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, acl_derived_mode)) + + res = close_file(sess, fh3, stateid=stateid3) + check(res) + +def testOpenCreateACLDerivation(t, env): + """OPEN CREATE with ACL should derive mode from ACL + + Per RFC 8881 Section 6.4.3: If just ACL is given, inheritance SHOULD NOT + take place, ACL should be set as given, and mode modified per Section 6.4.1.2. + + FLAGS: open acl all + CODE: OPEN39 + DEPEND: OPEN8a OPEN8b + """ + from nfs4acl import acl2mode_rfc8881 + + sess = env.c1.new_client_session(env.testname(t)) + + # Test with UNCHECKED4 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"GROUP@") + ] + attrs = {FATTR4_ACL: acl} + name1 = env.testname(t) + b"_unchecked" + res = create_file(sess, name1, attrs=attrs, mode=UNCHECKED4) + check(res) + fh1 = res.resarray[-1].object + stateid1 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh1, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("UNCHECKED4: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, expected_mode)) + + res = close_file(sess, fh1, stateid=stateid1) + check(res) + + # Test with GUARDED4 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"OWNER@") + ] + attrs = {FATTR4_ACL: acl} + name2 = env.testname(t) + b"_guarded" + res = create_file(sess, name2, attrs=attrs, mode=GUARDED4) + check(res) + fh2 = res.resarray[-1].object + stateid2 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh2, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("GUARDED4: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, expected_mode)) + + res = close_file(sess, fh2, stateid=stateid2) + check(res) + + # Test with EXCLUSIVE4_1 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + attrs = {FATTR4_ACL: acl} + name3 = env.testname(t) + b"_exclusive" + res = create_file(sess, name3, attrs=attrs, mode=EXCLUSIVE4_1) + check(res) + fh3 = res.resarray[-1].object + stateid3 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh3, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("EXCLUSIVE4_1: mode (0%o) doesn't match ACL derivation (0%o)" % + (result_mode, expected_mode)) + + res = close_file(sess, fh3, stateid=stateid3) + check(res) + +def testOpenCreateModeACLConflict(t, env): + """OPEN CREATE with both MODE and ACL - ACL should win + + Per RFC 8881 Section 6.4.3: If both mode and ACL are given, both attributes + will be set per Section 6.4.1.3 (MODE first, then ACL, with ACL modifying + the final mode). + + FLAGS: open acl all + CODE: OPEN40 + DEPEND: OPEN8a OPEN8b + """ + from nfs4acl import acl2mode_rfc8881 + + sess = env.c1.new_client_session(env.testname(t)) + + # Test with UNCHECKED4: ACL would derive to 0754, request MODE 0640 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA | ACE4_EXECUTE, + b"OWNER@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_EXECUTE, + b"GROUP@"), + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA, + b"EVERYONE@") + ] + mode = 0o640 + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + name1 = env.testname(t) + b"_unchecked" + res = create_file(sess, name1, attrs=attrs, mode=UNCHECKED4) + check(res) + fh1 = res.resarray[-1].object + stateid1 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh1, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("UNCHECKED4: mode (0%o) doesn't match ACL derivation (0%o). " + "Per Section 6.4.3, should follow Section 6.4.1.3 (ACL wins)" % + (result_mode, expected_mode)) + + res = close_file(sess, fh1, stateid=stateid1) + check(res) + + # Test with GUARDED4 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_READ_DATA | ACE4_WRITE_DATA | ACE4_APPEND_DATA, + b"OWNER@") + ] + mode = 0o755 + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + name2 = env.testname(t) + b"_guarded" + res = create_file(sess, name2, attrs=attrs, mode=GUARDED4) + check(res) + fh2 = res.resarray[-1].object + stateid2 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh2, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("GUARDED4: mode (0%o) doesn't match ACL derivation (0%o). " + "Per Section 6.4.3, should follow Section 6.4.1.3" % + (result_mode, expected_mode)) + + res = close_file(sess, fh2, stateid=stateid2) + check(res) + + # Test with EXCLUSIVE4_1 + acl = [ + nfsace4(ACE4_ACCESS_ALLOWED_ACE_TYPE, 0, + ACE4_EXECUTE, + b"OWNER@") + ] + mode = 0o644 + attrs = {FATTR4_MODE: mode, FATTR4_ACL: acl} + name3 = env.testname(t) + b"_exclusive" + res = create_file(sess, name3, attrs=attrs, mode=EXCLUSIVE4_1) + check(res) + fh3 = res.resarray[-1].object + stateid3 = res.resarray[-2].stateid + + attrs_dict = do_getattrdict(sess, fh3, [FATTR4_MODE, FATTR4_ACL]) + result_mode = attrs_dict[FATTR4_MODE] & 0o777 + expected_mode = acl2mode_rfc8881(acl) + + if result_mode != expected_mode: + fail("EXCLUSIVE4_1: mode (0%o) doesn't match ACL derivation (0%o). " + "Per Section 6.4.3, should follow Section 6.4.1.3" % + (result_mode, expected_mode)) + + res = close_file(sess, fh3, stateid=stateid3) + check(res)
Signature · 1 line
2.54.0
Quoted 64 lines · Chuck Lever
There are plenty of corner cases when an NFSv4 client requests setting an NFSv4 ACL and POSIX mode bits in the a single SETATTR request. It's even worse for NFS server implementations that have to translate NFSv4 ACLs to POSIX ACLs.
Note that the in-kernel Linux NFS client itself does not support NFSv4 ACLs since Linux is a POSIX ACL ecosystem. I believe it will never send an OPEN(create) or SETATTR that sets an NFSv4 ACL and mode bits simultaneously, relying on only user space tooling to set the ACL. So we must depend on only pynfs for testing this particular NFSD facility.
pynfs didn't have many tests in this particular category when I set out to troubleshoot a recently reported ACL+MODE bug in NFSD. So I've written a handful to exercise this specific case.
Claude and sashiko review found a number of issues in this old series. Resending with fixes.
---
Changes in v3:
- Compare ACE who fields as bytes inacl2mode_rfc8881(),make_test_acl()
- Run testMixed's RFC 8881 mode check beforepass_warn(), which raises
- Compare ACE flags inverify_acl()
- Letverify_acl()skip ACEs the server inserts before requested ones
- Letverify_acl()search past a matching ACE whose mask falls short
- Derive the expected mode from the requested ACL, not the returned one
- Fold SATT20/21 into SATT19 and drop thepass_warn()that always fired
- Require MODE in attrsset in SATT30, as SATT19 already does
- Mask the mode with 0o777, not 0o7777, in SATT22 and SATT30
- Warn rather than fail on the SHOULD clauses in SATT29 and SATT32
- Fail OPEN12 and OPEN8b when the REQUIREDsuppattr_exclcreatis missing
- Verify the whole ACE, not just who, in testSetattrACLNamedPrincipals
- Fold OPEN9/10 and OPEN15-18 into OPEN14, which covers all create modes
- Rewrite the OPEN(create) patch description
- Correct the SATT30 item and test list in the SETATTR patch description
- Link to v2: https://lore.kernel.org/linux-nfs/[email protected]/Changes in v2:
- De-duplicate code shared between NFSv4.0 and NFSv4.1
- Remove named principals tests for now
- Replace incorrect usage of binary strings
- Add missing patch descriptions---
Chuck Lever (9):
Add helper to report unsupported protocol features Add helper to format attribute bitmaps
Add a helper to compute POSIX mode bits from NFSv4 ACLs Addaccess_mask_to_str()helper tonfs4.0/nfs4acl.pyAddmake_test_acl()helper to nfs4acl modules Addverify_acl()helper to nfs4acl modules Addverify_mode_and_acl()helper to nfs4acl modules Add tests for SETATTR with MODE and ACL
Add tests for OPEN(create) with ACLsnfs4.0/nfs4acl.py | 193 ++++++++++ nfs4.0/servertests/st_setattr.py | 728 +++++++++++++++++++++++++++++++++++- nfs4.1/nfs4acl.py | 1 + nfs4.1/nfs4lib.py | 8 + nfs4.1/server41tests/environment.py | 3 + nfs4.1/server41tests/st_open.py | 463 ++++++++++++++++++++++- 6 files changed, 1392 insertions(+), 4 deletions(-)
Quoted 3 lines
---
base-commit:cd4701827a8261fedbfb4c6e39029fb9671321a6
change-id: 20261008-acl-mode-f3c35108c597
Please add me in the "Reviewed-by: Aurélien Couderc <[email protected]>" line.
Aurélien
Signature · 2 lines
Aurélien Couderc <[email protected]> Big Data/Data mining expert, chess enthusiast