Skip to contentSkip to conversation

ksmbd: fix DACL evaluation for denies and guest sessions

PATCH In review Ian Bridges 2 participants 7 messages Oct 30 – Nov 1

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

cover ksmbd: fix DACL evaluation for denies and guest sessions

On an acl_xattr share, smb_check_perm_dacl() has two evaluation errors. Patch 1 makes every open evaluate the DACL in order. Today a deny ACE that is the first one matching the caller grants every right it does not name. Patch 2 stops guest and anonymous sessions from matching Authenticated Users ACEs.

The two patches belong together. Each one alone moves some guest results the wrong way through the other error. Patch 1 alone lets a guest gather rights from Authenticated Users ACEs as it walks the DACL. Patch 2 alone can leave an Everyone deny as the first ACE a guest matches, and that deny then grants every right it does not name.

With both applied, every combination of one or two allow or deny ACEs for the caller, Everyone and Authenticated Users was tested, with READ_DATA, WRITE_DATA, WRITE_EA, APPEND_DATA or full access as the mask (930 DACLs). Each DACL also ended with an allow ACE giving the owner full access. The tests ran as an authenticated user, a guest and an anonymous session. Compared with the base, every result that changes agrees with ordered evaluation (MS-DTYP 2.5.3.2) and the definition of Authenticated Users (MS-DTYP 2.4.2.4), and no result that agreed changes.

Both patches carry Cc stable and need to be backported in this order.

Ian Bridges (2):
ksmbd: evaluate the DACL in order for requested access
ksmbd: exclude guest and anonymous sessions from Authenticated Users
ACEs

 fs/smb/server/mgmt/user_config.h   |  5 +++++
 fs/smb/server/smb2pdu.c            | 11 +++++++---
 fs/smb/server/smbacl.c             | 34 +++++++++++++-----------------
 fs/smb/server/smbacl.h             |  2 +-
 fs/smb/server/tests/smbacl_kunit.c |  4 ++--
 5 files changed, 31 insertions(+), 25 deletions(-)

base-commit: af8c72f6550744fe948ef2dbc7ab8739c149aab2

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 ksmbd: evaluate the DACL in order for requested access

On an acl_xattr share, smb_check_perm_dacl() evaluates the whole DACL in order only for a MAXIMUM_ALLOWED open. Any other open is checked against a single ACE, the first one whose SID matches the caller.

If that ACE is a deny, the caller gets every right the deny does not name. For example, a deny for writing grants read. Later ACEs are never checked, and an inherit-only ACE counts as if it applied to the file.

Evaluate the DACL in order (MS-DTYP 2.5.3.2) for every open, as the MAXIMUM_ALLOWED path already does. The extra rights that path gives the owner, and the execute right it adds, stay limited to MAXIMUM_ALLOWED.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

---

Notes:
Reachable on a share with "vfs objects = acl_xattr", for a file whose DACL has a deny ACE. An authenticated user reaches it through a deny that names them. A guest reaches it through a deny for Everyone on a share that allows guests.

Tested on ksmbd-for-next af8c72f65507. tester2's file has the DACL [DENY tester WRITE_DATA] [ALLOW tester2 FULL]. Before this change tester reads its contents over SMB. After it, the open returns STATUS_ACCESS_DENIED. Every combination of one or two allow or deny ACEs for the caller, Everyone and Authenticated Users was tested, with READ_DATA, WRITE_DATA, WRITE_EA, APPEND_DATA or full access as the mask (930 DACLs). Each DACL also ended with an allow ACE giving the owner full access. The DACLs were set on a file and probed with real reads and writes. Every result that changes for an authenticated user moves to what ordered evaluation gives, and none moves away. smbtorture smb2.acls and eight related suites match the base results, apart from smb2.create mkdir-dup, which also fails intermittently on base.

The ordered walk this reuses came with cc2f133e80eb ("ksmbd: fix maximum allowed access checks"), so a stable tree without it needs that commit first.

A reproducer is available on request.

This fix was developed with LLM assistance.

 fs/smb/server/smbacl.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
fs/smb/server/smbacl.c +10 -16
index 664f01263611..3542ae9a605c 100644
@@ -1481,6 +1481,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 	unsigned int sid_type = SIDOWNER;
 	unsigned short ace_size;
 	bool is_owner, owner_rights = false;
+	bool maximal = *pdaccess & FILE_MAXIMAL_ACCESS_LE;
 	vfsuid_t vfsuid;
 
 	ksmbd_debug(SMB, "check permission using windows acl\n");
@@ -1523,7 +1524,11 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 		goto err_out;
 	}
 
