]>
Commit | Line | Data |
---|---|---|
63e2b423 JJ |
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) | |
3ed02ada | 32 | * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) |
63e2b423 JJ |
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 | ||
3ed02ada JJ |
45 | BUG_ON(copy_size > alloc_size); |
46 | ||
63e2b423 JJ |
47 | if (*pos != 0) |
48 | /* only writes from pos 0, that is complete writes */ | |
49 | return ERR_PTR(-ESPIPE); | |
50 | ||
51 | /* | |
52 | * Don't allow profile load/replace/remove from profiles that don't | |
53 | * have CAP_MAC_ADMIN | |
54 | */ | |
55 | if (!aa_may_manage_policy(op)) | |
56 | return ERR_PTR(-EACCES); | |
57 | ||
58 | /* freed by caller to simple_write_to_buffer */ | |
59 | data = kvmalloc(alloc_size); | |
60 | if (data == NULL) | |
61 | return ERR_PTR(-ENOMEM); | |
62 | ||
63 | if (copy_from_user(data, userbuf, copy_size)) { | |
64 | kvfree(data); | |
65 | return ERR_PTR(-EFAULT); | |
66 | } | |
67 | ||
68 | return data; | |
69 | } | |
70 | ||
71 | ||
72 | /* .load file hook fn to load policy */ | |
73 | static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, | |
74 | loff_t *pos) | |
75 | { | |
76 | char *data; | |
77 | ssize_t error; | |
78 | ||
79 | data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos); | |
80 | ||
81 | error = PTR_ERR(data); | |
82 | if (!IS_ERR(data)) { | |
83 | error = aa_replace_profiles(data, size, PROF_ADD); | |
84 | kvfree(data); | |
85 | } | |
86 | ||
87 | return error; | |
88 | } | |
89 | ||
90 | static const struct file_operations aa_fs_profile_load = { | |
6038f373 AB |
91 | .write = profile_load, |
92 | .llseek = default_llseek, | |
63e2b423 JJ |
93 | }; |
94 | ||
95 | /* .replace file hook fn to load and/or replace policy */ | |
96 | static ssize_t profile_replace(struct file *f, const char __user *buf, | |
97 | size_t size, loff_t *pos) | |
98 | { | |
99 | char *data; | |
100 | ssize_t error; | |
101 | ||
102 | data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos); | |
103 | error = PTR_ERR(data); | |
104 | if (!IS_ERR(data)) { | |
105 | error = aa_replace_profiles(data, size, PROF_REPLACE); | |
106 | kvfree(data); | |
107 | } | |
108 | ||
109 | return error; | |
110 | } | |
111 | ||
112 | static const struct file_operations aa_fs_profile_replace = { | |
6038f373 AB |
113 | .write = profile_replace, |
114 | .llseek = default_llseek, | |
63e2b423 JJ |
115 | }; |
116 | ||
117 | /* .remove file hook fn to remove loaded policy */ | |
118 | static ssize_t profile_remove(struct file *f, const char __user *buf, | |
119 | size_t size, loff_t *pos) | |
120 | { | |
121 | char *data; | |
122 | ssize_t error; | |
123 | ||
124 | /* | |
125 | * aa_remove_profile needs a null terminated string so 1 extra | |
126 | * byte is allocated and the copied data is null terminated. | |
127 | */ | |
128 | data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos); | |
129 | ||
130 | error = PTR_ERR(data); | |
131 | if (!IS_ERR(data)) { | |
132 | data[size] = 0; | |
133 | error = aa_remove_profiles(data, size); | |
134 | kvfree(data); | |
135 | } | |
136 | ||
137 | return error; | |
138 | } | |
139 | ||
140 | static const struct file_operations aa_fs_profile_remove = { | |
6038f373 AB |
141 | .write = profile_remove, |
142 | .llseek = default_llseek, | |
63e2b423 JJ |
143 | }; |
144 | ||
145 | /** Base file system setup **/ | |
146 | ||
9acd494b KC |
147 | static struct aa_fs_entry aa_fs_entry_apparmor[] = { |
148 | AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load), | |
149 | AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace), | |
150 | AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove), | |
151 | { } | |
152 | }; | |
63e2b423 | 153 | |
9acd494b KC |
154 | static struct aa_fs_entry aa_fs_entry = |
155 | AA_FS_DIR("apparmor", aa_fs_entry_apparmor); | |
63e2b423 | 156 | |
9acd494b KC |
157 | /** |
158 | * aafs_create_file - create a file entry in the apparmor securityfs | |
159 | * @fs_file: aa_fs_entry to build an entry for (NOT NULL) | |
160 | * @parent: the parent dentry in the securityfs | |
161 | * | |
162 | * Use aafs_remove_file to remove entries created with this fn. | |
163 | */ | |
164 | static int __init aafs_create_file(struct aa_fs_entry *fs_file, | |
165 | struct dentry *parent) | |
166 | { | |
167 | int error = 0; | |
168 | ||
169 | fs_file->dentry = securityfs_create_file(fs_file->name, | |
170 | S_IFREG | fs_file->mode, | |
171 | parent, fs_file, | |
172 | fs_file->file_ops); | |
173 | if (IS_ERR(fs_file->dentry)) { | |
174 | error = PTR_ERR(fs_file->dentry); | |
175 | fs_file->dentry = NULL; | |
63e2b423 | 176 | } |
9acd494b | 177 | return error; |
63e2b423 JJ |
178 | } |
179 | ||
180 | /** | |
9acd494b KC |
181 | * aafs_create_dir - recursively create a directory entry in the securityfs |
182 | * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) | |
183 | * @parent: the parent dentry in the securityfs | |
63e2b423 | 184 | * |
9acd494b | 185 | * Use aafs_remove_dir to remove entries created with this fn. |
63e2b423 | 186 | */ |
9acd494b KC |
187 | static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, |
188 | struct dentry *parent) | |
63e2b423 | 189 | { |
9acd494b KC |
190 | int error; |
191 | struct aa_fs_entry *fs_file; | |
63e2b423 | 192 | |
9acd494b KC |
193 | fs_dir->dentry = securityfs_create_dir(fs_dir->name, parent); |
194 | if (IS_ERR(fs_dir->dentry)) { | |
195 | error = PTR_ERR(fs_dir->dentry); | |
196 | fs_dir->dentry = NULL; | |
197 | goto failed; | |
198 | } | |
63e2b423 | 199 | |
9acd494b KC |
200 | for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { |
201 | if (fs_file->v_type == AA_FS_TYPE_DIR) | |
202 | error = aafs_create_dir(fs_file, fs_dir->dentry); | |
203 | else | |
204 | error = aafs_create_file(fs_file, fs_dir->dentry); | |
205 | if (error) | |
206 | goto failed; | |
207 | } | |
208 | ||
209 | return 0; | |
210 | ||
211 | failed: | |
212 | return error; | |
213 | } | |
214 | ||
215 | /** | |
216 | * aafs_remove_file - drop a single file entry in the apparmor securityfs | |
217 | * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) | |
218 | */ | |
219 | static void __init aafs_remove_file(struct aa_fs_entry *fs_file) | |
220 | { | |
221 | if (!fs_file->dentry) | |
222 | return; | |
223 | ||
224 | securityfs_remove(fs_file->dentry); | |
225 | fs_file->dentry = NULL; | |
226 | } | |
227 | ||
228 | /** | |
229 | * aafs_remove_dir - recursively drop a directory entry from the securityfs | |
230 | * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) | |
231 | */ | |
232 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) | |
233 | { | |
234 | struct aa_fs_entry *fs_file; | |
235 | ||
236 | for (fs_file = fs_dir->v.files; fs_file->name; ++fs_file) { | |
237 | if (fs_file->v_type == AA_FS_TYPE_DIR) | |
238 | aafs_remove_dir(fs_file); | |
239 | else | |
240 | aafs_remove_file(fs_file); | |
241 | } | |
242 | ||
243 | aafs_remove_file(fs_dir); | |
63e2b423 JJ |
244 | } |
245 | ||
246 | /** | |
247 | * aa_destroy_aafs - cleanup and free aafs | |
248 | * | |
249 | * releases dentries allocated by aa_create_aafs | |
250 | */ | |
251 | void __init aa_destroy_aafs(void) | |
252 | { | |
9acd494b | 253 | aafs_remove_dir(&aa_fs_entry); |
63e2b423 JJ |
254 | } |
255 | ||
256 | /** | |
257 | * aa_create_aafs - create the apparmor security filesystem | |
258 | * | |
259 | * dentries created here are released by aa_destroy_aafs | |
260 | * | |
261 | * Returns: error on failure | |
262 | */ | |
3417d8d5 | 263 | static int __init aa_create_aafs(void) |
63e2b423 JJ |
264 | { |
265 | int error; | |
266 | ||
267 | if (!apparmor_initialized) | |
268 | return 0; | |
269 | ||
9acd494b | 270 | if (aa_fs_entry.dentry) { |
63e2b423 JJ |
271 | AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); |
272 | return -EEXIST; | |
273 | } | |
274 | ||
9acd494b KC |
275 | /* Populate fs tree. */ |
276 | error = aafs_create_dir(&aa_fs_entry, NULL); | |
63e2b423 JJ |
277 | if (error) |
278 | goto error; | |
279 | ||
280 | /* TODO: add support for apparmorfs_null and apparmorfs_mnt */ | |
281 | ||
282 | /* Report that AppArmor fs is enabled */ | |
283 | aa_info_message("AppArmor Filesystem Enabled"); | |
284 | return 0; | |
285 | ||
286 | error: | |
287 | aa_destroy_aafs(); | |
288 | AA_ERROR("Error creating AppArmor securityfs\n"); | |
289 | return error; | |
290 | } | |
291 | ||
292 | fs_initcall(aa_create_aafs); |