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