lsm.c 24 KB

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