lsm.c 31 KB

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