-	if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
+	/*
+	 * Evaluate the DACL in order (MS-DTYP 2.5.3.2) for every request, so a
+	 * deny ACE only denies and a later allow ACE still counts.
+	 */
+	{
 		ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
 		aces_size = pdacl_size - sizeof(struct smb_acl);
 		for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) {
@@ -1570,15 +1575,16 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 next_ace:
 			ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
 		}
-		if (is_owner && !owner_rights)
+		if (maximal && is_owner && !owner_rights)
 			access_bits |= READ_CONTROL | WRITE_DAC |
 				FILE_READ_ATTRIBUTES | DELETE;
 		access_bits &= ~denied;
-		if ((raw_daccess & FILE_GENERIC_EXECUTE_LE) &&
+		if (maximal && (raw_daccess & FILE_GENERIC_EXECUTE_LE) &&
 		    S_ISREG(d_inode(path->dentry)->i_mode) &&
 		    (access_bits & GENERIC_READ_FLAGS) == GENERIC_READ_FLAGS)
 			access_bits |= FILE_EXECUTE;
-		granted = requested | access_bits;
+		if (maximal)
+			granted = requested | access_bits;
 	}
 
 	ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
@@ -1651,18 +1657,6 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 		}
 	}
 
-	if (!(*pdaccess & FILE_MAXIMAL_ACCESS_LE)) {
-		switch (ace->type) {
-		case ACCESS_ALLOWED_ACE_TYPE:
-			access_bits = le32_to_cpu(ace->access_req);
-			break;
-		case ACCESS_DENIED_ACE_TYPE:
-		case ACCESS_DENIED_CALLBACK_ACE_TYPE:
-			access_bits = le32_to_cpu(~ace->access_req);
-			break;
-		}
-	}
-
 check_access_bits:
 	if (strict) {
 		access_bits &= granted;
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH 1/2] ksmbd: evaluate the DACL in order for requested access

Quoted 9 lines · Ian Bridges

On an acl_xattr share, smb_check_perm_dacl() evaluates the whole DACL in order only for a MAXIMUM_ALLOWED open. Any other open is checked against a single ACE, the first one whose SID matches the caller.

If that ACE is a deny, the caller gets every right the deny does not name. For example, a deny for writing grants read. Later ACEs are never checked, and an inherit-only ACE counts as if it applied to the file.

Quoted 7 lines

Evaluate the DACL in order (MS-DTYP 2.5.3.2) for every open, as the MAXIMUM_ALLOWED path already does. The extra rights that path gives the owner, and the execute right it adds, stay limited to MAXIMUM_ALLOWED.

Fixes e2f34481b24d ("cifsd: add server-side procedures for SMB3")

Can you check Sashiko's review comments ?

https://sashiko.dev/#/patchset/20261030020523.302085-1-icb%40fastmail.org

Thanks!

#3 lore raw

PATCH 2/2 ksmbd: exclude guest and anonymous sessions from Authenticated Users ACEs

In reply to Ian Bridges

smb_check_perm_dacl() matches an Authenticated Users ACE (S-1-5-11) for every caller, guest and anonymous sessions included. MS-DTYP 2.4.2.4 says users authenticated as Guest or Anonymous are not members of that group. As a result, on a share with guest access a client without credentials passes the DACL check with every right the DACL gives Authenticated Users. For example, it can read a file that only Authenticated Users may read. Access-based enumeration also lists that file to it.

Tell smb_check_perm_dacl() whether the session authenticated. A guest or anonymous session then no longer matches an Authenticated Users ACE but still matches an Everyone ACE. An authenticated user matches both.

Fixes cc2f133e80eb ("ksmbd: fix maximum allowed access checks")

---

Notes:
Reachable on a share with "vfs objects = acl_xattr" and "guest ok = yes", for files whose DACL grants Authenticated Users. That is enough for an anonymous session. A guest session for an unknown user also needs "map to guest = bad user". These settings are off by default.

The match came with cc2f133e80eb, first released in v7.3.

Tested on ksmbd-for-next af8c72f65507 with patch 1 applied, before and after this one. Every combination of one or two allow or deny ACEs for the caller, Everyone and Authenticated Users was tested, with READ_DATA, WRITE_DATA, WRITE_EA, APPEND_DATA or full access as the mask (930 DACLs). Each DACL also ended with an allow ACE giving the owner full access. The DACLs were set on a file and probed with real reads and writes as an authenticated user, a guest and an anonymous session. The authenticated user's results are unchanged. Every guest and anonymous change agrees with MS-DTYP 2.5.3.2 and 2.4.2.4, and none moves away. A guest or anonymous session no longer reads or lists a file only Authenticated Users may read, and still reads and lists one Everyone may read. smbtorture smb2.acls and eight related suites match the results with patch 1 alone, apart from smb2.create mkdir-dup, which also fails intermittently there.

