lsm.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor LSM hooks.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #include <linux/lsm_hooks.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/mount.h>
  15. #include <linux/namei.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/ctype.h>
  18. #include <linux/sysctl.h>
  19. #include <linux/audit.h>
  20. #include <linux/user_namespace.h>
  21. #include <linux/netfilter_ipv4.h>
  22. #include <linux/netfilter_ipv6.h>
  23. #include <linux/zstd.h>
  24. #include <net/sock.h>
  25. #include <uapi/linux/mount.h>
  26. #include <uapi/linux/lsm.h>
  27. #include "include/apparmor.h"
  28. #include "include/apparmorfs.h"
  29. #include "include/audit.h"
  30. #include "include/capability.h"
  31. #include "include/cred.h"
  32. #include "include/file.h"
  33. #include "include/ipc.h"
  34. #include "include/net.h"
  35. #include "include/path.h"
  36. #include "include/label.h"
  37. #include "include/policy.h"
  38. #include "include/policy_ns.h"
  39. #include "include/procattr.h"
  40. #include "include/mount.h"
  41. #include "include/secid.h"
  42. /* Flag indicating whether initialization completed */
  43. int apparmor_initialized;
  44. union aa_buffer {
  45. struct list_head list;
  46. DECLARE_FLEX_ARRAY(char, buffer);
  47. };
  48. struct aa_local_cache {
  49. unsigned int hold;
  50. unsigned int count;
  51. struct list_head head;
  52. };
  53. #define RESERVE_COUNT 2
  54. static int reserve_count = RESERVE_COUNT;
  55. static int buffer_count;
  56. static LIST_HEAD(aa_global_buffers);
  57. static DEFINE_SPINLOCK(aa_buffers_lock);
  58. static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
  59. /*
  60. * LSM hook functions
  61. */
  62. /*
  63. * put the associated labels
  64. */
  65. static void apparmor_cred_free(struct cred *cred)
  66. {
  67. aa_put_label(cred_label(cred));
  68. set_cred_label(cred, NULL);
  69. }
  70. /*
  71. * allocate the apparmor part of blank credentials
  72. */
  73. static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
  74. {
  75. set_cred_label(cred, NULL);
  76. return 0;
  77. }
  78. /*
  79. * prepare new cred label for modification by prepare_cred block
  80. */
  81. static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
  82. gfp_t gfp)
  83. {
  84. set_cred_label(new, aa_get_newest_label(cred_label(old)));
  85. return 0;
  86. }
  87. /*
  88. * transfer the apparmor data to a blank set of creds
  89. */
  90. static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
  91. {
  92. set_cred_label(new, aa_get_newest_label(cred_label(old)));
  93. }
  94. static void apparmor_task_free(struct task_struct *task)
  95. {
  96. aa_free_task_ctx(task_ctx(task));
  97. }
  98. static int apparmor_task_alloc(struct task_struct *task,
  99. unsigned long clone_flags)
  100. {
  101. struct aa_task_ctx *new = task_ctx(task);
  102. aa_dup_task_ctx(new, task_ctx(current));
  103. return 0;
  104. }
  105. static int apparmor_ptrace_access_check(struct task_struct *child,
  106. unsigned int mode)
  107. {
  108. struct aa_label *tracer, *tracee;
  109. const struct cred *cred;
  110. int error;
  111. cred = get_task_cred(child);
  112. tracee = cred_label(cred); /* ref count on cred */
  113. tracer = __begin_current_label_crit_section();
  114. error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
  115. (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
  116. : AA_PTRACE_TRACE);
  117. __end_current_label_crit_section(tracer);
  118. put_cred(cred);
  119. return error;
  120. }
  121. static int apparmor_ptrace_traceme(struct task_struct *parent)
  122. {
  123. struct aa_label *tracer, *tracee;
  124. const struct cred *cred;
  125. int error;
  126. tracee = __begin_current_label_crit_section();
  127. cred = get_task_cred(parent);
  128. tracer = cred_label(cred); /* ref count on cred */
  129. error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
  130. AA_PTRACE_TRACE);
  131. put_cred(cred);
  132. __end_current_label_crit_section(tracee);
  133. return error;
  134. }
  135. /* Derived from security/commoncap.c:cap_capget */
  136. static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
  137. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  138. {
  139. struct aa_label *label;
  140. const struct cred *cred;
  141. rcu_read_lock();
  142. cred = __task_cred(target);
  143. label = aa_get_newest_cred_label(cred);
  144. /*
  145. * cap_capget is stacked ahead of this and will
  146. * initialize effective and permitted.
  147. */
  148. if (!unconfined(label)) {
  149. struct aa_profile *profile;
  150. struct label_it i;
  151. label_for_each_confined(i, label, profile) {
  152. struct aa_ruleset *rules;
  153. if (COMPLAIN_MODE(profile))
  154. continue;
  155. rules = list_first_entry(&profile->rules,
  156. typeof(*rules), list);
  157. *effective = cap_intersect(*effective,
  158. rules->caps.allow);
  159. *permitted = cap_intersect(*permitted,
  160. rules->caps.allow);
  161. }
  162. }
  163. rcu_read_unlock();
  164. aa_put_label(label);
  165. return 0;
  166. }
  167. static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
  168. int cap, unsigned int opts)
  169. {
  170. struct aa_label *label;
  171. int error = 0;
  172. label = aa_get_newest_cred_label(cred);
  173. if (!unconfined(label))
  174. error = aa_capable(cred, label, cap, opts);
  175. aa_put_label(label);
  176. return error;
  177. }
  178. /**
  179. * common_perm - basic common permission check wrapper fn for paths
  180. * @op: operation being checked
  181. * @path: path to check permission of (NOT NULL)
  182. * @mask: requested permissions mask
  183. * @cond: conditional info for the permission request (NOT NULL)
  184. *
  185. * Returns: %0 else error code if error or permission denied
  186. */
  187. static int common_perm(const char *op, const struct path *path, u32 mask,
  188. struct path_cond *cond)
  189. {
  190. struct aa_label *label;
  191. int error = 0;
  192. label = __begin_current_label_crit_section();
  193. if (!unconfined(label))
  194. error = aa_path_perm(op, current_cred(), label, path, 0, mask,
  195. cond);
  196. __end_current_label_crit_section(label);
  197. return error;
  198. }
  199. /**
  200. * common_perm_cond - common permission wrapper around inode cond
  201. * @op: operation being checked
  202. * @path: location to check (NOT NULL)
  203. * @mask: requested permissions mask
  204. *
  205. * Returns: %0 else error code if error or permission denied
  206. */
  207. static int common_perm_cond(const char *op, const struct path *path, u32 mask)
  208. {
  209. vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
  210. d_backing_inode(path->dentry));
  211. struct path_cond cond = {
  212. vfsuid_into_kuid(vfsuid),
  213. d_backing_inode(path->dentry)->i_mode
  214. };
  215. if (!path_mediated_fs(path->dentry))
  216. return 0;
  217. return common_perm(op, path, mask, &cond);
  218. }
  219. /**
  220. * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
  221. * @op: operation being checked
  222. * @dir: directory of the dentry (NOT NULL)
  223. * @dentry: dentry to check (NOT NULL)
  224. * @mask: requested permissions mask
  225. * @cond: conditional info for the permission request (NOT NULL)
  226. *
  227. * Returns: %0 else error code if error or permission denied
  228. */
  229. static int common_perm_dir_dentry(const char *op, const struct path *dir,
  230. struct dentry *dentry, u32 mask,
  231. struct path_cond *cond)
  232. {
  233. struct path path = { .mnt = dir->mnt, .dentry = dentry };
  234. return common_perm(op, &path, mask, cond);
  235. }
  236. /**
  237. * common_perm_rm - common permission wrapper for operations doing rm
  238. * @op: operation being checked
  239. * @dir: directory that the dentry is in (NOT NULL)
  240. * @dentry: dentry being rm'd (NOT NULL)
  241. * @mask: requested permission mask
  242. *
  243. * Returns: %0 else error code if error or permission denied
  244. */
  245. static int common_perm_rm(const char *op, const struct path *dir,
  246. struct dentry *dentry, u32 mask)
  247. {
  248. struct inode *inode = d_backing_inode(dentry);
  249. struct path_cond cond = { };
  250. vfsuid_t vfsuid;
  251. if (!inode || !path_mediated_fs(dentry))
  252. return 0;
  253. vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
  254. cond.uid = vfsuid_into_kuid(vfsuid);
  255. cond.mode = inode->i_mode;
  256. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  257. }
  258. /**
  259. * common_perm_create - common permission wrapper for operations doing create
  260. * @op: operation being checked
  261. * @dir: directory that dentry will be created in (NOT NULL)
  262. * @dentry: dentry to create (NOT NULL)
  263. * @mask: request permission mask
  264. * @mode: created file mode
  265. *
  266. * Returns: %0 else error code if error or permission denied
  267. */
  268. static int common_perm_create(const char *op, const struct path *dir,
  269. struct dentry *dentry, u32 mask, umode_t mode)
  270. {
  271. struct path_cond cond = { current_fsuid(), mode };
  272. if (!path_mediated_fs(dir->dentry))
  273. return 0;
  274. return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
  275. }
  276. static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
  277. {
  278. return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
  279. }
  280. static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
  281. umode_t mode)
  282. {
  283. return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
  284. S_IFDIR);
  285. }
  286. static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
  287. {
  288. return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
  289. }
  290. static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
  291. umode_t mode, unsigned int dev)
  292. {
  293. return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
  294. }
  295. static int apparmor_path_truncate(const struct path *path)
  296. {
  297. return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
  298. }
  299. static int apparmor_file_truncate(struct file *file)
  300. {
  301. return apparmor_path_truncate(&file->f_path);
  302. }
  303. static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
  304. const char *old_name)
  305. {
  306. return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
  307. S_IFLNK);
  308. }
  309. static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
  310. struct dentry *new_dentry)
  311. {
  312. struct aa_label *label;
  313. int error = 0;
  314. if (!path_mediated_fs(old_dentry))
  315. return 0;
  316. label = begin_current_label_crit_section();
  317. if (!unconfined(label))
  318. error = aa_path_link(current_cred(), label, old_dentry, new_dir,
  319. new_dentry);
  320. end_current_label_crit_section(label);
  321. return error;
  322. }
  323. static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
  324. const struct path *new_dir, struct dentry *new_dentry,
  325. const unsigned int flags)
  326. {
  327. struct aa_label *label;
  328. int error = 0;
  329. if (!path_mediated_fs(old_dentry))
  330. return 0;
  331. if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
  332. return 0;
  333. label = begin_current_label_crit_section();
  334. if (!unconfined(label)) {
  335. struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
  336. vfsuid_t vfsuid;
  337. struct path old_path = { .mnt = old_dir->mnt,
  338. .dentry = old_dentry };
  339. struct path new_path = { .mnt = new_dir->mnt,
  340. .dentry = new_dentry };
  341. struct path_cond cond = {
  342. .mode = d_backing_inode(old_dentry)->i_mode
  343. };
  344. vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
  345. cond.uid = vfsuid_into_kuid(vfsuid);
  346. if (flags & RENAME_EXCHANGE) {
  347. struct path_cond cond_exchange = {
  348. .mode = d_backing_inode(new_dentry)->i_mode,
  349. };
  350. vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
  351. cond_exchange.uid = vfsuid_into_kuid(vfsuid);
  352. error = aa_path_perm(OP_RENAME_SRC, current_cred(),
  353. label, &new_path, 0,
  354. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  355. AA_MAY_SETATTR | AA_MAY_DELETE,
  356. &cond_exchange);
  357. if (!error)
  358. error = aa_path_perm(OP_RENAME_DEST, current_cred(),
  359. label, &old_path,
  360. 0, MAY_WRITE | AA_MAY_SETATTR |
  361. AA_MAY_CREATE, &cond_exchange);
  362. }
  363. if (!error)
  364. error = aa_path_perm(OP_RENAME_SRC, current_cred(),
  365. label, &old_path, 0,
  366. MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
  367. AA_MAY_SETATTR | AA_MAY_DELETE,
  368. &cond);
  369. if (!error)
  370. error = aa_path_perm(OP_RENAME_DEST, current_cred(),
  371. label, &new_path,
  372. 0, MAY_WRITE | AA_MAY_SETATTR |
  373. AA_MAY_CREATE, &cond);
  374. }
  375. end_current_label_crit_section(label);
  376. return error;
  377. }
  378. static int apparmor_path_chmod(const struct path *path, umode_t mode)
  379. {
  380. return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
  381. }
  382. static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
  383. {
  384. return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
  385. }
  386. static int apparmor_inode_getattr(const struct path *path)
  387. {
  388. return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
  389. }
  390. static int apparmor_file_open(struct file *file)
  391. {
  392. struct aa_file_ctx *fctx = file_ctx(file);
  393. struct aa_label *label;
  394. int error = 0;
  395. bool needput;
  396. if (!path_mediated_fs(file->f_path.dentry))
  397. return 0;
  398. /* If in exec, permission is handled by bprm hooks.
  399. * Cache permissions granted by the previous exec check, with
  400. * implicit read and executable mmap which are required to
  401. * actually execute the image.
  402. *
  403. * Illogically, FMODE_EXEC is in f_flags, not f_mode.
  404. */
  405. if (file->f_flags & __FMODE_EXEC) {
  406. fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
  407. return 0;
  408. }
  409. label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
  410. if (!unconfined(label)) {
  411. struct mnt_idmap *idmap = file_mnt_idmap(file);
  412. struct inode *inode = file_inode(file);
  413. vfsuid_t vfsuid;
  414. struct path_cond cond = {
  415. .mode = inode->i_mode,
  416. };
  417. vfsuid = i_uid_into_vfsuid(idmap, inode);
  418. cond.uid = vfsuid_into_kuid(vfsuid);
  419. error = aa_path_perm(OP_OPEN, file->f_cred,
  420. label, &file->f_path, 0,
  421. aa_map_file_to_perms(file), &cond);
  422. /* todo cache full allowed permissions set and state */
  423. fctx->allow = aa_map_file_to_perms(file);
  424. }
  425. aa_put_label_condref(label, needput);
  426. return error;
  427. }
  428. static int apparmor_file_alloc_security(struct file *file)
  429. {
  430. struct aa_file_ctx *ctx = file_ctx(file);
  431. struct aa_label *label = begin_current_label_crit_section();
  432. spin_lock_init(&ctx->lock);
  433. rcu_assign_pointer(ctx->label, aa_get_label(label));
  434. end_current_label_crit_section(label);
  435. return 0;
  436. }
  437. static void apparmor_file_free_security(struct file *file)
  438. {
  439. struct aa_file_ctx *ctx = file_ctx(file);
  440. if (ctx)
  441. aa_put_label(rcu_access_pointer(ctx->label));
  442. }
  443. static int common_file_perm(const char *op, struct file *file, u32 mask,
  444. bool in_atomic)
  445. {
  446. struct aa_label *label;
  447. int error = 0;
  448. /* don't reaudit files closed during inheritance */
  449. if (file->f_path.dentry == aa_null.dentry)
  450. return -EACCES;
  451. label = __begin_current_label_crit_section();
  452. error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
  453. __end_current_label_crit_section(label);
  454. return error;
  455. }
  456. static int apparmor_file_receive(struct file *file)
  457. {
  458. return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
  459. false);
  460. }
  461. static int apparmor_file_permission(struct file *file, int mask)
  462. {
  463. return common_file_perm(OP_FPERM, file, mask, false);
  464. }
  465. static int apparmor_file_lock(struct file *file, unsigned int cmd)
  466. {
  467. u32 mask = AA_MAY_LOCK;
  468. if (cmd == F_WRLCK)
  469. mask |= MAY_WRITE;
  470. return common_file_perm(OP_FLOCK, file, mask, false);
  471. }
  472. static int common_mmap(const char *op, struct file *file, unsigned long prot,
  473. unsigned long flags, bool in_atomic)
  474. {
  475. int mask = 0;
  476. if (!file || !file_ctx(file))
  477. return 0;
  478. if (prot & PROT_READ)
  479. mask |= MAY_READ;
  480. /*
  481. * Private mappings don't require write perms since they don't
  482. * write back to the files
  483. */
  484. if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
  485. mask |= MAY_WRITE;
  486. if (prot & PROT_EXEC)
  487. mask |= AA_EXEC_MMAP;
  488. return common_file_perm(op, file, mask, in_atomic);
  489. }
  490. static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
  491. unsigned long prot, unsigned long flags)
  492. {
  493. return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
  494. }
  495. static int apparmor_file_mprotect(struct vm_area_struct *vma,
  496. unsigned long reqprot, unsigned long prot)
  497. {
  498. return common_mmap(OP_FMPROT, vma->vm_file, prot,
  499. !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
  500. false);
  501. }
  502. #ifdef CONFIG_IO_URING
  503. static const char *audit_uring_mask(u32 mask)
  504. {
  505. if (mask & AA_MAY_CREATE_SQPOLL)
  506. return "sqpoll";
  507. if (mask & AA_MAY_OVERRIDE_CRED)
  508. return "override_creds";
  509. return "";
  510. }
  511. static void audit_uring_cb(struct audit_buffer *ab, void *va)
  512. {
  513. struct apparmor_audit_data *ad = aad_of_va(va);
  514. if (ad->request & AA_URING_PERM_MASK) {
  515. audit_log_format(ab, " requested=\"%s\"",
  516. audit_uring_mask(ad->request));
  517. if (ad->denied & AA_URING_PERM_MASK) {
  518. audit_log_format(ab, " denied=\"%s\"",
  519. audit_uring_mask(ad->denied));
  520. }
  521. }
  522. if (ad->uring.target) {
  523. audit_log_format(ab, " tcontext=");
  524. aa_label_xaudit(ab, labels_ns(ad->subj_label),
  525. ad->uring.target,
  526. FLAGS_NONE, GFP_ATOMIC);
  527. }
  528. }
  529. static int profile_uring(struct aa_profile *profile, u32 request,
  530. struct aa_label *new, int cap,
  531. struct apparmor_audit_data *ad)
  532. {
  533. unsigned int state;
  534. struct aa_ruleset *rules;
  535. int error = 0;
  536. AA_BUG(!profile);
  537. rules = list_first_entry(&profile->rules, typeof(*rules), list);
  538. state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
  539. if (state) {
  540. struct aa_perms perms = { };
  541. if (new) {
  542. aa_label_match(profile, rules, new, state,
  543. false, request, &perms);
  544. } else {
  545. perms = *aa_lookup_perms(rules->policy, state);
  546. }
  547. aa_apply_modes_to_perms(profile, &perms);
  548. error = aa_check_perms(profile, &perms, request, ad,
  549. audit_uring_cb);
  550. }
  551. return error;
  552. }
  553. /**
  554. * apparmor_uring_override_creds - check the requested cred override
  555. * @new: the target creds
  556. *
  557. * Check to see if the current task is allowed to override it's credentials
  558. * to service an io_uring operation.
  559. */
  560. static int apparmor_uring_override_creds(const struct cred *new)
  561. {
  562. struct aa_profile *profile;
  563. struct aa_label *label;
  564. int error;
  565. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
  566. OP_URING_OVERRIDE);
  567. ad.uring.target = cred_label(new);
  568. label = __begin_current_label_crit_section();
  569. error = fn_for_each(label, profile,
  570. profile_uring(profile, AA_MAY_OVERRIDE_CRED,
  571. cred_label(new), CAP_SYS_ADMIN, &ad));
  572. __end_current_label_crit_section(label);
  573. return error;
  574. }
  575. /**
  576. * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
  577. *
  578. * Check to see if the current task is allowed to create a new io_uring
  579. * kernel polling thread.
  580. */
  581. static int apparmor_uring_sqpoll(void)
  582. {
  583. struct aa_profile *profile;
  584. struct aa_label *label;
  585. int error;
  586. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
  587. OP_URING_SQPOLL);
  588. label = __begin_current_label_crit_section();
  589. error = fn_for_each(label, profile,
  590. profile_uring(profile, AA_MAY_CREATE_SQPOLL,
  591. NULL, CAP_SYS_ADMIN, &ad));
  592. __end_current_label_crit_section(label);
  593. return error;
  594. }
  595. #endif /* CONFIG_IO_URING */
  596. static int apparmor_sb_mount(const char *dev_name, const struct path *path,
  597. const char *type, unsigned long flags, void *data)
  598. {
  599. struct aa_label *label;
  600. int error = 0;
  601. /* Discard magic */
  602. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  603. flags &= ~MS_MGC_MSK;
  604. flags &= ~AA_MS_IGNORE_MASK;
  605. label = __begin_current_label_crit_section();
  606. if (!unconfined(label)) {
  607. if (flags & MS_REMOUNT)
  608. error = aa_remount(current_cred(), label, path, flags,
  609. data);
  610. else if (flags & MS_BIND)
  611. error = aa_bind_mount(current_cred(), label, path,
  612. dev_name, flags);
  613. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
  614. MS_UNBINDABLE))
  615. error = aa_mount_change_type(current_cred(), label,
  616. path, flags);
  617. else if (flags & MS_MOVE)
  618. error = aa_move_mount_old(current_cred(), label, path,
  619. dev_name);
  620. else
  621. error = aa_new_mount(current_cred(), label, dev_name,
  622. path, type, flags, data);
  623. }
  624. __end_current_label_crit_section(label);
  625. return error;
  626. }
  627. static int apparmor_move_mount(const struct path *from_path,
  628. const struct path *to_path)
  629. {
  630. struct aa_label *label;
  631. int error = 0;
  632. label = __begin_current_label_crit_section();
  633. if (!unconfined(label))
  634. error = aa_move_mount(current_cred(), label, from_path,
  635. to_path);
  636. __end_current_label_crit_section(label);
  637. return error;
  638. }
  639. static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
  640. {
  641. struct aa_label *label;
  642. int error = 0;
  643. label = __begin_current_label_crit_section();
  644. if (!unconfined(label))
  645. error = aa_umount(current_cred(), label, mnt, flags);
  646. __end_current_label_crit_section(label);
  647. return error;
  648. }
  649. static int apparmor_sb_pivotroot(const struct path *old_path,
  650. const struct path *new_path)
  651. {
  652. struct aa_label *label;
  653. int error = 0;
  654. label = aa_get_current_label();
  655. if (!unconfined(label))
  656. error = aa_pivotroot(current_cred(), label, old_path, new_path);
  657. aa_put_label(label);
  658. return error;
  659. }
  660. static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
  661. u32 *size, u32 flags)
  662. {
  663. int error = -ENOENT;
  664. struct aa_task_ctx *ctx = task_ctx(current);
  665. struct aa_label *label = NULL;
  666. char *value = NULL;
  667. switch (attr) {
  668. case LSM_ATTR_CURRENT:
  669. label = aa_get_newest_label(cred_label(current_cred()));
  670. break;
  671. case LSM_ATTR_PREV:
  672. if (ctx->previous)
  673. label = aa_get_newest_label(ctx->previous);
  674. break;
  675. case LSM_ATTR_EXEC:
  676. if (ctx->onexec)
  677. label = aa_get_newest_label(ctx->onexec);
  678. break;
  679. default:
  680. error = -EOPNOTSUPP;
  681. break;
  682. }
  683. if (label) {
  684. error = aa_getprocattr(label, &value, false);
  685. if (error > 0)
  686. error = lsm_fill_user_ctx(lx, size, value, error,
  687. LSM_ID_APPARMOR, 0);
  688. kfree(value);
  689. }
  690. aa_put_label(label);
  691. if (error < 0)
  692. return error;
  693. return 1;
  694. }
  695. static int apparmor_getprocattr(struct task_struct *task, const char *name,
  696. char **value)
  697. {
  698. int error = -ENOENT;
  699. /* released below */
  700. const struct cred *cred = get_task_cred(task);
  701. struct aa_task_ctx *ctx = task_ctx(current);
  702. struct aa_label *label = NULL;
  703. if (strcmp(name, "current") == 0)
  704. label = aa_get_newest_label(cred_label(cred));
  705. else if (strcmp(name, "prev") == 0 && ctx->previous)
  706. label = aa_get_newest_label(ctx->previous);
  707. else if (strcmp(name, "exec") == 0 && ctx->onexec)
  708. label = aa_get_newest_label(ctx->onexec);
  709. else
  710. error = -EINVAL;
  711. if (label)
  712. error = aa_getprocattr(label, value, true);
  713. aa_put_label(label);
  714. put_cred(cred);
  715. return error;
  716. }
  717. static int do_setattr(u64 attr, void *value, size_t size)
  718. {
  719. char *command, *largs = NULL, *args = value;
  720. size_t arg_size;
  721. int error;
  722. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
  723. OP_SETPROCATTR);
  724. if (size == 0)
  725. return -EINVAL;
  726. /* AppArmor requires that the buffer must be null terminated atm */
  727. if (args[size - 1] != '\0') {
  728. /* null terminate */
  729. largs = args = kmalloc(size + 1, GFP_KERNEL);
  730. if (!args)
  731. return -ENOMEM;
  732. memcpy(args, value, size);
  733. args[size] = '\0';
  734. }
  735. error = -EINVAL;
  736. args = strim(args);
  737. command = strsep(&args, " ");
  738. if (!args)
  739. goto out;
  740. args = skip_spaces(args);
  741. if (!*args)
  742. goto out;
  743. arg_size = size - (args - (largs ? largs : (char *) value));
  744. if (attr == LSM_ATTR_CURRENT) {
  745. if (strcmp(command, "changehat") == 0) {
  746. error = aa_setprocattr_changehat(args, arg_size,
  747. AA_CHANGE_NOFLAGS);
  748. } else if (strcmp(command, "permhat") == 0) {
  749. error = aa_setprocattr_changehat(args, arg_size,
  750. AA_CHANGE_TEST);
  751. } else if (strcmp(command, "changeprofile") == 0) {
  752. error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
  753. } else if (strcmp(command, "permprofile") == 0) {
  754. error = aa_change_profile(args, AA_CHANGE_TEST);
  755. } else if (strcmp(command, "stack") == 0) {
  756. error = aa_change_profile(args, AA_CHANGE_STACK);
  757. } else
  758. goto fail;
  759. } else if (attr == LSM_ATTR_EXEC) {
  760. if (strcmp(command, "exec") == 0)
  761. error = aa_change_profile(args, AA_CHANGE_ONEXEC);
  762. else if (strcmp(command, "stack") == 0)
  763. error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
  764. AA_CHANGE_STACK));
  765. else
  766. goto fail;
  767. } else
  768. /* only support the "current" and "exec" process attributes */
  769. goto fail;
  770. if (!error)
  771. error = size;
  772. out:
  773. kfree(largs);
  774. return error;
  775. fail:
  776. ad.subj_label = begin_current_label_crit_section();
  777. if (attr == LSM_ATTR_CURRENT)
  778. ad.info = "current";
  779. else if (attr == LSM_ATTR_EXEC)
  780. ad.info = "exec";
  781. else
  782. ad.info = "invalid";
  783. ad.error = error = -EINVAL;
  784. aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
  785. end_current_label_crit_section(ad.subj_label);
  786. goto out;
  787. }
  788. static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
  789. u32 size, u32 flags)
  790. {
  791. int rc;
  792. if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
  793. return -EOPNOTSUPP;
  794. rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
  795. if (rc > 0)
  796. return 0;
  797. return rc;
  798. }
  799. static int apparmor_setprocattr(const char *name, void *value,
  800. size_t size)
  801. {
  802. int attr = lsm_name_to_attr(name);
  803. if (attr)
  804. return do_setattr(attr, value, size);
  805. return -EINVAL;
  806. }
  807. /**
  808. * apparmor_bprm_committing_creds - do task cleanup on committing new creds
  809. * @bprm: binprm for the exec (NOT NULL)
  810. */
  811. static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
  812. {
  813. struct aa_label *label = aa_current_raw_label();
  814. struct aa_label *new_label = cred_label(bprm->cred);
  815. /* bail out if unconfined or not changing profile */
  816. if ((new_label->proxy == label->proxy) ||
  817. (unconfined(new_label)))
  818. return;
  819. aa_inherit_files(bprm->cred, current->files);
  820. current->pdeath_signal = 0;
  821. /* reset soft limits and set hard limits for the new label */
  822. __aa_transition_rlimits(label, new_label);
  823. }
  824. /**
  825. * apparmor_bprm_committed_creds() - do cleanup after new creds committed
  826. * @bprm: binprm for the exec (NOT NULL)
  827. */
  828. static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
  829. {
  830. /* clear out temporary/transitional state from the context */
  831. aa_clear_task_ctx_trans(task_ctx(current));
  832. return;
  833. }
  834. static void apparmor_current_getsecid_subj(u32 *secid)
  835. {
  836. struct aa_label *label = __begin_current_label_crit_section();
  837. *secid = label->secid;
  838. __end_current_label_crit_section(label);
  839. }
  840. static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
  841. {
  842. struct aa_label *label = aa_get_task_label(p);
  843. *secid = label->secid;
  844. aa_put_label(label);
  845. }
  846. static int apparmor_task_setrlimit(struct task_struct *task,
  847. unsigned int resource, struct rlimit *new_rlim)
  848. {
  849. struct aa_label *label = __begin_current_label_crit_section();
  850. int error = 0;
  851. if (!unconfined(label))
  852. error = aa_task_setrlimit(current_cred(), label, task,
  853. resource, new_rlim);
  854. __end_current_label_crit_section(label);
  855. return error;
  856. }
  857. static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
  858. int sig, const struct cred *cred)
  859. {
  860. const struct cred *tc;
  861. struct aa_label *cl, *tl;
  862. int error;
  863. tc = get_task_cred(target);
  864. tl = aa_get_newest_cred_label(tc);
  865. if (cred) {
  866. /*
  867. * Dealing with USB IO specific behavior
  868. */
  869. cl = aa_get_newest_cred_label(cred);
  870. error = aa_may_signal(cred, cl, tc, tl, sig);
  871. aa_put_label(cl);
  872. } else {
  873. cl = __begin_current_label_crit_section();
  874. error = aa_may_signal(current_cred(), cl, tc, tl, sig);
  875. __end_current_label_crit_section(cl);
  876. }
  877. aa_put_label(tl);
  878. put_cred(tc);
  879. return error;
  880. }
  881. static int apparmor_userns_create(const struct cred *cred)
  882. {
  883. struct aa_label *label;
  884. struct aa_profile *profile;
  885. int error = 0;
  886. DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
  887. OP_USERNS_CREATE);
  888. ad.subj_cred = current_cred();
  889. label = begin_current_label_crit_section();
  890. if (!unconfined(label)) {
  891. error = fn_for_each(label, profile,
  892. aa_profile_ns_perm(profile, &ad,
  893. AA_USERNS_CREATE));
  894. }
  895. end_current_label_crit_section(label);
  896. return error;
  897. }
  898. static void apparmor_sk_free_security(struct sock *sk)
  899. {
  900. struct aa_sk_ctx *ctx = aa_sock(sk);
  901. aa_put_label(ctx->label);
  902. aa_put_label(ctx->peer);
  903. }
  904. /**
  905. * apparmor_sk_clone_security - clone the sk_security field
  906. * @sk: sock to have security cloned
  907. * @newsk: sock getting clone
  908. */
  909. static void apparmor_sk_clone_security(const struct sock *sk,
  910. struct sock *newsk)
  911. {
  912. struct aa_sk_ctx *ctx = aa_sock(sk);
  913. struct aa_sk_ctx *new = aa_sock(newsk);
  914. if (new->label)
  915. aa_put_label(new->label);
  916. new->label = aa_get_label(ctx->label);
  917. if (new->peer)
  918. aa_put_label(new->peer);
  919. new->peer = aa_get_label(ctx->peer);
  920. }
  921. static int apparmor_socket_create(int family, int type, int protocol, int kern)
  922. {
  923. struct aa_label *label;
  924. int error = 0;
  925. AA_BUG(in_interrupt());
  926. label = begin_current_label_crit_section();
  927. if (!(kern || unconfined(label)))
  928. error = af_select(family,
  929. create_perm(label, family, type, protocol),
  930. aa_af_perm(current_cred(), label,
  931. OP_CREATE, AA_MAY_CREATE,
  932. family, type, protocol));
  933. end_current_label_crit_section(label);
  934. return error;
  935. }
  936. /**
  937. * apparmor_socket_post_create - setup the per-socket security struct
  938. * @sock: socket that is being setup
  939. * @family: family of socket being created
  940. * @type: type of the socket
  941. * @protocol: protocol of the socket
  942. * @kern: socket is a special kernel socket
  943. *
  944. * Note:
  945. * - kernel sockets labeled kernel_t used to use unconfined
  946. * - socket may not have sk here if created with sock_create_lite or
  947. * sock_alloc. These should be accept cases which will be handled in
  948. * sock_graft.
  949. */
  950. static int apparmor_socket_post_create(struct socket *sock, int family,
  951. int type, int protocol, int kern)
  952. {
  953. struct aa_label *label;
  954. if (kern) {
  955. label = aa_get_label(kernel_t);
  956. } else
  957. label = aa_get_current_label();
  958. if (sock->sk) {
  959. struct aa_sk_ctx *ctx = aa_sock(sock->sk);
  960. aa_put_label(ctx->label);
  961. ctx->label = aa_get_label(label);
  962. }
  963. aa_put_label(label);
  964. return 0;
  965. }
  966. static int apparmor_socket_bind(struct socket *sock,
  967. struct sockaddr *address, int addrlen)
  968. {
  969. AA_BUG(!sock);
  970. AA_BUG(!sock->sk);
  971. AA_BUG(!address);
  972. AA_BUG(in_interrupt());
  973. return af_select(sock->sk->sk_family,
  974. bind_perm(sock, address, addrlen),
  975. aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
  976. }
  977. static int apparmor_socket_connect(struct socket *sock,
  978. struct sockaddr *address, int addrlen)
  979. {
  980. AA_BUG(!sock);
  981. AA_BUG(!sock->sk);
  982. AA_BUG(!address);
  983. AA_BUG(in_interrupt());
  984. return af_select(sock->sk->sk_family,
  985. connect_perm(sock, address, addrlen),
  986. aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
  987. }
  988. static int apparmor_socket_listen(struct socket *sock, int backlog)
  989. {
  990. AA_BUG(!sock);
  991. AA_BUG(!sock->sk);
  992. AA_BUG(in_interrupt());
  993. return af_select(sock->sk->sk_family,
  994. listen_perm(sock, backlog),
  995. aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
  996. }
  997. /*
  998. * Note: while @newsock is created and has some information, the accept
  999. * has not been done.
  1000. */
  1001. static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
  1002. {
  1003. AA_BUG(!sock);
  1004. AA_BUG(!sock->sk);
  1005. AA_BUG(!newsock);
  1006. AA_BUG(in_interrupt());
  1007. return af_select(sock->sk->sk_family,
  1008. accept_perm(sock, newsock),
  1009. aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
  1010. }
  1011. static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
  1012. struct msghdr *msg, int size)
  1013. {
  1014. AA_BUG(!sock);
  1015. AA_BUG(!sock->sk);
  1016. AA_BUG(!msg);
  1017. AA_BUG(in_interrupt());
  1018. return af_select(sock->sk->sk_family,
  1019. msg_perm(op, request, sock, msg, size),
  1020. aa_sk_perm(op, request, sock->sk));
  1021. }
  1022. static int apparmor_socket_sendmsg(struct socket *sock,
  1023. struct msghdr *msg, int size)
  1024. {
  1025. return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
  1026. }
  1027. static int apparmor_socket_recvmsg(struct socket *sock,
  1028. struct msghdr *msg, int size, int flags)
  1029. {
  1030. return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
  1031. }
  1032. /* revaliation, get/set attr, shutdown */
  1033. static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
  1034. {
  1035. AA_BUG(!sock);
  1036. AA_BUG(!sock->sk);
  1037. AA_BUG(in_interrupt());
  1038. return af_select(sock->sk->sk_family,
  1039. sock_perm(op, request, sock),
  1040. aa_sk_perm(op, request, sock->sk));
  1041. }
  1042. static int apparmor_socket_getsockname(struct socket *sock)
  1043. {
  1044. return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
  1045. }
  1046. static int apparmor_socket_getpeername(struct socket *sock)
  1047. {
  1048. return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
  1049. }
  1050. /* revaliation, get/set attr, opt */
  1051. static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
  1052. int level, int optname)
  1053. {
  1054. AA_BUG(!sock);
  1055. AA_BUG(!sock->sk);
  1056. AA_BUG(in_interrupt());
  1057. return af_select(sock->sk->sk_family,
  1058. opt_perm(op, request, sock, level, optname),
  1059. aa_sk_perm(op, request, sock->sk));
  1060. }
  1061. static int apparmor_socket_getsockopt(struct socket *sock, int level,
  1062. int optname)
  1063. {
  1064. return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
  1065. level, optname);
  1066. }
  1067. static int apparmor_socket_setsockopt(struct socket *sock, int level,
  1068. int optname)
  1069. {
  1070. return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
  1071. level, optname);
  1072. }
  1073. static int apparmor_socket_shutdown(struct socket *sock, int how)
  1074. {
  1075. return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
  1076. }
  1077. #ifdef CONFIG_NETWORK_SECMARK
  1078. /**
  1079. * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
  1080. * @sk: sk to associate @skb with
  1081. * @skb: skb to check for perms
  1082. *
  1083. * Note: can not sleep may be called with locks held
  1084. *
  1085. * dont want protocol specific in __skb_recv_datagram()
  1086. * to deny an incoming connection socket_sock_rcv_skb()
  1087. */
  1088. static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  1089. {
  1090. struct aa_sk_ctx *ctx = aa_sock(sk);
  1091. if (!skb->secmark)
  1092. return 0;
  1093. /*
  1094. * If reach here before socket_post_create hook is called, in which
  1095. * case label is null, drop the packet.
  1096. */
  1097. if (!ctx->label)
  1098. return -EACCES;
  1099. return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
  1100. skb->secmark, sk);
  1101. }
  1102. #endif
  1103. static struct aa_label *sk_peer_label(struct sock *sk)
  1104. {
  1105. struct aa_sk_ctx *ctx = aa_sock(sk);
  1106. if (ctx->peer)
  1107. return ctx->peer;
  1108. return ERR_PTR(-ENOPROTOOPT);
  1109. }
  1110. /**
  1111. * apparmor_socket_getpeersec_stream - get security context of peer
  1112. * @sock: socket that we are trying to get the peer context of
  1113. * @optval: output - buffer to copy peer name to
  1114. * @optlen: output - size of copied name in @optval
  1115. * @len: size of @optval buffer
  1116. * Returns: 0 on success, -errno of failure
  1117. *
  1118. * Note: for tcp only valid if using ipsec or cipso on lan
  1119. */
  1120. static int apparmor_socket_getpeersec_stream(struct socket *sock,
  1121. sockptr_t optval, sockptr_t optlen,
  1122. unsigned int len)
  1123. {
  1124. char *name = NULL;
  1125. int slen, error = 0;
  1126. struct aa_label *label;
  1127. struct aa_label *peer;
  1128. label = begin_current_label_crit_section();
  1129. peer = sk_peer_label(sock->sk);
  1130. if (IS_ERR(peer)) {
  1131. error = PTR_ERR(peer);
  1132. goto done;
  1133. }
  1134. slen = aa_label_asxprint(&name, labels_ns(label), peer,
  1135. FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
  1136. FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
  1137. /* don't include terminating \0 in slen, it breaks some apps */
  1138. if (slen < 0) {
  1139. error = -ENOMEM;
  1140. goto done;
  1141. }
  1142. if (slen > len) {
  1143. error = -ERANGE;
  1144. goto done_len;
  1145. }
  1146. if (copy_to_sockptr(optval, name, slen))
  1147. error = -EFAULT;
  1148. done_len:
  1149. if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
  1150. error = -EFAULT;
  1151. done:
  1152. end_current_label_crit_section(label);
  1153. kfree(name);
  1154. return error;
  1155. }
  1156. /**
  1157. * apparmor_socket_getpeersec_dgram - get security label of packet
  1158. * @sock: the peer socket
  1159. * @skb: packet data
  1160. * @secid: pointer to where to put the secid of the packet
  1161. *
  1162. * Sets the netlabel socket state on sk from parent
  1163. */
  1164. static int apparmor_socket_getpeersec_dgram(struct socket *sock,
  1165. struct sk_buff *skb, u32 *secid)
  1166. {
  1167. /* TODO: requires secid support */
  1168. return -ENOPROTOOPT;
  1169. }
  1170. /**
  1171. * apparmor_sock_graft - Initialize newly created socket
  1172. * @sk: child sock
  1173. * @parent: parent socket
  1174. *
  1175. * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
  1176. * just set sk security information off of current creating process label
  1177. * Labeling of sk for accept case - probably should be sock based
  1178. * instead of task, because of the case where an implicitly labeled
  1179. * socket is shared by different tasks.
  1180. */
  1181. static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
  1182. {
  1183. struct aa_sk_ctx *ctx = aa_sock(sk);
  1184. if (!ctx->label)
  1185. ctx->label = aa_get_current_label();
  1186. }
  1187. #ifdef CONFIG_NETWORK_SECMARK
  1188. static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
  1189. struct request_sock *req)
  1190. {
  1191. struct aa_sk_ctx *ctx = aa_sock(sk);
  1192. if (!skb->secmark)
  1193. return 0;
  1194. return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
  1195. skb->secmark, sk);
  1196. }
  1197. #endif
  1198. /*
  1199. * The cred blob is a pointer to, not an instance of, an aa_label.
  1200. */
  1201. struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
  1202. .lbs_cred = sizeof(struct aa_label *),
  1203. .lbs_file = sizeof(struct aa_file_ctx),
  1204. .lbs_task = sizeof(struct aa_task_ctx),
  1205. .lbs_sock = sizeof(struct aa_sk_ctx),
  1206. };
  1207. static const struct lsm_id apparmor_lsmid = {
  1208. .name = "apparmor",
  1209. .id = LSM_ID_APPARMOR,
  1210. };
  1211. static struct security_hook_list apparmor_hooks[] __ro_after_init = {
  1212. LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
  1213. LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
  1214. LSM_HOOK_INIT(capget, apparmor_capget),
  1215. LSM_HOOK_INIT(capable, apparmor_capable),
  1216. LSM_HOOK_INIT(move_mount, apparmor_move_mount),
  1217. LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
  1218. LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
  1219. LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
  1220. LSM_HOOK_INIT(path_link, apparmor_path_link),
  1221. LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
  1222. LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
  1223. LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
  1224. LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
  1225. LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
  1226. LSM_HOOK_INIT(path_rename, apparmor_path_rename),
  1227. LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
  1228. LSM_HOOK_INIT(path_chown, apparmor_path_chown),
  1229. LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
  1230. LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
  1231. LSM_HOOK_INIT(file_open, apparmor_file_open),
  1232. LSM_HOOK_INIT(file_receive, apparmor_file_receive),
  1233. LSM_HOOK_INIT(file_permission, apparmor_file_permission),
  1234. LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
  1235. LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
  1236. LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
  1237. LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
  1238. LSM_HOOK_INIT(file_lock, apparmor_file_lock),
  1239. LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
  1240. LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
  1241. LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
  1242. LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
  1243. LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
  1244. LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
  1245. LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
  1246. LSM_HOOK_INIT(socket_create, apparmor_socket_create),
  1247. LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
  1248. LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
  1249. LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
  1250. LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
  1251. LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
  1252. LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
  1253. LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
  1254. LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
  1255. LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
  1256. LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
  1257. LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
  1258. LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
  1259. #ifdef CONFIG_NETWORK_SECMARK
  1260. LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
  1261. #endif
  1262. LSM_HOOK_INIT(socket_getpeersec_stream,
  1263. apparmor_socket_getpeersec_stream),
  1264. LSM_HOOK_INIT(socket_getpeersec_dgram,
  1265. apparmor_socket_getpeersec_dgram),
  1266. LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
  1267. #ifdef CONFIG_NETWORK_SECMARK
  1268. LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
  1269. #endif
  1270. LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
  1271. LSM_HOOK_INIT(cred_free, apparmor_cred_free),
  1272. LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
  1273. LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
  1274. LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
  1275. LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
  1276. LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
  1277. LSM_HOOK_INIT(task_free, apparmor_task_free),
  1278. LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
  1279. LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
  1280. LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
  1281. LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
  1282. LSM_HOOK_INIT(task_kill, apparmor_task_kill),
  1283. LSM_HOOK_INIT(userns_create, apparmor_userns_create),
  1284. #ifdef CONFIG_AUDIT
  1285. LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
  1286. LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
  1287. LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
  1288. LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
  1289. #endif
  1290. LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
  1291. LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
  1292. LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
  1293. #ifdef CONFIG_IO_URING
  1294. LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
  1295. LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
  1296. #endif
  1297. };
  1298. /*
  1299. * AppArmor sysfs module parameters
  1300. */
  1301. static int param_set_aabool(const char *val, const struct kernel_param *kp);
  1302. static int param_get_aabool(char *buffer, const struct kernel_param *kp);
  1303. #define param_check_aabool param_check_bool
  1304. static const struct kernel_param_ops param_ops_aabool = {
  1305. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1306. .set = param_set_aabool,
  1307. .get = param_get_aabool
  1308. };
  1309. static int param_set_aauint(const char *val, const struct kernel_param *kp);
  1310. static int param_get_aauint(char *buffer, const struct kernel_param *kp);
  1311. #define param_check_aauint param_check_uint
  1312. static const struct kernel_param_ops param_ops_aauint = {
  1313. .set = param_set_aauint,
  1314. .get = param_get_aauint
  1315. };
  1316. static int param_set_aacompressionlevel(const char *val,
  1317. const struct kernel_param *kp);
  1318. static int param_get_aacompressionlevel(char *buffer,
  1319. const struct kernel_param *kp);
  1320. #define param_check_aacompressionlevel param_check_int
  1321. static const struct kernel_param_ops param_ops_aacompressionlevel = {
  1322. .set = param_set_aacompressionlevel,
  1323. .get = param_get_aacompressionlevel
  1324. };
  1325. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
  1326. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
  1327. #define param_check_aalockpolicy param_check_bool
  1328. static const struct kernel_param_ops param_ops_aalockpolicy = {
  1329. .flags = KERNEL_PARAM_OPS_FL_NOARG,
  1330. .set = param_set_aalockpolicy,
  1331. .get = param_get_aalockpolicy
  1332. };
  1333. static int param_set_audit(const char *val, const struct kernel_param *kp);
  1334. static int param_get_audit(char *buffer, const struct kernel_param *kp);
  1335. static int param_set_mode(const char *val, const struct kernel_param *kp);
  1336. static int param_get_mode(char *buffer, const struct kernel_param *kp);
  1337. /* Flag values, also controllable via /sys/module/apparmor/parameters
  1338. * We define special types as we want to do additional mediation.
  1339. */
  1340. /* AppArmor global enforcement switch - complain, enforce, kill */
  1341. enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
  1342. module_param_call(mode, param_set_mode, param_get_mode,
  1343. &aa_g_profile_mode, S_IRUSR | S_IWUSR);
  1344. /* whether policy verification hashing is enabled */
  1345. bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
  1346. #ifdef CONFIG_SECURITY_APPARMOR_HASH
  1347. module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
  1348. #endif
  1349. /* whether policy exactly as loaded is retained for debug and checkpointing */
  1350. bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
  1351. #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
  1352. module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
  1353. #endif
  1354. /* policy loaddata compression level */
  1355. int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
  1356. module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
  1357. aacompressionlevel, 0400);
  1358. /* Debug mode */
  1359. bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
  1360. module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
  1361. /* Audit mode */
  1362. enum audit_mode aa_g_audit;
  1363. module_param_call(audit, param_set_audit, param_get_audit,
  1364. &aa_g_audit, S_IRUSR | S_IWUSR);
  1365. /* Determines if audit header is included in audited messages. This
  1366. * provides more context if the audit daemon is not running
  1367. */
  1368. bool aa_g_audit_header = true;
  1369. module_param_named(audit_header, aa_g_audit_header, aabool,
  1370. S_IRUSR | S_IWUSR);
  1371. /* lock out loading/removal of policy
  1372. * TODO: add in at boot loading of policy, which is the only way to
  1373. * load policy, if lock_policy is set
  1374. */
  1375. bool aa_g_lock_policy;
  1376. module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
  1377. S_IRUSR | S_IWUSR);
  1378. /* Syscall logging mode */
  1379. bool aa_g_logsyscall;
  1380. module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
  1381. /* Maximum pathname length before accesses will start getting rejected */
  1382. unsigned int aa_g_path_max = 2 * PATH_MAX;
  1383. module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
  1384. /* Determines how paranoid loading of policy is and how much verification
  1385. * on the loaded policy is done.
  1386. * DEPRECATED: read only as strict checking of load is always done now
  1387. * that none root users (user namespaces) can load policy.
  1388. */
  1389. bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
  1390. module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
  1391. static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
  1392. static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
  1393. #define param_check_aaintbool param_check_int
  1394. static const struct kernel_param_ops param_ops_aaintbool = {
  1395. .set = param_set_aaintbool,
  1396. .get = param_get_aaintbool
  1397. };
  1398. /* Boot time disable flag */
  1399. static int apparmor_enabled __ro_after_init = 1;
  1400. module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
  1401. static int __init apparmor_enabled_setup(char *str)
  1402. {
  1403. unsigned long enabled;
  1404. int error = kstrtoul(str, 0, &enabled);
  1405. if (!error)
  1406. apparmor_enabled = enabled ? 1 : 0;
  1407. return 1;
  1408. }
  1409. __setup("apparmor=", apparmor_enabled_setup);
  1410. /* set global flag turning off the ability to load policy */
  1411. static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
  1412. {
  1413. if (!apparmor_enabled)
  1414. return -EINVAL;
  1415. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1416. return -EPERM;
  1417. return param_set_bool(val, kp);
  1418. }
  1419. static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
  1420. {
  1421. if (!apparmor_enabled)
  1422. return -EINVAL;
  1423. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1424. return -EPERM;
  1425. return param_get_bool(buffer, kp);
  1426. }
  1427. static int param_set_aabool(const char *val, const struct kernel_param *kp)
  1428. {
  1429. if (!apparmor_enabled)
  1430. return -EINVAL;
  1431. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1432. return -EPERM;
  1433. return param_set_bool(val, kp);
  1434. }
  1435. static int param_get_aabool(char *buffer, const struct kernel_param *kp)
  1436. {
  1437. if (!apparmor_enabled)
  1438. return -EINVAL;
  1439. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1440. return -EPERM;
  1441. return param_get_bool(buffer, kp);
  1442. }
  1443. static int param_set_aauint(const char *val, const struct kernel_param *kp)
  1444. {
  1445. int error;
  1446. if (!apparmor_enabled)
  1447. return -EINVAL;
  1448. /* file is ro but enforce 2nd line check */
  1449. if (apparmor_initialized)
  1450. return -EPERM;
  1451. error = param_set_uint(val, kp);
  1452. aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
  1453. pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
  1454. return error;
  1455. }
  1456. static int param_get_aauint(char *buffer, const struct kernel_param *kp)
  1457. {
  1458. if (!apparmor_enabled)
  1459. return -EINVAL;
  1460. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1461. return -EPERM;
  1462. return param_get_uint(buffer, kp);
  1463. }
  1464. /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
  1465. static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
  1466. {
  1467. struct kernel_param kp_local;
  1468. bool value;
  1469. int error;
  1470. if (apparmor_initialized)
  1471. return -EPERM;
  1472. /* Create local copy, with arg pointing to bool type. */
  1473. value = !!*((int *)kp->arg);
  1474. memcpy(&kp_local, kp, sizeof(kp_local));
  1475. kp_local.arg = &value;
  1476. error = param_set_bool(val, &kp_local);
  1477. if (!error)
  1478. *((int *)kp->arg) = *((bool *)kp_local.arg);
  1479. return error;
  1480. }
  1481. /*
  1482. * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
  1483. * 1/0, this converts the "int that is actually bool" back to bool for
  1484. * display in the /sys filesystem, while keeping it "int" for the LSM
  1485. * infrastructure.
  1486. */
  1487. static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
  1488. {
  1489. struct kernel_param kp_local;
  1490. bool value;
  1491. /* Create local copy, with arg pointing to bool type. */
  1492. value = !!*((int *)kp->arg);
  1493. memcpy(&kp_local, kp, sizeof(kp_local));
  1494. kp_local.arg = &value;
  1495. return param_get_bool(buffer, &kp_local);
  1496. }
  1497. static int param_set_aacompressionlevel(const char *val,
  1498. const struct kernel_param *kp)
  1499. {
  1500. int error;
  1501. if (!apparmor_enabled)
  1502. return -EINVAL;
  1503. if (apparmor_initialized)
  1504. return -EPERM;
  1505. error = param_set_int(val, kp);
  1506. aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
  1507. AA_MIN_CLEVEL, AA_MAX_CLEVEL);
  1508. pr_info("AppArmor: policy rawdata compression level set to %d\n",
  1509. aa_g_rawdata_compression_level);
  1510. return error;
  1511. }
  1512. static int param_get_aacompressionlevel(char *buffer,
  1513. const struct kernel_param *kp)
  1514. {
  1515. if (!apparmor_enabled)
  1516. return -EINVAL;
  1517. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1518. return -EPERM;
  1519. return param_get_int(buffer, kp);
  1520. }
  1521. static int param_get_audit(char *buffer, const struct kernel_param *kp)
  1522. {
  1523. if (!apparmor_enabled)
  1524. return -EINVAL;
  1525. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1526. return -EPERM;
  1527. return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
  1528. }
  1529. static int param_set_audit(const char *val, const struct kernel_param *kp)
  1530. {
  1531. int i;
  1532. if (!apparmor_enabled)
  1533. return -EINVAL;
  1534. if (!val)
  1535. return -EINVAL;
  1536. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1537. return -EPERM;
  1538. i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
  1539. if (i < 0)
  1540. return -EINVAL;
  1541. aa_g_audit = i;
  1542. return 0;
  1543. }
  1544. static int param_get_mode(char *buffer, const struct kernel_param *kp)
  1545. {
  1546. if (!apparmor_enabled)
  1547. return -EINVAL;
  1548. if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
  1549. return -EPERM;
  1550. return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
  1551. }
  1552. static int param_set_mode(const char *val, const struct kernel_param *kp)
  1553. {
  1554. int i;
  1555. if (!apparmor_enabled)
  1556. return -EINVAL;
  1557. if (!val)
  1558. return -EINVAL;
  1559. if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
  1560. return -EPERM;
  1561. i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
  1562. val);
  1563. if (i < 0)
  1564. return -EINVAL;
  1565. aa_g_profile_mode = i;
  1566. return 0;
  1567. }
  1568. char *aa_get_buffer(bool in_atomic)
  1569. {
  1570. union aa_buffer *aa_buf;
  1571. struct aa_local_cache *cache;
  1572. bool try_again = true;
  1573. gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  1574. /* use per cpu cached buffers first */
  1575. cache = get_cpu_ptr(&aa_local_buffers);
  1576. if (!list_empty(&cache->head)) {
  1577. aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
  1578. list_del(&aa_buf->list);
  1579. cache->hold--;
  1580. cache->count--;
  1581. put_cpu_ptr(&aa_local_buffers);
  1582. return &aa_buf->buffer[0];
  1583. }
  1584. put_cpu_ptr(&aa_local_buffers);
  1585. if (!spin_trylock(&aa_buffers_lock)) {
  1586. cache = get_cpu_ptr(&aa_local_buffers);
  1587. cache->hold += 1;
  1588. put_cpu_ptr(&aa_local_buffers);
  1589. spin_lock(&aa_buffers_lock);
  1590. } else {
  1591. cache = get_cpu_ptr(&aa_local_buffers);
  1592. put_cpu_ptr(&aa_local_buffers);
  1593. }
  1594. retry:
  1595. if (buffer_count > reserve_count ||
  1596. (in_atomic && !list_empty(&aa_global_buffers))) {
  1597. aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
  1598. list);
  1599. list_del(&aa_buf->list);
  1600. buffer_count--;
  1601. spin_unlock(&aa_buffers_lock);
  1602. return aa_buf->buffer;
  1603. }
  1604. if (in_atomic) {
  1605. /*
  1606. * out of reserve buffers and in atomic context so increase
  1607. * how many buffers to keep in reserve
  1608. */
  1609. reserve_count++;
  1610. flags = GFP_ATOMIC;
  1611. }
  1612. spin_unlock(&aa_buffers_lock);
  1613. if (!in_atomic)
  1614. might_sleep();
  1615. aa_buf = kmalloc(aa_g_path_max, flags);
  1616. if (!aa_buf) {
  1617. if (try_again) {
  1618. try_again = false;
  1619. spin_lock(&aa_buffers_lock);
  1620. goto retry;
  1621. }
  1622. pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
  1623. return NULL;
  1624. }
  1625. return aa_buf->buffer;
  1626. }
  1627. void aa_put_buffer(char *buf)
  1628. {
  1629. union aa_buffer *aa_buf;
  1630. struct aa_local_cache *cache;
  1631. if (!buf)
  1632. return;
  1633. aa_buf = container_of(buf, union aa_buffer, buffer[0]);
  1634. cache = get_cpu_ptr(&aa_local_buffers);
  1635. if (!cache->hold) {
  1636. put_cpu_ptr(&aa_local_buffers);
  1637. if (spin_trylock(&aa_buffers_lock)) {
  1638. /* put back on global list */
  1639. list_add(&aa_buf->list, &aa_global_buffers);
  1640. buffer_count++;
  1641. spin_unlock(&aa_buffers_lock);
  1642. cache = get_cpu_ptr(&aa_local_buffers);
  1643. put_cpu_ptr(&aa_local_buffers);
  1644. return;
  1645. }
  1646. /* contention on global list, fallback to percpu */
  1647. cache = get_cpu_ptr(&aa_local_buffers);
  1648. cache->hold += 1;
  1649. }
  1650. /* cache in percpu list */
  1651. list_add(&aa_buf->list, &cache->head);
  1652. cache->count++;
  1653. put_cpu_ptr(&aa_local_buffers);
  1654. }
  1655. /*
  1656. * AppArmor init functions
  1657. */
  1658. /**
  1659. * set_init_ctx - set a task context and profile on the first task.
  1660. *
  1661. * TODO: allow setting an alternate profile than unconfined
  1662. */
  1663. static int __init set_init_ctx(void)
  1664. {
  1665. struct cred *cred = (__force struct cred *)current->real_cred;
  1666. set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
  1667. return 0;
  1668. }
  1669. static void destroy_buffers(void)
  1670. {
  1671. union aa_buffer *aa_buf;
  1672. spin_lock(&aa_buffers_lock);
  1673. while (!list_empty(&aa_global_buffers)) {
  1674. aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
  1675. list);
  1676. list_del(&aa_buf->list);
  1677. spin_unlock(&aa_buffers_lock);
  1678. kfree(aa_buf);
  1679. spin_lock(&aa_buffers_lock);
  1680. }
  1681. spin_unlock(&aa_buffers_lock);
  1682. }
  1683. static int __init alloc_buffers(void)
  1684. {
  1685. union aa_buffer *aa_buf;
  1686. int i, num;
  1687. /*
  1688. * per cpu set of cached allocated buffers used to help reduce
  1689. * lock contention
  1690. */
  1691. for_each_possible_cpu(i) {
  1692. per_cpu(aa_local_buffers, i).hold = 0;
  1693. per_cpu(aa_local_buffers, i).count = 0;
  1694. INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
  1695. }
  1696. /*
  1697. * A function may require two buffers at once. Usually the buffers are
  1698. * used for a short period of time and are shared. On UP kernel buffers
  1699. * two should be enough, with more CPUs it is possible that more
  1700. * buffers will be used simultaneously. The preallocated pool may grow.
  1701. * This preallocation has also the side-effect that AppArmor will be
  1702. * disabled early at boot if aa_g_path_max is extremly high.
  1703. */
  1704. if (num_online_cpus() > 1)
  1705. num = 4 + RESERVE_COUNT;
  1706. else
  1707. num = 2 + RESERVE_COUNT;
  1708. for (i = 0; i < num; i++) {
  1709. aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
  1710. __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  1711. if (!aa_buf) {
  1712. destroy_buffers();
  1713. return -ENOMEM;
  1714. }
  1715. aa_put_buffer(aa_buf->buffer);
  1716. }
  1717. return 0;
  1718. }
  1719. #ifdef CONFIG_SYSCTL
  1720. static int apparmor_dointvec(const struct ctl_table *table, int write,
  1721. void *buffer, size_t *lenp, loff_t *ppos)
  1722. {
  1723. if (!aa_current_policy_admin_capable(NULL))
  1724. return -EPERM;
  1725. if (!apparmor_enabled)
  1726. return -EINVAL;
  1727. return proc_dointvec(table, write, buffer, lenp, ppos);
  1728. }
  1729. static struct ctl_table apparmor_sysctl_table[] = {
  1730. #ifdef CONFIG_USER_NS
  1731. {
  1732. .procname = "unprivileged_userns_apparmor_policy",
  1733. .data = &unprivileged_userns_apparmor_policy,
  1734. .maxlen = sizeof(int),
  1735. .mode = 0600,
  1736. .proc_handler = apparmor_dointvec,
  1737. },
  1738. #endif /* CONFIG_USER_NS */
  1739. {
  1740. .procname = "apparmor_display_secid_mode",
  1741. .data = &apparmor_display_secid_mode,
  1742. .maxlen = sizeof(int),
  1743. .mode = 0600,
  1744. .proc_handler = apparmor_dointvec,
  1745. },
  1746. {
  1747. .procname = "apparmor_restrict_unprivileged_unconfined",
  1748. .data = &aa_unprivileged_unconfined_restricted,
  1749. .maxlen = sizeof(int),
  1750. .mode = 0600,
  1751. .proc_handler = apparmor_dointvec,
  1752. },
  1753. };
  1754. static int __init apparmor_init_sysctl(void)
  1755. {
  1756. return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
  1757. }
  1758. #else
  1759. static inline int apparmor_init_sysctl(void)
  1760. {
  1761. return 0;
  1762. }
  1763. #endif /* CONFIG_SYSCTL */
  1764. #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
  1765. static unsigned int apparmor_ip_postroute(void *priv,
  1766. struct sk_buff *skb,
  1767. const struct nf_hook_state *state)
  1768. {
  1769. struct aa_sk_ctx *ctx;
  1770. struct sock *sk;
  1771. if (!skb->secmark)
  1772. return NF_ACCEPT;
  1773. sk = skb_to_full_sk(skb);
  1774. if (sk == NULL)
  1775. return NF_ACCEPT;
  1776. ctx = aa_sock(sk);
  1777. if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
  1778. skb->secmark, sk))
  1779. return NF_ACCEPT;
  1780. return NF_DROP_ERR(-ECONNREFUSED);
  1781. }
  1782. static const struct nf_hook_ops apparmor_nf_ops[] = {
  1783. {
  1784. .hook = apparmor_ip_postroute,
  1785. .pf = NFPROTO_IPV4,
  1786. .hooknum = NF_INET_POST_ROUTING,
  1787. .priority = NF_IP_PRI_SELINUX_FIRST,
  1788. },
  1789. #if IS_ENABLED(CONFIG_IPV6)
  1790. {
  1791. .hook = apparmor_ip_postroute,
  1792. .pf = NFPROTO_IPV6,
  1793. .hooknum = NF_INET_POST_ROUTING,
  1794. .priority = NF_IP6_PRI_SELINUX_FIRST,
  1795. },
  1796. #endif
  1797. };
  1798. static int __net_init apparmor_nf_register(struct net *net)
  1799. {
  1800. return nf_register_net_hooks(net, apparmor_nf_ops,
  1801. ARRAY_SIZE(apparmor_nf_ops));
  1802. }
  1803. static void __net_exit apparmor_nf_unregister(struct net *net)
  1804. {
  1805. nf_unregister_net_hooks(net, apparmor_nf_ops,
  1806. ARRAY_SIZE(apparmor_nf_ops));
  1807. }
  1808. static struct pernet_operations apparmor_net_ops = {
  1809. .init = apparmor_nf_register,
  1810. .exit = apparmor_nf_unregister,
  1811. };
  1812. static int __init apparmor_nf_ip_init(void)
  1813. {
  1814. int err;
  1815. if (!apparmor_enabled)
  1816. return 0;
  1817. err = register_pernet_subsys(&apparmor_net_ops);
  1818. if (err)
  1819. panic("Apparmor: register_pernet_subsys: error %d\n", err);
  1820. return 0;
  1821. }
  1822. __initcall(apparmor_nf_ip_init);
  1823. #endif
  1824. static char nulldfa_src[] __aligned(8) = {
  1825. #include "nulldfa.in"
  1826. };
  1827. static struct aa_dfa *nulldfa;
  1828. static char stacksplitdfa_src[] __aligned(8) = {
  1829. #include "stacksplitdfa.in"
  1830. };
  1831. struct aa_dfa *stacksplitdfa;
  1832. struct aa_policydb *nullpdb;
  1833. static int __init aa_setup_dfa_engine(void)
  1834. {
  1835. int error = -ENOMEM;
  1836. nullpdb = aa_alloc_pdb(GFP_KERNEL);
  1837. if (!nullpdb)
  1838. return -ENOMEM;
  1839. nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
  1840. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  1841. TO_ACCEPT2_FLAG(YYTD_DATA32));
  1842. if (IS_ERR(nulldfa)) {
  1843. error = PTR_ERR(nulldfa);
  1844. goto fail;
  1845. }
  1846. nullpdb->dfa = aa_get_dfa(nulldfa);
  1847. nullpdb->perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
  1848. if (!nullpdb->perms)
  1849. goto fail;
  1850. nullpdb->size = 2;
  1851. stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
  1852. sizeof(stacksplitdfa_src),
  1853. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  1854. TO_ACCEPT2_FLAG(YYTD_DATA32));
  1855. if (IS_ERR(stacksplitdfa)) {
  1856. error = PTR_ERR(stacksplitdfa);
  1857. goto fail;
  1858. }
  1859. return 0;
  1860. fail:
  1861. aa_put_pdb(nullpdb);
  1862. aa_put_dfa(nulldfa);
  1863. nullpdb = NULL;
  1864. nulldfa = NULL;
  1865. stacksplitdfa = NULL;
  1866. return error;
  1867. }
  1868. static void __init aa_teardown_dfa_engine(void)
  1869. {
  1870. aa_put_dfa(stacksplitdfa);
  1871. aa_put_dfa(nulldfa);
  1872. aa_put_pdb(nullpdb);
  1873. nullpdb = NULL;
  1874. stacksplitdfa = NULL;
  1875. nulldfa = NULL;
  1876. }
  1877. static int __init apparmor_init(void)
  1878. {
  1879. int error;
  1880. error = aa_setup_dfa_engine();
  1881. if (error) {
  1882. AA_ERROR("Unable to setup dfa engine\n");
  1883. goto alloc_out;
  1884. }
  1885. error = aa_alloc_root_ns();
  1886. if (error) {
  1887. AA_ERROR("Unable to allocate default profile namespace\n");
  1888. goto alloc_out;
  1889. }
  1890. error = apparmor_init_sysctl();
  1891. if (error) {
  1892. AA_ERROR("Unable to register sysctls\n");
  1893. goto alloc_out;
  1894. }
  1895. error = alloc_buffers();
  1896. if (error) {
  1897. AA_ERROR("Unable to allocate work buffers\n");
  1898. goto alloc_out;
  1899. }
  1900. error = set_init_ctx();
  1901. if (error) {
  1902. AA_ERROR("Failed to set context on init task\n");
  1903. aa_free_root_ns();
  1904. goto buffers_out;
  1905. }
  1906. security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
  1907. &apparmor_lsmid);
  1908. /* Report that AppArmor successfully initialized */
  1909. apparmor_initialized = 1;
  1910. if (aa_g_profile_mode == APPARMOR_COMPLAIN)
  1911. aa_info_message("AppArmor initialized: complain mode enabled");
  1912. else if (aa_g_profile_mode == APPARMOR_KILL)
  1913. aa_info_message("AppArmor initialized: kill mode enabled");
  1914. else
  1915. aa_info_message("AppArmor initialized");
  1916. return error;
  1917. buffers_out:
  1918. destroy_buffers();
  1919. alloc_out:
  1920. aa_destroy_aafs();
  1921. aa_teardown_dfa_engine();
  1922. apparmor_enabled = false;
  1923. return error;
  1924. }
  1925. DEFINE_LSM(apparmor) = {
  1926. .name = "apparmor",
  1927. .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
  1928. .enabled = &apparmor_enabled,
  1929. .blobs = &apparmor_blob_sizes,
  1930. .init = apparmor_init,
  1931. };