Linux NFS development
 help / color / mirror / Atom feed
From: Chuck Lever <cel@kernel.org>
To: <linux-nfs@vger.kernel.org>
Subject: [PATCH pynfs] server41tests: Test READ and WRITE alongside machine-credential cleanup
Date: Thu,  3 Dec 2026 10:16:41 -0500	[thread overview]
Message-ID: <20261203151641.406497-1-cel@kernel.org> (raw)

RFC 8881 Section 18.35.3 lets a client ID that uses SP4_MACH_CRED send
only the operations in spo_must_allow with the machine credential. A
server that looks only for one spo_must_allow operation in a COMPOUND
lets a READ or WRITE in the same COMPOUND bypass the export's sec=
list. No test covers that case.

Add MACHCRED1 through MACHCRED8. Each opens a file with krb5p on an
export that permits only krb5p. With the krb5i machine credential, each
then sends PUTFH, a READ or WRITE, and a CLOSE, OPEN_DOWNGRADE, LOCKU,
or DELEGRETURN, and expects NFS4ERR_WRONGSEC. PUTFH followed by the
spo_must_allow operation alone has to succeed. MACHCRED7 and MACHCRED8
report UNSUPPORTED when the server does not grant a delegation. Add a --krb5pdir
option to name the directory. The tests report UNSUPPORTED without
--krb5pdir or --security=krb5i.

Signed-off-by: Chuck Lever <cel@kernel.org>
---
 nfs4.1/server41tests/__init__.py     |   1 +
 nfs4.1/server41tests/st_mach_cred.py | 210 +++++++++++++++++++++++++++
 nfs4.1/testserver.py                 |  11 ++
 3 files changed, 222 insertions(+)
 create mode 100644 nfs4.1/server41tests/st_mach_cred.py

