cifsacl.c 30 KB

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