file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /**
  22. * audit_file_mask - convert mask to permission string
  23. * @buffer: buffer to write string to (NOT NULL)
  24. * @mask: permission mask to convert
  25. */
  26. static void audit_file_mask(struct audit_buffer *ab, u32 mask)
  27. {
  28. char str[10];
  29. char *m = str;
  30. if (mask & AA_EXEC_MMAP)
  31. *m++ = 'm';
  32. if (mask & (MAY_READ | AA_MAY_META_READ))
  33. *m++ = 'r';
  34. if (mask & (MAY_WRITE | AA_MAY_META_WRITE | AA_MAY_CHMOD |
  35. AA_MAY_CHOWN))
  36. *m++ = 'w';
  37. else if (mask & MAY_APPEND)
  38. *m++ = 'a';
  39. if (mask & AA_MAY_CREATE)
  40. *m++ = 'c';
  41. if (mask & AA_MAY_DELETE)
  42. *m++ = 'd';
  43. if (mask & AA_MAY_LINK)
  44. *m++ = 'l';
  45. if (mask & AA_MAY_LOCK)
  46. *m++ = 'k';
  47. if (mask & MAY_EXEC)
  48. *m++ = 'x';
  49. *m = '\0';
  50. audit_log_string(ab, str);
  51. }
  52. /**
  53. * file_audit_cb - call back for file specific audit fields
  54. * @ab: audit_buffer (NOT NULL)
  55. * @va: audit struct to audit values of (NOT NULL)
  56. */
  57. static void file_audit_cb(struct audit_buffer *ab, void *va)
  58. {
  59. struct common_audit_data *sa = va;
  60. uid_t fsuid = current_fsuid();
  61. if (sa->aad.fs.request & AA_AUDIT_FILE_MASK) {
  62. audit_log_format(ab, " requested_mask=");
  63. audit_file_mask(ab, sa->aad.fs.request);
  64. }
  65. if (sa->aad.fs.denied & AA_AUDIT_FILE_MASK) {
  66. audit_log_format(ab, " denied_mask=");
  67. audit_file_mask(ab, sa->aad.fs.denied);
  68. }
  69. if (sa->aad.fs.request & AA_AUDIT_FILE_MASK) {
  70. audit_log_format(ab, " fsuid=%d", fsuid);
  71. audit_log_format(ab, " ouid=%d", sa->aad.fs.ouid);
  72. }
  73. if (sa->aad.fs.target) {
  74. audit_log_format(ab, " target=");
  75. audit_log_untrustedstring(ab, sa->aad.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. * @gfp: allocation flags
  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. * @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 file_perms *perms,
  94. gfp_t gfp, int op, u32 request, const char *name,
  95. const char *target, uid_t ouid, const char *info, int error)
  96. {
  97. int type = AUDIT_APPARMOR_AUTO;
  98. struct common_audit_data sa;
  99. COMMON_AUDIT_DATA_INIT(&sa, NONE);
  100. sa.aad.op = op,
  101. sa.aad.fs.request = request;
  102. sa.aad.name = name;
  103. sa.aad.fs.target = target;
  104. sa.aad.fs.ouid = ouid;
  105. sa.aad.info = info;
  106. sa.aad.error = error;
  107. if (likely(!sa.aad.error)) {
  108. u32 mask = perms->audit;
  109. if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
  110. mask = 0xffff;
  111. /* mask off perms that are not being force audited */
  112. sa.aad.fs.request &= mask;
  113. if (likely(!sa.aad.fs.request))
  114. return 0;
  115. type = AUDIT_APPARMOR_AUDIT;
  116. } else {
  117. /* only report permissions that were denied */
  118. sa.aad.fs.request = sa.aad.fs.request & ~perms->allow;
  119. if (sa.aad.fs.request & perms->kill)
  120. type = AUDIT_APPARMOR_KILL;
  121. /* quiet known rejects, assumes quiet and kill do not overlap */
  122. if ((sa.aad.fs.request & perms->quiet) &&
  123. AUDIT_MODE(profile) != AUDIT_NOQUIET &&
  124. AUDIT_MODE(profile) != AUDIT_ALL)
  125. sa.aad.fs.request &= ~perms->quiet;
  126. if (!sa.aad.fs.request)
  127. return COMPLAIN_MODE(profile) ? 0 : sa.aad.error;
  128. }
  129. sa.aad.fs.denied = sa.aad.fs.request & ~perms->allow;
  130. return aa_audit(type, profile, gfp, &sa, file_audit_cb);
  131. }
  132. /**
  133. * map_old_perms - map old file perms layout to the new layout
  134. * @old: permission set in old mapping
  135. *
  136. * Returns: new permission mapping
  137. */
  138. static u32 map_old_perms(u32 old)
  139. {
  140. u32 new = old & 0xf;
  141. if (old & MAY_READ)
  142. new |= AA_MAY_META_READ;
  143. if (old & MAY_WRITE)
  144. new |= AA_MAY_META_WRITE | AA_MAY_CREATE | AA_MAY_DELETE |
  145. AA_MAY_CHMOD | AA_MAY_CHOWN;
  146. if (old & 0x10)
  147. new |= AA_MAY_LINK;
  148. /* the old mapping lock and link_subset flags where overlaid
  149. * and use was determined by part of a pair that they were in
  150. */
  151. if (old & 0x20)
  152. new |= AA_MAY_LOCK | AA_LINK_SUBSET;
  153. if (old & 0x40) /* AA_EXEC_MMAP */
  154. new |= AA_EXEC_MMAP;
  155. return new;
  156. }
  157. /**
  158. * compute_perms - convert dfa compressed perms to internal perms
  159. * @dfa: dfa to compute perms for (NOT NULL)
  160. * @state: state in dfa
  161. * @cond: conditions to consider (NOT NULL)
  162. *
  163. * TODO: convert from dfa + state to permission entry, do computation conversion
  164. * at load time.
  165. *
  166. * Returns: computed permission set
  167. */
  168. static struct file_perms compute_perms(struct aa_dfa *dfa, unsigned int state,
  169. struct path_cond *cond)
  170. {
  171. struct file_perms perms;
  172. /* FIXME: change over to new dfa format
  173. * currently file perms are encoded in the dfa, new format
  174. * splits the permissions from the dfa. This mapping can be
  175. * done at profile load
  176. */
  177. perms.kill = 0;
  178. if (current_fsuid() == cond->uid) {
  179. perms.allow = map_old_perms(dfa_user_allow(dfa, state));
  180. perms.audit = map_old_perms(dfa_user_audit(dfa, state));
  181. perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
  182. perms.xindex = dfa_user_xindex(dfa, state);
  183. } else {
  184. perms.allow = map_old_perms(dfa_other_allow(dfa, state));
  185. perms.audit = map_old_perms(dfa_other_audit(dfa, state));
  186. perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
  187. perms.xindex = dfa_other_xindex(dfa, state);
  188. }
  189. perms.allow |= AA_MAY_META_READ;
  190. /* change_profile wasn't determined by ownership in old mapping */
  191. if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
  192. perms.allow |= AA_MAY_CHANGE_PROFILE;
  193. return perms;
  194. }
  195. /**
  196. * aa_str_perms - find permission that match @name
  197. * @dfa: to match against (MAYBE NULL)
  198. * @state: state to start matching in
  199. * @name: string to match against dfa (NOT NULL)
  200. * @cond: conditions to consider for permission set computation (NOT NULL)
  201. * @perms: Returns - the permissions found when matching @name
  202. *
  203. * Returns: the final state in @dfa when beginning @start and walking @name
  204. */
  205. unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
  206. const char *name, struct path_cond *cond,
  207. struct file_perms *perms)
  208. {
  209. unsigned int state;
  210. if (!dfa) {
  211. *perms = nullperms;
  212. return DFA_NOMATCH;
  213. }
  214. state = aa_dfa_match(dfa, start, name);
  215. *perms = compute_perms(dfa, state, cond);
  216. return state;
  217. }
  218. /**
  219. * is_deleted - test if a file has been completely unlinked
  220. * @dentry: dentry of file to test for deletion (NOT NULL)
  221. *
  222. * Returns: %1 if deleted else %0
  223. */
  224. static inline bool is_deleted(struct dentry *dentry)
  225. {
  226. if (d_unlinked(dentry) && dentry->d_inode->i_nlink == 0)
  227. return 1;
  228. return 0;
  229. }
  230. /**
  231. * aa_path_perm - do permissions check & audit for @path
  232. * @op: operation being checked
  233. * @profile: profile being enforced (NOT NULL)
  234. * @path: path to check permissions of (NOT NULL)
  235. * @flags: any additional path flags beyond what the profile specifies
  236. * @request: requested permissions
  237. * @cond: conditional info for this request (NOT NULL)
  238. *
  239. * Returns: %0 else error if access denied or other error
  240. */
  241. int aa_path_perm(int op, struct aa_profile *profile, struct path *path,
  242. int flags, u32 request, struct path_cond *cond)
  243. {
  244. char *buffer = NULL;
  245. struct file_perms perms = {};
  246. const char *name, *info = NULL;
  247. int error;
  248. flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
  249. error = aa_path_name(path, flags, &buffer, &name, &info);
  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, GFP_KERNEL, op, request, name,
  266. NULL, cond->uid, info, error);
  267. kfree(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. struct path *new_dir, struct dentry *new_dentry)
  308. {
  309. struct path link = { new_dir->mnt, new_dentry };
  310. struct path target = { new_dir->mnt, old_dentry };
  311. struct path_cond cond = {
  312. old_dentry->d_inode->i_uid,
  313. old_dentry->d_inode->i_mode
  314. };
  315. char *buffer = NULL, *buffer2 = NULL;
  316. const char *lname, *tname = NULL, *info = NULL;
  317. struct file_perms lperms, perms;
  318. u32 request = AA_MAY_LINK;
  319. unsigned int state;
  320. int error;
  321. lperms = nullperms;
  322. /* buffer freed below, lname is pointer in buffer */
  323. error = aa_path_name(&link, profile->path_flags, &buffer, &lname,
  324. &info);
  325. if (error)
  326. goto audit;
  327. /* buffer2 freed below, tname is pointer in buffer2 */
  328. error = aa_path_name(&target, profile->path_flags, &buffer2, &tname,
  329. &info);
  330. if (error)
  331. goto audit;
  332. error = -EACCES;
  333. /* aa_str_perms - handles the case of the dfa being NULL */
  334. state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
  335. &cond, &lperms);
  336. if (!(lperms.allow & AA_MAY_LINK))
  337. goto audit;
  338. /* test to see if target can be paired with link */
  339. state = aa_dfa_null_transition(profile->file.dfa, state);
  340. aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);
  341. /* force audit/quiet masks for link are stored in the second entry
  342. * in the link pair.
  343. */
  344. lperms.audit = perms.audit;
  345. lperms.quiet = perms.quiet;
  346. lperms.kill = perms.kill;
  347. if (!(perms.allow & AA_MAY_LINK)) {
  348. info = "target restricted";
  349. goto audit;
  350. }
  351. /* done if link subset test is not required */
  352. if (!(perms.allow & AA_LINK_SUBSET))
  353. goto done_tests;
  354. /* Do link perm subset test requiring allowed permission on link are a
  355. * subset of the allowed permissions on target.
  356. */
  357. aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
  358. &perms);
  359. /* AA_MAY_LINK is not considered in the subset test */
  360. request = lperms.allow & ~AA_MAY_LINK;
  361. lperms.allow &= perms.allow | AA_MAY_LINK;
  362. request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
  363. if (request & ~lperms.allow) {
  364. goto audit;
  365. } else if ((lperms.allow & MAY_EXEC) &&
  366. !xindex_is_subset(lperms.xindex, perms.xindex)) {
  367. lperms.allow &= ~MAY_EXEC;
  368. request |= MAY_EXEC;
  369. info = "link not subset of target";
  370. goto audit;
  371. }
  372. done_tests:
  373. error = 0;
  374. audit:
  375. error = aa_audit_file(profile, &lperms, GFP_KERNEL, OP_LINK, request,
  376. lname, tname, cond.uid, info, error);
  377. kfree(buffer);
  378. kfree(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(int op, struct aa_profile *profile, struct file *file,
  391. u32 request)
  392. {
  393. struct path_cond cond = {
  394. .uid = file->f_path.dentry->d_inode->i_uid,
  395. .mode = file->f_path.dentry->d_inode->i_mode
  396. };
  397. return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED,
  398. request, &cond);
  399. }