lsm.c 25 KB

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