diff --git a/nfs4.1/server41tests/__init__.py b/nfs4.1/server41tests/__init__.py
index c654212c617e..9141c1ea1933 100644
--- a/nfs4.1/server41tests/__init__.py
+++ b/nfs4.1/server41tests/__init__.py
@@ -7,6 +7,7 @@ __all__ = ["st_exchange_id.py", # draft 21
            "st_reclaim_complete.py",
            "st_secinfo_no_name.py",
            "st_secinfo.py",
+           "st_mach_cred.py",
            "st_sequence.py",
            "st_trunking.py",
            "st_open.py",
diff --git a/nfs4.1/server41tests/st_mach_cred.py b/nfs4.1/server41tests/st_mach_cred.py
new file mode 100644
index 000000000000..6b97b79f7275
--- /dev/null
+++ b/nfs4.1/server41tests/st_mach_cred.py
@@ -0,0 +1,210 @@
+from xdrdef.nfs4_const import *
+from xdrdef.nfs4_type import state_protect4_a, state_protect_ops4
+from xdrdef.nfs4_type import locker4, lock_owner4, open_to_lock_owner4
+from .environment import check, open_create_file_op, use_obj
+from rpc.security import AuthGss
+from rpc.gss_const import rpc_gss_svc_integrity, rpc_gss_svc_privacy
+import rpc.rpc as rpc
+import nfs_ops
+op = nfs_ops.NFS4ops()
+
+def _privacy_cred(env):
+    """Return a krb5p credential for the principal behind env.cred1"""
+    c1 = env.c1
+    call = c1.make_call_function(c1.c1, 0, c1.default_prog, c1.default_vers)
+    cred = AuthGss().init_cred(call, target="nfs@%s" % env.opts.server)
+    cred.service = rpc_gss_svc_privacy
+    return cred
+
+def _mach_cred_session(t, env, allow):
+    """Create a session whose client ID uses SP4_MACH_CRED
+
+    The machine credential is env.cred1, so the test has to run
+    with --security=krb5i.
+    """
+    if env.opts.krb5pdir is None:
+        t.fail_support("Needs --krb5pdir")
+    if env.opts.flavor != rpc.RPCSEC_GSS or \
+       env.opts.service != rpc_gss_svc_integrity:
+        t.fail_support("Needs --security=krb5i")
+
+    protect = state_protect4_a(SP4_MACH_CRED,
+                               spa_mach_ops=state_protect_ops4(0, allow))
+    c = env.c1.new_client(env.testname(t), protect=protect)
+    if c.protect.type != SP4_MACH_CRED:
+        t.fail_support("Server does not support SP4_MACH_CRED")
+    sess = c.create_session()
+    sess.compound([op.reclaim_complete(FALSE)])
+    return sess
+
+def _close(stateid):
+    return op.close(0, stateid)
+
+def _open_downgrade(stateid):
+    return op.open_downgrade(stateid, 0, OPEN4_SHARE_ACCESS_READ,
+                             OPEN4_SHARE_DENY_NONE)
+
+def _locku(stateid):
+    return op.locku(WRITE_LT, 0, stateid, 0, NFS4_UINT64_MAX)
+
+def _delegreturn(stateid):
+    return op.delegreturn(stateid)
+
+def _got_deleg(deleg):
+    return deleg.delegation_type not in (OPEN_DELEGATE_NONE,
+                                         OPEN_DELEGATE_NONE_EXT)
+
+def _io_with_mach_op(t, env, io_op, io_name, mach_op=_close, mach_name="CLOSE"):
+    """Send PUTFH, io_op(stateid), mach_op(stateid) with the machine
+    credential
+
+    The client ID uses SP4_MACH_CRED with CLOSE, OPEN_DOWNGRADE, LOCKU,
+    and DELEGRETURN in spo_must_allow. The file is on an export that
+    permits only krb5p, and the machine credential is krb5i. RFC 8881
+    Section 18.35.3 lets the machine credential send those operations
+    but not READ or WRITE, so PUTFH has to return
+    NFS4ERR_WRONGSEC. PUTFH, mach_op(stateid) alone has to succeed.
+    For LOCKU, a LOCK sent with krb5p supplies the lock stateid. For
+    DELEGRETURN, the krb5p OPEN asks for a delegation.
+    """
+    sess = _mach_cred_session(t, env,
+                              (1 << OP_CLOSE) | (1 << OP_OPEN_DOWNGRADE) |
+                              (1 << OP_LOCKU) | (1 << OP_DELEGRETURN))
+    cred_p = _privacy_cred(env)
+
+    name = env.testname(t)
+    owner = b"owner_%s" % name
+    path = env.opts.krb5pdir + [name]
+    access = OPEN4_SHARE_ACCESS_BOTH
+    if mach_op is _delegreturn:
+        access |= OPEN4_SHARE_ACCESS_WANT_ANY_DELEG
+    ops = open_create_file_op(sess, owner, path, access=access,
+                              open_create=OPEN4_CREATE)
+    res = sess.compound(ops, credinfo=cred_p)
+    check(res, msg="OPEN of a file in --krb5pdir with krb5p")
+    fh = res.resarray[-1].object
+    stateid = res.resarray[-2].stateid
+
+    mach_stateid = stateid
+    if mach_op is _delegreturn:
+        deleg = res.resarray[-2].delegation
+        if not _got_deleg(deleg):
+            # The server may not have probed the callback channel yet.
+            ops = open_create_file_op(sess, owner, path, access=access)
+            res = sess.compound(ops, credinfo=cred_p)
+            check(res, msg="Second OPEN of the test file with krb5p")
+            stateid = res.resarray[-2].stateid
+            deleg = res.resarray[-2].delegation
+        if deleg.delegation_type == OPEN_DELEGATE_WRITE:
+            mach_stateid = deleg.write.stateid
+        elif deleg.delegation_type == OPEN_DELEGATE_READ:
+            mach_stateid = deleg.read.stateid
+        else:
+            t.fail_support("Server did not grant a delegation")
+    elif mach_op is _locku:
+        locker = locker4(open_owner=open_to_lock_owner4(0, stateid, 0,
+                                        lock_owner4(0, b"lock_%s" % name)),
+                         new_lock_owner=True)
+        res = sess.compound([op.putfh(fh),
+                             op.lock(WRITE_LT, False, 0, NFS4_UINT64_MAX,
+                                     locker)],
+                            credinfo=cred_p)
+        check(res, msg="LOCK of the test file with krb5p")
+        mach_stateid = res.resarray[-1].lock_stateid
+
+    res = sess.compound([op.putfh(fh), io_op(stateid), mach_op(mach_stateid)])
+    check(res, NFS4ERR_WRONGSEC,
+          msg="PUTFH, %s, %s with the krb5i machine credential" %
+          (io_name, mach_name))
+
+    res = sess.compound([op.putfh(fh), mach_op(mach_stateid)])
+    check(res, msg="PUTFH, %s with the krb5i machine credential" % mach_name)
+
+    if mach_op is not _close:
+        if mach_op is _open_downgrade:
+            stateid = res.resarray[-1].open_stateid
+        res = sess.compound([op.putfh(fh), _close(stateid)])
+        check(res, msg="PUTFH, CLOSE with the krb5i machine credential")
+
+    res = sess.compound(use_obj(env.opts.krb5pdir) + [op.remove(name)],
+                        credinfo=cred_p)
+    check(res, msg="REMOVE of the test file with krb5p")
+
+def testWriteClose(t, env):
+    """A spo_must_allow CLOSE does not exempt a WRITE in its COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED1
+    """
+    _io_with_mach_op(t, env,
+              lambda stateid: op.write(stateid, 0, FILE_SYNC4, b"machcred"),
+              "WRITE")
+
+def testReadClose(t, env):
+    """A spo_must_allow CLOSE does not exempt a READ in its COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED2
+    """
+    _io_with_mach_op(t, env, lambda stateid: op.read(stateid, 0, 4096), "READ")
+
+def testWriteOpenDowngrade(t, env):
+    """A spo_must_allow OPEN_DOWNGRADE does not exempt a WRITE in its
+    COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED3
+    """
+    _io_with_mach_op(t, env,
+              lambda stateid: op.write(stateid, 0, FILE_SYNC4, b"machcred"),
+              "WRITE", _open_downgrade, "OPEN_DOWNGRADE")
+
+def testReadOpenDowngrade(t, env):
+    """A spo_must_allow OPEN_DOWNGRADE does not exempt a READ in its
+    COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED4
+    """
+    _io_with_mach_op(t, env, lambda stateid: op.read(stateid, 0, 4096),
+                     "READ", _open_downgrade, "OPEN_DOWNGRADE")
+
+def testWriteLocku(t, env):
+    """A spo_must_allow LOCKU does not exempt a WRITE in its COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED5
+    """
+    _io_with_mach_op(t, env,
+              lambda stateid: op.write(stateid, 0, FILE_SYNC4, b"machcred"),
+              "WRITE", _locku, "LOCKU")
+
+def testReadLocku(t, env):
+    """A spo_must_allow LOCKU does not exempt a READ in its COMPOUND
+
+    FLAGS: all
+    CODE: MACHCRED6
+    """
+    _io_with_mach_op(t, env, lambda stateid: op.read(stateid, 0, 4096),
+                     "READ", _locku, "LOCKU")
+
+def testWriteDelegreturn(t, env):
+    """A spo_must_allow DELEGRETURN does not exempt a WRITE in its
+    COMPOUND
+
+    FLAGS: deleg all
+    CODE: MACHCRED7
+    """
+    _io_with_mach_op(t, env,
+              lambda stateid: op.write(stateid, 0, FILE_SYNC4, b"machcred"),
+              "WRITE", _delegreturn, "DELEGRETURN")
+
+def testReadDelegreturn(t, env):
+    """A spo_must_allow DELEGRETURN does not exempt a READ in its
+    COMPOUND
+
+    FLAGS: deleg all
+    CODE: MACHCRED8
+    """
+    _io_with_mach_op(t, env, lambda stateid: op.read(stateid, 0, 4096),
+                     "READ", _delegreturn, "DELEGRETURN")
diff --git a/nfs4.1/testserver.py b/nfs4.1/testserver.py
index f38279b36cba..a769fe360547 100755
--- a/nfs4.1/testserver.py
+++ b/nfs4.1/testserver.py
@@ -151,6 +151,9 @@ def scan_options(p):
                  help="Use SERVER:/DIRPATH for ROFS tests")
     g.add_option("--usefh", default=None, metavar="FH",
                  help="Use FH for certain specialized tests")
+    g.add_option("--krb5pdir", default=None, metavar="DIRPATH",
+                 help="Use SERVER:/DIRPATH, on an export that permits "
+                 "only sec=krb5p, for the SP4_MACH_CRED tests")
     p.add_option_group(g)
 
     g = OptionGroup(p, "Inter-server copy options",
@@ -296,6 +299,14 @@ def main():
                     p.error(e)
             setattr(opt, attr, [comp for comp in path if comp])
 
+    # --krb5pdir has no default, so the tests that need it can skip
+    if opt.krb5pdir is not None:
+        try:
+            path = nfs4lib.path_components(os.fsencode(opt.krb5pdir))
+        except Exception as e:
+            p.error(e)
+        opt.krb5pdir = [comp for comp in path if comp]
+
     # Check that --security option is valid
     # FIXME STUB
     tempd = {'none' : (rpc.AUTH_NONE, 0),
-- 
2.56.0


                 reply	other threads:[~2026-12-03 15:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203151641.406497-1-cel@kernel.org \
    --to=cel@kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox