1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor LSM hooks.
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #include <linux/lsm_hooks.h>
12 #include <linux/moduleparam.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>
23 #include <uapi/linux/mount.h>
25 #include "include/af_unix.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"
41 /* Flag indicating whether initialization completed */
42 int apparmor_initialized
;
44 DEFINE_PER_CPU(struct aa_buffers
, aa_buffers
);
52 * put the associated labels
54 static void apparmor_cred_free(struct cred
*cred
)
56 aa_put_label(cred_label(cred
));
57 set_cred_label(cred
, NULL
);
61 * allocate the apparmor part of blank credentials
63 static int apparmor_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
65 set_cred_label(cred
, NULL
);
70 * prepare new cred label for modification by prepare_cred block
72 static int apparmor_cred_prepare(struct cred
*new, const struct cred
*old
,
75 set_cred_label(new, aa_get_newest_label(cred_label(old
)));
80 * transfer the apparmor data to a blank set of creds
82 static void apparmor_cred_transfer(struct cred
*new, const struct cred
*old
)
84 set_cred_label(new, aa_get_newest_label(cred_label(old
)));
87 static void apparmor_task_free(struct task_struct
*task
)
90 aa_free_task_ctx(task_ctx(task
));
93 static int apparmor_task_alloc(struct task_struct
*task
,
94 unsigned long clone_flags
)
96 struct aa_task_ctx
*new = task_ctx(task
);
98 aa_dup_task_ctx(new, task_ctx(current
));
103 static int apparmor_ptrace_access_check(struct task_struct
*child
,
106 struct aa_label
*tracer
, *tracee
;
109 tracer
= __begin_current_label_crit_section();
110 tracee
= aa_get_task_label(child
);
111 error
= aa_may_ptrace(tracer
, tracee
,
112 (mode
& PTRACE_MODE_READ
) ? AA_PTRACE_READ
114 aa_put_label(tracee
);
115 __end_current_label_crit_section(tracer
);
120 static int apparmor_ptrace_traceme(struct task_struct
*parent
)
122 struct aa_label
*tracer
, *tracee
;
125 tracee
= __begin_current_label_crit_section();
126 tracer
= aa_get_task_label(parent
);
127 error
= aa_may_ptrace(tracer
, tracee
, AA_PTRACE_TRACE
);
128 aa_put_label(tracer
);
129 __end_current_label_crit_section(tracee
);
134 /* Derived from security/commoncap.c:cap_capget */
135 static int apparmor_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
136 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
138 struct aa_label
*label
;
139 const struct cred
*cred
;
142 cred
= __task_cred(target
);
143 label
= aa_get_newest_cred_label(cred
);
146 * cap_capget is stacked ahead of this and will
147 * initialize effective and permitted.
149 if (!unconfined(label
)) {
150 struct aa_profile
*profile
;
153 label_for_each_confined(i
, label
, profile
) {
154 if (COMPLAIN_MODE(profile
))
156 *effective
= cap_intersect(*effective
,
157 profile
->caps
.allow
);
158 *permitted
= cap_intersect(*permitted
,
159 profile
->caps
.allow
);
168 static int apparmor_capable(const struct cred
*cred
, struct user_namespace
*ns
,
169 int cap
, unsigned int opts
)
171 struct aa_label
*label
;
174 label
= aa_get_newest_cred_label(cred
);
175 if (!unconfined(label
))
176 error
= aa_capable(label
, cap
, opts
);
183 * common_perm - basic common permission check wrapper fn for paths
184 * @op: operation being checked
185 * @path: path to check permission of (NOT NULL)
186 * @mask: requested permissions mask
187 * @cond: conditional info for the permission request (NOT NULL)
189 * Returns: %0 else error code if error or permission denied
191 static int common_perm(const char *op
, const struct path
*path
, u32 mask
,
192 struct path_cond
*cond
)
194 struct aa_label
*label
;
197 label
= __begin_current_label_crit_section();
198 if (!unconfined(label
))
199 error
= aa_path_perm(op
, label
, path
, 0, mask
, cond
);
200 __end_current_label_crit_section(label
);
206 * common_perm_cond - common permission wrapper around inode cond
207 * @op: operation being checked
208 * @path: location to check (NOT NULL)
209 * @mask: requested permissions mask
211 * Returns: %0 else error code if error or permission denied
213 static int common_perm_cond(const char *op
, const struct path
*path
, u32 mask
)
215 struct path_cond cond
= { d_backing_inode(path
->dentry
)->i_uid
,
216 d_backing_inode(path
->dentry
)->i_mode
219 if (!path_mediated_fs(path
->dentry
))
222 return common_perm(op
, path
, mask
, &cond
);
226 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
227 * @op: operation being checked
228 * @dir: directory of the dentry (NOT NULL)
229 * @dentry: dentry to check (NOT NULL)
230 * @mask: requested permissions mask
231 * @cond: conditional info for the permission request (NOT NULL)
233 * Returns: %0 else error code if error or permission denied
235 static int common_perm_dir_dentry(const char *op
, const struct path
*dir
,
236 struct dentry
*dentry
, u32 mask
,
237 struct path_cond
*cond
)
239 struct path path
= { .mnt
= dir
->mnt
, .dentry
= dentry
};
241 return common_perm(op
, &path
, mask
, cond
);
245 * common_perm_rm - common permission wrapper for operations doing rm
246 * @op: operation being checked
247 * @dir: directory that the dentry is in (NOT NULL)
248 * @dentry: dentry being rm'd (NOT NULL)
249 * @mask: requested permission mask
251 * Returns: %0 else error code if error or permission denied
253 static int common_perm_rm(const char *op
, const struct path
*dir
,
254 struct dentry
*dentry
, u32 mask
)
256 struct inode
*inode
= d_backing_inode(dentry
);
257 struct path_cond cond
= { };
259 if (!inode
|| !path_mediated_fs(dentry
))
262 cond
.uid
= inode
->i_uid
;
263 cond
.mode
= inode
->i_mode
;
265 return common_perm_dir_dentry(op
, dir
, dentry
, mask
, &cond
);
269 * common_perm_create - common permission wrapper for operations doing create
270 * @op: operation being checked
271 * @dir: directory that dentry will be created in (NOT NULL)
272 * @dentry: dentry to create (NOT NULL)
273 * @mask: request permission mask
274 * @mode: created file mode
276 * Returns: %0 else error code if error or permission denied
278 static int common_perm_create(const char *op
, const struct path
*dir
,
279 struct dentry
*dentry
, u32 mask
, umode_t mode
)
281 struct path_cond cond
= { current_fsuid(), mode
};
283 if (!path_mediated_fs(dir
->dentry
))
286 return common_perm_dir_dentry(op
, dir
, dentry
, mask
, &cond
);
289 static int apparmor_path_unlink(const struct path
*dir
, struct dentry
*dentry
)
291 return common_perm_rm(OP_UNLINK
, dir
, dentry
, AA_MAY_DELETE
);
294 static int apparmor_path_mkdir(const struct path
*dir
, struct dentry
*dentry
,
297 return common_perm_create(OP_MKDIR
, dir
, dentry
, AA_MAY_CREATE
,
301 static int apparmor_path_rmdir(const struct path
*dir
, struct dentry
*dentry
)
303 return common_perm_rm(OP_RMDIR
, dir
, dentry
, AA_MAY_DELETE
);
306 static int apparmor_path_mknod(const struct path
*dir
, struct dentry
*dentry
,
307 umode_t mode
, unsigned int dev
)
309 return common_perm_create(OP_MKNOD
, dir
, dentry
, AA_MAY_CREATE
, mode
);
312 static int apparmor_path_truncate(const struct path
*path
)
314 return common_perm_cond(OP_TRUNC
, path
, MAY_WRITE
| AA_MAY_SETATTR
);
317 static int apparmor_path_symlink(const struct path
*dir
, struct dentry
*dentry
,
318 const char *old_name
)
320 return common_perm_create(OP_SYMLINK
, dir
, dentry
, AA_MAY_CREATE
,
324 static int apparmor_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
325 struct dentry
*new_dentry
)
327 struct aa_label
*label
;
330 if (!path_mediated_fs(old_dentry
))
333 label
= begin_current_label_crit_section();
334 if (!unconfined(label
))
335 error
= aa_path_link(label
, old_dentry
, new_dir
, new_dentry
);
336 end_current_label_crit_section(label
);
341 static int apparmor_path_rename(const struct path
*old_dir
, struct dentry
*old_dentry
,
342 const struct path
*new_dir
, struct dentry
*new_dentry
)
344 struct aa_label
*label
;
347 if (!path_mediated_fs(old_dentry
))
350 label
= begin_current_label_crit_section();
351 if (!unconfined(label
)) {
352 struct path old_path
= { .mnt
= old_dir
->mnt
,
353 .dentry
= old_dentry
};
354 struct path new_path
= { .mnt
= new_dir
->mnt
,
355 .dentry
= new_dentry
};
356 struct path_cond cond
= { d_backing_inode(old_dentry
)->i_uid
,
357 d_backing_inode(old_dentry
)->i_mode
360 error
= aa_path_perm(OP_RENAME_SRC
, label
, &old_path
, 0,
361 MAY_READ
| AA_MAY_GETATTR
| MAY_WRITE
|
362 AA_MAY_SETATTR
| AA_MAY_DELETE
,
365 error
= aa_path_perm(OP_RENAME_DEST
, label
, &new_path
,
366 0, MAY_WRITE
| AA_MAY_SETATTR
|
367 AA_MAY_CREATE
, &cond
);
370 end_current_label_crit_section(label
);
375 static int apparmor_path_chmod(const struct path
*path
, umode_t mode
)
377 return common_perm_cond(OP_CHMOD
, path
, AA_MAY_CHMOD
);
380 static int apparmor_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
382 return common_perm_cond(OP_CHOWN
, path
, AA_MAY_CHOWN
);
385 static int apparmor_inode_getattr(const struct path
*path
)
387 return common_perm_cond(OP_GETATTR
, path
, AA_MAY_GETATTR
);
390 static int apparmor_file_open(struct file
*file
)
392 struct aa_file_ctx
*fctx
= file_ctx(file
);
393 struct aa_label
*label
;
396 if (!path_mediated_fs(file
->f_path
.dentry
))
399 /* If in exec, permission is handled by bprm hooks.
400 * Cache permissions granted by the previous exec check, with
401 * implicit read and executable mmap which are required to
402 * actually execute the image.
404 if (current
->in_execve
) {
405 fctx
->allow
= MAY_EXEC
| MAY_READ
| AA_EXEC_MMAP
;
409 label
= aa_get_newest_cred_label(file
->f_cred
);
410 if (!unconfined(label
)) {
411 struct inode
*inode
= file_inode(file
);
412 struct path_cond cond
= { inode
->i_uid
, inode
->i_mode
};
414 error
= aa_path_perm(OP_OPEN
, label
, &file
->f_path
, 0,
415 aa_map_file_to_perms(file
), &cond
);
416 /* todo cache full allowed permissions set and state */
417 fctx
->allow
= aa_map_file_to_perms(file
);
424 static int apparmor_file_alloc_security(struct file
*file
)
426 struct aa_file_ctx
*ctx
= file_ctx(file
);
427 struct aa_label
*label
= begin_current_label_crit_section();
429 spin_lock_init(&ctx
->lock
);
430 rcu_assign_pointer(ctx
->label
, aa_get_label(label
));
431 end_current_label_crit_section(label
);
435 static void apparmor_file_free_security(struct file
*file
)
437 struct aa_file_ctx
*ctx
= file_ctx(file
);
440 aa_put_label(rcu_access_pointer(ctx
->label
));
443 static int common_file_perm(const char *op
, struct file
*file
, u32 mask
)
445 struct aa_label
*label
;
448 /* don't reaudit files closed during inheritance */
449 if (file
->f_path
.dentry
== aa_null
.dentry
)
452 label
= __begin_current_label_crit_section();
453 error
= aa_file_perm(op
, label
, file
, mask
);
454 __end_current_label_crit_section(label
);
459 static int apparmor_file_receive(struct file
*file
)
461 return common_file_perm(OP_FRECEIVE
, file
, aa_map_file_to_perms(file
));
464 static int apparmor_file_permission(struct file
*file
, int mask
)
466 return common_file_perm(OP_FPERM
, file
, mask
);
469 static int apparmor_file_lock(struct file
*file
, unsigned int cmd
)
471 u32 mask
= AA_MAY_LOCK
;
476 return common_file_perm(OP_FLOCK
, file
, mask
);
479 static int common_mmap(const char *op
, struct file
*file
, unsigned long prot
,
484 if (!file
|| !file_ctx(file
))
487 if (prot
& PROT_READ
)
490 * Private mappings don't require write perms since they don't
491 * write back to the files
493 if ((prot
& PROT_WRITE
) && !(flags
& MAP_PRIVATE
))
495 if (prot
& PROT_EXEC
)
496 mask
|= AA_EXEC_MMAP
;
498 return common_file_perm(op
, file
, mask
);
501 static int apparmor_mmap_file(struct file
*file
, unsigned long reqprot
,
502 unsigned long prot
, unsigned long flags
)
504 return common_mmap(OP_FMMAP
, file
, prot
, flags
);
507 static int apparmor_file_mprotect(struct vm_area_struct
*vma
,
508 unsigned long reqprot
, unsigned long prot
)
510 return common_mmap(OP_FMPROT
, vma
->vm_file
, prot
,
511 !(vma
->vm_flags
& VM_SHARED
) ? MAP_PRIVATE
: 0);
514 static int apparmor_sb_mount(const char *dev_name
, const struct path
*path
,
515 const char *type
, unsigned long flags
, void *data
)
517 struct aa_label
*label
;
521 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
522 flags
&= ~MS_MGC_MSK
;
524 flags
&= ~AA_MS_IGNORE_MASK
;
526 label
= __begin_current_label_crit_section();
527 if (!unconfined(label
)) {
528 if (flags
& MS_REMOUNT
)
529 error
= aa_remount(label
, path
, flags
, data
);
530 else if (flags
& MS_BIND
)
531 error
= aa_bind_mount(label
, path
, dev_name
, flags
);
532 else if (flags
& (MS_SHARED
| MS_PRIVATE
| MS_SLAVE
|
534 error
= aa_mount_change_type(label
, path
, flags
);
535 else if (flags
& MS_MOVE
)
536 error
= aa_move_mount(label
, path
, dev_name
);
538 error
= aa_new_mount(label
, dev_name
, path
, type
,
541 __end_current_label_crit_section(label
);
546 static int apparmor_sb_umount(struct vfsmount
*mnt
, int flags
)
548 struct aa_label
*label
;
551 label
= __begin_current_label_crit_section();
552 if (!unconfined(label
))
553 error
= aa_umount(label
, mnt
, flags
);
554 __end_current_label_crit_section(label
);
559 static int apparmor_sb_pivotroot(const struct path
*old_path
,
560 const struct path
*new_path
)
562 struct aa_label
*label
;
565 label
= aa_get_current_label();
566 if (!unconfined(label
))
567 error
= aa_pivotroot(label
, old_path
, new_path
);
573 int apparmor_getprocattr(struct task_struct
*task
, char *name
, char **value
)
577 const struct cred
*cred
= get_task_cred(task
);
578 struct aa_task_ctx
*ctx
= task_ctx(current
);
579 struct aa_label
*label
= NULL
;
581 if (strcmp(name
, "current") == 0)
582 label
= aa_get_newest_label(cred_label(cred
));
583 else if (strcmp(name
, "prev") == 0 && ctx
->previous
)
584 label
= aa_get_newest_label(ctx
->previous
);
585 else if (strcmp(name
, "exec") == 0 && ctx
->onexec
)
586 label
= aa_get_newest_label(ctx
->onexec
);
591 error
= aa_getprocattr(label
, value
);
599 int apparmor_setprocattr(const char *name
, void *value
, size_t size
)
601 char *command
, *largs
= NULL
, *args
= value
;
604 DEFINE_AUDIT_DATA(sa
, LSM_AUDIT_DATA_NONE
, OP_SETPROCATTR
);
609 /* AppArmor requires that the buffer must be null terminated atm */
610 if (args
[size
- 1] != '\0') {
612 largs
= args
= kmalloc(size
+ 1, GFP_KERNEL
);
615 memcpy(args
, value
, size
);
621 command
= strsep(&args
, " ");
624 args
= skip_spaces(args
);
628 arg_size
= size
- (args
- (largs
? largs
: (char *) value
));
629 if (strcmp(name
, "current") == 0) {
630 if (strcmp(command
, "changehat") == 0) {
631 error
= aa_setprocattr_changehat(args
, arg_size
,
633 } else if (strcmp(command
, "permhat") == 0) {
634 error
= aa_setprocattr_changehat(args
, arg_size
,
636 } else if (strcmp(command
, "changeprofile") == 0) {
637 error
= aa_change_profile(args
, AA_CHANGE_NOFLAGS
);
638 } else if (strcmp(command
, "permprofile") == 0) {
639 error
= aa_change_profile(args
, AA_CHANGE_TEST
);
640 } else if (strcmp(command
, "stack") == 0) {
641 error
= aa_change_profile(args
, AA_CHANGE_STACK
);
644 } else if (strcmp(name
, "exec") == 0) {
645 if (strcmp(command
, "exec") == 0)
646 error
= aa_change_profile(args
, AA_CHANGE_ONEXEC
);
647 else if (strcmp(command
, "stack") == 0)
648 error
= aa_change_profile(args
, (AA_CHANGE_ONEXEC
|
653 /* only support the "current" and "exec" process attributes */
663 aad(&sa
)->label
= begin_current_label_crit_section();
664 aad(&sa
)->info
= name
;
665 aad(&sa
)->error
= error
= -EINVAL
;
666 aa_audit_msg(AUDIT_APPARMOR_DENIED
, &sa
, NULL
);
667 end_current_label_crit_section(aad(&sa
)->label
);
672 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
673 * @bprm: binprm for the exec (NOT NULL)
675 static void apparmor_bprm_committing_creds(struct linux_binprm
*bprm
)
677 struct aa_label
*label
= aa_current_raw_label();
678 struct aa_label
*new_label
= cred_label(bprm
->cred
);
680 /* bail out if unconfined or not changing profile */
681 if ((new_label
->proxy
== label
->proxy
) ||
682 (unconfined(new_label
)))
685 aa_inherit_files(bprm
->cred
, current
->files
);
687 current
->pdeath_signal
= 0;
689 /* reset soft limits and set hard limits for the new label */
690 __aa_transition_rlimits(label
, new_label
);
694 * apparmor_bprm_committed_cred - do cleanup after new creds committed
695 * @bprm: binprm for the exec (NOT NULL)
697 static void apparmor_bprm_committed_creds(struct linux_binprm
*bprm
)
699 /* clear out temporary/transitional state from the context */
700 aa_clear_task_ctx_trans(task_ctx(current
));
705 static int apparmor_task_setrlimit(struct task_struct
*task
,
706 unsigned int resource
, struct rlimit
*new_rlim
)
708 struct aa_label
*label
= __begin_current_label_crit_section();
711 if (!unconfined(label
))
712 error
= aa_task_setrlimit(label
, task
, resource
, new_rlim
);
713 __end_current_label_crit_section(label
);
718 static int apparmor_task_kill(struct task_struct
*target
, struct kernel_siginfo
*info
,
719 int sig
, const struct cred
*cred
)
721 struct aa_label
*cl
, *tl
;
726 * Dealing with USB IO specific behavior
728 cl
= aa_get_newest_cred_label(cred
);
729 tl
= aa_get_task_label(target
);
730 error
= aa_may_signal(cl
, tl
, sig
);
736 cl
= __begin_current_label_crit_section();
737 tl
= aa_get_task_label(target
);
738 error
= aa_may_signal(cl
, tl
, sig
);
740 __end_current_label_crit_section(cl
);
747 * apparmor_sk_free_security - free the sk_security field
749 static void apparmor_sk_free_security(struct sock
*sk
)
751 struct aa_sk_ctx
*ctx
= aa_sock(sk
);
753 aa_put_label(ctx
->label
);
754 aa_put_label(ctx
->peer
);
755 path_put(&ctx
->path
);
759 * apparmor_clone_security - clone the sk_security field
761 static void apparmor_sk_clone_security(const struct sock
*sk
,
764 struct aa_sk_ctx
*ctx
= aa_sock(sk
);
765 struct aa_sk_ctx
*new = aa_sock(newsk
);
768 aa_put_label(new->label
);
769 new->label
= aa_get_label(ctx
->label
);
772 aa_put_label(new->peer
);
773 new->peer
= aa_get_label(ctx
->peer
);
774 new->path
= ctx
->path
;
775 path_get(&new->path
);
778 static struct path
*UNIX_FS_CONN_PATH(struct sock
*sk
, struct sock
*newsk
)
780 if (sk
->sk_family
== PF_UNIX
&& UNIX_FS(sk
))
781 return &unix_sk(sk
)->path
;
782 else if (newsk
->sk_family
== PF_UNIX
&& UNIX_FS(newsk
))
783 return &unix_sk(newsk
)->path
;
788 * apparmor_unix_stream_connect - check perms before making unix domain conn
790 * peer is locked when this hook is called
792 static int apparmor_unix_stream_connect(struct sock
*sk
, struct sock
*peer_sk
,
795 struct aa_sk_ctx
*sk_ctx
= aa_sock(sk
);
796 struct aa_sk_ctx
*peer_ctx
= aa_sock(peer_sk
);
797 struct aa_sk_ctx
*new_ctx
= aa_sock(newsk
);
798 struct aa_label
*label
;
802 label
= __begin_current_label_crit_section();
803 error
= aa_unix_peer_perm(label
, OP_CONNECT
,
804 (AA_MAY_CONNECT
| AA_MAY_SEND
| AA_MAY_RECEIVE
),
806 if (!UNIX_FS(peer_sk
)) {
808 aa_unix_peer_perm(peer_ctx
->label
, OP_CONNECT
,
809 (AA_MAY_ACCEPT
| AA_MAY_SEND
| AA_MAY_RECEIVE
),
810 peer_sk
, sk
, label
));
812 __end_current_label_crit_section(label
);
817 /* label newsk if it wasn't labeled in post_create. Normally this
818 * would be done in sock_graft, but because we are directly looking
819 * at the peer_sk to obtain peer_labeling for unix socks this
823 new_ctx
->label
= aa_get_label(peer_ctx
->label
);
825 /* Cross reference the peer labels for SO_PEERSEC */
827 aa_put_label(new_ctx
->peer
);
830 aa_put_label(sk_ctx
->peer
);
832 new_ctx
->peer
= aa_get_label(sk_ctx
->label
);
833 sk_ctx
->peer
= aa_get_label(peer_ctx
->label
);
835 path
= UNIX_FS_CONN_PATH(sk
, peer_sk
);
837 new_ctx
->path
= *path
;
838 sk_ctx
->path
= *path
;
846 * apparmor_unix_may_send - check perms before conn or sending unix dgrams
848 * other is locked when this hook is called
850 * dgram connect calls may_send, peer setup but path not copied?????
852 static int apparmor_unix_may_send(struct socket
*sock
, struct socket
*peer
)
854 struct aa_sk_ctx
*peer_ctx
= aa_sock(peer
->sk
);
855 struct aa_label
*label
;
858 label
= __begin_current_label_crit_section();
859 error
= xcheck(aa_unix_peer_perm(label
, OP_SENDMSG
, AA_MAY_SEND
,
860 sock
->sk
, peer
->sk
, NULL
),
861 aa_unix_peer_perm(peer_ctx
->label
, OP_SENDMSG
,
863 peer
->sk
, sock
->sk
, label
));
864 __end_current_label_crit_section(label
);
870 * apparmor_socket_create - check perms before creating a new socket
872 static int apparmor_socket_create(int family
, int type
, int protocol
, int kern
)
874 struct aa_label
*label
;
877 AA_BUG(in_interrupt());
879 label
= begin_current_label_crit_section();
880 if (!(kern
|| unconfined(label
)))
881 error
= af_select(family
,
882 create_perm(label
, family
, type
, protocol
),
883 aa_af_perm(label
, OP_CREATE
, AA_MAY_CREATE
,
884 family
, type
, protocol
));
885 end_current_label_crit_section(label
);
891 * apparmor_socket_post_create - setup the per-socket security struct
894 * - kernel sockets currently labeled unconfined but we may want to
895 * move to a special kernel label
896 * - socket may not have sk here if created with sock_create_lite or
897 * sock_alloc. These should be accept cases which will be handled in
900 static int apparmor_socket_post_create(struct socket
*sock
, int family
,
901 int type
, int protocol
, int kern
)
903 struct aa_label
*label
;
906 struct aa_ns
*ns
= aa_get_current_ns();
908 label
= aa_get_label(ns_unconfined(ns
));
911 label
= aa_get_current_label();
914 struct aa_sk_ctx
*ctx
= aa_sock(sock
->sk
);
916 aa_put_label(ctx
->label
);
917 ctx
->label
= aa_get_label(label
);
925 * apparmor_socket_bind - check perms before bind addr to socket
927 static int apparmor_socket_bind(struct socket
*sock
,
928 struct sockaddr
*address
, int addrlen
)
933 AA_BUG(in_interrupt());
935 return af_select(sock
->sk
->sk_family
,
936 bind_perm(sock
, address
, addrlen
),
937 aa_sk_perm(OP_BIND
, AA_MAY_BIND
, sock
->sk
));
941 * apparmor_socket_connect - check perms before connecting @sock to @address
943 static int apparmor_socket_connect(struct socket
*sock
,
944 struct sockaddr
*address
, int addrlen
)
949 AA_BUG(in_interrupt());
951 return af_select(sock
->sk
->sk_family
,
952 connect_perm(sock
, address
, addrlen
),
953 aa_sk_perm(OP_CONNECT
, AA_MAY_CONNECT
, sock
->sk
));
957 * apparmor_socket_list - check perms before allowing listen
959 static int apparmor_socket_listen(struct socket
*sock
, int backlog
)
963 AA_BUG(in_interrupt());
965 return af_select(sock
->sk
->sk_family
,
966 listen_perm(sock
, backlog
),
967 aa_sk_perm(OP_LISTEN
, AA_MAY_LISTEN
, sock
->sk
));
971 * apparmor_socket_accept - check perms before accepting a new connection.
973 * Note: while @newsock is created and has some information, the accept
976 static int apparmor_socket_accept(struct socket
*sock
, struct socket
*newsock
)
981 AA_BUG(in_interrupt());
983 return af_select(sock
->sk
->sk_family
,
984 accept_perm(sock
, newsock
),
985 aa_sk_perm(OP_ACCEPT
, AA_MAY_ACCEPT
, sock
->sk
));
988 static int aa_sock_msg_perm(const char *op
, u32 request
, struct socket
*sock
,
989 struct msghdr
*msg
, int size
)
994 AA_BUG(in_interrupt());
996 return af_select(sock
->sk
->sk_family
,
997 msg_perm(op
, request
, sock
, msg
, size
),
998 aa_sk_perm(op
, request
, sock
->sk
));
1002 * apparmor_socket_sendmsg - check perms before sending msg to another socket
1004 static int apparmor_socket_sendmsg(struct socket
*sock
,
1005 struct msghdr
*msg
, int size
)
1007 return aa_sock_msg_perm(OP_SENDMSG
, AA_MAY_SEND
, sock
, msg
, size
);
1011 * apparmor_socket_recvmsg - check perms before receiving a message
1013 static int apparmor_socket_recvmsg(struct socket
*sock
,
1014 struct msghdr
*msg
, int size
, int flags
)
1016 return aa_sock_msg_perm(OP_RECVMSG
, AA_MAY_RECEIVE
, sock
, msg
, size
);
1019 /* revaliation, get/set attr, shutdown */
1020 static int aa_sock_perm(const char *op
, u32 request
, struct socket
*sock
)
1024 AA_BUG(in_interrupt());
1026 return af_select(sock
->sk
->sk_family
,
1027 sock_perm(op
, request
, sock
),
1028 aa_sk_perm(op
, request
, sock
->sk
));
1032 * apparmor_socket_getsockname - check perms before getting the local address
1034 static int apparmor_socket_getsockname(struct socket
*sock
)
1036 return aa_sock_perm(OP_GETSOCKNAME
, AA_MAY_GETATTR
, sock
);
1040 * apparmor_socket_getpeername - check perms before getting remote address
1042 static int apparmor_socket_getpeername(struct socket
*sock
)
1044 return aa_sock_perm(OP_GETPEERNAME
, AA_MAY_GETATTR
, sock
);
1047 /* revaliation, get/set attr, opt */
1048 static int aa_sock_opt_perm(const char *op
, u32 request
, struct socket
*sock
,
1049 int level
, int optname
)
1053 AA_BUG(in_interrupt());
1055 return af_select(sock
->sk
->sk_family
,
1056 opt_perm(op
, request
, sock
, level
, optname
),
1057 aa_sk_perm(op
, request
, sock
->sk
));
1061 * apparmor_getsockopt - check perms before getting socket options
1063 static int apparmor_socket_getsockopt(struct socket
*sock
, int level
,
1066 return aa_sock_opt_perm(OP_GETSOCKOPT
, AA_MAY_GETOPT
, sock
,
1071 * apparmor_setsockopt - check perms before setting socket options
1073 static int apparmor_socket_setsockopt(struct socket
*sock
, int level
,
1076 return aa_sock_opt_perm(OP_SETSOCKOPT
, AA_MAY_SETOPT
, sock
,
1081 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1083 static int apparmor_socket_shutdown(struct socket
*sock
, int how
)
1085 return aa_sock_perm(OP_SHUTDOWN
, AA_MAY_SHUTDOWN
, sock
);
1089 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1091 * Note: can not sleep may be called with locks held
1093 * dont want protocol specific in __skb_recv_datagram()
1094 * to deny an incoming connection socket_sock_rcv_skb()
1096 static int apparmor_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
1102 static struct aa_label
*sk_peer_label(struct sock
*sk
)
1104 struct sock
*peer_sk
;
1105 struct aa_sk_ctx
*ctx
= aa_sock(sk
);
1106 struct aa_label
*label
= ERR_PTR(-ENOPROTOOPT
);
1109 return aa_get_label(ctx
->peer
);
1111 if (sk
->sk_family
!= PF_UNIX
)
1112 return ERR_PTR(-ENOPROTOOPT
);
1114 /* check for sockpair peering which does not go through
1115 * security_unix_stream_connect
1117 peer_sk
= unix_peer_get(sk
);
1119 ctx
= aa_sock(peer_sk
);
1121 label
= aa_get_label(ctx
->label
);
1129 * apparmor_socket_getpeersec_stream - get security context of peer
1131 * Note: for tcp only valid if using ipsec or cipso on lan
1133 static int apparmor_socket_getpeersec_stream(struct socket
*sock
,
1134 char __user
*optval
,
1139 int slen
, error
= 0;
1140 struct aa_label
*label
;
1141 struct aa_label
*peer
;
1143 label
= begin_current_label_crit_section();
1144 peer
= sk_peer_label(sock
->sk
);
1146 error
= PTR_ERR(peer
);
1149 slen
= aa_label_asxprint(&name
, labels_ns(label
), peer
,
1150 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
1151 FLAG_HIDDEN_UNCONFINED
, GFP_KERNEL
);
1152 /* don't include terminating \0 in slen, it breaks some apps */
1158 } else if (copy_to_user(optval
, name
, slen
)) {
1162 if (put_user(slen
, optlen
))
1171 end_current_label_crit_section(label
);
1177 * apparmor_socket_getpeersec_dgram - get security label of packet
1178 * @sock: the peer socket
1180 * @secid: pointer to where to put the secid of the packet
1182 * Sets the netlabel socket state on sk from parent
1184 static int apparmor_socket_getpeersec_dgram(struct socket
*sock
,
1185 struct sk_buff
*skb
, u32
*secid
)
1188 /* TODO: requires secid support */
1189 return -ENOPROTOOPT
;
1193 * apparmor_sock_graft - Initialize newly created socket
1195 * @parent: parent socket
1197 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1198 * just set sk security information off of current creating process label
1199 * Labeling of sk for accept case - probably should be sock based
1200 * instead of task, because of the case where an implicitly labeled
1201 * socket is shared by different tasks.
1203 static void apparmor_sock_graft(struct sock
*sk
, struct socket
*parent
)
1205 struct aa_sk_ctx
*ctx
= aa_sock(sk
);
1208 ctx
->label
= aa_get_current_label();
1213 * The cred blob is a pointer to, not an instance of, an aa_task_ctx.
1215 struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init
= {
1216 .lbs_cred
= sizeof(struct aa_task_ctx
*),
1217 .lbs_file
= sizeof(struct aa_file_ctx
),
1218 .lbs_task
= sizeof(struct aa_task_ctx
),
1219 .lbs_sock
= sizeof(struct aa_sk_ctx
),
1222 static struct security_hook_list apparmor_hooks
[] __lsm_ro_after_init
= {
1223 LSM_HOOK_INIT(ptrace_access_check
, apparmor_ptrace_access_check
),
1224 LSM_HOOK_INIT(ptrace_traceme
, apparmor_ptrace_traceme
),
1225 LSM_HOOK_INIT(capget
, apparmor_capget
),
1226 LSM_HOOK_INIT(capable
, apparmor_capable
),
1228 LSM_HOOK_INIT(sb_mount
, apparmor_sb_mount
),
1229 LSM_HOOK_INIT(sb_umount
, apparmor_sb_umount
),
1230 LSM_HOOK_INIT(sb_pivotroot
, apparmor_sb_pivotroot
),
1232 LSM_HOOK_INIT(path_link
, apparmor_path_link
),
1233 LSM_HOOK_INIT(path_unlink
, apparmor_path_unlink
),
1234 LSM_HOOK_INIT(path_symlink
, apparmor_path_symlink
),
1235 LSM_HOOK_INIT(path_mkdir
, apparmor_path_mkdir
),
1236 LSM_HOOK_INIT(path_rmdir
, apparmor_path_rmdir
),
1237 LSM_HOOK_INIT(path_mknod
, apparmor_path_mknod
),
1238 LSM_HOOK_INIT(path_rename
, apparmor_path_rename
),
1239 LSM_HOOK_INIT(path_chmod
, apparmor_path_chmod
),
1240 LSM_HOOK_INIT(path_chown
, apparmor_path_chown
),
1241 LSM_HOOK_INIT(path_truncate
, apparmor_path_truncate
),
1242 LSM_HOOK_INIT(inode_getattr
, apparmor_inode_getattr
),
1244 LSM_HOOK_INIT(file_open
, apparmor_file_open
),
1245 LSM_HOOK_INIT(file_receive
, apparmor_file_receive
),
1246 LSM_HOOK_INIT(file_permission
, apparmor_file_permission
),
1247 LSM_HOOK_INIT(file_alloc_security
, apparmor_file_alloc_security
),
1248 LSM_HOOK_INIT(file_free_security
, apparmor_file_free_security
),
1249 LSM_HOOK_INIT(mmap_file
, apparmor_mmap_file
),
1250 LSM_HOOK_INIT(file_mprotect
, apparmor_file_mprotect
),
1251 LSM_HOOK_INIT(file_lock
, apparmor_file_lock
),
1253 LSM_HOOK_INIT(getprocattr
, apparmor_getprocattr
),
1254 LSM_HOOK_INIT(setprocattr
, apparmor_setprocattr
),
1256 LSM_HOOK_INIT(sk_free_security
, apparmor_sk_free_security
),
1257 LSM_HOOK_INIT(sk_clone_security
, apparmor_sk_clone_security
),
1259 LSM_HOOK_INIT(unix_stream_connect
, apparmor_unix_stream_connect
),
1260 LSM_HOOK_INIT(unix_may_send
, apparmor_unix_may_send
),
1262 LSM_HOOK_INIT(socket_create
, apparmor_socket_create
),
1263 LSM_HOOK_INIT(socket_post_create
, apparmor_socket_post_create
),
1264 LSM_HOOK_INIT(socket_bind
, apparmor_socket_bind
),
1265 LSM_HOOK_INIT(socket_connect
, apparmor_socket_connect
),
1266 LSM_HOOK_INIT(socket_listen
, apparmor_socket_listen
),
1267 LSM_HOOK_INIT(socket_accept
, apparmor_socket_accept
),
1268 LSM_HOOK_INIT(socket_sendmsg
, apparmor_socket_sendmsg
),
1269 LSM_HOOK_INIT(socket_recvmsg
, apparmor_socket_recvmsg
),
1270 LSM_HOOK_INIT(socket_getsockname
, apparmor_socket_getsockname
),
1271 LSM_HOOK_INIT(socket_getpeername
, apparmor_socket_getpeername
),
1272 LSM_HOOK_INIT(socket_getsockopt
, apparmor_socket_getsockopt
),
1273 LSM_HOOK_INIT(socket_setsockopt
, apparmor_socket_setsockopt
),
1274 LSM_HOOK_INIT(socket_shutdown
, apparmor_socket_shutdown
),
1275 LSM_HOOK_INIT(socket_sock_rcv_skb
, apparmor_socket_sock_rcv_skb
),
1276 LSM_HOOK_INIT(socket_getpeersec_stream
,
1277 apparmor_socket_getpeersec_stream
),
1278 LSM_HOOK_INIT(socket_getpeersec_dgram
,
1279 apparmor_socket_getpeersec_dgram
),
1280 LSM_HOOK_INIT(sock_graft
, apparmor_sock_graft
),
1282 LSM_HOOK_INIT(cred_alloc_blank
, apparmor_cred_alloc_blank
),
1283 LSM_HOOK_INIT(cred_free
, apparmor_cred_free
),
1284 LSM_HOOK_INIT(cred_prepare
, apparmor_cred_prepare
),
1285 LSM_HOOK_INIT(cred_transfer
, apparmor_cred_transfer
),
1287 LSM_HOOK_INIT(bprm_set_creds
, apparmor_bprm_set_creds
),
1288 LSM_HOOK_INIT(bprm_committing_creds
, apparmor_bprm_committing_creds
),
1289 LSM_HOOK_INIT(bprm_committed_creds
, apparmor_bprm_committed_creds
),
1291 LSM_HOOK_INIT(task_free
, apparmor_task_free
),
1292 LSM_HOOK_INIT(task_alloc
, apparmor_task_alloc
),
1293 LSM_HOOK_INIT(task_setrlimit
, apparmor_task_setrlimit
),
1294 LSM_HOOK_INIT(task_kill
, apparmor_task_kill
),
1298 * AppArmor sysfs module parameters
1301 static int param_set_aabool(const char *val
, const struct kernel_param
*kp
);
1302 static int param_get_aabool(char *buffer
, const struct kernel_param
*kp
);
1303 #define param_check_aabool param_check_bool
1304 static const struct kernel_param_ops param_ops_aabool
= {
1305 .flags
= KERNEL_PARAM_OPS_FL_NOARG
,
1306 .set
= param_set_aabool
,
1307 .get
= param_get_aabool
1310 static int param_set_aauint(const char *val
, const struct kernel_param
*kp
);
1311 static int param_get_aauint(char *buffer
, const struct kernel_param
*kp
);
1312 #define param_check_aauint param_check_uint
1313 static const struct kernel_param_ops param_ops_aauint
= {
1314 .set
= param_set_aauint
,
1315 .get
= param_get_aauint
1318 static int param_set_aalockpolicy(const char *val
, const struct kernel_param
*kp
);
1319 static int param_get_aalockpolicy(char *buffer
, const struct kernel_param
*kp
);
1320 #define param_check_aalockpolicy param_check_bool
1321 static const struct kernel_param_ops param_ops_aalockpolicy
= {
1322 .flags
= KERNEL_PARAM_OPS_FL_NOARG
,
1323 .set
= param_set_aalockpolicy
,
1324 .get
= param_get_aalockpolicy
1327 static int param_set_audit(const char *val
, const struct kernel_param
*kp
);
1328 static int param_get_audit(char *buffer
, const struct kernel_param
*kp
);
1330 static int param_set_mode(const char *val
, const struct kernel_param
*kp
);
1331 static int param_get_mode(char *buffer
, const struct kernel_param
*kp
);
1333 /* Flag values, also controllable via /sys/module/apparmor/parameters
1334 * We define special types as we want to do additional mediation.
1337 /* AppArmor global enforcement switch - complain, enforce, kill */
1338 enum profile_mode aa_g_profile_mode
= APPARMOR_ENFORCE
;
1339 module_param_call(mode
, param_set_mode
, param_get_mode
,
1340 &aa_g_profile_mode
, S_IRUSR
| S_IWUSR
);
1342 /* whether policy verification hashing is enabled */
1343 bool aa_g_hash_policy
= IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT
);
1344 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1345 module_param_named(hash_policy
, aa_g_hash_policy
, aabool
, S_IRUSR
| S_IWUSR
);
1349 bool aa_g_debug
= IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES
);
1350 module_param_named(debug
, aa_g_debug
, aabool
, S_IRUSR
| S_IWUSR
);
1353 enum audit_mode aa_g_audit
;
1354 module_param_call(audit
, param_set_audit
, param_get_audit
,
1355 &aa_g_audit
, S_IRUSR
| S_IWUSR
);
1357 /* Determines if audit header is included in audited messages. This
1358 * provides more context if the audit daemon is not running
1360 bool aa_g_audit_header
= true;
1361 module_param_named(audit_header
, aa_g_audit_header
, aabool
,
1364 /* lock out loading/removal of policy
1365 * TODO: add in at boot loading of policy, which is the only way to
1366 * load policy, if lock_policy is set
1368 bool aa_g_lock_policy
;
1369 module_param_named(lock_policy
, aa_g_lock_policy
, aalockpolicy
,
1372 /* Syscall logging mode */
1373 bool aa_g_logsyscall
;
1374 module_param_named(logsyscall
, aa_g_logsyscall
, aabool
, S_IRUSR
| S_IWUSR
);
1376 /* Maximum pathname length before accesses will start getting rejected */
1377 unsigned int aa_g_path_max
= 2 * PATH_MAX
;
1378 module_param_named(path_max
, aa_g_path_max
, aauint
, S_IRUSR
);
1380 /* Determines how paranoid loading of policy is and how much verification
1381 * on the loaded policy is done.
1382 * DEPRECATED: read only as strict checking of load is always done now
1383 * that none root users (user namespaces) can load policy.
1385 bool aa_g_paranoid_load
= true;
1386 module_param_named(paranoid_load
, aa_g_paranoid_load
, aabool
, S_IRUGO
);
1388 static int param_get_aaintbool(char *buffer
, const struct kernel_param
*kp
);
1389 static int param_set_aaintbool(const char *val
, const struct kernel_param
*kp
);
1390 #define param_check_aaintbool param_check_int
1391 static const struct kernel_param_ops param_ops_aaintbool
= {
1392 .set
= param_set_aaintbool
,
1393 .get
= param_get_aaintbool
1395 /* Boot time disable flag */
1396 static int apparmor_enabled __lsm_ro_after_init
= 1;
1397 module_param_named(enabled
, apparmor_enabled
, aaintbool
, 0444);
1399 static int __init
apparmor_enabled_setup(char *str
)
1401 unsigned long enabled
;
1402 int error
= kstrtoul(str
, 0, &enabled
);
1404 apparmor_enabled
= enabled
? 1 : 0;
1408 __setup("apparmor=", apparmor_enabled_setup
);
1410 /* set global flag turning off the ability to load policy */
1411 static int param_set_aalockpolicy(const char *val
, const struct kernel_param
*kp
)
1413 if (!apparmor_enabled
)
1415 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1417 return param_set_bool(val
, kp
);
1420 static int param_get_aalockpolicy(char *buffer
, const struct kernel_param
*kp
)
1422 if (!apparmor_enabled
)
1424 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1426 return param_get_bool(buffer
, kp
);
1429 static int param_set_aabool(const char *val
, const struct kernel_param
*kp
)
1431 if (!apparmor_enabled
)
1433 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1435 return param_set_bool(val
, kp
);
1438 static int param_get_aabool(char *buffer
, const struct kernel_param
*kp
)
1440 if (!apparmor_enabled
)
1442 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1444 return param_get_bool(buffer
, kp
);
1447 static int param_set_aauint(const char *val
, const struct kernel_param
*kp
)
1451 if (!apparmor_enabled
)
1453 /* file is ro but enforce 2nd line check */
1454 if (apparmor_initialized
)
1457 error
= param_set_uint(val
, kp
);
1458 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max
);
1463 static int param_get_aauint(char *buffer
, const struct kernel_param
*kp
)
1465 if (!apparmor_enabled
)
1467 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1469 return param_get_uint(buffer
, kp
);
1472 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1473 static int param_set_aaintbool(const char *val
, const struct kernel_param
*kp
)
1475 struct kernel_param kp_local
;
1479 if (apparmor_initialized
)
1482 /* Create local copy, with arg pointing to bool type. */
1483 value
= !!*((int *)kp
->arg
);
1484 memcpy(&kp_local
, kp
, sizeof(kp_local
));
1485 kp_local
.arg
= &value
;
1487 error
= param_set_bool(val
, &kp_local
);
1489 *((int *)kp
->arg
) = *((bool *)kp_local
.arg
);
1494 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1495 * 1/0, this converts the "int that is actually bool" back to bool for
1496 * display in the /sys filesystem, while keeping it "int" for the LSM
1499 static int param_get_aaintbool(char *buffer
, const struct kernel_param
*kp
)
1501 struct kernel_param kp_local
;
1504 /* Create local copy, with arg pointing to bool type. */
1505 value
= !!*((int *)kp
->arg
);
1506 memcpy(&kp_local
, kp
, sizeof(kp_local
));
1507 kp_local
.arg
= &value
;
1509 return param_get_bool(buffer
, &kp_local
);
1512 static int param_get_audit(char *buffer
, const struct kernel_param
*kp
)
1514 if (!apparmor_enabled
)
1516 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1518 return sprintf(buffer
, "%s", audit_mode_names
[aa_g_audit
]);
1521 static int param_set_audit(const char *val
, const struct kernel_param
*kp
)
1525 if (!apparmor_enabled
)
1529 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1532 i
= match_string(audit_mode_names
, AUDIT_MAX_INDEX
, val
);
1540 static int param_get_mode(char *buffer
, const struct kernel_param
*kp
)
1542 if (!apparmor_enabled
)
1544 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1547 return sprintf(buffer
, "%s", aa_profile_mode_names
[aa_g_profile_mode
]);
1550 static int param_set_mode(const char *val
, const struct kernel_param
*kp
)
1554 if (!apparmor_enabled
)
1558 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1561 i
= match_string(aa_profile_mode_names
, APPARMOR_MODE_NAMES_MAX_INDEX
,
1566 aa_g_profile_mode
= i
;
1571 * AppArmor init functions
1575 * set_init_ctx - set a task context and profile on the first task.
1577 * TODO: allow setting an alternate profile than unconfined
1579 static int __init
set_init_ctx(void)
1581 struct cred
*cred
= (struct cred
*)current
->real_cred
;
1583 set_cred_label(cred
, aa_get_label(ns_unconfined(root_ns
)));
1588 static void destroy_buffers(void)
1592 for_each_possible_cpu(i
) {
1593 for_each_cpu_buffer(j
) {
1594 kfree(per_cpu(aa_buffers
, i
).buf
[j
]);
1595 per_cpu(aa_buffers
, i
).buf
[j
] = NULL
;
1600 static int __init
alloc_buffers(void)
1604 for_each_possible_cpu(i
) {
1605 for_each_cpu_buffer(j
) {
1608 if (cpu_to_node(i
) > num_online_nodes())
1609 /* fallback to kmalloc for offline nodes */
1610 buffer
= kmalloc(aa_g_path_max
, GFP_KERNEL
);
1612 buffer
= kmalloc_node(aa_g_path_max
, GFP_KERNEL
,
1618 per_cpu(aa_buffers
, i
).buf
[j
] = buffer
;
1625 #ifdef CONFIG_SYSCTL
1626 static int apparmor_dointvec(struct ctl_table
*table
, int write
,
1627 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1629 if (!policy_admin_capable(NULL
))
1631 if (!apparmor_enabled
)
1634 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1637 static struct ctl_path apparmor_sysctl_path
[] = {
1638 { .procname
= "kernel", },
1642 static struct ctl_table apparmor_sysctl_table
[] = {
1644 .procname
= "unprivileged_userns_apparmor_policy",
1645 .data
= &unprivileged_userns_apparmor_policy
,
1646 .maxlen
= sizeof(int),
1648 .proc_handler
= apparmor_dointvec
,
1653 static int __init
apparmor_init_sysctl(void)
1655 return register_sysctl_paths(apparmor_sysctl_path
,
1656 apparmor_sysctl_table
) ? 0 : -ENOMEM
;
1659 static inline int apparmor_init_sysctl(void)
1663 #endif /* CONFIG_SYSCTL */
1665 static int __init
apparmor_init(void)
1669 error
= aa_setup_dfa_engine();
1671 AA_ERROR("Unable to setup dfa engine\n");
1675 error
= aa_alloc_root_ns();
1677 AA_ERROR("Unable to allocate default profile namespace\n");
1681 error
= apparmor_init_sysctl();
1683 AA_ERROR("Unable to register sysctls\n");
1688 error
= alloc_buffers();
1690 AA_ERROR("Unable to allocate work buffers\n");
1694 error
= set_init_ctx();
1696 AA_ERROR("Failed to set context on init task\n");
1700 security_add_hooks(apparmor_hooks
, ARRAY_SIZE(apparmor_hooks
),
1703 /* Report that AppArmor successfully initialized */
1704 apparmor_initialized
= 1;
1705 if (aa_g_profile_mode
== APPARMOR_COMPLAIN
)
1706 aa_info_message("AppArmor initialized: complain mode enabled");
1707 else if (aa_g_profile_mode
== APPARMOR_KILL
)
1708 aa_info_message("AppArmor initialized: kill mode enabled");
1710 aa_info_message("AppArmor initialized");
1719 aa_teardown_dfa_engine();
1721 apparmor_enabled
= false;
1725 DEFINE_LSM(apparmor
) = {
1727 .flags
= LSM_FLAG_LEGACY_MAJOR
,
1728 .enabled
= &apparmor_enabled
,
1729 .blobs
= &apparmor_blob_sizes
,
1730 .init
= apparmor_init
,