18 #include <linux/mman.h>
21 #include <linux/ptrace.h>
22 #include <linux/ctype.h>
23 #include <linux/sysctl.h>
24 #include <linux/audit.h>
30 #include "include/audit.h"
49 static void apparmor_cred_free(
struct cred *
cred)
52 cred->security =
NULL;
58 static int apparmor_cred_alloc_blank(
struct cred *
cred,
gfp_t gfp)
72 static int apparmor_cred_prepare(
struct cred *
new,
const struct cred *old,
88 static void apparmor_cred_transfer(
struct cred *
new,
const struct cred *old)
106 static int apparmor_ptrace_traceme(
struct task_struct *parent)
120 const struct cred *cred;
124 profile = aa_cred_profile(cred);
131 *effective = cap_intersect(*effective, profile->
caps.allow);
132 *permitted = cap_intersect(*permitted, profile->
caps.allow);
139 static int apparmor_capable(
const struct cred *cred,
struct user_namespace *
ns,
146 profile = aa_cred_profile(cred);
168 profile = __aa_current_profile();
185 static int common_perm_dir_dentry(
int op,
struct path *dir,
189 struct path path = { dir->
mnt, dentry };
191 return common_perm(op, &path, mask, cond);
203 static int common_perm_mnt_dentry(
int op,
struct vfsmount *
mnt,
206 struct path path = {
mnt, dentry };
211 return common_perm(op, &path, mask, &cond);
223 static int common_perm_rm(
int op,
struct path *dir,
229 if (!inode || !dir->
mnt || !mediated_filesystem(inode))
235 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
248 static int common_perm_create(
int op,
struct path *dir,
struct dentry *dentry,
253 if (!dir->
mnt || !mediated_filesystem(dir->
dentry->d_inode))
256 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
259 static int apparmor_path_unlink(
struct path *dir,
struct dentry *dentry)
264 static int apparmor_path_mkdir(
struct path *dir,
struct dentry *dentry,
271 static int apparmor_path_rmdir(
struct path *dir,
struct dentry *dentry)
276 static int apparmor_path_mknod(
struct path *dir,
struct dentry *dentry,
282 static int apparmor_path_truncate(
struct path *path)
285 path->
dentry->d_inode->i_mode
288 if (!path->
mnt || !mediated_filesystem(path->
dentry->d_inode))
295 static int apparmor_path_symlink(
struct path *dir,
struct dentry *dentry,
296 const char *old_name)
302 static int apparmor_path_link(
struct dentry *old_dentry,
struct path *new_dir,
303 struct dentry *new_dentry)
308 if (!mediated_filesystem(old_dentry->
d_inode))
311 profile = aa_current_profile();
313 error =
aa_path_link(profile, old_dentry, new_dir, new_dentry);
317 static int apparmor_path_rename(
struct path *old_dir,
struct dentry *old_dentry,
318 struct path *new_dir,
struct dentry *new_dentry)
323 if (!mediated_filesystem(old_dentry->
d_inode))
326 profile = aa_current_profile();
328 struct path old_path = { old_dir->
mnt, old_dentry };
329 struct path new_path = { new_dir->
mnt, new_dentry };
347 static int apparmor_path_chmod(
struct path *path,
umode_t mode)
349 if (!mediated_filesystem(path->
dentry->d_inode))
358 path->
dentry->d_inode->i_mode
361 if (!mediated_filesystem(path->
dentry->d_inode))
367 static int apparmor_inode_getattr(
struct vfsmount *mnt,
struct dentry *dentry)
369 if (!mediated_filesystem(dentry->
d_inode))
372 return common_perm_mnt_dentry(
OP_GETATTR, mnt, dentry,
376 static int apparmor_file_open(
struct file *
file,
const struct cred *cred)
382 if (!mediated_filesystem(file->
f_path.dentry->d_inode))
395 profile = aa_cred_profile(cred);
397 struct inode *inode = file->
f_path.dentry->d_inode;
401 aa_map_file_to_perms(file), &cond);
403 fcxt->
allow = aa_map_file_to_perms(file);
409 static int apparmor_file_alloc_security(
struct file *file)
412 file->f_security = aa_alloc_file_context(
GFP_KERNEL);
413 if (!file->f_security)
419 static void apparmor_file_free_security(
struct file *file)
423 aa_free_file_context(cxt);
426 static int common_file_perm(
int op,
struct file *file,
u32 mask)
435 !mediated_filesystem(file->
f_path.dentry->d_inode))
438 profile = __aa_current_profile();
448 ((fprofile != profile) || (mask & ~fcxt->
allow)))
454 static int apparmor_file_permission(
struct file *file,
int mask)
456 return common_file_perm(
OP_FPERM, file, mask);
459 static int apparmor_file_lock(
struct file *file,
unsigned int cmd)
466 return common_file_perm(
OP_FLOCK, file, mask);
469 static int common_mmap(
int op,
struct file *file,
unsigned long prot,
472 struct dentry *dentry;
475 if (!file || !file->f_security)
489 dentry = file->
f_path.dentry;
490 return common_file_perm(op, file, mask);
493 static int apparmor_mmap_file(
struct file *file,
unsigned long reqprot,
494 unsigned long prot,
unsigned long flags)
496 return common_mmap(
OP_FMMAP, file, prot, flags);
500 unsigned long reqprot,
unsigned long prot)
503 !(vma->
vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
514 profile = aa_cred_profile(cred);
516 if (
strcmp(name,
"current") == 0)
533 static int apparmor_setprocattr(
struct task_struct *task,
char *name,
534 void *value,
size_t size)
546 if (args[size - 1] !=
'\0') {
558 command =
strsep(&args,
" ");
565 arg_size = size - (args - (
char *) value);
566 if (
strcmp(name,
"current") == 0) {
567 if (
strcmp(command,
"changehat") == 0) {
570 }
else if (
strcmp(command,
"permhat") == 0) {
573 }
else if (
strcmp(command,
"changeprofile") == 0) {
576 }
else if (
strcmp(command,
"permprofile") == 0) {
579 }
else if (
strcmp(command,
"permipc") == 0) {
593 }
else if (
strcmp(name,
"exec") == 0) {
605 static int apparmor_task_setrlimit(
struct task_struct *task,
608 struct aa_profile *profile = __aa_current_profile();
617 static struct security_operations apparmor_ops = {
620 .ptrace_access_check = apparmor_ptrace_access_check,
621 .ptrace_traceme = apparmor_ptrace_traceme,
622 .capget = apparmor_capget,
623 .capable = apparmor_capable,
625 .path_link = apparmor_path_link,
626 .path_unlink = apparmor_path_unlink,
627 .path_symlink = apparmor_path_symlink,
628 .path_mkdir = apparmor_path_mkdir,
629 .path_rmdir = apparmor_path_rmdir,
630 .path_mknod = apparmor_path_mknod,
631 .path_rename = apparmor_path_rename,
632 .path_chmod = apparmor_path_chmod,
633 .path_chown = apparmor_path_chown,
634 .path_truncate = apparmor_path_truncate,
635 .inode_getattr = apparmor_inode_getattr,
637 .file_open = apparmor_file_open,
638 .file_permission = apparmor_file_permission,
639 .file_alloc_security = apparmor_file_alloc_security,
640 .file_free_security = apparmor_file_free_security,
641 .mmap_file = apparmor_mmap_file,
643 .file_mprotect = apparmor_file_mprotect,
644 .file_lock = apparmor_file_lock,
646 .getprocattr = apparmor_getprocattr,
647 .setprocattr = apparmor_setprocattr,
649 .cred_alloc_blank = apparmor_cred_alloc_blank,
650 .cred_free = apparmor_cred_free,
651 .cred_prepare = apparmor_cred_prepare,
652 .cred_transfer = apparmor_cred_transfer,
659 .task_setrlimit = apparmor_task_setrlimit,
666 static int param_set_aabool(
const char *
val,
const struct kernel_param *kp);
668 #define param_check_aabool param_check_bool
670 .set = param_set_aabool,
671 .get = param_get_aabool
674 static int param_set_aauint(
const char *
val,
const struct kernel_param *kp);
676 #define param_check_aauint param_check_uint
678 .set = param_set_aauint,
679 .get = param_get_aauint
682 static int param_set_aalockpolicy(
const char *
val,
const struct kernel_param *kp);
684 #define param_check_aalockpolicy param_check_bool
686 .set = param_set_aalockpolicy,
687 .get = param_get_aalockpolicy
745 static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
748 static int __init apparmor_enabled_setup(
char *
str)
753 apparmor_enabled = enabled ? 1 : 0;
757 __setup(
"apparmor=", apparmor_enabled_setup);
760 static int param_set_aalockpolicy(
const char *
val,
const struct kernel_param *kp)
764 if (aa_g_lock_policy)
776 static int param_set_aabool(
const char *val,
const struct kernel_param *kp)
783 static int param_get_aabool(
char *buffer,
const struct kernel_param *kp)
790 static int param_set_aauint(
const char *val,
const struct kernel_param *kp)
797 static int param_get_aauint(
char *buffer,
const struct kernel_param *kp)
804 static int param_get_audit(
char *buffer,
struct kernel_param *kp)
809 if (!apparmor_enabled)
815 static int param_set_audit(
const char *val,
struct kernel_param *kp)
821 if (!apparmor_enabled)
837 static int param_get_mode(
char *buffer,
struct kernel_param *kp)
842 if (!apparmor_enabled)
848 static int param_set_mode(
const char *val,
struct kernel_param *kp)
854 if (!apparmor_enabled)
862 aa_g_profile_mode =
i;
879 static int __init set_init_cxt(
void)
881 struct cred *cred = (
struct cred *)
current->real_cred;
889 cred->security = cxt;
894 static int __init apparmor_init(
void)
900 apparmor_enabled = 0;
906 AA_ERROR(
"Unable to allocate default profile namespace\n");
910 error = set_init_cxt();
912 AA_ERROR(
"Failed to set context on init task\n");
913 goto register_security_out;
918 AA_ERROR(
"Unable to register AppArmor\n");
919 goto set_init_cxt_out;
923 apparmor_initialized = 1;
936 register_security_out:
942 apparmor_enabled = 0;