2 * AppArmor security module
4 * This file contains AppArmor LSM hooks.
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #include <linux/lsm_hooks.h>
16 #include <linux/moduleparam.h>
18 #include <linux/mman.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/ptrace.h>
22 #include <linux/ctype.h>
23 #include <linux/sysctl.h>
24 #include <linux/audit.h>
25 #include <linux/user_namespace.h>
26 #include <linux/kmemleak.h>
29 #include "include/af_unix.h"
30 #include "include/apparmor.h"
31 #include "include/apparmorfs.h"
32 #include "include/audit.h"
33 #include "include/capability.h"
34 #include "include/context.h"
35 #include "include/file.h"
36 #include "include/ipc.h"
37 #include "include/net.h"
38 #include "include/path.h"
39 #include "include/label.h"
40 #include "include/policy.h"
41 #include "include/policy_ns.h"
42 #include "include/procattr.h"
43 #include "include/mount.h"
45 /* Flag indicating whether initialization completed */
46 int apparmor_initialized
;
48 DEFINE_PER_CPU(struct aa_buffers
, aa_buffers
);
56 * free the associated aa_task_ctx and put its labels
58 static void apparmor_cred_free(struct cred
*cred
)
60 aa_free_task_context(cred_ctx(cred
));
61 cred_ctx(cred
) = NULL
;
65 * allocate the apparmor part of blank credentials
67 static int apparmor_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
69 /* freed by apparmor_cred_free */
70 struct aa_task_ctx
*ctx
= aa_alloc_task_context(gfp
);
80 * prepare new aa_task_ctx for modification by prepare_cred block
82 static int apparmor_cred_prepare(struct cred
*new, const struct cred
*old
,
85 /* freed by apparmor_cred_free */
86 struct aa_task_ctx
*ctx
= aa_alloc_task_context(gfp
);
91 aa_dup_task_context(ctx
, cred_ctx(old
));
97 * transfer the apparmor data to a blank set of creds
99 static void apparmor_cred_transfer(struct cred
*new, const struct cred
*old
)
101 const struct aa_task_ctx
*old_ctx
= cred_ctx(old
);
102 struct aa_task_ctx
*new_ctx
= cred_ctx(new);
104 aa_dup_task_context(new_ctx
, old_ctx
);
107 static int apparmor_ptrace_access_check(struct task_struct
*child
,
110 struct aa_label
*tracer
, *tracee
;
113 tracer
= begin_current_label_crit_section();
114 tracee
= aa_get_task_label(child
);
115 error
= aa_may_ptrace(tracer
, tracee
,
116 mode
== PTRACE_MODE_READ
? AA_PTRACE_READ
: AA_PTRACE_TRACE
);
117 aa_put_label(tracee
);
118 end_current_label_crit_section(tracer
);
123 static int apparmor_ptrace_traceme(struct task_struct
*parent
)
125 struct aa_label
*tracer
, *tracee
;
128 tracee
= begin_current_label_crit_section();
129 tracer
= aa_get_task_label(parent
);
130 error
= aa_may_ptrace(tracer
, tracee
, AA_PTRACE_TRACE
);
131 aa_put_label(tracer
);
132 end_current_label_crit_section(tracee
);
137 /* Derived from security/commoncap.c:cap_capget */
138 static int apparmor_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
139 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
141 struct aa_label
*label
;
142 const struct cred
*cred
;
145 cred
= __task_cred(target
);
146 label
= aa_get_newest_cred_label(cred
);
149 * cap_capget is stacked ahead of this and will
150 * initialize effective and permitted.
152 if (!unconfined(label
)) {
153 struct aa_profile
*profile
;
156 label_for_each_confined(i
, label
, profile
) {
157 if (COMPLAIN_MODE(profile
))
159 *effective
= cap_intersect(*effective
,
160 profile
->caps
.allow
);
161 *permitted
= cap_intersect(*permitted
,
162 profile
->caps
.allow
);
171 static int apparmor_capable(const struct cred
*cred
, struct user_namespace
*ns
,
174 struct aa_label
*label
;
177 label
= aa_get_newest_cred_label(cred
);
178 if (!unconfined(label
))
179 error
= aa_capable(label
, cap
, audit
);
186 * common_perm - basic common permission check wrapper fn for paths
187 * @op: operation being checked
188 * @path: path to check permission of (NOT NULL)
189 * @mask: requested permissions mask
190 * @cond: conditional info for the permission request (NOT NULL)
192 * Returns: %0 else error code if error or permission denied
194 static int common_perm(const char *op
, const struct path
*path
, u32 mask
,
195 struct path_cond
*cond
)
197 struct aa_label
*label
;
200 label
= __begin_current_label_crit_section();
201 if (!unconfined(label
))
202 error
= aa_path_perm(op
, label
, path
, 0, mask
, cond
);
203 __end_current_label_crit_section(label
);
209 * common_perm_cond - common permission wrapper around inode cond
210 * @op: operation being checked
211 * @path: location to check (NOT NULL)
212 * @mask: requested permissions mask
214 * Returns: %0 else error code if error or permission denied
216 static int common_perm_cond(const char *op
, const struct path
*path
, u32 mask
)
218 struct path_cond cond
= { d_backing_inode(path
->dentry
)->i_uid
,
219 d_backing_inode(path
->dentry
)->i_mode
222 if (!path_mediated_fs(path
->dentry
))
225 return common_perm(op
, path
, mask
, &cond
);
229 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
230 * @op: operation being checked
231 * @dir: directory of the dentry (NOT NULL)
232 * @dentry: dentry to check (NOT NULL)
233 * @mask: requested permissions mask
234 * @cond: conditional info for the permission request (NOT NULL)
236 * Returns: %0 else error code if error or permission denied
238 static int common_perm_dir_dentry(const char *op
, const struct path
*dir
,
239 struct dentry
*dentry
, u32 mask
,
240 struct path_cond
*cond
)
242 struct path path
= { .mnt
= dir
->mnt
, .dentry
= dentry
};
244 return common_perm(op
, &path
, mask
, cond
);
248 * common_perm_rm - common permission wrapper for operations doing rm
249 * @op: operation being checked
250 * @dir: directory that the dentry is in (NOT NULL)
251 * @dentry: dentry being rm'd (NOT NULL)
252 * @mask: requested permission mask
254 * Returns: %0 else error code if error or permission denied
256 static int common_perm_rm(const char *op
, const struct path
*dir
,
257 struct dentry
*dentry
, u32 mask
)
259 struct inode
*inode
= d_backing_inode(dentry
);
260 struct path_cond cond
= { };
262 if (!inode
|| !path_mediated_fs(dentry
))
265 cond
.uid
= inode
->i_uid
;
266 cond
.mode
= inode
->i_mode
;
268 return common_perm_dir_dentry(op
, dir
, dentry
, mask
, &cond
);
272 * common_perm_create - common permission wrapper for operations doing create
273 * @op: operation being checked
274 * @dir: directory that dentry will be created in (NOT NULL)
275 * @dentry: dentry to create (NOT NULL)
276 * @mask: request permission mask
277 * @mode: created file mode
279 * Returns: %0 else error code if error or permission denied
281 static int common_perm_create(const char *op
, const struct path
*dir
,
282 struct dentry
*dentry
, u32 mask
, umode_t mode
)
284 struct path_cond cond
= { current_fsuid(), mode
};
286 if (!path_mediated_fs(dir
->dentry
))
289 return common_perm_dir_dentry(op
, dir
, dentry
, mask
, &cond
);
292 static int apparmor_path_unlink(const struct path
*dir
, struct dentry
*dentry
)
294 return common_perm_rm(OP_UNLINK
, dir
, dentry
, AA_MAY_DELETE
);
297 static int apparmor_path_mkdir(const struct path
*dir
, struct dentry
*dentry
,
300 return common_perm_create(OP_MKDIR
, dir
, dentry
, AA_MAY_CREATE
,
304 static int apparmor_path_rmdir(const struct path
*dir
, struct dentry
*dentry
)
306 return common_perm_rm(OP_RMDIR
, dir
, dentry
, AA_MAY_DELETE
);
309 static int apparmor_path_mknod(const struct path
*dir
, struct dentry
*dentry
,
310 umode_t mode
, unsigned int dev
)
312 return common_perm_create(OP_MKNOD
, dir
, dentry
, AA_MAY_CREATE
, mode
);
315 static int apparmor_path_truncate(const struct path
*path
)
317 return common_perm_cond(OP_TRUNC
, path
, MAY_WRITE
| AA_MAY_SETATTR
);
320 static int apparmor_path_symlink(const struct path
*dir
, struct dentry
*dentry
,
321 const char *old_name
)
323 return common_perm_create(OP_SYMLINK
, dir
, dentry
, AA_MAY_CREATE
,
327 static int apparmor_path_link(struct dentry
*old_dentry
, const struct path
*new_dir
,
328 struct dentry
*new_dentry
)
330 struct aa_label
*label
;
333 if (!path_mediated_fs(old_dentry
))
336 label
= begin_current_label_crit_section();
337 if (!unconfined(label
))
338 error
= aa_path_link(label
, old_dentry
, new_dir
, new_dentry
);
339 end_current_label_crit_section(label
);
344 static int apparmor_path_rename(const struct path
*old_dir
, struct dentry
*old_dentry
,
345 const struct path
*new_dir
, struct dentry
*new_dentry
)
347 struct aa_label
*label
;
350 if (!path_mediated_fs(old_dentry
))
353 label
= begin_current_label_crit_section();
354 if (!unconfined(label
)) {
355 struct path old_path
= { .mnt
= old_dir
->mnt
,
356 .dentry
= old_dentry
};
357 struct path new_path
= { .mnt
= new_dir
->mnt
,
358 .dentry
= new_dentry
};
359 struct path_cond cond
= { d_backing_inode(old_dentry
)->i_uid
,
360 d_backing_inode(old_dentry
)->i_mode
363 error
= aa_path_perm(OP_RENAME_SRC
, label
, &old_path
, 0,
364 MAY_READ
| AA_MAY_GETATTR
| MAY_WRITE
|
365 AA_MAY_SETATTR
| AA_MAY_DELETE
,
368 error
= aa_path_perm(OP_RENAME_DEST
, label
, &new_path
,
369 0, MAY_WRITE
| AA_MAY_SETATTR
|
370 AA_MAY_CREATE
, &cond
);
373 end_current_label_crit_section(label
);
378 static int apparmor_path_chmod(const struct path
*path
, umode_t mode
)
380 return common_perm_cond(OP_CHMOD
, path
, AA_MAY_CHMOD
);
383 static int apparmor_path_chown(const struct path
*path
, kuid_t uid
, kgid_t gid
)
385 return common_perm_cond(OP_CHOWN
, path
, AA_MAY_CHOWN
);
388 static int apparmor_inode_getattr(const struct path
*path
)
390 return common_perm_cond(OP_GETATTR
, path
, AA_MAY_GETATTR
);
393 static int apparmor_file_open(struct file
*file
, const struct cred
*cred
)
395 struct aa_file_ctx
*fctx
= file_ctx(file
);
396 struct aa_label
*label
;
399 if (!path_mediated_fs(file
->f_path
.dentry
))
402 /* If in exec, permission is handled by bprm hooks.
403 * Cache permissions granted by the previous exec check, with
404 * implicit read and executable mmap which are required to
405 * actually execute the image.
407 if (current
->in_execve
) {
408 fctx
->allow
= MAY_EXEC
| MAY_READ
| AA_EXEC_MMAP
;
412 label
= aa_get_newest_cred_label(cred
);
413 if (!unconfined(label
)) {
414 struct inode
*inode
= file_inode(file
);
415 struct path_cond cond
= { inode
->i_uid
, inode
->i_mode
};
417 error
= aa_path_perm(OP_OPEN
, label
, &file
->f_path
, 0,
418 aa_map_file_to_perms(file
), &cond
);
419 /* todo cache full allowed permissions set and state */
420 fctx
->allow
= aa_map_file_to_perms(file
);
427 static int apparmor_file_alloc_security(struct file
*file
)
431 /* freed by apparmor_file_free_security */
432 struct aa_label
*label
= begin_current_label_crit_section();
433 file
->f_security
= aa_alloc_file_ctx(label
, GFP_KERNEL
);
436 end_current_label_crit_section(label
);
441 static void apparmor_file_free_security(struct file
*file
)
443 aa_free_file_ctx(file_ctx(file
));
446 static int common_file_perm(const char *op
, struct file
*file
, u32 mask
)
448 struct aa_label
*label
;
451 /* don't reaudit files closed during inheritance */
452 if (file
->f_path
.dentry
== aa_null
.dentry
)
455 label
= __begin_current_label_crit_section();
456 error
= aa_file_perm(op
, label
, file
, mask
);
457 __end_current_label_crit_section(label
);
462 static int apparmor_file_receive(struct file
*file
)
464 return common_file_perm(OP_FRECEIVE
, file
, aa_map_file_to_perms(file
));
467 static int apparmor_file_permission(struct file
*file
, int mask
)
469 return common_file_perm(OP_FPERM
, file
, mask
);
472 static int apparmor_file_lock(struct file
*file
, unsigned int cmd
)
474 u32 mask
= AA_MAY_LOCK
;
479 return common_file_perm(OP_FLOCK
, file
, mask
);
482 static int common_mmap(const char *op
, struct file
*file
, unsigned long prot
,
487 if (!file
|| !file_ctx(file
))
490 if (prot
& PROT_READ
)
493 * Private mappings don't require write perms since they don't
494 * write back to the files
496 if ((prot
& PROT_WRITE
) && !(flags
& MAP_PRIVATE
))
498 if (prot
& PROT_EXEC
)
499 mask
|= AA_EXEC_MMAP
;
501 return common_file_perm(op
, file
, mask
);
504 static int apparmor_mmap_file(struct file
*file
, unsigned long reqprot
,
505 unsigned long prot
, unsigned long flags
)
507 return common_mmap(OP_FMMAP
, file
, prot
, flags
);
510 static int apparmor_file_mprotect(struct vm_area_struct
*vma
,
511 unsigned long reqprot
, unsigned long prot
)
513 return common_mmap(OP_FMPROT
, vma
->vm_file
, prot
,
514 !(vma
->vm_flags
& VM_SHARED
) ? MAP_PRIVATE
: 0);
517 static int apparmor_sb_mount(const char *dev_name
, const struct path
*path
,
518 const char *type
, unsigned long flags
, void *data
)
520 struct aa_label
*label
;
524 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
525 flags
&= ~MS_MGC_MSK
;
527 flags
&= ~AA_MS_IGNORE_MASK
;
529 label
= __begin_current_label_crit_section();
530 if (!unconfined(label
)) {
531 if (flags
& MS_REMOUNT
)
532 error
= aa_remount(label
, path
, flags
, data
);
533 else if (flags
& MS_BIND
)
534 error
= aa_bind_mount(label
, path
, dev_name
, flags
);
535 else if (flags
& (MS_SHARED
| MS_PRIVATE
| MS_SLAVE
|
537 error
= aa_mount_change_type(label
, path
, flags
);
538 else if (flags
& MS_MOVE
)
539 error
= aa_move_mount(label
, path
, dev_name
);
541 error
= aa_new_mount(label
, dev_name
, path
, type
,
544 __end_current_label_crit_section(label
);
549 static int apparmor_sb_umount(struct vfsmount
*mnt
, int flags
)
551 struct aa_label
*label
;
554 label
= __begin_current_label_crit_section();
555 if (!unconfined(label
))
556 error
= aa_umount(label
, mnt
, flags
);
557 __end_current_label_crit_section(label
);
562 static int apparmor_sb_pivotroot(const struct path
*old_path
,
563 const struct path
*new_path
)
565 struct aa_label
*label
;
568 label
= aa_get_current_label();
569 if (!unconfined(label
))
570 error
= aa_pivotroot(label
, old_path
, new_path
);
576 static int apparmor_getprocattr(struct task_struct
*task
, char *name
,
581 const struct cred
*cred
= get_task_cred(task
);
582 struct aa_task_ctx
*ctx
= cred_ctx(cred
);
583 struct aa_label
*label
= NULL
;
585 if (strcmp(name
, "current") == 0)
586 label
= aa_get_newest_label(ctx
->label
);
587 else if (strcmp(name
, "prev") == 0 && ctx
->previous
)
588 label
= aa_get_newest_label(ctx
->previous
);
589 else if (strcmp(name
, "exec") == 0 && ctx
->onexec
)
590 label
= aa_get_newest_label(ctx
->onexec
);
595 error
= aa_getprocattr(label
, value
);
603 static int apparmor_setprocattr(const char *name
, void *value
,
606 char *command
, *largs
= NULL
, *args
= value
;
609 DEFINE_AUDIT_DATA(sa
, LSM_AUDIT_DATA_NONE
, OP_SETPROCATTR
);
614 /* AppArmor requires that the buffer must be null terminated atm */
615 if (args
[size
- 1] != '\0') {
617 largs
= args
= kmalloc(size
+ 1, GFP_KERNEL
);
620 memcpy(args
, value
, size
);
626 command
= strsep(&args
, " ");
629 args
= skip_spaces(args
);
633 arg_size
= size
- (args
- (largs
? largs
: (char *) value
));
634 if (strcmp(name
, "current") == 0) {
635 if (strcmp(command
, "changehat") == 0) {
636 error
= aa_setprocattr_changehat(args
, arg_size
,
638 } else if (strcmp(command
, "permhat") == 0) {
639 error
= aa_setprocattr_changehat(args
, arg_size
,
641 } else if (strcmp(command
, "changeprofile") == 0) {
642 error
= aa_change_profile(args
, AA_CHANGE_NOFLAGS
);
643 } else if (strcmp(command
, "permprofile") == 0) {
644 error
= aa_change_profile(args
, AA_CHANGE_TEST
);
645 } else if (strcmp(command
, "stack") == 0) {
646 error
= aa_change_profile(args
, AA_CHANGE_STACK
);
649 } else if (strcmp(name
, "exec") == 0) {
650 if (strcmp(command
, "exec") == 0)
651 error
= aa_change_profile(args
, AA_CHANGE_ONEXEC
);
652 else if (strcmp(command
, "stack") == 0)
653 error
= aa_change_profile(args
, (AA_CHANGE_ONEXEC
|
658 /* only support the "current" and "exec" process attributes */
668 aad(&sa
)->label
= begin_current_label_crit_section();
669 aad(&sa
)->info
= name
;
670 aad(&sa
)->error
= error
= -EINVAL
;
671 aa_audit_msg(AUDIT_APPARMOR_DENIED
, &sa
, NULL
);
672 end_current_label_crit_section(aad(&sa
)->label
);
677 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
678 * @bprm: binprm for the exec (NOT NULL)
680 static void apparmor_bprm_committing_creds(struct linux_binprm
*bprm
)
682 struct aa_label
*label
= aa_current_raw_label();
683 struct aa_task_ctx
*new_ctx
= cred_ctx(bprm
->cred
);
685 /* bail out if unconfined or not changing profile */
686 if ((new_ctx
->label
->proxy
== label
->proxy
) ||
687 (unconfined(new_ctx
->label
)))
690 aa_inherit_files(bprm
->cred
, current
->files
);
692 current
->pdeath_signal
= 0;
694 /* reset soft limits and set hard limits for the new label */
695 __aa_transition_rlimits(label
, new_ctx
->label
);
699 * apparmor_bprm_committed_cred - do cleanup after new creds committed
700 * @bprm: binprm for the exec (NOT NULL)
702 static void apparmor_bprm_committed_creds(struct linux_binprm
*bprm
)
704 /* TODO: cleanup signals - ipc mediation */
708 static int apparmor_task_setrlimit(struct task_struct
*task
,
709 unsigned int resource
, struct rlimit
*new_rlim
)
711 struct aa_label
*label
= __begin_current_label_crit_section();
714 if (!unconfined(label
))
715 error
= aa_task_setrlimit(label
, task
, resource
, new_rlim
);
716 __end_current_label_crit_section(label
);
721 static int apparmor_task_kill(struct task_struct
*target
, struct siginfo
*info
,
724 struct aa_label
*cl
, *tl
;
728 /* TODO: after secid to label mapping is done.
729 * Dealing with USB IO specific behavior
732 cl
= __begin_current_label_crit_section();
733 tl
= aa_get_task_label(target
);
734 error
= aa_may_signal(cl
, tl
, sig
);
736 __end_current_label_crit_section(cl
);
742 * apparmor_sk_alloc_security - allocate and attach the sk_security field
744 static int apparmor_sk_alloc_security(struct sock
*sk
, int family
, gfp_t flags
)
746 struct aa_sk_ctx
*ctx
;
748 ctx
= kzalloc(sizeof(*ctx
), flags
);
758 * apparmor_sk_free_security - free the sk_security field
760 static void apparmor_sk_free_security(struct sock
*sk
)
762 struct aa_sk_ctx
*ctx
= SK_CTX(sk
);
765 aa_put_label(ctx
->label
);
766 aa_put_label(ctx
->peer
);
767 path_put(&ctx
->path
);
772 * apparmor_clone_security - clone the sk_security field
774 static void apparmor_sk_clone_security(const struct sock
*sk
,
777 struct aa_sk_ctx
*ctx
= SK_CTX(sk
);
778 struct aa_sk_ctx
*new = SK_CTX(newsk
);
780 new->label
= aa_get_label(ctx
->label
);
781 new->peer
= aa_get_label(ctx
->peer
);
782 new->path
= ctx
->path
;
783 path_get(&new->path
);
786 static struct path
*UNIX_FS_CONN_PATH(struct sock
*sk
, struct sock
*newsk
)
788 if (sk
->sk_family
== PF_UNIX
&& UNIX_FS(sk
))
789 return &unix_sk(sk
)->path
;
790 else if (newsk
->sk_family
== PF_UNIX
&& UNIX_FS(newsk
))
791 return &unix_sk(newsk
)->path
;
796 * apparmor_unix_stream_connect - check perms before making unix domain conn
798 * peer is locked when this hook is called
800 static int apparmor_unix_stream_connect(struct sock
*sk
, struct sock
*peer_sk
,
803 struct aa_sk_ctx
*sk_ctx
= SK_CTX(sk
);
804 struct aa_sk_ctx
*peer_ctx
= SK_CTX(peer_sk
);
805 struct aa_sk_ctx
*new_ctx
= SK_CTX(newsk
);
806 struct aa_label
*label
;
810 label
= __begin_current_label_crit_section();
811 error
= aa_unix_peer_perm(label
, OP_CONNECT
,
812 (AA_MAY_CONNECT
| AA_MAY_SEND
| AA_MAY_RECEIVE
),
814 if (!UNIX_FS(peer_sk
)) {
816 aa_unix_peer_perm(peer_ctx
->label
, OP_CONNECT
,
817 (AA_MAY_ACCEPT
| AA_MAY_SEND
| AA_MAY_RECEIVE
),
818 peer_sk
, sk
, label
));
820 __end_current_label_crit_section(label
);
825 /* label newsk if it wasn't labeled in post_create. Normally this
826 * would be done in sock_graft, but because we are directly looking
827 * at the peer_sk to obtain peer_labeling for unix socks this
831 new_ctx
->label
= aa_get_label(peer_ctx
->label
);
833 /* Cross reference the peer labels for SO_PEERSEC */
835 aa_put_label(new_ctx
->peer
);
838 aa_put_label(sk_ctx
->peer
);
840 new_ctx
->peer
= aa_get_label(sk_ctx
->label
);
841 sk_ctx
->peer
= aa_get_label(peer_ctx
->label
);
843 path
= UNIX_FS_CONN_PATH(sk
, peer_sk
);
845 new_ctx
->path
= *path
;
846 sk_ctx
->path
= *path
;
854 * apparmor_unix_may_send - check perms before conn or sending unix dgrams
856 * other is locked when this hook is called
858 * dgram connect calls may_send, peer setup but path not copied?????
860 static int apparmor_unix_may_send(struct socket
*sock
, struct socket
*peer
)
862 struct aa_sk_ctx
*peer_ctx
= SK_CTX(peer
->sk
);
863 struct aa_label
*label
;
866 label
= __begin_current_label_crit_section();
867 error
= xcheck(aa_unix_peer_perm(label
, OP_SENDMSG
, AA_MAY_SEND
,
868 sock
->sk
, peer
->sk
, NULL
),
869 aa_unix_peer_perm(peer_ctx
->label
, OP_SENDMSG
,
871 peer
->sk
, sock
->sk
, label
));
872 __end_current_label_crit_section(label
);
878 * apparmor_socket_create - check perms before creating a new socket
880 static int apparmor_socket_create(int family
, int type
, int protocol
, int kern
)
882 struct aa_label
*label
;
885 label
= begin_current_label_crit_section();
886 if (!(kern
|| unconfined(label
)))
887 error
= aa_sock_create_perm(label
, family
, type
, protocol
);
888 end_current_label_crit_section(label
);
894 * apparmor_socket_post_create - setup the per-socket security struct
897 * - kernel sockets currently labeled unconfined but we may want to
898 * move to a special kernel label
899 * - socket may not have sk here if created with sock_create_lite or
900 * sock_alloc. These should be accept cases which will be handled in
903 static int apparmor_socket_post_create(struct socket
*sock
, int family
,
904 int type
, int protocol
, int kern
)
906 struct aa_label
*label
;
909 struct aa_ns
*ns
= aa_get_current_ns();
911 label
= aa_get_label(ns_unconfined(ns
));
914 label
= aa_get_current_label();
917 struct aa_sk_ctx
*ctx
= SK_CTX(sock
->sk
);
919 aa_put_label(ctx
->label
);
920 ctx
->label
= aa_get_label(label
);
928 * apparmor_socket_bind - check perms before bind addr to socket
930 static int apparmor_socket_bind(struct socket
*sock
,
931 struct sockaddr
*address
, int addrlen
)
933 return aa_sock_bind_perm(sock
, address
, addrlen
);
937 * apparmor_socket_connect - check perms before connecting @sock to @address
939 static int apparmor_socket_connect(struct socket
*sock
,
940 struct sockaddr
*address
, int addrlen
)
942 return aa_sock_connect_perm(sock
, address
, addrlen
);
946 * apparmor_socket_list - check perms before allowing listen
948 static int apparmor_socket_listen(struct socket
*sock
, int backlog
)
950 return aa_sock_listen_perm(sock
, backlog
);
954 * apparmor_socket_accept - check perms before accepting a new connection.
956 * Note: while @newsock is created and has some information, the accept
959 static int apparmor_socket_accept(struct socket
*sock
, struct socket
*newsock
)
961 return aa_sock_accept_perm(sock
, newsock
);
965 * apparmor_socket_sendmsg - check perms before sending msg to another socket
967 static int apparmor_socket_sendmsg(struct socket
*sock
,
968 struct msghdr
*msg
, int size
)
970 return aa_sock_msg_perm(OP_SENDMSG
, AA_MAY_SEND
, sock
, msg
, size
);
974 * apparmor_socket_recvmsg - check perms before receiving a message
976 static int apparmor_socket_recvmsg(struct socket
*sock
,
977 struct msghdr
*msg
, int size
, int flags
)
979 return aa_sock_msg_perm(OP_RECVMSG
, AA_MAY_RECEIVE
, sock
, msg
, size
);
983 * apparmor_socket_getsockname - check perms before getting the local address
985 static int apparmor_socket_getsockname(struct socket
*sock
)
987 return aa_sock_perm(OP_GETSOCKNAME
, AA_MAY_GETATTR
, sock
);
991 * apparmor_socket_getpeername - check perms before getting remote address
993 static int apparmor_socket_getpeername(struct socket
*sock
)
995 return aa_sock_perm(OP_GETPEERNAME
, AA_MAY_GETATTR
, sock
);
999 * apparmor_getsockopt - check perms before getting socket options
1001 static int apparmor_socket_getsockopt(struct socket
*sock
, int level
,
1004 return aa_sock_opt_perm(OP_GETSOCKOPT
, AA_MAY_GETOPT
, sock
,
1009 * apparmor_setsockopt - check perms before setting socket options
1011 static int apparmor_socket_setsockopt(struct socket
*sock
, int level
,
1014 return aa_sock_opt_perm(OP_SETSOCKOPT
, AA_MAY_SETOPT
, sock
,
1019 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1021 static int apparmor_socket_shutdown(struct socket
*sock
, int how
)
1023 return aa_sock_perm(OP_SHUTDOWN
, AA_MAY_SHUTDOWN
, sock
);
1027 * apparmor_socket_sock_recv_skb - check perms before associating skb to sk
1029 * Note: can not sleep may be called with locks held
1031 * dont want protocol specific in __skb_recv_datagram()
1032 * to deny an incoming connection socket_sock_rcv_skb()
1034 static int apparmor_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
1040 static struct aa_label
*sk_peer_label(struct sock
*sk
)
1042 struct sock
*peer_sk
;
1043 struct aa_sk_ctx
*ctx
= SK_CTX(sk
);
1048 if (sk
->sk_family
!= PF_UNIX
)
1049 return ERR_PTR(-ENOPROTOOPT
);
1051 /* check for sockpair peering which does not go through
1052 * security_unix_stream_connect
1054 peer_sk
= unix_peer(sk
);
1056 ctx
= SK_CTX(peer_sk
);
1061 return ERR_PTR(-ENOPROTOOPT
);
1065 * apparmor_socket_getpeersec_stream - get security context of peer
1067 * Note: for tcp only valid if using ipsec or cipso on lan
1069 static int apparmor_socket_getpeersec_stream(struct socket
*sock
,
1070 char __user
*optval
,
1075 int slen
, error
= 0;
1076 struct aa_label
*label
;
1077 struct aa_label
*peer
;
1079 label
= begin_current_label_crit_section();
1080 peer
= sk_peer_label(sock
->sk
);
1082 error
= PTR_ERR(peer
);
1085 slen
= aa_label_asxprint(&name
, labels_ns(label
), peer
,
1086 FLAG_SHOW_MODE
| FLAG_VIEW_SUBNS
|
1087 FLAG_HIDDEN_UNCONFINED
, GFP_KERNEL
);
1088 /* don't include terminating \0 in slen, it breaks some apps */
1094 } else if (copy_to_user(optval
, name
, slen
)) {
1098 if (put_user(slen
, optlen
))
1106 end_current_label_crit_section(label
);
1112 * apparmor_socket_getpeersec_dgram - get security label of packet
1113 * @sock: the peer socket
1115 * @secid: pointer to where to put the secid of the packet
1117 * Sets the netlabel socket state on sk from parent
1119 static int apparmor_socket_getpeersec_dgram(struct socket
*sock
,
1120 struct sk_buff
*skb
, u32
*secid
)
1123 /* TODO: requires secid support */
1124 return -ENOPROTOOPT
;
1128 * apparmor_sock_graft - Initialize newly created socket
1130 * @parent: parent socket
1132 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1133 * just set sk security information off of current creating process label
1134 * Labeling of sk for accept case - probably should be sock based
1135 * instead of task, because of the case where an implicitly labeled
1136 * socket is shared by different tasks.
1138 static void apparmor_sock_graft(struct sock
*sk
, struct socket
*parent
)
1140 struct aa_sk_ctx
*ctx
= SK_CTX(sk
);
1143 ctx
->label
= aa_get_current_label();
1146 static struct security_hook_list apparmor_hooks
[] __lsm_ro_after_init
= {
1147 LSM_HOOK_INIT(ptrace_access_check
, apparmor_ptrace_access_check
),
1148 LSM_HOOK_INIT(ptrace_traceme
, apparmor_ptrace_traceme
),
1149 LSM_HOOK_INIT(capget
, apparmor_capget
),
1150 LSM_HOOK_INIT(capable
, apparmor_capable
),
1152 LSM_HOOK_INIT(sb_mount
, apparmor_sb_mount
),
1153 LSM_HOOK_INIT(sb_umount
, apparmor_sb_umount
),
1154 LSM_HOOK_INIT(sb_pivotroot
, apparmor_sb_pivotroot
),
1156 LSM_HOOK_INIT(path_link
, apparmor_path_link
),
1157 LSM_HOOK_INIT(path_unlink
, apparmor_path_unlink
),
1158 LSM_HOOK_INIT(path_symlink
, apparmor_path_symlink
),
1159 LSM_HOOK_INIT(path_mkdir
, apparmor_path_mkdir
),
1160 LSM_HOOK_INIT(path_rmdir
, apparmor_path_rmdir
),
1161 LSM_HOOK_INIT(path_mknod
, apparmor_path_mknod
),
1162 LSM_HOOK_INIT(path_rename
, apparmor_path_rename
),
1163 LSM_HOOK_INIT(path_chmod
, apparmor_path_chmod
),
1164 LSM_HOOK_INIT(path_chown
, apparmor_path_chown
),
1165 LSM_HOOK_INIT(path_truncate
, apparmor_path_truncate
),
1166 LSM_HOOK_INIT(inode_getattr
, apparmor_inode_getattr
),
1168 LSM_HOOK_INIT(file_open
, apparmor_file_open
),
1169 LSM_HOOK_INIT(file_receive
, apparmor_file_receive
),
1170 LSM_HOOK_INIT(file_permission
, apparmor_file_permission
),
1171 LSM_HOOK_INIT(file_alloc_security
, apparmor_file_alloc_security
),
1172 LSM_HOOK_INIT(file_free_security
, apparmor_file_free_security
),
1173 LSM_HOOK_INIT(mmap_file
, apparmor_mmap_file
),
1174 LSM_HOOK_INIT(file_mprotect
, apparmor_file_mprotect
),
1175 LSM_HOOK_INIT(file_lock
, apparmor_file_lock
),
1177 LSM_HOOK_INIT(getprocattr
, apparmor_getprocattr
),
1178 LSM_HOOK_INIT(setprocattr
, apparmor_setprocattr
),
1180 LSM_HOOK_INIT(sk_alloc_security
, apparmor_sk_alloc_security
),
1181 LSM_HOOK_INIT(sk_free_security
, apparmor_sk_free_security
),
1182 LSM_HOOK_INIT(sk_clone_security
, apparmor_sk_clone_security
),
1184 LSM_HOOK_INIT(unix_stream_connect
, apparmor_unix_stream_connect
),
1185 LSM_HOOK_INIT(unix_may_send
, apparmor_unix_may_send
),
1187 LSM_HOOK_INIT(socket_create
, apparmor_socket_create
),
1188 LSM_HOOK_INIT(socket_post_create
, apparmor_socket_post_create
),
1189 LSM_HOOK_INIT(socket_bind
, apparmor_socket_bind
),
1190 LSM_HOOK_INIT(socket_connect
, apparmor_socket_connect
),
1191 LSM_HOOK_INIT(socket_listen
, apparmor_socket_listen
),
1192 LSM_HOOK_INIT(socket_accept
, apparmor_socket_accept
),
1193 LSM_HOOK_INIT(socket_sendmsg
, apparmor_socket_sendmsg
),
1194 LSM_HOOK_INIT(socket_recvmsg
, apparmor_socket_recvmsg
),
1195 LSM_HOOK_INIT(socket_getsockname
, apparmor_socket_getsockname
),
1196 LSM_HOOK_INIT(socket_getpeername
, apparmor_socket_getpeername
),
1197 LSM_HOOK_INIT(socket_getsockopt
, apparmor_socket_getsockopt
),
1198 LSM_HOOK_INIT(socket_setsockopt
, apparmor_socket_setsockopt
),
1199 LSM_HOOK_INIT(socket_shutdown
, apparmor_socket_shutdown
),
1200 LSM_HOOK_INIT(socket_sock_rcv_skb
, apparmor_socket_sock_rcv_skb
),
1201 LSM_HOOK_INIT(socket_getpeersec_stream
,
1202 apparmor_socket_getpeersec_stream
),
1203 LSM_HOOK_INIT(socket_getpeersec_dgram
,
1204 apparmor_socket_getpeersec_dgram
),
1205 LSM_HOOK_INIT(sock_graft
, apparmor_sock_graft
),
1207 LSM_HOOK_INIT(cred_alloc_blank
, apparmor_cred_alloc_blank
),
1208 LSM_HOOK_INIT(cred_free
, apparmor_cred_free
),
1209 LSM_HOOK_INIT(cred_prepare
, apparmor_cred_prepare
),
1210 LSM_HOOK_INIT(cred_transfer
, apparmor_cred_transfer
),
1212 LSM_HOOK_INIT(bprm_set_creds
, apparmor_bprm_set_creds
),
1213 LSM_HOOK_INIT(bprm_committing_creds
, apparmor_bprm_committing_creds
),
1214 LSM_HOOK_INIT(bprm_committed_creds
, apparmor_bprm_committed_creds
),
1216 LSM_HOOK_INIT(task_setrlimit
, apparmor_task_setrlimit
),
1217 LSM_HOOK_INIT(task_kill
, apparmor_task_kill
),
1221 * AppArmor sysfs module parameters
1224 static int param_set_aabool(const char *val
, const struct kernel_param
*kp
);
1225 static int param_get_aabool(char *buffer
, const struct kernel_param
*kp
);
1226 #define param_check_aabool param_check_bool
1227 static const struct kernel_param_ops param_ops_aabool
= {
1228 .flags
= KERNEL_PARAM_OPS_FL_NOARG
,
1229 .set
= param_set_aabool
,
1230 .get
= param_get_aabool
1233 static int param_set_aauint(const char *val
, const struct kernel_param
*kp
);
1234 static int param_get_aauint(char *buffer
, const struct kernel_param
*kp
);
1235 #define param_check_aauint param_check_uint
1236 static const struct kernel_param_ops param_ops_aauint
= {
1237 .set
= param_set_aauint
,
1238 .get
= param_get_aauint
1241 static int param_set_aalockpolicy(const char *val
, const struct kernel_param
*kp
);
1242 static int param_get_aalockpolicy(char *buffer
, const struct kernel_param
*kp
);
1243 #define param_check_aalockpolicy param_check_bool
1244 static const struct kernel_param_ops param_ops_aalockpolicy
= {
1245 .flags
= KERNEL_PARAM_OPS_FL_NOARG
,
1246 .set
= param_set_aalockpolicy
,
1247 .get
= param_get_aalockpolicy
1250 static int param_set_audit(const char *val
, const struct kernel_param
*kp
);
1251 static int param_get_audit(char *buffer
, const struct kernel_param
*kp
);
1253 static int param_set_mode(const char *val
, const struct kernel_param
*kp
);
1254 static int param_get_mode(char *buffer
, const struct kernel_param
*kp
);
1256 /* Flag values, also controllable via /sys/module/apparmor/parameters
1257 * We define special types as we want to do additional mediation.
1260 /* AppArmor global enforcement switch - complain, enforce, kill */
1261 enum profile_mode aa_g_profile_mode
= APPARMOR_ENFORCE
;
1262 module_param_call(mode
, param_set_mode
, param_get_mode
,
1263 &aa_g_profile_mode
, S_IRUSR
| S_IWUSR
);
1265 /* whether policy verification hashing is enabled */
1266 bool aa_g_hash_policy
= IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT
);
1267 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1268 module_param_named(hash_policy
, aa_g_hash_policy
, aabool
, S_IRUSR
| S_IWUSR
);
1272 bool aa_g_debug
= IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES
);
1273 module_param_named(debug
, aa_g_debug
, aabool
, S_IRUSR
| S_IWUSR
);
1276 enum audit_mode aa_g_audit
;
1277 module_param_call(audit
, param_set_audit
, param_get_audit
,
1278 &aa_g_audit
, S_IRUSR
| S_IWUSR
);
1280 /* Determines if audit header is included in audited messages. This
1281 * provides more context if the audit daemon is not running
1283 bool aa_g_audit_header
= true;
1284 module_param_named(audit_header
, aa_g_audit_header
, aabool
,
1287 /* lock out loading/removal of policy
1288 * TODO: add in at boot loading of policy, which is the only way to
1289 * load policy, if lock_policy is set
1291 bool aa_g_lock_policy
;
1292 module_param_named(lock_policy
, aa_g_lock_policy
, aalockpolicy
,
1295 /* Syscall logging mode */
1296 bool aa_g_logsyscall
;
1297 module_param_named(logsyscall
, aa_g_logsyscall
, aabool
, S_IRUSR
| S_IWUSR
);
1299 /* Maximum pathname length before accesses will start getting rejected */
1300 unsigned int aa_g_path_max
= 2 * PATH_MAX
;
1301 module_param_named(path_max
, aa_g_path_max
, aauint
, S_IRUSR
);
1303 /* Determines how paranoid loading of policy is and how much verification
1304 * on the loaded policy is done.
1305 * DEPRECATED: read only as strict checking of load is always done now
1306 * that none root users (user namespaces) can load policy.
1308 bool aa_g_paranoid_load
= true;
1309 module_param_named(paranoid_load
, aa_g_paranoid_load
, aabool
, S_IRUGO
);
1311 /* Boot time disable flag */
1312 static bool apparmor_enabled
= CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE
;
1313 module_param_named(enabled
, apparmor_enabled
, bool, S_IRUGO
);
1315 static int __init
apparmor_enabled_setup(char *str
)
1317 unsigned long enabled
;
1318 int error
= kstrtoul(str
, 0, &enabled
);
1320 apparmor_enabled
= enabled
? 1 : 0;
1324 __setup("apparmor=", apparmor_enabled_setup
);
1326 /* set global flag turning off the ability to load policy */
1327 static int param_set_aalockpolicy(const char *val
, const struct kernel_param
*kp
)
1329 if (!apparmor_enabled
)
1331 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1333 return param_set_bool(val
, kp
);
1336 static int param_get_aalockpolicy(char *buffer
, const struct kernel_param
*kp
)
1338 if (!apparmor_enabled
)
1340 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1342 return param_get_bool(buffer
, kp
);
1345 static int param_set_aabool(const char *val
, const struct kernel_param
*kp
)
1347 if (!apparmor_enabled
)
1349 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1351 return param_set_bool(val
, kp
);
1354 static int param_get_aabool(char *buffer
, const struct kernel_param
*kp
)
1356 if (!apparmor_enabled
)
1358 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1360 return param_get_bool(buffer
, kp
);
1363 static int param_set_aauint(const char *val
, const struct kernel_param
*kp
)
1367 if (!apparmor_enabled
)
1369 /* file is ro but enforce 2nd line check */
1370 if (apparmor_initialized
)
1373 error
= param_set_uint(val
, kp
);
1374 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max
);
1379 static int param_get_aauint(char *buffer
, const struct kernel_param
*kp
)
1381 if (!apparmor_enabled
)
1383 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1385 return param_get_uint(buffer
, kp
);
1388 static int param_get_audit(char *buffer
, const struct kernel_param
*kp
)
1390 if (!apparmor_enabled
)
1392 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1394 return sprintf(buffer
, "%s", audit_mode_names
[aa_g_audit
]);
1397 static int param_set_audit(const char *val
, const struct kernel_param
*kp
)
1401 if (!apparmor_enabled
)
1405 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1408 for (i
= 0; i
< AUDIT_MAX_INDEX
; i
++) {
1409 if (strcmp(val
, audit_mode_names
[i
]) == 0) {
1418 static int param_get_mode(char *buffer
, const struct kernel_param
*kp
)
1420 if (!apparmor_enabled
)
1422 if (apparmor_initialized
&& !policy_view_capable(NULL
))
1425 return sprintf(buffer
, "%s", aa_profile_mode_names
[aa_g_profile_mode
]);
1428 static int param_set_mode(const char *val
, const struct kernel_param
*kp
)
1432 if (!apparmor_enabled
)
1436 if (apparmor_initialized
&& !policy_admin_capable(NULL
))
1439 for (i
= 0; i
< APPARMOR_MODE_NAMES_MAX_INDEX
; i
++) {
1440 if (strcmp(val
, aa_profile_mode_names
[i
]) == 0) {
1441 aa_g_profile_mode
= i
;
1450 * AppArmor init functions
1454 * set_init_ctx - set a task context and profile on the first task.
1456 * TODO: allow setting an alternate profile than unconfined
1458 static int __init
set_init_ctx(void)
1460 struct cred
*cred
= (struct cred
*)current
->real_cred
;
1461 struct aa_task_ctx
*ctx
;
1463 ctx
= aa_alloc_task_context(GFP_KERNEL
);
1467 ctx
->label
= aa_get_label(ns_unconfined(root_ns
));
1468 cred_ctx(cred
) = ctx
;
1473 static void destroy_buffers(void)
1477 for_each_possible_cpu(i
) {
1478 for_each_cpu_buffer(j
) {
1479 kfree(per_cpu(aa_buffers
, i
).buf
[j
]);
1480 per_cpu(aa_buffers
, i
).buf
[j
] = NULL
;
1485 static int __init
alloc_buffers(void)
1489 for_each_possible_cpu(i
) {
1490 for_each_cpu_buffer(j
) {
1493 if (cpu_to_node(i
) > num_online_nodes())
1494 /* fallback to kmalloc for offline nodes */
1495 buffer
= kmalloc(aa_g_path_max
, GFP_KERNEL
);
1497 buffer
= kmalloc_node(aa_g_path_max
, GFP_KERNEL
,
1503 per_cpu(aa_buffers
, i
).buf
[j
] = buffer
;
1510 #ifdef CONFIG_SYSCTL
1511 static int apparmor_dointvec(struct ctl_table
*table
, int write
,
1512 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
1514 if (!policy_admin_capable(NULL
))
1516 if (!apparmor_enabled
)
1519 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
1522 static struct ctl_path apparmor_sysctl_path
[] = {
1523 { .procname
= "kernel", },
1527 static struct ctl_table apparmor_sysctl_table
[] = {
1529 .procname
= "unprivileged_userns_apparmor_policy",
1530 .data
= &unprivileged_userns_apparmor_policy
,
1531 .maxlen
= sizeof(int),
1533 .proc_handler
= apparmor_dointvec
,
1538 static int __init
apparmor_init_sysctl(void)
1540 return register_sysctl_paths(apparmor_sysctl_path
,
1541 apparmor_sysctl_table
) ? 0 : -ENOMEM
;
1544 static inline int apparmor_init_sysctl(void)
1548 #endif /* CONFIG_SYSCTL */
1550 static int __init
apparmor_init(void)
1554 if (!apparmor_enabled
|| !security_module_enable("apparmor")) {
1555 aa_info_message("AppArmor disabled by boot time parameter");
1556 apparmor_enabled
= false;
1560 error
= aa_setup_dfa_engine();
1562 AA_ERROR("Unable to setup dfa engine\n");
1566 error
= aa_alloc_root_ns();
1568 AA_ERROR("Unable to allocate default profile namespace\n");
1572 error
= apparmor_init_sysctl();
1574 AA_ERROR("Unable to register sysctls\n");
1579 error
= alloc_buffers();
1581 AA_ERROR("Unable to allocate work buffers\n");
1585 error
= set_init_ctx();
1587 AA_ERROR("Failed to set context on init task\n");
1591 security_add_hooks(apparmor_hooks
, ARRAY_SIZE(apparmor_hooks
),
1594 /* Report that AppArmor successfully initialized */
1595 apparmor_initialized
= 1;
1596 if (aa_g_profile_mode
== APPARMOR_COMPLAIN
)
1597 aa_info_message("AppArmor initialized: complain mode enabled");
1598 else if (aa_g_profile_mode
== APPARMOR_KILL
)
1599 aa_info_message("AppArmor initialized: kill mode enabled");
1601 aa_info_message("AppArmor initialized");
1610 aa_teardown_dfa_engine();
1612 apparmor_enabled
= false;
1616 security_initcall(apparmor_init
);