ksmbd: Windows SIDs for user and group ids
Subsystems (4)
cover ksmbd: Windows SIDs for user and group ids
Hi,
Windows names owners and ACL entries by SID; for a domain account that is its objectSid. ksmbd shows a file's owner, group and POSIX ACL entries as S-1-22 SIDs or as its server domain SID + uid, which a Windows client can't relate to the domain's accounts, and a SID a client sets for a domain account doesn't map back to an id at all. This series lets user space, which knows the directory, translate between ids and SIDs in both directions, for accounts that are logged in or not.
It applies on top of the UUID/GUID id RFC (uid_r v2, <[email protected]>) and the NTLM pass-through series (<[email protected]>); the netlink events it adds follow theirs. Patches 1-3 are independent of uid_r in substance; I can rebase them onto mainline to go on their own if that is preferred.
1/6 ksmbd ignored the owner and group of a descriptor without a DACL,
which is what Windows sends to take ownership (and smbcacls -C/-G), and still reported success.
2/6 After setting a POSIX ACL from a DACL, the following chmod used the
owning group's bits as the mask and left named entries without effect.
3/6 passes the domain of an NTLM login (NTLMSSP DomainName) to user
space in the formerly reserved room of the login request, so a user space serving several domains can tell DOMAIN\user from OTHER\user.
4/6 adds KSMBD_EVENT_SID_MAP_REQUEST/RESPONSE for a user space that sets
KSMBD_GLOBAL_FLAG_SID_MAP at startup: the SID of an id ksmbd shows, the id of a domain SID a client sets. Answers are cached (mappings 5 minutes, "none" 1 minute, "couldn't say" never). Showing a descriptor never fails on a missing mapping; setting one fails with STATUS_NO_LOGON_SERVERS, STATUS_INVALID_OWNER or STATUS_INVALID_PRIMARY_GROUP instead of being applied in part or ignored. The session user also matches ACEs for its groups' SIDs, like a Windows access token.
5/6 shows classic users as S-1-22-1-<uid> instead of the per-server
domain SID + uid, and still takes the old form.
6/6 documents it in Documentation/userspace-api/uid_r.rst.
The user space side is sharedd (FenrirOS' ksmbd user space) with adconnect for the directory lookups. Tested in a VM against a directory
server: owner and group of accounts that aren't logged in, ownership and group changes, DACL round trips, a session getting access only through a nested group, inherited CREATOR OWNER ACEs, and the directory going away (cached mappings stay, new ones fail cleanly).
Questions:
- Is a netlink round trip per uncached id acceptable here, or would you rather have user space push the mappings of a session's ids at login only? The latter can't cover owners that aren't logged in.
- compare_sids() matches a SID against any prefix of it; the token uses an exact comparison. Should the existing callers move to that too?
Thanks,
Jan
Jan Koester (6):
ksmbd: don't ignore the owner and group of a descriptor without a DACL
ksmbd: keep named ACL entries effective when a DACL sets a POSIX ACL
ksmbd: pass the domain of an NTLM login to user space
ksmbd: map user and group ids to Windows SIDs through user space
ksmbd: show classic users as S-1-22-1-<uid> on every server
Documentation: uid_r: describe ksmbd's SID mapping
Documentation/userspace-api/uid_r.rst | 18 +- fs/smb/server/Makefile | 2 +- fs/smb/server/idmap.c | 320 ++++++++++++++++++++++++++ fs/smb/server/idmap.h | 25 ++ fs/smb/server/ksmbd_netlink.h | 62 ++++- fs/smb/server/mgmt/user_config.c | 21 +- fs/smb/server/mgmt/user_config.h | 7 +- fs/smb/server/server.c | 2 + fs/smb/server/smb2pdu.c | 35 ++- fs/smb/server/smbacl.c | 256 ++++++++++++++++++--- fs/smb/server/smbacl.h | 18 +- fs/smb/server/transport_ipc.c | 72 +++++- fs/smb/server/transport_ipc.h | 5 +- 13 files changed, 793 insertions(+), 50 deletions(-) create mode 100644 fs/smb/server/idmap.c create mode 100644 fs/smb/server/idmap.h
base-commit: bdd1d550e3f518d445d199c8ee73dc962c5abab8
Signature · 1 line
2.56.0
RFC PATCH 2/6 ksmbd: keep named ACL entries effective when a DACL sets a POSIX ACL
parse_dacl() turns ACEs for other users into named POSIX ACL entries with an rwx mask, but leaves the group bits of fattr->cf_mode as the owning group's ACE made them. set_info_sec() sets the ACL first and then the mode, and on an inode with an extended ACL the mode's group bits are the mask: a DACL without an ACE for the owning group (as Windows writes them) left the mask at --- and every named entry without effect.
Put the mask into the mode's group bits when there are named entries.
--- fs/smb/server/smbacl.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/smb/server/smbacl.c +6 -0
@@ -680,6 +680,12 @@ static int parse_dacl(struct mnt_idmap *idmap,if (acl_state.users->n || acl_state.groups->n) { acl_state.mask.allow =0x07 ; +/* +* With named entries the mode's group bits are the ACL's mask: +* the chmod after setting the ACL mustn't mask them all off. +*/ + fattr->cf_mode = (fattr->cf_mode & ~0070 ) | + (acl_state.mask.allow <<3 );if (IS_ENABLED (CONFIG_FS_POSIX_ACL )) { fattr->cf_acls =
Signature · 1 line
2.56.0
RFC PATCH 1/6 ksmbd: don't ignore the owner and group of a descriptor without a DACL
In reply to Jan Koesterparse_sec_desc() returned before looking at the owner and group SIDs when the descriptor had no DACL_PRESENT, so an SMB2 SET_INFO with only OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION -- what Windows sends to take ownership, and smbcacls -C/-G -- did nothing and still reported success.
Map the owner and group in any case and parse the DACL only if there is one. set_info_sec() then changes the mode and the POSIX ACLs only for a descriptor with a DACL, and leaves the stored descriptor alone without
one: replacing it would drop its DACL, and the owner shown comes from the inode anyway. This also covers the SD buffer create context, so its type_check argument goes away.
--- fs/smb/server/smb2pdu.c | 4 ++-- fs/smb/server/smbacl.c | 39 ++++++++++++++++++++++++++------------- fs/smb/server/smbacl.h | 2 +- 3 files changed, 29 insertions(+), 16 deletions(-)
fs/smb/server/smb2pdu.c +2 -2
@@ -3661,7 +3661,7 @@ static int smb2_create_sd_buffer(struct ksmbd_work *work, sizeof(struct create_sd_buf_req))return -EINVAL ;return set_info_sec (work->conn, work->tcon, path, &sd_buf->ntsd, -le32_to_cpu (sd_buf->ccontext.DataLength), true, false); +le32_to_cpu (sd_buf->ccontext.DataLength), false); } static void ksmbd_acls_fattr(struct smb_fattr *fattr,
@@ -8535,7 +8535,7 @@ static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,return -EACCES ;return set_info_sec (fp->conn, fp->tcon, &fp->filp->f_path, pntsd, - buf_len, false, true); + buf_len, true); }/**
fs/smb/server/smbacl.c +26 -13
@@ -1017,12 +1017,14 @@ int parse_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd, le32_to_cpu(pntsd->sacloffset), dacloffset); pntsd_type =le16_to_cpu (pntsd->type); -if (!(pntsd_type &DACL_PRESENT )) { +/* +* Without a DACL only the owner and group change (Windows sends that +* to take ownership): don't return before them. +*/ +if (pntsd_type &DACL_PRESENT ) + pntsd->type =cpu_to_le16 (DACL_PRESENT ); +else ksmbd_debug (SMB ,"DACL_PRESENT in DACL type is not set\n" ); -return rc; - } - - pntsd->type =cpu_to_le16 (DACL_PRESENT );if (pntsd->osidoffset) {if (le32_to_cpu (pntsd->osidoffset) <sizeof (struct smb_ntsd ))
@@ -1060,6 +1062,9 @@ int parse_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd, } } +if (!(pntsd_type &DACL_PRESENT )) +return 0 ; +if ((pntsd_type & (DACL_AUTO_INHERITED |DACL_AUTO_INHERIT_REQ )) == (DACL_AUTO_INHERITED |DACL_AUTO_INHERIT_REQ )) pntsd->type |=cpu_to_le16 (DACL_AUTO_INHERITED );
@@ -1768,18 +1773,21 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,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 get_write) {int rc;struct smb_fattr fattr = {{0 }};struct inode *inode =d_inode (path->dentry);struct mnt_idmap *idmap =mnt_idmap (path->mnt);struct iattr newattrs; +bool dacl; fattr.cf_uid =INVALID_UID ; fattr.cf_gid =INVALID_GID ; fattr.cf_mode = inode->i_mode; +/* parse_sec_desc() rewrites type */ + dacl =le16_to_cpu (pntsd->type) &DACL_PRESENT ; rc =parse_sec_desc (idmap, pntsd, ntsd_len, &fattr);if (rc) goto out;
@@ -1793,12 +1801,14 @@ int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon, newattrs.ia_valid |=ATTR_GID ; newattrs.ia_gid = fattr.cf_gid; } - newattrs.ia_valid |=ATTR_MODE ; - newattrs.ia_mode = (inode->i_mode & ~0777 ) | (fattr.cf_mode &0777 ); - -ksmbd_vfs_remove_acl_xattrs (idmap, path); +/* only the owner and group without a DACL: mode and ACLs stay */ +if (dacl) { + newattrs.ia_valid |=ATTR_MODE ; + newattrs.ia_mode = (inode->i_mode & ~0777 ) | (fattr.cf_mode &0777 ); +ksmbd_vfs_remove_acl_xattrs (idmap, path); + }/* Update posix acls */ -if (IS_ENABLED (CONFIG_FS_POSIX_ACL ) && fattr.cf_dacls) { +if (IS_ENABLED (CONFIG_FS_POSIX_ACL ) && dacl && fattr.cf_dacls) { rc =set_posix_acl (idmap, path->dentry,ACL_TYPE_ACCESS , fattr.cf_acls);if (rc <0 )
@@ -1821,8 +1831,11 @@ int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,if (rc) goto out; -/* Check it only calling from SD BUFFER context */ -if (type_check && !(le16_to_cpu (pntsd->type) &DACL_PRESENT )) +/* +* The stored descriptor keeps its DACL when only the owner or group +* changed: the owner shown is the inode's anyway. +*/ +if (!dacl) goto out;if (test_share_config_flag (tcon->share_conf,KSMBD_SHARE_FLAG_ACL_XATTR )) {
fs/smb/server/smbacl.h +1 -1
@@ -99,7 +99,7 @@ int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, 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 get_write);void id_to_sid (unsigned int cid,uint sidtype,struct smb_sid *ssid);void ksmbd_uid_to_sid (kuid_t uid,uint sidtype,struct smb_sid *ssid);void ksmbd_gid_to_sid (kgid_t gid,uint sidtype,struct smb_sid *ssid);
Signature · 1 line
2.56.0
RFC PATCH 3/6 ksmbd: pass the domain of an NTLM login to user space
In reply to Jan KoesterThe login request carries only the user name, though NTLMSSP's AUTHENTICATE message names the domain too. A user space serving several domains (or local accounts next to a domain's) can't tell DOMAIN\user apart from OTHER\user, picks the first account of that name, and a session that the domain controller then authenticates as one domain's user runs with another account's ids.
Put the domain into the formerly reserved room of struct ksmbd_login_request, flagged with KSMBD_LOGIN_REQ_DOMAIN, so the structure keeps its size and older user space keeps working. Ask for the login response extension by the account name user space answered
with: that one is unambiguous, the client's may not be.
--- fs/smb/server/ksmbd_netlink.h | 12 +++++++++++- fs/smb/server/mgmt/user_config.c | 16 ++++++++++++---- fs/smb/server/mgmt/user_config.h | 2 +- fs/smb/server/smb2pdu.c | 20 ++++++++++++++++---- fs/smb/server/transport_ipc.c | 12 +++++++++++- fs/smb/server/transport_ipc.h | 2 +- 6 files changed, 52 insertions(+), 12 deletions(-)
fs/smb/server/ksmbd_netlink.h +11 -1
@@ -66,6 +66,7 @@#define KSMBD_GENL_VERSION 0x01#define KSMBD_REQ_MAX_ACCOUNT_NAME_SZ 48 +#define KSMBD_REQ_MAX_DOMAIN_NAME_SZ 48#define KSMBD_REQ_MAX_HASH_SZ 18#define KSMBD_REQ_MAX_SHARE_NAME 64
@@ -140,9 +141,18 @@ struct ksmbd_shutdown_request {struct ksmbd_login_request {__u32 handle;__s8 account[KSMBD_REQ_MAX_ACCOUNT_NAME_SZ ];/* user account name */ -__u32 reserved[16 ];/* Reserved room */ +__u32 flags;/* KSMBD_LOGIN_REQ_* */ +__s8 domain[KSMBD_REQ_MAX_DOMAIN_NAME_SZ ];/* with _DOMAIN */ +__u32 reserved[3 ];/* Reserved room */ }; +/* +* The domain the client named with the account (NTLMSSP DomainName): user +* space serving several domains can tell their accounts of the same name +* apart. Formerly reserved, so zero from older ksmbd. +*/ +#define KSMBD_LOGIN_REQ_DOMAIN BIT(0) +/* * IPC user login response. */
fs/smb/server/mgmt/user_config.c +12 -4
@@ -9,21 +9,29 @@#include "user_config.h" #include "../transport_ipc.h" -struct ksmbd_user *ksmbd_login_user (const char *account) +struct ksmbd_user *ksmbd_login_user (const char *account,const char *domain) {struct ksmbd_login_response *resp;struct ksmbd_login_response_ext *resp_ext =NULL ;struct ksmbd_user *user =NULL ; - resp =ksmbd_ipc_login_request (account); + resp =ksmbd_ipc_login_request (account, domain);if (!resp)return NULL ;if (!(resp->status &KSMBD_USER_FLAG_OK )) goto out; -if (resp->status &KSMBD_USER_FLAG_EXTENSION ) - resp_ext =ksmbd_ipc_login_request_ext (account); +/* +* The extension of the account user space found: the name it +* answered with is unambiguous, the one the client gave may not be. +*/ +if (resp->status &KSMBD_USER_FLAG_EXTENSION ) { + resp->account[KSMBD_REQ_MAX_ACCOUNT_NAME_SZ -1 ] ='\0' ; + resp_ext =ksmbd_ipc_login_request_ext (resp->account[0 ] ? + (const char *)resp->account : + account); + } user =ksmbd_alloc_user (resp, resp_ext);kvfree (resp_ext);
fs/smb/server/mgmt/user_config.h +1 -1
@@ -63,7 +63,7 @@ static inline unsigned int user_gid(struct ksmbd_user *user)return user->gid; } -struct ksmbd_user *ksmbd_login_user (const char *account); +struct ksmbd_user *ksmbd_login_user (const char *account,const char *domain);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);
fs/smb/server/smb2pdu.c +16 -4
@@ -2169,8 +2169,8 @@ static struct ksmbd_user *session_user(struct ksmbd_conn *conn, {struct authenticate_message *authblob;struct ksmbd_user *user; -char *name; -unsigned int name_off, name_len, secbuf_len; +char *name, *domain =NULL ; +unsigned int name_off, name_len, dom_off, dom_len, secbuf_len;if (conn->use_spnego && conn->mechToken) secbuf_len = conn->mechTokenLen;
@@ -2196,8 +2196,20 @@ static struct ksmbd_user *session_user(struct ksmbd_conn *conn,return NULL ; } -ksmbd_debug (SMB ,"session setup request for user %s\n" , name); - user =ksmbd_login_user (name); +/* DOMAIN\user: which of several domains' user of that name */ + dom_off =le32_to_cpu (authblob->DomainName.BufferOffset); + dom_len =le16_to_cpu (authblob->DomainName.Length); +if (dom_len && secbuf_len >= (u64 )dom_off + dom_len) { + domain =smb_strndup_from_utf16 ((const char *)authblob + dom_off, + dom_len, true, conn->local_nls); +if (IS_ERR (domain)) + domain =NULL ; + } + +ksmbd_debug (SMB ,"session setup request for user %s domain %s\n" , + name, domain ? domain :"" ); + user =ksmbd_login_user (name, domain); +kfree (domain);kfree (name);return user; }
fs/smb/server/transport_ipc.c +11 -1
@@ -648,7 +648,12 @@ static int ksmbd_ipc_heartbeat_request(void)return ret; } -struct ksmbd_login_response *ksmbd_ipc_login_request (const char *account) +/* +* @domain: the domain the client named (NULL or "": none); one too long to +* pass is left out, user space then decides by the account alone +*/ +struct ksmbd_login_response *ksmbd_ipc_login_request (const char *account, +const char *domain) {struct ksmbd_ipc_msg *msg;struct ksmbd_login_request *req;
@@ -665,6 +670,11 @@ struct ksmbd_login_response *ksmbd_ipc_login_request(const char *account) req = (struct ksmbd_login_request *)msg->payload; req->handle =ksmbd_acquire_id (&ipc_ida);strscpy (req->account, account,KSMBD_REQ_MAX_ACCOUNT_NAME_SZ ); +if (domain && domain[0 ] && +strlen (domain) <KSMBD_REQ_MAX_DOMAIN_NAME_SZ ) { + req->flags |=KSMBD_LOGIN_REQ_DOMAIN ; +strscpy (req->domain, domain,KSMBD_REQ_MAX_DOMAIN_NAME_SZ ); + } resp =ipc_msg_send_request (msg, req->handle);ipc_msg_handle_free (req->handle);
fs/smb/server/transport_ipc.h +1 -1
@@ -11,7 +11,7 @@#define KSMBD_IPC_MAX_PAYLOAD 4096struct ksmbd_login_response * -ksmbd_ipc_login_request (const char *account); +ksmbd_ipc_login_request (const char *account,const char *domain);struct ksmbd_login_response_ext *ksmbd_ipc_login_request_ext (const char *account);
Signature · 1 line
2.56.0
RFC PATCH 4/6 ksmbd: map user and group ids to Windows SIDs through user space
In reply to Jan KoesterA domain account has a SID of its own (its objectSid) that Windows clients use for owners and ACL entries, while files and credentials carry the account's uid or gid -- a classic id, a UUID or a GUID. ksmbd shows the ids as S-1-22 SIDs or its server domain SID + uid, which Windows can't relate to the domain's accounts, and SIDs a client sets for domain accounts don't map back at all.
Let user space translate in both directions. A user space that sets KSMBD_GLOBAL_FLAG_SID_MAP at startup gets KSMBD_EVENT_SID_MAP_REQUEST for the SID of an id ksmbd shows as owner, group or ACL entry, and for the id of a domain account SID (S-1-5-21-..., outside the server's own domain) a client sets -- for any account, logged in or not. Answers are cached: mappings for five minutes, "no mapping" for one minute; an answer user space couldn't give (its directory unreachable) is never cached. Ids without a mapping keep the SIDs ksmbd gave them so far.
Showing a descriptor never fails on a missing mapping. Setting one fails instead of being applied in part: STATUS_NO_LOGON_SERVERS when user space can't answer, STATUS_INVALID_OWNER or STATUS_INVALID_PRIMARY_GROUP for a domain SID that maps to no user or group, which used to be ignored silently. ACEs for groups become ACL_GROUP entries of the POSIX ACL.
A session matched only its user's own SID against the ACEs of a stored descriptor. Give the session user the SIDs of its groups too, mapped and unmapped, like a Windows access token, built at login, and compare SIDs exactly: compare_sids() accepts a prefix.
--- fs/smb/server/Makefile | 2 +- fs/smb/server/idmap.c | 320 +++++++++++++++++++++++++++++++ fs/smb/server/idmap.h | 25 +++ fs/smb/server/ksmbd_netlink.h | 50 +++++ fs/smb/server/mgmt/user_config.c | 5 + fs/smb/server/mgmt/user_config.h | 5 + fs/smb/server/server.c | 2 + fs/smb/server/smb2pdu.c | 11 +- fs/smb/server/smbacl.c | 190 +++++++++++++++++- fs/smb/server/smbacl.h | 16 +- fs/smb/server/transport_ipc.c | 60 ++++++ fs/smb/server/transport_ipc.h | 3 + 12 files changed, 672 insertions(+), 17 deletions(-) create mode 100644 fs/smb/server/idmap.c create mode 100644 fs/smb/server/idmap.h
fs/smb/server/Makefile +1 -1
@@ -8,7 +8,7 @@ ksmbd-y := unicode.o auth.o vfs.o vfs_cache.o server.o ndr.o \ misc.o oplock.o connection.o ksmbd_work.o crypto_ctx.o \ mgmt/ksmbd_ida.o mgmt/user_config.o mgmt/share_config.o \ mgmt/tree_connect.o mgmt/user_session.o smb_common.o \ - transport_tcp.o transport_ipc.o smbacl.o smb2pdu.o \ + transport_tcp.o transport_ipc.o smbacl.o idmap.o smb2pdu.o \ smb2ops.o smb2misc.o ksmbd_spnego_negtokeninit.asn1.o \ ksmbd_spnego_negtokentarg.asn1.o asn1.o compress.o
fs/smb/server/idmap.c new file +320 -0
320 lines not on this page · raw message
fs/smb/server/idmap.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* The Windows SIDs of user and group ids, as user space maps them +* (KSMBD_EVENT_SID_MAP_REQUEST), cached. +*/ + +#ifndef __KSMBD_IDMAP_H__ +#define __KSMBD_IDMAP_H__ + +#include <linux/types.h> +#include <linux/uidgid_r.h> + +struct smb_sid ; + +bool ksmbd_idmap_enabled (void ); +bool ksmbd_idmap_sid_mappable (const struct smb_sid *sid); +/* 0 and *sid, -ENOENT: no SID of its own, -EAGAIN: user space couldn't say */ +int ksmbd_idmap_id_to_sid (bool group,const struct __kernel_uid_r *id, +struct smb_sid *sid); +/* 0 and *group, *id, -ENOENT: no account's SID, -EAGAIN: as above */ +int ksmbd_idmap_sid_to_id (const struct smb_sid *sid,bool *group, +struct __kernel_uid_r *id); +void ksmbd_idmap_flush (void ); + +#endif /* __KSMBD_IDMAP_H__ */
fs/smb/server/ksmbd_netlink.h +50 -0
74 lines not on this page · raw message
fs/smb/server/mgmt/user_config.c +5 -0
@@ -8,6 +8,7 @@#include "user_config.h" #include "../transport_ipc.h" +#include "../smbacl.h" struct ksmbd_user *ksmbd_login_user (const char *account,const char *domain) {
@@ -73,6 +74,8 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp, user->ngroups =0 ; user->sgid =NULL ; + user->nsids =0 ; + user->sids =NULL ;if (!user->name || !user->passkey) goto err_free;
@@ -114,6 +117,7 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp,ksmbd_debug (SMB ,"supplementary groups : %d\n" , user->ngroups); } +ksmbd_user_set_token (user);return user; err_free:
@@ -127,6 +131,7 @@ struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp,void ksmbd_free_user (struct ksmbd_user *user) {ksmbd_ipc_logout_request (user->name, user->flags); +kfree (user->sids);kfree (user->sgid);kfree (user->name);kfree_sensitive (user->passkey);
fs/smb/server/mgmt/user_config.h +5 -0
@@ -8,6 +8,8 @@#include "../glob.h" +struct smb_sid ; +struct ksmbd_user {unsigned short flags;
@@ -22,6 +24,9 @@ struct ksmbd_user {char *passkey;int ngroups;kgid_t *sgid; + +int nsids;/* ksmbd_user_set_token() */ +struct smb_sid *sids; };static inline bool user_guest (struct ksmbd_user *user)
fs/smb/server/server.c +2 -0
@@ -14,6 +14,7 @@#include <linux/moduleparam.h> #include "server.h" +#include "idmap.h" #include "smb_common.h" #include "smb2pdu.h" #include "../common/smb2status.h"
@@ -746,6 +747,7 @@ static void __exit ksmbd_server_exit(void)*/ ksmbd_conn_wq_destroy ();ksmbd_release_inode_hash (); +ksmbd_idmap_flush (); }MODULE_AUTHOR ("Namjae Jeon <[email protected]>" );
fs/smb/server/smb2pdu.c +7 -4
@@ -4606,14 +4606,14 @@ 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->kuid, false); + 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->kuid, false); +0 , sess->user, false);if (rc) goto err_out;
@@ -4799,8 +4799,10 @@ int smb2_open(struct ksmbd_work *work)ksmbd_debug (SMB ,"inherit posix acl failed : %d\n" , posix_acl_rc); rc =smb2_create_sd_buffer (work, req, &path); -if (rc && rc != -ENOENT ) +if (rc && rc != -ENOENT ) { + rsp->hdr.Status =ksmbd_sd_err_status (rc); goto err_out; + }if (rc == -ENOENT ) { if (test_share_config_flag(work->tcon->share_conf,
@@ -5866,7 +5868,7 @@ static int process_query_dir_entries(struct smb2_query_dir_private *priv) path.dentry = dent; rc =smb_check_perm_dacl (priv->work->conn, &path, &daccess, daccess, - priv->work->sess->user->kuid, + priv->work->sess->user, true);if (rc) {dput (dent);
@@ -8622,6 +8624,7 @@ int smb2_set_info(struct ksmbd_work *work)le32_to_cpu (req->AdditionalInformation), (char *)req +le16_to_cpu (req->BufferOffset), le32_to_cpu(req->BufferLength)); + rsp->hdr.Status =ksmbd_sd_err_status (rc);break ;default : rc = -EOPNOTSUPP ;
fs/smb/server/smbacl.c +180 -10
265 lines not on this page · raw message
fs/smb/server/smbacl.h +14 -2
@@ -81,6 +81,16 @@ struct posix_acl_state {struct posix_ace_state_array *groups; }; +/* +* parse_sec_desc() / set_info_sec() errors about the SIDs of domain +* accounts (KSMBD_GLOBAL_FLAG_SID_MAP); ksmbd_sd_err_status() names them. +*/ +#define KSMBD_SD_EOWNER EBADE/* owner SID: no such user */ +#define KSMBD_SD_EGROUP EBADR/* group SID: no such group */ +#define KSMBD_SD_EUNAVAIL EHOSTDOWN/* user space couldn't map now */ + +__le32 ksmbd_sd_err_status (int rc); +int parse_sec_desc (struct mnt_idmap *idmap,struct smb_ntsd *pntsd,int acl_len,struct smb_fattr *fattr); int build_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd,
@@ -94,9 +104,11 @@ int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid);bool smb_inherit_flags (int flags,bool is_dir);int smb_inherit_dacl (struct ksmbd_conn *conn,const struct path *path,kuid_t uid,kgid_t gid); +struct ksmbd_user ;int smb_check_perm_dacl (struct ksmbd_conn *conn,const struct path *path, -__le32 *pdaccess,__le32 raw_daccess,kuid_t uid, -bool strict); +__le32 *pdaccess,__le32 raw_daccess, +struct ksmbd_user *user,bool strict); +void ksmbd_user_set_token (struct ksmbd_user *user);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 get_write);
fs/smb/server/transport_ipc.c +60 -0
96 lines not on this page · raw message
fs/smb/server/transport_ipc.h +3 -0
@@ -32,6 +32,9 @@ struct ksmbd_share_config_response * ksmbd_ipc_share_config_request(const char *name);struct ksmbd_spnego_authen_response *ksmbd_ipc_spnego_authen_request (const char *spnego_blob,int blob_len); +struct ksmbd_sid_map_response * +ksmbd_ipc_sid_map_request (unsigned int flags,const struct __kernel_uid_r *id, +const void *sid,size_t sid_len);struct ksmbd_ntlm_authen_response *ksmbd_ipc_ntlm_authen_request (const char *challenge,const char *parts[5 ],const unsigned short lens[5 ]);
Signature · 1 line
2.56.0
RFC PATCH 5/6 ksmbd: show classic users as S-1-22-1-<uid> on every server
In reply to Jan KoesterA classic uid appeared in security descriptors as the server's own domain SID + uid. That domain SID comes from user space and differs from server to server (ksmbd-tools and sharedd derive it per machine), so the same owner had a different SID on every file server, and the RID looked like a domain account's.
Show them as S-1-22-1-<uid>, the SID ksmbd already used for root and that S-1-22-2-<gid> pairs with for groups. Keep taking the old form when a client sets it, and keep it in the session's SIDs, so descriptors stored before still match.
--- fs/smb/server/smbacl.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
fs/smb/server/smbacl.c +21 -8
@@ -300,14 +300,16 @@ static u32 sid_to_id_r(const struct smb_sid *psid, u8 *id) }/* -* The SID ksmbd itself gives an id: a classic one the usual SIDs, a UUID -* or GUID S-1-22-<kind>-w0-w1-w2-w3. +* The SID ksmbd itself gives an id: a classic uid S-1-22-1-<uid>, the same +* on every server (it used to be the server's own domain SID + uid), a +* classic gid S-1-22-2-<gid>, a UUID or GUID S-1-22-<kind>-w0-w1-w2-w3. */ static void native_uid_sid (kuid_t uid,uint sidtype,struct smb_sid *ssid) { -if (uid_is_legacy (uid) || sidtype ==SIDCREATOR_OWNER || - sidtype ==SIDCREATOR_GROUP ) +if (sidtype ==SIDCREATOR_OWNER || sidtype ==SIDCREATOR_GROUP )id_to_sid (from_kuid_munged (&init_user_ns, uid), sidtype, ssid); +else if (uid_is_legacy (uid)) +id_to_sid (from_kuid_munged (&init_user_ns, uid),SIDUNIX_USER , ssid);else if (uid.type ==KID_UUID )id_r_to_sid (SID_UNIX_UUID_USER , uid.uuid.b, ssid);else
@@ -373,13 +375,14 @@ static bool sid_equal(const struct smb_sid *a, const struct smb_sid *b)/* * The SIDs a user's ACL entries may name, like a Windows access token: the * user's and its groups', each the account's own SID (if user space maps -* one) and ksmbd's. Built at login; a +* one) and ksmbd's -- for a classic uid also the server's domain SID + uid +* of older ksmbd, which stored descriptors may hold. Built at login; a * mapping user space couldn't give then is missing for the session (its * ACL entries don't match: access is denied, not granted). */ void ksmbd_user_set_token (struct ksmbd_user *user) { -int i, n =0 , max =2 +2 * (user->ngroups +1 ); +int i, n =0 , max =3 +2 * (user->ngroups +1 );struct smb_sid *sids; sids =kcalloc (max,sizeof (*sids),KSMBD_DEFAULT_GFP );
@@ -388,6 +391,9 @@ void ksmbd_user_set_token(struct ksmbd_user *user)if (!mapped_uid_sid (user->kuid, &sids[n])) n++;native_uid_sid (user->kuid,SIDOWNER , &sids[n++]); +if (uid_is_legacy (user->kuid) && !uid_eq (user->kuid,GLOBAL_ROOT_UID )) +id_to_sid (from_kuid_munged (&init_user_ns, user->kuid),SIDOWNER , + &sids[n++]);for (i =-1 ; i < user->ngroups; i++) {kgid_t gid = i <0 ? user->kgid : user->sgid[i];
@@ -519,8 +525,15 @@ static int sid_to_id(struct mnt_idmap *idmap,kuid_t uid;uid_t id; -/* Only the server domain RID has a local uid representation. */ - sid_prefix = &server_conf.domain_sid; +/* +* A local uid: S-1-22-1-<uid>, or the server's domain SID + +* uid that ksmbd used to show (stored descriptors have it). +*/ +if (psid->num_subauth == sid_unix_users.num_subauth +1 && + !compare_sids (psid, &sid_unix_users)) + sid_prefix = &sid_unix_users; +else + sid_prefix = &server_conf.domain_sid;if (psid->num_subauth != sid_prefix->num_subauth +1 ||compare_sids (psid, sid_prefix))return -EINVAL ;
Signature · 1 line
2.56.0
RFC PATCH 6/6 Documentation: uid_r: describe ksmbd's SID mapping
In reply to Jan KoesterName the SIDs ksmbd gives classic users and groups, and describe KSMBD_EVENT_SID_MAP_REQUEST: what user space is asked, the caching and what happens when it can't answer.
--- Documentation/userspace-api/uid_r.rst | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
Documentation/userspace-api/uid_r.rst +17 -1
@@ -167,7 +167,23 @@ SMB (ksmbd) groups appear in security descriptors as ``S-1-22-<kind>-w0-w1-w2-w3``: kind 3 and 4 for a UUID user and group, 5 and 6 for a GUID user and group, w0 to w3 the 16 bytes of the id as - big endian words. + big endian words; classic users as ``S-1-22-1-<uid>``, classic groups + as ``S-1-22-2-<gid>``. + + A user space that sets ``KSMBD_GLOBAL_FLAG_SID_MAP`` at startup is + asked (``KSMBD_EVENT_SID_MAP_REQUEST``) for the Windows SID of every id + ksmbd shows as owner, group or ACL entry and for the id of every + domain SID (``S-1-5-21-...``) a client sets, also for accounts that + are not logged in. A mapped account appears with its own SID (e.g. a + directory's ``objectSid``), others keep the SIDs above. ksmbd caches + mappings for five minutes and "no mapping" for one; an answer user + space couldn't give is not cached: descriptors then show the SIDs + above, and setting an owner, group or ACL that needs a mapping fails + with ``STATUS_NO_LOGON_SERVERS`` instead of being applied in part. A + domain SID that maps to no user (as owner) or group (as group) is + refused with ``STATUS_INVALID_OWNER`` or ``STATUS_INVALID_PRIMARY_GROUP`` + rather than ignored. A session's user matches ACL entries naming its + own or its groups' SIDs, mapped or not. NFSv4 Owners and ACL principals that are UUIDs or GUIDs travel as their text
Signature · 1 line
2.56.0
Namjae Jeon
maintainerQuoted 2 lines · Jan Koester
Hi,
Hi Jan,
Quoted 10 lines
Windows names owners and ACL entries by SID; for a domain account that is its objectSid. ksmbd shows a file's owner, group and POSIX ACL entries as S-1-22 SIDs or as its server domain SID + uid, which a Windows client can't relate to the domain's accounts, and a SID a client sets for a domain account doesn't map back to an id at all. This series lets user space, which knows the directory, translate between ids and SIDs in both directions, for accounts that are logged in or not.
It applies on top of the UUID/GUID id RFC (
uid_rv2,
How is the uid_r RFC going? This series is based on it, so I would like to know its current status and what you plan to do next with it.
Quoted 11 lines
The user space side is sharedd (FenrirOS' ksmbd user space) with adconnect for the directory lookups. Tested in a VM against a directory
server: owner and group of accounts that aren't logged in, ownership and group changes, DACL round trips, a session getting access only through a nested group, inherited CREATOR OWNER ACEs, and the directory going away (cached mappings stay, new ones fail cleanly).Questions:
- Is a netlink round trip per uncached id acceptable here, or would you rather have user space push the mappings of a session's ids at login only? The latter can't cover owners that aren't logged in.
On-demand lookup on a cache miss looks right to me. ksmbd kernel should cache the results as proposed and query userspace again only when an entry expires or is not cached. Loading all mappings up front does not scale well and is difficult to keep current.
Quoted 2 lines
-
compare_sids()matches a SID against any prefix of it; the token uses an exact comparison. Should the existing callers move to that too?
Please don't change the existing callers. Some of them rely on prefix matching on purpose. If exact comparison is needed, add an exact-match helper and convert only the relevant callers.
Thanks.