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