file.c 19 KB

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