llseek: automatically add .llseek fop
[linux-flexiantxendom0-3.2.10.git] / security / apparmor / apparmorfs.c
1 /*
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>
21
22 #include "include/apparmor.h"
23 #include "include/apparmorfs.h"
24 #include "include/audit.h"
25 #include "include/context.h"
26 #include "include/policy.h"
27
28 /**
29  * aa_simple_write_to_buffer - common routine for getting policy from user
30  * @op: operation doing the user buffer copy
31  * @userbuf: user buffer to copy data from  (NOT NULL)
32  * @alloc_size: size of user buffer
33  * @copy_size: size of data to copy from user buffer
34  * @pos: position write is at in the file (NOT NULL)
35  *
36  * Returns: kernel buffer containing copy of user buffer data or an
37  *          ERR_PTR on failure.
38  */
39 static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
40                                        size_t alloc_size, size_t copy_size,
41                                        loff_t *pos)
42 {
43         char *data;
44
45         if (*pos != 0)
46                 /* only writes from pos 0, that is complete writes */
47                 return ERR_PTR(-ESPIPE);
48
49         /*
50          * Don't allow profile load/replace/remove from profiles that don't
51          * have CAP_MAC_ADMIN
52          */
53         if (!aa_may_manage_policy(op))
54                 return ERR_PTR(-EACCES);
55
56         /* freed by caller to simple_write_to_buffer */
57         data = kvmalloc(alloc_size);
58         if (data == NULL)
59                 return ERR_PTR(-ENOMEM);
60
61         if (copy_from_user(data, userbuf, copy_size)) {
62                 kvfree(data);
63                 return ERR_PTR(-EFAULT);
64         }
65
66         return data;
67 }
68
69
70 /* .load file hook fn to load policy */
71 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
72                             loff_t *pos)
73 {
74         char *data;
75         ssize_t error;
76
77         data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
78
79         error = PTR_ERR(data);
80         if (!IS_ERR(data)) {
81                 error = aa_replace_profiles(data, size, PROF_ADD);
82                 kvfree(data);
83         }
84
85         return error;
86 }
87
88 static const struct file_operations aa_fs_profile_load = {
89         .write = profile_load,
90         .llseek = default_llseek,
91 };
92
93 /* .replace file hook fn to load and/or replace policy */
94 static ssize_t profile_replace(struct file *f, const char __user *buf,
95                                size_t size, loff_t *pos)
96 {
97         char *data;
98         ssize_t error;
99
100         data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
101         error = PTR_ERR(data);
102         if (!IS_ERR(data)) {
103                 error = aa_replace_profiles(data, size, PROF_REPLACE);
104                 kvfree(data);
105         }
106
107         return error;
108 }
109
110 static const struct file_operations aa_fs_profile_replace = {
111         .write = profile_replace,
112         .llseek = default_llseek,
113 };
114
115 /* .remove file hook fn to remove loaded policy */
116 static ssize_t profile_remove(struct file *f, const char __user *buf,
117                               size_t size, loff_t *pos)
118 {
119         char *data;
120         ssize_t error;
121
122         /*
123          * aa_remove_profile needs a null terminated string so 1 extra
124          * byte is allocated and the copied data is null terminated.
125          */
126         data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
127
128         error = PTR_ERR(data);
129         if (!IS_ERR(data)) {
130                 data[size] = 0;
131                 error = aa_remove_profiles(data, size);
132                 kvfree(data);
133         }
134
135         return error;
136 }
137
138 static const struct file_operations aa_fs_profile_remove = {
139         .write = profile_remove,
140         .llseek = default_llseek,
141 };
142
143 /** Base file system setup **/
144
145 static struct dentry *aa_fs_dentry __initdata;
146
147 static void __init aafs_remove(const char *name)
148 {
149         struct dentry *dentry;
150
151         dentry = lookup_one_len(name, aa_fs_dentry, strlen(name));
152         if (!IS_ERR(dentry)) {
153                 securityfs_remove(dentry);
154                 dput(dentry);
155         }
156 }
157
158 /**
159  * aafs_create - create an entry in the apparmor filesystem
160  * @name: name of the entry (NOT NULL)
161  * @mask: file permission mask of the file
162  * @fops: file operations for the file (NOT NULL)
163  *
164  * Used aafs_remove to remove entries created with this fn.
165  */
166 static int __init aafs_create(const char *name, int mask,
167                               const struct file_operations *fops)
168 {
169         struct dentry *dentry;
170
171         dentry = securityfs_create_file(name, S_IFREG | mask, aa_fs_dentry,
172                                         NULL, fops);
173
174         return IS_ERR(dentry) ? PTR_ERR(dentry) : 0;
175 }
176
177 /**
178  * aa_destroy_aafs - cleanup and free aafs
179  *
180  * releases dentries allocated by aa_create_aafs
181  */
182 void __init aa_destroy_aafs(void)
183 {
184         if (aa_fs_dentry) {
185                 aafs_remove(".remove");
186                 aafs_remove(".replace");
187                 aafs_remove(".load");
188
189                 securityfs_remove(aa_fs_dentry);
190                 aa_fs_dentry = NULL;
191         }
192 }
193
194 /**
195  * aa_create_aafs - create the apparmor security filesystem
196  *
197  * dentries created here are released by aa_destroy_aafs
198  *
199  * Returns: error on failure
200  */
201 int __init aa_create_aafs(void)
202 {
203         int error;
204
205         if (!apparmor_initialized)
206                 return 0;
207
208         if (aa_fs_dentry) {
209                 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
210                 return -EEXIST;
211         }
212
213         aa_fs_dentry = securityfs_create_dir("apparmor", NULL);
214         if (IS_ERR(aa_fs_dentry)) {
215                 error = PTR_ERR(aa_fs_dentry);
216                 aa_fs_dentry = NULL;
217                 goto error;
218         }
219
220         error = aafs_create(".load", 0640, &aa_fs_profile_load);
221         if (error)
222                 goto error;
223         error = aafs_create(".replace", 0640, &aa_fs_profile_replace);
224         if (error)
225                 goto error;
226         error = aafs_create(".remove", 0640, &aa_fs_profile_remove);
227         if (error)
228                 goto error;
229
230         /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
231
232         /* Report that AppArmor fs is enabled */
233         aa_info_message("AppArmor Filesystem Enabled");
234         return 0;
235
236 error:
237         aa_destroy_aafs();
238         AA_ERROR("Error creating AppArmor securityfs\n");
239         return error;
240 }
241
242 fs_initcall(aa_create_aafs);