cifsacl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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}, {cpu_to_le32(11)} };
  40. /* group users */
  41. static const struct cifs_sid sid_user = {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
  42. /* S-1-22-1 Unmapped Unix users */
  43. static const struct cifs_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22},
  44. {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  45. /* S-1-22-2 Unmapped Unix groups */
  46. static const struct cifs_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22},
  47. {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  48. /*
  49. * See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  50. */
  51. /* S-1-5-88 MS NFS and Apple style UID/GID/mode */
  52. /* S-1-5-88-1 Unix uid */
  53. static const struct cifs_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5},
  54. {cpu_to_le32(88),
  55. cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  56. /* S-1-5-88-2 Unix gid */
  57. static const struct cifs_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5},
  58. {cpu_to_le32(88),
  59. cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  60. /* S-1-5-88-3 Unix mode */
  61. static const struct cifs_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5},
  62. {cpu_to_le32(88),
  63. cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  64. static const struct cred *root_cred;
  65. static int
  66. cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
  67. {
  68. char *payload;
  69. /*
  70. * If the payload is less than or equal to the size of a pointer, then
  71. * an allocation here is wasteful. Just copy the data directly to the
  72. * payload.value union member instead.
  73. *
  74. * With this however, you must check the datalen before trying to
  75. * dereference payload.data!
  76. */
  77. if (prep->datalen <= sizeof(key->payload)) {
  78. key->payload.data[0] = NULL;
  79. memcpy(&key->payload, prep->data, prep->datalen);
  80. } else {
  81. payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
  82. if (!payload)
  83. return -ENOMEM;
  84. key->payload.data[0] = payload;
  85. }
  86. key->datalen = prep->datalen;
  87. return 0;
  88. }
  89. static inline void
  90. cifs_idmap_key_destroy(struct key *key)
  91. {
  92. if (key->datalen > sizeof(key->payload))
  93. kfree(key->payload.data[0]);
  94. }
  95. static struct key_type cifs_idmap_key_type = {
  96. .name = "cifs.idmap",
  97. .instantiate = cifs_idmap_key_instantiate,
  98. .destroy = cifs_idmap_key_destroy,
  99. .describe = user_describe,
  100. };
  101. static char *
  102. sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
  103. {
  104. int i, len;
  105. unsigned int saval;
  106. char *sidstr, *strptr;
  107. unsigned long long id_auth_val;
  108. /* 3 bytes for prefix */
  109. sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
  110. (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
  111. GFP_KERNEL);
  112. if (!sidstr)
  113. return sidstr;
  114. strptr = sidstr;
  115. len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
  116. sidptr->revision);
  117. strptr += len;
  118. /* The authority field is a single 48-bit number */
  119. id_auth_val = (unsigned long long)sidptr->authority[5];
  120. id_auth_val |= (unsigned long long)sidptr->authority[4] << 8;
  121. id_auth_val |= (unsigned long long)sidptr->authority[3] << 16;
  122. id_auth_val |= (unsigned long long)sidptr->authority[2] << 24;
  123. id_auth_val |= (unsigned long long)sidptr->authority[1] << 32;
  124. id_auth_val |= (unsigned long long)sidptr->authority[0] << 48;
  125. /*
  126. * MS-DTYP states that if the authority is >= 2^32, then it should be
  127. * expressed as a hex value.
  128. */
  129. if (id_auth_val <= UINT_MAX)
  130. len = sprintf(strptr, "-%llu", id_auth_val);
  131. else
  132. len = sprintf(strptr, "-0x%llx", id_auth_val);
  133. strptr += len;
  134. for (i = 0; i < sidptr->num_subauth; ++i) {
  135. saval = le32_to_cpu(sidptr->sub_auth[i]);
  136. len = sprintf(strptr, "-%u", saval);
  137. strptr += len;
  138. }
  139. return sidstr;
  140. }
  141. /*
  142. * if the two SIDs (roughly equivalent to a UUID for a user or group) are
  143. * the same returns zero, if they do not match returns non-zero.
  144. */
  145. static int
  146. compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)
  147. {
  148. int i;
  149. int num_subauth, num_sat, num_saw;
  150. if ((!ctsid) || (!cwsid))
  151. return 1;
  152. /* compare the revision */
  153. if (ctsid->revision != cwsid->revision) {
  154. if (ctsid->revision > cwsid->revision)
  155. return 1;
  156. else
  157. return -1;
  158. }
  159. /* compare all of the six auth values */
  160. for (i = 0; i < NUM_AUTHS; ++i) {
  161. if (ctsid->authority[i] != cwsid->authority[i]) {
  162. if (ctsid->authority[i] > cwsid->authority[i])
  163. return 1;
  164. else
  165. return -1;
  166. }
  167. }
  168. /* compare all of the subauth values if any */
  169. num_sat = ctsid->num_subauth;
  170. num_saw = cwsid->num_subauth;
  171. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  172. if (num_subauth) {
  173. for (i = 0; i < num_subauth; ++i) {
  174. if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) {
  175. if (le32_to_cpu(ctsid->sub_auth[i]) >
  176. le32_to_cpu(cwsid->sub_auth[i]))
  177. return 1;
  178. else
  179. return -1;
  180. }
  181. }
  182. }
  183. return 0; /* sids compare/match */
  184. }
  185. static bool
  186. is_well_known_sid(const struct cifs_sid *psid, uint32_t *puid, bool is_group)
  187. {
  188. int i;
  189. int num_subauth;
  190. const struct cifs_sid *pwell_known_sid;
  191. if (!psid || (puid == NULL))
  192. return false;
  193. num_subauth = psid->num_subauth;
  194. /* check if Mac (or Windows NFS) vs. Samba format for Unix owner SID */
  195. if (num_subauth == 2) {
  196. if (is_group)
  197. pwell_known_sid = &sid_unix_groups;
  198. else
  199. pwell_known_sid = &sid_unix_users;
  200. } else if (num_subauth == 3) {
  201. if (is_group)
  202. pwell_known_sid = &sid_unix_NFS_groups;
  203. else
  204. pwell_known_sid = &sid_unix_NFS_users;
  205. } else
  206. return false;
  207. /* compare the revision */
  208. if (psid->revision != pwell_known_sid->revision)
  209. return false;
  210. /* compare all of the six auth values */
  211. for (i = 0; i < NUM_AUTHS; ++i) {
  212. if (psid->authority[i] != pwell_known_sid->authority[i]) {
  213. cifs_dbg(FYI, "auth %d did not match\n", i);
  214. return false;
  215. }
  216. }
  217. if (num_subauth == 2) {
  218. if (psid->sub_auth[0] != pwell_known_sid->sub_auth[0])
  219. return false;
  220. *puid = le32_to_cpu(psid->sub_auth[1]);
  221. } else /* 3 subauths, ie Windows/Mac style */ {
  222. *puid = le32_to_cpu(psid->sub_auth[0]);
  223. if ((psid->sub_auth[0] != pwell_known_sid->sub_auth[0]) ||
  224. (psid->sub_auth[1] != pwell_known_sid->sub_auth[1]))
  225. return false;
  226. *puid = le32_to_cpu(psid->sub_auth[2]);
  227. }
  228. cifs_dbg(FYI, "Unix UID %d returned from SID\n", *puid);
  229. return true; /* well known sid found, uid returned */
  230. }
  231. static void
  232. cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
  233. {
  234. int i;
  235. dst->revision = src->revision;
  236. dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
  237. for (i = 0; i < NUM_AUTHS; ++i)
  238. dst->authority[i] = src->authority[i];
  239. for (i = 0; i < dst->num_subauth; ++i)
  240. dst->sub_auth[i] = src->sub_auth[i];
  241. }
  242. static int
  243. id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
  244. {
  245. int rc;
  246. struct key *sidkey;
  247. struct cifs_sid *ksid;
  248. unsigned int ksid_size;
  249. char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
  250. const struct cred *saved_cred;
  251. rc = snprintf(desc, sizeof(desc), "%ci:%u",
  252. sidtype == SIDOWNER ? 'o' : 'g', cid);
  253. if (rc >= sizeof(desc))
  254. return -EINVAL;
  255. rc = 0;
  256. saved_cred = override_creds(root_cred);
  257. sidkey = request_key(&cifs_idmap_key_type, desc, "");
  258. if (IS_ERR(sidkey)) {
  259. rc = -EINVAL;
  260. cifs_dbg(FYI, "%s: Can't map %cid %u to a SID\n",
  261. __func__, sidtype == SIDOWNER ? 'u' : 'g', cid);
  262. goto out_revert_creds;
  263. } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
  264. rc = -EIO;
  265. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  266. __func__, sidkey->datalen);
  267. goto invalidate_key;
  268. }
  269. /*
  270. * A sid is usually too large to be embedded in payload.value, but if
  271. * there are no subauthorities and the host has 8-byte pointers, then
  272. * it could be.
  273. */
  274. ksid = sidkey->datalen <= sizeof(sidkey->payload) ?
  275. (struct cifs_sid *)&sidkey->payload :
  276. (struct cifs_sid *)sidkey->payload.data[0];
  277. ksid_size = CIFS_SID_BASE_SIZE + (ksid->num_subauth * sizeof(__le32));
  278. if (ksid_size > sidkey->datalen) {
  279. rc = -EIO;
  280. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu, ksid_size=%u)\n",
  281. __func__, sidkey->datalen, ksid_size);
  282. goto invalidate_key;
  283. }
  284. cifs_copy_sid(ssid, ksid);
  285. out_key_put:
  286. key_put(sidkey);
  287. out_revert_creds:
  288. revert_creds(saved_cred);
  289. return rc;
  290. invalidate_key:
  291. key_invalidate(sidkey);
  292. goto out_key_put;
  293. }
  294. static int
  295. sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
  296. struct cifs_fattr *fattr, uint sidtype)
  297. {
  298. int rc;
  299. struct key *sidkey;
  300. char *sidstr;
  301. const struct cred *saved_cred;
  302. kuid_t fuid = cifs_sb->mnt_uid;
  303. kgid_t fgid = cifs_sb->mnt_gid;
  304. /*
  305. * If we have too many subauthorities, then something is really wrong.
  306. * Just return an error.
  307. */
  308. if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
  309. cifs_dbg(FYI, "%s: %u subauthorities is too many!\n",
  310. __func__, psid->num_subauth);
  311. return -EIO;
  312. }
  313. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL) {
  314. uint32_t unix_id;
  315. bool is_group;
  316. if (sidtype != SIDOWNER)
  317. is_group = true;
  318. else
  319. is_group = false;
  320. if (is_well_known_sid(psid, &unix_id, is_group) == false)
  321. goto try_upcall_to_get_id;
  322. if (is_group) {
  323. kgid_t gid;
  324. gid_t id;
  325. id = (gid_t)unix_id;
  326. gid = make_kgid(&init_user_ns, id);
  327. if (gid_valid(gid)) {
  328. fgid = gid;
  329. goto got_valid_id;
  330. }
  331. } else {
  332. kuid_t uid;
  333. uid_t id;
  334. id = (uid_t)unix_id;
  335. uid = make_kuid(&init_user_ns, id);
  336. if (uid_valid(uid)) {
  337. fuid = uid;
  338. goto got_valid_id;
  339. }
  340. }
  341. /* If unable to find uid/gid easily from SID try via upcall */
  342. }
  343. try_upcall_to_get_id:
  344. sidstr = sid_to_key_str(psid, sidtype);
  345. if (!sidstr)
  346. return -ENOMEM;
  347. saved_cred = override_creds(root_cred);
  348. sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
  349. if (IS_ERR(sidkey)) {
  350. rc = -EINVAL;
  351. cifs_dbg(FYI, "%s: Can't map SID %s to a %cid\n",
  352. __func__, sidstr, sidtype == SIDOWNER ? 'u' : 'g');
  353. goto out_revert_creds;
  354. }
  355. /*
  356. * FIXME: Here we assume that uid_t and gid_t are same size. It's
  357. * probably a safe assumption but might be better to check based on
  358. * sidtype.
  359. */
  360. BUILD_BUG_ON(sizeof(uid_t) != sizeof(gid_t));
  361. if (sidkey->datalen != sizeof(uid_t)) {
  362. rc = -EIO;
  363. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  364. __func__, sidkey->datalen);
  365. key_invalidate(sidkey);
  366. goto out_key_put;
  367. }
  368. if (sidtype == SIDOWNER) {
  369. kuid_t uid;
  370. uid_t id;
  371. memcpy(&id, &sidkey->payload.data[0], sizeof(uid_t));
  372. uid = make_kuid(&init_user_ns, id);
  373. if (uid_valid(uid))
  374. fuid = uid;
  375. } else {
  376. kgid_t gid;
  377. gid_t id;
  378. memcpy(&id, &sidkey->payload.data[0], sizeof(gid_t));
  379. gid = make_kgid(&init_user_ns, id);
  380. if (gid_valid(gid))
  381. fgid = gid;
  382. }
  383. out_key_put:
  384. key_put(sidkey);
  385. out_revert_creds:
  386. revert_creds(saved_cred);
  387. kfree(sidstr);
  388. /*
  389. * Note that we return 0 here unconditionally. If the mapping
  390. * fails then we just fall back to using the mnt_uid/mnt_gid.
  391. */
  392. got_valid_id:
  393. if (sidtype == SIDOWNER)
  394. fattr->cf_uid = fuid;
  395. else
  396. fattr->cf_gid = fgid;
  397. return 0;
  398. }
  399. int
  400. init_cifs_idmap(void)
  401. {
  402. struct cred *cred;
  403. struct key *keyring;
  404. int ret;
  405. cifs_dbg(FYI, "Registering the %s key type\n",
  406. cifs_idmap_key_type.name);
  407. /* create an override credential set with a special thread keyring in
  408. * which requests are cached
  409. *
  410. * this is used to prevent malicious redirections from being installed
  411. * with add_key().
  412. */
  413. cred = prepare_kernel_cred(NULL);
  414. if (!cred)
  415. return -ENOMEM;
  416. keyring = keyring_alloc(".cifs_idmap",
  417. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  418. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  419. KEY_USR_VIEW | KEY_USR_READ,
  420. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  421. if (IS_ERR(keyring)) {
  422. ret = PTR_ERR(keyring);
  423. goto failed_put_cred;
  424. }
  425. ret = register_key_type(&cifs_idmap_key_type);
  426. if (ret < 0)
  427. goto failed_put_key;
  428. /* instruct request_key() to use this special keyring as a cache for
  429. * the results it looks up */
  430. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  431. cred->thread_keyring = keyring;
  432. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  433. root_cred = cred;
  434. cifs_dbg(FYI, "cifs idmap keyring: %d\n", key_serial(keyring));
  435. return 0;
  436. failed_put_key:
  437. key_put(keyring);
  438. failed_put_cred:
  439. put_cred(cred);
  440. return ret;
  441. }
  442. void
  443. exit_cifs_idmap(void)
  444. {
  445. key_revoke(root_cred->thread_keyring);
  446. unregister_key_type(&cifs_idmap_key_type);
  447. put_cred(root_cred);
  448. cifs_dbg(FYI, "Unregistered %s key type\n", cifs_idmap_key_type.name);
  449. }
  450. /* copy ntsd, owner sid, and group sid from a security descriptor to another */
  451. static void copy_sec_desc(const struct cifs_ntsd *pntsd,
  452. struct cifs_ntsd *pnntsd, __u32 sidsoffset)
  453. {
  454. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  455. struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
  456. /* copy security descriptor control portion */
  457. pnntsd->revision = pntsd->revision;
  458. pnntsd->type = pntsd->type;
  459. pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd));
  460. pnntsd->sacloffset = 0;
  461. pnntsd->osidoffset = cpu_to_le32(sidsoffset);
  462. pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid));
  463. /* copy owner sid */
  464. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  465. le32_to_cpu(pntsd->osidoffset));
  466. nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset);
  467. cifs_copy_sid(nowner_sid_ptr, owner_sid_ptr);
  468. /* copy group sid */
  469. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  470. le32_to_cpu(pntsd->gsidoffset));
  471. ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset +
  472. sizeof(struct cifs_sid));
  473. cifs_copy_sid(ngroup_sid_ptr, group_sid_ptr);
  474. return;
  475. }
  476. /*
  477. change posix mode to reflect permissions
  478. pmode is the existing mode (we only want to overwrite part of this
  479. bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007
  480. */
  481. static void access_flags_to_mode(__le32 ace_flags, int type, umode_t *pmode,
  482. umode_t *pbits_to_set)
  483. {
  484. __u32 flags = le32_to_cpu(ace_flags);
  485. /* the order of ACEs is important. The canonical order is to begin with
  486. DENY entries followed by ALLOW, otherwise an allow entry could be
  487. encountered first, making the subsequent deny entry like "dead code"
  488. which would be superflous since Windows stops when a match is made
  489. for the operation you are trying to perform for your user */
  490. /* For deny ACEs we change the mask so that subsequent allow access
  491. control entries do not turn on the bits we are denying */
  492. if (type == ACCESS_DENIED) {
  493. if (flags & GENERIC_ALL)
  494. *pbits_to_set &= ~S_IRWXUGO;
  495. if ((flags & GENERIC_WRITE) ||
  496. ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS))
  497. *pbits_to_set &= ~S_IWUGO;
  498. if ((flags & GENERIC_READ) ||
  499. ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS))
  500. *pbits_to_set &= ~S_IRUGO;
  501. if ((flags & GENERIC_EXECUTE) ||
  502. ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS))
  503. *pbits_to_set &= ~S_IXUGO;
  504. return;
  505. } else if (type != ACCESS_ALLOWED) {
  506. cifs_dbg(VFS, "unknown access control type %d\n", type);
  507. return;
  508. }
  509. /* else ACCESS_ALLOWED type */
  510. if (flags & GENERIC_ALL) {
  511. *pmode |= (S_IRWXUGO & (*pbits_to_set));
  512. cifs_dbg(NOISY, "all perms\n");
  513. return;
  514. }
  515. if ((flags & GENERIC_WRITE) ||
  516. ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS))
  517. *pmode |= (S_IWUGO & (*pbits_to_set));
  518. if ((flags & GENERIC_READ) ||
  519. ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS))
  520. *pmode |= (S_IRUGO & (*pbits_to_set));
  521. if ((flags & GENERIC_EXECUTE) ||
  522. ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS))
  523. *pmode |= (S_IXUGO & (*pbits_to_set));
  524. cifs_dbg(NOISY, "access flags 0x%x mode now 0x%x\n", flags, *pmode);
  525. return;
  526. }
  527. /*
  528. Generate access flags to reflect permissions mode is the existing mode.
  529. This function is called for every ACE in the DACL whose SID matches
  530. with either owner or group or everyone.
  531. */
  532. static void mode_to_access_flags(umode_t mode, umode_t bits_to_use,
  533. __u32 *pace_flags)
  534. {
  535. /* reset access mask */
  536. *pace_flags = 0x0;
  537. /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */
  538. mode &= bits_to_use;
  539. /* check for R/W/X UGO since we do not know whose flags
  540. is this but we have cleared all the bits sans RWX for
  541. either user or group or other as per bits_to_use */
  542. if (mode & S_IRUGO)
  543. *pace_flags |= SET_FILE_READ_RIGHTS;
  544. if (mode & S_IWUGO)
  545. *pace_flags |= SET_FILE_WRITE_RIGHTS;
  546. if (mode & S_IXUGO)
  547. *pace_flags |= SET_FILE_EXEC_RIGHTS;
  548. cifs_dbg(NOISY, "mode: 0x%x, access flags now 0x%x\n",
  549. mode, *pace_flags);
  550. return;
  551. }
  552. static __u16 fill_ace_for_sid(struct cifs_ace *pntace,
  553. const struct cifs_sid *psid, __u64 nmode, umode_t bits)
  554. {
  555. int i;
  556. __u16 size = 0;
  557. __u32 access_req = 0;
  558. pntace->type = ACCESS_ALLOWED;
  559. pntace->flags = 0x0;
  560. mode_to_access_flags(nmode, bits, &access_req);
  561. if (!access_req)
  562. access_req = SET_MINIMUM_RIGHTS;
  563. pntace->access_req = cpu_to_le32(access_req);
  564. pntace->sid.revision = psid->revision;
  565. pntace->sid.num_subauth = psid->num_subauth;
  566. for (i = 0; i < NUM_AUTHS; i++)
  567. pntace->sid.authority[i] = psid->authority[i];
  568. for (i = 0; i < psid->num_subauth; i++)
  569. pntace->sid.sub_auth[i] = psid->sub_auth[i];
  570. size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4);
  571. pntace->size = cpu_to_le16(size);
  572. return size;
  573. }
  574. #ifdef CONFIG_CIFS_DEBUG2
  575. static void dump_ace(struct cifs_ace *pace, char *end_of_acl)
  576. {
  577. int num_subauth;
  578. /* validate that we do not go past end of acl */
  579. if (le16_to_cpu(pace->size) < 16) {
  580. cifs_dbg(VFS, "ACE too small %d\n", le16_to_cpu(pace->size));
  581. return;
  582. }
  583. if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) {
  584. cifs_dbg(VFS, "ACL too small to parse ACE\n");
  585. return;
  586. }
  587. num_subauth = pace->sid.num_subauth;
  588. if (num_subauth) {
  589. int i;
  590. cifs_dbg(FYI, "ACE revision %d num_auth %d type %d flags %d size %d\n",
  591. pace->sid.revision, pace->sid.num_subauth, pace->type,
  592. pace->flags, le16_to_cpu(pace->size));
  593. for (i = 0; i < num_subauth; ++i) {
  594. cifs_dbg(FYI, "ACE sub_auth[%d]: 0x%x\n",
  595. i, le32_to_cpu(pace->sid.sub_auth[i]));
  596. }
  597. /* BB add length check to make sure that we do not have huge
  598. num auths and therefore go off the end */
  599. }
  600. return;
  601. }
  602. #endif
  603. static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl,
  604. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  605. struct cifs_fattr *fattr)
  606. {
  607. int i;
  608. int num_aces = 0;
  609. int acl_size;
  610. char *acl_base;
  611. struct cifs_ace **ppace;
  612. /* BB need to add parm so we can store the SID BB */
  613. if (!pdacl) {
  614. /* no DACL in the security descriptor, set
  615. all the permissions for user/group/other */
  616. fattr->cf_mode |= S_IRWXUGO;
  617. return;
  618. }
  619. /* validate that we do not go past end of acl */
  620. if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  621. cifs_dbg(VFS, "ACL too small to parse DACL\n");
  622. return;
  623. }
  624. cifs_dbg(NOISY, "DACL revision %d size %d num aces %d\n",
  625. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  626. le32_to_cpu(pdacl->num_aces));
  627. /* reset rwx permissions for user/group/other.
  628. Also, if num_aces is 0 i.e. DACL has no ACEs,
  629. user/group/other have no permissions */
  630. fattr->cf_mode &= ~(S_IRWXUGO);
  631. acl_base = (char *)pdacl;
  632. acl_size = sizeof(struct cifs_acl);
  633. num_aces = le32_to_cpu(pdacl->num_aces);
  634. if (num_aces > 0) {
  635. umode_t user_mask = S_IRWXU;
  636. umode_t group_mask = S_IRWXG;
  637. umode_t other_mask = S_IRWXU | S_IRWXG | S_IRWXO;
  638. if (num_aces > ULONG_MAX / sizeof(struct cifs_ace *))
  639. return;
  640. ppace = kmalloc(num_aces * sizeof(struct cifs_ace *),
  641. GFP_KERNEL);
  642. if (!ppace)
  643. return;
  644. for (i = 0; i < num_aces; ++i) {
  645. ppace[i] = (struct cifs_ace *) (acl_base + acl_size);
  646. #ifdef CONFIG_CIFS_DEBUG2
  647. dump_ace(ppace[i], end_of_acl);
  648. #endif
  649. if (compare_sids(&(ppace[i]->sid), pownersid) == 0)
  650. access_flags_to_mode(ppace[i]->access_req,
  651. ppace[i]->type,
  652. &fattr->cf_mode,
  653. &user_mask);
  654. if (compare_sids(&(ppace[i]->sid), pgrpsid) == 0)
  655. access_flags_to_mode(ppace[i]->access_req,
  656. ppace[i]->type,
  657. &fattr->cf_mode,
  658. &group_mask);
  659. if (compare_sids(&(ppace[i]->sid), &sid_everyone) == 0)
  660. access_flags_to_mode(ppace[i]->access_req,
  661. ppace[i]->type,
  662. &fattr->cf_mode,
  663. &other_mask);
  664. if (compare_sids(&(ppace[i]->sid), &sid_authusers) == 0)
  665. access_flags_to_mode(ppace[i]->access_req,
  666. ppace[i]->type,
  667. &fattr->cf_mode,
  668. &other_mask);
  669. /* memcpy((void *)(&(cifscred->aces[i])),
  670. (void *)ppace[i],
  671. sizeof(struct cifs_ace)); */
  672. acl_base = (char *)ppace[i];
  673. acl_size = le16_to_cpu(ppace[i]->size);
  674. }
  675. kfree(ppace);
  676. }
  677. return;
  678. }
  679. static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid,
  680. struct cifs_sid *pgrpsid, __u64 nmode)
  681. {
  682. u16 size = 0;
  683. struct cifs_acl *pnndacl;
  684. pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl));
  685. size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size),
  686. pownersid, nmode, S_IRWXU);
  687. size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size),
  688. pgrpsid, nmode, S_IRWXG);
  689. size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size),
  690. &sid_everyone, nmode, S_IRWXO);
  691. pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl));
  692. pndacl->num_aces = cpu_to_le32(3);
  693. return 0;
  694. }
  695. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  696. {
  697. /* BB need to add parm so we can store the SID BB */
  698. /* validate that we do not go past end of ACL - sid must be at least 8
  699. bytes long (assuming no sub-auths - e.g. the null SID */
  700. if (end_of_acl < (char *)psid + 8) {
  701. cifs_dbg(VFS, "ACL too small to parse SID %p\n", psid);
  702. return -EINVAL;
  703. }
  704. #ifdef CONFIG_CIFS_DEBUG2
  705. if (psid->num_subauth) {
  706. int i;
  707. cifs_dbg(FYI, "SID revision %d num_auth %d\n",
  708. psid->revision, psid->num_subauth);
  709. for (i = 0; i < psid->num_subauth; i++) {
  710. cifs_dbg(FYI, "SID sub_auth[%d]: 0x%x\n",
  711. i, le32_to_cpu(psid->sub_auth[i]));
  712. }
  713. /* BB add length check to make sure that we do not have huge
  714. num auths and therefore go off the end */
  715. cifs_dbg(FYI, "RID 0x%x\n",
  716. le32_to_cpu(psid->sub_auth[psid->num_subauth-1]));
  717. }
  718. #endif
  719. return 0;
  720. }
  721. /* Convert CIFS ACL to POSIX form */
  722. static int parse_sec_desc(struct cifs_sb_info *cifs_sb,
  723. struct cifs_ntsd *pntsd, int acl_len, struct cifs_fattr *fattr)
  724. {
  725. int rc = 0;
  726. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  727. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  728. char *end_of_acl = ((char *)pntsd) + acl_len;
  729. __u32 dacloffset;
  730. if (pntsd == NULL)
  731. return -EIO;
  732. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  733. le32_to_cpu(pntsd->osidoffset));
  734. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  735. le32_to_cpu(pntsd->gsidoffset));
  736. dacloffset = le32_to_cpu(pntsd->dacloffset);
  737. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  738. cifs_dbg(NOISY, "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
  739. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  740. le32_to_cpu(pntsd->gsidoffset),
  741. le32_to_cpu(pntsd->sacloffset), dacloffset);
  742. /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */
  743. rc = parse_sid(owner_sid_ptr, end_of_acl);
  744. if (rc) {
  745. cifs_dbg(FYI, "%s: Error %d parsing Owner SID\n", __func__, rc);
  746. return rc;
  747. }
  748. rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr, SIDOWNER);
  749. if (rc) {
  750. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to uid\n",
  751. __func__, rc);
  752. return rc;
  753. }
  754. rc = parse_sid(group_sid_ptr, end_of_acl);
  755. if (rc) {
  756. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to gid\n",
  757. __func__, rc);
  758. return rc;
  759. }
  760. rc = sid_to_id(cifs_sb, group_sid_ptr, fattr, SIDGROUP);
  761. if (rc) {
  762. cifs_dbg(FYI, "%s: Error %d mapping Group SID to gid\n",
  763. __func__, rc);
  764. return rc;
  765. }
  766. if (dacloffset)
  767. parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr,
  768. group_sid_ptr, fattr);
  769. else
  770. cifs_dbg(FYI, "no ACL\n"); /* BB grant all or default perms? */
  771. return rc;
  772. }
  773. /* Convert permission bits from mode to equivalent CIFS ACL */
  774. static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd,
  775. __u32 secdesclen, __u64 nmode, kuid_t uid, kgid_t gid, int *aclflag)
  776. {
  777. int rc = 0;
  778. __u32 dacloffset;
  779. __u32 ndacloffset;
  780. __u32 sidsoffset;
  781. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  782. struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
  783. struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */
  784. struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */
  785. if (nmode != NO_CHANGE_64) { /* chmod */
  786. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  787. le32_to_cpu(pntsd->osidoffset));
  788. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  789. le32_to_cpu(pntsd->gsidoffset));
  790. dacloffset = le32_to_cpu(pntsd->dacloffset);
  791. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  792. ndacloffset = sizeof(struct cifs_ntsd);
  793. ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset);
  794. ndacl_ptr->revision = dacl_ptr->revision;
  795. ndacl_ptr->size = 0;
  796. ndacl_ptr->num_aces = 0;
  797. rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr,
  798. nmode);
  799. sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size);
  800. /* copy sec desc control portion & owner and group sids */
  801. copy_sec_desc(pntsd, pnntsd, sidsoffset);
  802. *aclflag = CIFS_ACL_DACL;
  803. } else {
  804. memcpy(pnntsd, pntsd, secdesclen);
  805. if (uid_valid(uid)) { /* chown */
  806. uid_t id;
  807. owner_sid_ptr = (struct cifs_sid *)((char *)pnntsd +
  808. le32_to_cpu(pnntsd->osidoffset));
  809. nowner_sid_ptr = kmalloc(sizeof(struct cifs_sid),
  810. GFP_KERNEL);
  811. if (!nowner_sid_ptr)
  812. return -ENOMEM;
  813. id = from_kuid(&init_user_ns, uid);
  814. rc = id_to_sid(id, SIDOWNER, nowner_sid_ptr);
  815. if (rc) {
  816. cifs_dbg(FYI, "%s: Mapping error %d for owner id %d\n",
  817. __func__, rc, id);
  818. kfree(nowner_sid_ptr);
  819. return rc;
  820. }
  821. cifs_copy_sid(owner_sid_ptr, nowner_sid_ptr);
  822. kfree(nowner_sid_ptr);
  823. *aclflag = CIFS_ACL_OWNER;
  824. }
  825. if (gid_valid(gid)) { /* chgrp */
  826. gid_t id;
  827. group_sid_ptr = (struct cifs_sid *)((char *)pnntsd +
  828. le32_to_cpu(pnntsd->gsidoffset));
  829. ngroup_sid_ptr = kmalloc(sizeof(struct cifs_sid),
  830. GFP_KERNEL);
  831. if (!ngroup_sid_ptr)
  832. return -ENOMEM;
  833. id = from_kgid(&init_user_ns, gid);
  834. rc = id_to_sid(id, SIDGROUP, ngroup_sid_ptr);
  835. if (rc) {
  836. cifs_dbg(FYI, "%s: Mapping error %d for group id %d\n",
  837. __func__, rc, id);
  838. kfree(ngroup_sid_ptr);
  839. return rc;
  840. }
  841. cifs_copy_sid(group_sid_ptr, ngroup_sid_ptr);
  842. kfree(ngroup_sid_ptr);
  843. *aclflag = CIFS_ACL_GROUP;
  844. }
  845. }
  846. return rc;
  847. }
  848. struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb,
  849. const struct cifs_fid *cifsfid, u32 *pacllen)
  850. {
  851. struct cifs_ntsd *pntsd = NULL;
  852. unsigned int xid;
  853. int rc;
  854. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  855. if (IS_ERR(tlink))
  856. return ERR_CAST(tlink);
  857. xid = get_xid();
  858. rc = CIFSSMBGetCIFSACL(xid, tlink_tcon(tlink), cifsfid->netfid, &pntsd,
  859. pacllen);
  860. free_xid(xid);
  861. cifs_put_tlink(tlink);
  862. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  863. if (rc)
  864. return ERR_PTR(rc);
  865. return pntsd;
  866. }
  867. static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb,
  868. const char *path, u32 *pacllen)
  869. {
  870. struct cifs_ntsd *pntsd = NULL;
  871. int oplock = 0;
  872. unsigned int xid;
  873. int rc, create_options = 0;
  874. struct cifs_tcon *tcon;
  875. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  876. struct cifs_fid fid;
  877. struct cifs_open_parms oparms;
  878. if (IS_ERR(tlink))
  879. return ERR_CAST(tlink);
  880. tcon = tlink_tcon(tlink);
  881. xid = get_xid();
  882. if (backup_cred(cifs_sb))
  883. create_options |= CREATE_OPEN_BACKUP_INTENT;
  884. oparms.tcon = tcon;
  885. oparms.cifs_sb = cifs_sb;
  886. oparms.desired_access = READ_CONTROL;
  887. oparms.create_options = create_options;
  888. oparms.disposition = FILE_OPEN;
  889. oparms.path = path;
  890. oparms.fid = &fid;
  891. oparms.reconnect = false;
  892. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  893. if (!rc) {
  894. rc = CIFSSMBGetCIFSACL(xid, tcon, fid.netfid, &pntsd, pacllen);
  895. CIFSSMBClose(xid, tcon, fid.netfid);
  896. }
  897. cifs_put_tlink(tlink);
  898. free_xid(xid);
  899. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  900. if (rc)
  901. return ERR_PTR(rc);
  902. return pntsd;
  903. }
  904. /* Retrieve an ACL from the server */
  905. struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb,
  906. struct inode *inode, const char *path,
  907. u32 *pacllen)
  908. {
  909. struct cifs_ntsd *pntsd = NULL;
  910. struct cifsFileInfo *open_file = NULL;
  911. if (inode)
  912. open_file = find_readable_file(CIFS_I(inode), true);
  913. if (!open_file)
  914. return get_cifs_acl_by_path(cifs_sb, path, pacllen);
  915. pntsd = get_cifs_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
  916. cifsFileInfo_put(open_file);
  917. return pntsd;
  918. }
  919. /* Set an ACL on the server */
  920. int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
  921. struct inode *inode, const char *path, int aclflag)
  922. {
  923. int oplock = 0;
  924. unsigned int xid;
  925. int rc, access_flags, create_options = 0;
  926. struct cifs_tcon *tcon;
  927. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  928. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  929. struct cifs_fid fid;
  930. struct cifs_open_parms oparms;
  931. if (IS_ERR(tlink))
  932. return PTR_ERR(tlink);
  933. tcon = tlink_tcon(tlink);
  934. xid = get_xid();
  935. if (backup_cred(cifs_sb))
  936. create_options |= CREATE_OPEN_BACKUP_INTENT;
  937. if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
  938. access_flags = WRITE_OWNER;
  939. else
  940. access_flags = WRITE_DAC;
  941. oparms.tcon = tcon;
  942. oparms.cifs_sb = cifs_sb;
  943. oparms.desired_access = access_flags;
  944. oparms.create_options = create_options;
  945. oparms.disposition = FILE_OPEN;
  946. oparms.path = path;
  947. oparms.fid = &fid;
  948. oparms.reconnect = false;
  949. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  950. if (rc) {
  951. cifs_dbg(VFS, "Unable to open file to set ACL\n");
  952. goto out;
  953. }
  954. rc = CIFSSMBSetCIFSACL(xid, tcon, fid.netfid, pnntsd, acllen, aclflag);
  955. cifs_dbg(NOISY, "SetCIFSACL rc = %d\n", rc);
  956. CIFSSMBClose(xid, tcon, fid.netfid);
  957. out:
  958. free_xid(xid);
  959. cifs_put_tlink(tlink);
  960. return rc;
  961. }
  962. /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */
  963. int
  964. cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
  965. struct inode *inode, const char *path,
  966. const struct cifs_fid *pfid)
  967. {
  968. struct cifs_ntsd *pntsd = NULL;
  969. u32 acllen = 0;
  970. int rc = 0;
  971. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  972. struct cifs_tcon *tcon;
  973. cifs_dbg(NOISY, "converting ACL to mode for %s\n", path);
  974. if (IS_ERR(tlink))
  975. return PTR_ERR(tlink);
  976. tcon = tlink_tcon(tlink);
  977. if (pfid && (tcon->ses->server->ops->get_acl_by_fid))
  978. pntsd = tcon->ses->server->ops->get_acl_by_fid(cifs_sb, pfid,
  979. &acllen);
  980. else if (tcon->ses->server->ops->get_acl)
  981. pntsd = tcon->ses->server->ops->get_acl(cifs_sb, inode, path,
  982. &acllen);
  983. else {
  984. cifs_put_tlink(tlink);
  985. return -EOPNOTSUPP;
  986. }
  987. /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */
  988. if (IS_ERR(pntsd)) {
  989. rc = PTR_ERR(pntsd);
  990. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  991. } else {
  992. rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr);
  993. kfree(pntsd);
  994. if (rc)
  995. cifs_dbg(VFS, "parse sec desc failed rc = %d\n", rc);
  996. }
  997. cifs_put_tlink(tlink);
  998. return rc;
  999. }
  1000. /* Convert mode bits to an ACL so we can update the ACL on the server */
  1001. int
  1002. id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 nmode,
  1003. kuid_t uid, kgid_t gid)
  1004. {
  1005. int rc = 0;
  1006. int aclflag = CIFS_ACL_DACL; /* default flag to set */
  1007. __u32 secdesclen = 0;
  1008. struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */
  1009. struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */
  1010. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1011. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1012. struct cifs_tcon *tcon;
  1013. if (IS_ERR(tlink))
  1014. return PTR_ERR(tlink);
  1015. tcon = tlink_tcon(tlink);
  1016. cifs_dbg(NOISY, "set ACL from mode for %s\n", path);
  1017. /* Get the security descriptor */
  1018. if (tcon->ses->server->ops->get_acl == NULL) {
  1019. cifs_put_tlink(tlink);
  1020. return -EOPNOTSUPP;
  1021. }
  1022. pntsd = tcon->ses->server->ops->get_acl(cifs_sb, inode, path,
  1023. &secdesclen);
  1024. if (IS_ERR(pntsd)) {
  1025. rc = PTR_ERR(pntsd);
  1026. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  1027. cifs_put_tlink(tlink);
  1028. return rc;
  1029. }
  1030. /*
  1031. * Add three ACEs for owner, group, everyone getting rid of other ACEs
  1032. * as chmod disables ACEs and set the security descriptor. Allocate
  1033. * memory for the smb header, set security descriptor request security
  1034. * descriptor parameters, and secuirty descriptor itself
  1035. */
  1036. secdesclen = max_t(u32, secdesclen, DEFAULT_SEC_DESC_LEN);
  1037. pnntsd = kmalloc(secdesclen, GFP_KERNEL);
  1038. if (!pnntsd) {
  1039. kfree(pntsd);
  1040. cifs_put_tlink(tlink);
  1041. return -ENOMEM;
  1042. }
  1043. rc = build_sec_desc(pntsd, pnntsd, secdesclen, nmode, uid, gid,
  1044. &aclflag);
  1045. cifs_dbg(NOISY, "build_sec_desc rc: %d\n", rc);
  1046. if (tcon->ses->server->ops->set_acl == NULL)
  1047. rc = -EOPNOTSUPP;
  1048. if (!rc) {
  1049. /* Set the security descriptor */
  1050. rc = tcon->ses->server->ops->set_acl(pnntsd, secdesclen, inode,
  1051. path, aclflag);
  1052. cifs_dbg(NOISY, "set_cifs_acl rc: %d\n", rc);
  1053. }
  1054. cifs_put_tlink(tlink);
  1055. kfree(pnntsd);
  1056. kfree(pntsd);
  1057. return rc;
  1058. }