| 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> |
| 12 | #include <linux/mnt_idmapping.h> |
| 13 | |
| 14 | #include "smbacl.h" |
| 15 | #include "smb_common.h" |
| 16 | #include "server.h" |
| 17 | #include "misc.h" |
| 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 | */ |
| 71 | int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid) |
| 72 | { |
| 73 | int i; |
| 74 | int num_subauth, num_sat, num_saw; |
| 75 | |
| 76 | if (!ctsid || !cwsid) |
| 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 = min(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]) > |
| 105 | le32_to_cpu(cwsid->sub_auth[i])) |
| 106 | return 1; |
| 107 | else |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 0; /* sids compare/match */ |
| 114 | } |
| 115 | |
| 116 | static void smb_copy_sid(struct smb_sid *dst, const struct smb_sid *src) |
| 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, |
| 134 | int type) |
| 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 | |
| 145 | if ((flags & GENERIC_READ) || (flags & FILE_READ_RIGHTS)) |
| 146 | mode = 0444; |
| 147 | if ((flags & GENERIC_WRITE) || (flags & FILE_WRITE_RIGHTS)) { |
| 148 | mode |= 0222; |
| 149 | if (S_ISDIR(fattr->cf_mode)) |
| 150 | mode |= 0111; |
| 151 | } |
| 152 | if ((flags & GENERIC_EXECUTE) || (flags & FILE_EXEC_RIGHTS)) |
| 153 | mode |= 0111; |
| 154 | |
| 155 | if (type == ACCESS_DENIED_ACE_TYPE || type == ACCESS_DENIED_OBJECT_ACE_TYPE) |
| 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, |
| 169 | __u32 *pace_flags) |
| 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" , |
| 190 | mode, *pace_flags); |
| 191 | } |
| 192 | |
| 193 | static __u16 fill_ace_for_sid(struct smb_ace *pntace, |
| 194 | const struct smb_sid *psid, int type, int flags, |
| 195 | umode_t mode, umode_t bits) |
| 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_to_use: bits, pace_flags: &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(dst: ssid, src: &server_conf.domain_sid); |
| 226 | break; |
| 227 | case SIDUNIX_USER: |
| 228 | smb_copy_sid(dst: ssid, src: &sid_unix_users); |
| 229 | break; |
| 230 | case SIDUNIX_GROUP: |
| 231 | smb_copy_sid(dst: ssid, src: &sid_unix_groups); |
| 232 | break; |
| 233 | case SIDCREATOR_OWNER: |
| 234 | smb_copy_sid(dst: ssid, src: &creator_owner); |
| 235 | return; |
| 236 | case SIDCREATOR_GROUP: |
| 237 | smb_copy_sid(dst: ssid, src: &creator_group); |
| 238 | return; |
| 239 | case SIDNFS_USER: |
| 240 | smb_copy_sid(dst: ssid, src: &sid_unix_NFS_users); |
| 241 | break; |
| 242 | case SIDNFS_GROUP: |
| 243 | smb_copy_sid(dst: ssid, src: &sid_unix_NFS_groups); |
| 244 | break; |
| 245 | case SIDNFS_MODE: |
| 246 | smb_copy_sid(dst: ssid, src: &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 | |
| 257 | static int sid_to_id(struct mnt_idmap *idmap, |
| 258 | struct smb_sid *psid, uint sidtype, |
| 259 | struct smb_fattr *fattr) |
| 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)) { |
| 268 | pr_err("%s: %u subauthorities is too many!\n" , |
| 269 | __func__, psid->num_subauth); |
| 270 | return -EIO; |
| 271 | } |
| 272 | |
| 273 | if (psid->num_subauth == 0) { |
| 274 | pr_err("%s: zero subauthorities!\n" , __func__); |
| 275 | return -EIO; |
| 276 | } |
| 277 | |
| 278 | if (sidtype == SIDOWNER) { |
| 279 | kuid_t uid; |
| 280 | uid_t id; |
| 281 | |
| 282 | id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]); |
| 283 | uid = KUIDT_INIT(id); |
| 284 | uid = from_vfsuid(idmap, fs_userns: &init_user_ns, VFSUIDT_INIT(uid)); |
| 285 | if (uid_valid(uid)) { |
| 286 | fattr->cf_uid = uid; |
| 287 | rc = 0; |
| 288 | } |
| 289 | } else { |
| 290 | kgid_t gid; |
| 291 | gid_t id; |
| 292 | |
| 293 | id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]); |
| 294 | gid = KGIDT_INIT(id); |
| 295 | gid = from_vfsgid(idmap, fs_userns: &init_user_ns, VFSGIDT_INIT(gid)); |
| 296 | if (gid_valid(gid)) { |
| 297 | fattr->cf_gid = gid; |
| 298 | rc = 0; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | void posix_state_to_acl(struct posix_acl_state *state, |
| 306 | struct posix_acl_entry *pace) |
| 307 | { |
| 308 | int i; |
| 309 | |
| 310 | pace->e_tag = ACL_USER_OBJ; |
| 311 | pace->e_perm = state->owner.allow; |
| 312 | for (i = 0; i < state->users->n; i++) { |
| 313 | pace++; |
| 314 | pace->e_tag = ACL_USER; |
| 315 | pace->e_uid = state->users->aces[i].uid; |
| 316 | pace->e_perm = state->users->aces[i].perms.allow; |
| 317 | } |
| 318 | |
| 319 | pace++; |
| 320 | pace->e_tag = ACL_GROUP_OBJ; |
| 321 | pace->e_perm = state->group.allow; |
| 322 | |
| 323 | for (i = 0; i < state->groups->n; i++) { |
| 324 | pace++; |
| 325 | pace->e_tag = ACL_GROUP; |
| 326 | pace->e_gid = state->groups->aces[i].gid; |
| 327 | pace->e_perm = state->groups->aces[i].perms.allow; |
| 328 | } |
| 329 | |
| 330 | if (state->users->n || state->groups->n) { |
| 331 | pace++; |
| 332 | pace->e_tag = ACL_MASK; |
| 333 | pace->e_perm = state->mask.allow; |
| 334 | } |
| 335 | |
| 336 | pace++; |
| 337 | pace->e_tag = ACL_OTHER; |
| 338 | pace->e_perm = state->other.allow; |
| 339 | } |
| 340 | |
| 341 | int init_acl_state(struct posix_acl_state *state, u16 cnt) |
| 342 | { |
| 343 | int alloc; |
| 344 | |
| 345 | memset(state, 0, sizeof(struct posix_acl_state)); |
| 346 | /* |
| 347 | * In the worst case, each individual acl could be for a distinct |
| 348 | * named user or group, but we don't know which, so we allocate |
| 349 | * enough space for either: |
| 350 | */ |
| 351 | alloc = sizeof(struct posix_ace_state_array) |
| 352 | + cnt * sizeof(struct posix_user_ace_state); |
| 353 | state->users = kzalloc(alloc, KSMBD_DEFAULT_GFP); |
| 354 | if (!state->users) |
| 355 | return -ENOMEM; |
| 356 | state->groups = kzalloc(alloc, KSMBD_DEFAULT_GFP); |
| 357 | if (!state->groups) { |
| 358 | kfree(objp: state->users); |
| 359 | return -ENOMEM; |
| 360 | } |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | void free_acl_state(struct posix_acl_state *state) |
| 365 | { |
| 366 | kfree(objp: state->users); |
| 367 | kfree(objp: state->groups); |
| 368 | } |
| 369 | |
| 370 | static void parse_dacl(struct mnt_idmap *idmap, |
| 371 | struct smb_acl *pdacl, char *end_of_acl, |
| 372 | struct smb_sid *pownersid, struct smb_sid *pgrpsid, |
| 373 | struct smb_fattr *fattr) |
| 374 | { |
| 375 | int i, ret; |
| 376 | u16 num_aces = 0; |
| 377 | unsigned int acl_size; |
| 378 | char *acl_base; |
| 379 | struct smb_ace **ppace; |
| 380 | struct posix_acl_entry *cf_pace, *cf_pdace; |
| 381 | struct posix_acl_state acl_state, default_acl_state; |
| 382 | umode_t mode = 0, acl_mode; |
| 383 | bool owner_found = false, group_found = false, others_found = false; |
| 384 | |
| 385 | if (!pdacl) |
| 386 | return; |
| 387 | |
| 388 | /* validate that we do not go past end of acl */ |
| 389 | if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) || |
| 390 | end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
| 391 | pr_err("ACL too small to parse DACL\n" ); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n" , |
| 396 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
| 397 | le16_to_cpu(pdacl->num_aces)); |
| 398 | |
| 399 | acl_base = (char *)pdacl; |
| 400 | acl_size = sizeof(struct smb_acl); |
| 401 | |
| 402 | num_aces = le16_to_cpu(pdacl->num_aces); |
| 403 | if (num_aces <= 0) |
| 404 | return; |
| 405 | |
| 406 | if (num_aces > (le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) / |
| 407 | (offsetof(struct smb_ace, sid) + |
| 408 | offsetof(struct smb_sid, sub_auth) + sizeof(__le16))) |
| 409 | return; |
| 410 | |
| 411 | ret = init_acl_state(state: &acl_state, cnt: num_aces); |
| 412 | if (ret) |
| 413 | return; |
| 414 | ret = init_acl_state(state: &default_acl_state, cnt: num_aces); |
| 415 | if (ret) { |
| 416 | free_acl_state(state: &acl_state); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), KSMBD_DEFAULT_GFP); |
| 421 | if (!ppace) { |
| 422 | free_acl_state(state: &default_acl_state); |
| 423 | free_acl_state(state: &acl_state); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * reset rwx permissions for user/group/other. |
| 429 | * Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 430 | * user/group/other have no permissions |
| 431 | */ |
| 432 | for (i = 0; i < num_aces; ++i) { |
| 433 | if (end_of_acl - acl_base < acl_size) |
| 434 | break; |
| 435 | |
| 436 | ppace[i] = (struct smb_ace *)(acl_base + acl_size); |
| 437 | acl_base = (char *)ppace[i]; |
| 438 | acl_size = offsetof(struct smb_ace, sid) + |
| 439 | offsetof(struct smb_sid, sub_auth); |
| 440 | |
| 441 | if (end_of_acl - acl_base < acl_size || |
| 442 | ppace[i]->sid.num_subauth == 0 || |
| 443 | ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES || |
| 444 | (end_of_acl - acl_base < |
| 445 | acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) || |
| 446 | (le16_to_cpu(ppace[i]->size) < |
| 447 | acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth)) |
| 448 | break; |
| 449 | |
| 450 | acl_size = le16_to_cpu(ppace[i]->size); |
| 451 | ppace[i]->access_req = |
| 452 | smb_map_generic_desired_access(daccess: ppace[i]->access_req); |
| 453 | |
| 454 | if (!(compare_sids(ctsid: &ppace[i]->sid, cwsid: &sid_unix_NFS_mode))) { |
| 455 | fattr->cf_mode = |
| 456 | le32_to_cpu(ppace[i]->sid.sub_auth[2]); |
| 457 | break; |
| 458 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: pownersid)) { |
| 459 | acl_mode = access_flags_to_mode(fattr, |
| 460 | ace_flags: ppace[i]->access_req, |
| 461 | type: ppace[i]->type); |
| 462 | acl_mode &= 0700; |
| 463 | |
| 464 | if (!owner_found) { |
| 465 | mode &= ~(0700); |
| 466 | mode |= acl_mode; |
| 467 | } |
| 468 | owner_found = true; |
| 469 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: pgrpsid) || |
| 470 | ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] == |
| 471 | DOMAIN_USER_RID_LE) { |
| 472 | acl_mode = access_flags_to_mode(fattr, |
| 473 | ace_flags: ppace[i]->access_req, |
| 474 | type: ppace[i]->type); |
| 475 | acl_mode &= 0070; |
| 476 | if (!group_found) { |
| 477 | mode &= ~(0070); |
| 478 | mode |= acl_mode; |
| 479 | } |
| 480 | group_found = true; |
| 481 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: &sid_everyone)) { |
| 482 | acl_mode = access_flags_to_mode(fattr, |
| 483 | ace_flags: ppace[i]->access_req, |
| 484 | type: ppace[i]->type); |
| 485 | acl_mode &= 0007; |
| 486 | if (!others_found) { |
| 487 | mode &= ~(0007); |
| 488 | mode |= acl_mode; |
| 489 | } |
| 490 | others_found = true; |
| 491 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: &creator_owner)) { |
| 492 | continue; |
| 493 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: &creator_group)) { |
| 494 | continue; |
| 495 | } else if (!compare_sids(ctsid: &ppace[i]->sid, cwsid: &sid_authusers)) { |
| 496 | continue; |
| 497 | } else { |
| 498 | struct smb_fattr temp_fattr; |
| 499 | |
| 500 | acl_mode = access_flags_to_mode(fattr, ace_flags: ppace[i]->access_req, |
| 501 | type: ppace[i]->type); |
| 502 | temp_fattr.cf_uid = INVALID_UID; |
| 503 | ret = sid_to_id(idmap, psid: &ppace[i]->sid, sidtype: SIDOWNER, fattr: &temp_fattr); |
| 504 | if (ret || uid_eq(left: temp_fattr.cf_uid, INVALID_UID)) { |
| 505 | pr_err("%s: Error %d mapping Owner SID to uid\n" , |
| 506 | __func__, ret); |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004; |
| 511 | acl_state.users->aces[acl_state.users->n].uid = |
| 512 | temp_fattr.cf_uid; |
| 513 | acl_state.users->aces[acl_state.users->n++].perms.allow = |
| 514 | ((acl_mode & 0700) >> 6) | 0004; |
| 515 | default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004; |
| 516 | default_acl_state.users->aces[default_acl_state.users->n].uid = |
| 517 | temp_fattr.cf_uid; |
| 518 | default_acl_state.users->aces[default_acl_state.users->n++].perms.allow = |
| 519 | ((acl_mode & 0700) >> 6) | 0004; |
| 520 | } |
| 521 | } |
| 522 | kfree(objp: ppace); |
| 523 | |
| 524 | if (owner_found) { |
| 525 | /* The owner must be set to at least read-only. */ |
| 526 | acl_state.owner.allow = ((mode & 0700) >> 6) | 0004; |
| 527 | acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid; |
| 528 | acl_state.users->aces[acl_state.users->n++].perms.allow = |
| 529 | ((mode & 0700) >> 6) | 0004; |
| 530 | default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004; |
| 531 | default_acl_state.users->aces[default_acl_state.users->n].uid = |
| 532 | fattr->cf_uid; |
| 533 | default_acl_state.users->aces[default_acl_state.users->n++].perms.allow = |
| 534 | ((mode & 0700) >> 6) | 0004; |
| 535 | } |
| 536 | |
| 537 | if (group_found) { |
| 538 | acl_state.group.allow = (mode & 0070) >> 3; |
| 539 | acl_state.groups->aces[acl_state.groups->n].gid = |
| 540 | fattr->cf_gid; |
| 541 | acl_state.groups->aces[acl_state.groups->n++].perms.allow = |
| 542 | (mode & 0070) >> 3; |
| 543 | default_acl_state.group.allow = (mode & 0070) >> 3; |
| 544 | default_acl_state.groups->aces[default_acl_state.groups->n].gid = |
| 545 | fattr->cf_gid; |
| 546 | default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow = |
| 547 | (mode & 0070) >> 3; |
| 548 | } |
| 549 | |
| 550 | if (others_found) { |
| 551 | fattr->cf_mode &= ~(0007); |
| 552 | fattr->cf_mode |= mode & 0007; |
| 553 | |
| 554 | acl_state.other.allow = mode & 0007; |
| 555 | default_acl_state.other.allow = mode & 0007; |
| 556 | } |
| 557 | |
| 558 | if (acl_state.users->n || acl_state.groups->n) { |
| 559 | acl_state.mask.allow = 0x07; |
| 560 | |
| 561 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 562 | fattr->cf_acls = |
| 563 | posix_acl_alloc(count: acl_state.users->n + |
| 564 | acl_state.groups->n + 4, KSMBD_DEFAULT_GFP); |
| 565 | if (fattr->cf_acls) { |
| 566 | cf_pace = fattr->cf_acls->a_entries; |
| 567 | posix_state_to_acl(state: &acl_state, pace: cf_pace); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (default_acl_state.users->n || default_acl_state.groups->n) { |
| 573 | default_acl_state.mask.allow = 0x07; |
| 574 | |
| 575 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 576 | fattr->cf_dacls = |
| 577 | posix_acl_alloc(count: default_acl_state.users->n + |
| 578 | default_acl_state.groups->n + 4, KSMBD_DEFAULT_GFP); |
| 579 | if (fattr->cf_dacls) { |
| 580 | cf_pdace = fattr->cf_dacls->a_entries; |
| 581 | posix_state_to_acl(state: &default_acl_state, pace: cf_pdace); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | free_acl_state(state: &acl_state); |
| 586 | free_acl_state(state: &default_acl_state); |
| 587 | } |
| 588 | |
| 589 | static void set_posix_acl_entries_dacl(struct mnt_idmap *idmap, |
| 590 | struct smb_ace *pndace, |
| 591 | struct smb_fattr *fattr, u16 *num_aces, |
| 592 | u16 *size, u32 nt_aces_num) |
| 593 | { |
| 594 | struct posix_acl_entry *pace; |
| 595 | struct smb_sid *sid; |
| 596 | struct smb_ace *ntace; |
| 597 | int i, j; |
| 598 | |
| 599 | if (!fattr->cf_acls) |
| 600 | goto posix_default_acl; |
| 601 | |
| 602 | pace = fattr->cf_acls->a_entries; |
| 603 | for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) { |
| 604 | int flags = 0; |
| 605 | |
| 606 | sid = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP); |
| 607 | if (!sid) |
| 608 | break; |
| 609 | |
| 610 | if (pace->e_tag == ACL_USER) { |
| 611 | uid_t uid; |
| 612 | unsigned int sid_type = SIDOWNER; |
| 613 | |
| 614 | uid = posix_acl_uid_translate(idmap, pace); |
| 615 | if (!uid) |
| 616 | sid_type = SIDUNIX_USER; |
| 617 | id_to_sid(cid: uid, sidtype: sid_type, ssid: sid); |
| 618 | } else if (pace->e_tag == ACL_GROUP) { |
| 619 | gid_t gid; |
| 620 | |
| 621 | gid = posix_acl_gid_translate(idmap, pace); |
| 622 | id_to_sid(cid: gid, sidtype: SIDUNIX_GROUP, ssid: sid); |
| 623 | } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) { |
| 624 | smb_copy_sid(dst: sid, src: &sid_everyone); |
| 625 | } else { |
| 626 | kfree(objp: sid); |
| 627 | continue; |
| 628 | } |
| 629 | ntace = pndace; |
| 630 | for (j = 0; j < nt_aces_num; j++) { |
| 631 | if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] == |
| 632 | sid->sub_auth[sid->num_subauth - 1]) |
| 633 | goto pass_same_sid; |
| 634 | ntace = (struct smb_ace *)((char *)ntace + |
| 635 | le16_to_cpu(ntace->size)); |
| 636 | } |
| 637 | |
| 638 | if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER) |
| 639 | flags = 0x03; |
| 640 | |
| 641 | ntace = (struct smb_ace *)((char *)pndace + *size); |
| 642 | *size += fill_ace_for_sid(pntace: ntace, psid: sid, type: ACCESS_ALLOWED, flags, |
| 643 | mode: pace->e_perm, bits: 0777); |
| 644 | (*num_aces)++; |
| 645 | if (pace->e_tag == ACL_USER) |
| 646 | ntace->access_req |= |
| 647 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 648 | |
| 649 | if (S_ISDIR(fattr->cf_mode) && |
| 650 | (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) { |
| 651 | ntace = (struct smb_ace *)((char *)pndace + *size); |
| 652 | *size += fill_ace_for_sid(pntace: ntace, psid: sid, type: ACCESS_ALLOWED, |
| 653 | flags: 0x03, mode: pace->e_perm, bits: 0777); |
| 654 | (*num_aces)++; |
| 655 | if (pace->e_tag == ACL_USER) |
| 656 | ntace->access_req |= |
| 657 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 658 | } |
| 659 | |
| 660 | pass_same_sid: |
| 661 | kfree(objp: sid); |
| 662 | } |
| 663 | |
| 664 | if (nt_aces_num) |
| 665 | return; |
| 666 | |
| 667 | posix_default_acl: |
| 668 | if (!fattr->cf_dacls) |
| 669 | return; |
| 670 | |
| 671 | pace = fattr->cf_dacls->a_entries; |
| 672 | for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) { |
| 673 | sid = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP); |
| 674 | if (!sid) |
| 675 | break; |
| 676 | |
| 677 | if (pace->e_tag == ACL_USER) { |
| 678 | uid_t uid; |
| 679 | |
| 680 | uid = posix_acl_uid_translate(idmap, pace); |
| 681 | id_to_sid(cid: uid, sidtype: SIDCREATOR_OWNER, ssid: sid); |
| 682 | } else if (pace->e_tag == ACL_GROUP) { |
| 683 | gid_t gid; |
| 684 | |
| 685 | gid = posix_acl_gid_translate(idmap, pace); |
| 686 | id_to_sid(cid: gid, sidtype: SIDCREATOR_GROUP, ssid: sid); |
| 687 | } else { |
| 688 | kfree(objp: sid); |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | ntace = (struct smb_ace *)((char *)pndace + *size); |
| 693 | *size += fill_ace_for_sid(pntace: ntace, psid: sid, type: ACCESS_ALLOWED, flags: 0x0b, |
| 694 | mode: pace->e_perm, bits: 0777); |
| 695 | (*num_aces)++; |
| 696 | if (pace->e_tag == ACL_USER) |
| 697 | ntace->access_req |= |
| 698 | FILE_DELETE_LE | FILE_DELETE_CHILD_LE; |
| 699 | kfree(objp: sid); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | static void set_ntacl_dacl(struct mnt_idmap *idmap, |
| 704 | struct smb_acl *pndacl, |
| 705 | struct smb_acl *nt_dacl, |
| 706 | unsigned int aces_size, |
| 707 | const struct smb_sid *pownersid, |
| 708 | const struct smb_sid *pgrpsid, |
| 709 | struct smb_fattr *fattr) |
| 710 | { |
| 711 | struct smb_ace *ntace, *pndace; |
| 712 | u16 nt_num_aces = le16_to_cpu(nt_dacl->num_aces), num_aces = 0; |
| 713 | unsigned short size = 0; |
| 714 | int i; |
| 715 | |
| 716 | pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl)); |
| 717 | if (nt_num_aces) { |
| 718 | ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl)); |
| 719 | for (i = 0; i < nt_num_aces; i++) { |
| 720 | unsigned short nt_ace_size; |
| 721 | |
| 722 | if (offsetof(struct smb_ace, access_req) > aces_size) |
| 723 | break; |
| 724 | |
| 725 | nt_ace_size = le16_to_cpu(ntace->size); |
| 726 | if (nt_ace_size > aces_size) |
| 727 | break; |
| 728 | |
| 729 | memcpy((char *)pndace + size, ntace, nt_ace_size); |
| 730 | size += nt_ace_size; |
| 731 | aces_size -= nt_ace_size; |
| 732 | ntace = (struct smb_ace *)((char *)ntace + nt_ace_size); |
| 733 | num_aces++; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | set_posix_acl_entries_dacl(idmap, pndace, fattr, |
| 738 | num_aces: &num_aces, size: &size, nt_aces_num: nt_num_aces); |
| 739 | pndacl->num_aces = cpu_to_le16(num_aces); |
| 740 | pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size); |
| 741 | } |
| 742 | |
| 743 | static void set_mode_dacl(struct mnt_idmap *idmap, |
| 744 | struct smb_acl *pndacl, struct smb_fattr *fattr) |
| 745 | { |
| 746 | struct smb_ace *pace, *pndace; |
| 747 | u16 num_aces = 0; |
| 748 | u16 size = 0, ace_size = 0; |
| 749 | uid_t uid; |
| 750 | const struct smb_sid *sid; |
| 751 | |
| 752 | pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl)); |
| 753 | |
| 754 | if (fattr->cf_acls) { |
| 755 | set_posix_acl_entries_dacl(idmap, pndace, fattr, |
| 756 | num_aces: &num_aces, size: &size, nt_aces_num: num_aces); |
| 757 | goto out; |
| 758 | } |
| 759 | |
| 760 | /* owner RID */ |
| 761 | uid = from_kuid(to: &init_user_ns, uid: fattr->cf_uid); |
| 762 | if (uid) |
| 763 | sid = &server_conf.domain_sid; |
| 764 | else |
| 765 | sid = &sid_unix_users; |
| 766 | ace_size = fill_ace_for_sid(pntace: pace, psid: sid, type: ACCESS_ALLOWED, flags: 0, |
| 767 | mode: fattr->cf_mode, bits: 0700); |
| 768 | pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid); |
| 769 | pace->size = cpu_to_le16(ace_size + 4); |
| 770 | size += le16_to_cpu(pace->size); |
| 771 | pace = (struct smb_ace *)((char *)pndace + size); |
| 772 | |
| 773 | /* Group RID */ |
| 774 | ace_size = fill_ace_for_sid(pntace: pace, psid: &sid_unix_groups, |
| 775 | type: ACCESS_ALLOWED, flags: 0, mode: fattr->cf_mode, bits: 0070); |
| 776 | pace->sid.sub_auth[pace->sid.num_subauth++] = |
| 777 | cpu_to_le32(from_kgid(&init_user_ns, fattr->cf_gid)); |
| 778 | pace->size = cpu_to_le16(ace_size + 4); |
| 779 | size += le16_to_cpu(pace->size); |
| 780 | pace = (struct smb_ace *)((char *)pndace + size); |
| 781 | num_aces = 3; |
| 782 | |
| 783 | if (S_ISDIR(fattr->cf_mode)) { |
| 784 | pace = (struct smb_ace *)((char *)pndace + size); |
| 785 | |
| 786 | /* creator owner */ |
| 787 | size += fill_ace_for_sid(pntace: pace, psid: &creator_owner, type: ACCESS_ALLOWED, |
| 788 | flags: 0x0b, mode: fattr->cf_mode, bits: 0700); |
| 789 | pace = (struct smb_ace *)((char *)pndace + size); |
| 790 | |
| 791 | /* creator group */ |
| 792 | size += fill_ace_for_sid(pntace: pace, psid: &creator_group, type: ACCESS_ALLOWED, |
| 793 | flags: 0x0b, mode: fattr->cf_mode, bits: 0070); |
| 794 | pace = (struct smb_ace *)((char *)pndace + size); |
| 795 | num_aces = 5; |
| 796 | } |
| 797 | |
| 798 | /* other */ |
| 799 | size += fill_ace_for_sid(pntace: pace, psid: &sid_everyone, type: ACCESS_ALLOWED, flags: 0, |
| 800 | mode: fattr->cf_mode, bits: 0007); |
| 801 | |
| 802 | out: |
| 803 | pndacl->num_aces = cpu_to_le16(num_aces); |
| 804 | pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size); |
| 805 | } |
| 806 | |
| 807 | static int parse_sid(struct smb_sid *psid, char *end_of_acl) |
| 808 | { |
| 809 | /* |
| 810 | * validate that we do not go past end of ACL - sid must be at least 8 |
| 811 | * bytes long (assuming no sub-auths - e.g. the null SID |
| 812 | */ |
| 813 | if (end_of_acl < (char *)psid + 8) { |
| 814 | pr_err("ACL too small to parse SID %p\n" , psid); |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | |
| 818 | if (!psid->num_subauth) |
| 819 | return 0; |
| 820 | |
| 821 | if (psid->num_subauth > SID_MAX_SUB_AUTHORITIES || |
| 822 | end_of_acl < (char *)psid + 8 + sizeof(__le32) * psid->num_subauth) |
| 823 | return -EINVAL; |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | /* Convert CIFS ACL to POSIX form */ |
| 829 | int parse_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd, |
| 830 | int acl_len, struct smb_fattr *fattr) |
| 831 | { |
| 832 | int rc = 0; |
| 833 | struct smb_sid *owner_sid_ptr, *group_sid_ptr; |
| 834 | struct smb_acl *dacl_ptr; /* no need for SACL ptr */ |
| 835 | char *end_of_acl = ((char *)pntsd) + acl_len; |
| 836 | __u32 dacloffset; |
| 837 | int pntsd_type; |
| 838 | |
| 839 | if (!pntsd) |
| 840 | return -EIO; |
| 841 | |
| 842 | if (acl_len < sizeof(struct smb_ntsd)) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | owner_sid_ptr = (struct smb_sid *)((char *)pntsd + |
| 846 | le32_to_cpu(pntsd->osidoffset)); |
| 847 | group_sid_ptr = (struct smb_sid *)((char *)pntsd + |
| 848 | le32_to_cpu(pntsd->gsidoffset)); |
| 849 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 850 | dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset); |
| 851 | ksmbd_debug(SMB, |
| 852 | "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n" , |
| 853 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 854 | le32_to_cpu(pntsd->gsidoffset), |
| 855 | le32_to_cpu(pntsd->sacloffset), dacloffset); |
| 856 | |
| 857 | pntsd_type = le16_to_cpu(pntsd->type); |
| 858 | if (!(pntsd_type & DACL_PRESENT)) { |
| 859 | ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n" ); |
| 860 | return rc; |
| 861 | } |
| 862 | |
| 863 | pntsd->type = cpu_to_le16(DACL_PRESENT); |
| 864 | |
| 865 | if (pntsd->osidoffset) { |
| 866 | if (le32_to_cpu(pntsd->osidoffset) < sizeof(struct smb_ntsd)) |
| 867 | return -EINVAL; |
| 868 | |
| 869 | rc = parse_sid(psid: owner_sid_ptr, end_of_acl); |
| 870 | if (rc) { |
| 871 | pr_err("%s: Error %d parsing Owner SID\n" , __func__, rc); |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | rc = sid_to_id(idmap, psid: owner_sid_ptr, sidtype: SIDOWNER, fattr); |
| 876 | if (rc) { |
| 877 | pr_err("%s: Error %d mapping Owner SID to uid\n" , |
| 878 | __func__, rc); |
| 879 | owner_sid_ptr = NULL; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if (pntsd->gsidoffset) { |
| 884 | if (le32_to_cpu(pntsd->gsidoffset) < sizeof(struct smb_ntsd)) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | rc = parse_sid(psid: group_sid_ptr, end_of_acl); |
| 888 | if (rc) { |
| 889 | pr_err("%s: Error %d mapping Owner SID to gid\n" , |
| 890 | __func__, rc); |
| 891 | return rc; |
| 892 | } |
| 893 | rc = sid_to_id(idmap, psid: group_sid_ptr, sidtype: SIDUNIX_GROUP, fattr); |
| 894 | if (rc) { |
| 895 | pr_err("%s: Error %d mapping Group SID to gid\n" , |
| 896 | __func__, rc); |
| 897 | group_sid_ptr = NULL; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) == |
| 902 | (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) |
| 903 | pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED); |
| 904 | if (pntsd_type & DACL_PROTECTED) |
| 905 | pntsd->type |= cpu_to_le16(DACL_PROTECTED); |
| 906 | |
| 907 | if (dacloffset) { |
| 908 | if (dacloffset < sizeof(struct smb_ntsd)) |
| 909 | return -EINVAL; |
| 910 | |
| 911 | parse_dacl(idmap, pdacl: dacl_ptr, end_of_acl, |
| 912 | pownersid: owner_sid_ptr, pgrpsid: group_sid_ptr, fattr); |
| 913 | } |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
| 919 | int build_sec_desc(struct mnt_idmap *idmap, |
| 920 | struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd, |
| 921 | int ppntsd_size, int addition_info, __u32 *secdesclen, |
| 922 | struct smb_fattr *fattr) |
| 923 | { |
| 924 | int rc = 0; |
| 925 | __u32 offset; |
| 926 | struct smb_sid *owner_sid_ptr, *group_sid_ptr; |
| 927 | struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 928 | struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 929 | uid_t uid; |
| 930 | gid_t gid; |
| 931 | unsigned int sid_type = SIDOWNER; |
| 932 | |
| 933 | nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP); |
| 934 | if (!nowner_sid_ptr) |
| 935 | return -ENOMEM; |
| 936 | |
| 937 | uid = from_kuid(to: &init_user_ns, uid: fattr->cf_uid); |
| 938 | if (!uid) |
| 939 | sid_type = SIDUNIX_USER; |
| 940 | id_to_sid(cid: uid, sidtype: sid_type, ssid: nowner_sid_ptr); |
| 941 | |
| 942 | ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), KSMBD_DEFAULT_GFP); |
| 943 | if (!ngroup_sid_ptr) { |
| 944 | kfree(objp: nowner_sid_ptr); |
| 945 | return -ENOMEM; |
| 946 | } |
| 947 | |
| 948 | gid = from_kgid(to: &init_user_ns, gid: fattr->cf_gid); |
| 949 | id_to_sid(cid: gid, sidtype: SIDUNIX_GROUP, ssid: ngroup_sid_ptr); |
| 950 | |
| 951 | offset = sizeof(struct smb_ntsd); |
| 952 | pntsd->sacloffset = 0; |
| 953 | pntsd->revision = cpu_to_le16(1); |
| 954 | pntsd->type = cpu_to_le16(SELF_RELATIVE); |
| 955 | if (ppntsd) |
| 956 | pntsd->type |= ppntsd->type; |
| 957 | |
| 958 | if (addition_info & OWNER_SECINFO) { |
| 959 | pntsd->osidoffset = cpu_to_le32(offset); |
| 960 | owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset); |
| 961 | smb_copy_sid(dst: owner_sid_ptr, src: nowner_sid_ptr); |
| 962 | offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4); |
| 963 | } |
| 964 | |
| 965 | if (addition_info & GROUP_SECINFO) { |
| 966 | pntsd->gsidoffset = cpu_to_le32(offset); |
| 967 | group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset); |
| 968 | smb_copy_sid(dst: group_sid_ptr, src: ngroup_sid_ptr); |
| 969 | offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4); |
| 970 | } |
| 971 | |
| 972 | if (addition_info & DACL_SECINFO) { |
| 973 | pntsd->type |= cpu_to_le16(DACL_PRESENT); |
| 974 | dacl_ptr = (struct smb_acl *)((char *)pntsd + offset); |
| 975 | dacl_ptr->revision = cpu_to_le16(2); |
| 976 | dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl)); |
| 977 | dacl_ptr->num_aces = 0; |
| 978 | |
| 979 | if (!ppntsd) { |
| 980 | set_mode_dacl(idmap, pndacl: dacl_ptr, fattr); |
| 981 | } else { |
| 982 | struct smb_acl *ppdacl_ptr; |
| 983 | unsigned int dacl_offset = le32_to_cpu(ppntsd->dacloffset); |
| 984 | int ppdacl_size, ntacl_size = ppntsd_size - dacl_offset; |
| 985 | |
| 986 | if (!dacl_offset || |
| 987 | (dacl_offset + sizeof(struct smb_acl) > ppntsd_size)) |
| 988 | goto out; |
| 989 | |
| 990 | ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + dacl_offset); |
| 991 | ppdacl_size = le16_to_cpu(ppdacl_ptr->size); |
| 992 | if (ppdacl_size > ntacl_size || |
| 993 | ppdacl_size < sizeof(struct smb_acl)) |
| 994 | goto out; |
| 995 | |
| 996 | set_ntacl_dacl(idmap, pndacl: dacl_ptr, nt_dacl: ppdacl_ptr, |
| 997 | aces_size: ntacl_size - sizeof(struct smb_acl), |
| 998 | pownersid: nowner_sid_ptr, pgrpsid: ngroup_sid_ptr, |
| 999 | fattr); |
| 1000 | } |
| 1001 | pntsd->dacloffset = cpu_to_le32(offset); |
| 1002 | offset += le16_to_cpu(dacl_ptr->size); |
| 1003 | } |
| 1004 | |
| 1005 | out: |
| 1006 | kfree(objp: nowner_sid_ptr); |
| 1007 | kfree(objp: ngroup_sid_ptr); |
| 1008 | *secdesclen = offset; |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type, |
| 1013 | u8 flags, __le32 access_req) |
| 1014 | { |
| 1015 | ace->type = type; |
| 1016 | ace->flags = flags; |
| 1017 | ace->access_req = access_req; |
| 1018 | smb_copy_sid(dst: &ace->sid, src: sid); |
| 1019 | ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4)); |
| 1020 | } |
| 1021 | |
| 1022 | int smb_inherit_dacl(struct ksmbd_conn *conn, |
| 1023 | const struct path *path, |
| 1024 | unsigned int uid, unsigned int gid) |
| 1025 | { |
| 1026 | const struct smb_sid *psid, *creator = NULL; |
| 1027 | struct smb_ace *parent_aces, *aces; |
| 1028 | struct smb_acl *parent_pdacl; |
| 1029 | struct smb_ntsd *parent_pntsd = NULL; |
| 1030 | struct smb_sid owner_sid, group_sid; |
| 1031 | struct dentry *parent = path->dentry->d_parent; |
| 1032 | struct mnt_idmap *idmap = mnt_idmap(mnt: path->mnt); |
| 1033 | int inherited_flags = 0, flags = 0, i, nt_size = 0, pdacl_size; |
| 1034 | int rc = 0, pntsd_type, pntsd_size, acl_len, aces_size; |
| 1035 | unsigned int dacloffset; |
| 1036 | size_t dacl_struct_end; |
| 1037 | u16 num_aces, ace_cnt = 0; |
| 1038 | char *aces_base; |
| 1039 | bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode); |
| 1040 | |
| 1041 | pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap, |
| 1042 | dentry: parent, pntsd: &parent_pntsd); |
| 1043 | if (pntsd_size <= 0) |
| 1044 | return -ENOENT; |
| 1045 | |
| 1046 | dacloffset = le32_to_cpu(parent_pntsd->dacloffset); |
| 1047 | if (!dacloffset || |
| 1048 | check_add_overflow(dacloffset, sizeof(struct smb_acl), &dacl_struct_end) || |
| 1049 | dacl_struct_end > (size_t)pntsd_size) { |
| 1050 | rc = -EINVAL; |
| 1051 | goto free_parent_pntsd; |
| 1052 | } |
| 1053 | |
| 1054 | parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset); |
| 1055 | acl_len = pntsd_size - dacloffset; |
| 1056 | num_aces = le16_to_cpu(parent_pdacl->num_aces); |
| 1057 | pntsd_type = le16_to_cpu(parent_pntsd->type); |
| 1058 | pdacl_size = le16_to_cpu(parent_pdacl->size); |
| 1059 | |
| 1060 | if (pdacl_size > acl_len || pdacl_size < sizeof(struct smb_acl)) { |
| 1061 | rc = -EINVAL; |
| 1062 | goto free_parent_pntsd; |
| 1063 | } |
| 1064 | |
| 1065 | aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, |
| 1066 | KSMBD_DEFAULT_GFP); |
| 1067 | if (!aces_base) { |
| 1068 | rc = -ENOMEM; |
| 1069 | goto free_parent_pntsd; |
| 1070 | } |
| 1071 | |
| 1072 | aces = (struct smb_ace *)aces_base; |
| 1073 | parent_aces = (struct smb_ace *)((char *)parent_pdacl + |
| 1074 | sizeof(struct smb_acl)); |
| 1075 | aces_size = acl_len - sizeof(struct smb_acl); |
| 1076 | |
| 1077 | if (pntsd_type & DACL_AUTO_INHERITED) |
| 1078 | inherited_flags = INHERITED_ACE; |
| 1079 | |
| 1080 | for (i = 0; i < num_aces; i++) { |
| 1081 | int pace_size; |
| 1082 | |
| 1083 | if (offsetof(struct smb_ace, access_req) > aces_size) |
| 1084 | break; |
| 1085 | |
| 1086 | pace_size = le16_to_cpu(parent_aces->size); |
| 1087 | if (pace_size > aces_size) |
| 1088 | break; |
| 1089 | |
| 1090 | aces_size -= pace_size; |
| 1091 | |
| 1092 | flags = parent_aces->flags; |
| 1093 | if (!smb_inherit_flags(flags, is_dir)) |
| 1094 | goto pass; |
| 1095 | if (is_dir) { |
| 1096 | flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE); |
| 1097 | if (!(flags & CONTAINER_INHERIT_ACE)) |
| 1098 | flags |= INHERIT_ONLY_ACE; |
| 1099 | if (flags & NO_PROPAGATE_INHERIT_ACE) |
| 1100 | flags = 0; |
| 1101 | } else { |
| 1102 | flags = 0; |
| 1103 | } |
| 1104 | |
| 1105 | if (!compare_sids(ctsid: &creator_owner, cwsid: &parent_aces->sid)) { |
| 1106 | creator = &creator_owner; |
| 1107 | id_to_sid(cid: uid, sidtype: SIDOWNER, ssid: &owner_sid); |
| 1108 | psid = &owner_sid; |
| 1109 | } else if (!compare_sids(ctsid: &creator_group, cwsid: &parent_aces->sid)) { |
| 1110 | creator = &creator_group; |
| 1111 | id_to_sid(cid: gid, sidtype: SIDUNIX_GROUP, ssid: &group_sid); |
| 1112 | psid = &group_sid; |
| 1113 | } else { |
| 1114 | creator = NULL; |
| 1115 | psid = &parent_aces->sid; |
| 1116 | } |
| 1117 | |
| 1118 | if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) { |
| 1119 | smb_set_ace(ace: aces, sid: psid, type: parent_aces->type, flags: inherited_flags, |
| 1120 | access_req: parent_aces->access_req); |
| 1121 | nt_size += le16_to_cpu(aces->size); |
| 1122 | ace_cnt++; |
| 1123 | aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size)); |
| 1124 | flags |= INHERIT_ONLY_ACE; |
| 1125 | psid = creator; |
| 1126 | } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) { |
| 1127 | psid = &parent_aces->sid; |
| 1128 | } |
| 1129 | |
| 1130 | smb_set_ace(ace: aces, sid: psid, type: parent_aces->type, flags: flags | inherited_flags, |
| 1131 | access_req: parent_aces->access_req); |
| 1132 | nt_size += le16_to_cpu(aces->size); |
| 1133 | aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size)); |
| 1134 | ace_cnt++; |
| 1135 | pass: |
| 1136 | parent_aces = (struct smb_ace *)((char *)parent_aces + pace_size); |
| 1137 | } |
| 1138 | |
| 1139 | if (nt_size > 0) { |
| 1140 | struct smb_ntsd *pntsd; |
| 1141 | struct smb_acl *pdacl; |
| 1142 | struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL; |
| 1143 | int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size; |
| 1144 | int pntsd_alloc_size; |
| 1145 | |
| 1146 | if (parent_pntsd->osidoffset) { |
| 1147 | powner_sid = (struct smb_sid *)((char *)parent_pntsd + |
| 1148 | le32_to_cpu(parent_pntsd->osidoffset)); |
| 1149 | powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4); |
| 1150 | } |
| 1151 | if (parent_pntsd->gsidoffset) { |
| 1152 | pgroup_sid = (struct smb_sid *)((char *)parent_pntsd + |
| 1153 | le32_to_cpu(parent_pntsd->gsidoffset)); |
| 1154 | pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4); |
| 1155 | } |
| 1156 | |
| 1157 | pntsd_alloc_size = sizeof(struct smb_ntsd) + powner_sid_size + |
| 1158 | pgroup_sid_size + sizeof(struct smb_acl) + nt_size; |
| 1159 | |
| 1160 | pntsd = kzalloc(pntsd_alloc_size, KSMBD_DEFAULT_GFP); |
| 1161 | if (!pntsd) { |
| 1162 | rc = -ENOMEM; |
| 1163 | goto free_aces_base; |
| 1164 | } |
| 1165 | |
| 1166 | pntsd->revision = cpu_to_le16(1); |
| 1167 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT); |
| 1168 | if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED) |
| 1169 | pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED); |
| 1170 | pntsd_size = sizeof(struct smb_ntsd); |
| 1171 | pntsd->osidoffset = parent_pntsd->osidoffset; |
| 1172 | pntsd->gsidoffset = parent_pntsd->gsidoffset; |
| 1173 | pntsd->dacloffset = parent_pntsd->dacloffset; |
| 1174 | |
| 1175 | if ((u64)le32_to_cpu(pntsd->osidoffset) + powner_sid_size > |
| 1176 | pntsd_alloc_size) { |
| 1177 | rc = -EINVAL; |
| 1178 | kfree(objp: pntsd); |
| 1179 | goto free_aces_base; |
| 1180 | } |
| 1181 | |
| 1182 | if ((u64)le32_to_cpu(pntsd->gsidoffset) + pgroup_sid_size > |
| 1183 | pntsd_alloc_size) { |
| 1184 | rc = -EINVAL; |
| 1185 | kfree(objp: pntsd); |
| 1186 | goto free_aces_base; |
| 1187 | } |
| 1188 | |
| 1189 | if ((u64)le32_to_cpu(pntsd->dacloffset) + sizeof(struct smb_acl) + nt_size > |
| 1190 | pntsd_alloc_size) { |
| 1191 | rc = -EINVAL; |
| 1192 | kfree(objp: pntsd); |
| 1193 | goto free_aces_base; |
| 1194 | } |
| 1195 | |
| 1196 | if (pntsd->osidoffset) { |
| 1197 | struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd + |
| 1198 | le32_to_cpu(pntsd->osidoffset)); |
| 1199 | memcpy(owner_sid, powner_sid, powner_sid_size); |
| 1200 | pntsd_size += powner_sid_size; |
| 1201 | } |
| 1202 | |
| 1203 | if (pntsd->gsidoffset) { |
| 1204 | struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd + |
| 1205 | le32_to_cpu(pntsd->gsidoffset)); |
| 1206 | memcpy(group_sid, pgroup_sid, pgroup_sid_size); |
| 1207 | pntsd_size += pgroup_sid_size; |
| 1208 | } |
| 1209 | |
| 1210 | if (pntsd->dacloffset) { |
| 1211 | struct smb_ace *pace; |
| 1212 | |
| 1213 | pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset)); |
| 1214 | pdacl->revision = cpu_to_le16(2); |
| 1215 | pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size); |
| 1216 | pdacl->num_aces = cpu_to_le16(ace_cnt); |
| 1217 | pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1218 | memcpy(pace, aces_base, nt_size); |
| 1219 | pntsd_size += sizeof(struct smb_acl) + nt_size; |
| 1220 | } |
| 1221 | |
| 1222 | ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, len: pntsd_size, get_write: false); |
| 1223 | kfree(objp: pntsd); |
| 1224 | } |
| 1225 | |
| 1226 | free_aces_base: |
| 1227 | kfree(objp: aces_base); |
| 1228 | free_parent_pntsd: |
| 1229 | kfree(objp: parent_pntsd); |
| 1230 | return rc; |
| 1231 | } |
| 1232 | |
| 1233 | bool smb_inherit_flags(int flags, bool is_dir) |
| 1234 | { |
| 1235 | if (!is_dir) |
| 1236 | return (flags & OBJECT_INHERIT_ACE) != 0; |
| 1237 | |
| 1238 | if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE)) |
| 1239 | return true; |
| 1240 | |
| 1241 | if (flags & CONTAINER_INHERIT_ACE) |
| 1242 | return true; |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
| 1246 | int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path, |
| 1247 | __le32 *pdaccess, int uid) |
| 1248 | { |
| 1249 | struct mnt_idmap *idmap = mnt_idmap(mnt: path->mnt); |
| 1250 | struct smb_ntsd *pntsd = NULL; |
| 1251 | struct smb_acl *pdacl; |
| 1252 | struct posix_acl *posix_acls; |
| 1253 | int rc = 0, pntsd_size, acl_size, aces_size, pdacl_size; |
| 1254 | unsigned int dacl_offset; |
| 1255 | size_t dacl_struct_end; |
| 1256 | struct smb_sid sid; |
| 1257 | int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE); |
| 1258 | struct smb_ace *ace; |
| 1259 | int i, found = 0; |
| 1260 | unsigned int access_bits = 0; |
| 1261 | struct smb_ace *others_ace = NULL; |
| 1262 | struct posix_acl_entry *pa_entry; |
| 1263 | unsigned int sid_type = SIDOWNER; |
| 1264 | unsigned short ace_size; |
| 1265 | |
| 1266 | ksmbd_debug(SMB, "check permission using windows acl\n" ); |
| 1267 | pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap, |
| 1268 | dentry: path->dentry, pntsd: &pntsd); |
| 1269 | if (pntsd_size <= 0 || !pntsd) |
| 1270 | goto err_out; |
| 1271 | |
| 1272 | dacl_offset = le32_to_cpu(pntsd->dacloffset); |
| 1273 | if (!dacl_offset || |
| 1274 | check_add_overflow(dacl_offset, sizeof(struct smb_acl), &dacl_struct_end) || |
| 1275 | dacl_struct_end > (size_t)pntsd_size) |
| 1276 | goto err_out; |
| 1277 | |
| 1278 | pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset)); |
| 1279 | acl_size = pntsd_size - dacl_offset; |
| 1280 | pdacl_size = le16_to_cpu(pdacl->size); |
| 1281 | |
| 1282 | if (pdacl_size > acl_size || pdacl_size < sizeof(struct smb_acl)) |
| 1283 | goto err_out; |
| 1284 | |
| 1285 | if (!pdacl->num_aces) { |
| 1286 | if (!(pdacl_size - sizeof(struct smb_acl)) && |
| 1287 | *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) { |
| 1288 | rc = -EACCES; |
| 1289 | goto err_out; |
| 1290 | } |
| 1291 | goto err_out; |
| 1292 | } |
| 1293 | |
| 1294 | if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) { |
| 1295 | granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES | |
| 1296 | DELETE; |
| 1297 | |
| 1298 | ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1299 | aces_size = acl_size - sizeof(struct smb_acl); |
| 1300 | for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) { |
| 1301 | if (offsetof(struct smb_ace, access_req) > aces_size) |
| 1302 | break; |
| 1303 | ace_size = le16_to_cpu(ace->size); |
| 1304 | if (ace_size > aces_size) |
| 1305 | break; |
| 1306 | aces_size -= ace_size; |
| 1307 | granted |= le32_to_cpu(ace->access_req); |
| 1308 | ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | if (!uid) |
| 1313 | sid_type = SIDUNIX_USER; |
| 1314 | id_to_sid(cid: uid, sidtype: sid_type, ssid: &sid); |
| 1315 | |
| 1316 | ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl)); |
| 1317 | aces_size = acl_size - sizeof(struct smb_acl); |
| 1318 | for (i = 0; i < le16_to_cpu(pdacl->num_aces); i++) { |
| 1319 | if (offsetof(struct smb_ace, access_req) > aces_size) |
| 1320 | break; |
| 1321 | ace_size = le16_to_cpu(ace->size); |
| 1322 | if (ace_size > aces_size) |
| 1323 | break; |
| 1324 | aces_size -= ace_size; |
| 1325 | |
| 1326 | if (!compare_sids(ctsid: &sid, cwsid: &ace->sid) || |
| 1327 | !compare_sids(ctsid: &sid_unix_NFS_mode, cwsid: &ace->sid)) { |
| 1328 | found = 1; |
| 1329 | break; |
| 1330 | } |
| 1331 | if (!compare_sids(ctsid: &sid_everyone, cwsid: &ace->sid)) |
| 1332 | others_ace = ace; |
| 1333 | |
| 1334 | ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size)); |
| 1335 | } |
| 1336 | |
| 1337 | if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) { |
| 1338 | granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES | |
| 1339 | DELETE; |
| 1340 | |
| 1341 | granted |= le32_to_cpu(ace->access_req); |
| 1342 | |
| 1343 | if (!pdacl->num_aces) |
| 1344 | granted = GENERIC_ALL_FLAGS; |
| 1345 | } |
| 1346 | |
| 1347 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
| 1348 | posix_acls = get_inode_acl(inode: d_inode(dentry: path->dentry), ACL_TYPE_ACCESS); |
| 1349 | if (!IS_ERR_OR_NULL(ptr: posix_acls) && !found) { |
| 1350 | unsigned int id = -1; |
| 1351 | |
| 1352 | pa_entry = posix_acls->a_entries; |
| 1353 | for (i = 0; i < posix_acls->a_count; i++, pa_entry++) { |
| 1354 | if (pa_entry->e_tag == ACL_USER) |
| 1355 | id = posix_acl_uid_translate(idmap, pace: pa_entry); |
| 1356 | else if (pa_entry->e_tag == ACL_GROUP) |
| 1357 | id = posix_acl_gid_translate(idmap, pace: pa_entry); |
| 1358 | else |
| 1359 | continue; |
| 1360 | |
| 1361 | if (id == uid) { |
| 1362 | mode_to_access_flags(mode: pa_entry->e_perm, |
| 1363 | bits_to_use: 0777, |
| 1364 | pace_flags: &access_bits); |
| 1365 | if (!access_bits) |
| 1366 | access_bits = |
| 1367 | SET_MINIMUM_RIGHTS; |
| 1368 | posix_acl_release(acl: posix_acls); |
| 1369 | goto check_access_bits; |
| 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | if (!IS_ERR_OR_NULL(ptr: posix_acls)) |
| 1374 | posix_acl_release(acl: posix_acls); |
| 1375 | } |
| 1376 | |
| 1377 | if (!found) { |
| 1378 | if (others_ace) { |
| 1379 | ace = others_ace; |
| 1380 | } else { |
| 1381 | ksmbd_debug(SMB, "Can't find corresponding sid\n" ); |
| 1382 | rc = -EACCES; |
| 1383 | goto err_out; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | switch (ace->type) { |
| 1388 | case ACCESS_ALLOWED_ACE_TYPE: |
| 1389 | access_bits = le32_to_cpu(ace->access_req); |
| 1390 | break; |
| 1391 | case ACCESS_DENIED_ACE_TYPE: |
| 1392 | case ACCESS_DENIED_CALLBACK_ACE_TYPE: |
| 1393 | access_bits = le32_to_cpu(~ace->access_req); |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | check_access_bits: |
| 1398 | if (granted & |
| 1399 | ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) { |
| 1400 | ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n" , |
| 1401 | granted, le32_to_cpu(ace->access_req)); |
| 1402 | rc = -EACCES; |
| 1403 | goto err_out; |
| 1404 | } |
| 1405 | |
| 1406 | *pdaccess = cpu_to_le32(granted); |
| 1407 | err_out: |
| 1408 | kfree(objp: pntsd); |
| 1409 | return rc; |
| 1410 | } |
| 1411 | |
| 1412 | int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon, |
| 1413 | const struct path *path, struct smb_ntsd *pntsd, int ntsd_len, |
| 1414 | bool type_check, bool get_write) |
| 1415 | { |
| 1416 | int rc; |
| 1417 | struct smb_fattr fattr = {{0}}; |
| 1418 | struct inode *inode = d_inode(dentry: path->dentry); |
| 1419 | struct mnt_idmap *idmap = mnt_idmap(mnt: path->mnt); |
| 1420 | struct iattr newattrs; |
| 1421 | |
| 1422 | fattr.cf_uid = INVALID_UID; |
| 1423 | fattr.cf_gid = INVALID_GID; |
| 1424 | fattr.cf_mode = inode->i_mode; |
| 1425 | |
| 1426 | rc = parse_sec_desc(idmap, pntsd, acl_len: ntsd_len, fattr: &fattr); |
| 1427 | if (rc) |
| 1428 | goto out; |
| 1429 | |
| 1430 | newattrs.ia_valid = ATTR_CTIME; |
| 1431 | if (!uid_eq(left: fattr.cf_uid, INVALID_UID)) { |
| 1432 | newattrs.ia_valid |= ATTR_UID; |
| 1433 | newattrs.ia_uid = fattr.cf_uid; |
| 1434 | } |
| 1435 | if (!gid_eq(left: fattr.cf_gid, INVALID_GID)) { |
| 1436 | newattrs.ia_valid |= ATTR_GID; |
| 1437 | newattrs.ia_gid = fattr.cf_gid; |
| 1438 | } |
| 1439 | newattrs.ia_valid |= ATTR_MODE; |
| 1440 | newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777); |
| 1441 | |
| 1442 | ksmbd_vfs_remove_acl_xattrs(idmap, path); |
| 1443 | /* Update posix acls */ |
| 1444 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) { |
| 1445 | rc = set_posix_acl(idmap, path->dentry, |
| 1446 | ACL_TYPE_ACCESS, fattr.cf_acls); |
| 1447 | if (rc < 0) |
| 1448 | ksmbd_debug(SMB, |
| 1449 | "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n" , |
| 1450 | rc); |
| 1451 | if (S_ISDIR(inode->i_mode) && fattr.cf_dacls) { |
| 1452 | rc = set_posix_acl(idmap, path->dentry, |
| 1453 | ACL_TYPE_DEFAULT, fattr.cf_dacls); |
| 1454 | if (rc) |
| 1455 | ksmbd_debug(SMB, |
| 1456 | "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n" , |
| 1457 | rc); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | inode_lock(inode); |
| 1462 | rc = notify_change(idmap, path->dentry, &newattrs, NULL); |
| 1463 | inode_unlock(inode); |
| 1464 | if (rc) |
| 1465 | goto out; |
| 1466 | |
| 1467 | /* Check it only calling from SD BUFFER context */ |
| 1468 | if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT)) |
| 1469 | goto out; |
| 1470 | |
| 1471 | if (test_share_config_flag(share: tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) { |
| 1472 | /* Update WinACL in xattr */ |
| 1473 | ksmbd_vfs_remove_sd_xattrs(idmap, path); |
| 1474 | ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, len: ntsd_len, |
| 1475 | get_write); |
| 1476 | } |
| 1477 | |
| 1478 | out: |
| 1479 | posix_acl_release(acl: fattr.cf_acls); |
| 1480 | posix_acl_release(acl: fattr.cf_dacls); |
| 1481 | return rc; |
| 1482 | } |
| 1483 | |
| 1484 | void ksmbd_init_domain(u32 *sub_auth) |
| 1485 | { |
| 1486 | int i; |
| 1487 | |
| 1488 | memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid)); |
| 1489 | for (i = 0; i < 3; ++i) |
| 1490 | server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]); |
| 1491 | } |
| 1492 | |