1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 * Copyright (c) International Business Machines Corp., 2007
4 * Author(s): Steve French (sfrench@us.ibm.com)
5 * Modified by Namjae Jeon (linkinjeon@kernel.org)
12 #include <linux/namei.h>
13 #include <linux/posix_acl.h>
14 #include <linux/mnt_idmapping.h>
16 #include "mgmt/tree_connect.h"
18 #define NUM_AUTHS (6) /* number of authority fields */
19 #define SID_MAX_SUB_AUTHORITIES (15) /* max number of sub authority fields */
22 * ACE types - see MS-DTYP 2.4.4.1
44 /* Revision for ACLs */
47 /* Control flags for Security Descriptor */
48 #define OWNER_DEFAULTED 0x0001
49 #define GROUP_DEFAULTED 0x0002
50 #define DACL_PRESENT 0x0004
51 #define DACL_DEFAULTED 0x0008
52 #define SACL_PRESENT 0x0010
53 #define SACL_DEFAULTED 0x0020
54 #define DACL_TRUSTED 0x0040
55 #define SERVER_SECURITY 0x0080
56 #define DACL_AUTO_INHERIT_REQ 0x0100
57 #define SACL_AUTO_INHERIT_REQ 0x0200
58 #define DACL_AUTO_INHERITED 0x0400
59 #define SACL_AUTO_INHERITED 0x0800
60 #define DACL_PROTECTED 0x1000
61 #define SACL_PROTECTED 0x2000
62 #define RM_CONTROL_VALID 0x4000
63 #define SELF_RELATIVE 0x8000
65 /* ACE types - see MS-DTYP 2.4.4.1 */
66 #define ACCESS_ALLOWED_ACE_TYPE 0x00
67 #define ACCESS_DENIED_ACE_TYPE 0x01
68 #define SYSTEM_AUDIT_ACE_TYPE 0x02
69 #define SYSTEM_ALARM_ACE_TYPE 0x03
70 #define ACCESS_ALLOWED_COMPOUND_ACE_TYPE 0x04
71 #define ACCESS_ALLOWED_OBJECT_ACE_TYPE 0x05
72 #define ACCESS_DENIED_OBJECT_ACE_TYPE 0x06
73 #define SYSTEM_AUDIT_OBJECT_ACE_TYPE 0x07
74 #define SYSTEM_ALARM_OBJECT_ACE_TYPE 0x08
75 #define ACCESS_ALLOWED_CALLBACK_ACE_TYPE 0x09
76 #define ACCESS_DENIED_CALLBACK_ACE_TYPE 0x0A
77 #define ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE 0x0B
78 #define ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE 0x0C
79 #define SYSTEM_AUDIT_CALLBACK_ACE_TYPE 0x0D
80 #define SYSTEM_ALARM_CALLBACK_ACE_TYPE 0x0E /* Reserved */
81 #define SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE 0x0F
82 #define SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE 0x10 /* reserved */
83 #define SYSTEM_MANDATORY_LABEL_ACE_TYPE 0x11
84 #define SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE 0x12
85 #define SYSTEM_SCOPED_POLICY_ID_ACE_TYPE 0x13
88 #define OBJECT_INHERIT_ACE 0x01
89 #define CONTAINER_INHERIT_ACE 0x02
90 #define NO_PROPAGATE_INHERIT_ACE 0x04
91 #define INHERIT_ONLY_ACE 0x08
92 #define INHERITED_ACE 0x10
93 #define SUCCESSFUL_ACCESS_ACE_FLAG 0x40
94 #define FAILED_ACCESS_ACE_FLAG 0x80
97 * Maximum size of a string representation of a SID:
99 * The fields are unsigned values in decimal. So:
101 * u8: max 3 bytes in decimal
102 * u32: max 10 bytes in decimal
104 * "S-" + 3 bytes for version field + 15 for authority field + NULL terminator
106 * For authority field, max is when all 6 values are non-zero and it must be
107 * represented in hex. So "-0x" + 12 hex digits.
109 * Add 11 bytes for each subauthority field (10 bytes each + 1 for '-')
111 #define SID_STRING_BASE_SIZE (2 + 3 + 15 + 1)
112 #define SID_STRING_SUBAUTH_SIZE (11) /* size of a single subauth string */
114 #define DOMAIN_USER_RID_LE cpu_to_le32(513)
119 __le16 revision; /* revision level */
128 __u8 revision; /* revision level */
130 __u8 authority[NUM_AUTHS];
131 __le32 sub_auth[SID_MAX_SUB_AUTHORITIES]; /* sub_auth[num_subauth] */
134 /* size of a struct cifs_sid, sans sub_auth array */
135 #define CIFS_SID_BASE_SIZE (1 + 1 + NUM_AUTHS)
138 __le16 revision; /* revision level */
148 struct smb_sid sid; /* ie UUID of user or group who gets these perms */
156 struct posix_acl *cf_acls;
157 struct posix_acl *cf_dacls;
160 struct posix_ace_state {
165 struct posix_user_ace_state {
170 struct posix_ace_state perms;
173 struct posix_ace_state_array {
175 struct posix_user_ace_state aces[];
179 * while processing the nfsv4 ace, this maintains the partial permissions
183 struct posix_acl_state {
184 struct posix_ace_state owner;
185 struct posix_ace_state group;
186 struct posix_ace_state other;
187 struct posix_ace_state everyone;
188 struct posix_ace_state mask; /* deny unused in this case */
189 struct posix_ace_state_array *users;
190 struct posix_ace_state_array *groups;
193 int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
194 int acl_len, struct smb_fattr *fattr);
195 int build_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
196 struct smb_ntsd *ppntsd, int ppntsd_size, int addition_info,
197 __u32 *secdesclen, struct smb_fattr *fattr);
198 int init_acl_state(struct posix_acl_state *state, int cnt);
199 void free_acl_state(struct posix_acl_state *state);
200 void posix_state_to_acl(struct posix_acl_state *state,
201 struct posix_acl_entry *pace);
202 int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid);
203 bool smb_inherit_flags(int flags, bool is_dir);
204 int smb_inherit_dacl(struct ksmbd_conn *conn, const struct path *path,
205 unsigned int uid, unsigned int gid);
206 int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
207 __le32 *pdaccess, int uid);
208 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
209 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
210 bool type_check, bool get_write);
211 void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid);
212 void ksmbd_init_domain(u32 *sub_auth);
214 static inline uid_t posix_acl_uid_translate(struct user_namespace *mnt_userns,
215 struct posix_acl_entry *pace)
219 /* If this is an idmapped mount, apply the idmapping. */
220 vfsuid = make_vfsuid(mnt_userns, &init_user_ns, pace->e_uid);
222 /* Translate the kuid into a userspace id ksmbd would see. */
223 return from_kuid(&init_user_ns, vfsuid_into_kuid(vfsuid));
226 static inline gid_t posix_acl_gid_translate(struct user_namespace *mnt_userns,
227 struct posix_acl_entry *pace)
231 /* If this is an idmapped mount, apply the idmapping. */
232 vfsgid = make_vfsgid(mnt_userns, &init_user_ns, pace->e_gid);
234 /* Translate the kgid into a userspace id ksmbd would see. */
235 return from_kgid(&init_user_ns, vfsgid_into_kgid(vfsgid));
238 #endif /* _SMBACL_H */