file.c 19 KB

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