163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
1563e2b423SJohn Johansen #include <linux/security.h>
1663e2b423SJohn Johansen #include <linux/vmalloc.h>
1763e2b423SJohn Johansen #include <linux/module.h>
1863e2b423SJohn Johansen #include <linux/seq_file.h>
1963e2b423SJohn Johansen #include <linux/uaccess.h>
2063e2b423SJohn Johansen #include <linux/namei.h>
21e74abcf3SKees Cook #include <linux/capability.h>
2263e2b423SJohn Johansen 
2363e2b423SJohn Johansen #include "include/apparmor.h"
2463e2b423SJohn Johansen #include "include/apparmorfs.h"
2563e2b423SJohn Johansen #include "include/audit.h"
2663e2b423SJohn Johansen #include "include/context.h"
2763e2b423SJohn Johansen #include "include/policy.h"
28d384b0a1SKees Cook #include "include/resource.h"
2963e2b423SJohn Johansen 
3063e2b423SJohn Johansen /**
3163e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
3263e2b423SJohn Johansen  * @op: operation doing the user buffer copy
3363e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
343ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
3563e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
3663e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
3763e2b423SJohn Johansen  *
3863e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
3963e2b423SJohn Johansen  *          ERR_PTR on failure.
4063e2b423SJohn Johansen  */
4163e2b423SJohn Johansen static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
4263e2b423SJohn Johansen 				       size_t alloc_size, size_t copy_size,
4363e2b423SJohn Johansen 				       loff_t *pos)
4463e2b423SJohn Johansen {
4563e2b423SJohn Johansen 	char *data;
4663e2b423SJohn Johansen 
473ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
483ed02adaSJohn Johansen 
4963e2b423SJohn Johansen 	if (*pos != 0)
5063e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
5163e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
5263e2b423SJohn Johansen 
5363e2b423SJohn Johansen 	/*
5463e2b423SJohn Johansen 	 * Don't allow profile load/replace/remove from profiles that don't
5563e2b423SJohn Johansen 	 * have CAP_MAC_ADMIN
5663e2b423SJohn Johansen 	 */
5763e2b423SJohn Johansen 	if (!aa_may_manage_policy(op))
5863e2b423SJohn Johansen 		return ERR_PTR(-EACCES);
5963e2b423SJohn Johansen 
6063e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
6163e2b423SJohn Johansen 	data = kvmalloc(alloc_size);
6263e2b423SJohn Johansen 	if (data == NULL)
6363e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
6463e2b423SJohn Johansen 
6563e2b423SJohn Johansen 	if (copy_from_user(data, userbuf, copy_size)) {
6663e2b423SJohn Johansen 		kvfree(data);
6763e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
6863e2b423SJohn Johansen 	}
6963e2b423SJohn Johansen 
7063e2b423SJohn Johansen 	return data;
7163e2b423SJohn Johansen }
7263e2b423SJohn Johansen 
7363e2b423SJohn Johansen 
7463e2b423SJohn Johansen /* .load file hook fn to load policy */
7563e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
7663e2b423SJohn Johansen 			    loff_t *pos)
7763e2b423SJohn Johansen {
7863e2b423SJohn Johansen 	char *data;
7963e2b423SJohn Johansen 	ssize_t error;
8063e2b423SJohn Johansen 
8163e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
8263e2b423SJohn Johansen 
8363e2b423SJohn Johansen 	error = PTR_ERR(data);
8463e2b423SJohn Johansen 	if (!IS_ERR(data)) {
8563e2b423SJohn Johansen 		error = aa_replace_profiles(data, size, PROF_ADD);
8663e2b423SJohn Johansen 		kvfree(data);
8763e2b423SJohn Johansen 	}
8863e2b423SJohn Johansen 
8963e2b423SJohn Johansen 	return error;
9063e2b423SJohn Johansen }
9163e2b423SJohn Johansen 
9263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
936038f373SArnd Bergmann 	.write = profile_load,
946038f373SArnd Bergmann 	.llseek = default_llseek,
9563e2b423SJohn Johansen };
9663e2b423SJohn Johansen 
9763e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
9863e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
9963e2b423SJohn Johansen 			       size_t size, loff_t *pos)
10063e2b423SJohn Johansen {
10163e2b423SJohn Johansen 	char *data;
10263e2b423SJohn Johansen 	ssize_t error;
10363e2b423SJohn Johansen 
10463e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
10563e2b423SJohn Johansen 	error = PTR_ERR(data);
10663e2b423SJohn Johansen 	if (!IS_ERR(data)) {
10763e2b423SJohn Johansen 		error = aa_replace_profiles(data, size, PROF_REPLACE);
10863e2b423SJohn Johansen 		kvfree(data);
10963e2b423SJohn Johansen 	}
11063e2b423SJohn Johansen 
11163e2b423SJohn Johansen 	return error;
11263e2b423SJohn Johansen }
11363e2b423SJohn Johansen 
11463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1156038f373SArnd Bergmann 	.write = profile_replace,
1166038f373SArnd Bergmann 	.llseek = default_llseek,
11763e2b423SJohn Johansen };
11863e2b423SJohn Johansen 
11963e2b423SJohn Johansen /* .remove file hook fn to remove loaded policy */
12063e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
12163e2b423SJohn Johansen 			      size_t size, loff_t *pos)
12263e2b423SJohn Johansen {
12363e2b423SJohn Johansen 	char *data;
12463e2b423SJohn Johansen 	ssize_t error;
12563e2b423SJohn Johansen 
12663e2b423SJohn Johansen 	/*
12763e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
12863e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
12963e2b423SJohn Johansen 	 */
13063e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
13163e2b423SJohn Johansen 
13263e2b423SJohn Johansen 	error = PTR_ERR(data);
13363e2b423SJohn Johansen 	if (!IS_ERR(data)) {
13463e2b423SJohn Johansen 		data[size] = 0;
13563e2b423SJohn Johansen 		error = aa_remove_profiles(data, size);
13663e2b423SJohn Johansen 		kvfree(data);
13763e2b423SJohn Johansen 	}
13863e2b423SJohn Johansen 
13963e2b423SJohn Johansen 	return error;
14063e2b423SJohn Johansen }
14163e2b423SJohn Johansen 
14263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
1436038f373SArnd Bergmann 	.write = profile_remove,
1446038f373SArnd Bergmann 	.llseek = default_llseek,
14563e2b423SJohn Johansen };
14663e2b423SJohn Johansen 
147e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
148e74abcf3SKees Cook {
149e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
150e74abcf3SKees Cook 
151e74abcf3SKees Cook 	if (!fs_file)
152e74abcf3SKees Cook 		return 0;
153e74abcf3SKees Cook 
154e74abcf3SKees Cook 	switch (fs_file->v_type) {
155e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
156e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
157e74abcf3SKees Cook 		break;
158a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
159a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
160a9bf8e9fSKees Cook 		break;
161e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
162e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
163e74abcf3SKees Cook 		break;
164e74abcf3SKees Cook 	default:
165e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
166e74abcf3SKees Cook 		break;
167e74abcf3SKees Cook 	}
168e74abcf3SKees Cook 
169e74abcf3SKees Cook 	return 0;
170e74abcf3SKees Cook }
171e74abcf3SKees Cook 
172e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
173e74abcf3SKees Cook {
174e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
175e74abcf3SKees Cook }
176e74abcf3SKees Cook 
177e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
178e74abcf3SKees Cook 	.owner		= THIS_MODULE,
179e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
180e74abcf3SKees Cook 	.read		= seq_read,
181e74abcf3SKees Cook 	.llseek		= seq_lseek,
182e74abcf3SKees Cook 	.release	= single_release,
183e74abcf3SKees Cook };
184e74abcf3SKees Cook 
18563e2b423SJohn Johansen /** Base file system setup **/
18663e2b423SJohn Johansen 
187a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
188a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
189a9bf8e9fSKees Cook 				  "link lock"),
190a9bf8e9fSKees Cook 	{ }
191a9bf8e9fSKees Cook };
192a9bf8e9fSKees Cook 
193e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
194e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
195e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
196e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
197e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
198e74abcf3SKees Cook 	{ }
199e74abcf3SKees Cook };
200e74abcf3SKees Cook 
2019d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
2029d910a3bSJohn Johansen 	{}
2039d910a3bSJohn Johansen };
2049d910a3bSJohn Johansen 
205e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
2069d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
207e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
208a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
209e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
210d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
211e74abcf3SKees Cook 	{ }
212e74abcf3SKees Cook };
213e74abcf3SKees Cook 
2149acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
2159acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
2169acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
2179acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
218e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
2199acd494bSKees Cook 	{ }
2209acd494bSKees Cook };
22163e2b423SJohn Johansen 
2229acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
2239acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
2249acd494bSKees Cook 
2259acd494bSKees Cook /**
2269acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
2279acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
2289acd494bSKees Cook  * @parent: the parent dentry in the securityfs
2299acd494bSKees Cook  *
2309acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
2319acd494bSKees Cook  */
2329acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
2339acd494bSKees Cook 				   struct dentry *parent)
23463e2b423SJohn Johansen {
2359acd494bSKees Cook 	int error = 0;
23663e2b423SJohn Johansen 
2379acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
2389acd494bSKees Cook 						 S_IFREG | fs_file->mode,
2399acd494bSKees Cook 						 parent, fs_file,
2409acd494bSKees Cook 						 fs_file->file_ops);
2419acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
2429acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
2439acd494bSKees Cook 		fs_file->dentry = NULL;
24463e2b423SJohn Johansen 	}
2459acd494bSKees Cook 	return error;
24663e2b423SJohn Johansen }
24763e2b423SJohn Johansen 
24863e2b423SJohn Johansen /**
2499acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
2509acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
2519acd494bSKees Cook  * @parent: the parent dentry in the securityfs
25263e2b423SJohn Johansen  *
2539acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
25463e2b423SJohn Johansen  */
2559acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
2569acd494bSKees Cook 				  struct dentry *parent)
25763e2b423SJohn Johansen {
2589acd494bSKees Cook 	int error;
2599acd494bSKees Cook 	struct aa_fs_entry *fs_file;
26063e2b423SJohn Johansen 
2619acd494bSKees Cook 	fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent);
2629acd494bSKees Cook 	if (IS_ERR(fs_dir->dentry)) {
2639acd494bSKees Cook 		error = PTR_ERR(fs_dir->dentry);
2649acd494bSKees Cook 		fs_dir->dentry = NULL;
2659acd494bSKees Cook 		goto failed;
2669acd494bSKees Cook 	}
26763e2b423SJohn Johansen 
2689acd494bSKees Cook 	for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
2699acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
2709acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
2719acd494bSKees Cook 		else
2729acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
2739acd494bSKees Cook 		if (error)
2749acd494bSKees Cook 			goto failed;
2759acd494bSKees Cook 	}
2769acd494bSKees Cook 
2779acd494bSKees Cook 	return 0;
2789acd494bSKees Cook 
2799acd494bSKees Cook failed:
2809acd494bSKees Cook 	return error;
2819acd494bSKees Cook }
2829acd494bSKees Cook 
2839acd494bSKees Cook /**
2849acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
2859acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
2869acd494bSKees Cook  */
2879acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
2889acd494bSKees Cook {
2899acd494bSKees Cook 	if (!fs_file->dentry)
2909acd494bSKees Cook 		return;
2919acd494bSKees Cook 
2929acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
2939acd494bSKees Cook 	fs_file->dentry = NULL;
2949acd494bSKees Cook }
2959acd494bSKees Cook 
2969acd494bSKees Cook /**
2979acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
2989acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
2999acd494bSKees Cook  */
3009acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
3019acd494bSKees Cook {
3029acd494bSKees Cook 	struct aa_fs_entry *fs_file;
3039acd494bSKees Cook 
3049acd494bSKees Cook 	for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
3059acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
3069acd494bSKees Cook 			aafs_remove_dir(fs_file);
3079acd494bSKees Cook 		else
3089acd494bSKees Cook 			aafs_remove_file(fs_file);
3099acd494bSKees Cook 	}
3109acd494bSKees Cook 
3119acd494bSKees Cook 	aafs_remove_file(fs_dir);
31263e2b423SJohn Johansen }
31363e2b423SJohn Johansen 
31463e2b423SJohn Johansen /**
31563e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
31663e2b423SJohn Johansen  *
31763e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
31863e2b423SJohn Johansen  */
31963e2b423SJohn Johansen void __init aa_destroy_aafs(void)
32063e2b423SJohn Johansen {
3219acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
32263e2b423SJohn Johansen }
32363e2b423SJohn Johansen 
32463e2b423SJohn Johansen /**
32563e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
32663e2b423SJohn Johansen  *
32763e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
32863e2b423SJohn Johansen  *
32963e2b423SJohn Johansen  * Returns: error on failure
33063e2b423SJohn Johansen  */
3313417d8d5SJames Morris static int __init aa_create_aafs(void)
33263e2b423SJohn Johansen {
33363e2b423SJohn Johansen 	int error;
33463e2b423SJohn Johansen 
33563e2b423SJohn Johansen 	if (!apparmor_initialized)
33663e2b423SJohn Johansen 		return 0;
33763e2b423SJohn Johansen 
3389acd494bSKees Cook 	if (aa_fs_entry.dentry) {
33963e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
34063e2b423SJohn Johansen 		return -EEXIST;
34163e2b423SJohn Johansen 	}
34263e2b423SJohn Johansen 
3439acd494bSKees Cook 	/* Populate fs tree. */
3449acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
34563e2b423SJohn Johansen 	if (error)
34663e2b423SJohn Johansen 		goto error;
34763e2b423SJohn Johansen 
34863e2b423SJohn Johansen 	/* TODO: add support for apparmorfs_null and apparmorfs_mnt */
34963e2b423SJohn Johansen 
35063e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
35163e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
35263e2b423SJohn Johansen 	return 0;
35363e2b423SJohn Johansen 
35463e2b423SJohn Johansen error:
35563e2b423SJohn Johansen 	aa_destroy_aafs();
35663e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
35763e2b423SJohn Johansen 	return error;
35863e2b423SJohn Johansen }
35963e2b423SJohn Johansen 
36063e2b423SJohn Johansen fs_initcall(aa_create_aafs);
361