lsm.c 31 KB

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