lsm.c 28 KB

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