lsm.c 24 KB

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