file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/context.h"
  20. #include "include/file.h"
  21. #include "include/match.h"
  22. #include "include/path.h"
  23. #include "include/policy.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. * audit_file_mask - convert mask to permission string
  35. * @buffer: buffer to write string to (NOT NULL)
  36. * @mask: permission mask to convert
  37. */
  38. static void audit_file_mask(struct audit_buffer *ab, u32 mask)
  39. {
  40. char str[10];
  41. aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
  42. audit_log_string(ab, str);
  43. }
  44. /**
  45. * file_audit_cb - call back for file specific audit fields
  46. * @ab: audit_buffer (NOT NULL)
  47. * @va: audit struct to audit values of (NOT NULL)
  48. */
  49. static void file_audit_cb(struct audit_buffer *ab, void *va)
  50. {
  51. struct common_audit_data *sa = va;
  52. kuid_t fsuid = current_fsuid();
  53. if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
  54. audit_log_format(ab, " requested_mask=");
  55. audit_file_mask(ab, aad(sa)->request);
  56. }
  57. if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
  58. audit_log_format(ab, " denied_mask=");
  59. audit_file_mask(ab, aad(sa)->denied);
  60. }
  61. if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
  62. audit_log_format(ab, " fsuid=%d",
  63. from_kuid(&init_user_ns, fsuid));
  64. audit_log_format(ab, " ouid=%d",
  65. from_kuid(&init_user_ns, aad(sa)->fs.ouid));
  66. }
  67. if (aad(sa)->fs.target) {
  68. audit_log_format(ab, " target=");
  69. audit_log_untrustedstring(ab, aad(sa)->fs.target);
  70. }
  71. }
  72. /**
  73. * aa_audit_file - handle the auditing of file operations
  74. * @profile: the profile being enforced (NOT NULL)
  75. * @perms: the permissions computed for the request (NOT NULL)
  76. * @gfp: allocation flags
  77. * @op: operation being mediated
  78. * @request: permissions requested
  79. * @name: name of object being mediated (MAYBE NULL)
  80. * @target: name of target (MAYBE NULL)
  81. * @ouid: object uid
  82. * @info: extra information message (MAYBE NULL)
  83. * @error: 0 if operation allowed else failure error code
  84. *
  85. * Returns: %0 or error on failure
  86. */
  87. int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
  88. const char *op, u32 request, const char *name,
  89. const char *target, kuid_t ouid, const char *info, int error)
  90. {
  91. int type = AUDIT_APPARMOR_AUTO;
  92. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);
  93. sa.u.tsk = NULL;
  94. aad(&sa)->request = request;
  95. aad(&sa)->name = name;
  96. aad(&sa)->fs.target = target;
  97. aad(&sa)->fs.ouid = ouid;
  98. aad(&sa)->info = info;
  99. aad(&sa)->error = error;
  100. sa.u.tsk = NULL;
  101. if (likely(!aad(&sa)->error)) {
  102. u32 mask = perms->audit;
  103. if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
  104. mask = 0xffff;
  105. /* mask off perms that are not being force audited */
  106. aad(&sa)->request &= mask;
  107. if (likely(!aad(&sa)->request))
  108. return 0;
  109. type = AUDIT_APPARMOR_AUDIT;
  110. } else {
  111. /* only report permissions that were denied */
  112. aad(&sa)->request = aad(&sa)->request & ~perms->allow;
  113. AA_BUG(!aad(&sa)->request);
  114. if (aad(&sa)->request & perms->kill)
  115. type = AUDIT_APPARMOR_KILL;
  116. /* quiet known rejects, assumes quiet and kill do not overlap */
  117. if ((aad(&sa)->request & perms->quiet) &&
  118. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  119. AUDIT_MODE(profile) != AUDIT_ALL)
  120. aad(&sa)->request &= ~perms->quiet;
  121. if (!aad(&sa)->request)
  122. return COMPLAIN_MODE(profile) ? 0 : aad(&sa)->error;
  123. }
  124. aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
  125. return aa_audit(type, profile, &sa, file_audit_cb);
  126. }
  127. /**
  128. * map_old_perms - map old file perms layout to the new layout
  129. * @old: permission set in old mapping
  130. *
  131. * Returns: new permission mapping
  132. */
  133. static u32 map_old_perms(u32 old)
  134. {
  135. u32 new = old & 0xf;
  136. if (old & MAY_READ)
  137. new |= AA_MAY_GETATTR | AA_MAY_OPEN;
  138. if (old & MAY_WRITE)
  139. new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
  140. AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
  141. if (old & 0x10)
  142. new |= AA_MAY_LINK;
  143. /* the old mapping lock and link_subset flags where overlaid
  144. * and use was determined by part of a pair that they were in
  145. */
  146. if (old & 0x20)
  147. new |= AA_MAY_LOCK | AA_LINK_SUBSET;
  148. if (old & 0x40) /* AA_EXEC_MMAP */
  149. new |= AA_EXEC_MMAP;
  150. return new;
  151. }
  152. /**
  153. * aa_compute_fperms - convert dfa compressed perms to internal perms
  154. * @dfa: dfa to compute perms for (NOT NULL)
  155. * @state: state in dfa
  156. * @cond: conditions to consider (NOT NULL)
  157. *
  158. * TODO: convert from dfa + state to permission entry, do computation conversion
  159. * at load time.
  160. *
  161. * Returns: computed permission set
  162. */
  163. struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
  164. struct path_cond *cond)
  165. {
  166. struct aa_perms perms;
  167. /* FIXME: change over to new dfa format
  168. * currently file perms are encoded in the dfa, new format
  169. * splits the permissions from the dfa. This mapping can be
  170. * done at profile load
  171. */
  172. perms.deny = 0;
  173. perms.kill = perms.stop = 0;
  174. perms.complain = perms.cond = 0;
  175. perms.hide = 0;
  176. perms.prompt = 0;
  177. if (uid_eq(current_fsuid(), cond->uid)) {
  178. perms.allow = map_old_perms(dfa_user_allow(dfa, state));
  179. perms.audit = map_old_perms(dfa_user_audit(dfa, state));
  180. perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
  181. perms.xindex = dfa_user_xindex(dfa, state);
  182. } else {
  183. perms.allow = map_old_perms(dfa_other_allow(dfa, state));
  184. perms.audit = map_old_perms(dfa_other_audit(dfa, state));
  185. perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
  186. perms.xindex = dfa_other_xindex(dfa, state);
  187. }
  188. perms.allow |= AA_MAY_GETATTR;
  189. /* change_profile wasn't determined by ownership in old mapping */
  190. if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
  191. perms.allow |= AA_MAY_CHANGE_PROFILE;
  192. if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
  193. perms.allow |= AA_MAY_ONEXEC;
  194. return perms;
  195. }
  196. /**
  197. * aa_str_perms - find permission that match @name
  198. * @dfa: to match against (MAYBE NULL)
  199. * @state: state to start matching in
  200. * @name: string to match against dfa (NOT NULL)
  201. * @cond: conditions to consider for permission set computation (NOT NULL)
  202. * @perms: Returns - the permissions found when matching @name
  203. *
  204. * Returns: the final state in @dfa when beginning @start and walking @name
  205. */
  206. unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
  207. const char *name, struct path_cond *cond,
  208. struct aa_perms *perms)
  209. {
  210. unsigned int state;
  211. state = aa_dfa_match(dfa, start, name);
  212. *perms = aa_compute_fperms(dfa, state, cond);
  213. return state;
  214. }
  215. /**
  216. * is_deleted - test if a file has been completely unlinked
  217. * @dentry: dentry of file to test for deletion (NOT NULL)
  218. *
  219. * Returns: %1 if deleted else %0
  220. */
  221. static inline bool is_deleted(struct dentry *dentry)
  222. {
  223. if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
  224. return 1;
  225. return 0;
  226. }
  227. /**
  228. * aa_path_perm - do permissions check & audit for @path
  229. * @op: operation being checked
  230. * @profile: profile being enforced (NOT NULL)
  231. * @path: path to check permissions of (NOT NULL)
  232. * @flags: any additional path flags beyond what the profile specifies
  233. * @request: requested permissions
  234. * @cond: conditional info for this request (NOT NULL)
  235. *
  236. * Returns: %0 else error if access denied or other error
  237. */
  238. int aa_path_perm(const char *op, struct aa_profile *profile,
  239. const struct path *path, int flags, u32 request,
  240. struct path_cond *cond)
  241. {
  242. char *buffer = NULL;
  243. struct aa_perms perms = {};
  244. const char *name, *info = NULL;
  245. int error;
  246. flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
  247. get_buffers(buffer);
  248. error = aa_path_name(path, flags, buffer, &name, &info,
  249. profile->disconnected);
  250. if (error) {
  251. if (error == -ENOENT && is_deleted(path->dentry)) {
  252. /* Access to open files that are deleted are
  253. * give a pass (implicit delegation)
  254. */
  255. error = 0;
  256. info = NULL;
  257. perms.allow = request;
  258. }
  259. } else {
  260. aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
  261. &perms);
  262. if (request & ~perms.allow)
  263. error = -EACCES;
  264. }
  265. error = aa_audit_file(profile, &perms, op, request, name, NULL,
  266. cond->uid, info, error);
  267. put_buffers(buffer);
  268. return error;
  269. }
  270. /**
  271. * xindex_is_subset - helper for aa_path_link
  272. * @link: link permission set
  273. * @target: target permission set
  274. *
  275. * test target x permissions are equal OR a subset of link x permissions
  276. * this is done as part of the subset test, where a hardlink must have
  277. * a subset of permissions that the target has.
  278. *
  279. * Returns: %1 if subset else %0
  280. */
  281. static inline bool xindex_is_subset(u32 link, u32 target)
  282. {
  283. if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
  284. ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
  285. return 0;
  286. return 1;
  287. }
  288. /**
  289. * aa_path_link - Handle hard link permission check
  290. * @profile: the profile being enforced (NOT NULL)
  291. * @old_dentry: the target dentry (NOT NULL)
  292. * @new_dir: directory the new link will be created in (NOT NULL)
  293. * @new_dentry: the link being created (NOT NULL)
  294. *
  295. * Handle the permission test for a link & target pair. Permission
  296. * is encoded as a pair where the link permission is determined
  297. * first, and if allowed, the target is tested. The target test
  298. * is done from the point of the link match (not start of DFA)
  299. * making the target permission dependent on the link permission match.
  300. *
  301. * The subset test if required forces that permissions granted
  302. * on link are a subset of the permission granted to target.
  303. *
  304. * Returns: %0 if allowed else error
  305. */
  306. int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
  307. const struct path *new_dir, struct dentry *new_dentry)
  308. {
  309. struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
  310. struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
  311. struct path_cond cond = {
  312. d_backing_inode(old_dentry)->i_uid,
  313. d_backing_inode(old_dentry)->i_mode
  314. };
  315. char *buffer = NULL, *buffer2 = NULL;
  316. const char *lname, *tname = NULL, *info = NULL;
  317. struct aa_perms lperms, perms;
  318. u32 request = AA_MAY_LINK;
  319. unsigned int state;
  320. int error;
  321. get_buffers(buffer, buffer2);
  322. lperms = nullperms;
  323. /* buffer freed below, lname is pointer in buffer */
  324. error = aa_path_name(&link, profile->path_flags, buffer, &lname,
  325. &info, profile->disconnected);
  326. if (error)
  327. goto audit;
  328. /* buffer2 freed below, tname is pointer in buffer2 */
  329. error = aa_path_name(&target, profile->path_flags, buffer2, &tname,
  330. &info, profile->disconnected);
  331. if (error)
  332. goto audit;
  333. error = -EACCES;
  334. /* aa_str_perms - handles the case of the dfa being NULL */
  335. state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
  336. &cond, &lperms);
  337. if (!(lperms.allow & AA_MAY_LINK))
  338. goto audit;
  339. /* test to see if target can be paired with link */
  340. state = aa_dfa_null_transition(profile->file.dfa, state);
  341. aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
  342. /* force audit/quiet masks for link are stored in the second entry
  343. * in the link pair.
  344. */
  345. lperms.audit = perms.audit;
  346. lperms.quiet = perms.quiet;
  347. lperms.kill = perms.kill;
  348. if (!(perms.allow & AA_MAY_LINK)) {
  349. info = "target restricted";
  350. goto audit;
  351. }
  352. /* done if link subset test is not required */
  353. if (!(perms.allow & AA_LINK_SUBSET))
  354. goto done_tests;
  355. /* Do link perm subset test requiring allowed permission on link are a
  356. * subset of the allowed permissions on target.
  357. */
  358. aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
  359. &perms);
  360. /* AA_MAY_LINK is not considered in the subset test */
  361. request = lperms.allow & ~AA_MAY_LINK;
  362. lperms.allow &= perms.allow | AA_MAY_LINK;
  363. request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
  364. if (request & ~lperms.allow) {
  365. goto audit;
  366. } else if ((lperms.allow & MAY_EXEC) &&
  367. !xindex_is_subset(lperms.xindex, perms.xindex)) {
  368. lperms.allow &= ~MAY_EXEC;
  369. request |= MAY_EXEC;
  370. info = "link not subset of target";
  371. goto audit;
  372. }
  373. done_tests:
  374. error = 0;
  375. audit:
  376. error = aa_audit_file(profile, &lperms, OP_LINK, request,
  377. lname, tname, cond.uid, info, error);
  378. put_buffers(buffer, buffer2);
  379. return error;
  380. }
  381. /**
  382. * aa_file_perm - do permission revalidation check & audit for @file
  383. * @op: operation being checked
  384. * @profile: profile being enforced (NOT NULL)
  385. * @file: file to revalidate access permissions on (NOT NULL)
  386. * @request: requested permissions
  387. *
  388. * Returns: %0 if access allowed else error
  389. */
  390. int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file,
  391. u32 request)
  392. {
  393. struct path_cond cond = {
  394. .uid = file_inode(file)->i_uid,
  395. .mode = file_inode(file)->i_mode
  396. };
  397. return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
  398. request, &cond);
  399. }
  400. static void revalidate_tty(struct aa_label *label)
  401. {
  402. struct tty_struct *tty;
  403. int drop_tty = 0;
  404. tty = get_current_tty();
  405. if (!tty)
  406. return;
  407. spin_lock(&tty->files_lock);
  408. if (!list_empty(&tty->tty_files)) {
  409. struct tty_file_private *file_priv;
  410. struct file *file;
  411. /* TODO: Revalidate access to controlling tty. */
  412. file_priv = list_first_entry(&tty->tty_files,
  413. struct tty_file_private, list);
  414. file = file_priv->file;
  415. if (aa_file_perm(OP_INHERIT, labels_profile(label), file,
  416. MAY_READ | MAY_WRITE))
  417. drop_tty = 1;
  418. }
  419. spin_unlock(&tty->files_lock);
  420. tty_kref_put(tty);
  421. if (drop_tty)
  422. no_tty();
  423. }
  424. static int match_file(const void *p, struct file *file, unsigned int fd)
  425. {
  426. struct aa_label *label = (struct aa_label *)p;
  427. if (aa_file_perm(OP_INHERIT, labels_profile(label), file,
  428. aa_map_file_to_perms(file)))
  429. return fd + 1;
  430. return 0;
  431. }
  432. /* based on selinux's flush_unauthorized_files */
  433. void aa_inherit_files(const struct cred *cred, struct files_struct *files)
  434. {
  435. struct aa_label *label = aa_get_newest_cred_label(cred);
  436. struct file *devnull = NULL;
  437. unsigned int n;
  438. revalidate_tty(label);
  439. /* Revalidate access to inherited open files. */
  440. n = iterate_fd(files, 0, match_file, label);
  441. if (!n) /* none found? */
  442. goto out;
  443. devnull = dentry_open(&aa_null, O_RDWR, cred);
  444. if (IS_ERR(devnull))
  445. devnull = NULL;
  446. /* replace all the matching ones with this */
  447. do {
  448. replace_fd(n - 1, devnull, 0);
  449. } while ((n = iterate_fd(files, n, match_file, label)) != 0);
  450. if (devnull)
  451. fput(devnull);
  452. out:
  453. aa_put_label(label);
  454. }