posix_acl.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
  4. *
  5. * Fixes from William Schumacher incorporated on 15 March 2001.
  6. * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
  7. */
  8. /*
  9. * This file contains generic functions for manipulating
  10. * POSIX 1003.1e draft standard 17 ACLs.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/atomic.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/cred.h>
  18. #include <linux/posix_acl.h>
  19. #include <linux/posix_acl_xattr.h>
  20. #include <linux/xattr.h>
  21. #include <linux/export.h>
  22. #include <linux/user_namespace.h>
  23. #include <linux/namei.h>
  24. #include <linux/mnt_idmapping.h>
  25. #include <linux/iversion.h>
  26. #include <linux/security.h>
  27. #include <linux/fsnotify.h>
  28. #include <linux/filelock.h>
  29. #include "internal.h"
  30. static struct posix_acl **acl_by_type(struct inode *inode, int type)
  31. {
  32. switch (type) {
  33. case ACL_TYPE_ACCESS:
  34. return &inode->i_acl;
  35. case ACL_TYPE_DEFAULT:
  36. return &inode->i_default_acl;
  37. default:
  38. BUG();
  39. }
  40. }
  41. struct posix_acl *get_cached_acl(struct inode *inode, int type)
  42. {
  43. struct posix_acl **p = acl_by_type(inode, type);
  44. struct posix_acl *acl;
  45. for (;;) {
  46. rcu_read_lock();
  47. acl = rcu_dereference(*p);
  48. if (!acl || is_uncached_acl(acl) ||
  49. refcount_inc_not_zero(&acl->a_refcount))
  50. break;
  51. rcu_read_unlock();
  52. cpu_relax();
  53. }
  54. rcu_read_unlock();
  55. return acl;
  56. }
  57. EXPORT_SYMBOL(get_cached_acl);
  58. struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
  59. {
  60. struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
  61. if (acl == ACL_DONT_CACHE) {
  62. struct posix_acl *ret;
  63. ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU);
  64. if (!IS_ERR(ret))
  65. acl = ret;
  66. }
  67. return acl;
  68. }
  69. EXPORT_SYMBOL(get_cached_acl_rcu);
  70. void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
  71. {
  72. struct posix_acl **p = acl_by_type(inode, type);
  73. struct posix_acl *old;
  74. old = xchg(p, posix_acl_dup(acl));
  75. if (!is_uncached_acl(old))
  76. posix_acl_release(old);
  77. }
  78. EXPORT_SYMBOL(set_cached_acl);
  79. static void __forget_cached_acl(struct posix_acl **p)
  80. {
  81. struct posix_acl *old;
  82. old = xchg(p, ACL_NOT_CACHED);
  83. if (!is_uncached_acl(old))
  84. posix_acl_release(old);
  85. }
  86. void forget_cached_acl(struct inode *inode, int type)
  87. {
  88. __forget_cached_acl(acl_by_type(inode, type));
  89. }
  90. EXPORT_SYMBOL(forget_cached_acl);
  91. void forget_all_cached_acls(struct inode *inode)
  92. {
  93. __forget_cached_acl(&inode->i_acl);
  94. __forget_cached_acl(&inode->i_default_acl);
  95. }
  96. EXPORT_SYMBOL(forget_all_cached_acls);
  97. static struct posix_acl *__get_acl(struct mnt_idmap *idmap,
  98. struct dentry *dentry, struct inode *inode,
  99. int type)
  100. {
  101. struct posix_acl *sentinel;
  102. struct posix_acl **p;
  103. struct posix_acl *acl;
  104. /*
  105. * The sentinel is used to detect when another operation like
  106. * set_cached_acl() or forget_cached_acl() races with get_inode_acl().
  107. * It is guaranteed that is_uncached_acl(sentinel) is true.
  108. */
  109. acl = get_cached_acl(inode, type);
  110. if (!is_uncached_acl(acl))
  111. return acl;
  112. if (!IS_POSIXACL(inode))
  113. return NULL;
  114. sentinel = uncached_acl_sentinel(current);
  115. p = acl_by_type(inode, type);
  116. /*
  117. * If the ACL isn't being read yet, set our sentinel. Otherwise, the
  118. * current value of the ACL will not be ACL_NOT_CACHED and so our own
  119. * sentinel will not be set; another task will update the cache. We
  120. * could wait for that other task to complete its job, but it's easier
  121. * to just call ->get_inode_acl to fetch the ACL ourself. (This is
  122. * going to be an unlikely race.)
  123. */
  124. cmpxchg(p, ACL_NOT_CACHED, sentinel);
  125. /*
  126. * Normally, the ACL returned by ->get{_inode}_acl will be cached.
  127. * A filesystem can prevent that by calling
  128. * forget_cached_acl(inode, type) in ->get{_inode}_acl.
  129. *
  130. * If the filesystem doesn't have a get{_inode}_ acl() function at all,
  131. * we'll just create the negative cache entry.
  132. */
  133. if (dentry && inode->i_op->get_acl) {
  134. acl = inode->i_op->get_acl(idmap, dentry, type);
  135. } else if (inode->i_op->get_inode_acl) {
  136. acl = inode->i_op->get_inode_acl(inode, type, false);
  137. } else {
  138. set_cached_acl(inode, type, NULL);
  139. return NULL;
  140. }
  141. if (IS_ERR(acl)) {
  142. /*
  143. * Remove our sentinel so that we don't block future attempts
  144. * to cache the ACL.
  145. */
  146. cmpxchg(p, sentinel, ACL_NOT_CACHED);
  147. return acl;
  148. }
  149. /*
  150. * Cache the result, but only if our sentinel is still in place.
  151. */
  152. posix_acl_dup(acl);
  153. if (unlikely(!try_cmpxchg(p, &sentinel, acl)))
  154. posix_acl_release(acl);
  155. return acl;
  156. }
  157. struct posix_acl *get_inode_acl(struct inode *inode, int type)
  158. {
  159. return __get_acl(&nop_mnt_idmap, NULL, inode, type);
  160. }
  161. EXPORT_SYMBOL(get_inode_acl);
  162. /*
  163. * Init a fresh posix_acl
  164. */
  165. void
  166. posix_acl_init(struct posix_acl *acl, int count)
  167. {
  168. refcount_set(&acl->a_refcount, 1);
  169. acl->a_count = count;
  170. }
  171. EXPORT_SYMBOL(posix_acl_init);
  172. /*
  173. * Allocate a new ACL with the specified number of entries.
  174. */
  175. struct posix_acl *
  176. posix_acl_alloc(unsigned int count, gfp_t flags)
  177. {
  178. struct posix_acl *acl;
  179. acl = kmalloc_flex(*acl, a_entries, count, flags);
  180. if (acl)
  181. posix_acl_init(acl, count);
  182. return acl;
  183. }
  184. EXPORT_SYMBOL(posix_acl_alloc);
  185. /*
  186. * Clone an ACL.
  187. */
  188. struct posix_acl *
  189. posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
  190. {
  191. struct posix_acl *clone = NULL;
  192. if (acl) {
  193. clone = kmemdup(acl, struct_size(acl, a_entries, acl->a_count),
  194. flags);
  195. if (clone)
  196. refcount_set(&clone->a_refcount, 1);
  197. }
  198. return clone;
  199. }
  200. EXPORT_SYMBOL_GPL(posix_acl_clone);
  201. /*
  202. * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
  203. */
  204. int
  205. posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
  206. {
  207. const struct posix_acl_entry *pa, *pe;
  208. int state = ACL_USER_OBJ;
  209. int needs_mask = 0;
  210. FOREACH_ACL_ENTRY(pa, acl, pe) {
  211. if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
  212. return -EINVAL;
  213. switch (pa->e_tag) {
  214. case ACL_USER_OBJ:
  215. if (state == ACL_USER_OBJ) {
  216. state = ACL_USER;
  217. break;
  218. }
  219. return -EINVAL;
  220. case ACL_USER:
  221. if (state != ACL_USER)
  222. return -EINVAL;
  223. if (!kuid_has_mapping(user_ns, pa->e_uid))
  224. return -EINVAL;
  225. needs_mask = 1;
  226. break;
  227. case ACL_GROUP_OBJ:
  228. if (state == ACL_USER) {
  229. state = ACL_GROUP;
  230. break;
  231. }
  232. return -EINVAL;
  233. case ACL_GROUP:
  234. if (state != ACL_GROUP)
  235. return -EINVAL;
  236. if (!kgid_has_mapping(user_ns, pa->e_gid))
  237. return -EINVAL;
  238. needs_mask = 1;
  239. break;
  240. case ACL_MASK:
  241. if (state != ACL_GROUP)
  242. return -EINVAL;
  243. state = ACL_OTHER;
  244. break;
  245. case ACL_OTHER:
  246. if (state == ACL_OTHER ||
  247. (state == ACL_GROUP && !needs_mask)) {
  248. state = 0;
  249. break;
  250. }
  251. return -EINVAL;
  252. default:
  253. return -EINVAL;
  254. }
  255. }
  256. if (state == 0)
  257. return 0;
  258. return -EINVAL;
  259. }
  260. EXPORT_SYMBOL(posix_acl_valid);
  261. /*
  262. * Returns 0 if the acl can be exactly represented in the traditional
  263. * file mode permission bits, or else 1. Returns -E... on error.
  264. */
  265. int
  266. posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
  267. {
  268. const struct posix_acl_entry *pa, *pe;
  269. umode_t mode = 0;
  270. int not_equiv = 0;
  271. /*
  272. * A null ACL can always be presented as mode bits.
  273. */
  274. if (!acl)
  275. return 0;
  276. FOREACH_ACL_ENTRY(pa, acl, pe) {
  277. switch (pa->e_tag) {
  278. case ACL_USER_OBJ:
  279. mode |= (pa->e_perm & S_IRWXO) << 6;
  280. break;
  281. case ACL_GROUP_OBJ:
  282. mode |= (pa->e_perm & S_IRWXO) << 3;
  283. break;
  284. case ACL_OTHER:
  285. mode |= pa->e_perm & S_IRWXO;
  286. break;
  287. case ACL_MASK:
  288. mode = (mode & ~S_IRWXG) |
  289. ((pa->e_perm & S_IRWXO) << 3);
  290. not_equiv = 1;
  291. break;
  292. case ACL_USER:
  293. case ACL_GROUP:
  294. not_equiv = 1;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. }
  300. if (mode_p)
  301. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  302. return not_equiv;
  303. }
  304. EXPORT_SYMBOL(posix_acl_equiv_mode);
  305. /*
  306. * Create an ACL representing the file mode permission bits of an inode.
  307. */
  308. struct posix_acl *
  309. posix_acl_from_mode(umode_t mode, gfp_t flags)
  310. {
  311. struct posix_acl *acl = posix_acl_alloc(3, flags);
  312. if (!acl)
  313. return ERR_PTR(-ENOMEM);
  314. acl->a_entries[0].e_tag = ACL_USER_OBJ;
  315. acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
  316. acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
  317. acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
  318. acl->a_entries[2].e_tag = ACL_OTHER;
  319. acl->a_entries[2].e_perm = (mode & S_IRWXO);
  320. return acl;
  321. }
  322. EXPORT_SYMBOL(posix_acl_from_mode);
  323. /*
  324. * Return 0 if current is granted want access to the inode
  325. * by the acl. Returns -E... otherwise.
  326. */
  327. int
  328. posix_acl_permission(struct mnt_idmap *idmap, struct inode *inode,
  329. const struct posix_acl *acl, int want)
  330. {
  331. const struct posix_acl_entry *pa, *pe, *mask_obj;
  332. struct user_namespace *fs_userns = i_user_ns(inode);
  333. int found = 0;
  334. vfsuid_t vfsuid;
  335. vfsgid_t vfsgid;
  336. want &= MAY_READ | MAY_WRITE | MAY_EXEC;
  337. FOREACH_ACL_ENTRY(pa, acl, pe) {
  338. switch(pa->e_tag) {
  339. case ACL_USER_OBJ:
  340. /* (May have been checked already) */
  341. vfsuid = i_uid_into_vfsuid(idmap, inode);
  342. if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
  343. goto check_perm;
  344. break;
  345. case ACL_USER:
  346. vfsuid = make_vfsuid(idmap, fs_userns,
  347. pa->e_uid);
  348. if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
  349. goto mask;
  350. break;
  351. case ACL_GROUP_OBJ:
  352. vfsgid = i_gid_into_vfsgid(idmap, inode);
  353. if (vfsgid_in_group_p(vfsgid)) {
  354. found = 1;
  355. if ((pa->e_perm & want) == want)
  356. goto mask;
  357. }
  358. break;
  359. case ACL_GROUP:
  360. vfsgid = make_vfsgid(idmap, fs_userns,
  361. pa->e_gid);
  362. if (vfsgid_in_group_p(vfsgid)) {
  363. found = 1;
  364. if ((pa->e_perm & want) == want)
  365. goto mask;
  366. }
  367. break;
  368. case ACL_MASK:
  369. break;
  370. case ACL_OTHER:
  371. if (found)
  372. return -EACCES;
  373. else
  374. goto check_perm;
  375. default:
  376. return -EIO;
  377. }
  378. }
  379. return -EIO;
  380. mask:
  381. for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
  382. if (mask_obj->e_tag == ACL_MASK) {
  383. if ((pa->e_perm & mask_obj->e_perm & want) == want)
  384. return 0;
  385. return -EACCES;
  386. }
  387. }
  388. check_perm:
  389. if ((pa->e_perm & want) == want)
  390. return 0;
  391. return -EACCES;
  392. }
  393. /*
  394. * Modify acl when creating a new inode. The caller must ensure the acl is
  395. * only referenced once.
  396. *
  397. * mode_p initially must contain the mode parameter to the open() / creat()
  398. * system calls. All permissions that are not granted by the acl are removed.
  399. * The permissions in the acl are changed to reflect the mode_p parameter.
  400. */
  401. static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  402. {
  403. struct posix_acl_entry *pa, *pe;
  404. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  405. umode_t mode = *mode_p;
  406. int not_equiv = 0;
  407. /* assert(atomic_read(acl->a_refcount) == 1); */
  408. FOREACH_ACL_ENTRY(pa, acl, pe) {
  409. switch(pa->e_tag) {
  410. case ACL_USER_OBJ:
  411. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  412. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  413. break;
  414. case ACL_USER:
  415. case ACL_GROUP:
  416. not_equiv = 1;
  417. break;
  418. case ACL_GROUP_OBJ:
  419. group_obj = pa;
  420. break;
  421. case ACL_OTHER:
  422. pa->e_perm &= mode | ~S_IRWXO;
  423. mode &= pa->e_perm | ~S_IRWXO;
  424. break;
  425. case ACL_MASK:
  426. mask_obj = pa;
  427. not_equiv = 1;
  428. break;
  429. default:
  430. return -EIO;
  431. }
  432. }
  433. if (mask_obj) {
  434. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  435. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  436. } else {
  437. if (!group_obj)
  438. return -EIO;
  439. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  440. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  441. }
  442. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  443. return not_equiv;
  444. }
  445. /*
  446. * Modify the ACL for the chmod syscall.
  447. */
  448. static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
  449. {
  450. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  451. struct posix_acl_entry *pa, *pe;
  452. /* assert(atomic_read(acl->a_refcount) == 1); */
  453. FOREACH_ACL_ENTRY(pa, acl, pe) {
  454. switch(pa->e_tag) {
  455. case ACL_USER_OBJ:
  456. pa->e_perm = (mode & S_IRWXU) >> 6;
  457. break;
  458. case ACL_USER:
  459. case ACL_GROUP:
  460. break;
  461. case ACL_GROUP_OBJ:
  462. group_obj = pa;
  463. break;
  464. case ACL_MASK:
  465. mask_obj = pa;
  466. break;
  467. case ACL_OTHER:
  468. pa->e_perm = (mode & S_IRWXO);
  469. break;
  470. default:
  471. return -EIO;
  472. }
  473. }
  474. if (mask_obj) {
  475. mask_obj->e_perm = (mode & S_IRWXG) >> 3;
  476. } else {
  477. if (!group_obj)
  478. return -EIO;
  479. group_obj->e_perm = (mode & S_IRWXG) >> 3;
  480. }
  481. return 0;
  482. }
  483. int
  484. __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
  485. {
  486. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  487. int err = -ENOMEM;
  488. if (clone) {
  489. err = posix_acl_create_masq(clone, mode_p);
  490. if (err < 0) {
  491. posix_acl_release(clone);
  492. clone = NULL;
  493. }
  494. }
  495. posix_acl_release(*acl);
  496. *acl = clone;
  497. return err;
  498. }
  499. EXPORT_SYMBOL(__posix_acl_create);
  500. int
  501. __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
  502. {
  503. struct posix_acl *clone = posix_acl_clone(*acl, gfp);
  504. int err = -ENOMEM;
  505. if (clone) {
  506. err = __posix_acl_chmod_masq(clone, mode);
  507. if (err) {
  508. posix_acl_release(clone);
  509. clone = NULL;
  510. }
  511. }
  512. posix_acl_release(*acl);
  513. *acl = clone;
  514. return err;
  515. }
  516. EXPORT_SYMBOL(__posix_acl_chmod);
  517. /**
  518. * posix_acl_chmod - chmod a posix acl
  519. *
  520. * @idmap: idmap of the mount @inode was found from
  521. * @dentry: dentry to check permissions on
  522. * @mode: the new mode of @inode
  523. *
  524. * If the dentry has been found through an idmapped mount the idmap of
  525. * the vfsmount must be passed through @idmap. This function will then
  526. * take care to map the inode according to @idmap before checking
  527. * permissions. On non-idmapped mounts or if permission checking is to be
  528. * performed on the raw inode simply pass @nop_mnt_idmap.
  529. */
  530. int
  531. posix_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry,
  532. umode_t mode)
  533. {
  534. struct inode *inode = d_inode(dentry);
  535. struct posix_acl *acl;
  536. int ret = 0;
  537. if (!IS_POSIXACL(inode))
  538. return 0;
  539. if (!inode->i_op->set_acl)
  540. return -EOPNOTSUPP;
  541. acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
  542. if (IS_ERR_OR_NULL(acl)) {
  543. if (acl == ERR_PTR(-EOPNOTSUPP))
  544. return 0;
  545. return PTR_ERR(acl);
  546. }
  547. ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
  548. if (ret)
  549. return ret;
  550. ret = inode->i_op->set_acl(idmap, dentry, acl, ACL_TYPE_ACCESS);
  551. posix_acl_release(acl);
  552. return ret;
  553. }
  554. EXPORT_SYMBOL(posix_acl_chmod);
  555. int
  556. posix_acl_create(struct inode *dir, umode_t *mode,
  557. struct posix_acl **default_acl, struct posix_acl **acl)
  558. {
  559. struct posix_acl *p;
  560. struct posix_acl *clone;
  561. int ret;
  562. *acl = NULL;
  563. *default_acl = NULL;
  564. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  565. return 0;
  566. p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
  567. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  568. *mode &= ~current_umask();
  569. return 0;
  570. }
  571. if (IS_ERR(p))
  572. return PTR_ERR(p);
  573. ret = -ENOMEM;
  574. clone = posix_acl_clone(p, GFP_NOFS);
  575. if (!clone)
  576. goto err_release;
  577. ret = posix_acl_create_masq(clone, mode);
  578. if (ret < 0)
  579. goto err_release_clone;
  580. if (ret == 0)
  581. posix_acl_release(clone);
  582. else
  583. *acl = clone;
  584. if (!S_ISDIR(*mode))
  585. posix_acl_release(p);
  586. else
  587. *default_acl = p;
  588. return 0;
  589. err_release_clone:
  590. posix_acl_release(clone);
  591. err_release:
  592. posix_acl_release(p);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(posix_acl_create);
  596. /**
  597. * posix_acl_update_mode - update mode in set_acl
  598. * @idmap: idmap of the mount @inode was found from
  599. * @inode: target inode
  600. * @mode_p: mode (pointer) for update
  601. * @acl: acl pointer
  602. *
  603. * Update the file mode when setting an ACL: compute the new file permission
  604. * bits based on the ACL. In addition, if the ACL is equivalent to the new
  605. * file mode, set *@acl to NULL to indicate that no ACL should be set.
  606. *
  607. * As with chmod, clear the setgid bit if the caller is not in the owning group
  608. * or capable of CAP_FSETID (see inode_change_ok).
  609. *
  610. * If the inode has been found through an idmapped mount the idmap of
  611. * the vfsmount must be passed through @idmap. This function will then
  612. * take care to map the inode according to @idmap before checking
  613. * permissions. On non-idmapped mounts or if permission checking is to be
  614. * performed on the raw inode simply pass @nop_mnt_idmap.
  615. *
  616. * Called from set_acl inode operations.
  617. */
  618. int posix_acl_update_mode(struct mnt_idmap *idmap,
  619. struct inode *inode, umode_t *mode_p,
  620. struct posix_acl **acl)
  621. {
  622. umode_t mode = inode->i_mode;
  623. int error;
  624. error = posix_acl_equiv_mode(*acl, &mode);
  625. if (error < 0)
  626. return error;
  627. if (error == 0)
  628. *acl = NULL;
  629. if (!in_group_or_capable(idmap, inode,
  630. i_gid_into_vfsgid(idmap, inode)))
  631. mode &= ~S_ISGID;
  632. *mode_p = mode;
  633. return 0;
  634. }
  635. EXPORT_SYMBOL(posix_acl_update_mode);
  636. /*
  637. * Fix up the uids and gids in posix acl extended attributes in place.
  638. */
  639. static int posix_acl_fix_xattr_common(const void *value, size_t size)
  640. {
  641. const struct posix_acl_xattr_header *header = value;
  642. int count;
  643. if (!header)
  644. return -EINVAL;
  645. if (size < sizeof(struct posix_acl_xattr_header))
  646. return -EINVAL;
  647. if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
  648. return -EOPNOTSUPP;
  649. count = posix_acl_xattr_count(size);
  650. if (count < 0)
  651. return -EINVAL;
  652. if (count == 0)
  653. return 0;
  654. return count;
  655. }
  656. /**
  657. * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
  658. * @userns: the filesystem's idmapping
  659. * @value: the uapi representation of POSIX ACLs
  660. * @size: the size of @void
  661. *
  662. * Filesystems that store POSIX ACLs in the unaltered uapi format should use
  663. * posix_acl_from_xattr() when reading them from the backing store and
  664. * converting them into the struct posix_acl VFS format. The helper is
  665. * specifically intended to be called from the acl inode operation.
  666. *
  667. * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
  668. * in ACL_{GROUP,USER} entries into idmapping in @userns.
  669. *
  670. * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
  671. * If it did it calling it from the get acl inode operation would return POSIX
  672. * ACLs mapped according to an idmapped mount which would mean that the value
  673. * couldn't be cached for the filesystem. Idmapped mounts are taken into
  674. * account on the fly during permission checking or right at the VFS -
  675. * userspace boundary before reporting them to the user.
  676. *
  677. * Return: Allocated struct posix_acl on success, NULL for a valid header but
  678. * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
  679. */
  680. struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns,
  681. const void *value, size_t size)
  682. {
  683. const struct posix_acl_xattr_header *header = value;
  684. const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
  685. int count;
  686. struct posix_acl *acl;
  687. struct posix_acl_entry *acl_e;
  688. count = posix_acl_fix_xattr_common(value, size);
  689. if (count < 0)
  690. return ERR_PTR(count);
  691. if (count == 0)
  692. return NULL;
  693. acl = posix_acl_alloc(count, GFP_NOFS);
  694. if (!acl)
  695. return ERR_PTR(-ENOMEM);
  696. acl_e = acl->a_entries;
  697. for (end = entry + count; entry != end; acl_e++, entry++) {
  698. acl_e->e_tag = le16_to_cpu(entry->e_tag);
  699. acl_e->e_perm = le16_to_cpu(entry->e_perm);
  700. switch(acl_e->e_tag) {
  701. case ACL_USER_OBJ:
  702. case ACL_GROUP_OBJ:
  703. case ACL_MASK:
  704. case ACL_OTHER:
  705. break;
  706. case ACL_USER:
  707. acl_e->e_uid = make_kuid(userns,
  708. le32_to_cpu(entry->e_id));
  709. if (!uid_valid(acl_e->e_uid))
  710. goto fail;
  711. break;
  712. case ACL_GROUP:
  713. acl_e->e_gid = make_kgid(userns,
  714. le32_to_cpu(entry->e_id));
  715. if (!gid_valid(acl_e->e_gid))
  716. goto fail;
  717. break;
  718. default:
  719. goto fail;
  720. }
  721. }
  722. return acl;
  723. fail:
  724. posix_acl_release(acl);
  725. return ERR_PTR(-EINVAL);
  726. }
  727. EXPORT_SYMBOL (posix_acl_from_xattr);
  728. /*
  729. * Convert from in-memory to extended attribute representation.
  730. */
  731. void *
  732. posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
  733. size_t *sizep, gfp_t gfp)
  734. {
  735. struct posix_acl_xattr_header *ext_acl;
  736. struct posix_acl_xattr_entry *ext_entry;
  737. size_t size;
  738. int n;
  739. size = posix_acl_xattr_size(acl->a_count);
  740. ext_acl = kmalloc(size, gfp);
  741. if (!ext_acl)
  742. return NULL;
  743. ext_entry = (void *)(ext_acl + 1);
  744. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  745. for (n=0; n < acl->a_count; n++, ext_entry++) {
  746. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  747. ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
  748. ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  749. switch(acl_e->e_tag) {
  750. case ACL_USER:
  751. ext_entry->e_id =
  752. cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
  753. break;
  754. case ACL_GROUP:
  755. ext_entry->e_id =
  756. cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
  757. break;
  758. default:
  759. ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  760. break;
  761. }
  762. }
  763. *sizep = size;
  764. return ext_acl;
  765. }
  766. EXPORT_SYMBOL (posix_acl_to_xattr);
  767. /**
  768. * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
  769. * @idmap: idmap of the mount
  770. * @inode: inode the posix acls are set on
  771. * @acl: the posix acls as represented by the vfs
  772. * @buffer: the buffer into which to convert @acl
  773. * @size: size of @buffer
  774. *
  775. * This converts @acl from the VFS representation in the filesystem idmapping
  776. * to the uapi form reportable to userspace. And mount and caller idmappings
  777. * are handled appropriately.
  778. *
  779. * Return: On success, the size of the stored uapi posix acls, on error a
  780. * negative errno.
  781. */
  782. static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap,
  783. struct inode *inode,
  784. const struct posix_acl *acl, void *buffer,
  785. size_t size)
  786. {
  787. struct posix_acl_xattr_header *ext_acl = buffer;
  788. struct posix_acl_xattr_entry *ext_entry;
  789. struct user_namespace *fs_userns, *caller_userns;
  790. ssize_t real_size, n;
  791. vfsuid_t vfsuid;
  792. vfsgid_t vfsgid;
  793. real_size = posix_acl_xattr_size(acl->a_count);
  794. if (!buffer)
  795. return real_size;
  796. if (real_size > size)
  797. return -ERANGE;
  798. ext_entry = (void *)(ext_acl + 1);
  799. ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
  800. fs_userns = i_user_ns(inode);
  801. caller_userns = current_user_ns();
  802. for (n=0; n < acl->a_count; n++, ext_entry++) {
  803. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  804. ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
  805. ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
  806. switch(acl_e->e_tag) {
  807. case ACL_USER:
  808. vfsuid = make_vfsuid(idmap, fs_userns, acl_e->e_uid);
  809. ext_entry->e_id = cpu_to_le32(from_kuid(
  810. caller_userns, vfsuid_into_kuid(vfsuid)));
  811. break;
  812. case ACL_GROUP:
  813. vfsgid = make_vfsgid(idmap, fs_userns, acl_e->e_gid);
  814. ext_entry->e_id = cpu_to_le32(from_kgid(
  815. caller_userns, vfsgid_into_kgid(vfsgid)));
  816. break;
  817. default:
  818. ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  819. break;
  820. }
  821. }
  822. return real_size;
  823. }
  824. int
  825. set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  826. int type, struct posix_acl *acl)
  827. {
  828. struct inode *inode = d_inode(dentry);
  829. if (!IS_POSIXACL(inode))
  830. return -EOPNOTSUPP;
  831. if (!inode->i_op->set_acl)
  832. return -EOPNOTSUPP;
  833. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  834. return acl ? -EACCES : 0;
  835. if (!inode_owner_or_capable(idmap, inode))
  836. return -EPERM;
  837. if (acl) {
  838. int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  839. if (ret)
  840. return ret;
  841. }
  842. return inode->i_op->set_acl(idmap, dentry, acl, type);
  843. }
  844. EXPORT_SYMBOL(set_posix_acl);
  845. int posix_acl_listxattr(struct inode *inode, char **buffer,
  846. ssize_t *remaining_size)
  847. {
  848. int err;
  849. if (!IS_POSIXACL(inode))
  850. return 0;
  851. if (inode->i_acl) {
  852. err = xattr_list_one(buffer, remaining_size,
  853. XATTR_NAME_POSIX_ACL_ACCESS);
  854. if (err)
  855. return err;
  856. }
  857. if (inode->i_default_acl) {
  858. err = xattr_list_one(buffer, remaining_size,
  859. XATTR_NAME_POSIX_ACL_DEFAULT);
  860. if (err)
  861. return err;
  862. }
  863. return 0;
  864. }
  865. static bool
  866. posix_acl_xattr_list(struct dentry *dentry)
  867. {
  868. return IS_POSIXACL(d_backing_inode(dentry));
  869. }
  870. /*
  871. * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
  872. *
  873. * This is the legacy POSIX ACL access xattr handler. It is used by some
  874. * filesystems to implement their ->listxattr() inode operation. New code
  875. * should never use them.
  876. */
  877. const struct xattr_handler nop_posix_acl_access = {
  878. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  879. .list = posix_acl_xattr_list,
  880. };
  881. EXPORT_SYMBOL_GPL(nop_posix_acl_access);
  882. /*
  883. * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
  884. *
  885. * This is the legacy POSIX ACL default xattr handler. It is used by some
  886. * filesystems to implement their ->listxattr() inode operation. New code
  887. * should never use them.
  888. */
  889. const struct xattr_handler nop_posix_acl_default = {
  890. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  891. .list = posix_acl_xattr_list,
  892. };
  893. EXPORT_SYMBOL_GPL(nop_posix_acl_default);
  894. int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  895. struct posix_acl *acl, int type)
  896. {
  897. int error;
  898. struct inode *inode = d_inode(dentry);
  899. if (type == ACL_TYPE_ACCESS) {
  900. error = posix_acl_update_mode(idmap, inode,
  901. &inode->i_mode, &acl);
  902. if (error)
  903. return error;
  904. }
  905. inode_set_ctime_current(inode);
  906. if (IS_I_VERSION(inode))
  907. inode_inc_iversion(inode);
  908. set_cached_acl(inode, type, acl);
  909. return 0;
  910. }
  911. int simple_acl_create(struct inode *dir, struct inode *inode)
  912. {
  913. struct posix_acl *default_acl, *acl;
  914. int error;
  915. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  916. if (error)
  917. return error;
  918. set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
  919. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  920. if (default_acl)
  921. posix_acl_release(default_acl);
  922. if (acl)
  923. posix_acl_release(acl);
  924. return 0;
  925. }
  926. static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap,
  927. struct user_namespace *fs_userns,
  928. struct posix_acl *acl)
  929. {
  930. for (int n = 0; n < acl->a_count; n++) {
  931. struct posix_acl_entry *acl_e = &acl->a_entries[n];
  932. switch (acl_e->e_tag) {
  933. case ACL_USER:
  934. acl_e->e_uid = from_vfsuid(idmap, fs_userns,
  935. VFSUIDT_INIT(acl_e->e_uid));
  936. break;
  937. case ACL_GROUP:
  938. acl_e->e_gid = from_vfsgid(idmap, fs_userns,
  939. VFSGIDT_INIT(acl_e->e_gid));
  940. break;
  941. }
  942. }
  943. return 0;
  944. }
  945. /**
  946. * vfs_set_acl - set posix acls
  947. * @idmap: idmap of the mount
  948. * @dentry: the dentry based on which to set the posix acls
  949. * @acl_name: the name of the posix acl
  950. * @kacl: the posix acls in the appropriate VFS format
  951. *
  952. * This function sets @kacl. The caller must all posix_acl_release() on @kacl
  953. * afterwards.
  954. *
  955. * Return: On success 0, on error negative errno.
  956. */
  957. int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  958. const char *acl_name, struct posix_acl *kacl)
  959. {
  960. int acl_type;
  961. int error;
  962. struct inode *inode = d_inode(dentry);
  963. struct delegated_inode delegated_inode = { };
  964. acl_type = posix_acl_type(acl_name);
  965. if (acl_type < 0)
  966. return -EINVAL;
  967. if (kacl) {
  968. /*
  969. * If we're on an idmapped mount translate from mount specific
  970. * vfs{g,u}id_t into global filesystem k{g,u}id_t.
  971. * Afterwards we can cache the POSIX ACLs filesystem wide and -
  972. * if this is a filesystem with a backing store - ultimately
  973. * translate them to backing store values.
  974. */
  975. error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl);
  976. if (error)
  977. return error;
  978. }
  979. retry_deleg:
  980. inode_lock(inode);
  981. /*
  982. * We only care about restrictions the inode struct itself places upon
  983. * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
  984. */
  985. error = may_write_xattr(idmap, inode);
  986. if (error)
  987. goto out_inode_unlock;
  988. error = security_inode_set_acl(idmap, dentry, acl_name, kacl);
  989. if (error)
  990. goto out_inode_unlock;
  991. error = try_break_deleg(inode, &delegated_inode);
  992. if (error)
  993. goto out_inode_unlock;
  994. if (likely(!is_bad_inode(inode)))
  995. error = set_posix_acl(idmap, dentry, acl_type, kacl);
  996. else
  997. error = -EIO;
  998. if (!error) {
  999. fsnotify_xattr(dentry);
  1000. security_inode_post_set_acl(dentry, acl_name, kacl);
  1001. }
  1002. out_inode_unlock:
  1003. inode_unlock(inode);
  1004. if (is_delegated(&delegated_inode)) {
  1005. error = break_deleg_wait(&delegated_inode);
  1006. if (!error)
  1007. goto retry_deleg;
  1008. }
  1009. return error;
  1010. }
  1011. EXPORT_SYMBOL_GPL(vfs_set_acl);
  1012. /**
  1013. * vfs_get_acl - get posix acls
  1014. * @idmap: idmap of the mount
  1015. * @dentry: the dentry based on which to retrieve the posix acls
  1016. * @acl_name: the name of the posix acl
  1017. *
  1018. * This function retrieves @kacl from the filesystem. The caller must all
  1019. * posix_acl_release() on @kacl.
  1020. *
  1021. * Return: On success POSIX ACLs in VFS format, on error negative errno.
  1022. */
  1023. struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap,
  1024. struct dentry *dentry, const char *acl_name)
  1025. {
  1026. struct inode *inode = d_inode(dentry);
  1027. struct posix_acl *acl;
  1028. int acl_type, error;
  1029. acl_type = posix_acl_type(acl_name);
  1030. if (acl_type < 0)
  1031. return ERR_PTR(-EINVAL);
  1032. /*
  1033. * The VFS has no restrictions on reading POSIX ACLs so calling
  1034. * something like xattr_permission() isn't needed. Only LSMs get a say.
  1035. */
  1036. error = security_inode_get_acl(idmap, dentry, acl_name);
  1037. if (error)
  1038. return ERR_PTR(error);
  1039. if (!IS_POSIXACL(inode))
  1040. return ERR_PTR(-EOPNOTSUPP);
  1041. if (S_ISLNK(inode->i_mode))
  1042. return ERR_PTR(-EOPNOTSUPP);
  1043. acl = __get_acl(idmap, dentry, inode, acl_type);
  1044. if (IS_ERR(acl))
  1045. return acl;
  1046. if (!acl)
  1047. return ERR_PTR(-ENODATA);
  1048. return acl;
  1049. }
  1050. EXPORT_SYMBOL_GPL(vfs_get_acl);
  1051. /**
  1052. * vfs_remove_acl - remove posix acls
  1053. * @idmap: idmap of the mount
  1054. * @dentry: the dentry based on which to retrieve the posix acls
  1055. * @acl_name: the name of the posix acl
  1056. *
  1057. * This function removes posix acls.
  1058. *
  1059. * Return: On success 0, on error negative errno.
  1060. */
  1061. int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  1062. const char *acl_name)
  1063. {
  1064. int acl_type;
  1065. int error;
  1066. struct inode *inode = d_inode(dentry);
  1067. struct delegated_inode delegated_inode = { };
  1068. acl_type = posix_acl_type(acl_name);
  1069. if (acl_type < 0)
  1070. return -EINVAL;
  1071. retry_deleg:
  1072. inode_lock(inode);
  1073. /*
  1074. * We only care about restrictions the inode struct itself places upon
  1075. * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
  1076. */
  1077. error = may_write_xattr(idmap, inode);
  1078. if (error)
  1079. goto out_inode_unlock;
  1080. error = security_inode_remove_acl(idmap, dentry, acl_name);
  1081. if (error)
  1082. goto out_inode_unlock;
  1083. error = try_break_deleg(inode, &delegated_inode);
  1084. if (error)
  1085. goto out_inode_unlock;
  1086. if (likely(!is_bad_inode(inode)))
  1087. error = set_posix_acl(idmap, dentry, acl_type, NULL);
  1088. else
  1089. error = -EIO;
  1090. if (!error) {
  1091. fsnotify_xattr(dentry);
  1092. security_inode_post_remove_acl(idmap, dentry, acl_name);
  1093. }
  1094. out_inode_unlock:
  1095. inode_unlock(inode);
  1096. if (is_delegated(&delegated_inode)) {
  1097. error = break_deleg_wait(&delegated_inode);
  1098. if (!error)
  1099. goto retry_deleg;
  1100. }
  1101. return error;
  1102. }
  1103. EXPORT_SYMBOL_GPL(vfs_remove_acl);
  1104. int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  1105. const char *acl_name, const void *kvalue, size_t size)
  1106. {
  1107. int error;
  1108. struct posix_acl *acl = NULL;
  1109. if (size) {
  1110. /*
  1111. * Note that posix_acl_from_xattr() uses GFP_NOFS when it
  1112. * probably doesn't need to here.
  1113. */
  1114. acl = posix_acl_from_xattr(current_user_ns(), kvalue, size);
  1115. if (IS_ERR(acl))
  1116. return PTR_ERR(acl);
  1117. }
  1118. error = vfs_set_acl(idmap, dentry, acl_name, acl);
  1119. posix_acl_release(acl);
  1120. return error;
  1121. }
  1122. ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  1123. const char *acl_name, void *kvalue, size_t size)
  1124. {
  1125. ssize_t error;
  1126. struct posix_acl *acl;
  1127. acl = vfs_get_acl(idmap, dentry, acl_name);
  1128. if (IS_ERR(acl))
  1129. return PTR_ERR(acl);
  1130. error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry),
  1131. acl, kvalue, size);
  1132. posix_acl_release(acl);
  1133. return error;
  1134. }