file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor mediation of files
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/tty.h>
  11. #include <linux/fdtable.h>
  12. #include <linux/file.h>
  13. #include "include/apparmor.h"
  14. #include "include/audit.h"
  15. #include "include/cred.h"
  16. #include "include/file.h"
  17. #include "include/match.h"
  18. #include "include/net.h"
  19. #include "include/path.h"
  20. #include "include/policy.h"
  21. #include "include/label.h"
  22. static u32 map_mask_to_chr_mask(u32 mask)
  23. {
  24. u32 m = mask & PERMS_CHRS_MASK;
  25. if (mask & AA_MAY_GETATTR)
  26. m |= MAY_READ;
  27. if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
  28. m |= MAY_WRITE;
  29. return m;
  30. }
  31. /**
  32. * file_audit_cb - call back for file specific audit fields
  33. * @ab: audit_buffer (NOT NULL)
  34. * @va: audit struct to audit values of (NOT NULL)
  35. */
  36. static void file_audit_cb(struct audit_buffer *ab, void *va)
  37. {
  38. struct common_audit_data *sa = va;
  39. kuid_t fsuid = current_fsuid();
  40. char str[10];
  41. if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
  42. aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
  43. map_mask_to_chr_mask(aad(sa)->request));
  44. audit_log_format(ab, " requested_mask=\"%s\"", str);
  45. }
  46. if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
  47. aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
  48. map_mask_to_chr_mask(aad(sa)->denied));
  49. audit_log_format(ab, " denied_mask=\"%s\"", str);
  50. }
  51. if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
  52. audit_log_format(ab, " fsuid=%d",
  53. from_kuid(&init_user_ns, fsuid));
  54. audit_log_format(ab, " ouid=%d",
  55. from_kuid(&init_user_ns, aad(sa)->fs.ouid));
  56. }
  57. if (aad(sa)->peer) {
  58. audit_log_format(ab, " target=");
  59. aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer,
  60. FLAG_VIEW_SUBNS, GFP_KERNEL);
  61. } else if (aad(sa)->fs.target) {
  62. audit_log_format(ab, " target=");
  63. audit_log_untrustedstring(ab, aad(sa)->fs.target);
  64. }
  65. }
  66. /**
  67. * aa_audit_file - handle the auditing of file operations
  68. * @profile: the profile being enforced (NOT NULL)
  69. * @perms: the permissions computed for the request (NOT NULL)
  70. * @op: operation being mediated
  71. * @request: permissions requested
  72. * @name: name of object being mediated (MAYBE NULL)
  73. * @target: name of target (MAYBE NULL)
  74. * @tlabel: target label (MAY BE NULL)
  75. * @ouid: object uid
  76. * @info: extra information message (MAYBE NULL)
  77. * @error: 0 if operation allowed else failure error code
  78. *
  79. * Returns: %0 or error on failure
  80. */
  81. int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
  82. const char *op, u32 request, const char *name,
  83. const char *target, struct aa_label *tlabel,
  84. kuid_t ouid, const char *info, int error)
  85. {
  86. int type = AUDIT_APPARMOR_AUTO;
  87. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
  88. sa.u.tsk = NULL;
  89. aad(&sa)->request = request;
  90. aad(&sa)->name = name;
  91. aad(&sa)->fs.target = target;
  92. aad(&sa)->peer = tlabel;
  93. aad(&sa)->fs.ouid = ouid;
  94. aad(&sa)->info = info;
  95. aad(&sa)->error = error;
  96. sa.u.tsk = NULL;
  97. if (likely(!aad(&sa)->error)) {
  98. u32 mask = perms->audit;
  99. if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
  100. mask = 0xffff;
  101. /* mask off perms that are not being force audited */
  102. aad(&sa)->request &= mask;
  103. if (likely(!aad(&sa)->request))
  104. return 0;
  105. type = AUDIT_APPARMOR_AUDIT;
  106. } else {
  107. /* only report permissions that were denied */
  108. aad(&sa)->request = aad(&sa)->request & ~perms->allow;
  109. AA_BUG(!aad(&sa)->request);
  110. if (aad(&sa)->request & perms->kill)
  111. type = AUDIT_APPARMOR_KILL;
  112. /* quiet known rejects, assumes quiet and kill do not overlap */
  113. if ((aad(&sa)->request & perms->quiet) &&
  114. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  115. AUDIT_MODE(profile) != AUDIT_ALL)
  116. aad(&sa)->request &= ~perms->quiet;
  117. if (!aad(&sa)->request)
  118. return aad(&sa)->error;
  119. }
  120. aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
  121. return aa_audit(type, profile, &sa, file_audit_cb);
  122. }
  123. /**
  124. * is_deleted - test if a file has been completely unlinked
  125. * @dentry: dentry of file to test for deletion (NOT NULL)
  126. *
  127. * Returns: true if deleted else false
  128. */
  129. static inline bool is_deleted(struct dentry *dentry)
  130. {
  131. if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
  132. return true;
  133. return false;
  134. }
  135. static int path_name(const char *op, struct aa_label *label,
  136. const struct path *path, int flags, char *buffer,
  137. const char **name, struct path_cond *cond, u32 request)
  138. {
  139. struct aa_profile *profile;
  140. const char *info = NULL;
  141. int error;
  142. error = aa_path_name(path, flags, buffer, name, &info,
  143. labels_profile(label)->disconnected);
  144. if (error) {
  145. fn_for_each_confined(label, profile,
  146. aa_audit_file(profile, &nullperms, op, request, *name,
  147. NULL, NULL, cond->uid, info, error));
  148. return error;
  149. }
  150. return 0;
  151. }
  152. /**
  153. * map_old_perms - map old file perms layout to the new layout
  154. * @old: permission set in old mapping
  155. *
  156. * Returns: new permission mapping
  157. */
  158. static u32 map_old_perms(u32 old)
  159. {
  160. u32 new = old & 0xf;
  161. if (old & MAY_READ)
  162. new |= AA_MAY_GETATTR | AA_MAY_OPEN;
  163. if (old & MAY_WRITE)
  164. new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
  165. AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
  166. if (old & 0x10)
  167. new |= AA_MAY_LINK;
  168. /* the old mapping lock and link_subset flags where overlaid
  169. * and use was determined by part of a pair that they were in
  170. */
  171. if (old & 0x20)
  172. new |= AA_MAY_LOCK | AA_LINK_SUBSET;
  173. if (old & 0x40) /* AA_EXEC_MMAP */
  174. new |= AA_EXEC_MMAP;
  175. return new;
  176. }
  177. /**
  178. * aa_compute_fperms - convert dfa compressed perms to internal perms
  179. * @dfa: dfa to compute perms for (NOT NULL)
  180. * @state: state in dfa
  181. * @cond: conditions to consider (NOT NULL)
  182. *
  183. * TODO: convert from dfa + state to permission entry, do computation conversion
  184. * at load time.
  185. *
  186. * Returns: computed permission set
  187. */
  188. struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
  189. struct path_cond *cond)
  190. {
  191. /* FIXME: change over to new dfa format
  192. * currently file perms are encoded in the dfa, new format
  193. * splits the permissions from the dfa. This mapping can be
  194. * done at profile load
  195. */
  196. struct aa_perms perms = { };
  197. if (uid_eq(current_fsuid(), cond->uid)) {
  198. perms.allow = map_old_perms(dfa_user_allow(dfa, state));
  199. perms.audit = map_old_perms(dfa_user_audit(dfa, state));
  200. perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
  201. perms.xindex = dfa_user_xindex(dfa, state);
  202. } else {
  203. perms.allow = map_old_perms(dfa_other_allow(dfa, state));
  204. perms.audit = map_old_perms(dfa_other_audit(dfa, state));
  205. perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
  206. perms.xindex = dfa_other_xindex(dfa, state);
  207. }
  208. perms.allow |= AA_MAY_GETATTR;
  209. /* change_profile wasn't determined by ownership in old mapping */
  210. if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
  211. perms.allow |= AA_MAY_CHANGE_PROFILE;
  212. if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
  213. perms.allow |= AA_MAY_ONEXEC;
  214. return perms;
  215. }
  216. /**
  217. * aa_str_perms - find permission that match @name
  218. * @dfa: to match against (MAYBE NULL)
  219. * @state: state to start matching in
  220. * @name: string to match against dfa (NOT NULL)
  221. * @cond: conditions to consider for permission set computation (NOT NULL)
  222. * @perms: Returns - the permissions found when matching @name
  223. *
  224. * Returns: the final state in @dfa when beginning @start and walking @name
  225. */
  226. unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
  227. const char *name, struct path_cond *cond,
  228. struct aa_perms *perms)
  229. {
  230. unsigned int state;
  231. state = aa_dfa_match(dfa, start, name);
  232. *perms = aa_compute_fperms(dfa, state, cond);
  233. return state;
  234. }
  235. int __aa_path_perm(const char *op, struct aa_profile *profile, const char *name,
  236. u32 request, struct path_cond *cond, int flags,
  237. struct aa_perms *perms)
  238. {
  239. int e = 0;
  240. if (profile_unconfined(profile))
  241. return 0;
  242. aa_str_perms(profile->file.dfa, profile->file.start, name, cond, perms);
  243. if (request & ~perms->allow)
  244. e = -EACCES;
  245. return aa_audit_file(profile, perms, op, request, name, NULL, NULL,
  246. cond->uid, NULL, e);
  247. }
  248. static int profile_path_perm(const char *op, struct aa_profile *profile,
  249. const struct path *path, char *buffer, u32 request,
  250. struct path_cond *cond, int flags,
  251. struct aa_perms *perms)
  252. {
  253. const char *name;
  254. int error;
  255. if (profile_unconfined(profile))
  256. return 0;
  257. error = path_name(op, &profile->label, path,
  258. flags | profile->path_flags, buffer, &name, cond,
  259. request);
  260. if (error)
  261. return error;
  262. return __aa_path_perm(op, profile, name, request, cond, flags,
  263. perms);
  264. }
  265. /**
  266. * aa_path_perm - do permissions check & audit for @path
  267. * @op: operation being checked
  268. * @label: profile being enforced (NOT NULL)
  269. * @path: path to check permissions of (NOT NULL)
  270. * @flags: any additional path flags beyond what the profile specifies
  271. * @request: requested permissions
  272. * @cond: conditional info for this request (NOT NULL)
  273. *
  274. * Returns: %0 else error if access denied or other error
  275. */
  276. int aa_path_perm(const char *op, struct aa_label *label,
  277. const struct path *path, int flags, u32 request,
  278. struct path_cond *cond)
  279. {
  280. struct aa_perms perms = {};
  281. struct aa_profile *profile;
  282. char *buffer = NULL;
  283. int error;
  284. flags |= PATH_DELEGATE_DELETED | (S_ISDIR(cond->mode) ? PATH_IS_DIR :
  285. 0);
  286. buffer = aa_get_buffer(false);
  287. if (!buffer)
  288. return -ENOMEM;
  289. error = fn_for_each_confined(label, profile,
  290. profile_path_perm(op, profile, path, buffer, request,
  291. cond, flags, &perms));
  292. aa_put_buffer(buffer);
  293. return error;
  294. }
  295. /**
  296. * xindex_is_subset - helper for aa_path_link
  297. * @link: link permission set
  298. * @target: target permission set
  299. *
  300. * test target x permissions are equal OR a subset of link x permissions
  301. * this is done as part of the subset test, where a hardlink must have
  302. * a subset of permissions that the target has.
  303. *
  304. * Returns: true if subset else false
  305. */
  306. static inline bool xindex_is_subset(u32 link, u32 target)
  307. {
  308. if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
  309. ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
  310. return false;
  311. return true;
  312. }
  313. static int profile_path_link(struct aa_profile *profile,
  314. const struct path *link, char *buffer,
  315. const struct path *target, char *buffer2,
  316. struct path_cond *cond)
  317. {
  318. const char *lname, *tname = NULL;
  319. struct aa_perms lperms = {}, perms;
  320. const char *info = NULL;
  321. u32 request = AA_MAY_LINK;
  322. unsigned int state;
  323. int error;
  324. error = path_name(OP_LINK, &profile->label, link, profile->path_flags,
  325. buffer, &lname, cond, AA_MAY_LINK);
  326. if (error)
  327. goto audit;
  328. /* buffer2 freed below, tname is pointer in buffer2 */
  329. error = path_name(OP_LINK, &profile->label, target, profile->path_flags,
  330. buffer2, &tname, cond, AA_MAY_LINK);
  331. if (error)
  332. goto audit;
  333. error = -EACCES;
  334. /* aa_str_perms - handles the case of the dfa being NULL */
  335. state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
  336. cond, &lperms);
  337. if (!(lperms.allow & AA_MAY_LINK))
  338. goto audit;
  339. /* test to see if target can be paired with link */
  340. state = aa_dfa_null_transition(profile->file.dfa, state);
  341. aa_str_perms(profile->file.dfa, state, tname, cond, &perms);
  342. /* force audit/quiet masks for link are stored in the second entry
  343. * in the link pair.
  344. */
  345. lperms.audit = perms.audit;
  346. lperms.quiet = perms.quiet;
  347. lperms.kill = perms.kill;
  348. if (!(perms.allow & AA_MAY_LINK)) {
  349. info = "target restricted";
  350. lperms = perms;
  351. goto audit;
  352. }
  353. /* done if link subset test is not required */
  354. if (!(perms.allow & AA_LINK_SUBSET))
  355. goto done_tests;
  356. /* Do link perm subset test requiring allowed permission on link are
  357. * a subset of the allowed permissions on target.
  358. */
  359. aa_str_perms(profile->file.dfa, profile->file.start, tname, cond,
  360. &perms);
  361. /* AA_MAY_LINK is not considered in the subset test */
  362. request = lperms.allow & ~AA_MAY_LINK;
  363. lperms.allow &= perms.allow | AA_MAY_LINK;
  364. request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
  365. if (request & ~lperms.allow) {
  366. goto audit;
  367. } else if ((lperms.allow & MAY_EXEC) &&
  368. !xindex_is_subset(lperms.xindex, perms.xindex)) {
  369. lperms.allow &= ~MAY_EXEC;
  370. request |= MAY_EXEC;
  371. info = "link not subset of target";
  372. goto audit;
  373. }
  374. done_tests:
  375. error = 0;
  376. audit:
  377. return aa_audit_file(profile, &lperms, OP_LINK, request, lname, tname,
  378. NULL, cond->uid, info, error);
  379. }
  380. /**
  381. * aa_path_link - Handle hard link permission check
  382. * @label: the label being enforced (NOT NULL)
  383. * @old_dentry: the target dentry (NOT NULL)
  384. * @new_dir: directory the new link will be created in (NOT NULL)
  385. * @new_dentry: the link being created (NOT NULL)
  386. *
  387. * Handle the permission test for a link & target pair. Permission
  388. * is encoded as a pair where the link permission is determined
  389. * first, and if allowed, the target is tested. The target test
  390. * is done from the point of the link match (not start of DFA)
  391. * making the target permission dependent on the link permission match.
  392. *
  393. * The subset test if required forces that permissions granted
  394. * on link are a subset of the permission granted to target.
  395. *
  396. * Returns: %0 if allowed else error
  397. */
  398. int aa_path_link(struct aa_label *label, struct dentry *old_dentry,
  399. const struct path *new_dir, struct dentry *new_dentry)
  400. {
  401. struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
  402. struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
  403. struct path_cond cond = {
  404. d_backing_inode(old_dentry)->i_uid,
  405. d_backing_inode(old_dentry)->i_mode
  406. };
  407. char *buffer = NULL, *buffer2 = NULL;
  408. struct aa_profile *profile;
  409. int error;
  410. /* buffer freed below, lname is pointer in buffer */
  411. buffer = aa_get_buffer(false);
  412. buffer2 = aa_get_buffer(false);
  413. error = -ENOMEM;
  414. if (!buffer || !buffer2)
  415. goto out;
  416. error = fn_for_each_confined(label, profile,
  417. profile_path_link(profile, &link, buffer, &target,
  418. buffer2, &cond));
  419. out:
  420. aa_put_buffer(buffer);
  421. aa_put_buffer(buffer2);
  422. return error;
  423. }
  424. static void update_file_ctx(struct aa_file_ctx *fctx, struct aa_label *label,
  425. u32 request)
  426. {
  427. struct aa_label *l, *old;
  428. /* update caching of label on file_ctx */
  429. spin_lock(&fctx->lock);
  430. old = rcu_dereference_protected(fctx->label,
  431. lockdep_is_held(&fctx->lock));
  432. l = aa_label_merge(old, label, GFP_ATOMIC);
  433. if (l) {
  434. if (l != old) {
  435. rcu_assign_pointer(fctx->label, l);
  436. aa_put_label(old);
  437. } else
  438. aa_put_label(l);
  439. fctx->allow |= request;
  440. }
  441. spin_unlock(&fctx->lock);
  442. }
  443. static int __file_path_perm(const char *op, struct aa_label *label,
  444. struct aa_label *flabel, struct file *file,
  445. u32 request, u32 denied, bool in_atomic)
  446. {
  447. struct aa_profile *profile;
  448. struct aa_perms perms = {};
  449. struct path_cond cond = {
  450. .uid = file_inode(file)->i_uid,
  451. .mode = file_inode(file)->i_mode
  452. };
  453. char *buffer;
  454. int flags, error;
  455. /* revalidation due to label out of date. No revocation at this time */
  456. if (!denied && aa_label_is_subset(flabel, label))
  457. /* TODO: check for revocation on stale profiles */
  458. return 0;
  459. flags = PATH_DELEGATE_DELETED | (S_ISDIR(cond.mode) ? PATH_IS_DIR : 0);
  460. buffer = aa_get_buffer(in_atomic);
  461. if (!buffer)
  462. return -ENOMEM;
  463. /* check every profile in task label not in current cache */
  464. error = fn_for_each_not_in_set(flabel, label, profile,
  465. profile_path_perm(op, profile, &file->f_path, buffer,
  466. request, &cond, flags, &perms));
  467. if (denied && !error) {
  468. /*
  469. * check every profile in file label that was not tested
  470. * in the initial check above.
  471. *
  472. * TODO: cache full perms so this only happens because of
  473. * conditionals
  474. * TODO: don't audit here
  475. */
  476. if (label == flabel)
  477. error = fn_for_each(label, profile,
  478. profile_path_perm(op, profile, &file->f_path,
  479. buffer, request, &cond, flags,
  480. &perms));
  481. else
  482. error = fn_for_each_not_in_set(label, flabel, profile,
  483. profile_path_perm(op, profile, &file->f_path,
  484. buffer, request, &cond, flags,
  485. &perms));
  486. }
  487. if (!error)
  488. update_file_ctx(file_ctx(file), label, request);
  489. aa_put_buffer(buffer);
  490. return error;
  491. }
  492. static int __file_sock_perm(const char *op, struct aa_label *label,
  493. struct aa_label *flabel, struct file *file,
  494. u32 request, u32 denied)
  495. {
  496. struct socket *sock = (struct socket *) file->private_data;
  497. int error;
  498. AA_BUG(!sock);
  499. /* revalidation due to label out of date. No revocation at this time */
  500. if (!denied && aa_label_is_subset(flabel, label))
  501. return 0;
  502. /* TODO: improve to skip profiles cached in flabel */
  503. error = aa_sock_file_perm(label, op, request, sock);
  504. if (denied) {
  505. /* TODO: improve to skip profiles checked above */
  506. /* check every profile in file label to is cached */
  507. last_error(error, aa_sock_file_perm(flabel, op, request, sock));
  508. }
  509. if (!error)
  510. update_file_ctx(file_ctx(file), label, request);
  511. return error;
  512. }
  513. /**
  514. * aa_file_perm - do permission revalidation check & audit for @file
  515. * @op: operation being checked
  516. * @label: label being enforced (NOT NULL)
  517. * @file: file to revalidate access permissions on (NOT NULL)
  518. * @request: requested permissions
  519. * @in_atomic: whether allocations need to be done in atomic context
  520. *
  521. * Returns: %0 if access allowed else error
  522. */
  523. int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
  524. u32 request, bool in_atomic)
  525. {
  526. struct aa_file_ctx *fctx;
  527. struct aa_label *flabel;
  528. u32 denied;
  529. int error = 0;
  530. AA_BUG(!label);
  531. AA_BUG(!file);
  532. fctx = file_ctx(file);
  533. rcu_read_lock();
  534. flabel = rcu_dereference(fctx->label);
  535. AA_BUG(!flabel);
  536. /* revalidate access, if task is unconfined, or the cached cred
  537. * doesn't match or if the request is for more permissions than
  538. * was granted.
  539. *
  540. * Note: the test for !unconfined(flabel) is to handle file
  541. * delegation from unconfined tasks
  542. */
  543. denied = request & ~fctx->allow;
  544. if (unconfined(label) || unconfined(flabel) ||
  545. (!denied && aa_label_is_subset(flabel, label))) {
  546. rcu_read_unlock();
  547. goto done;
  548. }
  549. flabel = aa_get_newest_label(flabel);
  550. rcu_read_unlock();
  551. /* TODO: label cross check */
  552. if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
  553. error = __file_path_perm(op, label, flabel, file, request,
  554. denied, in_atomic);
  555. else if (S_ISSOCK(file_inode(file)->i_mode))
  556. error = __file_sock_perm(op, label, flabel, file, request,
  557. denied);
  558. aa_put_label(flabel);
  559. done:
  560. return error;
  561. }
  562. static void revalidate_tty(struct aa_label *label)
  563. {
  564. struct tty_struct *tty;
  565. int drop_tty = 0;
  566. tty = get_current_tty();
  567. if (!tty)
  568. return;
  569. spin_lock(&tty->files_lock);
  570. if (!list_empty(&tty->tty_files)) {
  571. struct tty_file_private *file_priv;
  572. struct file *file;
  573. /* TODO: Revalidate access to controlling tty. */
  574. file_priv = list_first_entry(&tty->tty_files,
  575. struct tty_file_private, list);
  576. file = file_priv->file;
  577. if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE,
  578. IN_ATOMIC))
  579. drop_tty = 1;
  580. }
  581. spin_unlock(&tty->files_lock);
  582. tty_kref_put(tty);
  583. if (drop_tty)
  584. no_tty();
  585. }
  586. static int match_file(const void *p, struct file *file, unsigned int fd)
  587. {
  588. struct aa_label *label = (struct aa_label *)p;
  589. if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file),
  590. IN_ATOMIC))
  591. return fd + 1;
  592. return 0;
  593. }
  594. /* based on selinux's flush_unauthorized_files */
  595. void aa_inherit_files(const struct cred *cred, struct files_struct *files)
  596. {
  597. struct aa_label *label = aa_get_newest_cred_label(cred);
  598. struct file *devnull = NULL;
  599. unsigned int n;
  600. revalidate_tty(label);
  601. /* Revalidate access to inherited open files. */
  602. n = iterate_fd(files, 0, match_file, label);
  603. if (!n) /* none found? */
  604. goto out;
  605. devnull = dentry_open(&aa_null, O_RDWR, cred);
  606. if (IS_ERR(devnull))
  607. devnull = NULL;
  608. /* replace all the matching ones with this */
  609. do {
  610. replace_fd(n - 1, devnull, 0);
  611. } while ((n = iterate_fd(files, n, match_file, label)) != 0);
  612. if (devnull)
  613. fput(devnull);
  614. out:
  615. aa_put_label(label);
  616. }