file.c 19 KB

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