ksmbd: map user and group ids to Windows SIDs through user space
Subsystems (3)
RFC PATCH 4/6 ksmbd: map user and group ids to Windows SIDs through user space
A 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
Hi Jan,
Quoted 3 lines · Jan Koester
+ * one) and ksmbd's. 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).
[...]
fs/smb/server/smbacl.c void ksmbd_gid_to_sid(kgid_t gid, uint sidtype, struct smb_sid *ssid) · line 389
Quoted 5 lines
fs/smb/server/smbacl.c +5
-0+ sids =kcalloc (max,sizeof (*sids),KSMBD_DEFAULT_GFP ); +if (!sids) +return ; +if (!mapped_uid_sid (user->kuid, &sids[n])) + n++;
If user space cannot answer at login (-EAGAIN), ksmbd_user_set_token() seems to leave that domain SID out of the token. It builds no token at all when the allocation fails. The comment says access is then denied. That holds for an allow ACE on that SID, but wouldn't a deny ACE on it stop applying? Take a DACL with a deny of WRITE_DATA for a mapped group, followed by an allow of full access for Everyone. Would a member whose group SID is missing get write access through the Everyone entry for the whole session? Could -EAGAIN and allocation failures be returned from the token build so the login fails, while -ENOENT, for an account with no mapping, stays as it is?
Thanks,
Ian