blob: 3ed56e21a9fd973d851c21e391d60dc88c7a743e [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"
Kees Cookd384b0a2012-01-26 16:29:23 -080028#include "include/resource.h"
John Johansen63e2b422010-07-29 14:48:03 -070029
30/**
31 * aa_simple_write_to_buffer - common routine for getting policy from user
32 * @op: operation doing the user buffer copy
33 * @userbuf: user buffer to copy data from (NOT NULL)
John Johansen3ed02ad2010-10-09 00:47:53 -070034 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
John Johansen63e2b422010-07-29 14:48:03 -070035 * @copy_size: size of data to copy from user buffer
36 * @pos: position write is at in the file (NOT NULL)
37 *
38 * Returns: kernel buffer containing copy of user buffer data or an
39 * ERR_PTR on failure.
40 */
41static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
42 size_t alloc_size, size_t copy_size,
43 loff_t *pos)
44{
45 char *data;
46
John Johansen3ed02ad2010-10-09 00:47:53 -070047 BUG_ON(copy_size > alloc_size);
48
John Johansen63e2b422010-07-29 14:48:03 -070049 if (*pos != 0)
50 /* only writes from pos 0, that is complete writes */
51 return ERR_PTR(-ESPIPE);
52
53 /*
54 * Don't allow profile load/replace/remove from profiles that don't
55 * have CAP_MAC_ADMIN
56 */
57 if (!aa_may_manage_policy(op))
58 return ERR_PTR(-EACCES);
59
60 /* freed by caller to simple_write_to_buffer */
61 data = kvmalloc(alloc_size);
62 if (data == NULL)
63 return ERR_PTR(-ENOMEM);
64
65 if (copy_from_user(data, userbuf, copy_size)) {
66 kvfree(data);
67 return ERR_PTR(-EFAULT);
68 }
69
70 return data;
71}
72
73
74/* .load file hook fn to load policy */
75static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
76 loff_t *pos)
77{
78 char *data;
79 ssize_t error;
80
81 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
82
83 error = PTR_ERR(data);
84 if (!IS_ERR(data)) {
85 error = aa_replace_profiles(data, size, PROF_ADD);
86 kvfree(data);
87 }
88
89 return error;
90}
91
92static const struct file_operations aa_fs_profile_load = {
Arnd Bergmann6038f372010-08-15 18:52:59 +020093 .write = profile_load,
94 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -070095};
96
97/* .replace file hook fn to load and/or replace policy */
98static ssize_t profile_replace(struct file *f, const char __user *buf,
99 size_t size, loff_t *pos)
100{
101 char *data;
102 ssize_t error;
103
104 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
105 error = PTR_ERR(data);
106 if (!IS_ERR(data)) {
107 error = aa_replace_profiles(data, size, PROF_REPLACE);
108 kvfree(data);
109 }
110
111 return error;
112}
113
114static const struct file_operations aa_fs_profile_replace = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200115 .write = profile_replace,
116 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700117};
118
119/* .remove file hook fn to remove loaded policy */
120static ssize_t profile_remove(struct file *f, const char __user *buf,
121 size_t size, loff_t *pos)
122{
123 char *data;
124 ssize_t error;
125
126 /*
127 * aa_remove_profile needs a null terminated string so 1 extra
128 * byte is allocated and the copied data is null terminated.
129 */
130 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
131
132 error = PTR_ERR(data);
133 if (!IS_ERR(data)) {
134 data[size] = 0;
135 error = aa_remove_profiles(data, size);
136 kvfree(data);
137 }
138
139 return error;
140}
141
142static const struct file_operations aa_fs_profile_remove = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200143 .write = profile_remove,
144 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700145};
146
Kees Cooke74abcf2012-01-26 16:29:21 -0800147static int aa_fs_seq_show(struct seq_file *seq, void *v)
148{
149 struct aa_fs_entry *fs_file = seq->private;
150
151 if (!fs_file)
152 return 0;
153
154 switch (fs_file->v_type) {
155 case AA_FS_TYPE_BOOLEAN:
156 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
157 break;
Kees Cooka9bf8e92012-01-26 16:29:22 -0800158 case AA_FS_TYPE_STRING:
159 seq_printf(seq, "%s\n", fs_file->v.string);
160 break;
Kees Cooke74abcf2012-01-26 16:29:21 -0800161 case AA_FS_TYPE_U64:
162 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
163 break;
164 default:
165 /* Ignore unpritable entry types. */
166 break;
167 }
168
169 return 0;
170}
171
172static int aa_fs_seq_open(struct inode *inode, struct file *file)
173{
174 return single_open(file, aa_fs_seq_show, inode->i_private);
175}
176
177const struct file_operations aa_fs_seq_file_ops = {
178 .owner = THIS_MODULE,
179 .open = aa_fs_seq_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
John Johansen63e2b422010-07-29 14:48:03 -0700185/** Base file system setup **/
186
Kees Cooka9bf8e92012-01-26 16:29:22 -0800187static struct aa_fs_entry aa_fs_entry_file[] = {
188 AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
189 "link lock"),
190 { }
191};
192
Kees Cooke74abcf2012-01-26 16:29:21 -0800193static struct aa_fs_entry aa_fs_entry_domain[] = {
194 AA_FS_FILE_BOOLEAN("change_hat", 1),
195 AA_FS_FILE_BOOLEAN("change_hatv", 1),
196 AA_FS_FILE_BOOLEAN("change_onexec", 1),
197 AA_FS_FILE_BOOLEAN("change_profile", 1),
198 { }
199};
200
John Johansen9d910a32013-07-10 21:04:43 -0700201static struct aa_fs_entry aa_fs_entry_policy[] = {
John Johansendd51c8482013-07-10 21:05:43 -0700202 AA_FS_FILE_BOOLEAN("set_load", 1),
John Johansen9d910a32013-07-10 21:04:43 -0700203 {}
204};
205
Kees Cooke74abcf2012-01-26 16:29:21 -0800206static struct aa_fs_entry aa_fs_entry_features[] = {
John Johansen9d910a32013-07-10 21:04:43 -0700207 AA_FS_DIR("policy", aa_fs_entry_policy),
Kees Cooke74abcf2012-01-26 16:29:21 -0800208 AA_FS_DIR("domain", aa_fs_entry_domain),
Kees Cooka9bf8e92012-01-26 16:29:22 -0800209 AA_FS_DIR("file", aa_fs_entry_file),
Kees Cooke74abcf2012-01-26 16:29:21 -0800210 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
Kees Cookd384b0a2012-01-26 16:29:23 -0800211 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
Kees Cooke74abcf2012-01-26 16:29:21 -0800212 { }
213};
214
Kees Cook9acd4942012-01-26 16:29:20 -0800215static struct aa_fs_entry aa_fs_entry_apparmor[] = {
216 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
217 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
218 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
Kees Cooke74abcf2012-01-26 16:29:21 -0800219 AA_FS_DIR("features", aa_fs_entry_features),
Kees Cook9acd4942012-01-26 16:29:20 -0800220 { }
221};
John Johansen63e2b422010-07-29 14:48:03 -0700222
Kees Cook9acd4942012-01-26 16:29:20 -0800223static struct aa_fs_entry aa_fs_entry =
224 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
225
226/**
227 * aafs_create_file - create a file entry in the apparmor securityfs
228 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
229 * @parent: the parent dentry in the securityfs
230 *
231 * Use aafs_remove_file to remove entries created with this fn.
232 */
233static int __init aafs_create_file(struct aa_fs_entry *fs_file,
234 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700235{
Kees Cook9acd4942012-01-26 16:29:20 -0800236 int error = 0;
John Johansen63e2b422010-07-29 14:48:03 -0700237
Kees Cook9acd4942012-01-26 16:29:20 -0800238 fs_file->dentry = securityfs_create_file(fs_file->name,
239 S_IFREG | fs_file->mode,
240 parent, fs_file,
241 fs_file->file_ops);
242 if (IS_ERR(fs_file->dentry)) {
243 error = PTR_ERR(fs_file->dentry);
244 fs_file->dentry = NULL;
John Johansen63e2b422010-07-29 14:48:03 -0700245 }
Kees Cook9acd4942012-01-26 16:29:20 -0800246 return error;
John Johansen63e2b422010-07-29 14:48:03 -0700247}
248
249/**
Kees Cook9acd4942012-01-26 16:29:20 -0800250 * aafs_create_dir - recursively create a directory entry in the securityfs
251 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
252 * @parent: the parent dentry in the securityfs
John Johansen63e2b422010-07-29 14:48:03 -0700253 *
Kees Cook9acd4942012-01-26 16:29:20 -0800254 * Use aafs_remove_dir to remove entries created with this fn.
John Johansen63e2b422010-07-29 14:48:03 -0700255 */
Kees Cook9acd4942012-01-26 16:29:20 -0800256static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
257 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700258{
Kees Cook9acd4942012-01-26 16:29:20 -0800259 int error;
260 struct aa_fs_entry *fs_file;
John Johansen63e2b422010-07-29 14:48:03 -0700261
Kees Cook9acd4942012-01-26 16:29:20 -0800262 fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent);
263 if (IS_ERR(fs_dir->dentry)) {
264 error = PTR_ERR(fs_dir->dentry);
265 fs_dir->dentry = NULL;
266 goto failed;
267 }
John Johansen63e2b422010-07-29 14:48:03 -0700268
Kees Cook9acd4942012-01-26 16:29:20 -0800269 for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
270 if (fs_file->v_type == AA_FS_TYPE_DIR)
271 error = aafs_create_dir(fs_file, fs_dir->dentry);
272 else
273 error = aafs_create_file(fs_file, fs_dir->dentry);
274 if (error)
275 goto failed;
276 }
277
278 return 0;
279
280failed:
281 return error;
282}
283
284/**
285 * aafs_remove_file - drop a single file entry in the apparmor securityfs
286 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
287 */
288static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
289{
290 if (!fs_file->dentry)
291 return;
292
293 securityfs_remove(fs_file->dentry);
294 fs_file->dentry = NULL;
295}
296
297/**
298 * aafs_remove_dir - recursively drop a directory entry from the securityfs
299 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
300 */
301static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
302{
303 struct aa_fs_entry *fs_file;
304
305 for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) {
306 if (fs_file->v_type == AA_FS_TYPE_DIR)
307 aafs_remove_dir(fs_file);
308 else
309 aafs_remove_file(fs_file);
310 }
311
312 aafs_remove_file(fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700313}
314
315/**
316 * aa_destroy_aafs - cleanup and free aafs
317 *
318 * releases dentries allocated by aa_create_aafs
319 */
320void __init aa_destroy_aafs(void)
321{
Kees Cook9acd4942012-01-26 16:29:20 -0800322 aafs_remove_dir(&aa_fs_entry);
John Johansen63e2b422010-07-29 14:48:03 -0700323}
324
325/**
326 * aa_create_aafs - create the apparmor security filesystem
327 *
328 * dentries created here are released by aa_destroy_aafs
329 *
330 * Returns: error on failure
331 */
James Morris3417d8d2011-08-17 11:05:21 +1000332static int __init aa_create_aafs(void)
John Johansen63e2b422010-07-29 14:48:03 -0700333{
334 int error;
335
336 if (!apparmor_initialized)
337 return 0;
338
Kees Cook9acd4942012-01-26 16:29:20 -0800339 if (aa_fs_entry.dentry) {
John Johansen63e2b422010-07-29 14:48:03 -0700340 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
341 return -EEXIST;
342 }
343
Kees Cook9acd4942012-01-26 16:29:20 -0800344 /* Populate fs tree. */
345 error = aafs_create_dir(&aa_fs_entry, NULL);
John Johansen63e2b422010-07-29 14:48:03 -0700346 if (error)
347 goto error;
348
349 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
350
351 /* Report that AppArmor fs is enabled */
352 aa_info_message("AppArmor Filesystem Enabled");
353 return 0;
354
355error:
356 aa_destroy_aafs();
357 AA_ERROR("Error creating AppArmor securityfs\n");
358 return error;
359}
360
361fs_initcall(aa_create_aafs);