A reproducer is available on request.

This fix was developed with LLM assistance.

 fs/smb/server/mgmt/user_config.h   |  5 +++++
 fs/smb/server/smb2pdu.c            | 11 ++++++++---
 fs/smb/server/smbacl.c             |  8 +++++---
 fs/smb/server/smbacl.h             |  2 +-
 fs/smb/server/tests/smbacl_kunit.c |  4 ++--
 5 files changed, 21 insertions(+), 9 deletions(-)
fs/smb/server/mgmt/user_config.h +5 -0
index cc460b4ff7d3..43c55357b95b 100644
@@ -66,5 +66,10 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp,
 		struct ksmbd_login_response_ext *resp_ext);
 void ksmbd_free_user(struct ksmbd_user *user);
 bool ksmbd_anonymous_user(struct ksmbd_user *user);
+
+static inline bool ksmbd_user_authenticated(struct ksmbd_user *user)
+{
+	return user && !user_guest(user) && !ksmbd_anonymous_user(user);
+}
 bool ksmbd_compare_user(struct ksmbd_user *u1, struct ksmbd_user *u2);
 #endif /* __USER_CONFIG_MANAGEMENT_H__ */
fs/smb/server/smb2pdu.c +8 -3
index 2e955d272f4b..8c7aba240e71 100644
@@ -4330,7 +4330,8 @@ static int smb2_handle_app_instance_id(struct ksmbd_work *work,
 	/* Authorize the referenced open even if its name changes meanwhile. */
 	old_path = &old_fp->filp->f_path;
 	rc = smb_check_perm_dacl(conn, old_path, &maximal_access, 0,
-				 work->sess->user->uid, false);
+				 work->sess->user->uid,
+				 ksmbd_user_authenticated(work->sess->user), false);
 	if (rc) {
 		/* Access denial prevents takeover, not the CREATE itself. */
 		ksmbd_put_durable_fd(old_fp);
@@ -4851,14 +4852,17 @@ int smb2_open(struct ksmbd_work *work)
 	if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
 		rc = smb_check_perm_dacl(conn, &path, &daccess,
 					 req->DesiredAccess,
-					 sess->user->uid, false);
+					 sess->user->uid,
+					 ksmbd_user_authenticated(sess->user), false);
 		if (rc)
 			goto err_out;
 
 		if (maximal_access_ctxt) {
 			maximal_access = FILE_MAXIMAL_ACCESS_LE;
 			rc = smb_check_perm_dacl(conn, &path, &maximal_access,
-						 0, sess->user->uid, false);
+						 0, sess->user->uid,
+						 ksmbd_user_authenticated(sess->user),
+						 false);
 			if (rc)
 				goto err_out;
 
@@ -6326,6 +6330,7 @@ static int process_query_dir_entries(struct smb2_query_dir_private *priv)
 			rc = smb_check_perm_dacl(priv->work->conn, &path,
 						 &daccess, daccess,
 						 priv->work->sess->user->uid,
+						 ksmbd_user_authenticated(priv->work->sess->user),
 						 true);
 			if (rc) {
 				dput(dent);
fs/smb/server/smbacl.c +5 -3
index 3542ae9a605c..b83aa84815ce 100644
@@ -1461,7 +1461,7 @@ bool smb_inherit_flags(int flags, bool is_dir)
 
 int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 			__le32 *pdaccess, __le32 raw_daccess, int uid,
-			bool strict)
+			bool authenticated, bool strict)
 {
 	const struct mnt_idmap *idmap = mnt_idmap(path->mnt);
 	struct smb_ntsd *pntsd = NULL;
@@ -1558,7 +1558,8 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 			    (compare_sids(&sid, &ace->sid) &&
 			     compare_sids(&sid_unix_NFS_mode, &ace->sid) &&
 			     compare_sids(&sid_everyone, &ace->sid) &&
-			     compare_sids(&sid_authusers, &ace->sid) &&
+			     (!authenticated ||
+			      compare_sids(&sid_authusers, &ace->sid)) &&
 			     compare_sids(&sid_owner_rights, &ace->sid)))
 				goto next_ace;
 
@@ -1611,7 +1612,8 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 			break;
 		}
 		if (!compare_sids(&sid_everyone, &ace->sid) ||
-		    !compare_sids(&sid_authusers, &ace->sid))
+		    (authenticated &&
+		     !compare_sids(&sid_authusers, &ace->sid)))
 			others_ace = ace;
 
 		ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
fs/smb/server/smbacl.h +1 -1
index 3342cebab333..bbd35948a379 100644
@@ -97,7 +97,7 @@ int smb_inherit_dacl(struct ksmbd_conn *conn, const struct path *path,
 		     unsigned int uid, unsigned int gid);
 int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
 			__le32 *pdaccess, __le32 raw_daccess, int uid,
