file.c 13 KB

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