cifsacl.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2007,2008
  5. * Author(s): Steve French ([email protected])
  6. *
  7. * Contains the routines for mapping CIFS/NTFS ACLs
  8. *
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <linux/keyctl.h>
  14. #include <linux/key-type.h>
  15. #include <keys/user-type.h>
  16. #include "cifspdu.h"
  17. #include "cifsglob.h"
  18. #include "cifsacl.h"
  19. #include "cifsproto.h"
  20. #include "cifs_debug.h"
  21. #include "fs_context.h"
  22. /* security id for everyone/world system group */
  23. static const struct cifs_sid sid_everyone = {
  24. 1, 1, {0, 0, 0, 0, 0, 1}, {0} };
  25. /* security id for Authenticated Users system group */
  26. static const struct cifs_sid sid_authusers = {
  27. 1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11)} };
  28. /* S-1-22-1 Unmapped Unix users */
  29. static const struct cifs_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22},
  30. {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  31. /* S-1-22-2 Unmapped Unix groups */
  32. static const struct cifs_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22},
  33. {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  34. /*
  35. * See https://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  36. */
  37. /* S-1-5-88 MS NFS and Apple style UID/GID/mode */
  38. /* S-1-5-88-1 Unix uid */
  39. static const struct cifs_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5},
  40. {cpu_to_le32(88),
  41. cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  42. /* S-1-5-88-2 Unix gid */
  43. static const struct cifs_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5},
  44. {cpu_to_le32(88),
  45. cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  46. /* S-1-5-88-3 Unix mode */
  47. static const struct cifs_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5},
  48. {cpu_to_le32(88),
  49. cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  50. static const struct cred *root_cred;
  51. static int
  52. cifs_idmap_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
  53. {
  54. char *payload;
  55. /*
  56. * If the payload is less than or equal to the size of a pointer, then
  57. * an allocation here is wasteful. Just copy the data directly to the
  58. * payload.value union member instead.
  59. *
  60. * With this however, you must check the datalen before trying to
  61. * dereference payload.data!
  62. */
  63. if (prep->datalen <= sizeof(key->payload)) {
  64. key->payload.data[0] = NULL;
  65. memcpy(&key->payload, prep->data, prep->datalen);
  66. } else {
  67. payload = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
  68. if (!payload)
  69. return -ENOMEM;
  70. key->payload.data[0] = payload;
  71. }
  72. key->datalen = prep->datalen;
  73. return 0;
  74. }
  75. static inline void
  76. cifs_idmap_key_destroy(struct key *key)
  77. {
  78. if (key->datalen > sizeof(key->payload))
  79. kfree(key->payload.data[0]);
  80. }
  81. static struct key_type cifs_idmap_key_type = {
  82. .name = "cifs.idmap",
  83. .instantiate = cifs_idmap_key_instantiate,
  84. .destroy = cifs_idmap_key_destroy,
  85. .describe = user_describe,
  86. };
  87. static char *
  88. sid_to_key_str(struct cifs_sid *sidptr, unsigned int type)
  89. {
  90. int i, len;
  91. unsigned int saval;
  92. char *sidstr, *strptr;
  93. unsigned long long id_auth_val;
  94. /* 3 bytes for prefix */
  95. sidstr = kmalloc(3 + SID_STRING_BASE_SIZE +
  96. (SID_STRING_SUBAUTH_SIZE * sidptr->num_subauth),
  97. GFP_KERNEL);
  98. if (!sidstr)
  99. return sidstr;
  100. strptr = sidstr;
  101. len = sprintf(strptr, "%cs:S-%hhu", type == SIDOWNER ? 'o' : 'g',
  102. sidptr->revision);
  103. strptr += len;
  104. /* The authority field is a single 48-bit number */
  105. id_auth_val = (unsigned long long)sidptr->authority[5];
  106. id_auth_val |= (unsigned long long)sidptr->authority[4] << 8;
  107. id_auth_val |= (unsigned long long)sidptr->authority[3] << 16;
  108. id_auth_val |= (unsigned long long)sidptr->authority[2] << 24;
  109. id_auth_val |= (unsigned long long)sidptr->authority[1] << 32;
  110. id_auth_val |= (unsigned long long)sidptr->authority[0] << 48;
  111. /*
  112. * MS-DTYP states that if the authority is >= 2^32, then it should be
  113. * expressed as a hex value.
  114. */
  115. if (id_auth_val <= UINT_MAX)
  116. len = sprintf(strptr, "-%llu", id_auth_val);
  117. else
  118. len = sprintf(strptr, "-0x%llx", id_auth_val);
  119. strptr += len;
  120. for (i = 0; i < sidptr->num_subauth; ++i) {
  121. saval = le32_to_cpu(sidptr->sub_auth[i]);
  122. len = sprintf(strptr, "-%u", saval);
  123. strptr += len;
  124. }
  125. return sidstr;
  126. }
  127. /*
  128. * if the two SIDs (roughly equivalent to a UUID for a user or group) are
  129. * the same returns zero, if they do not match returns non-zero.
  130. */
  131. static int
  132. compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)
  133. {
  134. int i;
  135. int num_subauth, num_sat, num_saw;
  136. if ((!ctsid) || (!cwsid))
  137. return 1;
  138. /* compare the revision */
  139. if (ctsid->revision != cwsid->revision) {
  140. if (ctsid->revision > cwsid->revision)
  141. return 1;
  142. else
  143. return -1;
  144. }
  145. /* compare all of the six auth values */
  146. for (i = 0; i < NUM_AUTHS; ++i) {
  147. if (ctsid->authority[i] != cwsid->authority[i]) {
  148. if (ctsid->authority[i] > cwsid->authority[i])
  149. return 1;
  150. else
  151. return -1;
  152. }
  153. }
  154. /* compare all of the subauth values if any */
  155. num_sat = ctsid->num_subauth;
  156. num_saw = cwsid->num_subauth;
  157. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  158. if (num_subauth) {
  159. for (i = 0; i < num_subauth; ++i) {
  160. if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) {
  161. if (le32_to_cpu(ctsid->sub_auth[i]) >
  162. le32_to_cpu(cwsid->sub_auth[i]))
  163. return 1;
  164. else
  165. return -1;
  166. }
  167. }
  168. }
  169. return 0; /* sids compare/match */
  170. }
  171. static bool
  172. is_well_known_sid(const struct cifs_sid *psid, uint32_t *puid, bool is_group)
  173. {
  174. int i;
  175. int num_subauth;
  176. const struct cifs_sid *pwell_known_sid;
  177. if (!psid || (puid == NULL))
  178. return false;
  179. num_subauth = psid->num_subauth;
  180. /* check if Mac (or Windows NFS) vs. Samba format for Unix owner SID */
  181. if (num_subauth == 2) {
  182. if (is_group)
  183. pwell_known_sid = &sid_unix_groups;
  184. else
  185. pwell_known_sid = &sid_unix_users;
  186. } else if (num_subauth == 3) {
  187. if (is_group)
  188. pwell_known_sid = &sid_unix_NFS_groups;
  189. else
  190. pwell_known_sid = &sid_unix_NFS_users;
  191. } else
  192. return false;
  193. /* compare the revision */
  194. if (psid->revision != pwell_known_sid->revision)
  195. return false;
  196. /* compare all of the six auth values */
  197. for (i = 0; i < NUM_AUTHS; ++i) {
  198. if (psid->authority[i] != pwell_known_sid->authority[i]) {
  199. cifs_dbg(FYI, "auth %d did not match\n", i);
  200. return false;
  201. }
  202. }
  203. if (num_subauth == 2) {
  204. if (psid->sub_auth[0] != pwell_known_sid->sub_auth[0])
  205. return false;
  206. *puid = le32_to_cpu(psid->sub_auth[1]);
  207. } else /* 3 subauths, ie Windows/Mac style */ {
  208. *puid = le32_to_cpu(psid->sub_auth[0]);
  209. if ((psid->sub_auth[0] != pwell_known_sid->sub_auth[0]) ||
  210. (psid->sub_auth[1] != pwell_known_sid->sub_auth[1]))
  211. return false;
  212. *puid = le32_to_cpu(psid->sub_auth[2]);
  213. }
  214. cifs_dbg(FYI, "Unix UID %d returned from SID\n", *puid);
  215. return true; /* well known sid found, uid returned */
  216. }
  217. static __u16
  218. cifs_copy_sid(struct cifs_sid *dst, const struct cifs_sid *src)
  219. {
  220. int i;
  221. __u16 size = 1 + 1 + 6;
  222. dst->revision = src->revision;
  223. dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
  224. for (i = 0; i < NUM_AUTHS; ++i)
  225. dst->authority[i] = src->authority[i];
  226. for (i = 0; i < dst->num_subauth; ++i)
  227. dst->sub_auth[i] = src->sub_auth[i];
  228. size += (dst->num_subauth * 4);
  229. return size;
  230. }
  231. static int
  232. id_to_sid(unsigned int cid, uint sidtype, struct cifs_sid *ssid)
  233. {
  234. int rc;
  235. struct key *sidkey;
  236. struct cifs_sid *ksid;
  237. unsigned int ksid_size;
  238. char desc[3 + 10 + 1]; /* 3 byte prefix + 10 bytes for value + NULL */
  239. const struct cred *saved_cred;
  240. rc = snprintf(desc, sizeof(desc), "%ci:%u",
  241. sidtype == SIDOWNER ? 'o' : 'g', cid);
  242. if (rc >= sizeof(desc))
  243. return -EINVAL;
  244. rc = 0;
  245. saved_cred = override_creds(root_cred);
  246. sidkey = request_key(&cifs_idmap_key_type, desc, "");
  247. if (IS_ERR(sidkey)) {
  248. rc = -EINVAL;
  249. cifs_dbg(FYI, "%s: Can't map %cid %u to a SID\n",
  250. __func__, sidtype == SIDOWNER ? 'u' : 'g', cid);
  251. goto out_revert_creds;
  252. } else if (sidkey->datalen < CIFS_SID_BASE_SIZE) {
  253. rc = -EIO;
  254. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  255. __func__, sidkey->datalen);
  256. goto invalidate_key;
  257. }
  258. /*
  259. * A sid is usually too large to be embedded in payload.value, but if
  260. * there are no subauthorities and the host has 8-byte pointers, then
  261. * it could be.
  262. */
  263. ksid = sidkey->datalen <= sizeof(sidkey->payload) ?
  264. (struct cifs_sid *)&sidkey->payload :
  265. (struct cifs_sid *)sidkey->payload.data[0];
  266. ksid_size = CIFS_SID_BASE_SIZE + (ksid->num_subauth * sizeof(__le32));
  267. if (ksid_size > sidkey->datalen) {
  268. rc = -EIO;
  269. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu, ksid_size=%u)\n",
  270. __func__, sidkey->datalen, ksid_size);
  271. goto invalidate_key;
  272. }
  273. cifs_copy_sid(ssid, ksid);
  274. out_key_put:
  275. key_put(sidkey);
  276. out_revert_creds:
  277. revert_creds(saved_cred);
  278. return rc;
  279. invalidate_key:
  280. key_invalidate(sidkey);
  281. goto out_key_put;
  282. }
  283. int
  284. sid_to_id(struct cifs_sb_info *cifs_sb, struct cifs_sid *psid,
  285. struct cifs_fattr *fattr, uint sidtype)
  286. {
  287. int rc = 0;
  288. struct key *sidkey;
  289. char *sidstr;
  290. const struct cred *saved_cred;
  291. kuid_t fuid = cifs_sb->ctx->linux_uid;
  292. kgid_t fgid = cifs_sb->ctx->linux_gid;
  293. /*
  294. * If we have too many subauthorities, then something is really wrong.
  295. * Just return an error.
  296. */
  297. if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
  298. cifs_dbg(FYI, "%s: %u subauthorities is too many!\n",
  299. __func__, psid->num_subauth);
  300. return -EIO;
  301. }
  302. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL) ||
  303. (cifs_sb_master_tcon(cifs_sb)->posix_extensions)) {
  304. uint32_t unix_id;
  305. bool is_group;
  306. if (sidtype != SIDOWNER)
  307. is_group = true;
  308. else
  309. is_group = false;
  310. if (is_well_known_sid(psid, &unix_id, is_group) == false)
  311. goto try_upcall_to_get_id;
  312. if (is_group) {
  313. kgid_t gid;
  314. gid_t id;
  315. id = (gid_t)unix_id;
  316. gid = make_kgid(&init_user_ns, id);
  317. if (gid_valid(gid)) {
  318. fgid = gid;
  319. goto got_valid_id;
  320. }
  321. } else {
  322. kuid_t uid;
  323. uid_t id;
  324. id = (uid_t)unix_id;
  325. uid = make_kuid(&init_user_ns, id);
  326. if (uid_valid(uid)) {
  327. fuid = uid;
  328. goto got_valid_id;
  329. }
  330. }
  331. /* If unable to find uid/gid easily from SID try via upcall */
  332. }
  333. try_upcall_to_get_id:
  334. sidstr = sid_to_key_str(psid, sidtype);
  335. if (!sidstr)
  336. return -ENOMEM;
  337. saved_cred = override_creds(root_cred);
  338. sidkey = request_key(&cifs_idmap_key_type, sidstr, "");
  339. if (IS_ERR(sidkey)) {
  340. cifs_dbg(FYI, "%s: Can't map SID %s to a %cid\n",
  341. __func__, sidstr, sidtype == SIDOWNER ? 'u' : 'g');
  342. goto out_revert_creds;
  343. }
  344. /*
  345. * FIXME: Here we assume that uid_t and gid_t are same size. It's
  346. * probably a safe assumption but might be better to check based on
  347. * sidtype.
  348. */
  349. BUILD_BUG_ON(sizeof(uid_t) != sizeof(gid_t));
  350. if (sidkey->datalen != sizeof(uid_t)) {
  351. cifs_dbg(FYI, "%s: Downcall contained malformed key (datalen=%hu)\n",
  352. __func__, sidkey->datalen);
  353. key_invalidate(sidkey);
  354. goto out_key_put;
  355. }
  356. if (sidtype == SIDOWNER) {
  357. kuid_t uid;
  358. uid_t id;
  359. memcpy(&id, &sidkey->payload.data[0], sizeof(uid_t));
  360. uid = make_kuid(&init_user_ns, id);
  361. if (uid_valid(uid))
  362. fuid = uid;
  363. } else {
  364. kgid_t gid;
  365. gid_t id;
  366. memcpy(&id, &sidkey->payload.data[0], sizeof(gid_t));
  367. gid = make_kgid(&init_user_ns, id);
  368. if (gid_valid(gid))
  369. fgid = gid;
  370. }
  371. out_key_put:
  372. key_put(sidkey);
  373. out_revert_creds:
  374. revert_creds(saved_cred);
  375. kfree(sidstr);
  376. /*
  377. * Note that we return 0 here unconditionally. If the mapping
  378. * fails then we just fall back to using the ctx->linux_uid/linux_gid.
  379. */
  380. got_valid_id:
  381. rc = 0;
  382. if (sidtype == SIDOWNER)
  383. fattr->cf_uid = fuid;
  384. else
  385. fattr->cf_gid = fgid;
  386. return rc;
  387. }
  388. int
  389. init_cifs_idmap(void)
  390. {
  391. struct cred *cred;
  392. struct key *keyring;
  393. int ret;
  394. cifs_dbg(FYI, "Registering the %s key type\n",
  395. cifs_idmap_key_type.name);
  396. /* create an override credential set with a special thread keyring in
  397. * which requests are cached
  398. *
  399. * this is used to prevent malicious redirections from being installed
  400. * with add_key().
  401. */
  402. cred = prepare_kernel_cred(NULL);
  403. if (!cred)
  404. return -ENOMEM;
  405. keyring = keyring_alloc(".cifs_idmap",
  406. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
  407. (KEY_POS_ALL & ~KEY_POS_SETATTR) |
  408. KEY_USR_VIEW | KEY_USR_READ,
  409. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  410. if (IS_ERR(keyring)) {
  411. ret = PTR_ERR(keyring);
  412. goto failed_put_cred;
  413. }
  414. ret = register_key_type(&cifs_idmap_key_type);
  415. if (ret < 0)
  416. goto failed_put_key;
  417. /* instruct request_key() to use this special keyring as a cache for
  418. * the results it looks up */
  419. set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
  420. cred->thread_keyring = keyring;
  421. cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  422. root_cred = cred;
  423. cifs_dbg(FYI, "cifs idmap keyring: %d\n", key_serial(keyring));
  424. return 0;
  425. failed_put_key:
  426. key_put(keyring);
  427. failed_put_cred:
  428. put_cred(cred);
  429. return ret;
  430. }
  431. void
  432. exit_cifs_idmap(void)
  433. {
  434. key_revoke(root_cred->thread_keyring);
  435. unregister_key_type(&cifs_idmap_key_type);
  436. put_cred(root_cred);
  437. cifs_dbg(FYI, "Unregistered %s key type\n", cifs_idmap_key_type.name);
  438. }
  439. /* copy ntsd, owner sid, and group sid from a security descriptor to another */
  440. static __u32 copy_sec_desc(const struct cifs_ntsd *pntsd,
  441. struct cifs_ntsd *pnntsd,
  442. __u32 sidsoffset,
  443. struct cifs_sid *pownersid,
  444. struct cifs_sid *pgrpsid)
  445. {
  446. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  447. struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
  448. /* copy security descriptor control portion */
  449. pnntsd->revision = pntsd->revision;
  450. pnntsd->type = pntsd->type;
  451. pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd));
  452. pnntsd->sacloffset = 0;
  453. pnntsd->osidoffset = cpu_to_le32(sidsoffset);
  454. pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid));
  455. /* copy owner sid */
  456. if (pownersid)
  457. owner_sid_ptr = pownersid;
  458. else
  459. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  460. le32_to_cpu(pntsd->osidoffset));
  461. nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset);
  462. cifs_copy_sid(nowner_sid_ptr, owner_sid_ptr);
  463. /* copy group sid */
  464. if (pgrpsid)
  465. group_sid_ptr = pgrpsid;
  466. else
  467. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  468. le32_to_cpu(pntsd->gsidoffset));
  469. ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset +
  470. sizeof(struct cifs_sid));
  471. cifs_copy_sid(ngroup_sid_ptr, group_sid_ptr);
  472. return sidsoffset + (2 * sizeof(struct cifs_sid));
  473. }
  474. /*
  475. change posix mode to reflect permissions
  476. pmode is the existing mode (we only want to overwrite part of this
  477. bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007
  478. */
  479. static void access_flags_to_mode(__le32 ace_flags, int type, umode_t *pmode,
  480. umode_t *pdenied, umode_t mask)
  481. {
  482. __u32 flags = le32_to_cpu(ace_flags);
  483. /*
  484. * Do not assume "preferred" or "canonical" order.
  485. * The first DENY or ALLOW ACE which matches perfectly is
  486. * the permission to be used. Once allowed or denied, same
  487. * permission in later ACEs do not matter.
  488. */
  489. /* If not already allowed, deny these bits */
  490. if (type == ACCESS_DENIED) {
  491. if (flags & GENERIC_ALL &&
  492. !(*pmode & mask & 0777))
  493. *pdenied |= mask & 0777;
  494. if (((flags & GENERIC_WRITE) ||
  495. ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) &&
  496. !(*pmode & mask & 0222))
  497. *pdenied |= mask & 0222;
  498. if (((flags & GENERIC_READ) ||
  499. ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) &&
  500. !(*pmode & mask & 0444))
  501. *pdenied |= mask & 0444;
  502. if (((flags & GENERIC_EXECUTE) ||
  503. ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) &&
  504. !(*pmode & mask & 0111))
  505. *pdenied |= mask & 0111;
  506. return;
  507. } else if (type != ACCESS_ALLOWED) {
  508. cifs_dbg(VFS, "unknown access control type %d\n", type);
  509. return;
  510. }
  511. /* else ACCESS_ALLOWED type */
  512. if ((flags & GENERIC_ALL) &&
  513. !(*pdenied & mask & 0777)) {
  514. *pmode |= mask & 0777;
  515. cifs_dbg(NOISY, "all perms\n");
  516. return;
  517. }
  518. if (((flags & GENERIC_WRITE) ||
  519. ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) &&
  520. !(*pdenied & mask & 0222))
  521. *pmode |= mask & 0222;
  522. if (((flags & GENERIC_READ) ||
  523. ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) &&
  524. !(*pdenied & mask & 0444))
  525. *pmode |= mask & 0444;
  526. if (((flags & GENERIC_EXECUTE) ||
  527. ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) &&
  528. !(*pdenied & mask & 0111))
  529. *pmode |= mask & 0111;
  530. /* If DELETE_CHILD is set only on an owner ACE, set sticky bit */
  531. if (flags & FILE_DELETE_CHILD) {
  532. if (mask == ACL_OWNER_MASK) {
  533. if (!(*pdenied & 01000))
  534. *pmode |= 01000;
  535. } else if (!(*pdenied & 01000)) {
  536. *pmode &= ~01000;
  537. *pdenied |= 01000;
  538. }
  539. }
  540. cifs_dbg(NOISY, "access flags 0x%x mode now %04o\n", flags, *pmode);
  541. return;
  542. }
  543. /*
  544. Generate access flags to reflect permissions mode is the existing mode.
  545. This function is called for every ACE in the DACL whose SID matches
  546. with either owner or group or everyone.
  547. */
  548. static void mode_to_access_flags(umode_t mode, umode_t bits_to_use,
  549. __u32 *pace_flags)
  550. {
  551. /* reset access mask */
  552. *pace_flags = 0x0;
  553. /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */
  554. mode &= bits_to_use;
  555. /* check for R/W/X UGO since we do not know whose flags
  556. is this but we have cleared all the bits sans RWX for
  557. either user or group or other as per bits_to_use */
  558. if (mode & S_IRUGO)
  559. *pace_flags |= SET_FILE_READ_RIGHTS;
  560. if (mode & S_IWUGO)
  561. *pace_flags |= SET_FILE_WRITE_RIGHTS;
  562. if (mode & S_IXUGO)
  563. *pace_flags |= SET_FILE_EXEC_RIGHTS;
  564. cifs_dbg(NOISY, "mode: %04o, access flags now 0x%x\n",
  565. mode, *pace_flags);
  566. return;
  567. }
  568. static __u16 cifs_copy_ace(struct cifs_ace *dst, struct cifs_ace *src, struct cifs_sid *psid)
  569. {
  570. __u16 size = 1 + 1 + 2 + 4;
  571. dst->type = src->type;
  572. dst->flags = src->flags;
  573. dst->access_req = src->access_req;
  574. /* Check if there's a replacement sid specified */
  575. if (psid)
  576. size += cifs_copy_sid(&dst->sid, psid);
  577. else
  578. size += cifs_copy_sid(&dst->sid, &src->sid);
  579. dst->size = cpu_to_le16(size);
  580. return size;
  581. }
  582. static __u16 fill_ace_for_sid(struct cifs_ace *pntace,
  583. const struct cifs_sid *psid, __u64 nmode,
  584. umode_t bits, __u8 access_type,
  585. bool allow_delete_child)
  586. {
  587. int i;
  588. __u16 size = 0;
  589. __u32 access_req = 0;
  590. pntace->type = access_type;
  591. pntace->flags = 0x0;
  592. mode_to_access_flags(nmode, bits, &access_req);
  593. if (access_type == ACCESS_ALLOWED && allow_delete_child)
  594. access_req |= FILE_DELETE_CHILD;
  595. if (access_type == ACCESS_ALLOWED && !access_req)
  596. access_req = SET_MINIMUM_RIGHTS;
  597. else if (access_type == ACCESS_DENIED)
  598. access_req &= ~SET_MINIMUM_RIGHTS;
  599. pntace->access_req = cpu_to_le32(access_req);
  600. pntace->sid.revision = psid->revision;
  601. pntace->sid.num_subauth = psid->num_subauth;
  602. for (i = 0; i < NUM_AUTHS; i++)
  603. pntace->sid.authority[i] = psid->authority[i];
  604. for (i = 0; i < psid->num_subauth; i++)
  605. pntace->sid.sub_auth[i] = psid->sub_auth[i];
  606. size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4);
  607. pntace->size = cpu_to_le16(size);
  608. return size;
  609. }
  610. #ifdef CONFIG_CIFS_DEBUG2
  611. static void dump_ace(struct cifs_ace *pace, char *end_of_acl)
  612. {
  613. int num_subauth;
  614. /* validate that we do not go past end of acl */
  615. if (le16_to_cpu(pace->size) < 16) {
  616. cifs_dbg(VFS, "ACE too small %d\n", le16_to_cpu(pace->size));
  617. return;
  618. }
  619. if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) {
  620. cifs_dbg(VFS, "ACL too small to parse ACE\n");
  621. return;
  622. }
  623. num_subauth = pace->sid.num_subauth;
  624. if (num_subauth) {
  625. int i;
  626. cifs_dbg(FYI, "ACE revision %d num_auth %d type %d flags %d size %d\n",
  627. pace->sid.revision, pace->sid.num_subauth, pace->type,
  628. pace->flags, le16_to_cpu(pace->size));
  629. for (i = 0; i < num_subauth; ++i) {
  630. cifs_dbg(FYI, "ACE sub_auth[%d]: 0x%x\n",
  631. i, le32_to_cpu(pace->sid.sub_auth[i]));
  632. }
  633. /* BB add length check to make sure that we do not have huge
  634. num auths and therefore go off the end */
  635. }
  636. return;
  637. }
  638. #endif
  639. static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl,
  640. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  641. struct cifs_fattr *fattr, bool mode_from_special_sid)
  642. {
  643. int i;
  644. int num_aces = 0;
  645. int acl_size;
  646. char *acl_base;
  647. struct cifs_ace **ppace;
  648. /* BB need to add parm so we can store the SID BB */
  649. if (!pdacl) {
  650. /* no DACL in the security descriptor, set
  651. all the permissions for user/group/other */
  652. fattr->cf_mode |= 0777;
  653. return;
  654. }
  655. /* validate that we do not go past end of acl */
  656. if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  657. cifs_dbg(VFS, "ACL too small to parse DACL\n");
  658. return;
  659. }
  660. cifs_dbg(NOISY, "DACL revision %d size %d num aces %d\n",
  661. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  662. le32_to_cpu(pdacl->num_aces));
  663. /* reset rwx permissions for user/group/other.
  664. Also, if num_aces is 0 i.e. DACL has no ACEs,
  665. user/group/other have no permissions */
  666. fattr->cf_mode &= ~(0777);
  667. acl_base = (char *)pdacl;
  668. acl_size = sizeof(struct cifs_acl);
  669. num_aces = le32_to_cpu(pdacl->num_aces);
  670. if (num_aces > 0) {
  671. umode_t denied_mode = 0;
  672. if (num_aces > ULONG_MAX / sizeof(struct cifs_ace *))
  673. return;
  674. ppace = kmalloc_array(num_aces, sizeof(struct cifs_ace *),
  675. GFP_KERNEL);
  676. if (!ppace)
  677. return;
  678. for (i = 0; i < num_aces; ++i) {
  679. ppace[i] = (struct cifs_ace *) (acl_base + acl_size);
  680. #ifdef CONFIG_CIFS_DEBUG2
  681. dump_ace(ppace[i], end_of_acl);
  682. #endif
  683. if (mode_from_special_sid &&
  684. (compare_sids(&(ppace[i]->sid),
  685. &sid_unix_NFS_mode) == 0)) {
  686. /*
  687. * Full permissions are:
  688. * 07777 = S_ISUID | S_ISGID | S_ISVTX |
  689. * S_IRWXU | S_IRWXG | S_IRWXO
  690. */
  691. fattr->cf_mode &= ~07777;
  692. fattr->cf_mode |=
  693. le32_to_cpu(ppace[i]->sid.sub_auth[2]);
  694. break;
  695. } else {
  696. if (compare_sids(&(ppace[i]->sid), pownersid) == 0) {
  697. access_flags_to_mode(ppace[i]->access_req,
  698. ppace[i]->type,
  699. &fattr->cf_mode,
  700. &denied_mode,
  701. ACL_OWNER_MASK);
  702. } else if (compare_sids(&(ppace[i]->sid), pgrpsid) == 0) {
  703. access_flags_to_mode(ppace[i]->access_req,
  704. ppace[i]->type,
  705. &fattr->cf_mode,
  706. &denied_mode,
  707. ACL_GROUP_MASK);
  708. } else if ((compare_sids(&(ppace[i]->sid), &sid_everyone) == 0) ||
  709. (compare_sids(&(ppace[i]->sid), &sid_authusers) == 0)) {
  710. access_flags_to_mode(ppace[i]->access_req,
  711. ppace[i]->type,
  712. &fattr->cf_mode,
  713. &denied_mode,
  714. ACL_EVERYONE_MASK);
  715. }
  716. }
  717. /* memcpy((void *)(&(cifscred->aces[i])),
  718. (void *)ppace[i],
  719. sizeof(struct cifs_ace)); */
  720. acl_base = (char *)ppace[i];
  721. acl_size = le16_to_cpu(ppace[i]->size);
  722. }
  723. kfree(ppace);
  724. }
  725. return;
  726. }
  727. unsigned int setup_authusers_ACE(struct cifs_ace *pntace)
  728. {
  729. int i;
  730. unsigned int ace_size = 20;
  731. pntace->type = ACCESS_ALLOWED_ACE_TYPE;
  732. pntace->flags = 0x0;
  733. pntace->access_req = cpu_to_le32(GENERIC_ALL);
  734. pntace->sid.num_subauth = 1;
  735. pntace->sid.revision = 1;
  736. for (i = 0; i < NUM_AUTHS; i++)
  737. pntace->sid.authority[i] = sid_authusers.authority[i];
  738. pntace->sid.sub_auth[0] = sid_authusers.sub_auth[0];
  739. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  740. pntace->size = cpu_to_le16(ace_size);
  741. return ace_size;
  742. }
  743. /*
  744. * Fill in the special SID based on the mode. See
  745. * https://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
  746. */
  747. unsigned int setup_special_mode_ACE(struct cifs_ace *pntace, __u64 nmode)
  748. {
  749. int i;
  750. unsigned int ace_size = 28;
  751. pntace->type = ACCESS_DENIED_ACE_TYPE;
  752. pntace->flags = 0x0;
  753. pntace->access_req = 0;
  754. pntace->sid.num_subauth = 3;
  755. pntace->sid.revision = 1;
  756. for (i = 0; i < NUM_AUTHS; i++)
  757. pntace->sid.authority[i] = sid_unix_NFS_mode.authority[i];
  758. pntace->sid.sub_auth[0] = sid_unix_NFS_mode.sub_auth[0];
  759. pntace->sid.sub_auth[1] = sid_unix_NFS_mode.sub_auth[1];
  760. pntace->sid.sub_auth[2] = cpu_to_le32(nmode & 07777);
  761. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  762. pntace->size = cpu_to_le16(ace_size);
  763. return ace_size;
  764. }
  765. unsigned int setup_special_user_owner_ACE(struct cifs_ace *pntace)
  766. {
  767. int i;
  768. unsigned int ace_size = 28;
  769. pntace->type = ACCESS_ALLOWED_ACE_TYPE;
  770. pntace->flags = 0x0;
  771. pntace->access_req = cpu_to_le32(GENERIC_ALL);
  772. pntace->sid.num_subauth = 3;
  773. pntace->sid.revision = 1;
  774. for (i = 0; i < NUM_AUTHS; i++)
  775. pntace->sid.authority[i] = sid_unix_NFS_users.authority[i];
  776. pntace->sid.sub_auth[0] = sid_unix_NFS_users.sub_auth[0];
  777. pntace->sid.sub_auth[1] = sid_unix_NFS_users.sub_auth[1];
  778. pntace->sid.sub_auth[2] = cpu_to_le32(current_fsgid().val);
  779. /* size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth*4) */
  780. pntace->size = cpu_to_le16(ace_size);
  781. return ace_size;
  782. }
  783. static void populate_new_aces(char *nacl_base,
  784. struct cifs_sid *pownersid,
  785. struct cifs_sid *pgrpsid,
  786. __u64 *pnmode, u32 *pnum_aces, u16 *pnsize,
  787. bool modefromsid)
  788. {
  789. __u64 nmode;
  790. u32 num_aces = 0;
  791. u16 nsize = 0;
  792. __u64 user_mode;
  793. __u64 group_mode;
  794. __u64 other_mode;
  795. __u64 deny_user_mode = 0;
  796. __u64 deny_group_mode = 0;
  797. bool sticky_set = false;
  798. struct cifs_ace *pnntace = NULL;
  799. nmode = *pnmode;
  800. num_aces = *pnum_aces;
  801. nsize = *pnsize;
  802. if (modefromsid) {
  803. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  804. nsize += setup_special_mode_ACE(pnntace, nmode);
  805. num_aces++;
  806. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  807. nsize += setup_authusers_ACE(pnntace);
  808. num_aces++;
  809. goto set_size;
  810. }
  811. /*
  812. * We'll try to keep the mode as requested by the user.
  813. * But in cases where we cannot meaningfully convert that
  814. * into ACL, return back the updated mode, so that it is
  815. * updated in the inode.
  816. */
  817. if (!memcmp(pownersid, pgrpsid, sizeof(struct cifs_sid))) {
  818. /*
  819. * Case when owner and group SIDs are the same.
  820. * Set the more restrictive of the two modes.
  821. */
  822. user_mode = nmode & (nmode << 3) & 0700;
  823. group_mode = nmode & (nmode >> 3) & 0070;
  824. } else {
  825. user_mode = nmode & 0700;
  826. group_mode = nmode & 0070;
  827. }
  828. other_mode = nmode & 0007;
  829. /* We need DENY ACE when the perm is more restrictive than the next sets. */
  830. deny_user_mode = ~(user_mode) & ((group_mode << 3) | (other_mode << 6)) & 0700;
  831. deny_group_mode = ~(group_mode) & (other_mode << 3) & 0070;
  832. *pnmode = user_mode | group_mode | other_mode | (nmode & ~0777);
  833. /* This tells if we should allow delete child for group and everyone. */
  834. if (nmode & 01000)
  835. sticky_set = true;
  836. if (deny_user_mode) {
  837. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  838. nsize += fill_ace_for_sid(pnntace, pownersid, deny_user_mode,
  839. 0700, ACCESS_DENIED, false);
  840. num_aces++;
  841. }
  842. /* Group DENY ACE does not conflict with owner ALLOW ACE. Keep in preferred order*/
  843. if (deny_group_mode && !(deny_group_mode & (user_mode >> 3))) {
  844. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  845. nsize += fill_ace_for_sid(pnntace, pgrpsid, deny_group_mode,
  846. 0070, ACCESS_DENIED, false);
  847. num_aces++;
  848. }
  849. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  850. nsize += fill_ace_for_sid(pnntace, pownersid, user_mode,
  851. 0700, ACCESS_ALLOWED, true);
  852. num_aces++;
  853. /* Group DENY ACE conflicts with owner ALLOW ACE. So keep it after. */
  854. if (deny_group_mode && (deny_group_mode & (user_mode >> 3))) {
  855. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  856. nsize += fill_ace_for_sid(pnntace, pgrpsid, deny_group_mode,
  857. 0070, ACCESS_DENIED, false);
  858. num_aces++;
  859. }
  860. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  861. nsize += fill_ace_for_sid(pnntace, pgrpsid, group_mode,
  862. 0070, ACCESS_ALLOWED, !sticky_set);
  863. num_aces++;
  864. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  865. nsize += fill_ace_for_sid(pnntace, &sid_everyone, other_mode,
  866. 0007, ACCESS_ALLOWED, !sticky_set);
  867. num_aces++;
  868. set_size:
  869. *pnum_aces = num_aces;
  870. *pnsize = nsize;
  871. }
  872. static __u16 replace_sids_and_copy_aces(struct cifs_acl *pdacl, struct cifs_acl *pndacl,
  873. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  874. struct cifs_sid *pnownersid, struct cifs_sid *pngrpsid)
  875. {
  876. int i;
  877. u16 size = 0;
  878. struct cifs_ace *pntace = NULL;
  879. char *acl_base = NULL;
  880. u32 src_num_aces = 0;
  881. u16 nsize = 0;
  882. struct cifs_ace *pnntace = NULL;
  883. char *nacl_base = NULL;
  884. u16 ace_size = 0;
  885. acl_base = (char *)pdacl;
  886. size = sizeof(struct cifs_acl);
  887. src_num_aces = le32_to_cpu(pdacl->num_aces);
  888. nacl_base = (char *)pndacl;
  889. nsize = sizeof(struct cifs_acl);
  890. /* Go through all the ACEs */
  891. for (i = 0; i < src_num_aces; ++i) {
  892. pntace = (struct cifs_ace *) (acl_base + size);
  893. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  894. if (pnownersid && compare_sids(&pntace->sid, pownersid) == 0)
  895. ace_size = cifs_copy_ace(pnntace, pntace, pnownersid);
  896. else if (pngrpsid && compare_sids(&pntace->sid, pgrpsid) == 0)
  897. ace_size = cifs_copy_ace(pnntace, pntace, pngrpsid);
  898. else
  899. ace_size = cifs_copy_ace(pnntace, pntace, NULL);
  900. size += le16_to_cpu(pntace->size);
  901. nsize += ace_size;
  902. }
  903. return nsize;
  904. }
  905. static int set_chmod_dacl(struct cifs_acl *pdacl, struct cifs_acl *pndacl,
  906. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  907. __u64 *pnmode, bool mode_from_sid)
  908. {
  909. int i;
  910. u16 size = 0;
  911. struct cifs_ace *pntace = NULL;
  912. char *acl_base = NULL;
  913. u32 src_num_aces = 0;
  914. u16 nsize = 0;
  915. struct cifs_ace *pnntace = NULL;
  916. char *nacl_base = NULL;
  917. u32 num_aces = 0;
  918. bool new_aces_set = false;
  919. /* Assuming that pndacl and pnmode are never NULL */
  920. nacl_base = (char *)pndacl;
  921. nsize = sizeof(struct cifs_acl);
  922. /* If pdacl is NULL, we don't have a src. Simply populate new ACL. */
  923. if (!pdacl) {
  924. populate_new_aces(nacl_base,
  925. pownersid, pgrpsid,
  926. pnmode, &num_aces, &nsize,
  927. mode_from_sid);
  928. goto finalize_dacl;
  929. }
  930. acl_base = (char *)pdacl;
  931. size = sizeof(struct cifs_acl);
  932. src_num_aces = le32_to_cpu(pdacl->num_aces);
  933. /* Retain old ACEs which we can retain */
  934. for (i = 0; i < src_num_aces; ++i) {
  935. pntace = (struct cifs_ace *) (acl_base + size);
  936. if (!new_aces_set && (pntace->flags & INHERITED_ACE)) {
  937. /* Place the new ACEs in between existing explicit and inherited */
  938. populate_new_aces(nacl_base,
  939. pownersid, pgrpsid,
  940. pnmode, &num_aces, &nsize,
  941. mode_from_sid);
  942. new_aces_set = true;
  943. }
  944. /* If it's any one of the ACE we're replacing, skip! */
  945. if (((compare_sids(&pntace->sid, &sid_unix_NFS_mode) == 0) ||
  946. (compare_sids(&pntace->sid, pownersid) == 0) ||
  947. (compare_sids(&pntace->sid, pgrpsid) == 0) ||
  948. (compare_sids(&pntace->sid, &sid_everyone) == 0) ||
  949. (compare_sids(&pntace->sid, &sid_authusers) == 0))) {
  950. goto next_ace;
  951. }
  952. /* update the pointer to the next ACE to populate*/
  953. pnntace = (struct cifs_ace *) (nacl_base + nsize);
  954. nsize += cifs_copy_ace(pnntace, pntace, NULL);
  955. num_aces++;
  956. next_ace:
  957. size += le16_to_cpu(pntace->size);
  958. }
  959. /* If inherited ACEs are not present, place the new ones at the tail */
  960. if (!new_aces_set) {
  961. populate_new_aces(nacl_base,
  962. pownersid, pgrpsid,
  963. pnmode, &num_aces, &nsize,
  964. mode_from_sid);
  965. new_aces_set = true;
  966. }
  967. finalize_dacl:
  968. pndacl->num_aces = cpu_to_le32(num_aces);
  969. pndacl->size = cpu_to_le16(nsize);
  970. return 0;
  971. }
  972. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  973. {
  974. /* BB need to add parm so we can store the SID BB */
  975. /* validate that we do not go past end of ACL - sid must be at least 8
  976. bytes long (assuming no sub-auths - e.g. the null SID */
  977. if (end_of_acl < (char *)psid + 8) {
  978. cifs_dbg(VFS, "ACL too small to parse SID %p\n", psid);
  979. return -EINVAL;
  980. }
  981. #ifdef CONFIG_CIFS_DEBUG2
  982. if (psid->num_subauth) {
  983. int i;
  984. cifs_dbg(FYI, "SID revision %d num_auth %d\n",
  985. psid->revision, psid->num_subauth);
  986. for (i = 0; i < psid->num_subauth; i++) {
  987. cifs_dbg(FYI, "SID sub_auth[%d]: 0x%x\n",
  988. i, le32_to_cpu(psid->sub_auth[i]));
  989. }
  990. /* BB add length check to make sure that we do not have huge
  991. num auths and therefore go off the end */
  992. cifs_dbg(FYI, "RID 0x%x\n",
  993. le32_to_cpu(psid->sub_auth[psid->num_subauth-1]));
  994. }
  995. #endif
  996. return 0;
  997. }
  998. /* Convert CIFS ACL to POSIX form */
  999. static int parse_sec_desc(struct cifs_sb_info *cifs_sb,
  1000. struct cifs_ntsd *pntsd, int acl_len, struct cifs_fattr *fattr,
  1001. bool get_mode_from_special_sid)
  1002. {
  1003. int rc = 0;
  1004. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  1005. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  1006. char *end_of_acl = ((char *)pntsd) + acl_len;
  1007. __u32 dacloffset;
  1008. if (pntsd == NULL)
  1009. return -EIO;
  1010. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  1011. le32_to_cpu(pntsd->osidoffset));
  1012. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  1013. le32_to_cpu(pntsd->gsidoffset));
  1014. dacloffset = le32_to_cpu(pntsd->dacloffset);
  1015. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  1016. cifs_dbg(NOISY, "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
  1017. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  1018. le32_to_cpu(pntsd->gsidoffset),
  1019. le32_to_cpu(pntsd->sacloffset), dacloffset);
  1020. /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */
  1021. rc = parse_sid(owner_sid_ptr, end_of_acl);
  1022. if (rc) {
  1023. cifs_dbg(FYI, "%s: Error %d parsing Owner SID\n", __func__, rc);
  1024. return rc;
  1025. }
  1026. rc = sid_to_id(cifs_sb, owner_sid_ptr, fattr, SIDOWNER);
  1027. if (rc) {
  1028. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to uid\n",
  1029. __func__, rc);
  1030. return rc;
  1031. }
  1032. rc = parse_sid(group_sid_ptr, end_of_acl);
  1033. if (rc) {
  1034. cifs_dbg(FYI, "%s: Error %d mapping Owner SID to gid\n",
  1035. __func__, rc);
  1036. return rc;
  1037. }
  1038. rc = sid_to_id(cifs_sb, group_sid_ptr, fattr, SIDGROUP);
  1039. if (rc) {
  1040. cifs_dbg(FYI, "%s: Error %d mapping Group SID to gid\n",
  1041. __func__, rc);
  1042. return rc;
  1043. }
  1044. if (dacloffset)
  1045. parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr,
  1046. group_sid_ptr, fattr, get_mode_from_special_sid);
  1047. else
  1048. cifs_dbg(FYI, "no ACL\n"); /* BB grant all or default perms? */
  1049. return rc;
  1050. }
  1051. /* Convert permission bits from mode to equivalent CIFS ACL */
  1052. static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd,
  1053. __u32 secdesclen, __u32 *pnsecdesclen, __u64 *pnmode, kuid_t uid, kgid_t gid,
  1054. bool mode_from_sid, bool id_from_sid, int *aclflag)
  1055. {
  1056. int rc = 0;
  1057. __u32 dacloffset;
  1058. __u32 ndacloffset;
  1059. __u32 sidsoffset;
  1060. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  1061. struct cifs_sid *nowner_sid_ptr = NULL, *ngroup_sid_ptr = NULL;
  1062. struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */
  1063. struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */
  1064. char *end_of_acl = ((char *)pntsd) + secdesclen;
  1065. u16 size = 0;
  1066. dacloffset = le32_to_cpu(pntsd->dacloffset);
  1067. if (dacloffset) {
  1068. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  1069. if (end_of_acl < (char *)dacl_ptr + le16_to_cpu(dacl_ptr->size)) {
  1070. cifs_dbg(VFS, "Server returned illegal ACL size\n");
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  1075. le32_to_cpu(pntsd->osidoffset));
  1076. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  1077. le32_to_cpu(pntsd->gsidoffset));
  1078. if (pnmode && *pnmode != NO_CHANGE_64) { /* chmod */
  1079. ndacloffset = sizeof(struct cifs_ntsd);
  1080. ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset);
  1081. ndacl_ptr->revision =
  1082. dacloffset ? dacl_ptr->revision : cpu_to_le16(ACL_REVISION);
  1083. ndacl_ptr->size = cpu_to_le16(0);
  1084. ndacl_ptr->num_aces = cpu_to_le32(0);
  1085. rc = set_chmod_dacl(dacl_ptr, ndacl_ptr, owner_sid_ptr, group_sid_ptr,
  1086. pnmode, mode_from_sid);
  1087. sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size);
  1088. /* copy the non-dacl portion of secdesc */
  1089. *pnsecdesclen = copy_sec_desc(pntsd, pnntsd, sidsoffset,
  1090. NULL, NULL);
  1091. *aclflag |= CIFS_ACL_DACL;
  1092. } else {
  1093. ndacloffset = sizeof(struct cifs_ntsd);
  1094. ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset);
  1095. ndacl_ptr->revision =
  1096. dacloffset ? dacl_ptr->revision : cpu_to_le16(ACL_REVISION);
  1097. ndacl_ptr->num_aces = dacl_ptr ? dacl_ptr->num_aces : 0;
  1098. if (uid_valid(uid)) { /* chown */
  1099. uid_t id;
  1100. nowner_sid_ptr = kzalloc(sizeof(struct cifs_sid),
  1101. GFP_KERNEL);
  1102. if (!nowner_sid_ptr) {
  1103. rc = -ENOMEM;
  1104. goto chown_chgrp_exit;
  1105. }
  1106. id = from_kuid(&init_user_ns, uid);
  1107. if (id_from_sid) {
  1108. struct owner_sid *osid = (struct owner_sid *)nowner_sid_ptr;
  1109. /* Populate the user ownership fields S-1-5-88-1 */
  1110. osid->Revision = 1;
  1111. osid->NumAuth = 3;
  1112. osid->Authority[5] = 5;
  1113. osid->SubAuthorities[0] = cpu_to_le32(88);
  1114. osid->SubAuthorities[1] = cpu_to_le32(1);
  1115. osid->SubAuthorities[2] = cpu_to_le32(id);
  1116. } else { /* lookup sid with upcall */
  1117. rc = id_to_sid(id, SIDOWNER, nowner_sid_ptr);
  1118. if (rc) {
  1119. cifs_dbg(FYI, "%s: Mapping error %d for owner id %d\n",
  1120. __func__, rc, id);
  1121. goto chown_chgrp_exit;
  1122. }
  1123. }
  1124. *aclflag |= CIFS_ACL_OWNER;
  1125. }
  1126. if (gid_valid(gid)) { /* chgrp */
  1127. gid_t id;
  1128. ngroup_sid_ptr = kzalloc(sizeof(struct cifs_sid),
  1129. GFP_KERNEL);
  1130. if (!ngroup_sid_ptr) {
  1131. rc = -ENOMEM;
  1132. goto chown_chgrp_exit;
  1133. }
  1134. id = from_kgid(&init_user_ns, gid);
  1135. if (id_from_sid) {
  1136. struct owner_sid *gsid = (struct owner_sid *)ngroup_sid_ptr;
  1137. /* Populate the group ownership fields S-1-5-88-2 */
  1138. gsid->Revision = 1;
  1139. gsid->NumAuth = 3;
  1140. gsid->Authority[5] = 5;
  1141. gsid->SubAuthorities[0] = cpu_to_le32(88);
  1142. gsid->SubAuthorities[1] = cpu_to_le32(2);
  1143. gsid->SubAuthorities[2] = cpu_to_le32(id);
  1144. } else { /* lookup sid with upcall */
  1145. rc = id_to_sid(id, SIDGROUP, ngroup_sid_ptr);
  1146. if (rc) {
  1147. cifs_dbg(FYI, "%s: Mapping error %d for group id %d\n",
  1148. __func__, rc, id);
  1149. goto chown_chgrp_exit;
  1150. }
  1151. }
  1152. *aclflag |= CIFS_ACL_GROUP;
  1153. }
  1154. if (dacloffset) {
  1155. /* Replace ACEs for old owner with new one */
  1156. size = replace_sids_and_copy_aces(dacl_ptr, ndacl_ptr,
  1157. owner_sid_ptr, group_sid_ptr,
  1158. nowner_sid_ptr, ngroup_sid_ptr);
  1159. ndacl_ptr->size = cpu_to_le16(size);
  1160. }
  1161. sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size);
  1162. /* copy the non-dacl portion of secdesc */
  1163. *pnsecdesclen = copy_sec_desc(pntsd, pnntsd, sidsoffset,
  1164. nowner_sid_ptr, ngroup_sid_ptr);
  1165. chown_chgrp_exit:
  1166. /* errors could jump here. So make sure we return soon after this */
  1167. kfree(nowner_sid_ptr);
  1168. kfree(ngroup_sid_ptr);
  1169. }
  1170. return rc;
  1171. }
  1172. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1173. struct cifs_ntsd *get_cifs_acl_by_fid(struct cifs_sb_info *cifs_sb,
  1174. const struct cifs_fid *cifsfid, u32 *pacllen,
  1175. u32 __maybe_unused unused)
  1176. {
  1177. struct cifs_ntsd *pntsd = NULL;
  1178. unsigned int xid;
  1179. int rc;
  1180. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1181. if (IS_ERR(tlink))
  1182. return ERR_CAST(tlink);
  1183. xid = get_xid();
  1184. rc = CIFSSMBGetCIFSACL(xid, tlink_tcon(tlink), cifsfid->netfid, &pntsd,
  1185. pacllen);
  1186. free_xid(xid);
  1187. cifs_put_tlink(tlink);
  1188. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  1189. if (rc)
  1190. return ERR_PTR(rc);
  1191. return pntsd;
  1192. }
  1193. static struct cifs_ntsd *get_cifs_acl_by_path(struct cifs_sb_info *cifs_sb,
  1194. const char *path, u32 *pacllen)
  1195. {
  1196. struct cifs_ntsd *pntsd = NULL;
  1197. int oplock = 0;
  1198. unsigned int xid;
  1199. int rc;
  1200. struct cifs_tcon *tcon;
  1201. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1202. struct cifs_fid fid;
  1203. struct cifs_open_parms oparms;
  1204. if (IS_ERR(tlink))
  1205. return ERR_CAST(tlink);
  1206. tcon = tlink_tcon(tlink);
  1207. xid = get_xid();
  1208. oparms = (struct cifs_open_parms) {
  1209. .tcon = tcon,
  1210. .cifs_sb = cifs_sb,
  1211. .desired_access = READ_CONTROL,
  1212. .create_options = cifs_create_options(cifs_sb, 0),
  1213. .disposition = FILE_OPEN,
  1214. .path = path,
  1215. .fid = &fid,
  1216. };
  1217. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1218. if (!rc) {
  1219. rc = CIFSSMBGetCIFSACL(xid, tcon, fid.netfid, &pntsd, pacllen);
  1220. CIFSSMBClose(xid, tcon, fid.netfid);
  1221. }
  1222. cifs_put_tlink(tlink);
  1223. free_xid(xid);
  1224. cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
  1225. if (rc)
  1226. return ERR_PTR(rc);
  1227. return pntsd;
  1228. }
  1229. /* Retrieve an ACL from the server */
  1230. struct cifs_ntsd *get_cifs_acl(struct cifs_sb_info *cifs_sb,
  1231. struct inode *inode, const char *path,
  1232. u32 *pacllen, u32 info)
  1233. {
  1234. struct cifs_ntsd *pntsd = NULL;
  1235. struct cifsFileInfo *open_file = NULL;
  1236. if (inode)
  1237. open_file = find_readable_file(CIFS_I(inode), true);
  1238. if (!open_file)
  1239. return get_cifs_acl_by_path(cifs_sb, path, pacllen);
  1240. pntsd = get_cifs_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
  1241. cifsFileInfo_put(open_file);
  1242. return pntsd;
  1243. }
  1244. /* Set an ACL on the server */
  1245. int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
  1246. struct inode *inode, const char *path, int aclflag)
  1247. {
  1248. int oplock = 0;
  1249. unsigned int xid;
  1250. int rc, access_flags;
  1251. struct cifs_tcon *tcon;
  1252. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1253. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1254. struct cifs_fid fid;
  1255. struct cifs_open_parms oparms;
  1256. if (IS_ERR(tlink))
  1257. return PTR_ERR(tlink);
  1258. tcon = tlink_tcon(tlink);
  1259. xid = get_xid();
  1260. if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
  1261. access_flags = WRITE_OWNER;
  1262. else
  1263. access_flags = WRITE_DAC;
  1264. oparms = (struct cifs_open_parms) {
  1265. .tcon = tcon,
  1266. .cifs_sb = cifs_sb,
  1267. .desired_access = access_flags,
  1268. .create_options = cifs_create_options(cifs_sb, 0),
  1269. .disposition = FILE_OPEN,
  1270. .path = path,
  1271. .fid = &fid,
  1272. };
  1273. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1274. if (rc) {
  1275. cifs_dbg(VFS, "Unable to open file to set ACL\n");
  1276. goto out;
  1277. }
  1278. rc = CIFSSMBSetCIFSACL(xid, tcon, fid.netfid, pnntsd, acllen, aclflag);
  1279. cifs_dbg(NOISY, "SetCIFSACL rc = %d\n", rc);
  1280. CIFSSMBClose(xid, tcon, fid.netfid);
  1281. out:
  1282. free_xid(xid);
  1283. cifs_put_tlink(tlink);
  1284. return rc;
  1285. }
  1286. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1287. /* Translate the CIFS ACL (similar to NTFS ACL) for a file into mode bits */
  1288. int
  1289. cifs_acl_to_fattr(struct cifs_sb_info *cifs_sb, struct cifs_fattr *fattr,
  1290. struct inode *inode, bool mode_from_special_sid,
  1291. const char *path, const struct cifs_fid *pfid)
  1292. {
  1293. struct cifs_ntsd *pntsd = NULL;
  1294. u32 acllen = 0;
  1295. int rc = 0;
  1296. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1297. struct smb_version_operations *ops;
  1298. const u32 info = 0;
  1299. cifs_dbg(NOISY, "converting ACL to mode for %s\n", path);
  1300. if (IS_ERR(tlink))
  1301. return PTR_ERR(tlink);
  1302. ops = tlink_tcon(tlink)->ses->server->ops;
  1303. if (pfid && (ops->get_acl_by_fid))
  1304. pntsd = ops->get_acl_by_fid(cifs_sb, pfid, &acllen, info);
  1305. else if (ops->get_acl)
  1306. pntsd = ops->get_acl(cifs_sb, inode, path, &acllen, info);
  1307. else {
  1308. cifs_put_tlink(tlink);
  1309. return -EOPNOTSUPP;
  1310. }
  1311. /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */
  1312. if (IS_ERR(pntsd)) {
  1313. rc = PTR_ERR(pntsd);
  1314. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  1315. } else if (mode_from_special_sid) {
  1316. rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr, true);
  1317. kfree(pntsd);
  1318. } else {
  1319. /* get approximated mode from ACL */
  1320. rc = parse_sec_desc(cifs_sb, pntsd, acllen, fattr, false);
  1321. kfree(pntsd);
  1322. if (rc)
  1323. cifs_dbg(VFS, "parse sec desc failed rc = %d\n", rc);
  1324. }
  1325. cifs_put_tlink(tlink);
  1326. return rc;
  1327. }
  1328. /* Convert mode bits to an ACL so we can update the ACL on the server */
  1329. int
  1330. id_mode_to_cifs_acl(struct inode *inode, const char *path, __u64 *pnmode,
  1331. kuid_t uid, kgid_t gid)
  1332. {
  1333. int rc = 0;
  1334. int aclflag = CIFS_ACL_DACL; /* default flag to set */
  1335. __u32 secdesclen = 0;
  1336. __u32 nsecdesclen = 0;
  1337. __u32 dacloffset = 0;
  1338. struct cifs_acl *dacl_ptr = NULL;
  1339. struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */
  1340. struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */
  1341. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1342. struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
  1343. struct smb_version_operations *ops;
  1344. bool mode_from_sid, id_from_sid;
  1345. const u32 info = 0;
  1346. if (IS_ERR(tlink))
  1347. return PTR_ERR(tlink);
  1348. ops = tlink_tcon(tlink)->ses->server->ops;
  1349. cifs_dbg(NOISY, "set ACL from mode for %s\n", path);
  1350. /* Get the security descriptor */
  1351. if (ops->get_acl == NULL) {
  1352. cifs_put_tlink(tlink);
  1353. return -EOPNOTSUPP;
  1354. }
  1355. pntsd = ops->get_acl(cifs_sb, inode, path, &secdesclen, info);
  1356. if (IS_ERR(pntsd)) {
  1357. rc = PTR_ERR(pntsd);
  1358. cifs_dbg(VFS, "%s: error %d getting sec desc\n", __func__, rc);
  1359. cifs_put_tlink(tlink);
  1360. return rc;
  1361. }
  1362. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)
  1363. mode_from_sid = true;
  1364. else
  1365. mode_from_sid = false;
  1366. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
  1367. id_from_sid = true;
  1368. else
  1369. id_from_sid = false;
  1370. /* Potentially, five new ACEs can be added to the ACL for U,G,O mapping */
  1371. nsecdesclen = secdesclen;
  1372. if (pnmode && *pnmode != NO_CHANGE_64) { /* chmod */
  1373. if (mode_from_sid)
  1374. nsecdesclen += 2 * sizeof(struct cifs_ace);
  1375. else /* cifsacl */
  1376. nsecdesclen += 5 * sizeof(struct cifs_ace);
  1377. } else { /* chown */
  1378. /* When ownership changes, changes new owner sid length could be different */
  1379. nsecdesclen = sizeof(struct cifs_ntsd) + (sizeof(struct cifs_sid) * 2);
  1380. dacloffset = le32_to_cpu(pntsd->dacloffset);
  1381. if (dacloffset) {
  1382. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  1383. if (mode_from_sid)
  1384. nsecdesclen +=
  1385. le32_to_cpu(dacl_ptr->num_aces) * sizeof(struct cifs_ace);
  1386. else /* cifsacl */
  1387. nsecdesclen += le16_to_cpu(dacl_ptr->size);
  1388. }
  1389. }
  1390. /*
  1391. * Add three ACEs for owner, group, everyone getting rid of other ACEs
  1392. * as chmod disables ACEs and set the security descriptor. Allocate
  1393. * memory for the smb header, set security descriptor request security
  1394. * descriptor parameters, and security descriptor itself
  1395. */
  1396. nsecdesclen = max_t(u32, nsecdesclen, DEFAULT_SEC_DESC_LEN);
  1397. pnntsd = kmalloc(nsecdesclen, GFP_KERNEL);
  1398. if (!pnntsd) {
  1399. kfree(pntsd);
  1400. cifs_put_tlink(tlink);
  1401. return -ENOMEM;
  1402. }
  1403. rc = build_sec_desc(pntsd, pnntsd, secdesclen, &nsecdesclen, pnmode, uid, gid,
  1404. mode_from_sid, id_from_sid, &aclflag);
  1405. cifs_dbg(NOISY, "build_sec_desc rc: %d\n", rc);
  1406. if (ops->set_acl == NULL)
  1407. rc = -EOPNOTSUPP;
  1408. if (!rc) {
  1409. /* Set the security descriptor */
  1410. rc = ops->set_acl(pnntsd, nsecdesclen, inode, path, aclflag);
  1411. cifs_dbg(NOISY, "set_cifs_acl rc: %d\n", rc);
  1412. }
  1413. cifs_put_tlink(tlink);
  1414. kfree(pnntsd);
  1415. kfree(pntsd);
  1416. return rc;
  1417. }