lsm.c 44 KB

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