]>
Commit | Line | Data |
---|---|---|
b5e95b48 JJ |
1 | /* |
2 | * AppArmor security module | |
3 | * | |
4 | * This file contains AppArmor LSM hooks. | |
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 | ||
3c4ed7bd | 15 | #include <linux/lsm_hooks.h> |
b5e95b48 JJ |
16 | #include <linux/moduleparam.h> |
17 | #include <linux/mm.h> | |
18 | #include <linux/mman.h> | |
19 | #include <linux/mount.h> | |
20 | #include <linux/namei.h> | |
21 | #include <linux/ptrace.h> | |
22 | #include <linux/ctype.h> | |
23 | #include <linux/sysctl.h> | |
24 | #include <linux/audit.h> | |
3486740a | 25 | #include <linux/user_namespace.h> |
e025be0f | 26 | #include <linux/kmemleak.h> |
b5e95b48 JJ |
27 | #include <net/sock.h> |
28 | ||
fd1cafbb | 29 | #include "include/af_unix.h" |
b5e95b48 JJ |
30 | #include "include/apparmor.h" |
31 | #include "include/apparmorfs.h" | |
32 | #include "include/audit.h" | |
33 | #include "include/capability.h" | |
34 | #include "include/context.h" | |
35 | #include "include/file.h" | |
36 | #include "include/ipc.h" | |
695b0eb9 | 37 | #include "include/net.h" |
b5e95b48 | 38 | #include "include/path.h" |
637f688d | 39 | #include "include/label.h" |
b5e95b48 | 40 | #include "include/policy.h" |
cff281f6 | 41 | #include "include/policy_ns.h" |
b5e95b48 | 42 | #include "include/procattr.h" |
21fad8c3 | 43 | #include "include/mount.h" |
b5e95b48 JJ |
44 | |
45 | /* Flag indicating whether initialization completed */ | |
545de8fe | 46 | int apparmor_initialized; |
b5e95b48 | 47 | |
d4669f0b JJ |
48 | DEFINE_PER_CPU(struct aa_buffers, aa_buffers); |
49 | ||
50 | ||
b5e95b48 JJ |
51 | /* |
52 | * LSM hook functions | |
53 | */ | |
54 | ||
55 | /* | |
637f688d | 56 | * free the associated aa_task_ctx and put its labels |
b5e95b48 JJ |
57 | */ |
58 | static void apparmor_cred_free(struct cred *cred) | |
59 | { | |
55a26ebf | 60 | aa_free_task_context(cred_ctx(cred)); |
b5e95b48 JJ |
61 | } |
62 | ||
63 | /* | |
55a26ebf | 64 | * prepare new aa_task_ctx for modification by prepare_cred block |
b5e95b48 JJ |
65 | */ |
66 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, | |
67 | gfp_t gfp) | |
68 | { | |
98738d70 | 69 | aa_dup_task_context(cred_ctx(new), cred_ctx(old)); |
b5e95b48 JJ |
70 | return 0; |
71 | } | |
72 | ||
73 | /* | |
74 | * transfer the apparmor data to a blank set of creds | |
75 | */ | |
76 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) | |
77 | { | |
55a26ebf JJ |
78 | const struct aa_task_ctx *old_ctx = cred_ctx(old); |
79 | struct aa_task_ctx *new_ctx = cred_ctx(new); | |
b5e95b48 | 80 | |
55a26ebf | 81 | aa_dup_task_context(new_ctx, old_ctx); |
b5e95b48 JJ |
82 | } |
83 | ||
84 | static int apparmor_ptrace_access_check(struct task_struct *child, | |
85 | unsigned int mode) | |
86 | { | |
b2d09ae4 JJ |
87 | struct aa_label *tracer, *tracee; |
88 | int error; | |
89 | ||
90 | tracer = begin_current_label_crit_section(); | |
91 | tracee = aa_get_task_label(child); | |
92 | error = aa_may_ptrace(tracer, tracee, | |
93 | mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE); | |
94 | aa_put_label(tracee); | |
95 | end_current_label_crit_section(tracer); | |
96 | ||
97 | return error; | |
b5e95b48 JJ |
98 | } |
99 | ||
100 | static int apparmor_ptrace_traceme(struct task_struct *parent) | |
101 | { | |
b2d09ae4 JJ |
102 | struct aa_label *tracer, *tracee; |
103 | int error; | |
104 | ||
105 | tracee = begin_current_label_crit_section(); | |
106 | tracer = aa_get_task_label(parent); | |
107 | error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE); | |
108 | aa_put_label(tracer); | |
109 | end_current_label_crit_section(tracee); | |
110 | ||
111 | return error; | |
b5e95b48 JJ |
112 | } |
113 | ||
114 | /* Derived from security/commoncap.c:cap_capget */ | |
115 | static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, | |
116 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | |
117 | { | |
637f688d | 118 | struct aa_label *label; |
b5e95b48 JJ |
119 | const struct cred *cred; |
120 | ||
121 | rcu_read_lock(); | |
122 | cred = __task_cred(target); | |
637f688d | 123 | label = aa_get_newest_cred_label(cred); |
c70c86c4 | 124 | |
b1d9e6b0 CS |
125 | /* |
126 | * cap_capget is stacked ahead of this and will | |
127 | * initialize effective and permitted. | |
128 | */ | |
c70c86c4 JJ |
129 | if (!unconfined(label)) { |
130 | struct aa_profile *profile; | |
131 | struct label_it i; | |
132 | ||
133 | label_for_each_confined(i, label, profile) { | |
134 | if (COMPLAIN_MODE(profile)) | |
135 | continue; | |
136 | *effective = cap_intersect(*effective, | |
137 | profile->caps.allow); | |
138 | *permitted = cap_intersect(*permitted, | |
139 | profile->caps.allow); | |
140 | } | |
b5e95b48 JJ |
141 | } |
142 | rcu_read_unlock(); | |
637f688d | 143 | aa_put_label(label); |
b5e95b48 JJ |
144 | |
145 | return 0; | |
146 | } | |
147 | ||
6a9de491 EP |
148 | static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, |
149 | int cap, int audit) | |
b5e95b48 | 150 | { |
637f688d | 151 | struct aa_label *label; |
b1d9e6b0 CS |
152 | int error = 0; |
153 | ||
637f688d JJ |
154 | label = aa_get_newest_cred_label(cred); |
155 | if (!unconfined(label)) | |
c70c86c4 | 156 | error = aa_capable(label, cap, audit); |
637f688d | 157 | aa_put_label(label); |
cf797c0e | 158 | |
b5e95b48 JJ |
159 | return error; |
160 | } | |
161 | ||
162 | /** | |
163 | * common_perm - basic common permission check wrapper fn for paths | |
164 | * @op: operation being checked | |
165 | * @path: path to check permission of (NOT NULL) | |
166 | * @mask: requested permissions mask | |
167 | * @cond: conditional info for the permission request (NOT NULL) | |
168 | * | |
169 | * Returns: %0 else error code if error or permission denied | |
170 | */ | |
47f6e5cc | 171 | static int common_perm(const char *op, const struct path *path, u32 mask, |
b5e95b48 JJ |
172 | struct path_cond *cond) |
173 | { | |
637f688d | 174 | struct aa_label *label; |
b5e95b48 JJ |
175 | int error = 0; |
176 | ||
637f688d JJ |
177 | label = __begin_current_label_crit_section(); |
178 | if (!unconfined(label)) | |
aebd873e | 179 | error = aa_path_perm(op, label, path, 0, mask, cond); |
637f688d | 180 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
181 | |
182 | return error; | |
183 | } | |
184 | ||
185 | /** | |
31f75bfe | 186 | * common_perm_cond - common permission wrapper around inode cond |
b5e95b48 | 187 | * @op: operation being checked |
31f75bfe | 188 | * @path: location to check (NOT NULL) |
b5e95b48 | 189 | * @mask: requested permissions mask |
b5e95b48 JJ |
190 | * |
191 | * Returns: %0 else error code if error or permission denied | |
192 | */ | |
31f75bfe | 193 | static int common_perm_cond(const char *op, const struct path *path, u32 mask) |
b5e95b48 | 194 | { |
31f75bfe JJ |
195 | struct path_cond cond = { d_backing_inode(path->dentry)->i_uid, |
196 | d_backing_inode(path->dentry)->i_mode | |
197 | }; | |
b5e95b48 | 198 | |
31f75bfe JJ |
199 | if (!path_mediated_fs(path->dentry)) |
200 | return 0; | |
201 | ||
202 | return common_perm(op, path, mask, &cond); | |
b5e95b48 JJ |
203 | } |
204 | ||
205 | /** | |
31f75bfe | 206 | * common_perm_dir_dentry - common permission wrapper when path is dir, dentry |
b5e95b48 | 207 | * @op: operation being checked |
31f75bfe JJ |
208 | * @dir: directory of the dentry (NOT NULL) |
209 | * @dentry: dentry to check (NOT NULL) | |
b5e95b48 | 210 | * @mask: requested permissions mask |
31f75bfe | 211 | * @cond: conditional info for the permission request (NOT NULL) |
b5e95b48 JJ |
212 | * |
213 | * Returns: %0 else error code if error or permission denied | |
214 | */ | |
31f75bfe JJ |
215 | static int common_perm_dir_dentry(const char *op, const struct path *dir, |
216 | struct dentry *dentry, u32 mask, | |
217 | struct path_cond *cond) | |
b5e95b48 | 218 | { |
31f75bfe | 219 | struct path path = { .mnt = dir->mnt, .dentry = dentry }; |
b5e95b48 | 220 | |
31f75bfe | 221 | return common_perm(op, &path, mask, cond); |
b5e95b48 JJ |
222 | } |
223 | ||
224 | /** | |
225 | * common_perm_rm - common permission wrapper for operations doing rm | |
226 | * @op: operation being checked | |
227 | * @dir: directory that the dentry is in (NOT NULL) | |
228 | * @dentry: dentry being rm'd (NOT NULL) | |
229 | * @mask: requested permission mask | |
230 | * | |
231 | * Returns: %0 else error code if error or permission denied | |
232 | */ | |
47f6e5cc | 233 | static int common_perm_rm(const char *op, const struct path *dir, |
b5e95b48 JJ |
234 | struct dentry *dentry, u32 mask) |
235 | { | |
c6f493d6 | 236 | struct inode *inode = d_backing_inode(dentry); |
b5e95b48 JJ |
237 | struct path_cond cond = { }; |
238 | ||
efeee83a | 239 | if (!inode || !path_mediated_fs(dentry)) |
b5e95b48 JJ |
240 | return 0; |
241 | ||
242 | cond.uid = inode->i_uid; | |
243 | cond.mode = inode->i_mode; | |
244 | ||
245 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
246 | } | |
247 | ||
248 | /** | |
249 | * common_perm_create - common permission wrapper for operations doing create | |
250 | * @op: operation being checked | |
251 | * @dir: directory that dentry will be created in (NOT NULL) | |
252 | * @dentry: dentry to create (NOT NULL) | |
253 | * @mask: request permission mask | |
254 | * @mode: created file mode | |
255 | * | |
256 | * Returns: %0 else error code if error or permission denied | |
257 | */ | |
47f6e5cc | 258 | static int common_perm_create(const char *op, const struct path *dir, |
d6b49f7a | 259 | struct dentry *dentry, u32 mask, umode_t mode) |
b5e95b48 JJ |
260 | { |
261 | struct path_cond cond = { current_fsuid(), mode }; | |
262 | ||
efeee83a | 263 | if (!path_mediated_fs(dir->dentry)) |
b5e95b48 JJ |
264 | return 0; |
265 | ||
266 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
267 | } | |
268 | ||
989f74e0 | 269 | static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
270 | { |
271 | return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); | |
272 | } | |
273 | ||
d3607752 | 274 | static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, |
4572befe | 275 | umode_t mode) |
b5e95b48 JJ |
276 | { |
277 | return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, | |
278 | S_IFDIR); | |
279 | } | |
280 | ||
989f74e0 | 281 | static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
282 | { |
283 | return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); | |
284 | } | |
285 | ||
d3607752 | 286 | static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, |
04fc66e7 | 287 | umode_t mode, unsigned int dev) |
b5e95b48 JJ |
288 | { |
289 | return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); | |
290 | } | |
291 | ||
81f4c506 | 292 | static int apparmor_path_truncate(const struct path *path) |
b5e95b48 | 293 | { |
e53cfe6c | 294 | return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); |
b5e95b48 JJ |
295 | } |
296 | ||
d3607752 | 297 | static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, |
b5e95b48 JJ |
298 | const char *old_name) |
299 | { | |
300 | return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, | |
301 | S_IFLNK); | |
302 | } | |
303 | ||
3ccee46a | 304 | static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, |
b5e95b48 JJ |
305 | struct dentry *new_dentry) |
306 | { | |
637f688d | 307 | struct aa_label *label; |
b5e95b48 JJ |
308 | int error = 0; |
309 | ||
efeee83a | 310 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 JJ |
311 | return 0; |
312 | ||
637f688d JJ |
313 | label = begin_current_label_crit_section(); |
314 | if (!unconfined(label)) | |
8014370f | 315 | error = aa_path_link(label, old_dentry, new_dir, new_dentry); |
637f688d | 316 | end_current_label_crit_section(label); |
cf797c0e | 317 | |
b5e95b48 JJ |
318 | return error; |
319 | } | |
320 | ||
3ccee46a AV |
321 | static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, |
322 | const struct path *new_dir, struct dentry *new_dentry) | |
b5e95b48 | 323 | { |
637f688d | 324 | struct aa_label *label; |
b5e95b48 JJ |
325 | int error = 0; |
326 | ||
efeee83a | 327 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 JJ |
328 | return 0; |
329 | ||
637f688d JJ |
330 | label = begin_current_label_crit_section(); |
331 | if (!unconfined(label)) { | |
8486adf0 KC |
332 | struct path old_path = { .mnt = old_dir->mnt, |
333 | .dentry = old_dentry }; | |
334 | struct path new_path = { .mnt = new_dir->mnt, | |
335 | .dentry = new_dentry }; | |
c6f493d6 DH |
336 | struct path_cond cond = { d_backing_inode(old_dentry)->i_uid, |
337 | d_backing_inode(old_dentry)->i_mode | |
b5e95b48 JJ |
338 | }; |
339 | ||
aebd873e | 340 | error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, |
e53cfe6c JJ |
341 | MAY_READ | AA_MAY_GETATTR | MAY_WRITE | |
342 | AA_MAY_SETATTR | AA_MAY_DELETE, | |
b5e95b48 JJ |
343 | &cond); |
344 | if (!error) | |
aebd873e | 345 | error = aa_path_perm(OP_RENAME_DEST, label, &new_path, |
e53cfe6c | 346 | 0, MAY_WRITE | AA_MAY_SETATTR | |
b5e95b48 JJ |
347 | AA_MAY_CREATE, &cond); |
348 | ||
349 | } | |
637f688d | 350 | end_current_label_crit_section(label); |
cf797c0e | 351 | |
b5e95b48 JJ |
352 | return error; |
353 | } | |
354 | ||
be01f9f2 | 355 | static int apparmor_path_chmod(const struct path *path, umode_t mode) |
b5e95b48 | 356 | { |
31f75bfe | 357 | return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); |
b5e95b48 JJ |
358 | } |
359 | ||
7fd25dac | 360 | static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
b5e95b48 | 361 | { |
31f75bfe | 362 | return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); |
b5e95b48 JJ |
363 | } |
364 | ||
3f7036a0 | 365 | static int apparmor_inode_getattr(const struct path *path) |
b5e95b48 | 366 | { |
e53cfe6c | 367 | return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); |
b5e95b48 JJ |
368 | } |
369 | ||
83d49856 | 370 | static int apparmor_file_open(struct file *file, const struct cred *cred) |
b5e95b48 | 371 | { |
637f688d JJ |
372 | struct aa_file_ctx *fctx = file_ctx(file); |
373 | struct aa_label *label; | |
b5e95b48 JJ |
374 | int error = 0; |
375 | ||
efeee83a | 376 | if (!path_mediated_fs(file->f_path.dentry)) |
b5e95b48 JJ |
377 | return 0; |
378 | ||
379 | /* If in exec, permission is handled by bprm hooks. | |
380 | * Cache permissions granted by the previous exec check, with | |
381 | * implicit read and executable mmap which are required to | |
382 | * actually execute the image. | |
383 | */ | |
384 | if (current->in_execve) { | |
55a26ebf | 385 | fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; |
b5e95b48 JJ |
386 | return 0; |
387 | } | |
388 | ||
637f688d JJ |
389 | label = aa_get_newest_cred_label(cred); |
390 | if (!unconfined(label)) { | |
496ad9aa | 391 | struct inode *inode = file_inode(file); |
b5e95b48 JJ |
392 | struct path_cond cond = { inode->i_uid, inode->i_mode }; |
393 | ||
aebd873e | 394 | error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, |
b5e95b48 JJ |
395 | aa_map_file_to_perms(file), &cond); |
396 | /* todo cache full allowed permissions set and state */ | |
55a26ebf | 397 | fctx->allow = aa_map_file_to_perms(file); |
b5e95b48 | 398 | } |
637f688d | 399 | aa_put_label(label); |
b5e95b48 JJ |
400 | |
401 | return error; | |
402 | } | |
403 | ||
404 | static int apparmor_file_alloc_security(struct file *file) | |
405 | { | |
004c5200 | 406 | struct aa_file_ctx *ctx = file_ctx(file); |
637f688d | 407 | struct aa_label *label = begin_current_label_crit_section(); |
b5e95b48 | 408 | |
004c5200 CS |
409 | spin_lock_init(&ctx->lock); |
410 | rcu_assign_pointer(ctx->label, aa_get_label(label)); | |
411 | end_current_label_crit_section(label); | |
412 | return 0; | |
b5e95b48 JJ |
413 | } |
414 | ||
415 | static void apparmor_file_free_security(struct file *file) | |
416 | { | |
004c5200 CS |
417 | struct aa_file_ctx *ctx = file_ctx(file); |
418 | ||
419 | if (ctx) | |
420 | aa_put_label(rcu_access_pointer(ctx->label)); | |
b5e95b48 JJ |
421 | } |
422 | ||
47f6e5cc | 423 | static int common_file_perm(const char *op, struct file *file, u32 mask) |
b5e95b48 | 424 | { |
190a9518 | 425 | struct aa_label *label; |
b5e95b48 JJ |
426 | int error = 0; |
427 | ||
192ca6b5 JJ |
428 | /* don't reaudit files closed during inheritance */ |
429 | if (file->f_path.dentry == aa_null.dentry) | |
430 | return -EACCES; | |
431 | ||
637f688d | 432 | label = __begin_current_label_crit_section(); |
190a9518 | 433 | error = aa_file_perm(op, label, file, mask); |
637f688d | 434 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
435 | |
436 | return error; | |
437 | } | |
438 | ||
064dc947 JJ |
439 | static int apparmor_file_receive(struct file *file) |
440 | { | |
441 | return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file)); | |
442 | } | |
443 | ||
b5e95b48 JJ |
444 | static int apparmor_file_permission(struct file *file, int mask) |
445 | { | |
446 | return common_file_perm(OP_FPERM, file, mask); | |
447 | } | |
448 | ||
449 | static int apparmor_file_lock(struct file *file, unsigned int cmd) | |
450 | { | |
451 | u32 mask = AA_MAY_LOCK; | |
452 | ||
453 | if (cmd == F_WRLCK) | |
454 | mask |= MAY_WRITE; | |
455 | ||
456 | return common_file_perm(OP_FLOCK, file, mask); | |
457 | } | |
458 | ||
47f6e5cc | 459 | static int common_mmap(const char *op, struct file *file, unsigned long prot, |
b5e95b48 JJ |
460 | unsigned long flags) |
461 | { | |
b5e95b48 JJ |
462 | int mask = 0; |
463 | ||
637f688d | 464 | if (!file || !file_ctx(file)) |
b5e95b48 JJ |
465 | return 0; |
466 | ||
467 | if (prot & PROT_READ) | |
468 | mask |= MAY_READ; | |
469 | /* | |
470 | * Private mappings don't require write perms since they don't | |
471 | * write back to the files | |
472 | */ | |
473 | if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) | |
474 | mask |= MAY_WRITE; | |
475 | if (prot & PROT_EXEC) | |
476 | mask |= AA_EXEC_MMAP; | |
477 | ||
b5e95b48 JJ |
478 | return common_file_perm(op, file, mask); |
479 | } | |
480 | ||
e5467859 AV |
481 | static int apparmor_mmap_file(struct file *file, unsigned long reqprot, |
482 | unsigned long prot, unsigned long flags) | |
b5e95b48 | 483 | { |
b5e95b48 JJ |
484 | return common_mmap(OP_FMMAP, file, prot, flags); |
485 | } | |
486 | ||
487 | static int apparmor_file_mprotect(struct vm_area_struct *vma, | |
488 | unsigned long reqprot, unsigned long prot) | |
489 | { | |
490 | return common_mmap(OP_FMPROT, vma->vm_file, prot, | |
491 | !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0); | |
492 | } | |
493 | ||
21fad8c3 JJ |
494 | static int apparmor_sb_mount(const char *dev_name, const struct path *path, |
495 | const char *type, unsigned long flags, void *data) | |
496 | { | |
497 | struct aa_label *label; | |
498 | int error = 0; | |
499 | ||
500 | /* Discard magic */ | |
501 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) | |
502 | flags &= ~MS_MGC_MSK; | |
503 | ||
504 | flags &= ~AA_MS_IGNORE_MASK; | |
505 | ||
506 | label = __begin_current_label_crit_section(); | |
507 | if (!unconfined(label)) { | |
508 | if (flags & MS_REMOUNT) | |
509 | error = aa_remount(label, path, flags, data); | |
510 | else if (flags & MS_BIND) | |
511 | error = aa_bind_mount(label, path, dev_name, flags); | |
512 | else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | | |
513 | MS_UNBINDABLE)) | |
514 | error = aa_mount_change_type(label, path, flags); | |
515 | else if (flags & MS_MOVE) | |
516 | error = aa_move_mount(label, path, dev_name); | |
517 | else | |
518 | error = aa_new_mount(label, dev_name, path, type, | |
519 | flags, data); | |
520 | } | |
521 | __end_current_label_crit_section(label); | |
522 | ||
523 | return error; | |
524 | } | |
525 | ||
526 | static int apparmor_sb_umount(struct vfsmount *mnt, int flags) | |
527 | { | |
528 | struct aa_label *label; | |
529 | int error = 0; | |
530 | ||
531 | label = __begin_current_label_crit_section(); | |
532 | if (!unconfined(label)) | |
533 | error = aa_umount(label, mnt, flags); | |
534 | __end_current_label_crit_section(label); | |
535 | ||
536 | return error; | |
537 | } | |
538 | ||
539 | static int apparmor_sb_pivotroot(const struct path *old_path, | |
540 | const struct path *new_path) | |
541 | { | |
542 | struct aa_label *label; | |
543 | int error = 0; | |
544 | ||
545 | label = aa_get_current_label(); | |
546 | if (!unconfined(label)) | |
547 | error = aa_pivotroot(label, old_path, new_path); | |
548 | aa_put_label(label); | |
549 | ||
550 | return error; | |
551 | } | |
552 | ||
b5e95b48 JJ |
553 | static int apparmor_getprocattr(struct task_struct *task, char *name, |
554 | char **value) | |
555 | { | |
556 | int error = -ENOENT; | |
b5e95b48 JJ |
557 | /* released below */ |
558 | const struct cred *cred = get_task_cred(task); | |
55a26ebf | 559 | struct aa_task_ctx *ctx = cred_ctx(cred); |
637f688d | 560 | struct aa_label *label = NULL; |
a3c3cde2 CS |
561 | char *vp; |
562 | char *np; | |
b5e95b48 JJ |
563 | |
564 | if (strcmp(name, "current") == 0) | |
637f688d | 565 | label = aa_get_newest_label(ctx->label); |
a3c3cde2 CS |
566 | else if (strcmp(name, "context") == 0 && ctx->label) |
567 | label = aa_get_newest_label(ctx->label); | |
55a26ebf | 568 | else if (strcmp(name, "prev") == 0 && ctx->previous) |
637f688d | 569 | label = aa_get_newest_label(ctx->previous); |
55a26ebf | 570 | else if (strcmp(name, "exec") == 0 && ctx->onexec) |
637f688d | 571 | label = aa_get_newest_label(ctx->onexec); |
b5e95b48 JJ |
572 | else |
573 | error = -EINVAL; | |
574 | ||
a3c3cde2 CS |
575 | if (label == NULL) |
576 | goto put_out; | |
577 | ||
578 | error = aa_getprocattr(label, &vp); | |
579 | if (error < 0) | |
580 | goto put_out; | |
581 | ||
582 | if (strcmp(name, "context") == 0) { | |
583 | *value = kasprintf(GFP_KERNEL, "apparmor='%s'", vp); | |
584 | if (*value == NULL) { | |
585 | error = -ENOMEM; | |
586 | goto put_out; | |
587 | } | |
588 | np = strchr(*value, '\n'); | |
589 | if (np != NULL) { | |
590 | np[0] = '\''; | |
591 | np[1] = '\0'; | |
592 | } | |
593 | error = strlen(*value); | |
594 | } else | |
595 | *value = vp; | |
596 | ||
597 | put_out: | |
77b071b3 | 598 | |
637f688d | 599 | aa_put_label(label); |
b5e95b48 JJ |
600 | put_cred(cred); |
601 | ||
602 | return error; | |
603 | } | |
604 | ||
b21507e2 SS |
605 | static int apparmor_setprocattr(const char *name, void *value, |
606 | size_t size) | |
b5e95b48 | 607 | { |
e89b8081 | 608 | char *command, *largs = NULL, *args = value; |
b5e95b48 JJ |
609 | size_t arg_size; |
610 | int error; | |
ef88a7ac | 611 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR); |
b5e95b48 JJ |
612 | |
613 | if (size == 0) | |
614 | return -EINVAL; | |
b5e95b48 | 615 | |
e89b8081 VN |
616 | /* AppArmor requires that the buffer must be null terminated atm */ |
617 | if (args[size - 1] != '\0') { | |
618 | /* null terminate */ | |
619 | largs = args = kmalloc(size + 1, GFP_KERNEL); | |
620 | if (!args) | |
621 | return -ENOMEM; | |
622 | memcpy(args, value, size); | |
623 | args[size] = '\0'; | |
624 | } | |
625 | ||
626 | error = -EINVAL; | |
b5e95b48 JJ |
627 | args = strim(args); |
628 | command = strsep(&args, " "); | |
629 | if (!args) | |
e89b8081 | 630 | goto out; |
b5e95b48 JJ |
631 | args = skip_spaces(args); |
632 | if (!*args) | |
e89b8081 | 633 | goto out; |
b5e95b48 | 634 | |
d4d03f74 | 635 | arg_size = size - (args - (largs ? largs : (char *) value)); |
a3c3cde2 | 636 | if (strcmp(name, "current") == 0 || strcmp(name, "context") == 0) { |
b5e95b48 JJ |
637 | if (strcmp(command, "changehat") == 0) { |
638 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 639 | AA_CHANGE_NOFLAGS); |
b5e95b48 JJ |
640 | } else if (strcmp(command, "permhat") == 0) { |
641 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 642 | AA_CHANGE_TEST); |
b5e95b48 | 643 | } else if (strcmp(command, "changeprofile") == 0) { |
df8073c6 | 644 | error = aa_change_profile(args, AA_CHANGE_NOFLAGS); |
b5e95b48 | 645 | } else if (strcmp(command, "permprofile") == 0) { |
df8073c6 | 646 | error = aa_change_profile(args, AA_CHANGE_TEST); |
6c5fc8f1 JJ |
647 | } else if (strcmp(command, "stack") == 0) { |
648 | error = aa_change_profile(args, AA_CHANGE_STACK); | |
3eea57c2 JJ |
649 | } else |
650 | goto fail; | |
b5e95b48 | 651 | } else if (strcmp(name, "exec") == 0) { |
3eea57c2 | 652 | if (strcmp(command, "exec") == 0) |
df8073c6 | 653 | error = aa_change_profile(args, AA_CHANGE_ONEXEC); |
6c5fc8f1 JJ |
654 | else if (strcmp(command, "stack") == 0) |
655 | error = aa_change_profile(args, (AA_CHANGE_ONEXEC | | |
656 | AA_CHANGE_STACK)); | |
3eea57c2 JJ |
657 | else |
658 | goto fail; | |
659 | } else | |
a3c3cde2 CS |
660 | /* |
661 | * only support the "current", "context" and "exec" | |
662 | * process attributes | |
663 | */ | |
e89b8081 | 664 | goto fail; |
3eea57c2 | 665 | |
b5e95b48 JJ |
666 | if (!error) |
667 | error = size; | |
e89b8081 VN |
668 | out: |
669 | kfree(largs); | |
b5e95b48 | 670 | return error; |
3eea57c2 JJ |
671 | |
672 | fail: | |
637f688d | 673 | aad(&sa)->label = begin_current_label_crit_section(); |
ef88a7ac JJ |
674 | aad(&sa)->info = name; |
675 | aad(&sa)->error = error = -EINVAL; | |
3eea57c2 | 676 | aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); |
637f688d | 677 | end_current_label_crit_section(aad(&sa)->label); |
e89b8081 | 678 | goto out; |
b5e95b48 JJ |
679 | } |
680 | ||
fe864821 JJ |
681 | /** |
682 | * apparmor_bprm_committing_creds - do task cleanup on committing new creds | |
683 | * @bprm: binprm for the exec (NOT NULL) | |
684 | */ | |
685 | static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) | |
686 | { | |
637f688d | 687 | struct aa_label *label = aa_current_raw_label(); |
fe864821 JJ |
688 | struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred); |
689 | ||
690 | /* bail out if unconfined or not changing profile */ | |
637f688d JJ |
691 | if ((new_ctx->label->proxy == label->proxy) || |
692 | (unconfined(new_ctx->label))) | |
fe864821 JJ |
693 | return; |
694 | ||
192ca6b5 JJ |
695 | aa_inherit_files(bprm->cred, current->files); |
696 | ||
fe864821 JJ |
697 | current->pdeath_signal = 0; |
698 | ||
637f688d | 699 | /* reset soft limits and set hard limits for the new label */ |
86b92cb7 | 700 | __aa_transition_rlimits(label, new_ctx->label); |
fe864821 JJ |
701 | } |
702 | ||
703 | /** | |
704 | * apparmor_bprm_committed_cred - do cleanup after new creds committed | |
705 | * @bprm: binprm for the exec (NOT NULL) | |
706 | */ | |
707 | static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) | |
708 | { | |
709 | /* TODO: cleanup signals - ipc mediation */ | |
710 | return; | |
711 | } | |
712 | ||
7cb4dc9f JS |
713 | static int apparmor_task_setrlimit(struct task_struct *task, |
714 | unsigned int resource, struct rlimit *new_rlim) | |
b5e95b48 | 715 | { |
637f688d | 716 | struct aa_label *label = __begin_current_label_crit_section(); |
b5e95b48 JJ |
717 | int error = 0; |
718 | ||
637f688d | 719 | if (!unconfined(label)) |
86b92cb7 | 720 | error = aa_task_setrlimit(label, task, resource, new_rlim); |
637f688d | 721 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
722 | |
723 | return error; | |
724 | } | |
725 | ||
84632f19 JJ |
726 | static int apparmor_task_kill(struct task_struct *target, struct siginfo *info, |
727 | int sig, u32 secid) | |
728 | { | |
729 | struct aa_label *cl, *tl; | |
730 | int error; | |
731 | ||
732 | if (secid) | |
733 | /* TODO: after secid to label mapping is done. | |
734 | * Dealing with USB IO specific behavior | |
735 | */ | |
736 | return 0; | |
737 | cl = __begin_current_label_crit_section(); | |
738 | tl = aa_get_task_label(target); | |
739 | error = aa_may_signal(cl, tl, sig); | |
740 | aa_put_label(tl); | |
741 | __end_current_label_crit_section(cl); | |
742 | ||
743 | return error; | |
744 | } | |
745 | ||
695b0eb9 JJ |
746 | /** |
747 | * apparmor_sk_alloc_security - allocate and attach the sk_security field | |
748 | */ | |
749 | static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) | |
750 | { | |
7a376a52 JS |
751 | struct aa_sk_ctx *ctx; |
752 | ||
753 | ctx = kzalloc(sizeof(*ctx), flags); | |
754 | if (!ctx) | |
755 | return -ENOMEM; | |
756 | ||
757 | SK_CTX(sk) = ctx; | |
695b0eb9 JJ |
758 | |
759 | return 0; | |
760 | } | |
761 | ||
762 | /** | |
763 | * apparmor_sk_free_security - free the sk_security field | |
764 | */ | |
765 | static void apparmor_sk_free_security(struct sock *sk) | |
766 | { | |
767 | struct aa_sk_ctx *ctx = SK_CTX(sk); | |
768 | ||
7a376a52 | 769 | SK_CTX(sk) = NULL; |
695b0eb9 JJ |
770 | aa_put_label(ctx->label); |
771 | aa_put_label(ctx->peer); | |
772 | path_put(&ctx->path); | |
7a376a52 | 773 | kfree(ctx); |
695b0eb9 JJ |
774 | } |
775 | ||
776 | /** | |
777 | * apparmor_clone_security - clone the sk_security field | |
778 | */ | |
779 | static void apparmor_sk_clone_security(const struct sock *sk, | |
780 | struct sock *newsk) | |
781 | { | |
782 | struct aa_sk_ctx *ctx = SK_CTX(sk); | |
783 | struct aa_sk_ctx *new = SK_CTX(newsk); | |
784 | ||
785 | new->label = aa_get_label(ctx->label); | |
786 | new->peer = aa_get_label(ctx->peer); | |
787 | new->path = ctx->path; | |
788 | path_get(&new->path); | |
789 | } | |
790 | ||
fd1cafbb | 791 | static struct path *UNIX_FS_CONN_PATH(struct sock *sk, struct sock *newsk) |
695b0eb9 | 792 | { |
fd1cafbb JJ |
793 | if (sk->sk_family == PF_UNIX && UNIX_FS(sk)) |
794 | return &unix_sk(sk)->path; | |
795 | else if (newsk->sk_family == PF_UNIX && UNIX_FS(newsk)) | |
796 | return &unix_sk(newsk)->path; | |
797 | return NULL; | |
798 | } | |
799 | ||
800 | /** | |
801 | * apparmor_unix_stream_connect - check perms before making unix domain conn | |
802 | * | |
803 | * peer is locked when this hook is called | |
804 | */ | |
805 | static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk, | |
806 | struct sock *newsk) | |
807 | { | |
808 | struct aa_sk_ctx *sk_ctx = SK_CTX(sk); | |
809 | struct aa_sk_ctx *peer_ctx = SK_CTX(peer_sk); | |
810 | struct aa_sk_ctx *new_ctx = SK_CTX(newsk); | |
811 | struct aa_label *label; | |
812 | struct path *path; | |
813 | int error; | |
695b0eb9 | 814 | |
fd1cafbb JJ |
815 | label = __begin_current_label_crit_section(); |
816 | error = aa_unix_peer_perm(label, OP_CONNECT, | |
817 | (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE), | |
818 | sk, peer_sk, NULL); | |
819 | if (!UNIX_FS(peer_sk)) { | |
820 | last_error(error, | |
821 | aa_unix_peer_perm(peer_ctx->label, OP_CONNECT, | |
822 | (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE), | |
823 | peer_sk, sk, label)); | |
824 | } | |
825 | __end_current_label_crit_section(label); | |
826 | ||
827 | if (error) | |
828 | return error; | |
829 | ||
830 | /* label newsk if it wasn't labeled in post_create. Normally this | |
831 | * would be done in sock_graft, but because we are directly looking | |
832 | * at the peer_sk to obtain peer_labeling for unix socks this | |
833 | * does not work | |
834 | */ | |
835 | if (!new_ctx->label) | |
836 | new_ctx->label = aa_get_label(peer_ctx->label); | |
837 | ||
838 | /* Cross reference the peer labels for SO_PEERSEC */ | |
839 | if (new_ctx->peer) | |
840 | aa_put_label(new_ctx->peer); | |
841 | ||
842 | if (sk_ctx->peer) | |
843 | aa_put_label(sk_ctx->peer); | |
844 | ||
845 | new_ctx->peer = aa_get_label(sk_ctx->label); | |
846 | sk_ctx->peer = aa_get_label(peer_ctx->label); | |
847 | ||
848 | path = UNIX_FS_CONN_PATH(sk, peer_sk); | |
849 | if (path) { | |
850 | new_ctx->path = *path; | |
851 | sk_ctx->path = *path; | |
852 | path_get(path); | |
853 | path_get(path); | |
854 | } | |
855 | return 0; | |
695b0eb9 JJ |
856 | } |
857 | ||
fd1cafbb JJ |
858 | /** |
859 | * apparmor_unix_may_send - check perms before conn or sending unix dgrams | |
860 | * | |
861 | * other is locked when this hook is called | |
862 | * | |
863 | * dgram connect calls may_send, peer setup but path not copied????? | |
864 | */ | |
865 | static int apparmor_unix_may_send(struct socket *sock, struct socket *peer) | |
866 | { | |
867 | struct aa_sk_ctx *peer_ctx = SK_CTX(peer->sk); | |
868 | struct aa_label *label; | |
869 | int error; | |
870 | ||
871 | label = __begin_current_label_crit_section(); | |
872 | error = xcheck(aa_unix_peer_perm(label, OP_SENDMSG, AA_MAY_SEND, | |
873 | sock->sk, peer->sk, NULL), | |
874 | aa_unix_peer_perm(peer_ctx->label, OP_SENDMSG, | |
875 | AA_MAY_RECEIVE, | |
876 | peer->sk, sock->sk, label)); | |
877 | __end_current_label_crit_section(label); | |
878 | ||
879 | return error; | |
880 | } | |
695b0eb9 JJ |
881 | |
882 | /** | |
883 | * apparmor_socket_create - check perms before creating a new socket | |
884 | */ | |
885 | static int apparmor_socket_create(int family, int type, int protocol, int kern) | |
886 | { | |
887 | struct aa_label *label; | |
888 | int error = 0; | |
889 | ||
890 | label = begin_current_label_crit_section(); | |
891 | if (!(kern || unconfined(label))) | |
892 | error = aa_sock_create_perm(label, family, type, protocol); | |
893 | end_current_label_crit_section(label); | |
894 | ||
895 | return error; | |
896 | } | |
897 | ||
898 | /** | |
899 | * apparmor_socket_post_create - setup the per-socket security struct | |
900 | * | |
901 | * Note: | |
902 | * - kernel sockets currently labeled unconfined but we may want to | |
903 | * move to a special kernel label | |
904 | * - socket may not have sk here if created with sock_create_lite or | |
905 | * sock_alloc. These should be accept cases which will be handled in | |
906 | * sock_graft. | |
907 | */ | |
908 | static int apparmor_socket_post_create(struct socket *sock, int family, | |
909 | int type, int protocol, int kern) | |
910 | { | |
911 | struct aa_label *label; | |
912 | ||
913 | if (kern) { | |
914 | struct aa_ns *ns = aa_get_current_ns(); | |
915 | ||
916 | label = aa_get_label(ns_unconfined(ns)); | |
917 | aa_put_ns(ns); | |
918 | } else | |
919 | label = aa_get_current_label(); | |
920 | ||
921 | if (sock->sk) { | |
922 | struct aa_sk_ctx *ctx = SK_CTX(sock->sk); | |
923 | ||
924 | aa_put_label(ctx->label); | |
925 | ctx->label = aa_get_label(label); | |
926 | } | |
927 | aa_put_label(label); | |
928 | ||
929 | return 0; | |
930 | } | |
931 | ||
932 | /** | |
933 | * apparmor_socket_bind - check perms before bind addr to socket | |
934 | */ | |
935 | static int apparmor_socket_bind(struct socket *sock, | |
936 | struct sockaddr *address, int addrlen) | |
937 | { | |
fd1cafbb | 938 | return aa_sock_bind_perm(sock, address, addrlen); |
695b0eb9 JJ |
939 | } |
940 | ||
941 | /** | |
942 | * apparmor_socket_connect - check perms before connecting @sock to @address | |
943 | */ | |
944 | static int apparmor_socket_connect(struct socket *sock, | |
945 | struct sockaddr *address, int addrlen) | |
946 | { | |
fd1cafbb | 947 | return aa_sock_connect_perm(sock, address, addrlen); |
695b0eb9 JJ |
948 | } |
949 | ||
950 | /** | |
951 | * apparmor_socket_list - check perms before allowing listen | |
952 | */ | |
953 | static int apparmor_socket_listen(struct socket *sock, int backlog) | |
954 | { | |
fd1cafbb | 955 | return aa_sock_listen_perm(sock, backlog); |
695b0eb9 JJ |
956 | } |
957 | ||
958 | /** | |
959 | * apparmor_socket_accept - check perms before accepting a new connection. | |
960 | * | |
961 | * Note: while @newsock is created and has some information, the accept | |
962 | * has not been done. | |
963 | */ | |
964 | static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) | |
965 | { | |
fd1cafbb | 966 | return aa_sock_accept_perm(sock, newsock); |
695b0eb9 JJ |
967 | } |
968 | ||
969 | /** | |
970 | * apparmor_socket_sendmsg - check perms before sending msg to another socket | |
971 | */ | |
972 | static int apparmor_socket_sendmsg(struct socket *sock, | |
973 | struct msghdr *msg, int size) | |
974 | { | |
975 | return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); | |
976 | } | |
977 | ||
978 | /** | |
979 | * apparmor_socket_recvmsg - check perms before receiving a message | |
980 | */ | |
981 | static int apparmor_socket_recvmsg(struct socket *sock, | |
982 | struct msghdr *msg, int size, int flags) | |
983 | { | |
984 | return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); | |
985 | } | |
986 | ||
695b0eb9 JJ |
987 | /** |
988 | * apparmor_socket_getsockname - check perms before getting the local address | |
989 | */ | |
990 | static int apparmor_socket_getsockname(struct socket *sock) | |
991 | { | |
992 | return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); | |
993 | } | |
994 | ||
995 | /** | |
996 | * apparmor_socket_getpeername - check perms before getting remote address | |
997 | */ | |
998 | static int apparmor_socket_getpeername(struct socket *sock) | |
999 | { | |
1000 | return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); | |
1001 | } | |
1002 | ||
695b0eb9 JJ |
1003 | /** |
1004 | * apparmor_getsockopt - check perms before getting socket options | |
1005 | */ | |
1006 | static int apparmor_socket_getsockopt(struct socket *sock, int level, | |
1007 | int optname) | |
1008 | { | |
1009 | return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, | |
1010 | level, optname); | |
1011 | } | |
1012 | ||
1013 | /** | |
1014 | * apparmor_setsockopt - check perms before setting socket options | |
1015 | */ | |
1016 | static int apparmor_socket_setsockopt(struct socket *sock, int level, | |
1017 | int optname) | |
1018 | { | |
1019 | return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, | |
1020 | level, optname); | |
1021 | } | |
1022 | ||
1023 | /** | |
1024 | * apparmor_socket_shutdown - check perms before shutting down @sock conn | |
1025 | */ | |
1026 | static int apparmor_socket_shutdown(struct socket *sock, int how) | |
1027 | { | |
1028 | return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); | |
1029 | } | |
1030 | ||
1031 | /** | |
1032 | * apparmor_socket_sock_recv_skb - check perms before associating skb to sk | |
1033 | * | |
1034 | * Note: can not sleep may be called with locks held | |
1035 | * | |
1036 | * dont want protocol specific in __skb_recv_datagram() | |
1037 | * to deny an incoming connection socket_sock_rcv_skb() | |
1038 | */ | |
1039 | static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
1040 | { | |
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | ||
1045 | static struct aa_label *sk_peer_label(struct sock *sk) | |
1046 | { | |
fd1cafbb | 1047 | struct sock *peer_sk; |
695b0eb9 JJ |
1048 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
1049 | ||
1050 | if (ctx->peer) | |
1051 | return ctx->peer; | |
1052 | ||
fd1cafbb JJ |
1053 | if (sk->sk_family != PF_UNIX) |
1054 | return ERR_PTR(-ENOPROTOOPT); | |
1055 | ||
1056 | /* check for sockpair peering which does not go through | |
1057 | * security_unix_stream_connect | |
1058 | */ | |
1059 | peer_sk = unix_peer(sk); | |
1060 | if (peer_sk) { | |
1061 | ctx = SK_CTX(peer_sk); | |
1062 | if (ctx->label) | |
1063 | return ctx->label; | |
1064 | } | |
1065 | ||
695b0eb9 JJ |
1066 | return ERR_PTR(-ENOPROTOOPT); |
1067 | } | |
1068 | ||
1069 | /** | |
1070 | * apparmor_socket_getpeersec_stream - get security context of peer | |
1071 | * | |
1072 | * Note: for tcp only valid if using ipsec or cipso on lan | |
1073 | */ | |
1074 | static int apparmor_socket_getpeersec_stream(struct socket *sock, | |
1075 | char __user *optval, | |
1076 | int __user *optlen, | |
1077 | unsigned int len) | |
1078 | { | |
1079 | char *name; | |
1080 | int slen, error = 0; | |
1081 | struct aa_label *label; | |
1082 | struct aa_label *peer; | |
1083 | ||
1084 | label = begin_current_label_crit_section(); | |
1085 | peer = sk_peer_label(sock->sk); | |
1086 | if (IS_ERR(peer)) { | |
1087 | error = PTR_ERR(peer); | |
1088 | goto done; | |
1089 | } | |
1090 | slen = aa_label_asxprint(&name, labels_ns(label), peer, | |
1091 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | | |
1092 | FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); | |
1093 | /* don't include terminating \0 in slen, it breaks some apps */ | |
1094 | if (slen < 0) { | |
1095 | error = -ENOMEM; | |
1096 | } else { | |
1097 | if (slen > len) { | |
1098 | error = -ERANGE; | |
1099 | } else if (copy_to_user(optval, name, slen)) { | |
1100 | error = -EFAULT; | |
1101 | goto out; | |
1102 | } | |
1103 | if (put_user(slen, optlen)) | |
1104 | error = -EFAULT; | |
1105 | out: | |
1106 | kfree(name); | |
1107 | ||
1108 | } | |
1109 | ||
1110 | done: | |
1111 | end_current_label_crit_section(label); | |
1112 | ||
1113 | return error; | |
1114 | } | |
1115 | ||
1116 | /** | |
1117 | * apparmor_socket_getpeersec_dgram - get security label of packet | |
1118 | * @sock: the peer socket | |
1119 | * @skb: packet data | |
1120 | * @secid: pointer to where to put the secid of the packet | |
1121 | * | |
1122 | * Sets the netlabel socket state on sk from parent | |
1123 | */ | |
1124 | static int apparmor_socket_getpeersec_dgram(struct socket *sock, | |
1125 | struct sk_buff *skb, u32 *secid) | |
1126 | ||
1127 | { | |
1128 | /* TODO: requires secid support */ | |
1129 | return -ENOPROTOOPT; | |
1130 | } | |
1131 | ||
1132 | /** | |
1133 | * apparmor_sock_graft - Initialize newly created socket | |
1134 | * @sk: child sock | |
1135 | * @parent: parent socket | |
1136 | * | |
1137 | * Note: could set off of SOCK_CTX(parent) but need to track inode and we can | |
1138 | * just set sk security information off of current creating process label | |
1139 | * Labeling of sk for accept case - probably should be sock based | |
1140 | * instead of task, because of the case where an implicitly labeled | |
1141 | * socket is shared by different tasks. | |
1142 | */ | |
1143 | static void apparmor_sock_graft(struct sock *sk, struct socket *parent) | |
1144 | { | |
1145 | struct aa_sk_ctx *ctx = SK_CTX(sk); | |
1146 | ||
1147 | if (!ctx->label) | |
1148 | ctx->label = aa_get_current_label(); | |
1149 | } | |
1150 | ||
98738d70 CS |
1151 | struct lsm_blob_sizes apparmor_blob_sizes = { |
1152 | .lbs_cred = sizeof(struct aa_task_ctx), | |
004c5200 | 1153 | .lbs_file = sizeof(struct aa_file_ctx), |
98738d70 CS |
1154 | }; |
1155 | ||
ca97d939 | 1156 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
e20b043a CS |
1157 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
1158 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), | |
1159 | LSM_HOOK_INIT(capget, apparmor_capget), | |
1160 | LSM_HOOK_INIT(capable, apparmor_capable), | |
1161 | ||
21fad8c3 JJ |
1162 | LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), |
1163 | LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), | |
1164 | LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), | |
1165 | ||
e20b043a CS |
1166 | LSM_HOOK_INIT(path_link, apparmor_path_link), |
1167 | LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), | |
1168 | LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), | |
1169 | LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), | |
1170 | LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), | |
1171 | LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), | |
1172 | LSM_HOOK_INIT(path_rename, apparmor_path_rename), | |
1173 | LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), | |
1174 | LSM_HOOK_INIT(path_chown, apparmor_path_chown), | |
1175 | LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), | |
1176 | LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), | |
1177 | ||
1178 | LSM_HOOK_INIT(file_open, apparmor_file_open), | |
064dc947 | 1179 | LSM_HOOK_INIT(file_receive, apparmor_file_receive), |
e20b043a CS |
1180 | LSM_HOOK_INIT(file_permission, apparmor_file_permission), |
1181 | LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), | |
1182 | LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), | |
1183 | LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), | |
e20b043a CS |
1184 | LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), |
1185 | LSM_HOOK_INIT(file_lock, apparmor_file_lock), | |
1186 | ||
1187 | LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), | |
1188 | LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), | |
1189 | ||
695b0eb9 JJ |
1190 | LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), |
1191 | LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), | |
1192 | LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), | |
1193 | ||
fd1cafbb JJ |
1194 | LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect), |
1195 | LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send), | |
1196 | ||
695b0eb9 JJ |
1197 | LSM_HOOK_INIT(socket_create, apparmor_socket_create), |
1198 | LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), | |
1199 | LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), | |
1200 | LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), | |
1201 | LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), | |
1202 | LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), | |
1203 | LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), | |
1204 | LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), | |
1205 | LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), | |
1206 | LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), | |
1207 | LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), | |
1208 | LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), | |
1209 | LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), | |
1210 | LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), | |
1211 | LSM_HOOK_INIT(socket_getpeersec_stream, | |
1212 | apparmor_socket_getpeersec_stream), | |
1213 | LSM_HOOK_INIT(socket_getpeersec_dgram, | |
1214 | apparmor_socket_getpeersec_dgram), | |
1215 | LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), | |
1216 | ||
e20b043a CS |
1217 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), |
1218 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), | |
1219 | LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), | |
1220 | ||
1221 | LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds), | |
1222 | LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), | |
1223 | LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), | |
1224 | LSM_HOOK_INIT(bprm_secureexec, apparmor_bprm_secureexec), | |
1225 | ||
1226 | LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), | |
84632f19 | 1227 | LSM_HOOK_INIT(task_kill, apparmor_task_kill), |
b5e95b48 JJ |
1228 | }; |
1229 | ||
1230 | /* | |
1231 | * AppArmor sysfs module parameters | |
1232 | */ | |
1233 | ||
101d6c82 SR |
1234 | static int param_set_aabool(const char *val, const struct kernel_param *kp); |
1235 | static int param_get_aabool(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1236 | #define param_check_aabool param_check_bool |
9c27847d | 1237 | static const struct kernel_param_ops param_ops_aabool = { |
6a4c2643 | 1238 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1239 | .set = param_set_aabool, |
1240 | .get = param_get_aabool | |
1241 | }; | |
b5e95b48 | 1242 | |
101d6c82 SR |
1243 | static int param_set_aauint(const char *val, const struct kernel_param *kp); |
1244 | static int param_get_aauint(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1245 | #define param_check_aauint param_check_uint |
9c27847d | 1246 | static const struct kernel_param_ops param_ops_aauint = { |
101d6c82 SR |
1247 | .set = param_set_aauint, |
1248 | .get = param_get_aauint | |
1249 | }; | |
b5e95b48 | 1250 | |
101d6c82 SR |
1251 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); |
1252 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1253 | #define param_check_aalockpolicy param_check_bool |
9c27847d | 1254 | static const struct kernel_param_ops param_ops_aalockpolicy = { |
6a4c2643 | 1255 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1256 | .set = param_set_aalockpolicy, |
1257 | .get = param_get_aalockpolicy | |
1258 | }; | |
b5e95b48 JJ |
1259 | |
1260 | static int param_set_audit(const char *val, struct kernel_param *kp); | |
1261 | static int param_get_audit(char *buffer, struct kernel_param *kp); | |
b5e95b48 JJ |
1262 | |
1263 | static int param_set_mode(const char *val, struct kernel_param *kp); | |
1264 | static int param_get_mode(char *buffer, struct kernel_param *kp); | |
b5e95b48 JJ |
1265 | |
1266 | /* Flag values, also controllable via /sys/module/apparmor/parameters | |
1267 | * We define special types as we want to do additional mediation. | |
1268 | */ | |
1269 | ||
1270 | /* AppArmor global enforcement switch - complain, enforce, kill */ | |
1271 | enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; | |
1272 | module_param_call(mode, param_set_mode, param_get_mode, | |
1273 | &aa_g_profile_mode, S_IRUSR | S_IWUSR); | |
1274 | ||
6059f71f | 1275 | /* whether policy verification hashing is enabled */ |
7616ac70 | 1276 | bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); |
3ccb76c5 | 1277 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
6059f71f | 1278 | module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); |
7616ac70 | 1279 | #endif |
6059f71f | 1280 | |
b5e95b48 | 1281 | /* Debug mode */ |
eea7a05f | 1282 | bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); |
b5e95b48 JJ |
1283 | module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); |
1284 | ||
1285 | /* Audit mode */ | |
1286 | enum audit_mode aa_g_audit; | |
1287 | module_param_call(audit, param_set_audit, param_get_audit, | |
1288 | &aa_g_audit, S_IRUSR | S_IWUSR); | |
1289 | ||
1290 | /* Determines if audit header is included in audited messages. This | |
1291 | * provides more context if the audit daemon is not running | |
1292 | */ | |
90ab5ee9 | 1293 | bool aa_g_audit_header = 1; |
b5e95b48 JJ |
1294 | module_param_named(audit_header, aa_g_audit_header, aabool, |
1295 | S_IRUSR | S_IWUSR); | |
1296 | ||
1297 | /* lock out loading/removal of policy | |
1298 | * TODO: add in at boot loading of policy, which is the only way to | |
1299 | * load policy, if lock_policy is set | |
1300 | */ | |
90ab5ee9 | 1301 | bool aa_g_lock_policy; |
b5e95b48 JJ |
1302 | module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, |
1303 | S_IRUSR | S_IWUSR); | |
1304 | ||
1305 | /* Syscall logging mode */ | |
90ab5ee9 | 1306 | bool aa_g_logsyscall; |
b5e95b48 JJ |
1307 | module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); |
1308 | ||
1309 | /* Maximum pathname length before accesses will start getting rejected */ | |
1310 | unsigned int aa_g_path_max = 2 * PATH_MAX; | |
622f6e32 | 1311 | module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); |
b5e95b48 JJ |
1312 | |
1313 | /* Determines how paranoid loading of policy is and how much verification | |
1314 | * on the loaded policy is done. | |
abbf8734 JJ |
1315 | * DEPRECATED: read only as strict checking of load is always done now |
1316 | * that none root users (user namespaces) can load policy. | |
b5e95b48 | 1317 | */ |
90ab5ee9 | 1318 | bool aa_g_paranoid_load = 1; |
abbf8734 | 1319 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
b5e95b48 JJ |
1320 | |
1321 | /* Boot time disable flag */ | |
90ab5ee9 | 1322 | static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE; |
c611616c | 1323 | module_param_named(enabled, apparmor_enabled, bool, S_IRUGO); |
b5e95b48 JJ |
1324 | |
1325 | static int __init apparmor_enabled_setup(char *str) | |
1326 | { | |
1327 | unsigned long enabled; | |
29707b20 | 1328 | int error = kstrtoul(str, 0, &enabled); |
b5e95b48 JJ |
1329 | if (!error) |
1330 | apparmor_enabled = enabled ? 1 : 0; | |
1331 | return 1; | |
1332 | } | |
1333 | ||
1334 | __setup("apparmor=", apparmor_enabled_setup); | |
1335 | ||
1336 | /* set global flag turning off the ability to load policy */ | |
101d6c82 | 1337 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1338 | { |
545de8fe JJ |
1339 | if (!apparmor_enabled) |
1340 | return -EINVAL; | |
1341 | if (apparmor_initialized && !policy_admin_capable(NULL)) | |
b5e95b48 | 1342 | return -EPERM; |
b5e95b48 JJ |
1343 | return param_set_bool(val, kp); |
1344 | } | |
1345 | ||
101d6c82 | 1346 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1347 | { |
ca4bd5ae JJ |
1348 | if (!apparmor_enabled) |
1349 | return -EINVAL; | |
545de8fe JJ |
1350 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1351 | return -EPERM; | |
b5e95b48 JJ |
1352 | return param_get_bool(buffer, kp); |
1353 | } | |
1354 | ||
101d6c82 | 1355 | static int param_set_aabool(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1356 | { |
ca4bd5ae JJ |
1357 | if (!apparmor_enabled) |
1358 | return -EINVAL; | |
545de8fe JJ |
1359 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1360 | return -EPERM; | |
b5e95b48 JJ |
1361 | return param_set_bool(val, kp); |
1362 | } | |
1363 | ||
101d6c82 | 1364 | static int param_get_aabool(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1365 | { |
ca4bd5ae JJ |
1366 | if (!apparmor_enabled) |
1367 | return -EINVAL; | |
545de8fe JJ |
1368 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1369 | return -EPERM; | |
b5e95b48 JJ |
1370 | return param_get_bool(buffer, kp); |
1371 | } | |
1372 | ||
101d6c82 | 1373 | static int param_set_aauint(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1374 | { |
39d84824 JJ |
1375 | int error; |
1376 | ||
ca4bd5ae JJ |
1377 | if (!apparmor_enabled) |
1378 | return -EINVAL; | |
39d84824 JJ |
1379 | /* file is ro but enforce 2nd line check */ |
1380 | if (apparmor_initialized) | |
545de8fe | 1381 | return -EPERM; |
39d84824 JJ |
1382 | |
1383 | error = param_set_uint(val, kp); | |
1384 | pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); | |
1385 | ||
1386 | return error; | |
b5e95b48 JJ |
1387 | } |
1388 | ||
101d6c82 | 1389 | static int param_get_aauint(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1390 | { |
ca4bd5ae JJ |
1391 | if (!apparmor_enabled) |
1392 | return -EINVAL; | |
545de8fe JJ |
1393 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1394 | return -EPERM; | |
b5e95b48 JJ |
1395 | return param_get_uint(buffer, kp); |
1396 | } | |
1397 | ||
1398 | static int param_get_audit(char *buffer, struct kernel_param *kp) | |
1399 | { | |
b5e95b48 JJ |
1400 | if (!apparmor_enabled) |
1401 | return -EINVAL; | |
545de8fe JJ |
1402 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1403 | return -EPERM; | |
b5e95b48 JJ |
1404 | return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); |
1405 | } | |
1406 | ||
1407 | static int param_set_audit(const char *val, struct kernel_param *kp) | |
1408 | { | |
1409 | int i; | |
b5e95b48 JJ |
1410 | |
1411 | if (!apparmor_enabled) | |
1412 | return -EINVAL; | |
b5e95b48 JJ |
1413 | if (!val) |
1414 | return -EINVAL; | |
545de8fe JJ |
1415 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1416 | return -EPERM; | |
b5e95b48 JJ |
1417 | |
1418 | for (i = 0; i < AUDIT_MAX_INDEX; i++) { | |
1419 | if (strcmp(val, audit_mode_names[i]) == 0) { | |
1420 | aa_g_audit = i; | |
1421 | return 0; | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | return -EINVAL; | |
1426 | } | |
1427 | ||
1428 | static int param_get_mode(char *buffer, struct kernel_param *kp) | |
1429 | { | |
b5e95b48 JJ |
1430 | if (!apparmor_enabled) |
1431 | return -EINVAL; | |
545de8fe JJ |
1432 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1433 | return -EPERM; | |
b5e95b48 | 1434 | |
0d259f04 | 1435 | return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); |
b5e95b48 JJ |
1436 | } |
1437 | ||
1438 | static int param_set_mode(const char *val, struct kernel_param *kp) | |
1439 | { | |
1440 | int i; | |
b5e95b48 JJ |
1441 | |
1442 | if (!apparmor_enabled) | |
1443 | return -EINVAL; | |
b5e95b48 JJ |
1444 | if (!val) |
1445 | return -EINVAL; | |
545de8fe JJ |
1446 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1447 | return -EPERM; | |
b5e95b48 | 1448 | |
0d259f04 JJ |
1449 | for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) { |
1450 | if (strcmp(val, aa_profile_mode_names[i]) == 0) { | |
b5e95b48 JJ |
1451 | aa_g_profile_mode = i; |
1452 | return 0; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | return -EINVAL; | |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * AppArmor init functions | |
1461 | */ | |
1462 | ||
1463 | /** | |
55a26ebf | 1464 | * set_init_ctx - set a task context and profile on the first task. |
b5e95b48 JJ |
1465 | * |
1466 | * TODO: allow setting an alternate profile than unconfined | |
1467 | */ | |
55a26ebf | 1468 | static int __init set_init_ctx(void) |
b5e95b48 JJ |
1469 | { |
1470 | struct cred *cred = (struct cred *)current->real_cred; | |
55a26ebf | 1471 | struct aa_task_ctx *ctx; |
b5e95b48 | 1472 | |
98738d70 CS |
1473 | lsm_early_cred(cred); |
1474 | ctx = apparmor_cred(cred); | |
b5e95b48 | 1475 | |
637f688d | 1476 | ctx->label = aa_get_label(ns_unconfined(root_ns)); |
b5e95b48 JJ |
1477 | |
1478 | return 0; | |
1479 | } | |
1480 | ||
d4669f0b JJ |
1481 | static void destroy_buffers(void) |
1482 | { | |
1483 | u32 i, j; | |
1484 | ||
1485 | for_each_possible_cpu(i) { | |
1486 | for_each_cpu_buffer(j) { | |
1487 | kfree(per_cpu(aa_buffers, i).buf[j]); | |
1488 | per_cpu(aa_buffers, i).buf[j] = NULL; | |
1489 | } | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | static int __init alloc_buffers(void) | |
1494 | { | |
1495 | u32 i, j; | |
1496 | ||
1497 | for_each_possible_cpu(i) { | |
1498 | for_each_cpu_buffer(j) { | |
1499 | char *buffer; | |
1500 | ||
1501 | if (cpu_to_node(i) > num_online_nodes()) | |
1502 | /* fallback to kmalloc for offline nodes */ | |
1503 | buffer = kmalloc(aa_g_path_max, GFP_KERNEL); | |
1504 | else | |
1505 | buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL, | |
1506 | cpu_to_node(i)); | |
1507 | if (!buffer) { | |
1508 | destroy_buffers(); | |
1509 | return -ENOMEM; | |
1510 | } | |
1511 | per_cpu(aa_buffers, i).buf[j] = buffer; | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | return 0; | |
1516 | } | |
1517 | ||
e3ea1ca5 TH |
1518 | #ifdef CONFIG_SYSCTL |
1519 | static int apparmor_dointvec(struct ctl_table *table, int write, | |
1520 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
1521 | { | |
1522 | if (!policy_admin_capable(NULL)) | |
1523 | return -EPERM; | |
1524 | if (!apparmor_enabled) | |
1525 | return -EINVAL; | |
1526 | ||
1527 | return proc_dointvec(table, write, buffer, lenp, ppos); | |
1528 | } | |
1529 | ||
1530 | static struct ctl_path apparmor_sysctl_path[] = { | |
1531 | { .procname = "kernel", }, | |
1532 | { } | |
1533 | }; | |
1534 | ||
1535 | static struct ctl_table apparmor_sysctl_table[] = { | |
1536 | { | |
1537 | .procname = "unprivileged_userns_apparmor_policy", | |
1538 | .data = &unprivileged_userns_apparmor_policy, | |
1539 | .maxlen = sizeof(int), | |
1540 | .mode = 0600, | |
1541 | .proc_handler = apparmor_dointvec, | |
1542 | }, | |
1543 | { } | |
1544 | }; | |
1545 | ||
1546 | static int __init apparmor_init_sysctl(void) | |
1547 | { | |
1548 | return register_sysctl_paths(apparmor_sysctl_path, | |
1549 | apparmor_sysctl_table) ? 0 : -ENOMEM; | |
1550 | } | |
1551 | #else | |
1552 | static inline int apparmor_init_sysctl(void) | |
1553 | { | |
1554 | return 0; | |
1555 | } | |
1556 | #endif /* CONFIG_SYSCTL */ | |
1557 | ||
b5e95b48 JJ |
1558 | static int __init apparmor_init(void) |
1559 | { | |
98738d70 | 1560 | static int finish; |
b5e95b48 JJ |
1561 | int error; |
1562 | ||
98738d70 | 1563 | if (!finish) { |
a3c3cde2 CS |
1564 | if (apparmor_enabled && |
1565 | security_module_enable("apparmor", | |
1566 | IS_ENABLED(CONFIG_SECURITY_APPARMOR_STACKED))) | |
98738d70 CS |
1567 | security_add_blobs(&apparmor_blob_sizes); |
1568 | finish = 1; | |
1569 | return 0; | |
1570 | } | |
1571 | ||
dea1e5a4 | 1572 | if (!apparmor_enabled) { |
b5e95b48 JJ |
1573 | aa_info_message("AppArmor disabled by boot time parameter"); |
1574 | apparmor_enabled = 0; | |
1575 | return 0; | |
1576 | } | |
1577 | ||
11c236b8 JJ |
1578 | error = aa_setup_dfa_engine(); |
1579 | if (error) { | |
1580 | AA_ERROR("Unable to setup dfa engine\n"); | |
1581 | goto alloc_out; | |
1582 | } | |
1583 | ||
b5e95b48 JJ |
1584 | error = aa_alloc_root_ns(); |
1585 | if (error) { | |
1586 | AA_ERROR("Unable to allocate default profile namespace\n"); | |
1587 | goto alloc_out; | |
1588 | } | |
1589 | ||
e3ea1ca5 TH |
1590 | error = apparmor_init_sysctl(); |
1591 | if (error) { | |
1592 | AA_ERROR("Unable to register sysctls\n"); | |
1593 | goto alloc_out; | |
1594 | ||
1595 | } | |
1596 | ||
d4669f0b JJ |
1597 | error = alloc_buffers(); |
1598 | if (error) { | |
1599 | AA_ERROR("Unable to allocate work buffers\n"); | |
1600 | goto buffers_out; | |
1601 | } | |
1602 | ||
55a26ebf | 1603 | error = set_init_ctx(); |
b5e95b48 JJ |
1604 | if (error) { |
1605 | AA_ERROR("Failed to set context on init task\n"); | |
b1d9e6b0 | 1606 | aa_free_root_ns(); |
d4669f0b | 1607 | goto buffers_out; |
b5e95b48 | 1608 | } |
d69dece5 CS |
1609 | security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), |
1610 | "apparmor"); | |
b5e95b48 JJ |
1611 | |
1612 | /* Report that AppArmor successfully initialized */ | |
1613 | apparmor_initialized = 1; | |
1614 | if (aa_g_profile_mode == APPARMOR_COMPLAIN) | |
1615 | aa_info_message("AppArmor initialized: complain mode enabled"); | |
1616 | else if (aa_g_profile_mode == APPARMOR_KILL) | |
1617 | aa_info_message("AppArmor initialized: kill mode enabled"); | |
1618 | else | |
1619 | aa_info_message("AppArmor initialized"); | |
1620 | ||
1621 | return error; | |
1622 | ||
d4669f0b JJ |
1623 | buffers_out: |
1624 | destroy_buffers(); | |
1625 | ||
b5e95b48 JJ |
1626 | alloc_out: |
1627 | aa_destroy_aafs(); | |
11c236b8 | 1628 | aa_teardown_dfa_engine(); |
b5e95b48 JJ |
1629 | |
1630 | apparmor_enabled = 0; | |
1631 | return error; | |
b5e95b48 JJ |
1632 | } |
1633 | ||
1634 | security_initcall(apparmor_init); |