Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1+ |
| 2 | /* |
| 3 | * Copyright (C) International Business Machines Corp., 2007,2008 |
| 4 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 5 | * Copyright (C) 2020 Samsung Electronics Co., Ltd. |
| 6 | * Author(s): Namjae Jeon <linkinjeon@kernel.org> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/string.h> |
Christian Brauner | a793d79 | 2021-12-03 12:16:59 +0100 | [diff] [blame] | 12 | #include <linux/mnt_idmapping.h> |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 13 | |
| 14 | #include "smbacl.h" |
| 15 | #include "smb_common.h" |
| 16 | #include "server.h" |
| 17 | #include "misc.h" |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 18 | #include "mgmt/share_config.h" |
| 19 | |
| 20 | static const struct smb_sid domain = {1, 4, {0, 0, 0, 0, 0, 5}, |
| 21 | {cpu_to_le32(21), cpu_to_le32(1), cpu_to_le32(2), cpu_to_le32(3), |
| 22 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 23 | |
| 24 | /* security id for everyone/world system group */ |
| 25 | static const struct smb_sid creator_owner = { |
| 26 | 1, 1, {0, 0, 0, 0, 0, 3}, {0} }; |
| 27 | /* security id for everyone/world system group */ |
| 28 | static const struct smb_sid creator_group = { |
| 29 | 1, 1, {0, 0, 0, 0, 0, 3}, {cpu_to_le32(1)} }; |
| 30 | |
| 31 | /* security id for everyone/world system group */ |
| 32 | static const struct smb_sid sid_everyone = { |
| 33 | 1, 1, {0, 0, 0, 0, 0, 1}, {0} }; |
| 34 | /* security id for Authenticated Users system group */ |
| 35 | static const struct smb_sid sid_authusers = { |
| 36 | 1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11)} }; |
| 37 | |
| 38 | /* S-1-22-1 Unmapped Unix users */ |
| 39 | static const struct smb_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22}, |
| 40 | {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 41 | |
| 42 | /* S-1-22-2 Unmapped Unix groups */ |
| 43 | static const struct smb_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22}, |
| 44 | {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 45 | |
| 46 | /* |
| 47 | * See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx |
| 48 | */ |
| 49 | |
| 50 | /* S-1-5-88 MS NFS and Apple style UID/GID/mode */ |
| 51 | |
| 52 | /* S-1-5-88-1 Unix uid */ |
| 53 | static const struct smb_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5}, |
| 54 | {cpu_to_le32(88), |
| 55 | cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 56 | |
| 57 | /* S-1-5-88-2 Unix gid */ |
| 58 | static const struct smb_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5}, |
| 59 | {cpu_to_le32(88), |
| 60 | cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 61 | |
| 62 | /* S-1-5-88-3 Unix mode */ |
| 63 | static const struct smb_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5}, |
| 64 | {cpu_to_le32(88), |
| 65 | cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; |
| 66 | |
| 67 | /* |
| 68 | * if the two SIDs (roughly equivalent to a UUID for a user or group) are |
| 69 | * the same returns zero, if they do not match returns non-zero. |
| 70 | */ |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 71 | int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 72 | { |
| 73 | int i; |
| 74 | int num_subauth, num_sat, num_saw; |
| 75 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 76 | if (!ctsid || !cwsid) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 77 | return 1; |
| 78 | |
| 79 | /* compare the revision */ |
| 80 | if (ctsid->revision != cwsid->revision) { |
| 81 | if (ctsid->revision > cwsid->revision) |
| 82 | return 1; |
| 83 | else |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | /* compare all of the six auth values */ |
| 88 | for (i = 0; i < NUM_AUTHS; ++i) { |
| 89 | if (ctsid->authority[i] != cwsid->authority[i]) { |
| 90 | if (ctsid->authority[i] > cwsid->authority[i]) |
| 91 | return 1; |
| 92 | else |
| 93 | return -1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* compare all of the subauth values if any */ |
| 98 | num_sat = ctsid->num_subauth; |
| 99 | num_saw = cwsid->num_subauth; |
| 100 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 101 | if (num_subauth) { |
| 102 | for (i = 0; i < num_subauth; ++i) { |
| 103 | if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) { |
| 104 | if (le32_to_cpu(ctsid->sub_auth[i]) > |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 105 | le32_to_cpu(cwsid->sub_auth[i])) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 106 | return 1; |
| 107 | else |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 0; /* sids compare/match */ |
| 114 | } |
| 115 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 116 | static void smb_copy_sid(struct smb_sid *dst, const struct smb_sid *src) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 117 | { |
| 118 | int i; |
| 119 | |
| 120 | dst->revision = src->revision; |
| 121 | dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES); |
| 122 | for (i = 0; i < NUM_AUTHS; ++i) |
| 123 | dst->authority[i] = src->authority[i]; |
| 124 | for (i = 0; i < dst->num_subauth; ++i) |
| 125 | dst->sub_auth[i] = src->sub_auth[i]; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * change posix mode to reflect permissions |
| 130 | * pmode is the existing mode (we only want to overwrite part of this |
| 131 | * bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007 |
| 132 | */ |
| 133 | static umode_t access_flags_to_mode(struct smb_fattr *fattr, __le32 ace_flags, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 134 | int type) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 135 | { |
| 136 | __u32 flags = le32_to_cpu(ace_flags); |
| 137 | umode_t mode = 0; |
| 138 | |
| 139 | if (flags & GENERIC_ALL) { |
| 140 | mode = 0777; |
| 141 | ksmbd_debug(SMB, "all perms\n"); |
| 142 | return mode; |
| 143 | } |
| 144 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 145 | if ((flags & GENERIC_READ) || (flags & FILE_READ_RIGHTS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 146 | mode = 0444; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 147 | if ((flags & GENERIC_WRITE) || (flags & FILE_WRITE_RIGHTS)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 148 | mode |= 0222; |
| 149 | if (S_ISDIR(fattr->cf_mode)) |
| 150 | mode |= 0111; |
| 151 | } |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 152 | if ((flags & GENERIC_EXECUTE) || (flags & FILE_EXEC_RIGHTS)) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 153 | mode |= 0111; |
| 154 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 155 | if (type == ACCESS_DENIED_ACE_TYPE || type == ACCESS_DENIED_OBJECT_ACE_TYPE) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 156 | mode = ~mode; |
| 157 | |
| 158 | ksmbd_debug(SMB, "access flags 0x%x mode now %04o\n", flags, mode); |
| 159 | |
| 160 | return mode; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Generate access flags to reflect permissions mode is the existing mode. |
| 165 | * This function is called for every ACE in the DACL whose SID matches |
| 166 | * with either owner or group or everyone. |
| 167 | */ |
| 168 | static void mode_to_access_flags(umode_t mode, umode_t bits_to_use, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 169 | __u32 *pace_flags) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 170 | { |
| 171 | /* reset access mask */ |
| 172 | *pace_flags = 0x0; |
| 173 | |
| 174 | /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */ |
| 175 | mode &= bits_to_use; |
| 176 | |
| 177 | /* |
| 178 | * check for R/W/X UGO since we do not know whose flags |
| 179 | * is this but we have cleared all the bits sans RWX for |
| 180 | * either user or group or other as per bits_to_use |
| 181 | */ |
| 182 | if (mode & 0444) |
| 183 | *pace_flags |= SET_FILE_READ_RIGHTS; |
| 184 | if (mode & 0222) |
| 185 | *pace_flags |= FILE_WRITE_RIGHTS; |
| 186 | if (mode & 0111) |
| 187 | *pace_flags |= SET_FILE_EXEC_RIGHTS; |
| 188 | |
| 189 | ksmbd_debug(SMB, "mode: %o, access flags now 0x%x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 190 | mode, *pace_flags); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static __u16 fill_ace_for_sid(struct smb_ace *pntace, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 194 | const struct smb_sid *psid, int type, int flags, |
| 195 | umode_t mode, umode_t bits) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 196 | { |
| 197 | int i; |
| 198 | __u16 size = 0; |
| 199 | __u32 access_req = 0; |
| 200 | |
| 201 | pntace->type = type; |
| 202 | pntace->flags = flags; |
| 203 | mode_to_access_flags(mode, bits, &access_req); |
| 204 | if (!access_req) |
| 205 | access_req = SET_MINIMUM_RIGHTS; |
| 206 | pntace->access_req = cpu_to_le32(access_req); |
| 207 | |
| 208 | pntace->sid.revision = psid->revision; |
| 209 | pntace->sid.num_subauth = psid->num_subauth; |
| 210 | for (i = 0; i < NUM_AUTHS; i++) |
| 211 | pntace->sid.authority[i] = psid->authority[i]; |
| 212 | for (i = 0; i < psid->num_subauth; i++) |
| 213 | pntace->sid.sub_auth[i] = psid->sub_auth[i]; |
| 214 | |
| 215 | size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4); |
| 216 | pntace->size = cpu_to_le16(size); |
| 217 | |
| 218 | return size; |
| 219 | } |
| 220 | |
| 221 | void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid) |
| 222 | { |
| 223 | switch (sidtype) { |
| 224 | case SIDOWNER: |
| 225 | smb_copy_sid(ssid, &server_conf.domain_sid); |
| 226 | break; |
| 227 | case SIDUNIX_USER: |
| 228 | smb_copy_sid(ssid, &sid_unix_users); |
| 229 | break; |
| 230 | case SIDUNIX_GROUP: |
| 231 | smb_copy_sid(ssid, &sid_unix_groups); |
| 232 | break; |
| 233 | case SIDCREATOR_OWNER: |
| 234 | smb_copy_sid(ssid, &creator_owner); |
| 235 | return; |
| 236 | case SIDCREATOR_GROUP: |
| 237 | smb_copy_sid(ssid, &creator_group); |
| 238 | return; |
| 239 | case SIDNFS_USER: |
| 240 | smb_copy_sid(ssid, &sid_unix_NFS_users); |
| 241 | break; |
| 242 | case SIDNFS_GROUP: |
| 243 | smb_copy_sid(ssid, &sid_unix_NFS_groups); |
| 244 | break; |
| 245 | case SIDNFS_MODE: |
| 246 | smb_copy_sid(ssid, &sid_unix_NFS_mode); |
| 247 | break; |
| 248 | default: |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | /* RID */ |
| 253 | ssid->sub_auth[ssid->num_subauth] = cpu_to_le32(cid); |
| 254 | ssid->num_subauth++; |
| 255 | } |
| 256 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 257 | static int sid_to_id(struct user_namespace *user_ns, |
| 258 | struct smb_sid *psid, uint sidtype, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 259 | struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 260 | { |
| 261 | int rc = -EINVAL; |
| 262 | |
| 263 | /* |
| 264 | * If we have too many subauthorities, then something is really wrong. |
| 265 | * Just return an error. |
| 266 | */ |
| 267 | if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 268 | pr_err("%s: %u subauthorities is too many!\n", |
| 269 | __func__, psid->num_subauth); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 270 | return -EIO; |
| 271 | } |
| 272 | |
| 273 | if (sidtype == SIDOWNER) { |
| 274 | kuid_t uid; |
| 275 | uid_t id; |
| 276 | |
| 277 | id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]); |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 278 | uid = mapped_kuid_user(user_ns, &init_user_ns, KUIDT_INIT(id)); |
Namjae Jeon | 4cf0ccd | 2021-09-07 08:16:26 +0900 | [diff] [blame] | 279 | if (uid_valid(uid)) { |
| 280 | fattr->cf_uid = uid; |
| 281 | rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 282 | } |
| 283 | } else { |
| 284 | kgid_t gid; |
| 285 | gid_t id; |
| 286 | |
| 287 | id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]); |
Christian Brauner | 4472071 | 2021-12-03 12:17:03 +0100 | [diff] [blame] | 288 | gid = mapped_kgid_user(user_ns, &init_user_ns, KGIDT_INIT(id)); |
Namjae Jeon | 4cf0ccd | 2021-09-07 08:16:26 +0900 | [diff] [blame] | 289 | if (gid_valid(gid)) { |
| 290 | fattr->cf_gid = gid; |
| 291 | rc = 0; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | return rc; |
| 296 | } |
| 297 | |
| 298 | void posix_state_to_acl(struct posix_acl_state *state, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 299 | struct posix_acl_entry *pace) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 300 | { |
| 301 | int i; |
| 302 | |
| 303 | pace->e_tag = ACL_USER_OBJ; |
| 304 | pace->e_perm = state->owner.allow; |
| 305 | for (i = 0; i < state->users->n; i++) { |
| 306 | pace++; |
| 307 | pace->e_tag = ACL_USER; |
| 308 | pace->e_uid = state->users->aces[i].uid; |
| 309 | pace->e_perm = state->users->aces[i].perms.allow; |
| 310 | } |
| 311 | |
| 312 | pace++; |
| 313 | pace->e_tag = ACL_GROUP_OBJ; |
| 314 | pace->e_perm = state->group.allow; |
| 315 | |
| 316 | for (i = 0; i < state->groups->n; i++) { |
| 317 | pace++; |
| 318 | pace->e_tag = ACL_GROUP; |
| 319 | pace->e_gid = state->groups->aces[i].gid; |
| 320 | pace->e_perm = state->groups->aces[i].perms.allow; |
| 321 | } |
| 322 | |
| 323 | if (state->users->n || state->groups->n) { |
| 324 | pace++; |
| 325 | pace->e_tag = ACL_MASK; |
| 326 | pace->e_perm = state->mask.allow; |
| 327 | } |
| 328 | |
| 329 | pace++; |
| 330 | pace->e_tag = ACL_OTHER; |
| 331 | pace->e_perm = state->other.allow; |
| 332 | } |
| 333 | |
| 334 | int init_acl_state(struct posix_acl_state *state, int cnt) |
| 335 | { |
| 336 | int alloc; |
| 337 | |
| 338 | memset(state, 0, sizeof(struct posix_acl_state)); |
| 339 | /* |
| 340 | * In the worst case, each individual acl could be for a distinct |
| 341 | * named user or group, but we don't know which, so we allocate |
| 342 | * enough space for either: |
| 343 | */ |
| 344 | alloc = sizeof(struct posix_ace_state_array) |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 345 | + cnt * sizeof(struct posix_user_ace_state); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 346 | state->users = kzalloc(alloc, GFP_KERNEL); |
| 347 | if (!state->users) |
| 348 | return -ENOMEM; |
| 349 | state->groups = kzalloc(alloc, GFP_KERNEL); |
| 350 | if (!state->groups) { |
| 351 | kfree(state->users); |
| 352 | return -ENOMEM; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | void free_acl_state(struct posix_acl_state *state) |
| 358 | { |
| 359 | kfree(state->users); |
| 360 | kfree(state->groups); |
| 361 | } |
| 362 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 363 | static void parse_dacl(struct user_namespace *user_ns, |
| 364 | struct smb_acl *pdacl, char *end_of_acl, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 365 | struct smb_sid *pownersid, struct smb_sid *pgrpsid, |
| 366 | struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 367 | { |
| 368 | int i, ret; |
| 369 | int num_aces = 0; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 370 | unsigned int acl_size; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 371 | char *acl_base; |
| 372 | struct smb_ace **ppace; |
| 373 | struct posix_acl_entry *cf_pace, *cf_pdace; |
| 374 | struct posix_acl_state acl_state, default_acl_state; |
| 375 | umode_t mode = 0, acl_mode; |
| 376 | bool owner_found = false, group_found = false, others_found = false; |
| 377 | |
| 378 | if (!pdacl) |
| 379 | return; |
| 380 | |
| 381 | /* validate that we do not go past end of acl */ |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 382 | if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) || |
Namjae Jeon | 548e9ad | 2021-03-21 17:30:49 +0900 | [diff] [blame] | 383 | end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 384 | pr_err("ACL too small to parse DACL\n"); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 389 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
| 390 | le32_to_cpu(pdacl->num_aces)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 391 | |
| 392 | acl_base = (char *)pdacl; |
| 393 | acl_size = sizeof(struct smb_acl); |
| 394 | |
| 395 | num_aces = le32_to_cpu(pdacl->num_aces); |
| 396 | if (num_aces <= 0) |
| 397 | return; |
| 398 | |
| 399 | if (num_aces > ULONG_MAX / sizeof(struct smb_ace *)) |
| 400 | return; |
| 401 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 402 | ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), GFP_KERNEL); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 403 | if (!ppace) |
| 404 | return; |
| 405 | |
| 406 | ret = init_acl_state(&acl_state, num_aces); |
| 407 | if (ret) |
| 408 | return; |
| 409 | ret = init_acl_state(&default_acl_state, num_aces); |
| 410 | if (ret) { |
| 411 | free_acl_state(&acl_state); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * reset rwx permissions for user/group/other. |
| 417 | * Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 418 | * user/group/other have no permissions |
| 419 | */ |
| 420 | for (i = 0; i < num_aces; ++i) { |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 421 | if (end_of_acl - acl_base < acl_size) |
| 422 | break; |
| 423 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 424 | ppace[i] = (struct smb_ace *)(acl_base + acl_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 425 | acl_base = (char *)ppace[i]; |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 426 | acl_size = offsetof(struct smb_ace, sid) + |
| 427 | offsetof(struct smb_sid, sub_auth); |
| 428 | |
| 429 | if (end_of_acl - acl_base < acl_size || |
| 430 | ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES || |
| 431 | (end_of_acl - acl_base < |
| 432 | acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) || |
| 433 | (le16_to_cpu(ppace[i]->size) < |
| 434 | acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth)) |
| 435 | break; |
| 436 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 437 | acl_size = le16_to_cpu(ppace[i]->size); |
| 438 | ppace[i]->access_req = |
| 439 | smb_map_generic_desired_access(ppace[i]->access_req); |
| 440 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 441 | if (!(compare_sids(&ppace[i]->sid, &sid_unix_NFS_mode))) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 442 | fattr->cf_mode = |
| 443 | le32_to_cpu(ppace[i]->sid.sub_auth[2]); |
| 444 | break; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 445 | } else if (!compare_sids(&ppace[i]->sid, pownersid)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 446 | acl_mode = access_flags_to_mode(fattr, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 447 | ppace[i]->access_req, |
| 448 | ppace[i]->type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 449 | acl_mode &= 0700; |
| 450 | |
| 451 | if (!owner_found) { |
| 452 | mode &= ~(0700); |
| 453 | mode |= acl_mode; |
| 454 | } |
| 455 | owner_found = true; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 456 | } else if (!compare_sids(&ppace[i]->sid, pgrpsid) || |
| 457 | ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] == |
| 458 | DOMAIN_USER_RID_LE) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 459 | acl_mode = access_flags_to_mode(fattr, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 460 | ppace[i]->access_req, |
| 461 | ppace[i]->type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 462 | acl_mode &= 0070; |
| 463 | if (!group_found) { |
| 464 | mode &= ~(0070); |
| 465 | mode |= acl_mode; |
| 466 | } |
| 467 | group_found = true; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 468 | } else if (!compare_sids(&ppace[i]->sid, &sid_everyone)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 469 | acl_mode = access_flags_to_mode(fattr, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 470 | ppace[i]->access_req, |
| 471 | ppace[i]->type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 472 | acl_mode &= 0007; |
| 473 | if (!others_found) { |
| 474 | mode &= ~(0007); |
| 475 | mode |= acl_mode; |
| 476 | } |
| 477 | others_found = true; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 478 | } else if (!compare_sids(&ppace[i]->sid, &creator_owner)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 479 | continue; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 480 | } else if (!compare_sids(&ppace[i]->sid, &creator_group)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 481 | continue; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 482 | } else if (!compare_sids(&ppace[i]->sid, &sid_authusers)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 483 | continue; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 484 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 485 | struct smb_fattr temp_fattr; |
| 486 | |
| 487 | acl_mode = access_flags_to_mode(fattr, ppace[i]->access_req, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 488 | ppace[i]->type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 489 | temp_fattr.cf_uid = INVALID_UID; |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 490 | ret = sid_to_id(user_ns, &ppace[i]->sid, SIDOWNER, &temp_fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 491 | if (ret || uid_eq(temp_fattr.cf_uid, INVALID_UID)) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 492 | pr_err("%s: Error %d mapping Owner SID to uid\n", |
| 493 | __func__, ret); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 494 | continue; |
| 495 | } |
| 496 | |
| 497 | acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004; |
| 498 | acl_state.users->aces[acl_state.users->n].uid = |
| 499 | temp_fattr.cf_uid; |
| 500 | acl_state.users->aces[acl_state.users->n++].perms.allow = |
| 501 | ((acl_mode & 0700) >> 6) | 0004; |
| 502 | default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004; |
| 503 | default_acl_state.users->aces[default_acl_state.users->n].uid = |
| 504 | temp_fattr.cf_uid; |
| 505 | default_acl_state.users->aces[default_acl_state.users->n++].perms.allow = |
| 506 | ((acl_mode & 0700) >> 6) | 0004; |
| 507 | } |
| 508 | } |
| 509 | kfree(ppace); |
| 510 | |
| 511 | if (owner_found) { |
| 512 | /* The owner must be set to at least read-only. */ |
| 513 | acl_state.owner.allow = ((mode & 0700) >> 6) | 0004; |
| 514 | acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid; |
| 515 | acl_state.users->aces[acl_state.users->n++].perms.allow = |
| 516 | ((mode & 0700) >> 6) | 0004; |
| 517 | default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004; |
| 518 | default_acl_state.users->aces[default_acl_state.users->n].uid = |
| 519 | fattr->cf_uid; |
| 520 | default_acl_state.users->aces[default_acl_state.users->n++].perms.allow = |
| 521 | ((mode & 0700) >> 6) | 0004; |
| 522 | } |
| 523 | |
| 524 | if (group_found) { |
| 525 | acl_state.group.allow = (mode & 0070) >> 3; |
| 526 | acl_state.groups->aces[acl_state.groups->n].gid = |
| 527 | fattr->cf_gid; |
| 528 | acl_state.groups->aces[acl_state.groups->n++].perms.allow = |
| 529 | (mode & 0070) >> 3; |
Dan Carpenter | 86df49e | 2021-03-18 16:10:21 +0300 | [diff] [blame] | 530 | default_acl_state.group.allow = (mode & 0070) >> 3; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 531 | default_acl_state.groups->aces[default_acl_state.groups->n].gid = |
| 532 | fattr->cf_gid; |
| 533 | default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow = |
| 534 | (mode & 0070) >> 3; |
| 535 | } |
| 536 | |
| 537 | if (others_found) { |
| 538 | fattr->cf_mode &= ~(0007); |
| 539 | fattr->cf_mode |= mode & 0007; |
| 540 | |
| 541 | acl_state.other.allow = mode & 0007; |
| 542 | default_acl_state.other.allow = mode & 0007; |
| 543 | } |
| 544 | |
| 545 | if (acl_state.users->n || acl_state.groups->n) { |
| 546 | acl_state.mask.allow = 0x07; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 547 | |
| 548 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 549 | fattr->cf_acls = |
| 550 | posix_acl_alloc(acl_state.users->n + |
| 551 | acl_state.groups->n + 4, GFP_KERNEL); |
| 552 | if (fattr->cf_acls) { |
| 553 | cf_pace = fattr->cf_acls->a_entries; |
| 554 | posix_state_to_acl(&acl_state, cf_pace); |
| 555 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | if (default_acl_state.users->n || default_acl_state.groups->n) { |
| 560 | default_acl_state.mask.allow = 0x07; |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 561 | |
| 562 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 563 | fattr->cf_dacls = |
| 564 | posix_acl_alloc(default_acl_state.users->n + |
| 565 | default_acl_state.groups->n + 4, GFP_KERNEL); |
| 566 | if (fattr->cf_dacls) { |
| 567 | cf_pdace = fattr->cf_dacls->a_entries; |
| 568 | posix_state_to_acl(&default_acl_state, cf_pdace); |
| 569 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | free_acl_state(&acl_state); |
| 573 | free_acl_state(&default_acl_state); |
| 574 | } |
| 575 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 576 | static void set_posix_acl_entries_dacl(struct user_namespace *user_ns, |
| 577 | struct smb_ace *pndace, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 578 | struct smb_fattr *fattr, u32 *num_aces, |
| 579 | u16 *size, u32 nt_aces_num) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 580 | { |
| 581 | struct posix_acl_entry *pace; |
| 582 | struct smb_sid *sid; |
| 583 | struct smb_ace *ntace; |
| 584 | int i, j; |
| 585 | |
| 586 | if (!fattr->cf_acls) |
| 587 | goto posix_default_acl; |
| 588 | |
| 589 | pace = fattr->cf_acls->a_entries; |
| 590 | for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) { |
| 591 | int flags = 0; |
| 592 | |
| 593 | sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL); |
| 594 | if (!sid) |
| 595 | break; |
| 596 | |
| 597 | if (pace->e_tag == ACL_USER) { |
| 598 | uid_t uid; |
| 599 | unsigned int sid_type = SIDOWNER; |
| 600 | |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 601 | uid = posix_acl_uid_translate(user_ns, pace); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 602 | if (!uid) |
| 603 | sid_type = SIDUNIX_USER; |
| 604 | id_to_sid(uid, sid_type, sid); |
| 605 | } else if (pace->e_tag == ACL_GROUP) { |
| 606 | gid_t gid; |
| 607 | |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 608 | gid = posix_acl_gid_translate(user_ns, pace); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 609 | id_to_sid(gid, SIDUNIX_GROUP, sid); |
| 610 | } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) { |
| 611 | smb_copy_sid(sid, &sid_everyone); |
| 612 | } else { |
| 613 | kfree(sid); |
| 614 | continue; |
| 615 | } |
| 616 | ntace = pndace; |
| 617 | for (j = 0; j < nt_aces_num; j++) { |
| 618 | if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] == |
| 619 | sid->sub_auth[sid->num_subauth - 1]) |
| 620 | goto pass_same_sid; |
| 621 | ntace = (struct smb_ace *)((char *)ntace + |
| 622 | le16_to_cpu(ntace->size)); |
| 623 | } |
| 624 | |
| 625 | if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER) |
| 626 | flags = 0x03; |
| 627 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 628 | ntace = (struct smb_ace *)((char *)pndace + *size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 629 | *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, flags, |
| 630 | pace->e_perm, 0777); |
| 631 | (*num_aces)++; |
| 632 | if (pace->e_tag == ACL_USER) |
| 633 | ntace->access_req |= |
| 634 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 635 | |
| 636 | if (S_ISDIR(fattr->cf_mode) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 637 | (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) { |
| 638 | ntace = (struct smb_ace *)((char *)pndace + *size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 639 | *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, |
| 640 | 0x03, pace->e_perm, 0777); |
| 641 | (*num_aces)++; |
| 642 | if (pace->e_tag == ACL_USER) |
| 643 | ntace->access_req |= |
| 644 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 645 | } |
| 646 | |
| 647 | pass_same_sid: |
| 648 | kfree(sid); |
| 649 | } |
| 650 | |
| 651 | if (nt_aces_num) |
| 652 | return; |
| 653 | |
| 654 | posix_default_acl: |
| 655 | if (!fattr->cf_dacls) |
| 656 | return; |
| 657 | |
| 658 | pace = fattr->cf_dacls->a_entries; |
| 659 | for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) { |
| 660 | sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL); |
| 661 | if (!sid) |
| 662 | break; |
| 663 | |
| 664 | if (pace->e_tag == ACL_USER) { |
| 665 | uid_t uid; |
| 666 | |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 667 | uid = posix_acl_uid_translate(user_ns, pace); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 668 | id_to_sid(uid, SIDCREATOR_OWNER, sid); |
| 669 | } else if (pace->e_tag == ACL_GROUP) { |
| 670 | gid_t gid; |
| 671 | |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 672 | gid = posix_acl_gid_translate(user_ns, pace); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 673 | id_to_sid(gid, SIDCREATOR_GROUP, sid); |
| 674 | } else { |
| 675 | kfree(sid); |
| 676 | continue; |
| 677 | } |
| 678 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 679 | ntace = (struct smb_ace *)((char *)pndace + *size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 680 | *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, 0x0b, |
| 681 | pace->e_perm, 0777); |
| 682 | (*num_aces)++; |
| 683 | if (pace->e_tag == ACL_USER) |
| 684 | ntace->access_req |= |
| 685 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 686 | kfree(sid); |
| 687 | } |
| 688 | } |
| 689 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 690 | static void set_ntacl_dacl(struct user_namespace *user_ns, |
| 691 | struct smb_acl *pndacl, |
| 692 | struct smb_acl *nt_dacl, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 693 | const struct smb_sid *pownersid, |
| 694 | const struct smb_sid *pgrpsid, |
| 695 | struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 696 | { |
| 697 | struct smb_ace *ntace, *pndace; |
| 698 | int nt_num_aces = le32_to_cpu(nt_dacl->num_aces), num_aces = 0; |
| 699 | unsigned short size = 0; |
| 700 | int i; |
| 701 | |
| 702 | pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl)); |
| 703 | if (nt_num_aces) { |
| 704 | ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl)); |
| 705 | for (i = 0; i < nt_num_aces; i++) { |
| 706 | memcpy((char *)pndace + size, ntace, le16_to_cpu(ntace->size)); |
| 707 | size += le16_to_cpu(ntace->size); |
| 708 | ntace = (struct smb_ace *)((char *)ntace + le16_to_cpu(ntace->size)); |
| 709 | num_aces++; |
| 710 | } |
| 711 | } |
| 712 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 713 | set_posix_acl_entries_dacl(user_ns, pndace, fattr, |
| 714 | &num_aces, &size, nt_num_aces); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 715 | pndacl->num_aces = cpu_to_le32(num_aces); |
| 716 | pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size); |
| 717 | } |
| 718 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 719 | static void set_mode_dacl(struct user_namespace *user_ns, |
| 720 | struct smb_acl *pndacl, struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 721 | { |
| 722 | struct smb_ace *pace, *pndace; |
| 723 | u32 num_aces = 0; |
| 724 | u16 size = 0, ace_size = 0; |
| 725 | uid_t uid; |
| 726 | const struct smb_sid *sid; |
| 727 | |
| 728 | pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl)); |
| 729 | |
| 730 | if (fattr->cf_acls) { |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 731 | set_posix_acl_entries_dacl(user_ns, pndace, fattr, |
| 732 | &num_aces, &size, num_aces); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 733 | goto out; |
| 734 | } |
| 735 | |
| 736 | /* owner RID */ |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 737 | uid = from_kuid(&init_user_ns, fattr->cf_uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 738 | if (uid) |
| 739 | sid = &server_conf.domain_sid; |
| 740 | else |
| 741 | sid = &sid_unix_users; |
| 742 | ace_size = fill_ace_for_sid(pace, sid, ACCESS_ALLOWED, 0, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 743 | fattr->cf_mode, 0700); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 744 | pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 745 | pace->size = cpu_to_le16(ace_size + 4); |
| 746 | size += le16_to_cpu(pace->size); |
| 747 | pace = (struct smb_ace *)((char *)pndace + size); |
| 748 | |
| 749 | /* Group RID */ |
| 750 | ace_size = fill_ace_for_sid(pace, &sid_unix_groups, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 751 | ACCESS_ALLOWED, 0, fattr->cf_mode, 0070); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 752 | pace->sid.sub_auth[pace->sid.num_subauth++] = |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 753 | cpu_to_le32(from_kgid(&init_user_ns, fattr->cf_gid)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 754 | pace->size = cpu_to_le16(ace_size + 4); |
| 755 | size += le16_to_cpu(pace->size); |
| 756 | pace = (struct smb_ace *)((char *)pndace + size); |
| 757 | num_aces = 3; |
| 758 | |
| 759 | if (S_ISDIR(fattr->cf_mode)) { |
| 760 | pace = (struct smb_ace *)((char *)pndace + size); |
| 761 | |
| 762 | /* creator owner */ |
| 763 | size += fill_ace_for_sid(pace, &creator_owner, ACCESS_ALLOWED, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 764 | 0x0b, fattr->cf_mode, 0700); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 765 | pace = (struct smb_ace *)((char *)pndace + size); |
| 766 | |
| 767 | /* creator group */ |
| 768 | size += fill_ace_for_sid(pace, &creator_group, ACCESS_ALLOWED, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 769 | 0x0b, fattr->cf_mode, 0070); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 770 | pace = (struct smb_ace *)((char *)pndace + size); |
| 771 | num_aces = 5; |
| 772 | } |
| 773 | |
| 774 | /* other */ |
| 775 | size += fill_ace_for_sid(pace, &sid_everyone, ACCESS_ALLOWED, 0, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 776 | fattr->cf_mode, 0007); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 777 | |
| 778 | out: |
| 779 | pndacl->num_aces = cpu_to_le32(num_aces); |
| 780 | pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size); |
| 781 | } |
| 782 | |
| 783 | static int parse_sid(struct smb_sid *psid, char *end_of_acl) |
| 784 | { |
| 785 | /* |
| 786 | * validate that we do not go past end of ACL - sid must be at least 8 |
| 787 | * bytes long (assuming no sub-auths - e.g. the null SID |
| 788 | */ |
| 789 | if (end_of_acl < (char *)psid + 8) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 790 | pr_err("ACL too small to parse SID %p\n", psid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 791 | return -EINVAL; |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | /* Convert CIFS ACL to POSIX form */ |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 798 | int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd, |
| 799 | int acl_len, struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 800 | { |
| 801 | int rc = 0; |
| 802 | struct smb_sid *owner_sid_ptr, *group_sid_ptr; |
| 803 | struct smb_acl *dacl_ptr; /* no need for SACL ptr */ |
| 804 | char *end_of_acl = ((char *)pntsd) + acl_len; |
| 805 | __u32 dacloffset; |
Namjae Jeon | 548e9ad | 2021-03-21 17:30:49 +0900 | [diff] [blame] | 806 | int pntsd_type; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 807 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 808 | if (!pntsd) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 809 | return -EIO; |
| 810 | |
Hyunchul Lee | 8f77150 | 2021-09-24 22:22:22 +0900 | [diff] [blame] | 811 | if (acl_len < sizeof(struct smb_ntsd)) |
| 812 | return -EINVAL; |
| 813 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 814 | owner_sid_ptr = (struct smb_sid *)((char *)pntsd + |
| 815 | le32_to_cpu(pntsd->osidoffset)); |
| 816 | group_sid_ptr = (struct smb_sid *)((char *)pntsd + |
| 817 | le32_to_cpu(pntsd->gsidoffset)); |
| 818 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 819 | dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset); |
| 820 | ksmbd_debug(SMB, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 821 | "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n", |
| 822 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 823 | le32_to_cpu(pntsd->gsidoffset), |
| 824 | le32_to_cpu(pntsd->sacloffset), dacloffset); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 825 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 826 | pntsd_type = le16_to_cpu(pntsd->type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 827 | if (!(pntsd_type & DACL_PRESENT)) { |
| 828 | ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n"); |
| 829 | return rc; |
| 830 | } |
| 831 | |
| 832 | pntsd->type = cpu_to_le16(DACL_PRESENT); |
| 833 | |
| 834 | if (pntsd->osidoffset) { |
| 835 | rc = parse_sid(owner_sid_ptr, end_of_acl); |
| 836 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 837 | pr_err("%s: Error %d parsing Owner SID\n", __func__, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 838 | return rc; |
| 839 | } |
| 840 | |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 841 | rc = sid_to_id(user_ns, owner_sid_ptr, SIDOWNER, fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 842 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 843 | pr_err("%s: Error %d mapping Owner SID to uid\n", |
| 844 | __func__, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 845 | owner_sid_ptr = NULL; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (pntsd->gsidoffset) { |
| 850 | rc = parse_sid(group_sid_ptr, end_of_acl); |
| 851 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 852 | pr_err("%s: Error %d mapping Owner SID to gid\n", |
| 853 | __func__, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 854 | return rc; |
| 855 | } |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 856 | rc = sid_to_id(user_ns, group_sid_ptr, SIDUNIX_GROUP, fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 857 | if (rc) { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 858 | pr_err("%s: Error %d mapping Group SID to gid\n", |
| 859 | __func__, rc); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 860 | group_sid_ptr = NULL; |
| 861 | } |
| 862 | } |
| 863 | |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 864 | if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) == |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 865 | (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) |
| 866 | pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED); |
| 867 | if (pntsd_type & DACL_PROTECTED) |
| 868 | pntsd->type |= cpu_to_le16(DACL_PROTECTED); |
| 869 | |
| 870 | if (dacloffset) { |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 871 | parse_dacl(user_ns, dacl_ptr, end_of_acl, |
| 872 | owner_sid_ptr, group_sid_ptr, fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 879 | int build_sec_desc(struct user_namespace *user_ns, |
| 880 | struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 881 | int addition_info, __u32 *secdesclen, |
| 882 | struct smb_fattr *fattr) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 883 | { |
| 884 | int rc = 0; |
| 885 | __u32 offset; |
| 886 | struct smb_sid *owner_sid_ptr, *group_sid_ptr; |
| 887 | struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 888 | struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 889 | uid_t uid; |
| 890 | gid_t gid; |
| 891 | unsigned int sid_type = SIDOWNER; |
| 892 | |
| 893 | nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL); |
| 894 | if (!nowner_sid_ptr) |
| 895 | return -ENOMEM; |
| 896 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 897 | uid = from_kuid(&init_user_ns, fattr->cf_uid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 898 | if (!uid) |
| 899 | sid_type = SIDUNIX_USER; |
| 900 | id_to_sid(uid, sid_type, nowner_sid_ptr); |
| 901 | |
| 902 | ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL); |
| 903 | if (!ngroup_sid_ptr) { |
| 904 | kfree(nowner_sid_ptr); |
| 905 | return -ENOMEM; |
| 906 | } |
| 907 | |
Christian Brauner | 43205ca | 2021-08-23 17:13:50 +0200 | [diff] [blame] | 908 | gid = from_kgid(&init_user_ns, fattr->cf_gid); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 909 | id_to_sid(gid, SIDUNIX_GROUP, ngroup_sid_ptr); |
| 910 | |
| 911 | offset = sizeof(struct smb_ntsd); |
| 912 | pntsd->sacloffset = 0; |
| 913 | pntsd->revision = cpu_to_le16(1); |
| 914 | pntsd->type = cpu_to_le16(SELF_RELATIVE); |
| 915 | if (ppntsd) |
| 916 | pntsd->type |= ppntsd->type; |
| 917 | |
| 918 | if (addition_info & OWNER_SECINFO) { |
| 919 | pntsd->osidoffset = cpu_to_le32(offset); |
| 920 | owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset); |
| 921 | smb_copy_sid(owner_sid_ptr, nowner_sid_ptr); |
| 922 | offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4); |
| 923 | } |
| 924 | |
| 925 | if (addition_info & GROUP_SECINFO) { |
| 926 | pntsd->gsidoffset = cpu_to_le32(offset); |
| 927 | group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset); |
| 928 | smb_copy_sid(group_sid_ptr, ngroup_sid_ptr); |
| 929 | offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4); |
| 930 | } |
| 931 | |
| 932 | if (addition_info & DACL_SECINFO) { |
| 933 | pntsd->type |= cpu_to_le16(DACL_PRESENT); |
| 934 | dacl_ptr = (struct smb_acl *)((char *)pntsd + offset); |
| 935 | dacl_ptr->revision = cpu_to_le16(2); |
| 936 | dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl)); |
| 937 | dacl_ptr->num_aces = 0; |
| 938 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 939 | if (!ppntsd) { |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 940 | set_mode_dacl(user_ns, dacl_ptr, fattr); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 941 | } else if (!ppntsd->dacloffset) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 942 | goto out; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 943 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 944 | struct smb_acl *ppdacl_ptr; |
| 945 | |
| 946 | ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + |
| 947 | le32_to_cpu(ppntsd->dacloffset)); |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 948 | set_ntacl_dacl(user_ns, dacl_ptr, ppdacl_ptr, |
| 949 | nowner_sid_ptr, ngroup_sid_ptr, fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 950 | } |
| 951 | pntsd->dacloffset = cpu_to_le32(offset); |
| 952 | offset += le16_to_cpu(dacl_ptr->size); |
| 953 | } |
| 954 | |
| 955 | out: |
| 956 | kfree(nowner_sid_ptr); |
| 957 | kfree(ngroup_sid_ptr); |
| 958 | *secdesclen = offset; |
| 959 | return rc; |
| 960 | } |
| 961 | |
| 962 | static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 963 | u8 flags, __le32 access_req) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 964 | { |
| 965 | ace->type = type; |
| 966 | ace->flags = flags; |
| 967 | ace->access_req = access_req; |
| 968 | smb_copy_sid(&ace->sid, sid); |
| 969 | ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4)); |
| 970 | } |
| 971 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 972 | int smb_inherit_dacl(struct ksmbd_conn *conn, |
| 973 | struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 974 | unsigned int uid, unsigned int gid) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 975 | { |
| 976 | const struct smb_sid *psid, *creator = NULL; |
| 977 | struct smb_ace *parent_aces, *aces; |
| 978 | struct smb_acl *parent_pdacl; |
| 979 | struct smb_ntsd *parent_pntsd = NULL; |
| 980 | struct smb_sid owner_sid, group_sid; |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 981 | struct dentry *parent = path->dentry->d_parent; |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 982 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 983 | int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0; |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 984 | int rc = 0, num_aces, dacloffset, pntsd_type, acl_len; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 985 | char *aces_base; |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 986 | bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 987 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 988 | acl_len = ksmbd_vfs_get_sd_xattr(conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 989 | parent, &parent_pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 990 | if (acl_len <= 0) |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 991 | return -ENOENT; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 992 | dacloffset = le32_to_cpu(parent_pntsd->dacloffset); |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 993 | if (!dacloffset) { |
| 994 | rc = -EINVAL; |
| 995 | goto free_parent_pntsd; |
| 996 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 997 | |
| 998 | parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset); |
| 999 | num_aces = le32_to_cpu(parent_pdacl->num_aces); |
| 1000 | pntsd_type = le16_to_cpu(parent_pntsd->type); |
| 1001 | |
| 1002 | aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, GFP_KERNEL); |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 1003 | if (!aces_base) { |
| 1004 | rc = -ENOMEM; |
| 1005 | goto free_parent_pntsd; |
| 1006 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1007 | |
| 1008 | aces = (struct smb_ace *)aces_base; |
| 1009 | parent_aces = (struct smb_ace *)((char *)parent_pdacl + |
| 1010 | sizeof(struct smb_acl)); |
| 1011 | |
| 1012 | if (pntsd_type & DACL_AUTO_INHERITED) |
| 1013 | inherited_flags = INHERITED_ACE; |
| 1014 | |
| 1015 | for (i = 0; i < num_aces; i++) { |
| 1016 | flags = parent_aces->flags; |
| 1017 | if (!smb_inherit_flags(flags, is_dir)) |
| 1018 | goto pass; |
| 1019 | if (is_dir) { |
| 1020 | flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE); |
| 1021 | if (!(flags & CONTAINER_INHERIT_ACE)) |
| 1022 | flags |= INHERIT_ONLY_ACE; |
| 1023 | if (flags & NO_PROPAGATE_INHERIT_ACE) |
| 1024 | flags = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1025 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1026 | flags = 0; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1027 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1028 | |
| 1029 | if (!compare_sids(&creator_owner, &parent_aces->sid)) { |
| 1030 | creator = &creator_owner; |
| 1031 | id_to_sid(uid, SIDOWNER, &owner_sid); |
| 1032 | psid = &owner_sid; |
| 1033 | } else if (!compare_sids(&creator_group, &parent_aces->sid)) { |
| 1034 | creator = &creator_group; |
| 1035 | id_to_sid(gid, SIDUNIX_GROUP, &group_sid); |
| 1036 | psid = &group_sid; |
| 1037 | } else { |
| 1038 | creator = NULL; |
| 1039 | psid = &parent_aces->sid; |
| 1040 | } |
| 1041 | |
| 1042 | if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) { |
| 1043 | smb_set_ace(aces, psid, parent_aces->type, inherited_flags, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1044 | parent_aces->access_req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1045 | nt_size += le16_to_cpu(aces->size); |
| 1046 | ace_cnt++; |
| 1047 | aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size)); |
| 1048 | flags |= INHERIT_ONLY_ACE; |
| 1049 | psid = creator; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1050 | } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1051 | psid = &parent_aces->sid; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1052 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1053 | |
| 1054 | smb_set_ace(aces, psid, parent_aces->type, flags | inherited_flags, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1055 | parent_aces->access_req); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1056 | nt_size += le16_to_cpu(aces->size); |
| 1057 | aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size)); |
| 1058 | ace_cnt++; |
| 1059 | pass: |
| 1060 | parent_aces = |
| 1061 | (struct smb_ace *)((char *)parent_aces + le16_to_cpu(parent_aces->size)); |
| 1062 | } |
| 1063 | |
| 1064 | if (nt_size > 0) { |
| 1065 | struct smb_ntsd *pntsd; |
| 1066 | struct smb_acl *pdacl; |
| 1067 | struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL; |
| 1068 | int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size; |
| 1069 | |
| 1070 | if (parent_pntsd->osidoffset) { |
| 1071 | powner_sid = (struct smb_sid *)((char *)parent_pntsd + |
| 1072 | le32_to_cpu(parent_pntsd->osidoffset)); |
| 1073 | powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4); |
| 1074 | } |
| 1075 | if (parent_pntsd->gsidoffset) { |
| 1076 | pgroup_sid = (struct smb_sid *)((char *)parent_pntsd + |
| 1077 | le32_to_cpu(parent_pntsd->gsidoffset)); |
| 1078 | pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4); |
| 1079 | } |
| 1080 | |
| 1081 | pntsd = kzalloc(sizeof(struct smb_ntsd) + powner_sid_size + |
| 1082 | pgroup_sid_size + sizeof(struct smb_acl) + |
| 1083 | nt_size, GFP_KERNEL); |
| 1084 | if (!pntsd) { |
| 1085 | rc = -ENOMEM; |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 1086 | goto free_aces_base; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | pntsd->revision = cpu_to_le16(1); |
| 1090 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT); |
| 1091 | if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED) |
| 1092 | pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED); |
| 1093 | pntsd_size = sizeof(struct smb_ntsd); |
| 1094 | pntsd->osidoffset = parent_pntsd->osidoffset; |
| 1095 | pntsd->gsidoffset = parent_pntsd->gsidoffset; |
| 1096 | pntsd->dacloffset = parent_pntsd->dacloffset; |
| 1097 | |
| 1098 | if (pntsd->osidoffset) { |
| 1099 | struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd + |
| 1100 | le32_to_cpu(pntsd->osidoffset)); |
| 1101 | memcpy(owner_sid, powner_sid, powner_sid_size); |
| 1102 | pntsd_size += powner_sid_size; |
| 1103 | } |
| 1104 | |
| 1105 | if (pntsd->gsidoffset) { |
| 1106 | struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd + |
| 1107 | le32_to_cpu(pntsd->gsidoffset)); |
| 1108 | memcpy(group_sid, pgroup_sid, pgroup_sid_size); |
| 1109 | pntsd_size += pgroup_sid_size; |
| 1110 | } |
| 1111 | |
| 1112 | if (pntsd->dacloffset) { |
| 1113 | struct smb_ace *pace; |
| 1114 | |
| 1115 | pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset)); |
| 1116 | pdacl->revision = cpu_to_le16(2); |
| 1117 | pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size); |
| 1118 | pdacl->num_aces = cpu_to_le32(ace_cnt); |
| 1119 | pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1120 | memcpy(pace, aces_base, nt_size); |
| 1121 | pntsd_size += sizeof(struct smb_acl) + nt_size; |
| 1122 | } |
| 1123 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1124 | ksmbd_vfs_set_sd_xattr(conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 1125 | path->dentry, pntsd, pntsd_size); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1126 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1127 | } |
| 1128 | |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 1129 | free_aces_base: |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1130 | kfree(aces_base); |
Namjae Jeon | a9071e3 | 2021-07-07 15:01:21 +0900 | [diff] [blame] | 1131 | free_parent_pntsd: |
| 1132 | kfree(parent_pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1133 | return rc; |
| 1134 | } |
| 1135 | |
| 1136 | bool smb_inherit_flags(int flags, bool is_dir) |
| 1137 | { |
| 1138 | if (!is_dir) |
| 1139 | return (flags & OBJECT_INHERIT_ACE) != 0; |
| 1140 | |
| 1141 | if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE)) |
| 1142 | return true; |
| 1143 | |
| 1144 | if (flags & CONTAINER_INHERIT_ACE) |
| 1145 | return true; |
| 1146 | return false; |
| 1147 | } |
| 1148 | |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 1149 | int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1150 | __le32 *pdaccess, int uid) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1151 | { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1152 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1153 | struct smb_ntsd *pntsd = NULL; |
| 1154 | struct smb_acl *pdacl; |
| 1155 | struct posix_acl *posix_acls; |
| 1156 | int rc = 0, acl_size; |
| 1157 | struct smb_sid sid; |
| 1158 | int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE); |
| 1159 | struct smb_ace *ace; |
| 1160 | int i, found = 0; |
| 1161 | unsigned int access_bits = 0; |
| 1162 | struct smb_ace *others_ace = NULL; |
| 1163 | struct posix_acl_entry *pa_entry; |
| 1164 | unsigned int sid_type = SIDOWNER; |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1165 | char *end_of_acl; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1166 | |
| 1167 | ksmbd_debug(SMB, "check permission using windows acl\n"); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1168 | acl_size = ksmbd_vfs_get_sd_xattr(conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 1169 | path->dentry, &pntsd); |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1170 | if (acl_size <= 0 || !pntsd || !pntsd->dacloffset) { |
| 1171 | kfree(pntsd); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1172 | return 0; |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1173 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1174 | |
| 1175 | pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset)); |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1176 | end_of_acl = ((char *)pntsd) + acl_size; |
| 1177 | if (end_of_acl <= (char *)pdacl) { |
| 1178 | kfree(pntsd); |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size) || |
| 1183 | le16_to_cpu(pdacl->size) < sizeof(struct smb_acl)) { |
| 1184 | kfree(pntsd); |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1188 | if (!pdacl->num_aces) { |
| 1189 | if (!(le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) && |
| 1190 | *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) { |
| 1191 | rc = -EACCES; |
| 1192 | goto err_out; |
| 1193 | } |
| 1194 | kfree(pntsd); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) { |
| 1199 | granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES | |
| 1200 | DELETE; |
| 1201 | |
| 1202 | ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1203 | for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) { |
| 1204 | granted |= le32_to_cpu(ace->access_req); |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1205 | ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1206 | if (end_of_acl < (char *)ace) |
| 1207 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | if (!pdacl->num_aces) |
| 1211 | granted = GENERIC_ALL_FLAGS; |
| 1212 | } |
| 1213 | |
| 1214 | if (!uid) |
| 1215 | sid_type = SIDUNIX_USER; |
| 1216 | id_to_sid(uid, sid_type, &sid); |
| 1217 | |
| 1218 | ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1219 | for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) { |
| 1220 | if (!compare_sids(&sid, &ace->sid) || |
| 1221 | !compare_sids(&sid_unix_NFS_mode, &ace->sid)) { |
| 1222 | found = 1; |
| 1223 | break; |
| 1224 | } |
| 1225 | if (!compare_sids(&sid_everyone, &ace->sid)) |
| 1226 | others_ace = ace; |
| 1227 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1228 | ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); |
Namjae Jeon | 50355b0 | 2021-03-19 13:52:12 +0900 | [diff] [blame] | 1229 | if (end_of_acl < (char *)ace) |
| 1230 | goto err_out; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) { |
| 1234 | granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES | |
| 1235 | DELETE; |
| 1236 | |
| 1237 | granted |= le32_to_cpu(ace->access_req); |
| 1238 | |
| 1239 | if (!pdacl->num_aces) |
| 1240 | granted = GENERIC_ALL_FLAGS; |
| 1241 | } |
| 1242 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1243 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 1244 | posix_acls = get_acl(d_inode(path->dentry), ACL_TYPE_ACCESS); |
| 1245 | if (posix_acls && !found) { |
| 1246 | unsigned int id = -1; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1247 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1248 | pa_entry = posix_acls->a_entries; |
| 1249 | for (i = 0; i < posix_acls->a_count; i++, pa_entry++) { |
| 1250 | if (pa_entry->e_tag == ACL_USER) |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 1251 | id = posix_acl_uid_translate(user_ns, pa_entry); |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1252 | else if (pa_entry->e_tag == ACL_GROUP) |
Christian Brauner | 0e844ef | 2021-08-23 17:13:51 +0200 | [diff] [blame] | 1253 | id = posix_acl_gid_translate(user_ns, pa_entry); |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1254 | else |
| 1255 | continue; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1256 | |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1257 | if (id == uid) { |
| 1258 | mode_to_access_flags(pa_entry->e_perm, |
| 1259 | 0777, |
| 1260 | &access_bits); |
| 1261 | if (!access_bits) |
| 1262 | access_bits = |
| 1263 | SET_MINIMUM_RIGHTS; |
| 1264 | goto check_access_bits; |
| 1265 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1266 | } |
| 1267 | } |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1268 | if (posix_acls) |
| 1269 | posix_acl_release(posix_acls); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1270 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1271 | |
| 1272 | if (!found) { |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1273 | if (others_ace) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1274 | ace = others_ace; |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1275 | } else { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1276 | ksmbd_debug(SMB, "Can't find corresponding sid\n"); |
| 1277 | rc = -EACCES; |
| 1278 | goto err_out; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | switch (ace->type) { |
| 1283 | case ACCESS_ALLOWED_ACE_TYPE: |
| 1284 | access_bits = le32_to_cpu(ace->access_req); |
| 1285 | break; |
| 1286 | case ACCESS_DENIED_ACE_TYPE: |
| 1287 | case ACCESS_DENIED_CALLBACK_ACE_TYPE: |
| 1288 | access_bits = le32_to_cpu(~ace->access_req); |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | check_access_bits: |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1293 | if (granted & |
| 1294 | ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1295 | ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n", |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1296 | granted, le32_to_cpu(ace->access_req)); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1297 | rc = -EACCES; |
| 1298 | goto err_out; |
| 1299 | } |
| 1300 | |
| 1301 | *pdaccess = cpu_to_le32(granted); |
| 1302 | err_out: |
| 1303 | kfree(pntsd); |
| 1304 | return rc; |
| 1305 | } |
| 1306 | |
| 1307 | int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon, |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 1308 | struct path *path, struct smb_ntsd *pntsd, int ntsd_len, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 1309 | bool type_check) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1310 | { |
| 1311 | int rc; |
| 1312 | struct smb_fattr fattr = {{0}}; |
Hyunchul Lee | ef24c96 | 2021-06-30 18:25:52 +0900 | [diff] [blame] | 1313 | struct inode *inode = d_inode(path->dentry); |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1314 | struct user_namespace *user_ns = mnt_user_ns(path->mnt); |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 1315 | struct iattr newattrs; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1316 | |
| 1317 | fattr.cf_uid = INVALID_UID; |
| 1318 | fattr.cf_gid = INVALID_GID; |
| 1319 | fattr.cf_mode = inode->i_mode; |
| 1320 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1321 | rc = parse_sec_desc(user_ns, pntsd, ntsd_len, &fattr); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1322 | if (rc) |
| 1323 | goto out; |
| 1324 | |
Namjae Jeon | e70e392 | 2021-08-21 23:26:01 +0900 | [diff] [blame] | 1325 | newattrs.ia_valid = ATTR_CTIME; |
| 1326 | if (!uid_eq(fattr.cf_uid, INVALID_UID)) { |
| 1327 | newattrs.ia_valid |= ATTR_UID; |
| 1328 | newattrs.ia_uid = fattr.cf_uid; |
| 1329 | } |
| 1330 | if (!gid_eq(fattr.cf_gid, INVALID_GID)) { |
| 1331 | newattrs.ia_valid |= ATTR_GID; |
| 1332 | newattrs.ia_gid = fattr.cf_gid; |
| 1333 | } |
| 1334 | newattrs.ia_valid |= ATTR_MODE; |
| 1335 | newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777); |
| 1336 | |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1337 | ksmbd_vfs_remove_acl_xattrs(user_ns, path->dentry); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1338 | /* Update posix acls */ |
Namjae Jeon | 777cad1 | 2021-08-13 08:15:33 +0900 | [diff] [blame] | 1339 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1340 | rc = set_posix_acl(user_ns, inode, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 1341 | ACL_TYPE_ACCESS, fattr.cf_acls); |
Christian Brauner | 28a5d3d | 2021-08-26 17:05:17 +0900 | [diff] [blame] | 1342 | if (rc < 0) |
| 1343 | ksmbd_debug(SMB, |
| 1344 | "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n", |
| 1345 | rc); |
| 1346 | if (S_ISDIR(inode->i_mode) && fattr.cf_dacls) { |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1347 | rc = set_posix_acl(user_ns, inode, |
Namjae Jeon | 67d1c43 | 2021-06-22 11:42:29 +0900 | [diff] [blame] | 1348 | ACL_TYPE_DEFAULT, fattr.cf_dacls); |
Christian Brauner | 28a5d3d | 2021-08-26 17:05:17 +0900 | [diff] [blame] | 1349 | if (rc) |
| 1350 | ksmbd_debug(SMB, |
| 1351 | "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n", |
| 1352 | rc); |
| 1353 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1354 | } |
| 1355 | |
Christian Brauner | 28a5d3d | 2021-08-26 17:05:17 +0900 | [diff] [blame] | 1356 | inode_lock(inode); |
| 1357 | rc = notify_change(user_ns, path->dentry, &newattrs, NULL); |
| 1358 | inode_unlock(inode); |
| 1359 | if (rc) |
| 1360 | goto out; |
| 1361 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1362 | /* Check it only calling from SD BUFFER context */ |
| 1363 | if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT)) |
| 1364 | goto out; |
| 1365 | |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 1366 | if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1367 | /* Update WinACL in xattr */ |
Hyunchul Lee | 465d720 | 2021-07-03 12:10:36 +0900 | [diff] [blame] | 1368 | ksmbd_vfs_remove_sd_xattrs(user_ns, path->dentry); |
| 1369 | ksmbd_vfs_set_sd_xattr(conn, user_ns, |
Hyunchul Lee | af34983 | 2021-06-30 18:25:53 +0900 | [diff] [blame] | 1370 | path->dentry, pntsd, ntsd_len); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | out: |
| 1374 | posix_acl_release(fattr.cf_acls); |
| 1375 | posix_acl_release(fattr.cf_dacls); |
| 1376 | mark_inode_dirty(inode); |
| 1377 | return rc; |
| 1378 | } |
| 1379 | |
| 1380 | void ksmbd_init_domain(u32 *sub_auth) |
| 1381 | { |
| 1382 | int i; |
| 1383 | |
| 1384 | memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid)); |
| 1385 | for (i = 0; i < 3; ++i) |
| 1386 | server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]); |
| 1387 | } |