file.c 13 KB

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