lsm.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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 <net/sock.h>
  26. #include "include/apparmor.h"
  27. #include "include/apparmorfs.h"
  28. #include "include/audit.h"
  29. #include "include/capability.h"
  30. #include "include/cred.h"
  31. #include "include/file.h"
  32. #include "include/ipc.h"
  33. #include "include/net.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. #include "include/secid.h"
  41. /* Flag indicating whether initialization completed */
  42. int apparmor_initialized;
  43. DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
  44. /*
  45. * LSM hook functions
  46. */
  47. /*
  48. * put the associated labels
  49. */
  50. static void apparmor_cred_free(struct cred *cred)
  51. {
  52. aa_put_label(cred_label(cred));
  53. cred_label(cred) = NULL;
  54. }
  55. /*
  56. * allocate the apparmor part of blank credentials
  57. */
  58. static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  59. {
  60. cred_label(cred) = NULL;
  61. return 0;
  62. }
  63. /*
  64. * prepare new cred label for modification by prepare_cred block
  65. */
  66. static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  67. gfp_t gfp)
  68. {
  69. cred_label(new) = aa_get_newest_label(cred_label(old));
  70. return 0;
  71. }
  72. /*
  73. * transfer the apparmor data to a blank set of creds
  74. */
  75. static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  76. {
  77. cred_label(new) = aa_get_newest_label(cred_label(old));
  78. }
  79. static void apparmor_task_free(struct task_struct *task)
  80. {
  81. aa_free_task_ctx(task_ctx(task));
  82. task_ctx(task) = NULL;
  83. }
  84. static int apparmor_task_alloc(struct task_struct *task,
  85. unsigned long clone_flags)
  86. {
  87. struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL);
  88. if (!new)
  89. return -ENOMEM;
  90. aa_dup_task_ctx(new, task_ctx(current));
  91. task_ctx(task) = new;
  92. return 0;
  93. }
  94. static int apparmor_ptrace_access_check(struct task_struct *child,
  95. unsigned int mode)
  96. {
  97. struct aa_label *tracer, *tracee;
  98. int error;
  99. tracer = begin_current_label_crit_section();
  100. tracee = aa_get_task_label(child);
  101. error = aa_may_ptrace(tracer, tracee,
  102. (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
  103. : AA_PTRACE_TRACE);
  104. aa_put_label(tracee);
  105. end_current_label_crit_section(tracer);
  106. return error;
  107. }
  108. static int apparmor_ptrace_traceme(struct task_struct *parent)
  109. {
  110. struct aa_label *tracer, *tracee;
  111. int error;
  112. tracee = begin_current_label_crit_section();
  113. tracer = aa_get_task_label(parent);
  114. error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
  115. aa_put_label(tracer);
  116. end_current_label_crit_section(tracee);
  117. return error;
  118. }
  119. /* Derived from security/commoncap.c:cap_capget */
  120. static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
  121. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  122. {
  123. struct aa_label *label;
  124. const struct cred *cred;
  125. rcu_read_lock();
  126. cred = __task_cred(target);
  127. label = aa_get_newest_cred_label(cred);
  128. /*
  129. * cap_capget is stacked ahead of this and will
  130. * initialize effective and permitted.
  131. */
  132. if (!unconfined(label)) {
  133. struct aa_profile *profile;
  134. struct label_it i;
  135. label_for_each_confined(i, label, profile) {
  136. if (COMPLAIN_MODE(profile))
  137. continue;
  138. *effective = cap_intersect(*effective,
  139. profile->caps.allow);
  140. *permitted = cap_intersect(*permitted,
  141. profile->caps.allow);
  142. }
  143. }
  144. rcu_read_unlock();
  145. aa_put_label(label);
  146. return 0;
  147. }
  148. static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
  149. int cap, int audit)
  150. {
  151. struct aa_label *label;
  152. int error = 0;
  153. label = aa_get_newest_cred_label(cred);
  154. if (!unconfined(label))
  155. error = aa_capable(label, cap, audit);
  156. aa_put_label(label);
  157. return error;
  158. }
  159. /**
  160. * common_perm - basic common permission check wrapper fn for paths
  161. * @op: operation being checked
  162. * @path: path to check permission of (NOT NULL)
  163. * @mask: requested permissions mask
  164. * @cond: conditional info for the permission request (NOT NULL)
  165. *
  166. * Returns: %0 else error code if error or permission denied
  167. */
  168. static int common_perm(const char *op, const struct path *path, u32 mask,
  169. struct path_cond *cond)
  170. {
  171. struct aa_label *label;
  172. int error = 0;
  173. label = __begin_current_label_crit_section();
  174. if (!unconfined(label))
  175. error = aa_path_perm(op, label, path, 0, mask, cond);
  176. __end_current_label_crit_section(label);
  177. return error;
  178. }
  179. /**
  180. * common_perm_cond - common permission wrapper around inode cond
  181. * @op: operation being checked
  182. * @path: location to check (NOT NULL)
  183. * @mask: requested permissions mask
  184. *
  185. * Returns: %0 else error code if error or permission denied
  186. */
  187. static int common_perm_cond(const char *op, const struct path *path, u32 mask)
  188. {
  189. struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
  190. d_backing_inode(path->dentry)->i_mode
  191. };
  192. if (!path_mediated_fs(path->dentry))
  193. return 0;
  194. return common_perm(op, path, mask, &cond);
  195. }
  196. /**
  197. * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
  198. * @op: operation being checked
  199. * @dir: directory of the dentry (NOT NULL)
  200. * @dentry: dentry to check (NOT NULL)
  201. * @mask: requested permissions mask
  202. * @cond: conditional info for the permission request (NOT NULL)
  203. *
  204. * Returns: %0 else error code if error or permission denied
  205. */
  206. static int common_perm_dir_dentry(const char *op, const struct path *dir,
  207. struct dentry *dentry, u32 mask,
  208. struct path_cond *cond)
  209. {
  210. struct path path = { .mnt = dir->mnt, .dentry = dentry };
  211. return common_perm(op, &path, mask, cond);
  212. }
  213. /**
  214. * common_perm_rm - common permission wrapper for operations doing rm
  215. * @op: operation being checked
  216. * @dir: directory that the dentry is in (NOT NULL)
  217. * @dentry: dentry being rm'd (NOT NULL)
  218. * @mask: requested permission mask
  219. *
  220. * Returns: %0 else error code if error or permission denied
  221. */
  222. static int common_perm_rm(const char *op, const struct path *dir,
  223. struct dentry *dentry, u32 mask)
  224. {
  225. struct inode *inode = d_backing_inode(dentry);
  226. struct path_cond cond = { };
  227. if (!inode || !path_mediated_fs(dentry))
  228. return 0;
  229. cond.uid = inode->i_uid;
  230. cond.mode = inode->i_mode;
  231. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  232. }
  233. /**
  234. * common_perm_create - common permission wrapper for operations doing create
  235. * @op: operation being checked
  236. * @dir: directory that dentry will be created in (NOT NULL)
  237. * @dentry: dentry to create (NOT NULL)
  238. * @mask: request permission mask
  239. * @mode: created file mode
  240. *
  241. * Returns: %0 else error code if error or permission denied
  242. */
  243. static int common_perm_create(const char *op, const struct path *dir,
  244. struct dentry *dentry, u32 mask, umode_t mode)
  245. {
  246. struct path_cond cond = { current_fsuid(), mode };
  247. if (!path_mediated_fs(dir->dentry))
  248. return 0;
  249. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  250. }
  251. static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
  252. {
  253. return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
  254. }
  255. static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
  256. umode_t mode)
  257. {
  258. return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
  259. S_IFDIR);
  260. }
  261. static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
  262. {
  263. return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
  264. }
  265. static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
  266. umode_t mode, unsigned int dev)
  267. {
  268. return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
  269. }
  270. static int apparmor_path_truncate(const struct path *path)
  271. {
  272. return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
  273. }
  274. static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
  275. const char *old_name)
  276. {
  277. return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
  278. S_IFLNK);
  279. }
  280. static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
  281. struct dentry *new_dentry)
  282. {
  283. struct aa_label *label;
  284. int error = 0;
  285. if (!path_mediated_fs(old_dentry))
  286. return 0;
  287. label = begin_current_label_crit_section();
  288. if (!unconfined(label))
  289. error = aa_path_link(label, old_dentry, new_dir, new_dentry);
  290. end_current_label_crit_section(label);
  291. return error;
  292. }
  293. static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  294. const struct path *new_dir, struct dentry *new_dentry)
  295. {
  296. struct aa_label *label;
  297. int error = 0;
  298. if (!path_mediated_fs(old_dentry))
  299. return 0;
  300. label = begin_current_label_crit_section();
  301. if (!unconfined(label)) {
  302. struct path old_path = { .mnt = old_dir->mnt,
  303. .dentry = old_dentry };
  304. struct path new_path = { .mnt = new_dir->mnt,
  305. .dentry = new_dentry };
  306. struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
  307. d_backing_inode(old_dentry)->i_mode
  308. };
  309. error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
  310. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  311. AA_MAY_SETATTR | AA_MAY_DELETE,
  312. &cond);
  313. if (!error)
  314. error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
  315. 0, MAY_WRITE | AA_MAY_SETATTR |
  316. AA_MAY_CREATE, &cond);
  317. }
  318. end_current_label_crit_section(label);
  319. return error;
  320. }
  321. static int apparmor_path_chmod(const struct path *path, umode_t mode)
  322. {
  323. return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
  324. }
  325. static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  326. {
  327. return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
  328. }
  329. static int apparmor_inode_getattr(const struct path *path)
  330. {
  331. return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
  332. }
  333. static int apparmor_file_open(struct file *file)
  334. {
  335. struct aa_file_ctx *fctx = file_ctx(file);
  336. struct aa_label *label;
  337. int error = 0;
  338. if (!path_mediated_fs(file->f_path.dentry))
  339. return 0;
  340. /* If in exec, permission is handled by bprm hooks.
  341. * Cache permissions granted by the previous exec check, with
  342. * implicit read and executable mmap which are required to
  343. * actually execute the image.
  344. */
  345. if (current->in_execve) {
  346. fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
  347. return 0;
  348. }
  349. label = aa_get_newest_cred_label(file->f_cred);
  350. if (!unconfined(label)) {
  351. struct inode *inode = file_inode(file);
  352. struct path_cond cond = { inode->i_uid, inode->i_mode };
  353. error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
  354. aa_map_file_to_perms(file), &cond);
  355. /* todo cache full allowed permissions set and state */
  356. fctx->allow = aa_map_file_to_perms(file);
  357. }
  358. aa_put_label(label);
  359. return error;
  360. }
  361. static int apparmor_file_alloc_security(struct file *file)
  362. {
  363. int error = 0;
  364. /* freed by apparmor_file_free_security */
  365. struct aa_label *label = begin_current_label_crit_section();
  366. file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
  367. if (!file_ctx(file))
  368. error = -ENOMEM;
  369. end_current_label_crit_section(label);
  370. return error;
  371. }
  372. static void apparmor_file_free_security(struct file *file)
  373. {
  374. aa_free_file_ctx(file_ctx(file));
  375. }
  376. static int common_file_perm(const char *op, struct file *file, u32 mask)
  377. {
  378. struct aa_label *label;
  379. int error = 0;
  380. /* don't reaudit files closed during inheritance */
  381. if (file->f_path.dentry == aa_null.dentry)
  382. return -EACCES;
  383. label = __begin_current_label_crit_section();
  384. error = aa_file_perm(op, label, file, mask);
  385. __end_current_label_crit_section(label);
  386. return error;
  387. }
  388. static int apparmor_file_receive(struct file *file)
  389. {
  390. return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
  391. }
  392. static int apparmor_file_permission(struct file *file, int mask)
  393. {
  394. return common_file_perm(OP_FPERM, file, mask);
  395. }
  396. static int apparmor_file_lock(struct file *file, unsigned int cmd)
  397. {
  398. u32 mask = AA_MAY_LOCK;
  399. if (cmd == F_WRLCK)
  400. mask |= MAY_WRITE;
  401. return common_file_perm(OP_FLOCK, file, mask);
  402. }
  403. static int common_mmap(const char *op, struct file *file, unsigned long prot,
  404. unsigned long flags)
  405. {
  406. int mask = 0;
  407. if (!file || !file_ctx(file))
  408. return 0;
  409. if (prot & PROT_READ)
  410. mask |= MAY_READ;
  411. /*
  412. * Private mappings don't require write perms since they don't
  413. * write back to the files
  414. */
  415. if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
  416. mask |= MAY_WRITE;
  417. if (prot & PROT_EXEC)
  418. mask |= AA_EXEC_MMAP;
  419. return common_file_perm(op, file, mask);
  420. }
  421. static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
  422. unsigned long prot, unsigned long flags)
  423. {
  424. return common_mmap(OP_FMMAP, file, prot, flags);
  425. }
  426. static int apparmor_file_mprotect(struct vm_area_struct *vma,
  427. unsigned long reqprot, unsigned long prot)
  428. {
  429. return common_mmap(OP_FMPROT, vma->vm_file, prot,
  430. !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
  431. }
  432. static int apparmor_sb_mount(const char *dev_name, const struct path *path,
  433. const char *type, unsigned long flags, void *data)
  434. {
  435. struct aa_label *label;
  436. int error = 0;
  437. /* Discard magic */
  438. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  439. flags &= ~MS_MGC_MSK;
  440. flags &= ~AA_MS_IGNORE_MASK;
  441. label = __begin_current_label_crit_section();
  442. if (!unconfined(label)) {
  443. if (flags & MS_REMOUNT)
  444. error = aa_remount(label, path, flags, data);
  445. else if (flags & MS_BIND)
  446. error = aa_bind_mount(label, path, dev_name, flags);
  447. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
  448. MS_UNBINDABLE))
  449. error = aa_mount_change_type(label, path, flags);
  450. else if (flags & MS_MOVE)
  451. error = aa_move_mount(label, path, dev_name);
  452. else
  453. error = aa_new_mount(label, dev_name, path, type,
  454. flags, data);
  455. }
  456. __end_current_label_crit_section(label);
  457. return error;
  458. }
  459. static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
  460. {
  461. struct aa_label *label;
  462. int error = 0;
  463. label = __begin_current_label_crit_section();
  464. if (!unconfined(label))
  465. error = aa_umount(label, mnt, flags);
  466. __end_current_label_crit_section(label);
  467. return error;
  468. }
  469. static int apparmor_sb_pivotroot(const struct path *old_path,
  470. const struct path *new_path)
  471. {
  472. struct aa_label *label;
  473. int error = 0;
  474. label = aa_get_current_label();
  475. if (!unconfined(label))
  476. error = aa_pivotroot(label, old_path, new_path);
  477. aa_put_label(label);
  478. return error;
  479. }
  480. static int apparmor_getprocattr(struct task_struct *task, char *name,
  481. char **value)
  482. {
  483. int error = -ENOENT;
  484. /* released below */
  485. const struct cred *cred = get_task_cred(task);
  486. struct aa_task_ctx *ctx = task_ctx(current);
  487. struct aa_label *label = NULL;
  488. if (strcmp(name, "current") == 0)
  489. label = aa_get_newest_label(cred_label(cred));
  490. else if (strcmp(name, "prev") == 0 && ctx->previous)
  491. label = aa_get_newest_label(ctx->previous);
  492. else if (strcmp(name, "exec") == 0 && ctx->onexec)
  493. label = aa_get_newest_label(ctx->onexec);
  494. else
  495. error = -EINVAL;
  496. if (label)
  497. error = aa_getprocattr(label, value);
  498. aa_put_label(label);
  499. put_cred(cred);
  500. return error;
  501. }
  502. static int apparmor_setprocattr(const char *name, void *value,
  503. size_t size)
  504. {
  505. char *command, *largs = NULL, *args = value;
  506. size_t arg_size;
  507. int error;
  508. DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
  509. if (size == 0)
  510. return -EINVAL;
  511. /* AppArmor requires that the buffer must be null terminated atm */
  512. if (args[size - 1] != '\0') {
  513. /* null terminate */
  514. largs = args = kmalloc(size + 1, GFP_KERNEL);
  515. if (!args)
  516. return -ENOMEM;
  517. memcpy(args, value, size);
  518. args[size] = '\0';
  519. }
  520. error = -EINVAL;
  521. args = strim(args);
  522. command = strsep(&args, " ");
  523. if (!args)
  524. goto out;
  525. args = skip_spaces(args);
  526. if (!*args)
  527. goto out;
  528. arg_size = size - (args - (largs ? largs : (char *) value));
  529. if (strcmp(name, "current") == 0) {
  530. if (strcmp(command, "changehat") == 0) {
  531. error = aa_setprocattr_changehat(args, arg_size,
  532. AA_CHANGE_NOFLAGS);
  533. } else if (strcmp(command, "permhat") == 0) {
  534. error = aa_setprocattr_changehat(args, arg_size,
  535. AA_CHANGE_TEST);
  536. } else if (strcmp(command, "changeprofile") == 0) {
  537. error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
  538. } else if (strcmp(command, "permprofile") == 0) {
  539. error = aa_change_profile(args, AA_CHANGE_TEST);
  540. } else if (strcmp(command, "stack") == 0) {
  541. error = aa_change_profile(args, AA_CHANGE_STACK);
  542. } else
  543. goto fail;
  544. } else if (strcmp(name, "exec") == 0) {
  545. if (strcmp(command, "exec") == 0)
  546. error = aa_change_profile(args, AA_CHANGE_ONEXEC);
  547. else if (strcmp(command, "stack") == 0)
  548. error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
  549. AA_CHANGE_STACK));
  550. else
  551. goto fail;
  552. } else
  553. /* only support the "current" and "exec" process attributes */
  554. goto fail;
  555. if (!error)
  556. error = size;
  557. out:
  558. kfree(largs);
  559. return error;
  560. fail:
  561. aad(&sa)->label = begin_current_label_crit_section();
  562. aad(&sa)->info = name;
  563. aad(&sa)->error = error = -EINVAL;
  564. aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
  565. end_current_label_crit_section(aad(&sa)->label);
  566. goto out;
  567. }
  568. /**
  569. * apparmor_bprm_committing_creds - do task cleanup on committing new creds
  570. * @bprm: binprm for the exec (NOT NULL)
  571. */
  572. static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
  573. {
  574. struct aa_label *label = aa_current_raw_label();
  575. struct aa_label *new_label = cred_label(bprm->cred);
  576. /* bail out if unconfined or not changing profile */
  577. if ((new_label->proxy == label->proxy) ||
  578. (unconfined(new_label)))
  579. return;
  580. aa_inherit_files(bprm->cred, current->files);
  581. current->pdeath_signal = 0;
  582. /* reset soft limits and set hard limits for the new label */
  583. __aa_transition_rlimits(label, new_label);
  584. }
  585. /**
  586. * apparmor_bprm_committed_cred - do cleanup after new creds committed
  587. * @bprm: binprm for the exec (NOT NULL)
  588. */
  589. static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
  590. {
  591. /* clear out temporary/transitional state from the context */
  592. aa_clear_task_ctx_trans(task_ctx(current));
  593. return;
  594. }
  595. static void apparmor_task_getsecid(struct task_struct *p, u32 *secid)
  596. {
  597. struct aa_label *label = aa_get_task_label(p);
  598. *secid = label->secid;
  599. aa_put_label(label);
  600. }
  601. static int apparmor_task_setrlimit(struct task_struct *task,
  602. unsigned int resource, struct rlimit *new_rlim)
  603. {
  604. struct aa_label *label = __begin_current_label_crit_section();
  605. int error = 0;
  606. if (!unconfined(label))
  607. error = aa_task_setrlimit(label, task, resource, new_rlim);
  608. __end_current_label_crit_section(label);
  609. return error;
  610. }
  611. static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
  612. int sig, const struct cred *cred)
  613. {
  614. struct aa_label *cl, *tl;
  615. int error;
  616. if (cred) {
  617. /*
  618. * Dealing with USB IO specific behavior
  619. */
  620. cl = aa_get_newest_cred_label(cred);
  621. tl = aa_get_task_label(target);
  622. error = aa_may_signal(cl, tl, sig);
  623. aa_put_label(cl);
  624. aa_put_label(tl);
  625. return error;
  626. }
  627. cl = __begin_current_label_crit_section();
  628. tl = aa_get_task_label(target);
  629. error = aa_may_signal(cl, tl, sig);
  630. aa_put_label(tl);
  631. __end_current_label_crit_section(cl);
  632. return error;
  633. }
  634. /**
  635. * apparmor_sk_alloc_security - allocate and attach the sk_security field
  636. */
  637. static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
  638. {
  639. struct aa_sk_ctx *ctx;
  640. ctx = kzalloc(sizeof(*ctx), flags);
  641. if (!ctx)
  642. return -ENOMEM;
  643. SK_CTX(sk) = ctx;
  644. return 0;
  645. }
  646. /**
  647. * apparmor_sk_free_security - free the sk_security field
  648. */
  649. static void apparmor_sk_free_security(struct sock *sk)
  650. {
  651. struct aa_sk_ctx *ctx = SK_CTX(sk);
  652. SK_CTX(sk) = NULL;
  653. aa_put_label(ctx->label);
  654. aa_put_label(ctx->peer);
  655. kfree(ctx);
  656. }
  657. /**
  658. * apparmor_clone_security - clone the sk_security field
  659. */
  660. static void apparmor_sk_clone_security(const struct sock *sk,
  661. struct sock *newsk)
  662. {
  663. struct aa_sk_ctx *ctx = SK_CTX(sk);
  664. struct aa_sk_ctx *new = SK_CTX(newsk);
  665. new->label = aa_get_label(ctx->label);
  666. new->peer = aa_get_label(ctx->peer);
  667. }
  668. /**
  669. * apparmor_socket_create - check perms before creating a new socket
  670. */
  671. static int apparmor_socket_create(int family, int type, int protocol, int kern)
  672. {
  673. struct aa_label *label;
  674. int error = 0;
  675. AA_BUG(in_interrupt());
  676. label = begin_current_label_crit_section();
  677. if (!(kern || unconfined(label)))
  678. error = af_select(family,
  679. create_perm(label, family, type, protocol),
  680. aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
  681. family, type, protocol));
  682. end_current_label_crit_section(label);
  683. return error;
  684. }
  685. /**
  686. * apparmor_socket_post_create - setup the per-socket security struct
  687. *
  688. * Note:
  689. * - kernel sockets currently labeled unconfined but we may want to
  690. * move to a special kernel label
  691. * - socket may not have sk here if created with sock_create_lite or
  692. * sock_alloc. These should be accept cases which will be handled in
  693. * sock_graft.
  694. */
  695. static int apparmor_socket_post_create(struct socket *sock, int family,
  696. int type, int protocol, int kern)
  697. {
  698. struct aa_label *label;
  699. if (kern) {
  700. struct aa_ns *ns = aa_get_current_ns();
  701. label = aa_get_label(ns_unconfined(ns));
  702. aa_put_ns(ns);
  703. } else
  704. label = aa_get_current_label();
  705. if (sock->sk) {
  706. struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
  707. aa_put_label(ctx->label);
  708. ctx->label = aa_get_label(label);
  709. }
  710. aa_put_label(label);
  711. return 0;
  712. }
  713. /**
  714. * apparmor_socket_bind - check perms before bind addr to socket
  715. */
  716. static int apparmor_socket_bind(struct socket *sock,
  717. struct sockaddr *address, int addrlen)
  718. {
  719. AA_BUG(!sock);
  720. AA_BUG(!sock->sk);
  721. AA_BUG(!address);
  722. AA_BUG(in_interrupt());
  723. return af_select(sock->sk->sk_family,
  724. bind_perm(sock, address, addrlen),
  725. aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
  726. }
  727. /**
  728. * apparmor_socket_connect - check perms before connecting @sock to @address
  729. */
  730. static int apparmor_socket_connect(struct socket *sock,
  731. struct sockaddr *address, int addrlen)
  732. {
  733. AA_BUG(!sock);
  734. AA_BUG(!sock->sk);
  735. AA_BUG(!address);
  736. AA_BUG(in_interrupt());
  737. return af_select(sock->sk->sk_family,
  738. connect_perm(sock, address, addrlen),
  739. aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
  740. }
  741. /**
  742. * apparmor_socket_list - check perms before allowing listen
  743. */
  744. static int apparmor_socket_listen(struct socket *sock, int backlog)
  745. {
  746. AA_BUG(!sock);
  747. AA_BUG(!sock->sk);
  748. AA_BUG(in_interrupt());
  749. return af_select(sock->sk->sk_family,
  750. listen_perm(sock, backlog),
  751. aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
  752. }
  753. /**
  754. * apparmor_socket_accept - check perms before accepting a new connection.
  755. *
  756. * Note: while @newsock is created and has some information, the accept
  757. * has not been done.
  758. */
  759. static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
  760. {
  761. AA_BUG(!sock);
  762. AA_BUG(!sock->sk);
  763. AA_BUG(!newsock);
  764. AA_BUG(in_interrupt());
  765. return af_select(sock->sk->sk_family,
  766. accept_perm(sock, newsock),
  767. aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
  768. }
  769. static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
  770. struct msghdr *msg, int size)
  771. {
  772. AA_BUG(!sock);
  773. AA_BUG(!sock->sk);
  774. AA_BUG(!msg);
  775. AA_BUG(in_interrupt());
  776. return af_select(sock->sk->sk_family,
  777. msg_perm(op, request, sock, msg, size),
  778. aa_sk_perm(op, request, sock->sk));
  779. }
  780. /**
  781. * apparmor_socket_sendmsg - check perms before sending msg to another socket
  782. */
  783. static int apparmor_socket_sendmsg(struct socket *sock,
  784. struct msghdr *msg, int size)
  785. {
  786. return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
  787. }
  788. /**
  789. * apparmor_socket_recvmsg - check perms before receiving a message
  790. */
  791. static int apparmor_socket_recvmsg(struct socket *sock,
  792. struct msghdr *msg, int size, int flags)
  793. {
  794. return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
  795. }
  796. /* revaliation, get/set attr, shutdown */
  797. static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
  798. {
  799. AA_BUG(!sock);
  800. AA_BUG(!sock->sk);
  801. AA_BUG(in_interrupt());
  802. return af_select(sock->sk->sk_family,
  803. sock_perm(op, request, sock),
  804. aa_sk_perm(op, request, sock->sk));
  805. }
  806. /**
  807. * apparmor_socket_getsockname - check perms before getting the local address
  808. */
  809. static int apparmor_socket_getsockname(struct socket *sock)
  810. {
  811. return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
  812. }
  813. /**
  814. * apparmor_socket_getpeername - check perms before getting remote address
  815. */
  816. static int apparmor_socket_getpeername(struct socket *sock)
  817. {
  818. return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
  819. }
  820. /* revaliation, get/set attr, opt */
  821. static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
  822. int level, int optname)
  823. {
  824. AA_BUG(!sock);
  825. AA_BUG(!sock->sk);
  826. AA_BUG(in_interrupt());
  827. return af_select(sock->sk->sk_family,
  828. opt_perm(op, request, sock, level, optname),
  829. aa_sk_perm(op, request, sock->sk));
  830. }
  831. /**
  832. * apparmor_getsockopt - check perms before getting socket options
  833. */
  834. static int apparmor_socket_getsockopt(struct socket *sock, int level,
  835. int optname)
  836. {
  837. return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
  838. level, optname);
  839. }
  840. /**
  841. * apparmor_setsockopt - check perms before setting socket options
  842. */
  843. static int apparmor_socket_setsockopt(struct socket *sock, int level,
  844. int optname)
  845. {
  846. return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
  847. level, optname);
  848. }
  849. /**
  850. * apparmor_socket_shutdown - check perms before shutting down @sock conn
  851. */
  852. static int apparmor_socket_shutdown(struct socket *sock, int how)
  853. {
  854. return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
  855. }
  856. /**
  857. * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
  858. *
  859. * Note: can not sleep may be called with locks held
  860. *
  861. * dont want protocol specific in __skb_recv_datagram()
  862. * to deny an incoming connection socket_sock_rcv_skb()
  863. */
  864. static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  865. {
  866. return 0;
  867. }
  868. static struct aa_label *sk_peer_label(struct sock *sk)
  869. {
  870. struct aa_sk_ctx *ctx = SK_CTX(sk);
  871. if (ctx->peer)
  872. return ctx->peer;
  873. return ERR_PTR(-ENOPROTOOPT);
  874. }
  875. /**
  876. * apparmor_socket_getpeersec_stream - get security context of peer
  877. *
  878. * Note: for tcp only valid if using ipsec or cipso on lan
  879. */
  880. static int apparmor_socket_getpeersec_stream(struct socket *sock,
  881. char __user *optval,
  882. int __user *optlen,
  883. unsigned int len)
  884. {
  885. char *name;
  886. int slen, error = 0;
  887. struct aa_label *label;
  888. struct aa_label *peer;
  889. label = begin_current_label_crit_section();
  890. peer = sk_peer_label(sock->sk);
  891. if (IS_ERR(peer)) {
  892. error = PTR_ERR(peer);
  893. goto done;
  894. }
  895. slen = aa_label_asxprint(&name, labels_ns(label), peer,
  896. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  897. FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
  898. /* don't include terminating \0 in slen, it breaks some apps */
  899. if (slen < 0) {
  900. error = -ENOMEM;
  901. } else {
  902. if (slen > len) {
  903. error = -ERANGE;
  904. } else if (copy_to_user(optval, name, slen)) {
  905. error = -EFAULT;
  906. goto out;
  907. }
  908. if (put_user(slen, optlen))
  909. error = -EFAULT;
  910. out:
  911. kfree(name);
  912. }
  913. done:
  914. end_current_label_crit_section(label);
  915. return error;
  916. }
  917. /**
  918. * apparmor_socket_getpeersec_dgram - get security label of packet
  919. * @sock: the peer socket
  920. * @skb: packet data
  921. * @secid: pointer to where to put the secid of the packet
  922. *
  923. * Sets the netlabel socket state on sk from parent
  924. */
  925. static int apparmor_socket_getpeersec_dgram(struct socket *sock,
  926. struct sk_buff *skb, u32 *secid)
  927. {
  928. /* TODO: requires secid support */
  929. return -ENOPROTOOPT;
  930. }
  931. /**
  932. * apparmor_sock_graft - Initialize newly created socket
  933. * @sk: child sock
  934. * @parent: parent socket
  935. *
  936. * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
  937. * just set sk security information off of current creating process label
  938. * Labeling of sk for accept case - probably should be sock based
  939. * instead of task, because of the case where an implicitly labeled
  940. * socket is shared by different tasks.
  941. */
  942. static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
  943. {
  944. struct aa_sk_ctx *ctx = SK_CTX(sk);
  945. if (!ctx->label)
  946. ctx->label = aa_get_current_label();
  947. }
  948. static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
  949. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  950. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  951. LSM_HOOK_INIT(capget, apparmor_capget),
  952. LSM_HOOK_INIT(capable, apparmor_capable),
  953. LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
  954. LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
  955. LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
  956. LSM_HOOK_INIT(path_link, apparmor_path_link),
  957. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  958. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  959. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  960. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  961. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  962. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  963. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  964. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  965. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  966. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  967. LSM_HOOK_INIT(file_open, apparmor_file_open),
  968. LSM_HOOK_INIT(file_receive, apparmor_file_receive),
  969. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  970. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  971. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  972. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  973. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  974. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  975. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  976. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  977. LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
  978. LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
  979. LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
  980. LSM_HOOK_INIT(socket_create, apparmor_socket_create),
  981. LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
  982. LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
  983. LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
  984. LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
  985. LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
  986. LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
  987. LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
  988. LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
  989. LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
  990. LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
  991. LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
  992. LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
  993. LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
  994. LSM_HOOK_INIT(socket_getpeersec_stream,
  995. apparmor_socket_getpeersec_stream),
  996. LSM_HOOK_INIT(socket_getpeersec_dgram,
  997. apparmor_socket_getpeersec_dgram),
  998. LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
  999. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  1000. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  1001. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  1002. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  1003. LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
  1004. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  1005. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  1006. LSM_HOOK_INIT(task_free, apparmor_task_free),
  1007. LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
  1008. LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid),
  1009. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  1010. LSM_HOOK_INIT(task_kill, apparmor_task_kill),
  1011. #ifdef CONFIG_AUDIT
  1012. LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
  1013. LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
  1014. LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
  1015. LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
  1016. #endif
  1017. LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
  1018. LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
  1019. LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
  1020. };
  1021. /*
  1022. * AppArmor sysfs module parameters
  1023. */
  1024. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  1025. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  1026. #define param_check_aabool param_check_bool
  1027. static const struct kernel_param_ops param_ops_aabool = {
  1028. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1029. .set = param_set_aabool,
  1030. .get = param_get_aabool
  1031. };
  1032. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  1033. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  1034. #define param_check_aauint param_check_uint
  1035. static const struct kernel_param_ops param_ops_aauint = {
  1036. .set = param_set_aauint,
  1037. .get = param_get_aauint
  1038. };
  1039. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  1040. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  1041. #define param_check_aalockpolicy param_check_bool
  1042. static const struct kernel_param_ops param_ops_aalockpolicy = {
  1043. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1044. .set = param_set_aalockpolicy,
  1045. .get = param_get_aalockpolicy
  1046. };
  1047. static int param_set_audit(const char *val, const struct kernel_param *kp);
  1048. static int param_get_audit(char *buffer, const struct kernel_param *kp);
  1049. static int param_set_mode(const char *val, const struct kernel_param *kp);
  1050. static int param_get_mode(char *buffer, const struct kernel_param *kp);
  1051. /* Flag values, also controllable via /sys/module/apparmor/parameters
  1052. * We define special types as we want to do additional mediation.
  1053. */
  1054. /* AppArmor global enforcement switch - complain, enforce, kill */
  1055. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  1056. module_param_call(mode, param_set_mode, param_get_mode,
  1057. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  1058. /* whether policy verification hashing is enabled */
  1059. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  1060. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  1061. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  1062. #endif
  1063. /* Debug mode */
  1064. bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
  1065. module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
  1066. /* Audit mode */
  1067. enum audit_mode aa_g_audit;
  1068. module_param_call(audit, param_set_audit, param_get_audit,
  1069. &aa_g_audit, S_IRUSR | S_IWUSR);
  1070. /* Determines if audit header is included in audited messages. This
  1071. * provides more context if the audit daemon is not running
  1072. */
  1073. bool aa_g_audit_header = true;
  1074. module_param_named(audit_header, aa_g_audit_header, aabool,
  1075. S_IRUSR | S_IWUSR);
  1076. /* lock out loading/removal of policy
  1077. * TODO: add in at boot loading of policy, which is the only way to
  1078. * load policy, if lock_policy is set
  1079. */
  1080. bool aa_g_lock_policy;
  1081. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  1082. S_IRUSR | S_IWUSR);
  1083. /* Syscall logging mode */
  1084. bool aa_g_logsyscall;
  1085. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  1086. /* Maximum pathname length before accesses will start getting rejected */
  1087. unsigned int aa_g_path_max = 2 * PATH_MAX;
  1088. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  1089. /* Determines how paranoid loading of policy is and how much verification
  1090. * on the loaded policy is done.
  1091. * DEPRECATED: read only as strict checking of load is always done now
  1092. * that none root users (user namespaces) can load policy.
  1093. */
  1094. bool aa_g_paranoid_load = true;
  1095. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  1096. /* Boot time disable flag */
  1097. static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
  1098. module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
  1099. static int __init apparmor_enabled_setup(char *str)
  1100. {
  1101. unsigned long enabled;
  1102. int error = kstrtoul(str, 0, &enabled);
  1103. if (!error)
  1104. apparmor_enabled = enabled ? 1 : 0;
  1105. return 1;
  1106. }
  1107. __setup("apparmor=", apparmor_enabled_setup);
  1108. /* set global flag turning off the ability to load policy */
  1109. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  1110. {
  1111. if (!apparmor_enabled)
  1112. return -EINVAL;
  1113. if (apparmor_initialized && !policy_admin_capable(NULL))
  1114. return -EPERM;
  1115. return param_set_bool(val, kp);
  1116. }
  1117. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  1118. {
  1119. if (!apparmor_enabled)
  1120. return -EINVAL;
  1121. if (apparmor_initialized && !policy_view_capable(NULL))
  1122. return -EPERM;
  1123. return param_get_bool(buffer, kp);
  1124. }
  1125. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  1126. {
  1127. if (!apparmor_enabled)
  1128. return -EINVAL;
  1129. if (apparmor_initialized && !policy_admin_capable(NULL))
  1130. return -EPERM;
  1131. return param_set_bool(val, kp);
  1132. }
  1133. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  1134. {
  1135. if (!apparmor_enabled)
  1136. return -EINVAL;
  1137. if (apparmor_initialized && !policy_view_capable(NULL))
  1138. return -EPERM;
  1139. return param_get_bool(buffer, kp);
  1140. }
  1141. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  1142. {
  1143. int error;
  1144. if (!apparmor_enabled)
  1145. return -EINVAL;
  1146. /* file is ro but enforce 2nd line check */
  1147. if (apparmor_initialized)
  1148. return -EPERM;
  1149. error = param_set_uint(val, kp);
  1150. pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
  1151. return error;
  1152. }
  1153. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  1154. {
  1155. if (!apparmor_enabled)
  1156. return -EINVAL;
  1157. if (apparmor_initialized && !policy_view_capable(NULL))
  1158. return -EPERM;
  1159. return param_get_uint(buffer, kp);
  1160. }
  1161. static int param_get_audit(char *buffer, const struct kernel_param *kp)
  1162. {
  1163. if (!apparmor_enabled)
  1164. return -EINVAL;
  1165. if (apparmor_initialized && !policy_view_capable(NULL))
  1166. return -EPERM;
  1167. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  1168. }
  1169. static int param_set_audit(const char *val, const struct kernel_param *kp)
  1170. {
  1171. int i;
  1172. if (!apparmor_enabled)
  1173. return -EINVAL;
  1174. if (!val)
  1175. return -EINVAL;
  1176. if (apparmor_initialized && !policy_admin_capable(NULL))
  1177. return -EPERM;
  1178. i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
  1179. if (i < 0)
  1180. return -EINVAL;
  1181. aa_g_audit = i;
  1182. return 0;
  1183. }
  1184. static int param_get_mode(char *buffer, const struct kernel_param *kp)
  1185. {
  1186. if (!apparmor_enabled)
  1187. return -EINVAL;
  1188. if (apparmor_initialized && !policy_view_capable(NULL))
  1189. return -EPERM;
  1190. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  1191. }
  1192. static int param_set_mode(const char *val, const struct kernel_param *kp)
  1193. {
  1194. int i;
  1195. if (!apparmor_enabled)
  1196. return -EINVAL;
  1197. if (!val)
  1198. return -EINVAL;
  1199. if (apparmor_initialized && !policy_admin_capable(NULL))
  1200. return -EPERM;
  1201. i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
  1202. val);
  1203. if (i < 0)
  1204. return -EINVAL;
  1205. aa_g_profile_mode = i;
  1206. return 0;
  1207. }
  1208. /*
  1209. * AppArmor init functions
  1210. */
  1211. /**
  1212. * set_init_ctx - set a task context and profile on the first task.
  1213. *
  1214. * TODO: allow setting an alternate profile than unconfined
  1215. */
  1216. static int __init set_init_ctx(void)
  1217. {
  1218. struct cred *cred = (struct cred *)current->real_cred;
  1219. struct aa_task_ctx *ctx;
  1220. ctx = aa_alloc_task_ctx(GFP_KERNEL);
  1221. if (!ctx)
  1222. return -ENOMEM;
  1223. cred_label(cred) = aa_get_label(ns_unconfined(root_ns));
  1224. task_ctx(current) = ctx;
  1225. return 0;
  1226. }
  1227. static void destroy_buffers(void)
  1228. {
  1229. u32 i, j;
  1230. for_each_possible_cpu(i) {
  1231. for_each_cpu_buffer(j) {
  1232. kfree(per_cpu(aa_buffers, i).buf[j]);
  1233. per_cpu(aa_buffers, i).buf[j] = NULL;
  1234. }
  1235. }
  1236. }
  1237. static int __init alloc_buffers(void)
  1238. {
  1239. u32 i, j;
  1240. for_each_possible_cpu(i) {
  1241. for_each_cpu_buffer(j) {
  1242. char *buffer;
  1243. if (cpu_to_node(i) > num_online_nodes())
  1244. /* fallback to kmalloc for offline nodes */
  1245. buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
  1246. else
  1247. buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
  1248. cpu_to_node(i));
  1249. if (!buffer) {
  1250. destroy_buffers();
  1251. return -ENOMEM;
  1252. }
  1253. per_cpu(aa_buffers, i).buf[j] = buffer;
  1254. }
  1255. }
  1256. return 0;
  1257. }
  1258. #ifdef CONFIG_SYSCTL
  1259. static int apparmor_dointvec(struct ctl_table *table, int write,
  1260. void __user *buffer, size_t *lenp, loff_t *ppos)
  1261. {
  1262. if (!policy_admin_capable(NULL))
  1263. return -EPERM;
  1264. if (!apparmor_enabled)
  1265. return -EINVAL;
  1266. return proc_dointvec(table, write, buffer, lenp, ppos);
  1267. }
  1268. static struct ctl_path apparmor_sysctl_path[] = {
  1269. { .procname = "kernel", },
  1270. { }
  1271. };
  1272. static struct ctl_table apparmor_sysctl_table[] = {
  1273. {
  1274. .procname = "unprivileged_userns_apparmor_policy",
  1275. .data = &unprivileged_userns_apparmor_policy,
  1276. .maxlen = sizeof(int),
  1277. .mode = 0600,
  1278. .proc_handler = apparmor_dointvec,
  1279. },
  1280. { }
  1281. };
  1282. static int __init apparmor_init_sysctl(void)
  1283. {
  1284. return register_sysctl_paths(apparmor_sysctl_path,
  1285. apparmor_sysctl_table) ? 0 : -ENOMEM;
  1286. }
  1287. #else
  1288. static inline int apparmor_init_sysctl(void)
  1289. {
  1290. return 0;
  1291. }
  1292. #endif /* CONFIG_SYSCTL */
  1293. static int __init apparmor_init(void)
  1294. {
  1295. int error;
  1296. if (!apparmor_enabled || !security_module_enable("apparmor")) {
  1297. aa_info_message("AppArmor disabled by boot time parameter");
  1298. apparmor_enabled = false;
  1299. return 0;
  1300. }
  1301. aa_secids_init();
  1302. error = aa_setup_dfa_engine();
  1303. if (error) {
  1304. AA_ERROR("Unable to setup dfa engine\n");
  1305. goto alloc_out;
  1306. }
  1307. error = aa_alloc_root_ns();
  1308. if (error) {
  1309. AA_ERROR("Unable to allocate default profile namespace\n");
  1310. goto alloc_out;
  1311. }
  1312. error = apparmor_init_sysctl();
  1313. if (error) {
  1314. AA_ERROR("Unable to register sysctls\n");
  1315. goto alloc_out;
  1316. }
  1317. error = alloc_buffers();
  1318. if (error) {
  1319. AA_ERROR("Unable to allocate work buffers\n");
  1320. goto buffers_out;
  1321. }
  1322. error = set_init_ctx();
  1323. if (error) {
  1324. AA_ERROR("Failed to set context on init task\n");
  1325. aa_free_root_ns();
  1326. goto buffers_out;
  1327. }
  1328. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  1329. "apparmor");
  1330. /* Report that AppArmor successfully initialized */
  1331. apparmor_initialized = 1;
  1332. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  1333. aa_info_message("AppArmor initialized: complain mode enabled");
  1334. else if (aa_g_profile_mode == APPARMOR_KILL)
  1335. aa_info_message("AppArmor initialized: kill mode enabled");
  1336. else
  1337. aa_info_message("AppArmor initialized");
  1338. return error;
  1339. buffers_out:
  1340. destroy_buffers();
  1341. alloc_out:
  1342. aa_destroy_aafs();
  1343. aa_teardown_dfa_engine();
  1344. apparmor_enabled = false;
  1345. return error;
  1346. }
  1347. DEFINE_LSM(apparmor) = {
  1348. .name = "apparmor",
  1349. .init = apparmor_init,
  1350. };