-			bool strict);
+			bool authenticated, bool strict);
 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
 		 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
 		 bool type_check, bool get_write, bool posix_open,
fs/smb/server/tests/smbacl_kunit.c +2 -2
index 33496b4d31a3..bf28439a7696 100644
@@ -229,7 +229,7 @@ static void ksmbd_smb_check_perm_dacl_boundary_test(struct kunit *test)
 		goto out;
 
 	rc = smb_check_perm_dacl(NULL, &file->f_path, &daccess,
-				 cpu_to_le32(FILE_READ_DATA), 0, false);
+				 cpu_to_le32(FILE_READ_DATA), 0, true, false);
 
 	/*
 	 * The post-boundary ACE (ACE #2, beyond pdacl->size) grants
@@ -271,7 +271,7 @@ ksmbd_smb_check_perm_dacl_maximal_boundary_test(struct kunit *test)
 		goto out;
 
 	rc = smb_check_perm_dacl(NULL, &file->f_path, &daccess,
-				 FILE_MAXIMAL_ACCESS_LE, 0, false);
+				 FILE_MAXIMAL_ACCESS_LE, 0, true, false);
 	KUNIT_EXPECT_EQ(test, 0, rc);
 	if (rc)
 		goto out;
Signature · 1 line
2.47.3
maintainer
#4 lore raw

Re: [PATCH 2/2] ksmbd: exclude guest and anonymous sessions from Authenticated Users ACEs

fs/smb/server/smbacl.c int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, · line 1617

Quoted 8 lines
fs/smb/server/smbacl.c +2 -1
@@ -1611,7 +1612,8 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
                        break;
                }
                if (!compare_sids(&sid_everyone, &ace->sid) ||
-                   !compare_sids(&sid_authusers, &ace->sid))
+                   (authenticated &&
+                    !compare_sids(&sid_authusers, &ace->sid)))
                        others_ace = ace;

Skipping this ACE for a guest can leave both found and others_ace unset. The POSIX ACL fallback then runs. That code compares ACL_GROUP gids with the caller's uid instead of checking group membership. For example, consider a guest with uid 1000 who does not belong to group 1000. The file's DACL contains:

DENY Authenticated Users READ_DATA
ALLOW S-1-22-2-1000 read access
ALLOW owner full access

With S-1-22-2-1000 also set as the descriptor's Group SID, DACL conversion creates a POSIX group:1000:r-- entry. Before this patch, ABE hides the file. After this patch, the fallback mistakes group 1000 for user 1000 and lists the file, although the guest still fails the POSIX read permission check. Please also fix the fallback to check actual group membership for ACL_GROUP entries. The UID/GID comparison bug already exists, but this change exposes it for the DACL above.

maintainer
#5 lore raw
In reply to Ian Bridges
Quoted 23 lines · Ian Bridges

On an acl_xattr share, smb_check_perm_dacl() has two evaluation errors. Patch 1 makes every open evaluate the DACL in order. Today a deny ACE that is the first one matching the caller grants every right it does not name. Patch 2 stops guest and anonymous sessions from matching Authenticated Users ACEs.

The two patches belong together. Each one alone moves some guest results the wrong way through the other error. Patch 1 alone lets a guest gather rights from Authenticated Users ACEs as it walks the DACL. Patch 2 alone can leave an Everyone deny as the first ACE a guest matches, and that deny then grants every right it does not name.

With both applied, every combination of one or two allow or deny ACEs for the caller, Everyone and Authenticated Users was tested, with READ_DATA, WRITE_DATA, WRITE_EA, APPEND_DATA or full access as the mask (930 DACLs). Each DACL also ended with an allow ACE giving the owner full access. The tests ran as an authenticated user, a guest and an anonymous session. Compared with the base, every result that changes agrees with ordered evaluation (MS-DTYP 2.5.3.2) and the definition of Authenticated Users (MS-DTYP 2.4.2.4), and no result that agreed changes.

Quoted 6 lines

Both patches carry Cc stable and need to be backported in this order.

Ian Bridges (2):
ksmbd: evaluate the DACL in order for requested access
ksmbd: exclude guest and anonymous sessions from Authenticated Users
ACEs

This patch conflicts with your "[PATCH v2] ksmbd: enforce the DACL permission check on delete-on-close opens" patch when applied. Can you check it?

#6 lore raw

Hi Namjae,

Please drop this series.

Your point on 2/2 was right. Working through it and Sashiko's comments showed that the series depends on other issues that need to be fixed first.

I will send patches for those issues, then resend fixes for these bugs once those patches are merged.

Thanks for the review,
Ian