blob: f30dada0dca2d8cf1b525122d1afb197e82e4d1d [file] [log] [blame]
John Johansen63e2b422010-07-29 14:48:03 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
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
12 * License.
13 */
14
15#include <linux/security.h>
16#include <linux/vmalloc.h>
17#include <linux/module.h>
18#include <linux/seq_file.h>
19#include <linux/uaccess.h>
20#include <linux/namei.h>
Kees Cooke74abcf2012-01-26 16:29:21 -080021#include <linux/capability.h>
John Johansen63e2b422010-07-29 14:48:03 -070022
23#include "include/apparmor.h"
24#include "include/apparmorfs.h"
25#include "include/audit.h"
26#include "include/context.h"
27#include "include/policy.h"
28
29/**
30 * aa_simple_write_to_buffer - common routine for getting policy from user
31 * @op: operation doing the user buffer copy
32 * @userbuf: user buffer to copy data from (NOT NULL)
John Johansen3ed02ad2010-10-09 00:47:53 -070033 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
John Johansen63e2b422010-07-29 14:48:03 -070034 * @copy_size: size of data to copy from user buffer
35 * @pos: position write is at in the file (NOT NULL)
36 *
37 * Returns: kernel buffer containing copy of user buffer data or an
38 * ERR_PTR on failure.
39 */
40static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
41 size_t alloc_size, size_t copy_size,
42 loff_t *pos)
43{
44 char *data;
45
John Johansen3ed02ad2010-10-09 00:47:53 -070046 BUG_ON(copy_size > alloc_size);
47
John Johansen63e2b422010-07-29 14:48:03 -070048 if (*pos != 0)
49 /* only writes from pos 0, that is complete writes */
50 return ERR_PTR(-ESPIPE);
51
52 /*
53 * Don't allow profile load/replace/remove from profiles that don't
54 * have CAP_MAC_ADMIN
55 */
56 if (!aa_may_manage_policy(op))
57 return ERR_PTR(-EACCES);
58
59 /* freed by caller to simple_write_to_buffer */
60 data = kvmalloc(alloc_size);
61 if (data == NULL)
62 return ERR_PTR(-ENOMEM);
63
64 if (copy_from_user(data, userbuf, copy_size)) {
65 kvfree(data);
66 return ERR_PTR(-EFAULT);
67 }
68
69 return data;
70}
71
72
73/* .load file hook fn to load policy */
74static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
75 loff_t *pos)
76{
77 char *data;
78 ssize_t error;
79
80 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
81
82 error = PTR_ERR(data);
83 if (!IS_ERR(data)) {
84 error = aa_replace_profiles(data, size, PROF_ADD);
85 kvfree(data);
86 }
87
88 return error;
89}
90
91static const struct file_operations aa_fs_profile_load = {
Arnd Bergmann6038f372010-08-15 18:52:59 +020092 .write = profile_load,
93 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -070094};
95
96/* .replace file hook fn to load and/or replace policy */
97static ssize_t profile_replace(struct file *f, const char __user *buf,
98 size_t size, loff_t *pos)
99{
100 char *data;
101 ssize_t error;
102
103 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
104 error = PTR_ERR(data);
105 if (!IS_ERR(data)) {
106 error = aa_replace_profiles(data, size, PROF_REPLACE);
107 kvfree(data);
108 }
109
110 return error;
111}
112
113static const struct file_operations aa_fs_profile_replace = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200114 .write = profile_replace,
115 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700116};
117
118/* .remove file hook fn to remove loaded policy */
119static ssize_t profile_remove(struct file *f, const char __user *buf,
120 size_t size, loff_t *pos)
121{
122 char *data;
123 ssize_t error;
124
125 /*
126 * aa_remove_profile needs a null terminated string so 1 extra
127 * byte is allocated and the copied data is null terminated.
128 */
129 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
130
131 error = PTR_ERR(data);
132 if (!IS_ERR(data)) {
133 data[size] = 0;
134 error = aa_remove_profiles(data, size);
135 kvfree(data);
136 }
137
138 return error;
139}
140
141static const struct file_operations aa_fs_profile_remove = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200142 .write = profile_remove,
143 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700144};
145
Kees Cooke74abcf2012-01-26 16:29:21 -0800146static int aa_fs_seq_show(struct seq_file *seq, void *v)
147{
148 struct aa_fs_entry *fs_file = seq->private;
149
150 if (!fs_file)
151 return 0;
152
153 switch (fs_file->v_type) {
154 case AA_FS_TYPE_BOOLEAN:
155 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
156 break;
157 case AA_FS_TYPE_U64:
158 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
159 break;
160 default:
161 /* Ignore unpritable entry types. */
162 break;
163 }
164
165 return 0;
166}
167
168static int aa_fs_seq_open(struct inode *inode, struct file *file)
169{
170 return single_open(file, aa_fs_seq_show, inode->i_private);
171}
172
173const struct file_operations aa_fs_seq_file_ops = {
174 .owner = THIS_MODULE,
175 .open = aa_fs_seq_open,
176 .read = seq_read,
177 .llseek = seq_lseek,
178 .release = single_release,
179};
180
John Johansen63e2b422010-07-29 14:48:03 -0700181/** Base file system setup **/
182
Kees Cooke74abcf2012-01-26 16:29:21 -0800183static struct aa_fs_entry aa_fs_entry_domain[] = {
184 AA_FS_FILE_BOOLEAN("change_hat", 1),
185 AA_FS_FILE_BOOLEAN("change_hatv", 1),
186 AA_FS_FILE_BOOLEAN("change_onexec", 1),
187 AA_FS_FILE_BOOLEAN("change_profile", 1),
188 { }
189};
190
191static struct aa_fs_entry aa_fs_entry_features[] = {
192 AA_FS_DIR("domain", aa_fs_entry_domain),
193 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
194 { }
195};
196
Kees Cook9acd4942012-01-26 16:29:20 -0800197static struct aa_fs_entry aa_fs_entry_apparmor[] = {
198 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
199 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
200 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
Kees Cooke74abcf2012-01-26 16:29:21 -0800201 AA_FS_DIR("features", aa_fs_entry_features),
Kees Cook9acd4942012-01-26 16:29:20 -0800202 { }
203};
John Johansen63e2b422010-07-29 14:48:03 -0700204
Kees Cook9acd4942012-01-26 16:29:20 -0800205static struct aa_fs_entry aa_fs_entry =
206 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
207
208/**
209 * aafs_create_file - create a file entry in the apparmor securityfs
210 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
211 * @parent: the parent dentry in the securityfs
212 *
213 * Use aafs_remove_file to remove entries created with this fn.
214 */
215static int __init aafs_create_file(struct aa_fs_entry *fs_file,
216 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700217{
Kees Cook9acd4942012-01-26 16:29:20 -0800218 int error = 0;
John Johansen63e2b422010-07-29 14:48:03 -0700219
Kees Cook9acd4942012-01-26 16:29:20 -0800220 fs_file->dentry = securityfs_create_file(fs_file->name,
221 S_IFREG | fs_file->mode,
222 parent, fs_file,
223 fs_file->file_ops);
224 if (IS_ERR(fs_file->dentry)) {
225 error = PTR_ERR(fs_file->dentry);
226 fs_file->dentry = NULL;
John Johansen63e2b422010-07-29 14:48:03 -0700227 }
Kees Cook9acd4942012-01-26 16:29:20 -0800228 return error;
John Johansen63e2b422010-07-29 14:48:03 -0700229}
230
231/**
Kees Cook9acd4942012-01-26 16:29:20 -0800232 * aafs_create_dir - recursively create a directory entry in the securityfs
233 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
234 * @parent: the parent dentry in the securityfs
John Johansen63e2b422010-07-29 14:48:03 -0700235 *
Kees Cook9acd4942012-01-26 16:29:20 -0800236 * Use aafs_remove_dir to remove entries created with this fn.
John Johansen63e2b422010-07-29 14:48:03 -0700237 */
Kees Cook9acd4942012-01-26 16:29:20 -0800238static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
239 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700240{
Kees Cook9acd4942012-01-26 16:29:20 -0800241 int error;
242 struct aa_fs_entry *fs_file;
John Johansen63e2b422010-07-29 14:48:03 -0700243
Kees Cook9acd4942012-01-26 16:29:20 -0800244 fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent);
245 if (IS_ERR(fs_dir->dentry)) {
246 error = PTR_ERR(fs_dir->dentry);
247 fs_dir->dentry = NULL;
248 goto failed;
249 }
John Johansen63e2b422010-07-29 14:48:03 -0700250
Kees Cook9acd4942012-01-26 16:29:20 -0800251 for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
252 if (fs_file->v_type == AA_FS_TYPE_DIR)
253 error = aafs_create_dir(fs_file, fs_dir->dentry);
254 else
255 error = aafs_create_file(fs_file, fs_dir->dentry);
256 if (error)
257 goto failed;
258 }
259
260 return 0;
261
262failed:
263 return error;
264}
265
266/**
267 * aafs_remove_file - drop a single file entry in the apparmor securityfs
268 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
269 */
270static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
271{
272 if (!fs_file->dentry)
273 return;
274
275 securityfs_remove(fs_file->dentry);
276 fs_file->dentry = NULL;
277}
278
279/**
280 * aafs_remove_dir - recursively drop a directory entry from the securityfs
281 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
282 */
283static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
284{
285 struct aa_fs_entry *fs_file;
286
287 for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
288 if (fs_file->v_type == AA_FS_TYPE_DIR)
289 aafs_remove_dir(fs_file);
290 else
291 aafs_remove_file(fs_file);
292 }
293
294 aafs_remove_file(fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700295}
296
297/**
298 * aa_destroy_aafs - cleanup and free aafs
299 *
300 * releases dentries allocated by aa_create_aafs
301 */
302void __init aa_destroy_aafs(void)
303{
Kees Cook9acd4942012-01-26 16:29:20 -0800304 aafs_remove_dir(&aa_fs_entry);
John Johansen63e2b422010-07-29 14:48:03 -0700305}
306
307/**
308 * aa_create_aafs - create the apparmor security filesystem
309 *
310 * dentries created here are released by aa_destroy_aafs
311 *
312 * Returns: error on failure
313 */
James Morris3417d8d2011-08-17 11:05:21 +1000314static int __init aa_create_aafs(void)
John Johansen63e2b422010-07-29 14:48:03 -0700315{
316 int error;
317
318 if (!apparmor_initialized)
319 return 0;
320
Kees Cook9acd4942012-01-26 16:29:20 -0800321 if (aa_fs_entry.dentry) {
John Johansen63e2b422010-07-29 14:48:03 -0700322 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
323 return -EEXIST;
324 }
325
Kees Cook9acd4942012-01-26 16:29:20 -0800326 /* Populate fs tree. */
327 error = aafs_create_dir(&aa_fs_entry, NULL);
John Johansen63e2b422010-07-29 14:48:03 -0700328 if (error)
329 goto error;
330
331 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
332
333 /* Report that AppArmor fs is enabled */
334 aa_info_message("AppArmor Filesystem Enabled");
335 return 0;
336
337error:
338 aa_destroy_aafs();
339 AA_ERROR("Error creating AppArmor securityfs\n");
340 return error;
341}
342
343fs_initcall(aa_create_aafs);