]>
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 | ||
0d259f04 | 15 | #include <linux/ctype.h> |
63e2b423 JJ |
16 | #include <linux/security.h> |
17 | #include <linux/vmalloc.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/seq_file.h> | |
20 | #include <linux/uaccess.h> | |
80594fc2 | 21 | #include <linux/mount.h> |
63e2b423 | 22 | #include <linux/namei.h> |
e74abcf3 | 23 | #include <linux/capability.h> |
29b3822f | 24 | #include <linux/rcupdate.h> |
80594fc2 | 25 | #include <uapi/linux/major.h> |
ec25af4b | 26 | #include <linux/fs.h> |
63e2b423 JJ |
27 | |
28 | #include "include/apparmor.h" | |
29 | #include "include/apparmorfs.h" | |
30 | #include "include/audit.h" | |
31 | #include "include/context.h" | |
f8eb8a13 | 32 | #include "include/crypto.h" |
80594fc2 JJ |
33 | #include "include/ipc.h" |
34 | #include "include/policy_ns.h" | |
35 | #include "include/label.h" | |
63e2b423 | 36 | #include "include/policy.h" |
d384b0a1 | 37 | #include "include/resource.h" |
18015a11 | 38 | #include "include/label.h" |
80594fc2 | 39 | #include "include/lib.h" |
3b8ad847 | 40 | #include "include/policy_unpack.h" |
63e2b423 | 41 | |
0d259f04 JJ |
42 | /** |
43 | * aa_mangle_name - mangle a profile name to std profile layout form | |
44 | * @name: profile name to mangle (NOT NULL) | |
45 | * @target: buffer to store mangled name, same length as @name (MAYBE NULL) | |
46 | * | |
47 | * Returns: length of mangled name | |
48 | */ | |
80594fc2 | 49 | static int mangle_name(const char *name, char *target) |
0d259f04 JJ |
50 | { |
51 | char *t = target; | |
52 | ||
53 | while (*name == '/' || *name == '.') | |
54 | name++; | |
55 | ||
56 | if (target) { | |
57 | for (; *name; name++) { | |
58 | if (*name == '/') | |
59 | *(t)++ = '.'; | |
60 | else if (isspace(*name)) | |
61 | *(t)++ = '_'; | |
62 | else if (isalnum(*name) || strchr("._-", *name)) | |
63 | *(t)++ = *name; | |
64 | } | |
65 | ||
66 | *t = 0; | |
67 | } else { | |
68 | int len = 0; | |
69 | for (; *name; name++) { | |
70 | if (isalnum(*name) || isspace(*name) || | |
71 | strchr("/._-", *name)) | |
72 | len++; | |
73 | } | |
74 | ||
75 | return len; | |
76 | } | |
77 | ||
78 | return t - target; | |
79 | } | |
80 | ||
63e2b423 JJ |
81 | /** |
82 | * aa_simple_write_to_buffer - common routine for getting policy from user | |
63e2b423 | 83 | * @userbuf: user buffer to copy data from (NOT NULL) |
3ed02ada | 84 | * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size) |
63e2b423 JJ |
85 | * @copy_size: size of data to copy from user buffer |
86 | * @pos: position write is at in the file (NOT NULL) | |
87 | * | |
88 | * Returns: kernel buffer containing copy of user buffer data or an | |
89 | * ERR_PTR on failure. | |
90 | */ | |
3b8ad847 JJ |
91 | static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf, |
92 | size_t alloc_size, | |
93 | size_t copy_size, | |
94 | loff_t *pos) | |
63e2b423 | 95 | { |
3b8ad847 | 96 | struct aa_loaddata *data; |
63e2b423 | 97 | |
3ed02ada JJ |
98 | BUG_ON(copy_size > alloc_size); |
99 | ||
63e2b423 JJ |
100 | if (*pos != 0) |
101 | /* only writes from pos 0, that is complete writes */ | |
102 | return ERR_PTR(-ESPIPE); | |
103 | ||
63e2b423 | 104 | /* freed by caller to simple_write_to_buffer */ |
3b8ad847 | 105 | data = kvmalloc(sizeof(*data) + alloc_size); |
63e2b423 JJ |
106 | if (data == NULL) |
107 | return ERR_PTR(-ENOMEM); | |
3b8ad847 JJ |
108 | kref_init(&data->count); |
109 | data->size = copy_size; | |
110 | data->hash = NULL; | |
111 | data->abi = 0; | |
63e2b423 | 112 | |
3b8ad847 | 113 | if (copy_from_user(data->data, userbuf, copy_size)) { |
63e2b423 JJ |
114 | kvfree(data); |
115 | return ERR_PTR(-EFAULT); | |
116 | } | |
117 | ||
118 | return data; | |
119 | } | |
120 | ||
80594fc2 | 121 | static ssize_t policy_update(u32 mask, const char __user *buf, size_t size, |
0acae0c2 | 122 | loff_t *pos, struct aa_ns *ns) |
63e2b423 | 123 | { |
80594fc2 | 124 | struct aa_label *label; |
63e2b423 | 125 | ssize_t error; |
3b8ad847 | 126 | struct aa_loaddata *data; |
63e2b423 | 127 | |
80594fc2 | 128 | label = aa_begin_current_label(DO_UPDATE); |
63e2b423 | 129 | |
80594fc2 JJ |
130 | /* high level check about policy management - fine grained in |
131 | * below after unpack | |
132 | */ | |
0acae0c2 | 133 | error = aa_may_manage_policy(label, ns, mask); |
80594fc2 JJ |
134 | if (error) |
135 | return error; | |
136 | ||
137 | data = aa_simple_write_to_buffer(buf, size, size, pos); | |
63e2b423 JJ |
138 | error = PTR_ERR(data); |
139 | if (!IS_ERR(data)) { | |
0acae0c2 JJ |
140 | error = aa_replace_profiles(ns ? ns : labels_ns(label), label, |
141 | mask, data); | |
3b8ad847 | 142 | aa_put_loaddata(data); |
63e2b423 | 143 | } |
80594fc2 | 144 | aa_end_current_label(label); |
63e2b423 JJ |
145 | |
146 | return error; | |
147 | } | |
148 | ||
80594fc2 JJ |
149 | /* .load file hook fn to load policy */ |
150 | static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, | |
151 | loff_t *pos) | |
152 | { | |
0acae0c2 JJ |
153 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
154 | int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns); | |
155 | aa_put_ns(ns); | |
156 | ||
157 | return error; | |
80594fc2 JJ |
158 | } |
159 | ||
63e2b423 | 160 | static const struct file_operations aa_fs_profile_load = { |
6038f373 AB |
161 | .write = profile_load, |
162 | .llseek = default_llseek, | |
63e2b423 JJ |
163 | }; |
164 | ||
165 | /* .replace file hook fn to load and/or replace policy */ | |
166 | static ssize_t profile_replace(struct file *f, const char __user *buf, | |
167 | size_t size, loff_t *pos) | |
168 | { | |
0acae0c2 JJ |
169 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
170 | int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY, | |
171 | buf, size, pos, ns); | |
172 | aa_put_ns(ns); | |
173 | ||
174 | return error; | |
63e2b423 JJ |
175 | } |
176 | ||
177 | static const struct file_operations aa_fs_profile_replace = { | |
6038f373 AB |
178 | .write = profile_replace, |
179 | .llseek = default_llseek, | |
63e2b423 JJ |
180 | }; |
181 | ||
182 | /* .remove file hook fn to remove loaded policy */ | |
183 | static ssize_t profile_remove(struct file *f, const char __user *buf, | |
184 | size_t size, loff_t *pos) | |
185 | { | |
3b8ad847 | 186 | struct aa_loaddata *data; |
80594fc2 | 187 | struct aa_label *label; |
63e2b423 | 188 | ssize_t error; |
0acae0c2 | 189 | struct aa_ns *ns = aa_get_ns(f->f_inode->i_private); |
80594fc2 JJ |
190 | |
191 | label = aa_begin_current_label(DO_UPDATE); | |
192 | /* high level check about policy management - fine grained in | |
193 | * below after unpack | |
194 | */ | |
0acae0c2 | 195 | error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY); |
80594fc2 | 196 | if (error) |
0acae0c2 | 197 | goto out; |
63e2b423 JJ |
198 | |
199 | /* | |
200 | * aa_remove_profile needs a null terminated string so 1 extra | |
201 | * byte is allocated and the copied data is null terminated. | |
202 | */ | |
80594fc2 | 203 | data = aa_simple_write_to_buffer(buf, size + 1, size, pos); |
63e2b423 JJ |
204 | |
205 | error = PTR_ERR(data); | |
206 | if (!IS_ERR(data)) { | |
3b8ad847 | 207 | data->data[size] = 0; |
0acae0c2 JJ |
208 | error = aa_remove_profiles(ns ? ns : labels_ns(label), label, |
209 | data->data, size); | |
3b8ad847 | 210 | aa_put_loaddata(data); |
63e2b423 | 211 | } |
0acae0c2 | 212 | out: |
80594fc2 | 213 | aa_end_current_label(label); |
0acae0c2 | 214 | aa_put_ns(ns); |
63e2b423 JJ |
215 | return error; |
216 | } | |
217 | ||
218 | static const struct file_operations aa_fs_profile_remove = { | |
6038f373 AB |
219 | .write = profile_remove, |
220 | .llseek = default_llseek, | |
63e2b423 JJ |
221 | }; |
222 | ||
80594fc2 JJ |
223 | |
224 | static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms, | |
225 | const char *match_str, size_t match_len) | |
226 | { | |
227 | struct aa_perms tmp; | |
228 | struct aa_dfa *dfa; | |
229 | unsigned int state = 0; | |
230 | ||
231 | if (profile_unconfined(profile)) | |
232 | return; | |
233 | if (profile->file.dfa && *match_str == AA_CLASS_FILE) { | |
234 | dfa = profile->file.dfa; | |
235 | state = aa_dfa_match_len(dfa, profile->file.start, | |
236 | match_str + 1, match_len - 1); | |
237 | tmp = nullperms; | |
238 | if (state) { | |
239 | struct path_cond cond = { }; | |
240 | tmp = aa_compute_fperms(dfa, state, &cond); | |
241 | } | |
242 | } else if (profile->policy.dfa) { | |
243 | if (!PROFILE_MEDIATES_SAFE(profile, *match_str)) | |
244 | return; /* no change to current perms */ | |
245 | dfa = profile->policy.dfa; | |
246 | state = aa_dfa_match_len(dfa, profile->policy.start[0], | |
247 | match_str, match_len); | |
248 | if (state) | |
249 | aa_compute_perms(dfa, state, &tmp); | |
250 | else | |
251 | tmp = nullperms; | |
252 | } | |
253 | aa_apply_modes_to_perms(profile, &tmp); | |
254 | aa_perms_accum_raw(perms, &tmp); | |
255 | } | |
256 | ||
b7370901 WH |
257 | /** |
258 | * query_data - queries a policy and writes its data to buf | |
259 | * @buf: the resulting data is stored here (NOT NULL) | |
260 | * @buf_len: size of buf | |
261 | * @query: query string used to retrieve data | |
262 | * @query_len: size of query including second NUL byte | |
263 | * | |
264 | * The buffers pointed to by buf and query may overlap. The query buffer is | |
265 | * parsed before buf is written to. | |
266 | * | |
267 | * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of | |
268 | * the security confinement context and <KEY> is the name of the data to | |
269 | * retrieve. <LABEL> and <KEY> must not be NUL-terminated. | |
270 | * | |
271 | * Don't expect the contents of buf to be preserved on failure. | |
272 | * | |
273 | * Returns: number of characters written to buf or -errno on failure | |
274 | */ | |
275 | static ssize_t query_data(char *buf, size_t buf_len, | |
276 | char *query, size_t query_len) | |
277 | { | |
278 | char *out; | |
279 | const char *key; | |
280 | struct label_it i; | |
281 | struct aa_label *label, *curr; | |
282 | struct aa_profile *profile; | |
283 | struct aa_data *data; | |
284 | u32 bytes; | |
285 | u32 blocks; | |
286 | u32 size; | |
287 | ||
288 | if (!query_len) | |
289 | return -EINVAL; /* need a query */ | |
290 | ||
291 | key = query + strnlen(query, query_len) + 1; | |
292 | if (key + 1 >= query + query_len) | |
293 | return -EINVAL; /* not enough space for a non-empty key */ | |
294 | if (key + strnlen(key, query + query_len - key) >= query + query_len) | |
295 | return -EINVAL; /* must end with NUL */ | |
296 | ||
297 | if (buf_len < sizeof(bytes) + sizeof(blocks)) | |
298 | return -EINVAL; /* not enough space */ | |
299 | ||
300 | curr = aa_begin_current_label(DO_UPDATE); | |
301 | label = aa_label_parse(curr, query, GFP_KERNEL, false, false); | |
302 | aa_end_current_label(curr); | |
303 | if (IS_ERR(label)) | |
304 | return PTR_ERR(label); | |
305 | ||
306 | /* We are going to leave space for two numbers. The first is the total | |
307 | * number of bytes we are writing after the first number. This is so | |
308 | * users can read the full output without reallocation. | |
309 | * | |
310 | * The second number is the number of data blocks we're writing. An | |
311 | * application might be confined by multiple policies having data in | |
312 | * the same key. | |
313 | */ | |
314 | memset(buf, 0, sizeof(bytes) + sizeof(blocks)); | |
315 | out = buf + sizeof(bytes) + sizeof(blocks); | |
316 | ||
317 | blocks = 0; | |
318 | label_for_each_confined(i, label, profile) { | |
319 | if (!profile->data) | |
320 | continue; | |
321 | ||
322 | data = rhashtable_lookup_fast(profile->data, &key, | |
323 | profile->data->p); | |
324 | ||
325 | if (data) { | |
326 | if (out + sizeof(size) + data->size > buf + buf_len) { | |
327 | aa_put_label(label); | |
328 | return -EINVAL; /* not enough space */ | |
329 | } | |
330 | size = __cpu_to_le32(data->size); | |
331 | memcpy(out, &size, sizeof(size)); | |
332 | out += sizeof(size); | |
333 | memcpy(out, data->data, data->size); | |
334 | out += data->size; | |
335 | blocks++; | |
336 | } | |
337 | } | |
338 | aa_put_label(label); | |
339 | ||
340 | bytes = out - buf - sizeof(bytes); | |
341 | bytes = __cpu_to_le32(bytes); | |
342 | blocks = __cpu_to_le32(blocks); | |
343 | memcpy(buf, &bytes, sizeof(bytes)); | |
344 | memcpy(buf + sizeof(bytes), &blocks, sizeof(blocks)); | |
345 | ||
346 | return out - buf; | |
347 | } | |
348 | ||
80594fc2 JJ |
349 | /** |
350 | * query_label - queries a label and writes permissions to buf | |
351 | * @buf: the resulting permissions string is stored here (NOT NULL) | |
352 | * @buf_len: size of buf | |
353 | * @query: binary query string to match against the dfa | |
354 | * @query_len: size of query | |
355 | * | |
356 | * The buffers pointed to by buf and query may overlap. The query buffer is | |
357 | * parsed before buf is written to. | |
358 | * | |
359 | * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is | |
360 | * the name of the label, in the current namespace, that is to be queried and | |
361 | * DFA_STRING is a binary string to match against the label(s)'s DFA. | |
362 | * | |
363 | * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters | |
364 | * but must *not* be NUL terminated. | |
365 | * | |
366 | * Returns: number of characters written to buf or -errno on failure | |
367 | */ | |
368 | static ssize_t query_label(char *buf, size_t buf_len, | |
369 | char *query, size_t query_len, bool ns_only) | |
370 | { | |
371 | struct aa_profile *profile; | |
372 | struct aa_label *label, *curr; | |
373 | char *label_name, *match_str; | |
374 | size_t label_name_len, match_len; | |
375 | struct aa_perms perms; | |
376 | struct label_it i; | |
377 | ||
378 | if (!query_len) | |
379 | return -EINVAL; | |
380 | ||
381 | label_name = query; | |
382 | label_name_len = strnlen(query, query_len); | |
383 | if (!label_name_len || label_name_len == query_len) | |
384 | return -EINVAL; | |
385 | ||
386 | /** | |
387 | * The extra byte is to account for the null byte between the | |
388 | * profile name and dfa string. profile_name_len is greater | |
389 | * than zero and less than query_len, so a byte can be safely | |
390 | * added or subtracted. | |
391 | */ | |
392 | match_str = label_name + label_name_len + 1; | |
393 | match_len = query_len - label_name_len - 1; | |
394 | ||
395 | curr = aa_begin_current_label(DO_UPDATE); | |
396 | label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false); | |
397 | aa_end_current_label(curr); | |
398 | if (IS_ERR(label)) | |
399 | return PTR_ERR(label); | |
400 | ||
401 | perms = allperms; | |
402 | if (ns_only) { | |
403 | label_for_each_in_ns(i, labels_ns(label), label, profile) { | |
404 | profile_query_cb(profile, &perms, match_str, match_len); | |
405 | } | |
406 | } else { | |
407 | label_for_each(i, label, profile) { | |
408 | profile_query_cb(profile, &perms, match_str, match_len); | |
409 | } | |
410 | } | |
411 | aa_put_label(label); | |
412 | ||
413 | return scnprintf(buf, buf_len, | |
414 | "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n", | |
415 | perms.allow, perms.deny, perms.audit, perms.quiet); | |
416 | } | |
417 | ||
418 | #define QUERY_CMD_LABEL "label\0" | |
419 | #define QUERY_CMD_LABEL_LEN 6 | |
420 | #define QUERY_CMD_PROFILE "profile\0" | |
421 | #define QUERY_CMD_PROFILE_LEN 8 | |
422 | #define QUERY_CMD_LABELALL "labelall\0" | |
423 | #define QUERY_CMD_LABELALL_LEN 9 | |
b7370901 WH |
424 | #define QUERY_CMD_DATA "data\0" |
425 | #define QUERY_CMD_DATA_LEN 5 | |
80594fc2 JJ |
426 | |
427 | /** | |
b7370901 | 428 | * aa_write_access - generic permissions and data query |
80594fc2 JJ |
429 | * @file: pointer to open apparmorfs/access file |
430 | * @ubuf: user buffer containing the complete query string (NOT NULL) | |
431 | * @count: size of ubuf | |
432 | * @ppos: position in the file (MUST BE ZERO) | |
433 | * | |
b7370901 WH |
434 | * Allows for one permissions or data query per open(), write(), and read() |
435 | * sequence. The only queries currently supported are label-based queries for | |
436 | * permissions or data. | |
437 | * | |
438 | * For permissions queries, ubuf must begin with "label\0", followed by the | |
439 | * profile query specific format described in the query_label() function | |
440 | * documentation. | |
441 | * | |
442 | * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where | |
443 | * <LABEL> is the name of the security confinement context and <KEY> is the | |
444 | * name of the data to retrieve. | |
80594fc2 JJ |
445 | * |
446 | * Returns: number of bytes written or -errno on failure | |
447 | */ | |
448 | static ssize_t aa_write_access(struct file *file, const char __user *ubuf, | |
449 | size_t count, loff_t *ppos) | |
450 | { | |
451 | char *buf; | |
452 | ssize_t len; | |
453 | ||
454 | if (*ppos) | |
455 | return -ESPIPE; | |
456 | ||
457 | buf = simple_transaction_get(file, ubuf, count); | |
458 | if (IS_ERR(buf)) | |
459 | return PTR_ERR(buf); | |
460 | ||
461 | if (count > QUERY_CMD_PROFILE_LEN && | |
462 | !memcmp(buf, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) { | |
463 | len = query_label(buf, SIMPLE_TRANSACTION_LIMIT, | |
464 | buf + QUERY_CMD_PROFILE_LEN, | |
465 | count - QUERY_CMD_PROFILE_LEN, true); | |
466 | } else if (count > QUERY_CMD_LABEL_LEN && | |
467 | !memcmp(buf, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) { | |
468 | len = query_label(buf, SIMPLE_TRANSACTION_LIMIT, | |
469 | buf + QUERY_CMD_LABEL_LEN, | |
470 | count - QUERY_CMD_LABEL_LEN, true); | |
471 | } else if (count > QUERY_CMD_LABELALL_LEN && | |
472 | !memcmp(buf, QUERY_CMD_LABELALL, QUERY_CMD_LABELALL_LEN)) { | |
473 | len = query_label(buf, SIMPLE_TRANSACTION_LIMIT, | |
474 | buf + QUERY_CMD_LABELALL_LEN, | |
475 | count - QUERY_CMD_LABELALL_LEN, false); | |
b7370901 WH |
476 | } else if (count > QUERY_CMD_DATA_LEN && |
477 | !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) { | |
478 | len = query_data(buf, SIMPLE_TRANSACTION_LIMIT, | |
479 | buf + QUERY_CMD_DATA_LEN, | |
480 | count - QUERY_CMD_DATA_LEN); | |
80594fc2 JJ |
481 | } else |
482 | len = -EINVAL; | |
483 | ||
484 | if (len < 0) | |
485 | return len; | |
486 | ||
487 | simple_transaction_set(file, len); | |
488 | ||
489 | return count; | |
490 | } | |
491 | ||
492 | static const struct file_operations aa_fs_access = { | |
493 | .write = aa_write_access, | |
494 | .read = simple_transaction_read, | |
495 | .release = simple_transaction_release, | |
496 | .llseek = generic_file_llseek, | |
497 | }; | |
498 | ||
e74abcf3 KC |
499 | static int aa_fs_seq_show(struct seq_file *seq, void *v) |
500 | { | |
501 | struct aa_fs_entry *fs_file = seq->private; | |
502 | ||
503 | if (!fs_file) | |
504 | return 0; | |
505 | ||
506 | switch (fs_file->v_type) { | |
507 | case AA_FS_TYPE_BOOLEAN: | |
508 | seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no"); | |
509 | break; | |
a9bf8e9f KC |
510 | case AA_FS_TYPE_STRING: |
511 | seq_printf(seq, "%s\n", fs_file->v.string); | |
512 | break; | |
e74abcf3 KC |
513 | case AA_FS_TYPE_U64: |
514 | seq_printf(seq, "%#08lx\n", fs_file->v.u64); | |
515 | break; | |
516 | default: | |
517 | /* Ignore unpritable entry types. */ | |
518 | break; | |
519 | } | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
524 | static int aa_fs_seq_open(struct inode *inode, struct file *file) | |
525 | { | |
526 | return single_open(file, aa_fs_seq_show, inode->i_private); | |
527 | } | |
528 | ||
529 | const struct file_operations aa_fs_seq_file_ops = { | |
530 | .owner = THIS_MODULE, | |
531 | .open = aa_fs_seq_open, | |
532 | .read = seq_read, | |
533 | .llseek = seq_lseek, | |
534 | .release = single_release, | |
535 | }; | |
536 | ||
0d259f04 JJ |
537 | static int aa_fs_seq_profile_open(struct inode *inode, struct file *file, |
538 | int (*show)(struct seq_file *, void *)) | |
539 | { | |
80594fc2 JJ |
540 | struct aa_proxy *proxy = aa_get_proxy(inode->i_private); |
541 | int error = single_open(file, show, proxy); | |
0d259f04 JJ |
542 | |
543 | if (error) { | |
544 | file->private_data = NULL; | |
80594fc2 | 545 | aa_put_proxy(proxy); |
0d259f04 JJ |
546 | } |
547 | ||
548 | return error; | |
549 | } | |
550 | ||
551 | static int aa_fs_seq_profile_release(struct inode *inode, struct file *file) | |
552 | { | |
553 | struct seq_file *seq = (struct seq_file *) file->private_data; | |
554 | if (seq) | |
80594fc2 | 555 | aa_put_proxy(seq->private); |
0d259f04 JJ |
556 | return single_release(inode, file); |
557 | } | |
558 | ||
559 | static int aa_fs_seq_profname_show(struct seq_file *seq, void *v) | |
560 | { | |
80594fc2 JJ |
561 | struct aa_proxy *proxy = seq->private; |
562 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
563 | struct aa_profile *profile = labels_profile(label); | |
0d259f04 | 564 | seq_printf(seq, "%s\n", profile->base.name); |
80594fc2 | 565 | aa_put_label(label); |
0d259f04 JJ |
566 | |
567 | return 0; | |
568 | } | |
569 | ||
570 | static int aa_fs_seq_profname_open(struct inode *inode, struct file *file) | |
571 | { | |
572 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show); | |
573 | } | |
574 | ||
575 | static const struct file_operations aa_fs_profname_fops = { | |
576 | .owner = THIS_MODULE, | |
577 | .open = aa_fs_seq_profname_open, | |
578 | .read = seq_read, | |
579 | .llseek = seq_lseek, | |
580 | .release = aa_fs_seq_profile_release, | |
581 | }; | |
582 | ||
583 | static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v) | |
584 | { | |
80594fc2 JJ |
585 | struct aa_proxy *proxy = seq->private; |
586 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
587 | struct aa_profile *profile = labels_profile(label); | |
0d259f04 | 588 | seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]); |
80594fc2 | 589 | aa_put_label(label); |
0d259f04 JJ |
590 | |
591 | return 0; | |
592 | } | |
593 | ||
594 | static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file) | |
595 | { | |
596 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show); | |
597 | } | |
598 | ||
599 | static const struct file_operations aa_fs_profmode_fops = { | |
600 | .owner = THIS_MODULE, | |
601 | .open = aa_fs_seq_profmode_open, | |
602 | .read = seq_read, | |
603 | .llseek = seq_lseek, | |
604 | .release = aa_fs_seq_profile_release, | |
605 | }; | |
606 | ||
556d0be7 JJ |
607 | static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v) |
608 | { | |
80594fc2 JJ |
609 | struct aa_proxy *proxy = seq->private; |
610 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
611 | struct aa_profile *profile = labels_profile(label); | |
556d0be7 JJ |
612 | if (profile->attach) |
613 | seq_printf(seq, "%s\n", profile->attach); | |
614 | else if (profile->xmatch) | |
615 | seq_puts(seq, "<unknown>\n"); | |
616 | else | |
617 | seq_printf(seq, "%s\n", profile->base.name); | |
80594fc2 | 618 | aa_put_label(label); |
556d0be7 JJ |
619 | |
620 | return 0; | |
621 | } | |
622 | ||
623 | static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file) | |
624 | { | |
625 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show); | |
626 | } | |
627 | ||
628 | static const struct file_operations aa_fs_profattach_fops = { | |
629 | .owner = THIS_MODULE, | |
630 | .open = aa_fs_seq_profattach_open, | |
631 | .read = seq_read, | |
632 | .llseek = seq_lseek, | |
633 | .release = aa_fs_seq_profile_release, | |
634 | }; | |
635 | ||
f8eb8a13 JJ |
636 | static int aa_fs_seq_hash_show(struct seq_file *seq, void *v) |
637 | { | |
80594fc2 JJ |
638 | struct aa_proxy *proxy = seq->private; |
639 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
640 | struct aa_profile *profile = labels_profile(label); | |
f8eb8a13 JJ |
641 | unsigned int i, size = aa_hash_size(); |
642 | ||
643 | if (profile->hash) { | |
644 | for (i = 0; i < size; i++) | |
645 | seq_printf(seq, "%.2x", profile->hash[i]); | |
646 | seq_puts(seq, "\n"); | |
647 | } | |
80594fc2 | 648 | aa_put_label(label); |
f8eb8a13 JJ |
649 | |
650 | return 0; | |
651 | } | |
652 | ||
653 | static int aa_fs_seq_hash_open(struct inode *inode, struct file *file) | |
654 | { | |
655 | return single_open(file, aa_fs_seq_hash_show, inode->i_private); | |
656 | } | |
657 | ||
658 | static const struct file_operations aa_fs_seq_hash_fops = { | |
659 | .owner = THIS_MODULE, | |
660 | .open = aa_fs_seq_hash_open, | |
661 | .read = seq_read, | |
662 | .llseek = seq_lseek, | |
663 | .release = single_release, | |
664 | }; | |
665 | ||
18015a11 JJ |
666 | static int aa_fs_seq_show_stacked(struct seq_file *seq, void *v) |
667 | { | |
668 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
669 | seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no"); | |
670 | aa_end_current_label(label); | |
671 | ||
672 | return 0; | |
673 | } | |
674 | ||
675 | static int aa_fs_seq_open_stacked(struct inode *inode, struct file *file) | |
676 | { | |
677 | return single_open(file, aa_fs_seq_show_stacked, inode->i_private); | |
678 | } | |
679 | ||
680 | static const struct file_operations aa_fs_stacked = { | |
681 | .owner = THIS_MODULE, | |
682 | .open = aa_fs_seq_open_stacked, | |
683 | .read = seq_read, | |
684 | .llseek = seq_lseek, | |
685 | .release = single_release, | |
686 | }; | |
687 | ||
688 | static int aa_fs_seq_show_ns_stacked(struct seq_file *seq, void *v) | |
689 | { | |
690 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
691 | struct aa_profile *profile; | |
692 | struct label_it it; | |
693 | int count = 1; | |
694 | ||
695 | if (label->size > 1) { | |
696 | label_for_each(it, label, profile) | |
697 | if (profile->ns != labels_ns(label)) { | |
698 | count++; | |
699 | break; | |
700 | } | |
701 | } | |
702 | ||
703 | seq_printf(seq, "%s\n", count > 1 ? "yes" : "no"); | |
704 | aa_end_current_label(label); | |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
709 | static int aa_fs_seq_open_ns_stacked(struct inode *inode, struct file *file) | |
710 | { | |
711 | return single_open(file, aa_fs_seq_show_ns_stacked, inode->i_private); | |
712 | } | |
713 | ||
714 | static const struct file_operations aa_fs_ns_stacked = { | |
715 | .owner = THIS_MODULE, | |
716 | .open = aa_fs_seq_open_ns_stacked, | |
717 | .read = seq_read, | |
718 | .llseek = seq_lseek, | |
719 | .release = single_release, | |
720 | }; | |
721 | ||
722 | static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v) | |
723 | { | |
724 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
725 | seq_printf(seq, "%d\n", labels_ns(label)->level); | |
726 | aa_end_current_label(label); | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file) | |
732 | { | |
733 | return single_open(file, aa_fs_seq_show_ns_level, inode->i_private); | |
734 | } | |
735 | ||
736 | static const struct file_operations aa_fs_ns_level = { | |
737 | .owner = THIS_MODULE, | |
738 | .open = aa_fs_seq_open_ns_level, | |
739 | .read = seq_read, | |
740 | .llseek = seq_lseek, | |
741 | .release = single_release, | |
742 | }; | |
743 | ||
744 | static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v) | |
745 | { | |
746 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
747 | seq_printf(seq, "%s\n", labels_ns(label)->base.name); | |
748 | aa_end_current_label(label); | |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
753 | static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file) | |
754 | { | |
755 | return single_open(file, aa_fs_seq_show_ns_name, inode->i_private); | |
756 | } | |
757 | ||
758 | static const struct file_operations aa_fs_ns_name = { | |
759 | .owner = THIS_MODULE, | |
760 | .open = aa_fs_seq_open_ns_name, | |
761 | .read = seq_read, | |
762 | .llseek = seq_lseek, | |
763 | .release = single_release, | |
764 | }; | |
765 | ||
3b8ad847 JJ |
766 | static int rawdata_release(struct inode *inode, struct file *file) |
767 | { | |
768 | /* TODO: switch to loaddata when profile switched to symlink */ | |
769 | aa_put_proxy(file->private_data); | |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
774 | static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v) | |
775 | { | |
776 | struct aa_proxy *proxy = seq->private; | |
777 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
778 | struct aa_profile *profile = labels_profile(label); | |
779 | ||
780 | if (profile->rawdata->abi) { | |
781 | seq_printf(seq, "v%d", profile->rawdata->abi); | |
782 | seq_puts(seq, "\n"); | |
783 | } | |
784 | aa_put_label(label); | |
785 | ||
786 | return 0; | |
787 | } | |
788 | ||
789 | static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file) | |
790 | { | |
791 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show); | |
792 | } | |
793 | ||
794 | static const struct file_operations aa_fs_seq_raw_abi_fops = { | |
795 | .owner = THIS_MODULE, | |
796 | .open = aa_fs_seq_raw_abi_open, | |
797 | .read = seq_read, | |
798 | .llseek = seq_lseek, | |
799 | .release = aa_fs_seq_profile_release, | |
800 | }; | |
801 | ||
802 | static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v) | |
803 | { | |
804 | struct aa_proxy *proxy = seq->private; | |
805 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
806 | struct aa_profile *profile = labels_profile(label); | |
807 | unsigned int i, size = aa_hash_size(); | |
808 | ||
809 | if (profile->rawdata->hash) { | |
810 | for (i = 0; i < size; i++) | |
811 | seq_printf(seq, "%.2x", profile->rawdata->hash[i]); | |
812 | seq_puts(seq, "\n"); | |
813 | } | |
814 | aa_put_label(label); | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
819 | static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file) | |
820 | { | |
821 | return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show); | |
822 | } | |
823 | ||
824 | static const struct file_operations aa_fs_seq_raw_hash_fops = { | |
825 | .owner = THIS_MODULE, | |
826 | .open = aa_fs_seq_raw_hash_open, | |
827 | .read = seq_read, | |
828 | .llseek = seq_lseek, | |
829 | .release = aa_fs_seq_profile_release, | |
830 | }; | |
831 | ||
832 | static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size, | |
833 | loff_t *ppos) | |
834 | { | |
835 | struct aa_proxy *proxy = file->private_data; | |
836 | struct aa_label *label = aa_get_label_rcu(&proxy->label); | |
837 | struct aa_profile *profile = labels_profile(label); | |
838 | ||
839 | ssize_t ret = simple_read_from_buffer(buf, size, ppos, profile->rawdata->data, profile->rawdata->size); | |
840 | aa_put_label(label); | |
841 | ||
842 | return ret; | |
843 | } | |
844 | ||
845 | static int rawdata_open(struct inode *inode, struct file *file) | |
846 | { | |
11141223 | 847 | if (!policy_view_capable(NULL)) |
3b8ad847 JJ |
848 | return -EACCES; |
849 | ||
850 | file->private_data = aa_get_proxy(inode->i_private); | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | static const struct file_operations aa_fs_rawdata_fops = { | |
856 | .open = rawdata_open, | |
857 | .read = rawdata_read, | |
858 | .llseek = generic_file_llseek, | |
859 | .release = rawdata_release, | |
860 | }; | |
861 | ||
0d259f04 | 862 | /** fns to setup dynamic per profile/namespace files **/ |
80594fc2 JJ |
863 | |
864 | /** | |
865 | * | |
866 | * Requires: @profile->ns->lock held | |
867 | */ | |
0d259f04 JJ |
868 | void __aa_fs_profile_rmdir(struct aa_profile *profile) |
869 | { | |
870 | struct aa_profile *child; | |
871 | int i; | |
872 | ||
873 | if (!profile) | |
874 | return; | |
80594fc2 | 875 | AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); |
0d259f04 JJ |
876 | |
877 | list_for_each_entry(child, &profile->base.profiles, base.list) | |
878 | __aa_fs_profile_rmdir(child); | |
879 | ||
880 | for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) { | |
80594fc2 | 881 | struct aa_proxy *proxy; |
0d259f04 JJ |
882 | if (!profile->dents[i]) |
883 | continue; | |
884 | ||
80594fc2 | 885 | proxy = d_inode(profile->dents[i])->i_private; |
0d259f04 | 886 | securityfs_remove(profile->dents[i]); |
80594fc2 | 887 | aa_put_proxy(proxy); |
0d259f04 JJ |
888 | profile->dents[i] = NULL; |
889 | } | |
890 | } | |
891 | ||
80594fc2 JJ |
892 | /** |
893 | * | |
894 | * Requires: @old->ns->lock held | |
895 | */ | |
0d259f04 JJ |
896 | void __aa_fs_profile_migrate_dents(struct aa_profile *old, |
897 | struct aa_profile *new) | |
898 | { | |
899 | int i; | |
900 | ||
80594fc2 JJ |
901 | AA_BUG(!old); |
902 | AA_BUG(!new); | |
903 | AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock)); | |
904 | ||
0d259f04 JJ |
905 | for (i = 0; i < AAFS_PROF_SIZEOF; i++) { |
906 | new->dents[i] = old->dents[i]; | |
d671e890 | 907 | if (new->dents[i]) |
078cd827 | 908 | new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode); |
0d259f04 JJ |
909 | old->dents[i] = NULL; |
910 | } | |
911 | } | |
912 | ||
913 | static struct dentry *create_profile_file(struct dentry *dir, const char *name, | |
914 | struct aa_profile *profile, | |
915 | const struct file_operations *fops) | |
916 | { | |
80594fc2 | 917 | struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy); |
0d259f04 JJ |
918 | struct dentry *dent; |
919 | ||
80594fc2 | 920 | dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops); |
0d259f04 | 921 | if (IS_ERR(dent)) |
80594fc2 | 922 | aa_put_proxy(proxy); |
0d259f04 JJ |
923 | |
924 | return dent; | |
925 | } | |
926 | ||
80594fc2 JJ |
927 | /** |
928 | * | |
929 | * Requires: @profile->ns->lock held | |
930 | */ | |
0d259f04 JJ |
931 | int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent) |
932 | { | |
933 | struct aa_profile *child; | |
934 | struct dentry *dent = NULL, *dir; | |
935 | int error; | |
936 | ||
80594fc2 JJ |
937 | AA_BUG(!profile); |
938 | AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock)); | |
939 | ||
0d259f04 JJ |
940 | if (!parent) { |
941 | struct aa_profile *p; | |
942 | p = aa_deref_parent(profile); | |
943 | dent = prof_dir(p); | |
944 | /* adding to parent that previously didn't have children */ | |
945 | dent = securityfs_create_dir("profiles", dent); | |
946 | if (IS_ERR(dent)) | |
947 | goto fail; | |
948 | prof_child_dir(p) = parent = dent; | |
949 | } | |
950 | ||
951 | if (!profile->dirname) { | |
952 | int len, id_len; | |
953 | len = mangle_name(profile->base.name, NULL); | |
954 | id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id); | |
955 | ||
956 | profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL); | |
957 | if (!profile->dirname) | |
958 | goto fail; | |
959 | ||
960 | mangle_name(profile->base.name, profile->dirname); | |
961 | sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++); | |
962 | } | |
963 | ||
964 | dent = securityfs_create_dir(profile->dirname, parent); | |
965 | if (IS_ERR(dent)) | |
966 | goto fail; | |
967 | prof_dir(profile) = dir = dent; | |
968 | ||
969 | dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops); | |
970 | if (IS_ERR(dent)) | |
971 | goto fail; | |
972 | profile->dents[AAFS_PROF_NAME] = dent; | |
973 | ||
974 | dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops); | |
975 | if (IS_ERR(dent)) | |
976 | goto fail; | |
977 | profile->dents[AAFS_PROF_MODE] = dent; | |
978 | ||
556d0be7 JJ |
979 | dent = create_profile_file(dir, "attach", profile, |
980 | &aa_fs_profattach_fops); | |
981 | if (IS_ERR(dent)) | |
982 | goto fail; | |
983 | profile->dents[AAFS_PROF_ATTACH] = dent; | |
984 | ||
f8eb8a13 JJ |
985 | if (profile->hash) { |
986 | dent = create_profile_file(dir, "sha1", profile, | |
987 | &aa_fs_seq_hash_fops); | |
988 | if (IS_ERR(dent)) | |
989 | goto fail; | |
990 | profile->dents[AAFS_PROF_HASH] = dent; | |
991 | } | |
992 | ||
3b8ad847 JJ |
993 | if (profile->rawdata) { |
994 | dent = create_profile_file(dir, "raw_hash", profile, | |
995 | &aa_fs_seq_raw_hash_fops); | |
996 | if (IS_ERR(dent)) | |
997 | goto fail; | |
998 | profile->dents[AAFS_PROF_RAW_HASH] = dent; | |
999 | ||
1000 | dent = create_profile_file(dir, "raw_abi", profile, | |
1001 | &aa_fs_seq_raw_abi_fops); | |
1002 | if (IS_ERR(dent)) | |
1003 | goto fail; | |
1004 | profile->dents[AAFS_PROF_RAW_ABI] = dent; | |
1005 | ||
1006 | dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir, | |
1007 | profile->label.proxy, | |
1008 | &aa_fs_rawdata_fops); | |
1009 | if (IS_ERR(dent)) | |
1010 | goto fail; | |
1011 | profile->dents[AAFS_PROF_RAW_DATA] = dent; | |
1012 | d_inode(dent)->i_size = profile->rawdata->size; | |
1013 | aa_get_proxy(profile->label.proxy); | |
1014 | } | |
1015 | ||
0d259f04 JJ |
1016 | list_for_each_entry(child, &profile->base.profiles, base.list) { |
1017 | error = __aa_fs_profile_mkdir(child, prof_child_dir(profile)); | |
1018 | if (error) | |
1019 | goto fail2; | |
1020 | } | |
1021 | ||
1022 | return 0; | |
1023 | ||
1024 | fail: | |
1025 | error = PTR_ERR(dent); | |
1026 | ||
1027 | fail2: | |
1028 | __aa_fs_profile_rmdir(profile); | |
1029 | ||
1030 | return error; | |
1031 | } | |
1032 | ||
ec25af4b JJ |
1033 | static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode) |
1034 | { | |
1035 | struct aa_ns *ns, *parent; | |
1036 | /* TODO: improve permission check */ | |
1037 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
11141223 | 1038 | int error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); |
ec25af4b JJ |
1039 | aa_end_current_label(label); |
1040 | if (error) | |
1041 | return error; | |
1042 | ||
1043 | parent = aa_get_ns(dir->i_private); | |
1044 | AA_BUG(d_inode(ns_subns_dir(parent)) != dir); | |
1045 | ||
1046 | /* we have to unlock and then relock to get locking order right | |
1047 | * for pin_fs | |
1048 | */ | |
1049 | inode_unlock(dir); | |
1050 | securityfs_pin_fs(); | |
1051 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1052 | ||
1053 | error = __securityfs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL, | |
1054 | NULL, NULL); | |
1055 | if (error) | |
1056 | return error; | |
1057 | ||
1058 | ns = aa_create_ns(parent, ACCESS_ONCE(dentry->d_name.name), dentry); | |
1059 | if (IS_ERR(ns)) { | |
1060 | error = PTR_ERR(ns); | |
1061 | ns = NULL; | |
1062 | } | |
1063 | ||
1064 | aa_put_ns(ns); /* list ref remains */ | |
1065 | aa_put_ns(parent); | |
1066 | ||
1067 | return error; | |
1068 | } | |
1069 | ||
1070 | static int ns_rmdir_op(struct inode *dir, struct dentry *dentry) | |
1071 | { | |
1072 | struct aa_ns *ns, *parent; | |
1073 | /* TODO: improve permission check */ | |
1074 | struct aa_label *label = aa_begin_current_label(DO_UPDATE); | |
11141223 | 1075 | int error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY); |
ec25af4b JJ |
1076 | aa_end_current_label(label); |
1077 | if (error) | |
1078 | return error; | |
1079 | ||
1080 | parent = aa_get_ns(dir->i_private); | |
1081 | /* rmdir calls the generic securityfs functions to remove files | |
1082 | * from the apparmor dir. It is up to the apparmor ns locking | |
1083 | * to avoid races. | |
1084 | */ | |
1085 | inode_unlock(dir); | |
1086 | inode_unlock(dentry->d_inode); | |
1087 | ||
1088 | mutex_lock(&parent->lock); | |
1089 | ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name, | |
1090 | dentry->d_name.len)); | |
1091 | if (!ns) { | |
1092 | error = -ENOENT; | |
1093 | goto out; | |
1094 | } | |
1095 | AA_BUG(ns_dir(ns) != dentry); | |
1096 | ||
1097 | __aa_remove_ns(ns); | |
1098 | aa_put_ns(ns); | |
1099 | ||
1100 | out: | |
1101 | mutex_unlock(&parent->lock); | |
1102 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1103 | inode_lock(dentry->d_inode); | |
1104 | aa_put_ns(parent); | |
1105 | ||
1106 | return error; | |
1107 | } | |
1108 | ||
1109 | static const struct inode_operations ns_dir_inode_operations = { | |
1110 | .lookup = simple_lookup, | |
1111 | .mkdir = ns_mkdir_op, | |
1112 | .rmdir = ns_rmdir_op, | |
1113 | }; | |
1114 | ||
80594fc2 JJ |
1115 | /** |
1116 | * | |
1117 | * Requires: @ns->lock held | |
1118 | */ | |
1119 | void __aa_fs_ns_rmdir(struct aa_ns *ns) | |
0d259f04 | 1120 | { |
80594fc2 | 1121 | struct aa_ns *sub; |
0d259f04 JJ |
1122 | struct aa_profile *child; |
1123 | int i; | |
1124 | ||
1125 | if (!ns) | |
1126 | return; | |
80594fc2 | 1127 | AA_BUG(!mutex_is_locked(&ns->lock)); |
0d259f04 JJ |
1128 | |
1129 | list_for_each_entry(child, &ns->base.profiles, base.list) | |
1130 | __aa_fs_profile_rmdir(child); | |
1131 | ||
1132 | list_for_each_entry(sub, &ns->sub_ns, base.list) { | |
1133 | mutex_lock(&sub->lock); | |
80594fc2 | 1134 | __aa_fs_ns_rmdir(sub); |
0d259f04 JJ |
1135 | mutex_unlock(&sub->lock); |
1136 | } | |
1137 | ||
ec25af4b JJ |
1138 | if (ns_subns_dir(ns)) { |
1139 | sub = d_inode(ns_subns_dir(ns))->i_private; | |
1140 | aa_put_ns(sub); | |
1141 | } | |
0acae0c2 JJ |
1142 | if (ns_subload(ns)) { |
1143 | sub = d_inode(ns_subload(ns))->i_private; | |
1144 | aa_put_ns(sub); | |
1145 | } | |
1146 | if (ns_subreplace(ns)) { | |
1147 | sub = d_inode(ns_subreplace(ns))->i_private; | |
1148 | aa_put_ns(sub); | |
1149 | } | |
1150 | if (ns_subremove(ns)) { | |
1151 | sub = d_inode(ns_subremove(ns))->i_private; | |
1152 | aa_put_ns(sub); | |
1153 | } | |
1154 | ||
0d259f04 JJ |
1155 | for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) { |
1156 | securityfs_remove(ns->dents[i]); | |
1157 | ns->dents[i] = NULL; | |
1158 | } | |
1159 | } | |
1160 | ||
ec25af4b JJ |
1161 | /* assumes cleanup in caller */ |
1162 | static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir) | |
1163 | { | |
1164 | struct dentry *dent; | |
1165 | ||
1166 | AA_BUG(!ns); | |
1167 | AA_BUG(!dir); | |
1168 | ||
1169 | dent = securityfs_create_dir("profiles", dir); | |
1170 | if (IS_ERR(dent)) | |
1171 | return PTR_ERR(dent); | |
1172 | ns_subprofs_dir(ns) = dent; | |
1173 | ||
1174 | dent = securityfs_create_dir("raw_data", dir); | |
1175 | if (IS_ERR(dent)) | |
1176 | return PTR_ERR(dent); | |
1177 | ns_subdata_dir(ns) = dent; | |
1178 | ||
0acae0c2 JJ |
1179 | dent = securityfs_create_file(".load", 0666, dir, ns, |
1180 | &aa_fs_profile_load); | |
1181 | if (IS_ERR(dent)) | |
1182 | return PTR_ERR(dent); | |
1183 | aa_get_ns(ns); | |
1184 | ns_subload(ns) = dent; | |
1185 | ||
1186 | dent = securityfs_create_file(".replace", 0666, dir, ns, | |
1187 | &aa_fs_profile_replace); | |
1188 | if (IS_ERR(dent)) | |
1189 | return PTR_ERR(dent); | |
1190 | aa_get_ns(ns); | |
1191 | ns_subreplace(ns) = dent; | |
1192 | ||
1193 | dent = securityfs_create_file(".remove", 0666, dir, ns, | |
1194 | &aa_fs_profile_remove); | |
1195 | if (IS_ERR(dent)) | |
1196 | return PTR_ERR(dent); | |
1197 | aa_get_ns(ns); | |
1198 | ns_subremove(ns) = dent; | |
1199 | ||
1200 | /* use create_dentry so we can supply private data */ | |
ec25af4b JJ |
1201 | dent = securityfs_create_dentry("namespaces", |
1202 | S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, | |
1203 | dir, ns, NULL, | |
1204 | &ns_dir_inode_operations); | |
1205 | if (IS_ERR(dent)) | |
1206 | return PTR_ERR(dent); | |
1207 | aa_get_ns(ns); | |
1208 | ns_subns_dir(ns) = dent; | |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
80594fc2 JJ |
1213 | /** |
1214 | * | |
1215 | * Requires: @ns->lock held | |
1216 | */ | |
ec25af4b JJ |
1217 | int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name, |
1218 | struct dentry *dent) | |
0d259f04 | 1219 | { |
80594fc2 | 1220 | struct aa_ns *sub; |
0d259f04 | 1221 | struct aa_profile *child; |
ec25af4b | 1222 | struct dentry *dir; |
0d259f04 JJ |
1223 | int error; |
1224 | ||
80594fc2 JJ |
1225 | AA_BUG(!ns); |
1226 | AA_BUG(!parent); | |
1227 | AA_BUG(!mutex_is_locked(&ns->lock)); | |
1228 | ||
0d259f04 JJ |
1229 | if (!name) |
1230 | name = ns->base.name; | |
1231 | ||
ec25af4b JJ |
1232 | if (!dent) { |
1233 | /* create ns dir if it doesn't already exist */ | |
1234 | dent = securityfs_create_dir(name, parent); | |
1235 | if (IS_ERR(dent)) | |
1236 | goto fail; | |
1237 | } else | |
1238 | dget(dent); | |
0d259f04 | 1239 | ns_dir(ns) = dir = dent; |
ec25af4b JJ |
1240 | error = __aa_fs_ns_mkdir_entries(ns, dir); |
1241 | if (error) | |
1242 | goto fail2; | |
0d259f04 | 1243 | |
ec25af4b | 1244 | /* profiles */ |
0d259f04 JJ |
1245 | list_for_each_entry(child, &ns->base.profiles, base.list) { |
1246 | error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns)); | |
1247 | if (error) | |
1248 | goto fail2; | |
1249 | } | |
1250 | ||
ec25af4b | 1251 | /* subnamespaces */ |
0d259f04 JJ |
1252 | list_for_each_entry(sub, &ns->sub_ns, base.list) { |
1253 | mutex_lock(&sub->lock); | |
ec25af4b | 1254 | error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL); |
0d259f04 JJ |
1255 | mutex_unlock(&sub->lock); |
1256 | if (error) | |
1257 | goto fail2; | |
1258 | } | |
1259 | ||
1260 | return 0; | |
1261 | ||
1262 | fail: | |
1263 | error = PTR_ERR(dent); | |
1264 | ||
1265 | fail2: | |
80594fc2 | 1266 | __aa_fs_ns_rmdir(ns); |
0d259f04 JJ |
1267 | |
1268 | return error; | |
1269 | } | |
1270 | ||
1271 | ||
29b3822f JJ |
1272 | #define list_entry_is_head(pos, head, member) (&pos->member == (head)) |
1273 | ||
1274 | /** | |
80594fc2 | 1275 | * __next_ns - find the next namespace to list |
29b3822f JJ |
1276 | * @root: root namespace to stop search at (NOT NULL) |
1277 | * @ns: current ns position (NOT NULL) | |
1278 | * | |
1279 | * Find the next namespace from @ns under @root and handle all locking needed | |
1280 | * while switching current namespace. | |
1281 | * | |
1282 | * Returns: next namespace or NULL if at last namespace under @root | |
1283 | * Requires: ns->parent->lock to be held | |
1284 | * NOTE: will not unlock root->lock | |
1285 | */ | |
80594fc2 | 1286 | static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns) |
29b3822f | 1287 | { |
80594fc2 JJ |
1288 | struct aa_ns *parent, *next; |
1289 | ||
1290 | AA_BUG(!root); | |
1291 | AA_BUG(!ns); | |
1292 | AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock)); | |
29b3822f JJ |
1293 | |
1294 | /* is next namespace a child */ | |
1295 | if (!list_empty(&ns->sub_ns)) { | |
1296 | next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list); | |
1297 | mutex_lock(&next->lock); | |
1298 | return next; | |
1299 | } | |
1300 | ||
1301 | /* check if the next ns is a sibling, parent, gp, .. */ | |
1302 | parent = ns->parent; | |
ed2c7da3 | 1303 | while (ns != root) { |
29b3822f | 1304 | mutex_unlock(&ns->lock); |
38dbd7d8 | 1305 | next = list_next_entry(ns, base.list); |
29b3822f JJ |
1306 | if (!list_entry_is_head(next, &parent->sub_ns, base.list)) { |
1307 | mutex_lock(&next->lock); | |
1308 | return next; | |
1309 | } | |
29b3822f JJ |
1310 | ns = parent; |
1311 | parent = parent->parent; | |
1312 | } | |
1313 | ||
1314 | return NULL; | |
1315 | } | |
1316 | ||
1317 | /** | |
1318 | * __first_profile - find the first profile in a namespace | |
1319 | * @root: namespace that is root of profiles being displayed (NOT NULL) | |
80594fc2 | 1320 | * @ns: namespace to start in (MAY BE NULL) |
29b3822f JJ |
1321 | * |
1322 | * Returns: unrefcounted profile or NULL if no profile | |
80594fc2 | 1323 | * Requires: ns.lock to be held |
29b3822f | 1324 | */ |
80594fc2 | 1325 | static struct aa_profile *__first_profile(struct aa_ns *root, struct aa_ns *ns) |
29b3822f | 1326 | { |
80594fc2 JJ |
1327 | AA_BUG(!root); |
1328 | AA_BUG(ns && !mutex_is_locked(&ns->lock)); | |
1329 | ||
1330 | for (; ns; ns = __next_ns(root, ns)) { | |
29b3822f JJ |
1331 | if (!list_empty(&ns->base.profiles)) |
1332 | return list_first_entry(&ns->base.profiles, | |
1333 | struct aa_profile, base.list); | |
1334 | } | |
1335 | return NULL; | |
1336 | } | |
1337 | ||
1338 | /** | |
1339 | * __next_profile - step to the next profile in a profile tree | |
1340 | * @profile: current profile in tree (NOT NULL) | |
1341 | * | |
1342 | * Perform a depth first traversal on the profile tree in a namespace | |
1343 | * | |
1344 | * Returns: next profile or NULL if done | |
1345 | * Requires: profile->ns.lock to be held | |
1346 | */ | |
1347 | static struct aa_profile *__next_profile(struct aa_profile *p) | |
1348 | { | |
1349 | struct aa_profile *parent; | |
80594fc2 JJ |
1350 | struct aa_ns *ns = p->ns; |
1351 | ||
1352 | AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock)); | |
29b3822f JJ |
1353 | |
1354 | /* is next profile a child */ | |
1355 | if (!list_empty(&p->base.profiles)) | |
1356 | return list_first_entry(&p->base.profiles, typeof(*p), | |
1357 | base.list); | |
1358 | ||
1359 | /* is next profile a sibling, parent sibling, gp, sibling, .. */ | |
1360 | parent = rcu_dereference_protected(p->parent, | |
1361 | mutex_is_locked(&p->ns->lock)); | |
1362 | while (parent) { | |
38dbd7d8 | 1363 | p = list_next_entry(p, base.list); |
29b3822f JJ |
1364 | if (!list_entry_is_head(p, &parent->base.profiles, base.list)) |
1365 | return p; | |
1366 | p = parent; | |
1367 | parent = rcu_dereference_protected(parent->parent, | |
1368 | mutex_is_locked(&parent->ns->lock)); | |
1369 | } | |
1370 | ||
1371 | /* is next another profile in the namespace */ | |
38dbd7d8 | 1372 | p = list_next_entry(p, base.list); |
29b3822f JJ |
1373 | if (!list_entry_is_head(p, &ns->base.profiles, base.list)) |
1374 | return p; | |
1375 | ||
1376 | return NULL; | |
1377 | } | |
1378 | ||
1379 | /** | |
1380 | * next_profile - step to the next profile in where ever it may be | |
1381 | * @root: root namespace (NOT NULL) | |
1382 | * @profile: current profile (NOT NULL) | |
1383 | * | |
1384 | * Returns: next profile or NULL if there isn't one | |
1385 | */ | |
80594fc2 | 1386 | static struct aa_profile *next_profile(struct aa_ns *root, |
29b3822f JJ |
1387 | struct aa_profile *profile) |
1388 | { | |
1389 | struct aa_profile *next = __next_profile(profile); | |
1390 | if (next) | |
1391 | return next; | |
1392 | ||
1393 | /* finished all profiles in namespace move to next namespace */ | |
80594fc2 | 1394 | return __first_profile(root, __next_ns(root, profile->ns)); |
29b3822f JJ |
1395 | } |
1396 | ||
1397 | /** | |
1398 | * p_start - start a depth first traversal of profile tree | |
1399 | * @f: seq_file to fill | |
1400 | * @pos: current position | |
1401 | * | |
1402 | * Returns: first profile under current namespace or NULL if none found | |
1403 | * | |
1404 | * acquires first ns->lock | |
1405 | */ | |
1406 | static void *p_start(struct seq_file *f, loff_t *pos) | |
1407 | { | |
1408 | struct aa_profile *profile = NULL; | |
80594fc2 | 1409 | struct aa_ns *root = aa_get_current_ns(); |
29b3822f | 1410 | loff_t l = *pos; |
80594fc2 | 1411 | f->private = root; |
29b3822f JJ |
1412 | |
1413 | /* find the first profile */ | |
1414 | mutex_lock(&root->lock); | |
1415 | profile = __first_profile(root, root); | |
1416 | ||
1417 | /* skip to position */ | |
1418 | for (; profile && l > 0; l--) | |
1419 | profile = next_profile(root, profile); | |
1420 | ||
1421 | return profile; | |
1422 | } | |
1423 | ||
1424 | /** | |
1425 | * p_next - read the next profile entry | |
1426 | * @f: seq_file to fill | |
1427 | * @p: profile previously returned | |
1428 | * @pos: current position | |
1429 | * | |
1430 | * Returns: next profile after @p or NULL if none | |
1431 | * | |
1432 | * may acquire/release locks in namespace tree as necessary | |
1433 | */ | |
1434 | static void *p_next(struct seq_file *f, void *p, loff_t *pos) | |
1435 | { | |
1436 | struct aa_profile *profile = p; | |
80594fc2 | 1437 | struct aa_ns *ns = f->private; |
29b3822f JJ |
1438 | (*pos)++; |
1439 | ||
1440 | return next_profile(ns, profile); | |
1441 | } | |
1442 | ||
1443 | /** | |
1444 | * p_stop - stop depth first traversal | |
1445 | * @f: seq_file we are filling | |
1446 | * @p: the last profile writen | |
1447 | * | |
1448 | * Release all locking done by p_start/p_next on namespace tree | |
1449 | */ | |
1450 | static void p_stop(struct seq_file *f, void *p) | |
1451 | { | |
1452 | struct aa_profile *profile = p; | |
80594fc2 | 1453 | struct aa_ns *root = f->private, *ns; |
29b3822f JJ |
1454 | |
1455 | if (profile) { | |
1456 | for (ns = profile->ns; ns && ns != root; ns = ns->parent) | |
1457 | mutex_unlock(&ns->lock); | |
1458 | } | |
1459 | mutex_unlock(&root->lock); | |
80594fc2 | 1460 | aa_put_ns(root); |
29b3822f JJ |
1461 | } |
1462 | ||
1463 | /** | |
1464 | * seq_show_profile - show a profile entry | |
1465 | * @f: seq_file to file | |
1466 | * @p: current position (profile) (NOT NULL) | |
1467 | * | |
1468 | * Returns: error on failure | |
1469 | */ | |
1470 | static int seq_show_profile(struct seq_file *f, void *p) | |
1471 | { | |
1472 | struct aa_profile *profile = (struct aa_profile *)p; | |
80594fc2 | 1473 | struct aa_ns *root = f->private; |
29b3822f | 1474 | |
80594fc2 JJ |
1475 | aa_label_seq_xprint(f, root, &profile->label, |
1476 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL); | |
1477 | seq_printf(f, "\n"); | |
29b3822f JJ |
1478 | |
1479 | return 0; | |
1480 | } | |
1481 | ||
1482 | static const struct seq_operations aa_fs_profiles_op = { | |
1483 | .start = p_start, | |
1484 | .next = p_next, | |
1485 | .stop = p_stop, | |
1486 | .show = seq_show_profile, | |
1487 | }; | |
1488 | ||
1489 | static int profiles_open(struct inode *inode, struct file *file) | |
1490 | { | |
11141223 | 1491 | if (!policy_view_capable(NULL)) |
80594fc2 JJ |
1492 | return -EACCES; |
1493 | ||
29b3822f JJ |
1494 | return seq_open(file, &aa_fs_profiles_op); |
1495 | } | |
1496 | ||
1497 | static int profiles_release(struct inode *inode, struct file *file) | |
1498 | { | |
1499 | return seq_release(inode, file); | |
1500 | } | |
1501 | ||
1502 | static const struct file_operations aa_fs_profiles_fops = { | |
1503 | .open = profiles_open, | |
1504 | .read = seq_read, | |
1505 | .llseek = seq_lseek, | |
1506 | .release = profiles_release, | |
1507 | }; | |
1508 | ||
1509 | ||
0d259f04 | 1510 | /** Base file system setup **/ |
a9bf8e9f KC |
1511 | static struct aa_fs_entry aa_fs_entry_file[] = { |
1512 | AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \ | |
1513 | "link lock"), | |
1514 | { } | |
1515 | }; | |
1516 | ||
80594fc2 JJ |
1517 | static struct aa_fs_entry aa_fs_entry_ptrace[] = { |
1518 | AA_FS_FILE_STRING("mask", "read trace"), | |
1519 | { } | |
1520 | }; | |
1521 | ||
1522 | static struct aa_fs_entry aa_fs_entry_signal[] = { | |
1523 | AA_FS_FILE_STRING("mask", AA_FS_SIG_MASK), | |
1524 | { } | |
1525 | }; | |
1526 | ||
e74abcf3 KC |
1527 | static struct aa_fs_entry aa_fs_entry_domain[] = { |
1528 | AA_FS_FILE_BOOLEAN("change_hat", 1), | |
1529 | AA_FS_FILE_BOOLEAN("change_hatv", 1), | |
1530 | AA_FS_FILE_BOOLEAN("change_onexec", 1), | |
1531 | AA_FS_FILE_BOOLEAN("change_profile", 1), | |
80594fc2 | 1532 | AA_FS_FILE_BOOLEAN("stack", 1), |
f5f10a2c | 1533 | AA_FS_FILE_STRING("version", "1.1"), |
80594fc2 JJ |
1534 | { } |
1535 | }; | |
1536 | ||
1537 | static struct aa_fs_entry aa_fs_entry_versions[] = { | |
1538 | AA_FS_FILE_BOOLEAN("v5", 1), | |
1539 | AA_FS_FILE_BOOLEAN("v6", 1), | |
1540 | AA_FS_FILE_BOOLEAN("v7", 1), | |
e74abcf3 KC |
1541 | { } |
1542 | }; | |
1543 | ||
9d910a3b | 1544 | static struct aa_fs_entry aa_fs_entry_policy[] = { |
80594fc2 JJ |
1545 | AA_FS_DIR("versions", aa_fs_entry_versions), |
1546 | AA_FS_FILE_BOOLEAN("set_load", 1), | |
1547 | { } | |
1548 | }; | |
1549 | ||
1550 | static struct aa_fs_entry aa_fs_entry_mount[] = { | |
1551 | AA_FS_FILE_STRING("mask", "mount umount"), | |
1552 | { } | |
1553 | }; | |
1554 | ||
1555 | static struct aa_fs_entry aa_fs_entry_ns[] = { | |
1556 | AA_FS_FILE_BOOLEAN("profile", 1), | |
1557 | AA_FS_FILE_BOOLEAN("pivot_root", 1), | |
1558 | { } | |
1559 | }; | |
1560 | ||
1561 | static struct aa_fs_entry aa_fs_entry_dbus[] = { | |
1562 | AA_FS_FILE_STRING("mask", "acquire send receive"), | |
1563 | { } | |
9d910a3b JJ |
1564 | }; |
1565 | ||
e74abcf3 | 1566 | static struct aa_fs_entry aa_fs_entry_features[] = { |
9d910a3b | 1567 | AA_FS_DIR("policy", aa_fs_entry_policy), |
e74abcf3 | 1568 | AA_FS_DIR("domain", aa_fs_entry_domain), |
a9bf8e9f | 1569 | AA_FS_DIR("file", aa_fs_entry_file), |
80594fc2 JJ |
1570 | AA_FS_DIR("network", aa_fs_entry_network), |
1571 | AA_FS_DIR("mount", aa_fs_entry_mount), | |
1572 | AA_FS_DIR("namespaces", aa_fs_entry_ns), | |
e74abcf3 | 1573 | AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK), |
d384b0a1 | 1574 | AA_FS_DIR("rlimit", aa_fs_entry_rlimit), |
84f1f787 | 1575 | AA_FS_DIR("caps", aa_fs_entry_caps), |
80594fc2 JJ |
1576 | AA_FS_DIR("ptrace", aa_fs_entry_ptrace), |
1577 | AA_FS_DIR("signal", aa_fs_entry_signal), | |
1578 | AA_FS_DIR("dbus", aa_fs_entry_dbus), | |
e74abcf3 KC |
1579 | { } |
1580 | }; | |
1581 | ||
9acd494b | 1582 | static struct aa_fs_entry aa_fs_entry_apparmor[] = { |
80594fc2 | 1583 | AA_FS_FILE_FOPS(".access", 0666, &aa_fs_access), |
18015a11 JJ |
1584 | AA_FS_FILE_FOPS(".stacked", 0666, &aa_fs_stacked), |
1585 | AA_FS_FILE_FOPS(".ns_stacked", 0666, &aa_fs_ns_stacked), | |
1586 | AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level), | |
1587 | AA_FS_FILE_FOPS(".ns_name", 0666, &aa_fs_ns_name), | |
80594fc2 | 1588 | AA_FS_FILE_FOPS("profiles", 0444, &aa_fs_profiles_fops), |
e74abcf3 | 1589 | AA_FS_DIR("features", aa_fs_entry_features), |
9acd494b KC |
1590 | { } |
1591 | }; | |
63e2b423 | 1592 | |
9acd494b KC |
1593 | static struct aa_fs_entry aa_fs_entry = |
1594 | AA_FS_DIR("apparmor", aa_fs_entry_apparmor); | |
63e2b423 | 1595 | |
9acd494b KC |
1596 | /** |
1597 | * aafs_create_file - create a file entry in the apparmor securityfs | |
1598 | * @fs_file: aa_fs_entry to build an entry for (NOT NULL) | |
1599 | * @parent: the parent dentry in the securityfs | |
1600 | * | |
1601 | * Use aafs_remove_file to remove entries created with this fn. | |
1602 | */ | |
1603 | static int __init aafs_create_file(struct aa_fs_entry *fs_file, | |
1604 | struct dentry *parent) | |
1605 | { | |
1606 | int error = 0; | |
1607 | ||
1608 | fs_file->dentry = securityfs_create_file(fs_file->name, | |
1609 | S_IFREG | fs_file->mode, | |
1610 | parent, fs_file, | |
1611 | fs_file->file_ops); | |
1612 | if (IS_ERR(fs_file->dentry)) { | |
1613 | error = PTR_ERR(fs_file->dentry); | |
1614 | fs_file->dentry = NULL; | |
63e2b423 | 1615 | } |
9acd494b | 1616 | return error; |
63e2b423 JJ |
1617 | } |
1618 | ||
0d259f04 | 1619 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir); |
63e2b423 | 1620 | /** |
9acd494b KC |
1621 | * aafs_create_dir - recursively create a directory entry in the securityfs |
1622 | * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL) | |
1623 | * @parent: the parent dentry in the securityfs | |
63e2b423 | 1624 | * |
9acd494b | 1625 | * Use aafs_remove_dir to remove entries created with this fn. |
63e2b423 | 1626 | */ |
9acd494b KC |
1627 | static int __init aafs_create_dir(struct aa_fs_entry *fs_dir, |
1628 | struct dentry *parent) | |
63e2b423 | 1629 | { |
9acd494b | 1630 | struct aa_fs_entry *fs_file; |
0d259f04 JJ |
1631 | struct dentry *dir; |
1632 | int error; | |
63e2b423 | 1633 | |
0d259f04 JJ |
1634 | dir = securityfs_create_dir(fs_dir->name, parent); |
1635 | if (IS_ERR(dir)) | |
1636 | return PTR_ERR(dir); | |
1637 | fs_dir->dentry = dir; | |
63e2b423 | 1638 | |
0d259f04 | 1639 | for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { |
9acd494b KC |
1640 | if (fs_file->v_type == AA_FS_TYPE_DIR) |
1641 | error = aafs_create_dir(fs_file, fs_dir->dentry); | |
1642 | else | |
1643 | error = aafs_create_file(fs_file, fs_dir->dentry); | |
1644 | if (error) | |
1645 | goto failed; | |
1646 | } | |
1647 | ||
1648 | return 0; | |
1649 | ||
1650 | failed: | |
0d259f04 JJ |
1651 | aafs_remove_dir(fs_dir); |
1652 | ||
9acd494b KC |
1653 | return error; |
1654 | } | |
1655 | ||
1656 | /** | |
1657 | * aafs_remove_file - drop a single file entry in the apparmor securityfs | |
1658 | * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL) | |
1659 | */ | |
1660 | static void __init aafs_remove_file(struct aa_fs_entry *fs_file) | |
1661 | { | |
1662 | if (!fs_file->dentry) | |
1663 | return; | |
1664 | ||
1665 | securityfs_remove(fs_file->dentry); | |
1666 | fs_file->dentry = NULL; | |
1667 | } | |
1668 | ||
1669 | /** | |
1670 | * aafs_remove_dir - recursively drop a directory entry from the securityfs | |
1671 | * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL) | |
1672 | */ | |
1673 | static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir) | |
1674 | { | |
1675 | struct aa_fs_entry *fs_file; | |
1676 | ||
0d259f04 | 1677 | for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) { |
9acd494b KC |
1678 | if (fs_file->v_type == AA_FS_TYPE_DIR) |
1679 | aafs_remove_dir(fs_file); | |
1680 | else | |
1681 | aafs_remove_file(fs_file); | |
1682 | } | |
1683 | ||
1684 | aafs_remove_file(fs_dir); | |
63e2b423 JJ |
1685 | } |
1686 | ||
1687 | /** | |
1688 | * aa_destroy_aafs - cleanup and free aafs | |
1689 | * | |
1690 | * releases dentries allocated by aa_create_aafs | |
1691 | */ | |
1692 | void __init aa_destroy_aafs(void) | |
1693 | { | |
9acd494b | 1694 | aafs_remove_dir(&aa_fs_entry); |
63e2b423 JJ |
1695 | } |
1696 | ||
80594fc2 JJ |
1697 | |
1698 | #define NULL_FILE_NAME ".null" | |
1699 | struct path aa_null; | |
1700 | ||
1701 | static int aa_mk_null_file(struct dentry *parent) | |
1702 | { | |
1703 | struct vfsmount *mount = NULL; | |
1704 | struct dentry *dentry; | |
1705 | struct inode *inode; | |
1706 | int count = 0; | |
1707 | int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count); | |
1708 | if (error) | |
1709 | return error; | |
1710 | ||
1711 | inode_lock(d_inode(parent)); | |
1712 | dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME)); | |
1713 | if (IS_ERR(dentry)) { | |
1714 | error = PTR_ERR(dentry); | |
1715 | goto out; | |
1716 | } | |
1717 | inode = new_inode(parent->d_inode->i_sb); | |
1718 | if (!inode) { | |
1719 | error = -ENOMEM; | |
1720 | goto out1; | |
1721 | } | |
1722 | ||
1723 | inode->i_ino = get_next_ino(); | |
1724 | inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO; | |
1725 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
1726 | init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, | |
1727 | MKDEV(MEM_MAJOR, 3)); | |
1728 | d_instantiate(dentry, inode); | |
1729 | aa_null.dentry = dget(dentry); | |
1730 | aa_null.mnt = mntget(mount); | |
1731 | ||
1732 | error = 0; | |
1733 | ||
1734 | out1: | |
1735 | dput(dentry); | |
1736 | out: | |
1737 | inode_unlock(d_inode(parent)); | |
1738 | simple_release_fs(&mount, &count); | |
1739 | return error; | |
1740 | } | |
1741 | ||
63e2b423 JJ |
1742 | /** |
1743 | * aa_create_aafs - create the apparmor security filesystem | |
1744 | * | |
1745 | * dentries created here are released by aa_destroy_aafs | |
1746 | * | |
1747 | * Returns: error on failure | |
1748 | */ | |
3417d8d5 | 1749 | static int __init aa_create_aafs(void) |
63e2b423 | 1750 | { |
0acae0c2 | 1751 | struct dentry *dent; |
63e2b423 JJ |
1752 | int error; |
1753 | ||
1754 | if (!apparmor_initialized) | |
1755 | return 0; | |
1756 | ||
9acd494b | 1757 | if (aa_fs_entry.dentry) { |
63e2b423 JJ |
1758 | AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); |
1759 | return -EEXIST; | |
1760 | } | |
1761 | ||
9acd494b KC |
1762 | /* Populate fs tree. */ |
1763 | error = aafs_create_dir(&aa_fs_entry, NULL); | |
63e2b423 JJ |
1764 | if (error) |
1765 | goto error; | |
1766 | ||
0acae0c2 JJ |
1767 | dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry, |
1768 | NULL, &aa_fs_profile_load); | |
1769 | if (IS_ERR(dent)) { | |
1770 | error = PTR_ERR(dent); | |
1771 | goto error; | |
1772 | } | |
1773 | ns_subload(root_ns) = dent; | |
1774 | ||
1775 | dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry, | |
1776 | NULL, &aa_fs_profile_replace); | |
1777 | if (IS_ERR(dent)) { | |
1778 | error = PTR_ERR(dent); | |
1779 | goto error; | |
1780 | } | |
1781 | ns_subreplace(root_ns) = dent; | |
1782 | ||
1783 | dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry, | |
1784 | NULL, &aa_fs_profile_remove); | |
1785 | if (IS_ERR(dent)) { | |
1786 | error = PTR_ERR(dent); | |
1787 | goto error; | |
1788 | } | |
1789 | ns_subremove(root_ns) = dent; | |
1790 | ||
80594fc2 | 1791 | mutex_lock(&root_ns->lock); |
ec25af4b | 1792 | error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy", NULL); |
80594fc2 JJ |
1793 | mutex_unlock(&root_ns->lock); |
1794 | ||
1795 | if (error) | |
1796 | goto error; | |
1797 | ||
1798 | error = aa_mk_null_file(aa_fs_entry.dentry); | |
0d259f04 JJ |
1799 | if (error) |
1800 | goto error; | |
1801 | ||
80594fc2 JJ |
1802 | if (!aa_g_unconfined_init) { |
1803 | /* TODO: add default profile to apparmorfs */ | |
1804 | } | |
63e2b423 JJ |
1805 | |
1806 | /* Report that AppArmor fs is enabled */ | |
1807 | aa_info_message("AppArmor Filesystem Enabled"); | |
1808 | return 0; | |
1809 | ||
1810 | error: | |
1811 | aa_destroy_aafs(); | |
1812 | AA_ERROR("Error creating AppArmor securityfs\n"); | |
1813 | return error; | |
1814 | } | |
1815 | ||
1816 | fs_initcall(aa_create_aafs); |