Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifsacl.c |
| 3 | * |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2007,2008 |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * Contains the routines for mapping CIFS/NTFS ACLs |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 24 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 26 | #include <linux/string.h> |
| 27 | #include <linux/keyctl.h> |
| 28 | #include <linux/key-type.h> |
| 29 | #include <keys/user-type.h> |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 30 | #include "cifspdu.h" |
| 31 | #include "cifsglob.h" |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 32 | #include "cifsacl.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 33 | #include "cifsproto.h" |
| 34 | #include "cifs_debug.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 35 | |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 36 | /* security id for everyone/world system group */ |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 37 | static const struct cifs_sid sid_everyone = { |
| 38 | 1, 1, {0, 0, 0, 0, 0, 1}, {0} }; |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 39 | /* security id for Authenticated Users system group */ |
| 40 | static const struct cifs_sid sid_authusers = { |
Steve French | 4f61258 | 2011-05-27 20:40:18 +0000 | [diff] [blame] | 41 | 1, 1, {0, 0, 0, 0, 0, 5}, {__constant_cpu_to_le32(11)} }; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 42 | /* group users */ |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 43 | static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} }; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 44 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 45 | const struct cred *root_cred; |
| 46 | |
| 47 | static void |
| 48 | shrink_idmap_tree(struct rb_root *root, int nr_to_scan, int *nr_rem, |
| 49 | int *nr_del) |
| 50 | { |
| 51 | struct rb_node *node; |
| 52 | struct rb_node *tmp; |
| 53 | struct cifs_sid_id *psidid; |
| 54 | |
| 55 | node = rb_first(root); |
| 56 | while (node) { |
| 57 | tmp = node; |
| 58 | node = rb_next(tmp); |
| 59 | psidid = rb_entry(tmp, struct cifs_sid_id, rbnode); |
| 60 | if (nr_to_scan == 0 || *nr_del == nr_to_scan) |
| 61 | ++(*nr_rem); |
| 62 | else { |
| 63 | if (time_after(jiffies, psidid->time + SID_MAP_EXPIRE) |
| 64 | && psidid->refcount == 0) { |
| 65 | rb_erase(tmp, root); |
| 66 | ++(*nr_del); |
| 67 | } else |
| 68 | ++(*nr_rem); |
| 69 | } |
| 70 | } |
| 71 | } |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Run idmap cache shrinker. |
| 75 | */ |
| 76 | static int |
Al Viro | ef1d575 | 2011-05-29 13:46:08 +0100 | [diff] [blame] | 77 | cifs_idmap_shrinker(struct shrinker *shrink, struct shrink_control *sc) |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 78 | { |
Al Viro | ef1d575 | 2011-05-29 13:46:08 +0100 | [diff] [blame] | 79 | int nr_to_scan = sc->nr_to_scan; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 80 | int nr_del = 0; |
| 81 | int nr_rem = 0; |
| 82 | struct rb_root *root; |
| 83 | |
| 84 | root = &uidtree; |
| 85 | spin_lock(&siduidlock); |
| 86 | shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del); |
| 87 | spin_unlock(&siduidlock); |
| 88 | |
| 89 | root = &gidtree; |
| 90 | spin_lock(&sidgidlock); |
| 91 | shrink_idmap_tree(root, nr_to_scan, &nr_rem, &nr_del); |
| 92 | spin_unlock(&sidgidlock); |
| 93 | |
| 94 | return nr_rem; |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static struct shrinker cifs_shrinker = { |
| 98 | .shrink = cifs_idmap_shrinker, |
| 99 | .seeks = DEFAULT_SEEKS, |
| 100 | }; |
| 101 | |
| 102 | static int |
| 103 | cifs_idmap_key_instantiate(struct key *key, const void *data, size_t datalen) |
| 104 | { |
| 105 | char *payload; |
| 106 | |
| 107 | payload = kmalloc(datalen, GFP_KERNEL); |
| 108 | if (!payload) |
| 109 | return -ENOMEM; |
| 110 | |
| 111 | memcpy(payload, data, datalen); |
| 112 | key->payload.data = payload; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static inline void |
| 117 | cifs_idmap_key_destroy(struct key *key) |
| 118 | { |
| 119 | kfree(key->payload.data); |
| 120 | } |
| 121 | |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 122 | struct key_type cifs_idmap_key_type = { |
Shirish Pargaonkar | c4aca0c | 2011-05-06 02:35:00 -0500 | [diff] [blame] | 123 | .name = "cifs.idmap", |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 124 | .instantiate = cifs_idmap_key_instantiate, |
| 125 | .destroy = cifs_idmap_key_destroy, |
| 126 | .describe = user_describe, |
| 127 | .match = user_match, |
| 128 | }; |
| 129 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 130 | static void |
| 131 | sid_to_str(struct cifs_sid *sidptr, char *sidstr) |
| 132 | { |
| 133 | int i; |
| 134 | unsigned long saval; |
| 135 | char *strptr; |
| 136 | |
| 137 | strptr = sidstr; |
| 138 | |
| 139 | sprintf(strptr, "%s", "S"); |
| 140 | strptr = sidstr + strlen(sidstr); |
| 141 | |
| 142 | sprintf(strptr, "-%d", sidptr->revision); |
| 143 | strptr = sidstr + strlen(sidstr); |
| 144 | |
| 145 | for (i = 0; i < 6; ++i) { |
| 146 | if (sidptr->authority[i]) { |
| 147 | sprintf(strptr, "-%d", sidptr->authority[i]); |
| 148 | strptr = sidstr + strlen(sidstr); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | for (i = 0; i < sidptr->num_subauth; ++i) { |
| 153 | saval = le32_to_cpu(sidptr->sub_auth[i]); |
| 154 | sprintf(strptr, "-%ld", saval); |
| 155 | strptr = sidstr + strlen(sidstr); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | id_rb_insert(struct rb_root *root, struct cifs_sid *sidptr, |
| 161 | struct cifs_sid_id **psidid, char *typestr) |
| 162 | { |
| 163 | int rc; |
| 164 | char *strptr; |
| 165 | struct rb_node *node = root->rb_node; |
| 166 | struct rb_node *parent = NULL; |
| 167 | struct rb_node **linkto = &(root->rb_node); |
| 168 | struct cifs_sid_id *lsidid; |
| 169 | |
| 170 | while (node) { |
| 171 | lsidid = rb_entry(node, struct cifs_sid_id, rbnode); |
| 172 | parent = node; |
| 173 | rc = compare_sids(sidptr, &((lsidid)->sid)); |
| 174 | if (rc > 0) { |
| 175 | linkto = &(node->rb_left); |
| 176 | node = node->rb_left; |
| 177 | } else if (rc < 0) { |
| 178 | linkto = &(node->rb_right); |
| 179 | node = node->rb_right; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | memcpy(&(*psidid)->sid, sidptr, sizeof(struct cifs_sid)); |
| 184 | (*psidid)->time = jiffies - (SID_MAP_RETRY + 1); |
| 185 | (*psidid)->refcount = 0; |
| 186 | |
| 187 | sprintf((*psidid)->sidstr, "%s", typestr); |
| 188 | strptr = (*psidid)->sidstr + strlen((*psidid)->sidstr); |
| 189 | sid_to_str(&(*psidid)->sid, strptr); |
| 190 | |
| 191 | clear_bit(SID_ID_PENDING, &(*psidid)->state); |
| 192 | clear_bit(SID_ID_MAPPED, &(*psidid)->state); |
| 193 | |
| 194 | rb_link_node(&(*psidid)->rbnode, parent, linkto); |
| 195 | rb_insert_color(&(*psidid)->rbnode, root); |
| 196 | } |
| 197 | |
| 198 | static struct cifs_sid_id * |
| 199 | id_rb_search(struct rb_root *root, struct cifs_sid *sidptr) |
| 200 | { |
| 201 | int rc; |
| 202 | struct rb_node *node = root->rb_node; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 203 | struct cifs_sid_id *lsidid; |
| 204 | |
| 205 | while (node) { |
| 206 | lsidid = rb_entry(node, struct cifs_sid_id, rbnode); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 207 | rc = compare_sids(sidptr, &((lsidid)->sid)); |
| 208 | if (rc > 0) { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 209 | node = node->rb_left; |
| 210 | } else if (rc < 0) { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 211 | node = node->rb_right; |
| 212 | } else /* node found */ |
| 213 | return lsidid; |
| 214 | } |
| 215 | |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | static int |
| 220 | sidid_pending_wait(void *unused) |
| 221 | { |
| 222 | schedule(); |
| 223 | return signal_pending(current) ? -ERESTARTSYS : 0; |
| 224 | } |
| 225 | |
| 226 | static int |
| 227 | sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid, |
| 228 | struct cifs_fattr *fattr, uint sidtype) |
| 229 | { |
| 230 | int rc; |
| 231 | unsigned long cid; |
| 232 | struct key *idkey; |
| 233 | const struct cred *saved_cred; |
| 234 | struct cifs_sid_id *psidid, *npsidid; |
| 235 | struct rb_root *cidtree; |
| 236 | spinlock_t *cidlock; |
| 237 | |
| 238 | if (sidtype == SIDOWNER) { |
| 239 | cid = cifs_sb->mnt_uid; /* default uid, in case upcall fails */ |
| 240 | cidlock = &siduidlock; |
| 241 | cidtree = &uidtree; |
| 242 | } else if (sidtype == SIDGROUP) { |
| 243 | cid = cifs_sb->mnt_gid; /* default gid, in case upcall fails */ |
| 244 | cidlock = &sidgidlock; |
| 245 | cidtree = &gidtree; |
| 246 | } else |
| 247 | return -ENOENT; |
| 248 | |
| 249 | spin_lock(cidlock); |
| 250 | psidid = id_rb_search(cidtree, psid); |
| 251 | |
| 252 | if (!psidid) { /* node does not exist, allocate one & attempt adding */ |
| 253 | spin_unlock(cidlock); |
| 254 | npsidid = kzalloc(sizeof(struct cifs_sid_id), GFP_KERNEL); |
| 255 | if (!npsidid) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | npsidid->sidstr = kmalloc(SIDLEN, GFP_KERNEL); |
| 259 | if (!npsidid->sidstr) { |
| 260 | kfree(npsidid); |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
| 264 | spin_lock(cidlock); |
| 265 | psidid = id_rb_search(cidtree, psid); |
| 266 | if (psidid) { /* node happened to get inserted meanwhile */ |
| 267 | ++psidid->refcount; |
| 268 | spin_unlock(cidlock); |
| 269 | kfree(npsidid->sidstr); |
| 270 | kfree(npsidid); |
| 271 | } else { |
| 272 | psidid = npsidid; |
| 273 | id_rb_insert(cidtree, psid, &psidid, |
| 274 | sidtype == SIDOWNER ? "os:" : "gs:"); |
| 275 | ++psidid->refcount; |
| 276 | spin_unlock(cidlock); |
| 277 | } |
| 278 | } else { |
| 279 | ++psidid->refcount; |
| 280 | spin_unlock(cidlock); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * If we are here, it is safe to access psidid and its fields |
| 285 | * since a reference was taken earlier while holding the spinlock. |
| 286 | * A reference on the node is put without holding the spinlock |
| 287 | * and it is OK to do so in this case, shrinker will not erase |
| 288 | * this node until all references are put and we do not access |
| 289 | * any fields of the node after a reference is put . |
| 290 | */ |
| 291 | if (test_bit(SID_ID_MAPPED, &psidid->state)) { |
| 292 | cid = psidid->id; |
| 293 | psidid->time = jiffies; /* update ts for accessing */ |
| 294 | goto sid_to_id_out; |
| 295 | } |
| 296 | |
| 297 | if (time_after(psidid->time + SID_MAP_RETRY, jiffies)) |
| 298 | goto sid_to_id_out; |
| 299 | |
| 300 | if (!test_and_set_bit(SID_ID_PENDING, &psidid->state)) { |
| 301 | saved_cred = override_creds(root_cred); |
| 302 | idkey = request_key(&cifs_idmap_key_type, psidid->sidstr, ""); |
| 303 | if (IS_ERR(idkey)) |
| 304 | cFYI(1, "%s: Can't map SID to an id", __func__); |
| 305 | else { |
| 306 | cid = *(unsigned long *)idkey->payload.value; |
| 307 | psidid->id = cid; |
| 308 | set_bit(SID_ID_MAPPED, &psidid->state); |
| 309 | key_put(idkey); |
| 310 | kfree(psidid->sidstr); |
| 311 | } |
| 312 | revert_creds(saved_cred); |
| 313 | psidid->time = jiffies; /* update ts for accessing */ |
| 314 | clear_bit(SID_ID_PENDING, &psidid->state); |
| 315 | wake_up_bit(&psidid->state, SID_ID_PENDING); |
| 316 | } else { |
| 317 | rc = wait_on_bit(&psidid->state, SID_ID_PENDING, |
| 318 | sidid_pending_wait, TASK_INTERRUPTIBLE); |
| 319 | if (rc) { |
| 320 | cFYI(1, "%s: sidid_pending_wait interrupted %d", |
| 321 | __func__, rc); |
| 322 | --psidid->refcount; /* decremented without spinlock */ |
| 323 | return rc; |
| 324 | } |
| 325 | if (test_bit(SID_ID_MAPPED, &psidid->state)) |
| 326 | cid = psidid->id; |
| 327 | } |
| 328 | |
| 329 | sid_to_id_out: |
| 330 | --psidid->refcount; /* decremented without spinlock */ |
| 331 | if (sidtype == SIDOWNER) |
| 332 | fattr->cf_uid = cid; |
| 333 | else |
| 334 | fattr->cf_gid = cid; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
Shirish Pargaonkar | 4d79dba | 2011-04-27 23:34:35 -0500 | [diff] [blame] | 339 | int |
| 340 | init_cifs_idmap(void) |
| 341 | { |
| 342 | struct cred *cred; |
| 343 | struct key *keyring; |
| 344 | int ret; |
| 345 | |
| 346 | cFYI(1, "Registering the %s key type\n", cifs_idmap_key_type.name); |
| 347 | |
| 348 | /* create an override credential set with a special thread keyring in |
| 349 | * which requests are cached |
| 350 | * |
| 351 | * this is used to prevent malicious redirections from being installed |
| 352 | * with add_key(). |
| 353 | */ |
| 354 | cred = prepare_kernel_cred(NULL); |
| 355 | if (!cred) |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | keyring = key_alloc(&key_type_keyring, ".cifs_idmap", 0, 0, cred, |
| 359 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | |
| 360 | KEY_USR_VIEW | KEY_USR_READ, |
| 361 | KEY_ALLOC_NOT_IN_QUOTA); |
| 362 | if (IS_ERR(keyring)) { |
| 363 | ret = PTR_ERR(keyring); |
| 364 | goto failed_put_cred; |
| 365 | } |
| 366 | |
| 367 | ret = key_instantiate_and_link(keyring, NULL, 0, NULL, NULL); |
| 368 | if (ret < 0) |
| 369 | goto failed_put_key; |
| 370 | |
| 371 | ret = register_key_type(&cifs_idmap_key_type); |
| 372 | if (ret < 0) |
| 373 | goto failed_put_key; |
| 374 | |
| 375 | /* instruct request_key() to use this special keyring as a cache for |
| 376 | * the results it looks up */ |
| 377 | cred->thread_keyring = keyring; |
| 378 | cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; |
| 379 | root_cred = cred; |
| 380 | |
| 381 | spin_lock_init(&siduidlock); |
| 382 | uidtree = RB_ROOT; |
| 383 | spin_lock_init(&sidgidlock); |
| 384 | gidtree = RB_ROOT; |
| 385 | |
| 386 | register_shrinker(&cifs_shrinker); |
| 387 | |
| 388 | cFYI(1, "cifs idmap keyring: %d\n", key_serial(keyring)); |
| 389 | return 0; |
| 390 | |
| 391 | failed_put_key: |
| 392 | key_put(keyring); |
| 393 | failed_put_cred: |
| 394 | put_cred(cred); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | void |
| 399 | exit_cifs_idmap(void) |
| 400 | { |
| 401 | key_revoke(root_cred->thread_keyring); |
| 402 | unregister_key_type(&cifs_idmap_key_type); |
| 403 | put_cred(root_cred); |
| 404 | unregister_shrinker(&cifs_shrinker); |
| 405 | cFYI(1, "Unregistered %s key type\n", cifs_idmap_key_type.name); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | cifs_destroy_idmaptrees(void) |
| 410 | { |
| 411 | struct rb_root *root; |
| 412 | struct rb_node *node; |
| 413 | |
| 414 | root = &uidtree; |
| 415 | spin_lock(&siduidlock); |
| 416 | while ((node = rb_first(root))) |
| 417 | rb_erase(node, root); |
| 418 | spin_unlock(&siduidlock); |
| 419 | |
| 420 | root = &gidtree; |
| 421 | spin_lock(&sidgidlock); |
| 422 | while ((node = rb_first(root))) |
| 423 | rb_erase(node, root); |
| 424 | spin_unlock(&sidgidlock); |
| 425 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 426 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 427 | /* if the two SIDs (roughly equivalent to a UUID for a user or group) are |
| 428 | the same returns 1, if they do not match returns 0 */ |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 429 | int compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid) |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 430 | { |
| 431 | int i; |
| 432 | int num_subauth, num_sat, num_saw; |
| 433 | |
| 434 | if ((!ctsid) || (!cwsid)) |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 435 | return 1; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 436 | |
| 437 | /* compare the revision */ |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 438 | if (ctsid->revision != cwsid->revision) { |
| 439 | if (ctsid->revision > cwsid->revision) |
| 440 | return 1; |
| 441 | else |
| 442 | return -1; |
| 443 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 444 | |
| 445 | /* compare all of the six auth values */ |
| 446 | for (i = 0; i < 6; ++i) { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 447 | if (ctsid->authority[i] != cwsid->authority[i]) { |
| 448 | if (ctsid->authority[i] > cwsid->authority[i]) |
| 449 | return 1; |
| 450 | else |
| 451 | return -1; |
| 452 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* compare all of the subauth values if any */ |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 456 | num_sat = ctsid->num_subauth; |
Steve French | adddd49 | 2007-10-17 02:48:17 +0000 | [diff] [blame] | 457 | num_saw = cwsid->num_subauth; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 458 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 459 | if (num_subauth) { |
| 460 | for (i = 0; i < num_subauth; ++i) { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 461 | if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) { |
Steve French | 383c553 | 2011-05-27 15:19:12 +0000 | [diff] [blame] | 462 | if (le32_to_cpu(ctsid->sub_auth[i]) > |
| 463 | le32_to_cpu(cwsid->sub_auth[i])) |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 464 | return 1; |
| 465 | else |
| 466 | return -1; |
| 467 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 471 | return 0; /* sids compare/match */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 474 | |
| 475 | /* copy ntsd, owner sid, and group sid from a security descriptor to another */ |
| 476 | static void copy_sec_desc(const struct cifs_ntsd *pntsd, |
| 477 | struct cifs_ntsd *pnntsd, __u32 sidsoffset) |
| 478 | { |
| 479 | int i; |
| 480 | |
| 481 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 482 | struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 483 | |
| 484 | /* copy security descriptor control portion */ |
| 485 | pnntsd->revision = pntsd->revision; |
| 486 | pnntsd->type = pntsd->type; |
| 487 | pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd)); |
| 488 | pnntsd->sacloffset = 0; |
| 489 | pnntsd->osidoffset = cpu_to_le32(sidsoffset); |
| 490 | pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid)); |
| 491 | |
| 492 | /* copy owner sid */ |
| 493 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 494 | le32_to_cpu(pntsd->osidoffset)); |
| 495 | nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset); |
| 496 | |
| 497 | nowner_sid_ptr->revision = owner_sid_ptr->revision; |
| 498 | nowner_sid_ptr->num_subauth = owner_sid_ptr->num_subauth; |
| 499 | for (i = 0; i < 6; i++) |
| 500 | nowner_sid_ptr->authority[i] = owner_sid_ptr->authority[i]; |
| 501 | for (i = 0; i < 5; i++) |
| 502 | nowner_sid_ptr->sub_auth[i] = owner_sid_ptr->sub_auth[i]; |
| 503 | |
| 504 | /* copy group sid */ |
| 505 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 506 | le32_to_cpu(pntsd->gsidoffset)); |
| 507 | ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset + |
| 508 | sizeof(struct cifs_sid)); |
| 509 | |
| 510 | ngroup_sid_ptr->revision = group_sid_ptr->revision; |
| 511 | ngroup_sid_ptr->num_subauth = group_sid_ptr->num_subauth; |
| 512 | for (i = 0; i < 6; i++) |
| 513 | ngroup_sid_ptr->authority[i] = group_sid_ptr->authority[i]; |
| 514 | for (i = 0; i < 5; i++) |
Shirish Pargaonkar | b1910ad | 2008-07-24 14:53:20 +0000 | [diff] [blame] | 515 | ngroup_sid_ptr->sub_auth[i] = group_sid_ptr->sub_auth[i]; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 516 | |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 521 | /* |
| 522 | change posix mode to reflect permissions |
| 523 | pmode is the existing mode (we only want to overwrite part of this |
| 524 | bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007 |
| 525 | */ |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 526 | static void access_flags_to_mode(__le32 ace_flags, int type, umode_t *pmode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 527 | umode_t *pbits_to_set) |
Steve French | 4879b44 | 2007-10-19 21:57:39 +0000 | [diff] [blame] | 528 | { |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 529 | __u32 flags = le32_to_cpu(ace_flags); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 530 | /* the order of ACEs is important. The canonical order is to begin with |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 531 | DENY entries followed by ALLOW, otherwise an allow entry could be |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 532 | encountered first, making the subsequent deny entry like "dead code" |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 533 | which would be superflous since Windows stops when a match is made |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 534 | for the operation you are trying to perform for your user */ |
| 535 | |
| 536 | /* For deny ACEs we change the mask so that subsequent allow access |
| 537 | control entries do not turn on the bits we are denying */ |
| 538 | if (type == ACCESS_DENIED) { |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 539 | if (flags & GENERIC_ALL) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 540 | *pbits_to_set &= ~S_IRWXUGO; |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 541 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 542 | if ((flags & GENERIC_WRITE) || |
| 543 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 544 | *pbits_to_set &= ~S_IWUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 545 | if ((flags & GENERIC_READ) || |
| 546 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 547 | *pbits_to_set &= ~S_IRUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 548 | if ((flags & GENERIC_EXECUTE) || |
| 549 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 550 | *pbits_to_set &= ~S_IXUGO; |
| 551 | return; |
| 552 | } else if (type != ACCESS_ALLOWED) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 553 | cERROR(1, "unknown access control type %d", type); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | /* else ACCESS_ALLOWED type */ |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 557 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 558 | if (flags & GENERIC_ALL) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 559 | *pmode |= (S_IRWXUGO & (*pbits_to_set)); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 560 | cFYI(DBG2, "all perms"); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 561 | return; |
| 562 | } |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 563 | if ((flags & GENERIC_WRITE) || |
| 564 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 565 | *pmode |= (S_IWUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 566 | if ((flags & GENERIC_READ) || |
| 567 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 568 | *pmode |= (S_IRUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 569 | if ((flags & GENERIC_EXECUTE) || |
| 570 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 571 | *pmode |= (S_IXUGO & (*pbits_to_set)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 572 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 573 | cFYI(DBG2, "access flags 0x%x mode now 0x%x", flags, *pmode); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 577 | /* |
| 578 | Generate access flags to reflect permissions mode is the existing mode. |
| 579 | This function is called for every ACE in the DACL whose SID matches |
| 580 | with either owner or group or everyone. |
| 581 | */ |
| 582 | |
| 583 | static void mode_to_access_flags(umode_t mode, umode_t bits_to_use, |
| 584 | __u32 *pace_flags) |
| 585 | { |
| 586 | /* reset access mask */ |
| 587 | *pace_flags = 0x0; |
| 588 | |
| 589 | /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */ |
| 590 | mode &= bits_to_use; |
| 591 | |
| 592 | /* check for R/W/X UGO since we do not know whose flags |
| 593 | is this but we have cleared all the bits sans RWX for |
| 594 | either user or group or other as per bits_to_use */ |
| 595 | if (mode & S_IRUGO) |
| 596 | *pace_flags |= SET_FILE_READ_RIGHTS; |
| 597 | if (mode & S_IWUGO) |
| 598 | *pace_flags |= SET_FILE_WRITE_RIGHTS; |
| 599 | if (mode & S_IXUGO) |
| 600 | *pace_flags |= SET_FILE_EXEC_RIGHTS; |
| 601 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 602 | cFYI(DBG2, "mode: 0x%x, access flags now 0x%x", mode, *pace_flags); |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 606 | static __u16 fill_ace_for_sid(struct cifs_ace *pntace, |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 607 | const struct cifs_sid *psid, __u64 nmode, umode_t bits) |
| 608 | { |
| 609 | int i; |
| 610 | __u16 size = 0; |
| 611 | __u32 access_req = 0; |
| 612 | |
| 613 | pntace->type = ACCESS_ALLOWED; |
| 614 | pntace->flags = 0x0; |
| 615 | mode_to_access_flags(nmode, bits, &access_req); |
| 616 | if (!access_req) |
| 617 | access_req = SET_MINIMUM_RIGHTS; |
| 618 | pntace->access_req = cpu_to_le32(access_req); |
| 619 | |
| 620 | pntace->sid.revision = psid->revision; |
| 621 | pntace->sid.num_subauth = psid->num_subauth; |
| 622 | for (i = 0; i < 6; i++) |
| 623 | pntace->sid.authority[i] = psid->authority[i]; |
| 624 | for (i = 0; i < psid->num_subauth; i++) |
| 625 | pntace->sid.sub_auth[i] = psid->sub_auth[i]; |
| 626 | |
| 627 | size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4); |
| 628 | pntace->size = cpu_to_le16(size); |
| 629 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 630 | return size; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 633 | |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 634 | #ifdef CONFIG_CIFS_DEBUG2 |
| 635 | static void dump_ace(struct cifs_ace *pace, char *end_of_acl) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 636 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 637 | int num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 638 | |
| 639 | /* validate that we do not go past end of acl */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 640 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 641 | if (le16_to_cpu(pace->size) < 16) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 642 | cERROR(1, "ACE too small %d", le16_to_cpu(pace->size)); |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
| 646 | if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 647 | cERROR(1, "ACL too small to parse ACE"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 648 | return; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 649 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 650 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 651 | num_subauth = pace->sid.num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 652 | if (num_subauth) { |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 653 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 654 | cFYI(1, "ACE revision %d num_auth %d type %d flags %d size %d", |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 655 | pace->sid.revision, pace->sid.num_subauth, pace->type, |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 656 | pace->flags, le16_to_cpu(pace->size)); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 657 | for (i = 0; i < num_subauth; ++i) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 658 | cFYI(1, "ACE sub_auth[%d]: 0x%x", i, |
| 659 | le32_to_cpu(pace->sid.sub_auth[i])); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 660 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 661 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 662 | /* BB add length check to make sure that we do not have huge |
| 663 | num auths and therefore go off the end */ |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 664 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 665 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 666 | return; |
| 667 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 668 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 669 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 670 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 671 | static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl, |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 672 | struct cifs_sid *pownersid, struct cifs_sid *pgrpsid, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 673 | struct cifs_fattr *fattr) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 674 | { |
| 675 | int i; |
| 676 | int num_aces = 0; |
| 677 | int acl_size; |
| 678 | char *acl_base; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 679 | struct cifs_ace **ppace; |
| 680 | |
| 681 | /* BB need to add parm so we can store the SID BB */ |
| 682 | |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 683 | if (!pdacl) { |
| 684 | /* no DACL in the security descriptor, set |
| 685 | all the permissions for user/group/other */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 686 | fattr->cf_mode |= S_IRWXUGO; |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 690 | /* validate that we do not go past end of acl */ |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 691 | if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 692 | cERROR(1, "ACL too small to parse DACL"); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 696 | cFYI(DBG2, "DACL revision %d size %d num aces %d", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 697 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 698 | le32_to_cpu(pdacl->num_aces)); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 699 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 700 | /* reset rwx permissions for user/group/other. |
| 701 | Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 702 | user/group/other have no permissions */ |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 703 | fattr->cf_mode &= ~(S_IRWXUGO); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 704 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 705 | acl_base = (char *)pdacl; |
| 706 | acl_size = sizeof(struct cifs_acl); |
| 707 | |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 708 | num_aces = le32_to_cpu(pdacl->num_aces); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 709 | if (num_aces > 0) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 710 | umode_t user_mask = S_IRWXU; |
| 711 | umode_t group_mask = S_IRWXG; |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 712 | umode_t other_mask = S_IRWXU | S_IRWXG | S_IRWXO; |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 713 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 714 | ppace = kmalloc(num_aces * sizeof(struct cifs_ace *), |
| 715 | GFP_KERNEL); |
Stanislav Fomichev | 8132b65 | 2011-02-06 02:05:28 +0300 | [diff] [blame] | 716 | if (!ppace) { |
| 717 | cERROR(1, "DACL memory allocation error"); |
| 718 | return; |
| 719 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 720 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 721 | for (i = 0; i < num_aces; ++i) { |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 722 | ppace[i] = (struct cifs_ace *) (acl_base + acl_size); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 723 | #ifdef CONFIG_CIFS_DEBUG2 |
| 724 | dump_ace(ppace[i], end_of_acl); |
| 725 | #endif |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 726 | if (compare_sids(&(ppace[i]->sid), pownersid) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 727 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 728 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 729 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 730 | &user_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 731 | if (compare_sids(&(ppace[i]->sid), pgrpsid) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 732 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 733 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 734 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 735 | &group_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 736 | if (compare_sids(&(ppace[i]->sid), &sid_everyone) == 0) |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 737 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 738 | ppace[i]->type, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 739 | &fattr->cf_mode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 740 | &other_mask); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 741 | if (compare_sids(&(ppace[i]->sid), &sid_authusers) == 0) |
Shirish Pargaonkar | 2fbc2f1 | 2010-12-06 14:56:46 -0600 | [diff] [blame] | 742 | access_flags_to_mode(ppace[i]->access_req, |
| 743 | ppace[i]->type, |
| 744 | &fattr->cf_mode, |
| 745 | &other_mask); |
| 746 | |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 747 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 748 | /* memcpy((void *)(&(cifscred->aces[i])), |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 749 | (void *)ppace[i], |
| 750 | sizeof(struct cifs_ace)); */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 751 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 752 | acl_base = (char *)ppace[i]; |
| 753 | acl_size = le16_to_cpu(ppace[i]->size); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | kfree(ppace); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | return; |
| 760 | } |
| 761 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 762 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 763 | static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid, |
| 764 | struct cifs_sid *pgrpsid, __u64 nmode) |
| 765 | { |
Al Viro | 2b210ad | 2008-03-29 03:09:18 +0000 | [diff] [blame] | 766 | u16 size = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 767 | struct cifs_acl *pnndacl; |
| 768 | |
| 769 | pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl)); |
| 770 | |
| 771 | size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size), |
| 772 | pownersid, nmode, S_IRWXU); |
| 773 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 774 | pgrpsid, nmode, S_IRWXG); |
| 775 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 776 | &sid_everyone, nmode, S_IRWXO); |
| 777 | |
| 778 | pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl)); |
Shirish Pargaonkar | d9f382e | 2008-02-12 20:46:26 +0000 | [diff] [blame] | 779 | pndacl->num_aces = cpu_to_le32(3); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 780 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 781 | return 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 785 | static int parse_sid(struct cifs_sid *psid, char *end_of_acl) |
| 786 | { |
| 787 | /* BB need to add parm so we can store the SID BB */ |
| 788 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 789 | /* validate that we do not go past end of ACL - sid must be at least 8 |
| 790 | bytes long (assuming no sub-auths - e.g. the null SID */ |
| 791 | if (end_of_acl < (char *)psid + 8) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 792 | cERROR(1, "ACL too small to parse SID %p", psid); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 793 | return -EINVAL; |
| 794 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 795 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 796 | if (psid->num_subauth) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 797 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 798 | int i; |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 799 | cFYI(1, "SID revision %d num_auth %d", |
| 800 | psid->revision, psid->num_subauth); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 801 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 802 | for (i = 0; i < psid->num_subauth; i++) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 803 | cFYI(1, "SID sub_auth[%d]: 0x%x ", i, |
| 804 | le32_to_cpu(psid->sub_auth[i])); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 807 | /* BB add length check to make sure that we do not have huge |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 808 | num auths and therefore go off the end */ |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 809 | cFYI(1, "RID 0x%x", |
| 810 | le32_to_cpu(psid->sub_auth[psid->num_subauth-1])); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 811 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 814 | return 0; |
| 815 | } |
| 816 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 817 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 818 | /* Convert CIFS ACL to POSIX form */ |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 819 | static int parse_sec_desc(struct cifs_sb_info *cifs_sb, |
| 820 | struct cifs_ntsd *pntsd, int acl_len, struct cifs_fattr *fattr) |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 821 | { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 822 | int rc = 0; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 823 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 824 | struct cifs_acl *dacl_ptr; /* no need for SACL ptr */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 825 | char *end_of_acl = ((char *)pntsd) + acl_len; |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 826 | __u32 dacloffset; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 827 | |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 828 | if (pntsd == NULL) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 829 | return -EIO; |
| 830 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 831 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 832 | le32_to_cpu(pntsd->osidoffset)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 833 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 834 | le32_to_cpu(pntsd->gsidoffset)); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 835 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 836 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 837 | cFYI(DBG2, "revision %d type 0x%x ooffset 0x%x goffset 0x%x " |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 838 | "sacloffset 0x%x dacloffset 0x%x", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 839 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 840 | le32_to_cpu(pntsd->gsidoffset), |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 841 | le32_to_cpu(pntsd->sacloffset), dacloffset); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 842 | /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 843 | rc = parse_sid(owner_sid_ptr, end_of_acl); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 844 | if (rc) { |
| 845 | cFYI(1, "%s: Error %d parsing Owner SID", __func__, rc); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 846 | return rc; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 847 | } |
| 848 | rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr, SIDOWNER); |
| 849 | if (rc) { |
| 850 | cFYI(1, "%s: Error %d mapping Owner SID to uid", __func__, rc); |
| 851 | return rc; |
| 852 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 853 | |
| 854 | rc = parse_sid(group_sid_ptr, end_of_acl); |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 855 | if (rc) { |
| 856 | cFYI(1, "%s: Error %d mapping Owner SID to gid", __func__, rc); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 857 | return rc; |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 858 | } |
| 859 | rc = sid_to_id(cifs_sb, group_sid_ptr, fattr, SIDGROUP); |
| 860 | if (rc) { |
| 861 | cFYI(1, "%s: Error %d mapping Group SID to gid", __func__, rc); |
| 862 | return rc; |
| 863 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 864 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 865 | if (dacloffset) |
| 866 | parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr, |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 867 | group_sid_ptr, fattr); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 868 | else |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 869 | cFYI(1, "no ACL"); /* BB grant all or default perms? */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 870 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 871 | /* cifscred->uid = owner_sid_ptr->rid; |
| 872 | cifscred->gid = group_sid_ptr->rid; |
| 873 | memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 874 | sizeof(struct cifs_sid)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 875 | memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 876 | sizeof(struct cifs_sid)); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 877 | |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 878 | return rc; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 879 | } |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 880 | |
| 881 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 882 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
| 883 | static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd, |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 884 | struct inode *inode, __u64 nmode) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 885 | { |
| 886 | int rc = 0; |
| 887 | __u32 dacloffset; |
| 888 | __u32 ndacloffset; |
| 889 | __u32 sidsoffset; |
| 890 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 891 | struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 892 | struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */ |
| 893 | |
| 894 | if ((inode == NULL) || (pntsd == NULL) || (pnntsd == NULL)) |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 895 | return -EIO; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 896 | |
| 897 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 898 | le32_to_cpu(pntsd->osidoffset)); |
| 899 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 900 | le32_to_cpu(pntsd->gsidoffset)); |
| 901 | |
| 902 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 903 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
| 904 | |
| 905 | ndacloffset = sizeof(struct cifs_ntsd); |
| 906 | ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset); |
| 907 | ndacl_ptr->revision = dacl_ptr->revision; |
| 908 | ndacl_ptr->size = 0; |
| 909 | ndacl_ptr->num_aces = 0; |
| 910 | |
| 911 | rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr, nmode); |
| 912 | |
| 913 | sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size); |
| 914 | |
| 915 | /* copy security descriptor control portion and owner and group sid */ |
| 916 | copy_sec_desc(pntsd, pnntsd, sidsoffset); |
| 917 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 918 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 921 | static struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb, |
| 922 | __u16 fid, u32 *pacllen) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 923 | { |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 924 | struct cifs_ntsd *pntsd = NULL; |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 925 | int xid, rc; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 926 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
| 927 | |
| 928 | if (IS_ERR(tlink)) |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 929 | return ERR_CAST(tlink); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 930 | |
| 931 | xid = GetXid(); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 932 | rc = CIFSSMBGetCIFSACL(xid, tlink_tcon(tlink), fid, &pntsd, pacllen); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 933 | FreeXid(xid); |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 934 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 935 | cifs_put_tlink(tlink); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 936 | |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 937 | cFYI(1, "%s: rc = %d ACL len %d", __func__, rc, *pacllen); |
| 938 | if (rc) |
| 939 | return ERR_PTR(rc); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 940 | return pntsd; |
| 941 | } |
| 942 | |
| 943 | static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb, |
| 944 | const char *path, u32 *pacllen) |
| 945 | { |
| 946 | struct cifs_ntsd *pntsd = NULL; |
| 947 | int oplock = 0; |
| 948 | int xid, rc; |
| 949 | __u16 fid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 950 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 951 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 952 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 953 | if (IS_ERR(tlink)) |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 954 | return ERR_CAST(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 955 | |
| 956 | tcon = tlink_tcon(tlink); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 957 | xid = GetXid(); |
| 958 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 959 | rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, READ_CONTROL, 0, |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 960 | &fid, &oplock, NULL, cifs_sb->local_nls, |
| 961 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 962 | if (!rc) { |
| 963 | rc = CIFSSMBGetCIFSACL(xid, tcon, fid, &pntsd, pacllen); |
| 964 | CIFSSMBClose(xid, tcon, fid); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 967 | cifs_put_tlink(tlink); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 968 | FreeXid(xid); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 969 | |
| 970 | cFYI(1, "%s: rc = %d ACL len %d", __func__, rc, *pacllen); |
| 971 | if (rc) |
| 972 | return ERR_PTR(rc); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 973 | return pntsd; |
| 974 | } |
| 975 | |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 976 | /* Retrieve an ACL from the server */ |
Shirish Pargaonkar | fbeba8b | 2010-11-27 11:37:54 -0600 | [diff] [blame] | 977 | struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb, |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 978 | struct inode *inode, const char *path, |
| 979 | u32 *pacllen) |
| 980 | { |
| 981 | struct cifs_ntsd *pntsd = NULL; |
| 982 | struct cifsFileInfo *open_file = NULL; |
| 983 | |
| 984 | if (inode) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 985 | open_file = find_readable_file(CIFS_I(inode), true); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 986 | if (!open_file) |
| 987 | return get_cifs_acl_by_path(cifs_sb, path, pacllen); |
| 988 | |
| 989 | pntsd = get_cifs_acl_by_fid(cifs_sb, open_file->netfid, pacllen); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 990 | cifsFileInfo_put(open_file); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 991 | return pntsd; |
| 992 | } |
| 993 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 994 | static int set_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb, __u16 fid, |
| 995 | struct cifs_ntsd *pnntsd, u32 acllen) |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 996 | { |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 997 | int xid, rc; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 998 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
| 999 | |
| 1000 | if (IS_ERR(tlink)) |
| 1001 | return PTR_ERR(tlink); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1002 | |
| 1003 | xid = GetXid(); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1004 | rc = CIFSSMBSetCIFSACL(xid, tlink_tcon(tlink), fid, pnntsd, acllen); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1005 | FreeXid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1006 | cifs_put_tlink(tlink); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1007 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1008 | cFYI(DBG2, "SetCIFSACL rc = %d", rc); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static int set_cifs_acl_by_path(struct cifs_sb_info *cifs_sb, const char *path, |
| 1013 | struct cifs_ntsd *pnntsd, u32 acllen) |
| 1014 | { |
| 1015 | int oplock = 0; |
| 1016 | int xid, rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1017 | __u16 fid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1018 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1019 | struct tcon_link *tlink = cifs_sb_tlink(cifs_sb); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1020 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1021 | if (IS_ERR(tlink)) |
| 1022 | return PTR_ERR(tlink); |
| 1023 | |
| 1024 | tcon = tlink_tcon(tlink); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1025 | xid = GetXid(); |
| 1026 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1027 | rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, WRITE_DAC, 0, |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1028 | &fid, &oplock, NULL, cifs_sb->local_nls, |
| 1029 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1030 | if (rc) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1031 | cERROR(1, "Unable to open file to set ACL"); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1032 | goto out; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1035 | rc = CIFSSMBSetCIFSACL(xid, tcon, fid, pnntsd, acllen); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1036 | cFYI(DBG2, "SetCIFSACL rc = %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1037 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1038 | CIFSSMBClose(xid, tcon, fid); |
| 1039 | out: |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1040 | FreeXid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1041 | cifs_put_tlink(tlink); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1042 | return rc; |
| 1043 | } |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1044 | |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1045 | /* Set an ACL on the server */ |
Steve French | b73b9a4 | 2011-04-19 18:27:10 +0000 | [diff] [blame] | 1046 | int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen, |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1047 | struct inode *inode, const char *path) |
| 1048 | { |
| 1049 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1050 | struct cifsFileInfo *open_file; |
| 1051 | int rc; |
| 1052 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1053 | cFYI(DBG2, "set ACL for %s from mode 0x%x", path, inode->i_mode); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1054 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1055 | open_file = find_readable_file(CIFS_I(inode), true); |
Christoph Hellwig | b96d31a | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1056 | if (!open_file) |
| 1057 | return set_cifs_acl_by_path(cifs_sb, path, pnntsd, acllen); |
| 1058 | |
| 1059 | rc = set_cifs_acl_by_fid(cifs_sb, open_file->netfid, pnntsd, acllen); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 1060 | cifsFileInfo_put(open_file); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 1061 | return rc; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1064 | /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */ |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 1065 | int |
Jeff Layton | 0b8f18e | 2009-07-09 01:46:37 -0400 | [diff] [blame] | 1066 | cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr, |
| 1067 | struct inode *inode, const char *path, const __u16 *pfid) |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1068 | { |
| 1069 | struct cifs_ntsd *pntsd = NULL; |
| 1070 | u32 acllen = 0; |
| 1071 | int rc = 0; |
| 1072 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1073 | cFYI(DBG2, "converting ACL to mode for %s", path); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1074 | |
| 1075 | if (pfid) |
| 1076 | pntsd = get_cifs_acl_by_fid(cifs_sb, *pfid, &acllen); |
| 1077 | else |
| 1078 | pntsd = get_cifs_acl(cifs_sb, inode, path, &acllen); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */ |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 1081 | if (IS_ERR(pntsd)) { |
| 1082 | rc = PTR_ERR(pntsd); |
| 1083 | cERROR(1, "%s: error %d getting sec desc", __func__, rc); |
| 1084 | } else { |
Shirish Pargaonkar | 9409ae5 | 2011-04-22 12:09:36 -0500 | [diff] [blame] | 1085 | rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr); |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 1086 | kfree(pntsd); |
| 1087 | if (rc) |
| 1088 | cERROR(1, "parse sec desc failed rc = %d", rc); |
| 1089 | } |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1090 | |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 1091 | return rc; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 1092 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1093 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1094 | /* Convert mode bits to an ACL so we can update the ACL on the server */ |
Shirish Pargaonkar | 78415d2 | 2010-11-27 11:37:26 -0600 | [diff] [blame] | 1095 | int mode_to_cifs_acl(struct inode *inode, const char *path, __u64 nmode) |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1096 | { |
| 1097 | int rc = 0; |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 1098 | __u32 secdesclen = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1099 | struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */ |
| 1100 | struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1101 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1102 | cFYI(DBG2, "set ACL from mode for %s", path); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* Get the security descriptor */ |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 1105 | pntsd = get_cifs_acl(CIFS_SB(inode->i_sb), inode, path, &secdesclen); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1106 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1107 | /* Add three ACEs for owner, group, everyone getting rid of |
| 1108 | other ACEs as chmod disables ACEs and set the security descriptor */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1109 | |
Shirish Pargaonkar | 987b21d | 2010-11-10 07:50:35 -0600 | [diff] [blame] | 1110 | if (IS_ERR(pntsd)) { |
| 1111 | rc = PTR_ERR(pntsd); |
| 1112 | cERROR(1, "%s: error %d getting sec desc", __func__, rc); |
| 1113 | } else { |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1114 | /* allocate memory for the smb header, |
| 1115 | set security descriptor request security descriptor |
| 1116 | parameters, and secuirty descriptor itself */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1117 | |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 1118 | secdesclen = secdesclen < DEFSECDESCLEN ? |
| 1119 | DEFSECDESCLEN : secdesclen; |
| 1120 | pnntsd = kmalloc(secdesclen, GFP_KERNEL); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1121 | if (!pnntsd) { |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1122 | cERROR(1, "Unable to allocate security descriptor"); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1123 | kfree(pntsd); |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 1124 | return -ENOMEM; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1125 | } |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 1126 | |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 1127 | rc = build_sec_desc(pntsd, pnntsd, inode, nmode); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1128 | |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1129 | cFYI(DBG2, "build_sec_desc rc: %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1130 | |
| 1131 | if (!rc) { |
| 1132 | /* Set the security descriptor */ |
Steve French | cce246e | 2008-04-09 20:55:31 +0000 | [diff] [blame] | 1133 | rc = set_cifs_acl(pnntsd, secdesclen, inode, path); |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 1134 | cFYI(DBG2, "set_cifs_acl rc: %d", rc); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | kfree(pnntsd); |
| 1138 | kfree(pntsd); |
| 1139 | } |
| 1140 | |
Shirish Pargaonkar | ef571ca | 2008-07-24 15:56:05 +0000 | [diff] [blame] | 1141 | return rc; |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 1142 | } |