file.c 19 KB

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