lsm.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor LSM hooks.
  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/lsm_hooks.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/mount.h>
  19. #include <linux/namei.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/ctype.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/audit.h>
  24. #include <linux/user_namespace.h>
  25. #include <linux/kmemleak.h>
  26. #include <net/sock.h>
  27. #include "include/apparmor.h"
  28. #include "include/apparmorfs.h"
  29. #include "include/audit.h"
  30. #include "include/capability.h"
  31. #include "include/context.h"
  32. #include "include/file.h"
  33. #include "include/ipc.h"
  34. #include "include/path.h"
  35. #include "include/policy.h"
  36. #include "include/policy_ns.h"
  37. #include "include/procattr.h"
  38. /* Flag indicating whether initialization completed */
  39. int apparmor_initialized;
  40. DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
  41. /*
  42. * LSM hook functions
  43. */
  44. /*
  45. * free the associated aa_task_ctx and put its profiles
  46. */
  47. static void apparmor_cred_free(struct cred *cred)
  48. {
  49. aa_free_task_context(cred_ctx(cred));
  50. cred_ctx(cred) = NULL;
  51. }
  52. /*
  53. * allocate the apparmor part of blank credentials
  54. */
  55. static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  56. {
  57. /* freed by apparmor_cred_free */
  58. struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
  59. if (!ctx)
  60. return -ENOMEM;
  61. cred_ctx(cred) = ctx;
  62. return 0;
  63. }
  64. /*
  65. * prepare new aa_task_ctx for modification by prepare_cred block
  66. */
  67. static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  68. gfp_t gfp)
  69. {
  70. /* freed by apparmor_cred_free */
  71. struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
  72. if (!ctx)
  73. return -ENOMEM;
  74. aa_dup_task_context(ctx, cred_ctx(old));
  75. cred_ctx(new) = ctx;
  76. return 0;
  77. }
  78. /*
  79. * transfer the apparmor data to a blank set of creds
  80. */
  81. static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  82. {
  83. const struct aa_task_ctx *old_ctx = cred_ctx(old);
  84. struct aa_task_ctx *new_ctx = cred_ctx(new);
  85. aa_dup_task_context(new_ctx, old_ctx);
  86. }
  87. static int apparmor_ptrace_access_check(struct task_struct *child,
  88. unsigned int mode)
  89. {
  90. return aa_ptrace(current, child, mode);
  91. }
  92. static int apparmor_ptrace_traceme(struct task_struct *parent)
  93. {
  94. return aa_ptrace(parent, current, PTRACE_MODE_ATTACH);
  95. }
  96. /* Derived from security/commoncap.c:cap_capget */
  97. static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
  98. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  99. {
  100. struct aa_profile *profile;
  101. const struct cred *cred;
  102. rcu_read_lock();
  103. cred = __task_cred(target);
  104. profile = aa_cred_profile(cred);
  105. /*
  106. * cap_capget is stacked ahead of this and will
  107. * initialize effective and permitted.
  108. */
  109. if (!unconfined(profile) && !COMPLAIN_MODE(profile)) {
  110. *effective = cap_intersect(*effective, profile->caps.allow);
  111. *permitted = cap_intersect(*permitted, profile->caps.allow);
  112. }
  113. rcu_read_unlock();
  114. return 0;
  115. }
  116. static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
  117. int cap, int audit)
  118. {
  119. struct aa_profile *profile;
  120. int error = 0;
  121. profile = aa_cred_profile(cred);
  122. if (!unconfined(profile))
  123. error = aa_capable(profile, cap, audit);
  124. return error;
  125. }
  126. /**
  127. * common_perm - basic common permission check wrapper fn for paths
  128. * @op: operation being checked
  129. * @path: path to check permission of (NOT NULL)
  130. * @mask: requested permissions mask
  131. * @cond: conditional info for the permission request (NOT NULL)
  132. *
  133. * Returns: %0 else error code if error or permission denied
  134. */
  135. static int common_perm(const char *op, const struct path *path, u32 mask,
  136. struct path_cond *cond)
  137. {
  138. struct aa_profile *profile;
  139. int error = 0;
  140. profile = __aa_current_profile();
  141. if (!unconfined(profile))
  142. error = aa_path_perm(op, profile, path, 0, mask, cond);
  143. return error;
  144. }
  145. /**
  146. * common_perm_cond - common permission wrapper around inode cond
  147. * @op: operation being checked
  148. * @path: location to check (NOT NULL)
  149. * @mask: requested permissions mask
  150. *
  151. * Returns: %0 else error code if error or permission denied
  152. */
  153. static int common_perm_cond(const char *op, const struct path *path, u32 mask)
  154. {
  155. struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
  156. d_backing_inode(path->dentry)->i_mode
  157. };
  158. if (!path_mediated_fs(path->dentry))
  159. return 0;
  160. return common_perm(op, path, mask, &cond);
  161. }
  162. /**
  163. * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
  164. * @op: operation being checked
  165. * @dir: directory of the dentry (NOT NULL)
  166. * @dentry: dentry to check (NOT NULL)
  167. * @mask: requested permissions mask
  168. * @cond: conditional info for the permission request (NOT NULL)
  169. *
  170. * Returns: %0 else error code if error or permission denied
  171. */
  172. static int common_perm_dir_dentry(const char *op, const struct path *dir,
  173. struct dentry *dentry, u32 mask,
  174. struct path_cond *cond)
  175. {
  176. struct path path = { .mnt = dir->mnt, .dentry = dentry };
  177. return common_perm(op, &path, mask, cond);
  178. }
  179. /**
  180. * common_perm_rm - common permission wrapper for operations doing rm
  181. * @op: operation being checked
  182. * @dir: directory that the dentry is in (NOT NULL)
  183. * @dentry: dentry being rm'd (NOT NULL)
  184. * @mask: requested permission mask
  185. *
  186. * Returns: %0 else error code if error or permission denied
  187. */
  188. static int common_perm_rm(const char *op, const struct path *dir,
  189. struct dentry *dentry, u32 mask)
  190. {
  191. struct inode *inode = d_backing_inode(dentry);
  192. struct path_cond cond = { };
  193. if (!inode || !path_mediated_fs(dentry))
  194. return 0;
  195. cond.uid = inode->i_uid;
  196. cond.mode = inode->i_mode;
  197. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  198. }
  199. /**
  200. * common_perm_create - common permission wrapper for operations doing create
  201. * @op: operation being checked
  202. * @dir: directory that dentry will be created in (NOT NULL)
  203. * @dentry: dentry to create (NOT NULL)
  204. * @mask: request permission mask
  205. * @mode: created file mode
  206. *
  207. * Returns: %0 else error code if error or permission denied
  208. */
  209. static int common_perm_create(const char *op, const struct path *dir,
  210. struct dentry *dentry, u32 mask, umode_t mode)
  211. {
  212. struct path_cond cond = { current_fsuid(), mode };
  213. if (!path_mediated_fs(dir->dentry))
  214. return 0;
  215. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  216. }
  217. static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
  218. {
  219. return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
  220. }
  221. static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
  222. umode_t mode)
  223. {
  224. return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
  225. S_IFDIR);
  226. }
  227. static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
  228. {
  229. return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
  230. }
  231. static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
  232. umode_t mode, unsigned int dev)
  233. {
  234. return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
  235. }
  236. static int apparmor_path_truncate(const struct path *path)
  237. {
  238. return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_META_WRITE);
  239. }
  240. static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
  241. const char *old_name)
  242. {
  243. return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
  244. S_IFLNK);
  245. }
  246. static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
  247. struct dentry *new_dentry)
  248. {
  249. struct aa_profile *profile;
  250. int error = 0;
  251. if (!path_mediated_fs(old_dentry))
  252. return 0;
  253. profile = aa_current_profile();
  254. if (!unconfined(profile))
  255. error = aa_path_link(profile, old_dentry, new_dir, new_dentry);
  256. return error;
  257. }
  258. static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  259. const struct path *new_dir, struct dentry *new_dentry)
  260. {
  261. struct aa_profile *profile;
  262. int error = 0;
  263. if (!path_mediated_fs(old_dentry))
  264. return 0;
  265. profile = aa_current_profile();
  266. if (!unconfined(profile)) {
  267. struct path old_path = { .mnt = old_dir->mnt,
  268. .dentry = old_dentry };
  269. struct path new_path = { .mnt = new_dir->mnt,
  270. .dentry = new_dentry };
  271. struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
  272. d_backing_inode(old_dentry)->i_mode
  273. };
  274. error = aa_path_perm(OP_RENAME_SRC, profile, &old_path, 0,
  275. MAY_READ | AA_MAY_META_READ | MAY_WRITE |
  276. AA_MAY_META_WRITE | AA_MAY_DELETE,
  277. &cond);
  278. if (!error)
  279. error = aa_path_perm(OP_RENAME_DEST, profile, &new_path,
  280. 0, MAY_WRITE | AA_MAY_META_WRITE |
  281. AA_MAY_CREATE, &cond);
  282. }
  283. return error;
  284. }
  285. static int apparmor_path_chmod(const struct path *path, umode_t mode)
  286. {
  287. return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
  288. }
  289. static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  290. {
  291. return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
  292. }
  293. static int apparmor_inode_getattr(const struct path *path)
  294. {
  295. return common_perm_cond(OP_GETATTR, path, AA_MAY_META_READ);
  296. }
  297. static int apparmor_file_open(struct file *file, const struct cred *cred)
  298. {
  299. struct aa_file_ctx *fctx = file->f_security;
  300. struct aa_profile *profile;
  301. int error = 0;
  302. if (!path_mediated_fs(file->f_path.dentry))
  303. return 0;
  304. /* If in exec, permission is handled by bprm hooks.
  305. * Cache permissions granted by the previous exec check, with
  306. * implicit read and executable mmap which are required to
  307. * actually execute the image.
  308. */
  309. if (current->in_execve) {
  310. fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
  311. return 0;
  312. }
  313. profile = aa_cred_profile(cred);
  314. if (!unconfined(profile)) {
  315. struct inode *inode = file_inode(file);
  316. struct path_cond cond = { inode->i_uid, inode->i_mode };
  317. error = aa_path_perm(OP_OPEN, profile, &file->f_path, 0,
  318. aa_map_file_to_perms(file), &cond);
  319. /* todo cache full allowed permissions set and state */
  320. fctx->allow = aa_map_file_to_perms(file);
  321. }
  322. return error;
  323. }
  324. static int apparmor_file_alloc_security(struct file *file)
  325. {
  326. /* freed by apparmor_file_free_security */
  327. file->f_security = aa_alloc_file_context(GFP_KERNEL);
  328. if (!file->f_security)
  329. return -ENOMEM;
  330. return 0;
  331. }
  332. static void apparmor_file_free_security(struct file *file)
  333. {
  334. struct aa_file_ctx *ctx = file->f_security;
  335. aa_free_file_context(ctx);
  336. }
  337. static int common_file_perm(const char *op, struct file *file, u32 mask)
  338. {
  339. struct aa_file_ctx *fctx = file->f_security;
  340. struct aa_profile *profile, *fprofile = aa_cred_profile(file->f_cred);
  341. int error = 0;
  342. AA_BUG(!fprofile);
  343. if (!file->f_path.mnt ||
  344. !path_mediated_fs(file->f_path.dentry))
  345. return 0;
  346. profile = __aa_current_profile();
  347. /* revalidate access, if task is unconfined, or the cached cred
  348. * doesn't match or if the request is for more permissions than
  349. * was granted.
  350. *
  351. * Note: the test for !unconfined(fprofile) is to handle file
  352. * delegation from unconfined tasks
  353. */
  354. if (!unconfined(profile) && !unconfined(fprofile) &&
  355. ((fprofile != profile) || (mask & ~fctx->allow)))
  356. error = aa_file_perm(op, profile, file, mask);
  357. return error;
  358. }
  359. static int apparmor_file_permission(struct file *file, int mask)
  360. {
  361. return common_file_perm(OP_FPERM, file, mask);
  362. }
  363. static int apparmor_file_lock(struct file *file, unsigned int cmd)
  364. {
  365. u32 mask = AA_MAY_LOCK;
  366. if (cmd == F_WRLCK)
  367. mask |= MAY_WRITE;
  368. return common_file_perm(OP_FLOCK, file, mask);
  369. }
  370. static int common_mmap(const char *op, struct file *file, unsigned long prot,
  371. unsigned long flags)
  372. {
  373. int mask = 0;
  374. if (!file || !file->f_security)
  375. return 0;
  376. if (prot & PROT_READ)
  377. mask |= MAY_READ;
  378. /*
  379. * Private mappings don't require write perms since they don't
  380. * write back to the files
  381. */
  382. if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
  383. mask |= MAY_WRITE;
  384. if (prot & PROT_EXEC)
  385. mask |= AA_EXEC_MMAP;
  386. return common_file_perm(op, file, mask);
  387. }
  388. static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
  389. unsigned long prot, unsigned long flags)
  390. {
  391. return common_mmap(OP_FMMAP, file, prot, flags);
  392. }
  393. static int apparmor_file_mprotect(struct vm_area_struct *vma,
  394. unsigned long reqprot, unsigned long prot)
  395. {
  396. return common_mmap(OP_FMPROT, vma->vm_file, prot,
  397. !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
  398. }
  399. static int apparmor_getprocattr(struct task_struct *task, char *name,
  400. char **value)
  401. {
  402. int error = -ENOENT;
  403. /* released below */
  404. const struct cred *cred = get_task_cred(task);
  405. struct aa_task_ctx *ctx = cred_ctx(cred);
  406. struct aa_profile *profile = NULL;
  407. if (strcmp(name, "current") == 0)
  408. profile = aa_get_newest_profile(ctx->profile);
  409. else if (strcmp(name, "prev") == 0 && ctx->previous)
  410. profile = aa_get_newest_profile(ctx->previous);
  411. else if (strcmp(name, "exec") == 0 && ctx->onexec)
  412. profile = aa_get_newest_profile(ctx->onexec);
  413. else
  414. error = -EINVAL;
  415. if (profile)
  416. error = aa_getprocattr(profile, value);
  417. aa_put_profile(profile);
  418. put_cred(cred);
  419. return error;
  420. }
  421. static int apparmor_setprocattr(const char *name, void *value,
  422. size_t size)
  423. {
  424. char *command, *largs = NULL, *args = value;
  425. size_t arg_size;
  426. int error;
  427. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
  428. if (size == 0)
  429. return -EINVAL;
  430. /* AppArmor requires that the buffer must be null terminated atm */
  431. if (args[size - 1] != '\0') {
  432. /* null terminate */
  433. largs = args = kmalloc(size + 1, GFP_KERNEL);
  434. if (!args)
  435. return -ENOMEM;
  436. memcpy(args, value, size);
  437. args[size] = '\0';
  438. }
  439. error = -EINVAL;
  440. args = strim(args);
  441. command = strsep(&args, " ");
  442. if (!args)
  443. goto out;
  444. args = skip_spaces(args);
  445. if (!*args)
  446. goto out;
  447. arg_size = size - (args - (largs ? largs : (char *) value));
  448. if (strcmp(name, "current") == 0) {
  449. if (strcmp(command, "changehat") == 0) {
  450. error = aa_setprocattr_changehat(args, arg_size,
  451. !AA_DO_TEST);
  452. } else if (strcmp(command, "permhat") == 0) {
  453. error = aa_setprocattr_changehat(args, arg_size,
  454. AA_DO_TEST);
  455. } else if (strcmp(command, "changeprofile") == 0) {
  456. error = aa_change_profile(args, !AA_ONEXEC,
  457. !AA_DO_TEST, false);
  458. } else if (strcmp(command, "permprofile") == 0) {
  459. error = aa_change_profile(args, !AA_ONEXEC, AA_DO_TEST,
  460. false);
  461. } else
  462. goto fail;
  463. } else if (strcmp(name, "exec") == 0) {
  464. if (strcmp(command, "exec") == 0)
  465. error = aa_change_profile(args, AA_ONEXEC, !AA_DO_TEST,
  466. false);
  467. else
  468. goto fail;
  469. } else
  470. /* only support the "current" and "exec" process attributes */
  471. goto fail;
  472. if (!error)
  473. error = size;
  474. out:
  475. kfree(largs);
  476. return error;
  477. fail:
  478. aad(&sa)->profile = aa_current_profile();
  479. aad(&sa)->info = name;
  480. aad(&sa)->error = error = -EINVAL;
  481. aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
  482. goto out;
  483. }
  484. static int apparmor_task_setrlimit(struct task_struct *task,
  485. unsigned int resource, struct rlimit *new_rlim)
  486. {
  487. struct aa_profile *profile = __aa_current_profile();
  488. int error = 0;
  489. if (!unconfined(profile))
  490. error = aa_task_setrlimit(profile, task, resource, new_rlim);
  491. return error;
  492. }
  493. static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
  494. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  495. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  496. LSM_HOOK_INIT(capget, apparmor_capget),
  497. LSM_HOOK_INIT(capable, apparmor_capable),
  498. LSM_HOOK_INIT(path_link, apparmor_path_link),
  499. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  500. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  501. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  502. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  503. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  504. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  505. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  506. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  507. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  508. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  509. LSM_HOOK_INIT(file_open, apparmor_file_open),
  510. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  511. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  512. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  513. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  514. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  515. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  516. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  517. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  518. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  519. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  520. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  521. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  522. LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
  523. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  524. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  525. LSM_HOOK_INIT(bprm_secureexec, apparmor_bprm_secureexec),
  526. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  527. };
  528. /*
  529. * AppArmor sysfs module parameters
  530. */
  531. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  532. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  533. #define param_check_aabool param_check_bool
  534. static const struct kernel_param_ops param_ops_aabool = {
  535. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  536. .set = param_set_aabool,
  537. .get = param_get_aabool
  538. };
  539. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  540. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  541. #define param_check_aauint param_check_uint
  542. static const struct kernel_param_ops param_ops_aauint = {
  543. .set = param_set_aauint,
  544. .get = param_get_aauint
  545. };
  546. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  547. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  548. #define param_check_aalockpolicy param_check_bool
  549. static const struct kernel_param_ops param_ops_aalockpolicy = {
  550. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  551. .set = param_set_aalockpolicy,
  552. .get = param_get_aalockpolicy
  553. };
  554. static int param_set_audit(const char *val, struct kernel_param *kp);
  555. static int param_get_audit(char *buffer, struct kernel_param *kp);
  556. static int param_set_mode(const char *val, struct kernel_param *kp);
  557. static int param_get_mode(char *buffer, struct kernel_param *kp);
  558. /* Flag values, also controllable via /sys/module/apparmor/parameters
  559. * We define special types as we want to do additional mediation.
  560. */
  561. /* AppArmor global enforcement switch - complain, enforce, kill */
  562. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  563. module_param_call(mode, param_set_mode, param_get_mode,
  564. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  565. /* whether policy verification hashing is enabled */
  566. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  567. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  568. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  569. #endif
  570. /* Debug mode */
  571. bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
  572. module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
  573. /* Audit mode */
  574. enum audit_mode aa_g_audit;
  575. module_param_call(audit, param_set_audit, param_get_audit,
  576. &aa_g_audit, S_IRUSR | S_IWUSR);
  577. /* Determines if audit header is included in audited messages. This
  578. * provides more context if the audit daemon is not running
  579. */
  580. bool aa_g_audit_header = 1;
  581. module_param_named(audit_header, aa_g_audit_header, aabool,
  582. S_IRUSR | S_IWUSR);
  583. /* lock out loading/removal of policy
  584. * TODO: add in at boot loading of policy, which is the only way to
  585. * load policy, if lock_policy is set
  586. */
  587. bool aa_g_lock_policy;
  588. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  589. S_IRUSR | S_IWUSR);
  590. /* Syscall logging mode */
  591. bool aa_g_logsyscall;
  592. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  593. /* Maximum pathname length before accesses will start getting rejected */
  594. unsigned int aa_g_path_max = 2 * PATH_MAX;
  595. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  596. /* Determines how paranoid loading of policy is and how much verification
  597. * on the loaded policy is done.
  598. * DEPRECATED: read only as strict checking of load is always done now
  599. * that none root users (user namespaces) can load policy.
  600. */
  601. bool aa_g_paranoid_load = 1;
  602. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  603. /* Boot time disable flag */
  604. static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
  605. module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
  606. static int __init apparmor_enabled_setup(char *str)
  607. {
  608. unsigned long enabled;
  609. int error = kstrtoul(str, 0, &enabled);
  610. if (!error)
  611. apparmor_enabled = enabled ? 1 : 0;
  612. return 1;
  613. }
  614. __setup("apparmor=", apparmor_enabled_setup);
  615. /* set global flag turning off the ability to load policy */
  616. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  617. {
  618. if (!apparmor_enabled)
  619. return -EINVAL;
  620. if (apparmor_initialized && !policy_admin_capable(NULL))
  621. return -EPERM;
  622. return param_set_bool(val, kp);
  623. }
  624. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  625. {
  626. if (!apparmor_enabled)
  627. return -EINVAL;
  628. if (apparmor_initialized && !policy_view_capable(NULL))
  629. return -EPERM;
  630. return param_get_bool(buffer, kp);
  631. }
  632. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  633. {
  634. if (!apparmor_enabled)
  635. return -EINVAL;
  636. if (apparmor_initialized && !policy_admin_capable(NULL))
  637. return -EPERM;
  638. return param_set_bool(val, kp);
  639. }
  640. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  641. {
  642. if (!apparmor_enabled)
  643. return -EINVAL;
  644. if (apparmor_initialized && !policy_view_capable(NULL))
  645. return -EPERM;
  646. return param_get_bool(buffer, kp);
  647. }
  648. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  649. {
  650. if (!apparmor_enabled)
  651. return -EINVAL;
  652. if (apparmor_initialized && !policy_admin_capable(NULL))
  653. return -EPERM;
  654. return param_set_uint(val, kp);
  655. }
  656. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  657. {
  658. if (!apparmor_enabled)
  659. return -EINVAL;
  660. if (apparmor_initialized && !policy_view_capable(NULL))
  661. return -EPERM;
  662. return param_get_uint(buffer, kp);
  663. }
  664. static int param_get_audit(char *buffer, struct kernel_param *kp)
  665. {
  666. if (!apparmor_enabled)
  667. return -EINVAL;
  668. if (apparmor_initialized && !policy_view_capable(NULL))
  669. return -EPERM;
  670. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  671. }
  672. static int param_set_audit(const char *val, struct kernel_param *kp)
  673. {
  674. int i;
  675. if (!apparmor_enabled)
  676. return -EINVAL;
  677. if (!val)
  678. return -EINVAL;
  679. if (apparmor_initialized && !policy_admin_capable(NULL))
  680. return -EPERM;
  681. for (i = 0; i < AUDIT_MAX_INDEX; i++) {
  682. if (strcmp(val, audit_mode_names[i]) == 0) {
  683. aa_g_audit = i;
  684. return 0;
  685. }
  686. }
  687. return -EINVAL;
  688. }
  689. static int param_get_mode(char *buffer, struct kernel_param *kp)
  690. {
  691. if (!apparmor_enabled)
  692. return -EINVAL;
  693. if (apparmor_initialized && !policy_view_capable(NULL))
  694. return -EPERM;
  695. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  696. }
  697. static int param_set_mode(const char *val, struct kernel_param *kp)
  698. {
  699. int i;
  700. if (!apparmor_enabled)
  701. return -EINVAL;
  702. if (!val)
  703. return -EINVAL;
  704. if (apparmor_initialized && !policy_admin_capable(NULL))
  705. return -EPERM;
  706. for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
  707. if (strcmp(val, aa_profile_mode_names[i]) == 0) {
  708. aa_g_profile_mode = i;
  709. return 0;
  710. }
  711. }
  712. return -EINVAL;
  713. }
  714. /*
  715. * AppArmor init functions
  716. */
  717. /**
  718. * set_init_ctx - set a task context and profile on the first task.
  719. *
  720. * TODO: allow setting an alternate profile than unconfined
  721. */
  722. static int __init set_init_ctx(void)
  723. {
  724. struct cred *cred = (struct cred *)current->real_cred;
  725. struct aa_task_ctx *ctx;
  726. ctx = aa_alloc_task_context(GFP_KERNEL);
  727. if (!ctx)
  728. return -ENOMEM;
  729. ctx->profile = aa_get_profile(root_ns->unconfined);
  730. cred_ctx(cred) = ctx;
  731. return 0;
  732. }
  733. static void destroy_buffers(void)
  734. {
  735. u32 i, j;
  736. for_each_possible_cpu(i) {
  737. for_each_cpu_buffer(j) {
  738. kfree(per_cpu(aa_buffers, i).buf[j]);
  739. per_cpu(aa_buffers, i).buf[j] = NULL;
  740. }
  741. }
  742. }
  743. static int __init alloc_buffers(void)
  744. {
  745. u32 i, j;
  746. for_each_possible_cpu(i) {
  747. for_each_cpu_buffer(j) {
  748. char *buffer;
  749. if (cpu_to_node(i) > num_online_nodes())
  750. /* fallback to kmalloc for offline nodes */
  751. buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
  752. else
  753. buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
  754. cpu_to_node(i));
  755. if (!buffer) {
  756. destroy_buffers();
  757. return -ENOMEM;
  758. }
  759. per_cpu(aa_buffers, i).buf[j] = buffer;
  760. }
  761. }
  762. return 0;
  763. }
  764. #ifdef CONFIG_SYSCTL
  765. static int apparmor_dointvec(struct ctl_table *table, int write,
  766. void __user *buffer, size_t *lenp, loff_t *ppos)
  767. {
  768. if (!policy_admin_capable(NULL))
  769. return -EPERM;
  770. if (!apparmor_enabled)
  771. return -EINVAL;
  772. return proc_dointvec(table, write, buffer, lenp, ppos);
  773. }
  774. static struct ctl_path apparmor_sysctl_path[] = {
  775. { .procname = "kernel", },
  776. { }
  777. };
  778. static struct ctl_table apparmor_sysctl_table[] = {
  779. {
  780. .procname = "unprivileged_userns_apparmor_policy",
  781. .data = &unprivileged_userns_apparmor_policy,
  782. .maxlen = sizeof(int),
  783. .mode = 0600,
  784. .proc_handler = apparmor_dointvec,
  785. },
  786. { }
  787. };
  788. static int __init apparmor_init_sysctl(void)
  789. {
  790. return register_sysctl_paths(apparmor_sysctl_path,
  791. apparmor_sysctl_table) ? 0 : -ENOMEM;
  792. }
  793. #else
  794. static inline int apparmor_init_sysctl(void)
  795. {
  796. return 0;
  797. }
  798. #endif /* CONFIG_SYSCTL */
  799. static int __init apparmor_init(void)
  800. {
  801. int error;
  802. if (!apparmor_enabled || !security_module_enable("apparmor")) {
  803. aa_info_message("AppArmor disabled by boot time parameter");
  804. apparmor_enabled = 0;
  805. return 0;
  806. }
  807. error = aa_setup_dfa_engine();
  808. if (error) {
  809. AA_ERROR("Unable to setup dfa engine\n");
  810. goto alloc_out;
  811. }
  812. error = aa_alloc_root_ns();
  813. if (error) {
  814. AA_ERROR("Unable to allocate default profile namespace\n");
  815. goto alloc_out;
  816. }
  817. error = apparmor_init_sysctl();
  818. if (error) {
  819. AA_ERROR("Unable to register sysctls\n");
  820. goto alloc_out;
  821. }
  822. error = alloc_buffers();
  823. if (error) {
  824. AA_ERROR("Unable to allocate work buffers\n");
  825. goto buffers_out;
  826. }
  827. error = set_init_ctx();
  828. if (error) {
  829. AA_ERROR("Failed to set context on init task\n");
  830. aa_free_root_ns();
  831. goto buffers_out;
  832. }
  833. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  834. "apparmor");
  835. /* Report that AppArmor successfully initialized */
  836. apparmor_initialized = 1;
  837. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  838. aa_info_message("AppArmor initialized: complain mode enabled");
  839. else if (aa_g_profile_mode == APPARMOR_KILL)
  840. aa_info_message("AppArmor initialized: kill mode enabled");
  841. else
  842. aa_info_message("AppArmor initialized");
  843. return error;
  844. buffers_out:
  845. destroy_buffers();
  846. alloc_out:
  847. aa_destroy_aafs();
  848. aa_teardown_dfa_engine();
  849. apparmor_enabled = 0;
  850. return error;
  851. }
  852. security_initcall(apparmor_init);