lsm.c 49 KB

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