cifsacl.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. /* S-1-22-1 Unmapped Unix users */
  41. static const struct cifs_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22},
  42. {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  43. /* S-1-22-2 Unmapped Unix groups */
  44. static const struct cifs_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22},
  45. {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  46. /*
  47. * See https://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  48. */
  49. /* S-1-5-88 MS NFS and Apple style UID/GID/mode */
  50. /* S-1-5-88-1 Unix uid */
  51. static const struct cifs_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5},
  52. {cpu_to_le32(88),
  53. cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  54. /* S-1-5-88-2 Unix gid */
  55. static const struct cifs_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5},
  56. {cpu_to_le32(88),
  57. cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  58. /* S-1-5-88-3 Unix mode */
  59. static const struct cifs_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5},
  60. {cpu_to_le32(88),
  61. cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  62. static const struct cred *root_cred;
  63. static int
  64. cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
  65. {
  66. char *payload;
  67. /*
  68. * If the payload is less than or equal to the size of a pointer, then
  69. * an allocation here is wasteful. Just copy the data directly to the
  70. * payload.value union member instead.
  71. *
  72. * With this however, you must check the datalen before trying to
  73. * dereference payload.data!
  74. */
  75. if (prep->datalen <= sizeof(key->payload)) {
  76. key->payload.data[0] = NULL;
  77. memcpy(&key->payload, prep->data, prep->datalen);
  78. } else {
  79. payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
  80. if (!payload)
  81. return -ENOMEM;
  82. key->payload.data[0] = payload;
  83. }
  84. key->datalen = prep->datalen;
  85. return 0;
  86. }
  87. static inline void
  88. cifs_idmap_key_destroy(struct key *key)
  89. {
  90. if (key->datalen > sizeof(key->payload))
  91. kfree(key->payload.data[0]);
  92. }
  93. static struct key_type cifs_idmap_key_type = {
  94. .name = "cifs.idmap",
  95. .instantiate = cifs_idmap_key_instantiate,
  96. .destroy = cifs_idmap_key_destroy,
  97. .describe = user_describe,
  98. };
  99. static char *
  100. sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
  101. {
  102. int i, len;
  103. unsigned int saval;
  104. char *sidstr, *strptr;
  105. unsigned long long id_auth_val;
  106. /* 3 bytes for prefix */
  107. sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
  108. (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
  109. GFP_KERNEL);
  110. if (!sidstr)
  111. return sidstr;
  112. strptr = sidstr;
  113. len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
  114. sidptr->revision);
  115. strptr += len;
  116. /* The authority field is a single 48-bit number */
  117. id_auth_val = (unsigned long long)sidptr->authority[5];
  118. id_auth_val |= (unsigned long long)sidptr->authority[4] << 8;
  119. id_auth_val |= (unsigned long long)sidptr->authority[3] << 16;
  120. id_auth_val |= (unsigned long long)sidptr->authority[2] << 24;
  121. id_auth_val |= (unsigned long long)sidptr->authority[1] << 32;
  122. id_auth_val |= (unsigned long long)sidptr->authority[0] << 48;
  123. /*
  124. * MS-DTYP states that if the authority is >= 2^32, then it should be
  125. * expressed as a hex value.
  126. */
  127. if (id_auth_val <= UINT_MAX)
  128. len = sprintf(strptr, "-%llu", id_auth_val);
  129. else
  130. len = sprintf(strptr, "-0x%llx", id_auth_val);
  131. strptr += len;
  132. for (i = 0; i < sidptr->num_subauth; ++i) {
  133. saval = le32_to_cpu(sidptr->sub_auth[i]);
  134. len = sprintf(strptr, "-%u", saval);
  135. strptr += len;
  136. }
  137. return sidstr;
  138. }
  139. /*
  140. * if the two SIDs (roughly equivalent to a UUID for a user or group) are
  141. * the same returns zero, if they do not match returns non-zero.
  142. */
  143. static int
  144. compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)
  145. {
  146. int i;
  147. int num_subauth, num_sat, num_saw;
  148. if ((!ctsid) || (!cwsid))
  149. return 1;
  150. /* compare the revision */
  151. if (ctsid->revision != cwsid->revision) {
  152. if (ctsid->revision > cwsid->revision)
  153. return 1;
  154. else
  155. return -1;
  156. }
  157. /* compare all of the six auth values */
  158. for (i = 0; i < NUM_AUTHS; ++i) {
  159. if (ctsid->authority[i] != cwsid->authority[i]) {
  160. if (ctsid->authority[i] > cwsid->authority[i])
  161. return 1;
  162. else
  163. return -1;
  164. }
  165. }
  166. /* compare all of the subauth values if any */
  167. num_sat = ctsid->num_subauth;
  168. num_saw = cwsid->num_subauth;
  169. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  170. if (num_subauth) {
  171. for (i = 0; i < num_subauth; ++i) {
  172. if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) {
  173. if (le32_to_cpu(ctsid->sub_auth[i]) >
  174. le32_to_cpu(cwsid->sub_auth[i]))
  175. return 1;
  176. else
  177. return -1;
  178. }
  179. }
  180. }
  181. return 0; /* sids compare/match */
  182. }
  183. static bool
  184. is_well_known_sid(const struct cifs_sid *psid, uint32_t *puid, bool is_group)
  185. {
  186. int i;
  187. int num_subauth;
  188. const struct cifs_sid *pwell_known_sid;
  189. if (!psid || (puid == NULL))
  190. return false;
  191. num_subauth = psid->num_subauth;
  192. /* check if Mac (or Windows NFS) vs. Samba format for Unix owner SID */
  193. if (num_subauth == 2) {
  194. if (is_group)
  195. pwell_known_sid = &sid_unix_groups;
  196. else
  197. pwell_known_sid = &sid_unix_users;
  198. } else if (num_subauth == 3) {
  199. if (is_group)
  200. pwell_known_sid = &sid_unix_NFS_groups;
  201. else
  202. pwell_known_sid = &sid_unix_NFS_users;
  203. } else
  204. return false;
  205. /* compare the revision */
  206. if (psid->revision != pwell_known_sid->revision)
  207. return false;
  208. /* compare all of the six auth values */
  209. for (i = 0; i < NUM_AUTHS; ++i) {
  210. if (psid->authority[i] != pwell_known_sid->authority[i]) {
  211. cifs_dbg(FYI, "auth %d did not match\n", i);
  212. return false;
  213. }
  214. }
  215. if (num_subauth == 2) {
  216. if (psid->sub_auth[0] != pwell_known_sid->sub_auth[0])
  217. return false;
  218. *puid = le32_to_cpu(psid->sub_auth[1]);
  219. } else /* 3 subauths, ie Windows/Mac style */ {
  220. *puid = le32_to_cpu(psid->sub_auth[0]);
  221. if ((psid->sub_auth[0] != pwell_known_sid->sub_auth[0]) ||
  222. (psid->sub_auth[1] != pwell_known_sid->sub_auth[1]))
  223. return false;
  224. *puid = le32_to_cpu(psid->sub_auth[2]);
  225. }
  226. cifs_dbg(FYI, "Unix UID %d returned from SID\n", *puid);
  227. return true; /* well known sid found, uid returned */
  228. }
  229. static void
  230. cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
  231. {
  232. int i;
  233. dst->revision = src->revision;
  234. dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
  235. for (i = 0; i < NUM_AUTHS; ++i)
  236. dst->authority[i] = src->authority[i];
  237. for (i = 0; i < dst->num_subauth; ++i)
  238. dst->sub_auth[i] = src->sub_auth[i];
  239. }
  240. static int
  241. id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
  242. {
  243. int rc;
  244. struct key *sidkey;
  245. struct cifs_sid *ksid;
  246. unsigned int ksid_size;
  247. char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
  248. const struct cred *saved_cred;
  249. rc = snprintf(desc, sizeof(desc), "%ci:%u",
  250. sidtype == SIDOWNER ? 'o' : 'g', cid);
  251. if (rc >= sizeof(desc))
  252. return -EINVAL;
  253. rc = 0;
  254. saved_cred = override_creds(root_cred);
  255. sidkey = request_key(&cifs_idmap_key_type, desc, "");
  256. if (IS_ERR(sidkey)) {
  257. rc = -EINVAL;
  258. cifs_dbg(FYI, "%s: Can't map %cid %u to a SID\n",
  259. __func__, sidtype == SIDOWNER ? 'u' : 'g', cid);
  260. goto out_revert_creds;
  261. } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
  262. rc = -EIO;
  263. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  264. __func__, sidkey->datalen);
  265. goto invalidate_key;
  266. }
  267. /*
  268. * A sid is usually too large to be embedded in payload.value, but if
  269. * there are no subauthorities and the host has 8-byte pointers, then
  270. * it could be.
  271. */
  272. ksid = sidkey->datalen <= sizeof(sidkey->payload) ?
  273. (struct cifs_sid *)&sidkey->payload :
  274. (struct cifs_sid *)sidkey->payload.data[0];
  275. ksid_size = CIFS_SID_BASE_SIZE + (ksid->num_subauth * sizeof(__le32));
  276. if (ksid_size > sidkey->datalen) {
  277. rc = -EIO;
  278. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu, ksid_size=%u)\n",
  279. __func__, sidkey->datalen, ksid_size);
  280. goto invalidate_key;
  281. }
  282. cifs_copy_sid(ssid, ksid);
  283. out_key_put:
  284. key_put(sidkey);
  285. out_revert_creds:
  286. revert_creds(saved_cred);
  287. return rc;
  288. invalidate_key:
  289. key_invalidate(sidkey);
  290. goto out_key_put;
  291. }
  292. int
  293. sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
  294. struct cifs_fattr *fattr, uint sidtype)
  295. {
  296. int rc = 0;
  297. struct key *sidkey;
  298. char *sidstr;
  299. const struct cred *saved_cred;
  300. kuid_t fuid = cifs_sb->mnt_uid;
  301. kgid_t fgid = cifs_sb->mnt_gid;
  302. /*
  303. * If we have too many subauthorities, then something is really wrong.
  304. * Just return an error.
  305. */
  306. if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
  307. cifs_dbg(FYI, "%s: %u subauthorities is too many!\n",
  308. __func__, psid->num_subauth);
  309. return -EIO;
  310. }
  311. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL) ||
  312. (cifs_sb_master_tcon(cifs_sb)->posix_extensions)) {
  313. uint32_t unix_id;
  314. bool is_group;
  315. if (sidtype != SIDOWNER)
  316. is_group = true;
  317. else
  318. is_group = false;
  319. if (is_well_known_sid(psid, &unix_id, is_group) == false)
  320. goto try_upcall_to_get_id;
  321. if (is_group) {
  322. kgid_t gid;
  323. gid_t id;
  324. id = (gid_t)unix_id;
  325. gid = make_kgid(&init_user_ns, id);
  326. if (gid_valid(gid)) {
  327. fgid = gid;
  328. goto got_valid_id;
  329. }
  330. } else {
  331. kuid_t uid;
  332. uid_t id;
  333. id = (uid_t)unix_id;
  334. uid = make_kuid(&init_user_ns, id);
  335. if (uid_valid(uid)) {
  336. fuid = uid;
  337. goto got_valid_id;
  338. }
  339. }
  340. /* If unable to find uid/gid easily from SID try via upcall */
  341. }
  342. try_upcall_to_get_id:
  343. sidstr = sid_to_key_str(psid, sidtype);
  344. if (!sidstr)
  345. return -ENOMEM;
  346. saved_cred = override_creds(root_cred);
  347. sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
  348. if (IS_ERR(sidkey)) {
  349. rc = -EINVAL;
  350. cifs_dbg(FYI, "%s: Can't map SID %s to a %cid\n",
  351. __func__, sidstr, sidtype == SIDOWNER ? 'u' : 'g');
  352. goto out_revert_creds;
  353. }
  354. /*
  355. * FIXME: Here we assume that uid_t and gid_t are same size. It's
  356. * probably a safe assumption but might be better to check based on
  357. * sidtype.
  358. */
  359. BUILD_BUG_ON(sizeof(uid_t) != sizeof(gid_t));
  360. if (sidkey->datalen != sizeof(uid_t)) {
  361. rc = -EIO;
  362. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  363. __func__, sidkey->datalen);
  364. key_invalidate(sidkey);
  365. goto out_key_put;
  366. }
  367. if (sidtype == SIDOWNER) {
  368. kuid_t uid;
  369. uid_t id;
  370. memcpy(&id, &sidkey->payload.data[0], sizeof(uid_t));
  371. uid = make_kuid(&init_user_ns, id);
  372. if (uid_valid(uid))
  373. fuid = uid;
  374. } else {
  375. kgid_t gid;
  376. gid_t id;
  377. memcpy(&id, &sidkey->payload.data[0], sizeof(gid_t));
  378. gid = make_kgid(&init_user_ns, id);
  379. if (gid_valid(gid))
  380. fgid = gid;
  381. }
  382. out_key_put:
  383. key_put(sidkey);
  384. out_revert_creds:
  385. revert_creds(saved_cred);
  386. kfree(sidstr);
  387. /*
  388. * Note that we return 0 here unconditionally. If the mapping
  389. * fails then we just fall back to using the mnt_uid/mnt_gid.
  390. */
  391. got_valid_id:
  392. rc = 0;
  393. if (sidtype == SIDOWNER)
  394. fattr->cf_uid = fuid;
  395. else
  396. fattr->cf_gid = fgid;
  397. return rc;
  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 %04o\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: %04o, 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, bool mode_from_special_sid)
  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_array(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 (mode_from_special_sid &&
  650. (compare_sids(&(ppace[i]->sid),
  651. &sid_unix_NFS_mode) == 0)) {
  652. /*
  653. * Full permissions are:
  654. * 07777 = S_ISUID | S_ISGID | S_ISVTX |
  655. * S_IRWXU | S_IRWXG | S_IRWXO
  656. */
  657. fattr->cf_mode &= ~07777;
  658. fattr->cf_mode |=
  659. le32_to_cpu(ppace[i]->sid.sub_auth[2]);
  660. break;
  661. } else if (compare_sids(&(ppace[i]->sid), pownersid) == 0)
  662. access_flags_to_mode(ppace[i]->access_req,
  663. ppace[i]->type,
  664. &fattr->cf_mode,
  665. &user_mask);
  666. else if (compare_sids(&(ppace[i]->sid), pgrpsid) == 0)
  667. access_flags_to_mode(ppace[i]->access_req,
  668. ppace[i]->type,
  669. &fattr->cf_mode,
  670. &group_mask);
  671. else if (compare_sids(&(ppace[i]->sid), &sid_everyone) == 0)
  672. access_flags_to_mode(ppace[i]->access_req,
  673. ppace[i]->type,
  674. &fattr->cf_mode,
  675. &other_mask);
  676. else if (compare_sids(&(ppace[i]->sid), &sid_authusers) == 0)
  677. access_flags_to_mode(ppace[i]->access_req,
  678. ppace[i]->type,
  679. &fattr->cf_mode,
  680. &other_mask);
  681. /* memcpy((void *)(&(cifscred->aces[i])),
  682. (void *)ppace[i],
  683. sizeof(struct cifs_ace)); */
  684. acl_base = (char *)ppace[i];
  685. acl_size = le16_to_cpu(ppace[i]->size);
  686. }
  687. kfree(ppace);
  688. }
  689. return;
  690. }
  691. unsigned int setup_authusers_ACE(struct cifs_ace *pntace)
  692. {
  693. int i;
  694. unsigned int ace_size = 20;
  695. pntace->type = ACCESS_ALLOWED_ACE_TYPE;
  696. pntace->flags = 0x0;
  697. pntace->access_req = cpu_to_le32(GENERIC_ALL);
  698. pntace->sid.num_subauth = 1;
  699. pntace->sid.revision = 1;
  700. for (i = 0; i < NUM_AUTHS; i++)
  701. pntace->sid.authority[i] = sid_authusers.authority[i];
  702. pntace->sid.sub_auth[0] = sid_authusers.sub_auth[0];
  703. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  704. pntace->size = cpu_to_le16(ace_size);
  705. return ace_size;
  706. }
  707. /*
  708. * Fill in the special SID based on the mode. See
  709. * https://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  710. */
  711. unsigned int setup_special_mode_ACE(struct cifs_ace *pntace, __u64 nmode)
  712. {
  713. int i;
  714. unsigned int ace_size = 28;
  715. pntace->type = ACCESS_DENIED_ACE_TYPE;
  716. pntace->flags = 0x0;
  717. pntace->access_req = 0;
  718. pntace->sid.num_subauth = 3;
  719. pntace->sid.revision = 1;
  720. for (i = 0; i < NUM_AUTHS; i++)
  721. pntace->sid.authority[i] = sid_unix_NFS_mode.authority[i];
  722. pntace->sid.sub_auth[0] = sid_unix_NFS_mode.sub_auth[0];
  723. pntace->sid.sub_auth[1] = sid_unix_NFS_mode.sub_auth[1];
  724. pntace->sid.sub_auth[2] = cpu_to_le32(nmode & 07777);
  725. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  726. pntace->size = cpu_to_le16(ace_size);
  727. return ace_size;
  728. }
  729. unsigned int setup_special_user_owner_ACE(struct cifs_ace *pntace)
  730. {
  731. int i;
  732. unsigned int ace_size = 28;
  733. pntace->type = ACCESS_ALLOWED_ACE_TYPE;
  734. pntace->flags = 0x0;
  735. pntace->access_req = cpu_to_le32(GENERIC_ALL);
  736. pntace->sid.num_subauth = 3;
  737. pntace->sid.revision = 1;
  738. for (i = 0; i < NUM_AUTHS; i++)
  739. pntace->sid.authority[i] = sid_unix_NFS_users.authority[i];
  740. pntace->sid.sub_auth[0] = sid_unix_NFS_users.sub_auth[0];
  741. pntace->sid.sub_auth[1] = sid_unix_NFS_users.sub_auth[1];
  742. pntace->sid.sub_auth[2] = cpu_to_le32(current_fsgid().val);
  743. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  744. pntace->size = cpu_to_le16(ace_size);
  745. return ace_size;
  746. }
  747. static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid,
  748. struct cifs_sid *pgrpsid, __u64 nmode, bool modefromsid)
  749. {
  750. u16 size = 0;
  751. u32 num_aces = 0;
  752. struct cifs_acl *pnndacl;
  753. pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl));
  754. if (modefromsid) {
  755. struct cifs_ace *pntace =
  756. (struct cifs_ace *)((char *)pnndacl + size);
  757. size += setup_special_mode_ACE(pntace, nmode);
  758. num_aces++;
  759. }
  760. size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size),
  761. pownersid, nmode, S_IRWXU);
  762. num_aces++;
  763. size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size),
  764. pgrpsid, nmode, S_IRWXG);
  765. num_aces++;
  766. size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size),
  767. &sid_everyone, nmode, S_IRWXO);
  768. num_aces++;
  769. pndacl->num_aces = cpu_to_le32(num_aces);
  770. pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl));
  771. return 0;
  772. }
  773. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  774. {
  775. /* BB need to add parm so we can store the SID BB */
  776. /* validate that we do not go past end of ACL - sid must be at least 8
  777. bytes long (assuming no sub-auths - e.g. the null SID */
  778. if (end_of_acl < (char *)psid + 8) {
  779. cifs_dbg(VFS, "ACL too small to parse SID %p\n", psid);
  780. return -EINVAL;
  781. }
  782. #ifdef CONFIG_CIFS_DEBUG2
  783. if (psid->num_subauth) {
  784. int i;
  785. cifs_dbg(FYI, "SID revision %d num_auth %d\n",
  786. psid->revision, psid->num_subauth);
  787. for (i = 0; i < psid->num_subauth; i++) {
  788. cifs_dbg(FYI, "SID sub_auth[%d]: 0x%x\n",
  789. i, le32_to_cpu(psid->sub_auth[i]));
  790. }
  791. /* BB add length check to make sure that we do not have huge
  792. num auths and therefore go off the end */
  793. cifs_dbg(FYI, "RID 0x%x\n",
  794. le32_to_cpu(psid->sub_auth[psid->num_subauth-1]));
  795. }
  796. #endif
  797. return 0;
  798. }
  799. /* Convert CIFS ACL to POSIX form */
  800. static int parse_sec_desc(struct cifs_sb_info *cifs_sb,
  801. struct cifs_ntsd *pntsd, int acl_len, struct cifs_fattr *fattr,
  802. bool get_mode_from_special_sid)
  803. {
  804. int rc = 0;
  805. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  806. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  807. char *end_of_acl = ((char *)pntsd) + acl_len;
  808. __u32 dacloffset;
  809. if (pntsd == NULL)
  810. return -EIO;
  811. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  812. le32_to_cpu(pntsd->osidoffset));
  813. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  814. le32_to_cpu(pntsd->gsidoffset));
  815. dacloffset = le32_to_cpu(pntsd->dacloffset);
  816. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  817. cifs_dbg(NOISY, "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
  818. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  819. le32_to_cpu(pntsd->gsidoffset),
  820. le32_to_cpu(pntsd->sacloffset), dacloffset);
  821. /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */
  822. rc = parse_sid(owner_sid_ptr, end_of_acl);
  823. if (rc) {
  824. cifs_dbg(FYI, "%s: Error %d parsing Owner SID\n", __func__, rc);
  825. return rc;
  826. }
  827. rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr, SIDOWNER);
  828. if (rc) {
  829. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to uid\n",
  830. __func__, rc);
  831. return rc;
  832. }
  833. rc = parse_sid(group_sid_ptr, end_of_acl);
  834. if (rc) {
  835. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to gid\n",
  836. __func__, rc);
  837. return rc;
  838. }
  839. rc = sid_to_id(cifs_sb, group_sid_ptr, fattr, SIDGROUP);
  840. if (rc) {
  841. cifs_dbg(FYI, "%s: Error %d mapping Group SID to gid\n",
  842. __func__, rc);
  843. return rc;
  844. }
  845. if (dacloffset)
  846. parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr,
  847. group_sid_ptr, fattr, get_mode_from_special_sid);
  848. else
  849. cifs_dbg(FYI, "no ACL\n"); /* BB grant all or default perms? */
  850. return rc;
  851. }
  852. /* Convert permission bits from mode to equivalent CIFS ACL */
  853. static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd,
  854. __u32 secdesclen, __u64 nmode, kuid_t uid, kgid_t gid,
  855. bool mode_from_sid, bool id_from_sid, int *aclflag)
  856. {
  857. int rc = 0;
  858. __u32 dacloffset;
  859. __u32 ndacloffset;
  860. __u32 sidsoffset;
  861. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  862. struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
  863. struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */
  864. struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */
  865. if (nmode != NO_CHANGE_64) { /* chmod */
  866. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  867. le32_to_cpu(pntsd->osidoffset));
  868. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  869. le32_to_cpu(pntsd->gsidoffset));
  870. dacloffset = le32_to_cpu(pntsd->dacloffset);
  871. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  872. ndacloffset = sizeof(struct cifs_ntsd);
  873. ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset);
  874. ndacl_ptr->revision = dacl_ptr->revision;
  875. ndacl_ptr->size = 0;
  876. ndacl_ptr->num_aces = 0;
  877. rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr,
  878. nmode, mode_from_sid);
  879. sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size);
  880. /* copy sec desc control portion & owner and group sids */
  881. copy_sec_desc(pntsd, pnntsd, sidsoffset);
  882. *aclflag = CIFS_ACL_DACL;
  883. } else {
  884. memcpy(pnntsd, pntsd, secdesclen);
  885. if (uid_valid(uid)) { /* chown */
  886. uid_t id;
  887. owner_sid_ptr = (struct cifs_sid *)((char *)pnntsd +
  888. le32_to_cpu(pnntsd->osidoffset));
  889. nowner_sid_ptr = kmalloc(sizeof(struct cifs_sid),
  890. GFP_KERNEL);
  891. if (!nowner_sid_ptr)
  892. return -ENOMEM;
  893. id = from_kuid(&init_user_ns, uid);
  894. if (id_from_sid) {
  895. struct owner_sid *osid = (struct owner_sid *)nowner_sid_ptr;
  896. /* Populate the user ownership fields S-1-5-88-1 */
  897. osid->Revision = 1;
  898. osid->NumAuth = 3;
  899. osid->Authority[5] = 5;
  900. osid->SubAuthorities[0] = cpu_to_le32(88);
  901. osid->SubAuthorities[1] = cpu_to_le32(1);
  902. osid->SubAuthorities[2] = cpu_to_le32(id);
  903. } else { /* lookup sid with upcall */
  904. rc = id_to_sid(id, SIDOWNER, nowner_sid_ptr);
  905. if (rc) {
  906. cifs_dbg(FYI, "%s: Mapping error %d for owner id %d\n",
  907. __func__, rc, id);
  908. kfree(nowner_sid_ptr);
  909. return rc;
  910. }
  911. }
  912. cifs_copy_sid(owner_sid_ptr, nowner_sid_ptr);
  913. kfree(nowner_sid_ptr);
  914. *aclflag = CIFS_ACL_OWNER;
  915. }
  916. if (gid_valid(gid)) { /* chgrp */
  917. gid_t id;
  918. group_sid_ptr = (struct cifs_sid *)((char *)pnntsd +
  919. le32_to_cpu(pnntsd->gsidoffset));
  920. ngroup_sid_ptr = kmalloc(sizeof(struct cifs_sid),
  921. GFP_KERNEL);
  922. if (!ngroup_sid_ptr)
  923. return -ENOMEM;
  924. id = from_kgid(&init_user_ns, gid);
  925. if (id_from_sid) {
  926. struct owner_sid *gsid = (struct owner_sid *)ngroup_sid_ptr;
  927. /* Populate the group ownership fields S-1-5-88-2 */
  928. gsid->Revision = 1;
  929. gsid->NumAuth = 3;
  930. gsid->Authority[5] = 5;
  931. gsid->SubAuthorities[0] = cpu_to_le32(88);
  932. gsid->SubAuthorities[1] = cpu_to_le32(2);
  933. gsid->SubAuthorities[2] = cpu_to_le32(id);
  934. } else { /* lookup sid with upcall */
  935. rc = id_to_sid(id, SIDGROUP, ngroup_sid_ptr);
  936. if (rc) {
  937. cifs_dbg(FYI, "%s: Mapping error %d for group id %d\n",
  938. __func__, rc, id);
  939. kfree(ngroup_sid_ptr);
  940. return rc;
  941. }
  942. }
  943. cifs_copy_sid(group_sid_ptr, ngroup_sid_ptr);
  944. kfree(ngroup_sid_ptr);
  945. *aclflag = CIFS_ACL_GROUP;
  946. }
  947. }
  948. return rc;
  949. }
  950. struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb,
  951. const struct cifs_fid *cifsfid, u32 *pacllen)
  952. {
  953. struct cifs_ntsd *pntsd = NULL;
  954. unsigned int xid;
  955. int rc;
  956. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  957. if (IS_ERR(tlink))
  958. return ERR_CAST(tlink);
  959. xid = get_xid();
  960. rc = CIFSSMBGetCIFSACL(xid, tlink_tcon(tlink), cifsfid->netfid, &pntsd,
  961. pacllen);
  962. free_xid(xid);
  963. cifs_put_tlink(tlink);
  964. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  965. if (rc)
  966. return ERR_PTR(rc);
  967. return pntsd;
  968. }
  969. static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb,
  970. const char *path, u32 *pacllen)
  971. {
  972. struct cifs_ntsd *pntsd = NULL;
  973. int oplock = 0;
  974. unsigned int xid;
  975. int rc;
  976. struct cifs_tcon *tcon;
  977. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  978. struct cifs_fid fid;
  979. struct cifs_open_parms oparms;
  980. if (IS_ERR(tlink))
  981. return ERR_CAST(tlink);
  982. tcon = tlink_tcon(tlink);
  983. xid = get_xid();
  984. oparms.tcon = tcon;
  985. oparms.cifs_sb = cifs_sb;
  986. oparms.desired_access = READ_CONTROL;
  987. oparms.create_options = cifs_create_options(cifs_sb, 0);
  988. oparms.disposition = FILE_OPEN;
  989. oparms.path = path;
  990. oparms.fid = &fid;
  991. oparms.reconnect = false;
  992. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  993. if (!rc) {
  994. rc = CIFSSMBGetCIFSACL(xid, tcon, fid.netfid, &pntsd, pacllen);
  995. CIFSSMBClose(xid, tcon, fid.netfid);
  996. }
  997. cifs_put_tlink(tlink);
  998. free_xid(xid);
  999. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  1000. if (rc)
  1001. return ERR_PTR(rc);
  1002. return pntsd;
  1003. }
  1004. /* Retrieve an ACL from the server */
  1005. struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb,
  1006. struct inode *inode, const char *path,
  1007. u32 *pacllen)
  1008. {
  1009. struct cifs_ntsd *pntsd = NULL;
  1010. struct cifsFileInfo *open_file = NULL;
  1011. if (inode)
  1012. open_file = find_readable_file(CIFS_I(inode), true);
  1013. if (!open_file)
  1014. return get_cifs_acl_by_path(cifs_sb, path, pacllen);
  1015. pntsd = get_cifs_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
  1016. cifsFileInfo_put(open_file);
  1017. return pntsd;
  1018. }
  1019. /* Set an ACL on the server */
  1020. int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
  1021. struct inode *inode, const char *path, int aclflag)
  1022. {
  1023. int oplock = 0;
  1024. unsigned int xid;
  1025. int rc, access_flags;
  1026. struct cifs_tcon *tcon;
  1027. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1028. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1029. struct cifs_fid fid;
  1030. struct cifs_open_parms oparms;
  1031. if (IS_ERR(tlink))
  1032. return PTR_ERR(tlink);
  1033. tcon = tlink_tcon(tlink);
  1034. xid = get_xid();
  1035. if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
  1036. access_flags = WRITE_OWNER;
  1037. else
  1038. access_flags = WRITE_DAC;
  1039. oparms.tcon = tcon;
  1040. oparms.cifs_sb = cifs_sb;
  1041. oparms.desired_access = access_flags;
  1042. oparms.create_options = cifs_create_options(cifs_sb, 0);
  1043. oparms.disposition = FILE_OPEN;
  1044. oparms.path = path;
  1045. oparms.fid = &fid;
  1046. oparms.reconnect = false;
  1047. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1048. if (rc) {
  1049. cifs_dbg(VFS, "Unable to open file to set ACL\n");
  1050. goto out;
  1051. }
  1052. rc = CIFSSMBSetCIFSACL(xid, tcon, fid.netfid, pnntsd, acllen, aclflag);
  1053. cifs_dbg(NOISY, "SetCIFSACL rc = %d\n", rc);
  1054. CIFSSMBClose(xid, tcon, fid.netfid);
  1055. out:
  1056. free_xid(xid);
  1057. cifs_put_tlink(tlink);
  1058. return rc;
  1059. }
  1060. /* Translate the CIFS ACL (similar to NTFS ACL) for a file into mode bits */
  1061. int
  1062. cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
  1063. struct inode *inode, bool mode_from_special_sid,
  1064. const char *path, const struct cifs_fid *pfid)
  1065. {
  1066. struct cifs_ntsd *pntsd = NULL;
  1067. u32 acllen = 0;
  1068. int rc = 0;
  1069. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1070. struct smb_version_operations *ops;
  1071. cifs_dbg(NOISY, "converting ACL to mode for %s\n", path);
  1072. if (IS_ERR(tlink))
  1073. return PTR_ERR(tlink);
  1074. ops = tlink_tcon(tlink)->ses->server->ops;
  1075. if (pfid && (ops->get_acl_by_fid))
  1076. pntsd = ops->get_acl_by_fid(cifs_sb, pfid, &acllen);
  1077. else if (ops->get_acl)
  1078. pntsd = ops->get_acl(cifs_sb, inode, path, &acllen);
  1079. else {
  1080. cifs_put_tlink(tlink);
  1081. return -EOPNOTSUPP;
  1082. }
  1083. /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */
  1084. if (IS_ERR(pntsd)) {
  1085. rc = PTR_ERR(pntsd);
  1086. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  1087. } else if (mode_from_special_sid) {
  1088. rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr, true);
  1089. kfree(pntsd);
  1090. } else {
  1091. /* get approximated mode from ACL */
  1092. rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr, false);
  1093. kfree(pntsd);
  1094. if (rc)
  1095. cifs_dbg(VFS, "parse sec desc failed rc = %d\n", rc);
  1096. }
  1097. cifs_put_tlink(tlink);
  1098. return rc;
  1099. }
  1100. /* Convert mode bits to an ACL so we can update the ACL on the server */
  1101. int
  1102. id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 nmode,
  1103. kuid_t uid, kgid_t gid)
  1104. {
  1105. int rc = 0;
  1106. int aclflag = CIFS_ACL_DACL; /* default flag to set */
  1107. __u32 secdesclen = 0;
  1108. struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */
  1109. struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */
  1110. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1111. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1112. struct smb_version_operations *ops;
  1113. bool mode_from_sid, id_from_sid;
  1114. if (IS_ERR(tlink))
  1115. return PTR_ERR(tlink);
  1116. ops = tlink_tcon(tlink)->ses->server->ops;
  1117. cifs_dbg(NOISY, "set ACL from mode for %s\n", path);
  1118. /* Get the security descriptor */
  1119. if (ops->get_acl == NULL) {
  1120. cifs_put_tlink(tlink);
  1121. return -EOPNOTSUPP;
  1122. }
  1123. pntsd = ops->get_acl(cifs_sb, inode, path, &secdesclen);
  1124. if (IS_ERR(pntsd)) {
  1125. rc = PTR_ERR(pntsd);
  1126. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  1127. cifs_put_tlink(tlink);
  1128. return rc;
  1129. }
  1130. /*
  1131. * Add three ACEs for owner, group, everyone getting rid of other ACEs
  1132. * as chmod disables ACEs and set the security descriptor. Allocate
  1133. * memory for the smb header, set security descriptor request security
  1134. * descriptor parameters, and secuirty descriptor itself
  1135. */
  1136. secdesclen = max_t(u32, secdesclen, DEFAULT_SEC_DESC_LEN);
  1137. pnntsd = kmalloc(secdesclen, GFP_KERNEL);
  1138. if (!pnntsd) {
  1139. kfree(pntsd);
  1140. cifs_put_tlink(tlink);
  1141. return -ENOMEM;
  1142. }
  1143. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
  1144. mode_from_sid = true;
  1145. else
  1146. mode_from_sid = false;
  1147. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
  1148. id_from_sid = true;
  1149. else
  1150. id_from_sid = false;
  1151. rc = build_sec_desc(pntsd, pnntsd, secdesclen, nmode, uid, gid,
  1152. mode_from_sid, id_from_sid, &aclflag);
  1153. cifs_dbg(NOISY, "build_sec_desc rc: %d\n", rc);
  1154. if (ops->set_acl == NULL)
  1155. rc = -EOPNOTSUPP;
  1156. if (!rc) {
  1157. /* Set the security descriptor */
  1158. rc = ops->set_acl(pnntsd, secdesclen, inode, path, aclflag);
  1159. cifs_dbg(NOISY, "set_cifs_acl rc: %d\n", rc);
  1160. }
  1161. cifs_put_tlink(tlink);
  1162. kfree(pnntsd);
  1163. kfree(pntsd);
  1164. return rc;
  1165. }