cifsacl.c 30 KB

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