]>
Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b5e95b48 JJ |
2 | /* |
3 | * AppArmor security module | |
4 | * | |
5 | * This file contains AppArmor LSM hooks. | |
6 | * | |
7 | * Copyright (C) 1998-2008 Novell/SUSE | |
8 | * Copyright 2009-2010 Canonical Ltd. | |
b5e95b48 JJ |
9 | */ |
10 | ||
3c4ed7bd | 11 | #include <linux/lsm_hooks.h> |
b5e95b48 JJ |
12 | #include <linux/moduleparam.h> |
13 | #include <linux/mm.h> | |
14 | #include <linux/mman.h> | |
15 | #include <linux/mount.h> | |
16 | #include <linux/namei.h> | |
17 | #include <linux/ptrace.h> | |
18 | #include <linux/ctype.h> | |
19 | #include <linux/sysctl.h> | |
20 | #include <linux/audit.h> | |
3486740a | 21 | #include <linux/user_namespace.h> |
ab9f2115 MG |
22 | #include <linux/netfilter_ipv4.h> |
23 | #include <linux/netfilter_ipv6.h> | |
63c16c3a | 24 | #include <linux/zlib.h> |
b5e95b48 | 25 | #include <net/sock.h> |
e262e32d | 26 | #include <uapi/linux/mount.h> |
b5e95b48 | 27 | |
2775e078 | 28 | #include "include/af_unix.h" |
b5e95b48 JJ |
29 | #include "include/apparmor.h" |
30 | #include "include/apparmorfs.h" | |
31 | #include "include/audit.h" | |
32 | #include "include/capability.h" | |
d8889d49 | 33 | #include "include/cred.h" |
b5e95b48 JJ |
34 | #include "include/file.h" |
35 | #include "include/ipc.h" | |
56974a6f | 36 | #include "include/net.h" |
b5e95b48 | 37 | #include "include/path.h" |
637f688d | 38 | #include "include/label.h" |
b5e95b48 | 39 | #include "include/policy.h" |
cff281f6 | 40 | #include "include/policy_ns.h" |
b5e95b48 | 41 | #include "include/procattr.h" |
2ea3ffb7 | 42 | #include "include/mount.h" |
c0929212 | 43 | #include "include/secid.h" |
b5e95b48 JJ |
44 | |
45 | /* Flag indicating whether initialization completed */ | |
545de8fe | 46 | int apparmor_initialized; |
b5e95b48 | 47 | |
df323337 SAS |
48 | union aa_buffer { |
49 | struct list_head list; | |
50 | char buffer[1]; | |
51 | }; | |
d4669f0b | 52 | |
341c1fda JJ |
53 | #define RESERVE_COUNT 2 |
54 | static int reserve_count = RESERVE_COUNT; | |
55 | static int buffer_count; | |
d4669f0b | 56 | |
df323337 SAS |
57 | static LIST_HEAD(aa_global_buffers); |
58 | static DEFINE_SPINLOCK(aa_buffers_lock); | |
d4669f0b | 59 | |
b5e95b48 JJ |
60 | /* |
61 | * LSM hook functions | |
62 | */ | |
63 | ||
64 | /* | |
d9087c49 | 65 | * put the associated labels |
b5e95b48 JJ |
66 | */ |
67 | static void apparmor_cred_free(struct cred *cred) | |
68 | { | |
d9087c49 | 69 | aa_put_label(cred_label(cred)); |
69b5a44a | 70 | set_cred_label(cred, NULL); |
b5e95b48 JJ |
71 | } |
72 | ||
73 | /* | |
74 | * allocate the apparmor part of blank credentials | |
75 | */ | |
76 | static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) | |
77 | { | |
69b5a44a | 78 | set_cred_label(cred, NULL); |
b5e95b48 JJ |
79 | return 0; |
80 | } | |
81 | ||
82 | /* | |
d9087c49 | 83 | * prepare new cred label for modification by prepare_cred block |
b5e95b48 JJ |
84 | */ |
85 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, | |
86 | gfp_t gfp) | |
87 | { | |
69b5a44a | 88 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
b5e95b48 JJ |
89 | return 0; |
90 | } | |
91 | ||
92 | /* | |
93 | * transfer the apparmor data to a blank set of creds | |
94 | */ | |
95 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) | |
96 | { | |
69b5a44a | 97 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
b5e95b48 JJ |
98 | } |
99 | ||
3b529a76 JJ |
100 | static void apparmor_task_free(struct task_struct *task) |
101 | { | |
102 | ||
103 | aa_free_task_ctx(task_ctx(task)); | |
3b529a76 JJ |
104 | } |
105 | ||
106 | static int apparmor_task_alloc(struct task_struct *task, | |
107 | unsigned long clone_flags) | |
108 | { | |
f4ad8f2c | 109 | struct aa_task_ctx *new = task_ctx(task); |
3b529a76 | 110 | |
de62de59 | 111 | aa_dup_task_ctx(new, task_ctx(current)); |
b5e95b48 | 112 | |
3b529a76 | 113 | return 0; |
b5e95b48 JJ |
114 | } |
115 | ||
116 | static int apparmor_ptrace_access_check(struct task_struct *child, | |
117 | unsigned int mode) | |
118 | { | |
b2d09ae4 JJ |
119 | struct aa_label *tracer, *tracee; |
120 | int error; | |
121 | ||
1f8266ff | 122 | tracer = __begin_current_label_crit_section(); |
b2d09ae4 JJ |
123 | tracee = aa_get_task_label(child); |
124 | error = aa_may_ptrace(tracer, tracee, | |
338d0be4 JJ |
125 | (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ |
126 | : AA_PTRACE_TRACE); | |
b2d09ae4 | 127 | aa_put_label(tracee); |
1f8266ff | 128 | __end_current_label_crit_section(tracer); |
b2d09ae4 JJ |
129 | |
130 | return error; | |
b5e95b48 JJ |
131 | } |
132 | ||
133 | static int apparmor_ptrace_traceme(struct task_struct *parent) | |
134 | { | |
b2d09ae4 JJ |
135 | struct aa_label *tracer, *tracee; |
136 | int error; | |
137 | ||
ca3fde52 | 138 | tracee = __begin_current_label_crit_section(); |
b2d09ae4 JJ |
139 | tracer = aa_get_task_label(parent); |
140 | error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE); | |
141 | aa_put_label(tracer); | |
ca3fde52 | 142 | __end_current_label_crit_section(tracee); |
b2d09ae4 JJ |
143 | |
144 | return error; | |
b5e95b48 JJ |
145 | } |
146 | ||
147 | /* Derived from security/commoncap.c:cap_capget */ | |
148 | static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, | |
149 | kernel_cap_t *inheritable, kernel_cap_t *permitted) | |
150 | { | |
637f688d | 151 | struct aa_label *label; |
b5e95b48 JJ |
152 | const struct cred *cred; |
153 | ||
154 | rcu_read_lock(); | |
155 | cred = __task_cred(target); | |
637f688d | 156 | label = aa_get_newest_cred_label(cred); |
c70c86c4 | 157 | |
b1d9e6b0 CS |
158 | /* |
159 | * cap_capget is stacked ahead of this and will | |
160 | * initialize effective and permitted. | |
161 | */ | |
c70c86c4 JJ |
162 | if (!unconfined(label)) { |
163 | struct aa_profile *profile; | |
164 | struct label_it i; | |
165 | ||
166 | label_for_each_confined(i, label, profile) { | |
167 | if (COMPLAIN_MODE(profile)) | |
168 | continue; | |
169 | *effective = cap_intersect(*effective, | |
170 | profile->caps.allow); | |
171 | *permitted = cap_intersect(*permitted, | |
172 | profile->caps.allow); | |
173 | } | |
b5e95b48 JJ |
174 | } |
175 | rcu_read_unlock(); | |
637f688d | 176 | aa_put_label(label); |
b5e95b48 JJ |
177 | |
178 | return 0; | |
179 | } | |
180 | ||
6a9de491 | 181 | static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, |
c1a85a00 | 182 | int cap, unsigned int opts) |
b5e95b48 | 183 | { |
637f688d | 184 | struct aa_label *label; |
b1d9e6b0 CS |
185 | int error = 0; |
186 | ||
637f688d JJ |
187 | label = aa_get_newest_cred_label(cred); |
188 | if (!unconfined(label)) | |
c1a85a00 | 189 | error = aa_capable(label, cap, opts); |
637f688d | 190 | aa_put_label(label); |
cf797c0e | 191 | |
b5e95b48 JJ |
192 | return error; |
193 | } | |
194 | ||
195 | /** | |
196 | * common_perm - basic common permission check wrapper fn for paths | |
197 | * @op: operation being checked | |
198 | * @path: path to check permission of (NOT NULL) | |
199 | * @mask: requested permissions mask | |
200 | * @cond: conditional info for the permission request (NOT NULL) | |
201 | * | |
202 | * Returns: %0 else error code if error or permission denied | |
203 | */ | |
47f6e5cc | 204 | static int common_perm(const char *op, const struct path *path, u32 mask, |
b5e95b48 JJ |
205 | struct path_cond *cond) |
206 | { | |
637f688d | 207 | struct aa_label *label; |
b5e95b48 JJ |
208 | int error = 0; |
209 | ||
637f688d JJ |
210 | label = __begin_current_label_crit_section(); |
211 | if (!unconfined(label)) | |
aebd873e | 212 | error = aa_path_perm(op, label, path, 0, mask, cond); |
637f688d | 213 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
214 | |
215 | return error; | |
216 | } | |
217 | ||
218 | /** | |
31f75bfe | 219 | * common_perm_cond - common permission wrapper around inode cond |
b5e95b48 | 220 | * @op: operation being checked |
31f75bfe | 221 | * @path: location to check (NOT NULL) |
b5e95b48 | 222 | * @mask: requested permissions mask |
b5e95b48 JJ |
223 | * |
224 | * Returns: %0 else error code if error or permission denied | |
225 | */ | |
31f75bfe | 226 | static int common_perm_cond(const char *op, const struct path *path, u32 mask) |
b5e95b48 | 227 | { |
31f75bfe JJ |
228 | struct path_cond cond = { d_backing_inode(path->dentry)->i_uid, |
229 | d_backing_inode(path->dentry)->i_mode | |
230 | }; | |
b5e95b48 | 231 | |
31f75bfe JJ |
232 | if (!path_mediated_fs(path->dentry)) |
233 | return 0; | |
234 | ||
235 | return common_perm(op, path, mask, &cond); | |
b5e95b48 JJ |
236 | } |
237 | ||
238 | /** | |
31f75bfe | 239 | * common_perm_dir_dentry - common permission wrapper when path is dir, dentry |
b5e95b48 | 240 | * @op: operation being checked |
31f75bfe JJ |
241 | * @dir: directory of the dentry (NOT NULL) |
242 | * @dentry: dentry to check (NOT NULL) | |
b5e95b48 | 243 | * @mask: requested permissions mask |
31f75bfe | 244 | * @cond: conditional info for the permission request (NOT NULL) |
b5e95b48 JJ |
245 | * |
246 | * Returns: %0 else error code if error or permission denied | |
247 | */ | |
31f75bfe JJ |
248 | static int common_perm_dir_dentry(const char *op, const struct path *dir, |
249 | struct dentry *dentry, u32 mask, | |
250 | struct path_cond *cond) | |
b5e95b48 | 251 | { |
31f75bfe | 252 | struct path path = { .mnt = dir->mnt, .dentry = dentry }; |
b5e95b48 | 253 | |
31f75bfe | 254 | return common_perm(op, &path, mask, cond); |
b5e95b48 JJ |
255 | } |
256 | ||
257 | /** | |
258 | * common_perm_rm - common permission wrapper for operations doing rm | |
259 | * @op: operation being checked | |
260 | * @dir: directory that the dentry is in (NOT NULL) | |
261 | * @dentry: dentry being rm'd (NOT NULL) | |
262 | * @mask: requested permission mask | |
263 | * | |
264 | * Returns: %0 else error code if error or permission denied | |
265 | */ | |
47f6e5cc | 266 | static int common_perm_rm(const char *op, const struct path *dir, |
b5e95b48 JJ |
267 | struct dentry *dentry, u32 mask) |
268 | { | |
c6f493d6 | 269 | struct inode *inode = d_backing_inode(dentry); |
b5e95b48 JJ |
270 | struct path_cond cond = { }; |
271 | ||
efeee83a | 272 | if (!inode || !path_mediated_fs(dentry)) |
b5e95b48 JJ |
273 | return 0; |
274 | ||
275 | cond.uid = inode->i_uid; | |
276 | cond.mode = inode->i_mode; | |
277 | ||
278 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
279 | } | |
280 | ||
281 | /** | |
282 | * common_perm_create - common permission wrapper for operations doing create | |
283 | * @op: operation being checked | |
284 | * @dir: directory that dentry will be created in (NOT NULL) | |
285 | * @dentry: dentry to create (NOT NULL) | |
286 | * @mask: request permission mask | |
287 | * @mode: created file mode | |
288 | * | |
289 | * Returns: %0 else error code if error or permission denied | |
290 | */ | |
47f6e5cc | 291 | static int common_perm_create(const char *op, const struct path *dir, |
d6b49f7a | 292 | struct dentry *dentry, u32 mask, umode_t mode) |
b5e95b48 JJ |
293 | { |
294 | struct path_cond cond = { current_fsuid(), mode }; | |
295 | ||
efeee83a | 296 | if (!path_mediated_fs(dir->dentry)) |
b5e95b48 JJ |
297 | return 0; |
298 | ||
299 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); | |
300 | } | |
301 | ||
989f74e0 | 302 | static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
303 | { |
304 | return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); | |
305 | } | |
306 | ||
d3607752 | 307 | static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, |
4572befe | 308 | umode_t mode) |
b5e95b48 JJ |
309 | { |
310 | return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, | |
311 | S_IFDIR); | |
312 | } | |
313 | ||
989f74e0 | 314 | static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) |
b5e95b48 JJ |
315 | { |
316 | return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); | |
317 | } | |
318 | ||
d3607752 | 319 | static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, |
04fc66e7 | 320 | umode_t mode, unsigned int dev) |
b5e95b48 JJ |
321 | { |
322 | return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); | |
323 | } | |
324 | ||
81f4c506 | 325 | static int apparmor_path_truncate(const struct path *path) |
b5e95b48 | 326 | { |
e53cfe6c | 327 | return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); |
b5e95b48 JJ |
328 | } |
329 | ||
d3607752 | 330 | static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, |
b5e95b48 JJ |
331 | const char *old_name) |
332 | { | |
333 | return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, | |
334 | S_IFLNK); | |
335 | } | |
336 | ||
3ccee46a | 337 | static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, |
b5e95b48 JJ |
338 | struct dentry *new_dentry) |
339 | { | |
637f688d | 340 | struct aa_label *label; |
b5e95b48 JJ |
341 | int error = 0; |
342 | ||
efeee83a | 343 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 JJ |
344 | return 0; |
345 | ||
637f688d JJ |
346 | label = begin_current_label_crit_section(); |
347 | if (!unconfined(label)) | |
8014370f | 348 | error = aa_path_link(label, old_dentry, new_dir, new_dentry); |
637f688d | 349 | end_current_label_crit_section(label); |
cf797c0e | 350 | |
b5e95b48 JJ |
351 | return error; |
352 | } | |
353 | ||
3ccee46a AV |
354 | static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, |
355 | const struct path *new_dir, struct dentry *new_dentry) | |
b5e95b48 | 356 | { |
637f688d | 357 | struct aa_label *label; |
b5e95b48 JJ |
358 | int error = 0; |
359 | ||
efeee83a | 360 | if (!path_mediated_fs(old_dentry)) |
b5e95b48 JJ |
361 | return 0; |
362 | ||
637f688d JJ |
363 | label = begin_current_label_crit_section(); |
364 | if (!unconfined(label)) { | |
8486adf0 KC |
365 | struct path old_path = { .mnt = old_dir->mnt, |
366 | .dentry = old_dentry }; | |
367 | struct path new_path = { .mnt = new_dir->mnt, | |
368 | .dentry = new_dentry }; | |
c6f493d6 DH |
369 | struct path_cond cond = { d_backing_inode(old_dentry)->i_uid, |
370 | d_backing_inode(old_dentry)->i_mode | |
b5e95b48 JJ |
371 | }; |
372 | ||
aebd873e | 373 | error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, |
e53cfe6c JJ |
374 | MAY_READ | AA_MAY_GETATTR | MAY_WRITE | |
375 | AA_MAY_SETATTR | AA_MAY_DELETE, | |
b5e95b48 JJ |
376 | &cond); |
377 | if (!error) | |
aebd873e | 378 | error = aa_path_perm(OP_RENAME_DEST, label, &new_path, |
e53cfe6c | 379 | 0, MAY_WRITE | AA_MAY_SETATTR | |
b5e95b48 JJ |
380 | AA_MAY_CREATE, &cond); |
381 | ||
382 | } | |
637f688d | 383 | end_current_label_crit_section(label); |
cf797c0e | 384 | |
b5e95b48 JJ |
385 | return error; |
386 | } | |
387 | ||
be01f9f2 | 388 | static int apparmor_path_chmod(const struct path *path, umode_t mode) |
b5e95b48 | 389 | { |
31f75bfe | 390 | return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); |
b5e95b48 JJ |
391 | } |
392 | ||
7fd25dac | 393 | static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
b5e95b48 | 394 | { |
31f75bfe | 395 | return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); |
b5e95b48 JJ |
396 | } |
397 | ||
3f7036a0 | 398 | static int apparmor_inode_getattr(const struct path *path) |
b5e95b48 | 399 | { |
e53cfe6c | 400 | return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); |
b5e95b48 JJ |
401 | } |
402 | ||
94817692 | 403 | static int apparmor_file_open(struct file *file) |
b5e95b48 | 404 | { |
637f688d JJ |
405 | struct aa_file_ctx *fctx = file_ctx(file); |
406 | struct aa_label *label; | |
b5e95b48 JJ |
407 | int error = 0; |
408 | ||
efeee83a | 409 | if (!path_mediated_fs(file->f_path.dentry)) |
b5e95b48 JJ |
410 | return 0; |
411 | ||
412 | /* If in exec, permission is handled by bprm hooks. | |
413 | * Cache permissions granted by the previous exec check, with | |
414 | * implicit read and executable mmap which are required to | |
415 | * actually execute the image. | |
416 | */ | |
417 | if (current->in_execve) { | |
55a26ebf | 418 | fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; |
b5e95b48 JJ |
419 | return 0; |
420 | } | |
421 | ||
94817692 | 422 | label = aa_get_newest_cred_label(file->f_cred); |
637f688d | 423 | if (!unconfined(label)) { |
496ad9aa | 424 | struct inode *inode = file_inode(file); |
b5e95b48 JJ |
425 | struct path_cond cond = { inode->i_uid, inode->i_mode }; |
426 | ||
aebd873e | 427 | error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, |
b5e95b48 JJ |
428 | aa_map_file_to_perms(file), &cond); |
429 | /* todo cache full allowed permissions set and state */ | |
55a26ebf | 430 | fctx->allow = aa_map_file_to_perms(file); |
b5e95b48 | 431 | } |
637f688d | 432 | aa_put_label(label); |
b5e95b48 JJ |
433 | |
434 | return error; | |
435 | } | |
436 | ||
437 | static int apparmor_file_alloc_security(struct file *file) | |
438 | { | |
33bf60ca | 439 | struct aa_file_ctx *ctx = file_ctx(file); |
637f688d | 440 | struct aa_label *label = begin_current_label_crit_section(); |
b5e95b48 | 441 | |
33bf60ca CS |
442 | spin_lock_init(&ctx->lock); |
443 | rcu_assign_pointer(ctx->label, aa_get_label(label)); | |
444 | end_current_label_crit_section(label); | |
445 | return 0; | |
b5e95b48 JJ |
446 | } |
447 | ||
448 | static void apparmor_file_free_security(struct file *file) | |
449 | { | |
33bf60ca CS |
450 | struct aa_file_ctx *ctx = file_ctx(file); |
451 | ||
452 | if (ctx) | |
453 | aa_put_label(rcu_access_pointer(ctx->label)); | |
b5e95b48 JJ |
454 | } |
455 | ||
341c1fda JJ |
456 | static int common_file_perm(const char *op, struct file *file, u32 mask, |
457 | bool in_atomic) | |
b5e95b48 | 458 | { |
190a9518 | 459 | struct aa_label *label; |
b5e95b48 JJ |
460 | int error = 0; |
461 | ||
192ca6b5 JJ |
462 | /* don't reaudit files closed during inheritance */ |
463 | if (file->f_path.dentry == aa_null.dentry) | |
464 | return -EACCES; | |
465 | ||
637f688d | 466 | label = __begin_current_label_crit_section(); |
341c1fda | 467 | error = aa_file_perm(op, label, file, mask, in_atomic); |
637f688d | 468 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
469 | |
470 | return error; | |
471 | } | |
472 | ||
064dc947 JJ |
473 | static int apparmor_file_receive(struct file *file) |
474 | { | |
341c1fda JJ |
475 | return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), |
476 | false); | |
064dc947 JJ |
477 | } |
478 | ||
b5e95b48 JJ |
479 | static int apparmor_file_permission(struct file *file, int mask) |
480 | { | |
341c1fda | 481 | return common_file_perm(OP_FPERM, file, mask, false); |
b5e95b48 JJ |
482 | } |
483 | ||
484 | static int apparmor_file_lock(struct file *file, unsigned int cmd) | |
485 | { | |
486 | u32 mask = AA_MAY_LOCK; | |
487 | ||
488 | if (cmd == F_WRLCK) | |
489 | mask |= MAY_WRITE; | |
490 | ||
341c1fda | 491 | return common_file_perm(OP_FLOCK, file, mask, false); |
b5e95b48 JJ |
492 | } |
493 | ||
47f6e5cc | 494 | static int common_mmap(const char *op, struct file *file, unsigned long prot, |
341c1fda | 495 | unsigned long flags, bool in_atomic) |
b5e95b48 | 496 | { |
b5e95b48 JJ |
497 | int mask = 0; |
498 | ||
637f688d | 499 | if (!file || !file_ctx(file)) |
b5e95b48 JJ |
500 | return 0; |
501 | ||
502 | if (prot & PROT_READ) | |
503 | mask |= MAY_READ; | |
504 | /* | |
505 | * Private mappings don't require write perms since they don't | |
506 | * write back to the files | |
507 | */ | |
508 | if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) | |
509 | mask |= MAY_WRITE; | |
510 | if (prot & PROT_EXEC) | |
511 | mask |= AA_EXEC_MMAP; | |
512 | ||
341c1fda | 513 | return common_file_perm(op, file, mask, in_atomic); |
b5e95b48 JJ |
514 | } |
515 | ||
e5467859 AV |
516 | static int apparmor_mmap_file(struct file *file, unsigned long reqprot, |
517 | unsigned long prot, unsigned long flags) | |
b5e95b48 | 518 | { |
341c1fda | 519 | return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); |
b5e95b48 JJ |
520 | } |
521 | ||
522 | static int apparmor_file_mprotect(struct vm_area_struct *vma, | |
523 | unsigned long reqprot, unsigned long prot) | |
524 | { | |
525 | return common_mmap(OP_FMPROT, vma->vm_file, prot, | |
341c1fda JJ |
526 | !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, |
527 | false); | |
b5e95b48 JJ |
528 | } |
529 | ||
2ea3ffb7 JJ |
530 | static int apparmor_sb_mount(const char *dev_name, const struct path *path, |
531 | const char *type, unsigned long flags, void *data) | |
532 | { | |
533 | struct aa_label *label; | |
534 | int error = 0; | |
535 | ||
536 | /* Discard magic */ | |
537 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) | |
538 | flags &= ~MS_MGC_MSK; | |
539 | ||
540 | flags &= ~AA_MS_IGNORE_MASK; | |
541 | ||
542 | label = __begin_current_label_crit_section(); | |
543 | if (!unconfined(label)) { | |
544 | if (flags & MS_REMOUNT) | |
545 | error = aa_remount(label, path, flags, data); | |
546 | else if (flags & MS_BIND) | |
547 | error = aa_bind_mount(label, path, dev_name, flags); | |
548 | else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | | |
549 | MS_UNBINDABLE)) | |
550 | error = aa_mount_change_type(label, path, flags); | |
551 | else if (flags & MS_MOVE) | |
552 | error = aa_move_mount(label, path, dev_name); | |
553 | else | |
554 | error = aa_new_mount(label, dev_name, path, type, | |
555 | flags, data); | |
556 | } | |
557 | __end_current_label_crit_section(label); | |
558 | ||
559 | return error; | |
560 | } | |
561 | ||
562 | static int apparmor_sb_umount(struct vfsmount *mnt, int flags) | |
563 | { | |
564 | struct aa_label *label; | |
565 | int error = 0; | |
566 | ||
567 | label = __begin_current_label_crit_section(); | |
568 | if (!unconfined(label)) | |
569 | error = aa_umount(label, mnt, flags); | |
570 | __end_current_label_crit_section(label); | |
571 | ||
572 | return error; | |
573 | } | |
574 | ||
575 | static int apparmor_sb_pivotroot(const struct path *old_path, | |
576 | const struct path *new_path) | |
577 | { | |
578 | struct aa_label *label; | |
579 | int error = 0; | |
580 | ||
581 | label = aa_get_current_label(); | |
582 | if (!unconfined(label)) | |
583 | error = aa_pivotroot(label, old_path, new_path); | |
584 | aa_put_label(label); | |
585 | ||
586 | return error; | |
587 | } | |
588 | ||
b5e95b48 JJ |
589 | static int apparmor_getprocattr(struct task_struct *task, char *name, |
590 | char **value) | |
591 | { | |
592 | int error = -ENOENT; | |
b5e95b48 JJ |
593 | /* released below */ |
594 | const struct cred *cred = get_task_cred(task); | |
de62de59 | 595 | struct aa_task_ctx *ctx = task_ctx(current); |
637f688d | 596 | struct aa_label *label = NULL; |
2925d862 | 597 | bool newline = true; |
b5e95b48 JJ |
598 | |
599 | if (strcmp(name, "current") == 0) | |
d9087c49 | 600 | label = aa_get_newest_label(cred_label(cred)); |
55a26ebf | 601 | else if (strcmp(name, "prev") == 0 && ctx->previous) |
637f688d | 602 | label = aa_get_newest_label(ctx->previous); |
55a26ebf | 603 | else if (strcmp(name, "exec") == 0 && ctx->onexec) |
637f688d | 604 | label = aa_get_newest_label(ctx->onexec); |
2925d862 CS |
605 | else if (strcmp(name, "context") == 0) { |
606 | label = aa_get_newest_label(cred_label(cred)); | |
607 | newline = false; | |
608 | } else | |
b5e95b48 JJ |
609 | error = -EINVAL; |
610 | ||
637f688d | 611 | if (label) |
2925d862 | 612 | error = aa_getprocattr(label, value, newline); |
77b071b3 | 613 | |
637f688d | 614 | aa_put_label(label); |
b5e95b48 JJ |
615 | put_cred(cred); |
616 | ||
617 | return error; | |
618 | } | |
619 | ||
3ddd2931 CS |
620 | |
621 | static int profile_display_lsm(struct aa_profile *profile, | |
622 | struct common_audit_data *sa) | |
623 | { | |
624 | struct aa_perms perms = { }; | |
625 | unsigned int state; | |
626 | ||
627 | state = PROFILE_MEDIATES(profile, AA_CLASS_DISPLAY_LSM); | |
628 | if (state) { | |
629 | aa_compute_perms(profile->policy.dfa, state, &perms); | |
630 | aa_apply_modes_to_perms(profile, &perms); | |
631 | aad(sa)->label = &profile->label; | |
632 | ||
633 | return aa_check_perms(profile, &perms, AA_MAY_WRITE, sa, NULL); | |
634 | } | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
b21507e2 SS |
639 | static int apparmor_setprocattr(const char *name, void *value, |
640 | size_t size) | |
b5e95b48 | 641 | { |
e89b8081 | 642 | char *command, *largs = NULL, *args = value; |
b5e95b48 JJ |
643 | size_t arg_size; |
644 | int error; | |
ef88a7ac | 645 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR); |
b5e95b48 JJ |
646 | |
647 | if (size == 0) | |
648 | return -EINVAL; | |
b5e95b48 | 649 | |
3ddd2931 CS |
650 | /* LSM infrastructure does actual setting of display if allowed */ |
651 | if (!strcmp(name, "display")) { | |
652 | struct aa_profile *profile; | |
653 | struct aa_label *label; | |
654 | ||
655 | aad(&sa)->info = "set display lsm"; | |
656 | label = begin_current_label_crit_section(); | |
657 | error = fn_for_each_confined(label, profile, | |
658 | profile_display_lsm(profile, &sa)); | |
659 | end_current_label_crit_section(label); | |
660 | return error; | |
661 | } | |
662 | ||
e89b8081 VN |
663 | /* AppArmor requires that the buffer must be null terminated atm */ |
664 | if (args[size - 1] != '\0') { | |
665 | /* null terminate */ | |
666 | largs = args = kmalloc(size + 1, GFP_KERNEL); | |
667 | if (!args) | |
668 | return -ENOMEM; | |
669 | memcpy(args, value, size); | |
670 | args[size] = '\0'; | |
671 | } | |
672 | ||
673 | error = -EINVAL; | |
b5e95b48 JJ |
674 | args = strim(args); |
675 | command = strsep(&args, " "); | |
676 | if (!args) | |
e89b8081 | 677 | goto out; |
b5e95b48 JJ |
678 | args = skip_spaces(args); |
679 | if (!*args) | |
e89b8081 | 680 | goto out; |
b5e95b48 | 681 | |
d4d03f74 | 682 | arg_size = size - (args - (largs ? largs : (char *) value)); |
b5e95b48 JJ |
683 | if (strcmp(name, "current") == 0) { |
684 | if (strcmp(command, "changehat") == 0) { | |
685 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 686 | AA_CHANGE_NOFLAGS); |
b5e95b48 JJ |
687 | } else if (strcmp(command, "permhat") == 0) { |
688 | error = aa_setprocattr_changehat(args, arg_size, | |
df8073c6 | 689 | AA_CHANGE_TEST); |
b5e95b48 | 690 | } else if (strcmp(command, "changeprofile") == 0) { |
df8073c6 | 691 | error = aa_change_profile(args, AA_CHANGE_NOFLAGS); |
b5e95b48 | 692 | } else if (strcmp(command, "permprofile") == 0) { |
df8073c6 | 693 | error = aa_change_profile(args, AA_CHANGE_TEST); |
6c5fc8f1 JJ |
694 | } else if (strcmp(command, "stack") == 0) { |
695 | error = aa_change_profile(args, AA_CHANGE_STACK); | |
3eea57c2 JJ |
696 | } else |
697 | goto fail; | |
b5e95b48 | 698 | } else if (strcmp(name, "exec") == 0) { |
3eea57c2 | 699 | if (strcmp(command, "exec") == 0) |
df8073c6 | 700 | error = aa_change_profile(args, AA_CHANGE_ONEXEC); |
6c5fc8f1 JJ |
701 | else if (strcmp(command, "stack") == 0) |
702 | error = aa_change_profile(args, (AA_CHANGE_ONEXEC | | |
703 | AA_CHANGE_STACK)); | |
3eea57c2 JJ |
704 | else |
705 | goto fail; | |
706 | } else | |
b5e95b48 | 707 | /* only support the "current" and "exec" process attributes */ |
e89b8081 | 708 | goto fail; |
3eea57c2 | 709 | |
b5e95b48 JJ |
710 | if (!error) |
711 | error = size; | |
e89b8081 VN |
712 | out: |
713 | kfree(largs); | |
b5e95b48 | 714 | return error; |
3eea57c2 JJ |
715 | |
716 | fail: | |
637f688d | 717 | aad(&sa)->label = begin_current_label_crit_section(); |
ef88a7ac JJ |
718 | aad(&sa)->info = name; |
719 | aad(&sa)->error = error = -EINVAL; | |
3eea57c2 | 720 | aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); |
637f688d | 721 | end_current_label_crit_section(aad(&sa)->label); |
e89b8081 | 722 | goto out; |
b5e95b48 JJ |
723 | } |
724 | ||
fe864821 JJ |
725 | /** |
726 | * apparmor_bprm_committing_creds - do task cleanup on committing new creds | |
727 | * @bprm: binprm for the exec (NOT NULL) | |
728 | */ | |
729 | static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) | |
730 | { | |
637f688d | 731 | struct aa_label *label = aa_current_raw_label(); |
d9087c49 | 732 | struct aa_label *new_label = cred_label(bprm->cred); |
fe864821 JJ |
733 | |
734 | /* bail out if unconfined or not changing profile */ | |
d9087c49 JJ |
735 | if ((new_label->proxy == label->proxy) || |
736 | (unconfined(new_label))) | |
fe864821 JJ |
737 | return; |
738 | ||
192ca6b5 JJ |
739 | aa_inherit_files(bprm->cred, current->files); |
740 | ||
fe864821 JJ |
741 | current->pdeath_signal = 0; |
742 | ||
637f688d | 743 | /* reset soft limits and set hard limits for the new label */ |
d9087c49 | 744 | __aa_transition_rlimits(label, new_label); |
fe864821 JJ |
745 | } |
746 | ||
747 | /** | |
748 | * apparmor_bprm_committed_cred - do cleanup after new creds committed | |
749 | * @bprm: binprm for the exec (NOT NULL) | |
750 | */ | |
751 | static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) | |
752 | { | |
3b529a76 | 753 | /* clear out temporary/transitional state from the context */ |
de62de59 | 754 | aa_clear_task_ctx_trans(task_ctx(current)); |
3b529a76 | 755 | |
fe864821 JJ |
756 | return; |
757 | } | |
758 | ||
a7ae3645 JJ |
759 | static void apparmor_task_getsecid(struct task_struct *p, u32 *secid) |
760 | { | |
761 | struct aa_label *label = aa_get_task_label(p); | |
762 | *secid = label->secid; | |
763 | aa_put_label(label); | |
764 | } | |
765 | ||
7cb4dc9f JS |
766 | static int apparmor_task_setrlimit(struct task_struct *task, |
767 | unsigned int resource, struct rlimit *new_rlim) | |
b5e95b48 | 768 | { |
637f688d | 769 | struct aa_label *label = __begin_current_label_crit_section(); |
b5e95b48 JJ |
770 | int error = 0; |
771 | ||
637f688d | 772 | if (!unconfined(label)) |
86b92cb7 | 773 | error = aa_task_setrlimit(label, task, resource, new_rlim); |
637f688d | 774 | __end_current_label_crit_section(label); |
b5e95b48 JJ |
775 | |
776 | return error; | |
777 | } | |
778 | ||
ae7795bc | 779 | static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, |
6b4f3d01 | 780 | int sig, const struct cred *cred) |
cd1dbf76 JJ |
781 | { |
782 | struct aa_label *cl, *tl; | |
783 | int error; | |
784 | ||
6b4f3d01 SS |
785 | if (cred) { |
786 | /* | |
787 | * Dealing with USB IO specific behavior | |
cd1dbf76 | 788 | */ |
6b4f3d01 SS |
789 | cl = aa_get_newest_cred_label(cred); |
790 | tl = aa_get_task_label(target); | |
791 | error = aa_may_signal(cl, tl, sig); | |
792 | aa_put_label(cl); | |
793 | aa_put_label(tl); | |
794 | return error; | |
795 | } | |
796 | ||
cd1dbf76 JJ |
797 | cl = __begin_current_label_crit_section(); |
798 | tl = aa_get_task_label(target); | |
799 | error = aa_may_signal(cl, tl, sig); | |
800 | aa_put_label(tl); | |
801 | __end_current_label_crit_section(cl); | |
802 | ||
803 | return error; | |
804 | } | |
805 | ||
56974a6f JJ |
806 | /** |
807 | * apparmor_sk_free_security - free the sk_security field | |
808 | */ | |
809 | static void apparmor_sk_free_security(struct sock *sk) | |
810 | { | |
716eca8c | 811 | struct aa_sk_ctx *ctx = aa_sock(sk); |
56974a6f | 812 | |
56974a6f JJ |
813 | aa_put_label(ctx->label); |
814 | aa_put_label(ctx->peer); | |
2775e078 | 815 | path_put(&ctx->path); |
56974a6f JJ |
816 | } |
817 | ||
818 | /** | |
819 | * apparmor_clone_security - clone the sk_security field | |
820 | */ | |
821 | static void apparmor_sk_clone_security(const struct sock *sk, | |
822 | struct sock *newsk) | |
823 | { | |
716eca8c CS |
824 | struct aa_sk_ctx *ctx = aa_sock(sk); |
825 | struct aa_sk_ctx *new = aa_sock(newsk); | |
56974a6f | 826 | |
3b646abc MFO |
827 | if (new->label) |
828 | aa_put_label(new->label); | |
56974a6f | 829 | new->label = aa_get_label(ctx->label); |
3b646abc MFO |
830 | |
831 | if (new->peer) | |
832 | aa_put_label(new->peer); | |
56974a6f | 833 | new->peer = aa_get_label(ctx->peer); |
2775e078 JJ |
834 | new->path = ctx->path; |
835 | path_get(&new->path); | |
836 | } | |
837 | ||
838 | static struct path *UNIX_FS_CONN_PATH(struct sock *sk, struct sock *newsk) | |
839 | { | |
840 | if (sk->sk_family == PF_UNIX && UNIX_FS(sk)) | |
841 | return &unix_sk(sk)->path; | |
842 | else if (newsk->sk_family == PF_UNIX && UNIX_FS(newsk)) | |
843 | return &unix_sk(newsk)->path; | |
844 | return NULL; | |
845 | } | |
846 | ||
847 | /** | |
848 | * apparmor_unix_stream_connect - check perms before making unix domain conn | |
849 | * | |
850 | * peer is locked when this hook is called | |
851 | */ | |
852 | static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk, | |
853 | struct sock *newsk) | |
854 | { | |
dc2011ae JJ |
855 | struct aa_sk_ctx *sk_ctx = aa_sock(sk); |
856 | struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk); | |
857 | struct aa_sk_ctx *new_ctx = aa_sock(newsk); | |
2775e078 JJ |
858 | struct aa_label *label; |
859 | struct path *path; | |
860 | int error; | |
861 | ||
862 | label = __begin_current_label_crit_section(); | |
863 | error = aa_unix_peer_perm(label, OP_CONNECT, | |
864 | (AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE), | |
865 | sk, peer_sk, NULL); | |
866 | if (!UNIX_FS(peer_sk)) { | |
867 | last_error(error, | |
868 | aa_unix_peer_perm(peer_ctx->label, OP_CONNECT, | |
869 | (AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE), | |
870 | peer_sk, sk, label)); | |
871 | } | |
872 | __end_current_label_crit_section(label); | |
873 | ||
874 | if (error) | |
875 | return error; | |
876 | ||
877 | /* label newsk if it wasn't labeled in post_create. Normally this | |
878 | * would be done in sock_graft, but because we are directly looking | |
879 | * at the peer_sk to obtain peer_labeling for unix socks this | |
880 | * does not work | |
881 | */ | |
882 | if (!new_ctx->label) | |
883 | new_ctx->label = aa_get_label(peer_ctx->label); | |
884 | ||
885 | /* Cross reference the peer labels for SO_PEERSEC */ | |
886 | if (new_ctx->peer) | |
887 | aa_put_label(new_ctx->peer); | |
888 | ||
889 | if (sk_ctx->peer) | |
890 | aa_put_label(sk_ctx->peer); | |
891 | ||
892 | new_ctx->peer = aa_get_label(sk_ctx->label); | |
893 | sk_ctx->peer = aa_get_label(peer_ctx->label); | |
894 | ||
895 | path = UNIX_FS_CONN_PATH(sk, peer_sk); | |
896 | if (path) { | |
897 | new_ctx->path = *path; | |
898 | sk_ctx->path = *path; | |
899 | path_get(path); | |
900 | path_get(path); | |
901 | } | |
902 | return 0; | |
903 | } | |
904 | ||
905 | /** | |
906 | * apparmor_unix_may_send - check perms before conn or sending unix dgrams | |
907 | * | |
908 | * other is locked when this hook is called | |
909 | * | |
910 | * dgram connect calls may_send, peer setup but path not copied????? | |
911 | */ | |
912 | static int apparmor_unix_may_send(struct socket *sock, struct socket *peer) | |
913 | { | |
dc2011ae | 914 | struct aa_sk_ctx *peer_ctx = aa_sock(peer->sk); |
2775e078 JJ |
915 | struct aa_label *label; |
916 | int error; | |
917 | ||
918 | label = __begin_current_label_crit_section(); | |
919 | error = xcheck(aa_unix_peer_perm(label, OP_SENDMSG, AA_MAY_SEND, | |
920 | sock->sk, peer->sk, NULL), | |
921 | aa_unix_peer_perm(peer_ctx->label, OP_SENDMSG, | |
922 | AA_MAY_RECEIVE, | |
923 | peer->sk, sock->sk, label)); | |
924 | __end_current_label_crit_section(label); | |
925 | ||
926 | return error; | |
56974a6f JJ |
927 | } |
928 | ||
929 | /** | |
930 | * apparmor_socket_create - check perms before creating a new socket | |
931 | */ | |
932 | static int apparmor_socket_create(int family, int type, int protocol, int kern) | |
933 | { | |
934 | struct aa_label *label; | |
935 | int error = 0; | |
936 | ||
937 | AA_BUG(in_interrupt()); | |
938 | ||
939 | label = begin_current_label_crit_section(); | |
940 | if (!(kern || unconfined(label))) | |
941 | error = af_select(family, | |
942 | create_perm(label, family, type, protocol), | |
943 | aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, | |
944 | family, type, protocol)); | |
945 | end_current_label_crit_section(label); | |
946 | ||
947 | return error; | |
948 | } | |
949 | ||
950 | /** | |
951 | * apparmor_socket_post_create - setup the per-socket security struct | |
952 | * | |
953 | * Note: | |
954 | * - kernel sockets currently labeled unconfined but we may want to | |
955 | * move to a special kernel label | |
956 | * - socket may not have sk here if created with sock_create_lite or | |
957 | * sock_alloc. These should be accept cases which will be handled in | |
958 | * sock_graft. | |
959 | */ | |
960 | static int apparmor_socket_post_create(struct socket *sock, int family, | |
961 | int type, int protocol, int kern) | |
962 | { | |
963 | struct aa_label *label; | |
964 | ||
965 | if (kern) { | |
966 | struct aa_ns *ns = aa_get_current_ns(); | |
967 | ||
968 | label = aa_get_label(ns_unconfined(ns)); | |
969 | aa_put_ns(ns); | |
970 | } else | |
971 | label = aa_get_current_label(); | |
972 | ||
973 | if (sock->sk) { | |
716eca8c | 974 | struct aa_sk_ctx *ctx = aa_sock(sock->sk); |
56974a6f JJ |
975 | |
976 | aa_put_label(ctx->label); | |
977 | ctx->label = aa_get_label(label); | |
978 | } | |
979 | aa_put_label(label); | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
984 | /** | |
985 | * apparmor_socket_bind - check perms before bind addr to socket | |
986 | */ | |
987 | static int apparmor_socket_bind(struct socket *sock, | |
988 | struct sockaddr *address, int addrlen) | |
989 | { | |
990 | AA_BUG(!sock); | |
991 | AA_BUG(!sock->sk); | |
992 | AA_BUG(!address); | |
993 | AA_BUG(in_interrupt()); | |
994 | ||
995 | return af_select(sock->sk->sk_family, | |
996 | bind_perm(sock, address, addrlen), | |
997 | aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); | |
998 | } | |
999 | ||
1000 | /** | |
1001 | * apparmor_socket_connect - check perms before connecting @sock to @address | |
1002 | */ | |
1003 | static int apparmor_socket_connect(struct socket *sock, | |
1004 | struct sockaddr *address, int addrlen) | |
1005 | { | |
1006 | AA_BUG(!sock); | |
1007 | AA_BUG(!sock->sk); | |
1008 | AA_BUG(!address); | |
1009 | AA_BUG(in_interrupt()); | |
1010 | ||
1011 | return af_select(sock->sk->sk_family, | |
1012 | connect_perm(sock, address, addrlen), | |
1013 | aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); | |
1014 | } | |
1015 | ||
1016 | /** | |
1017 | * apparmor_socket_list - check perms before allowing listen | |
1018 | */ | |
1019 | static int apparmor_socket_listen(struct socket *sock, int backlog) | |
1020 | { | |
1021 | AA_BUG(!sock); | |
1022 | AA_BUG(!sock->sk); | |
1023 | AA_BUG(in_interrupt()); | |
1024 | ||
1025 | return af_select(sock->sk->sk_family, | |
1026 | listen_perm(sock, backlog), | |
1027 | aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); | |
1028 | } | |
1029 | ||
1030 | /** | |
1031 | * apparmor_socket_accept - check perms before accepting a new connection. | |
1032 | * | |
1033 | * Note: while @newsock is created and has some information, the accept | |
1034 | * has not been done. | |
1035 | */ | |
1036 | static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) | |
1037 | { | |
1038 | AA_BUG(!sock); | |
1039 | AA_BUG(!sock->sk); | |
1040 | AA_BUG(!newsock); | |
1041 | AA_BUG(in_interrupt()); | |
1042 | ||
1043 | return af_select(sock->sk->sk_family, | |
1044 | accept_perm(sock, newsock), | |
1045 | aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); | |
1046 | } | |
1047 | ||
1048 | static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, | |
1049 | struct msghdr *msg, int size) | |
1050 | { | |
1051 | AA_BUG(!sock); | |
1052 | AA_BUG(!sock->sk); | |
1053 | AA_BUG(!msg); | |
1054 | AA_BUG(in_interrupt()); | |
1055 | ||
1056 | return af_select(sock->sk->sk_family, | |
1057 | msg_perm(op, request, sock, msg, size), | |
1058 | aa_sk_perm(op, request, sock->sk)); | |
1059 | } | |
1060 | ||
1061 | /** | |
1062 | * apparmor_socket_sendmsg - check perms before sending msg to another socket | |
1063 | */ | |
1064 | static int apparmor_socket_sendmsg(struct socket *sock, | |
1065 | struct msghdr *msg, int size) | |
1066 | { | |
1067 | return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); | |
1068 | } | |
1069 | ||
1070 | /** | |
1071 | * apparmor_socket_recvmsg - check perms before receiving a message | |
1072 | */ | |
1073 | static int apparmor_socket_recvmsg(struct socket *sock, | |
1074 | struct msghdr *msg, int size, int flags) | |
1075 | { | |
1076 | return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); | |
1077 | } | |
1078 | ||
1079 | /* revaliation, get/set attr, shutdown */ | |
1080 | static int aa_sock_perm(const char *op, u32 request, struct socket *sock) | |
1081 | { | |
1082 | AA_BUG(!sock); | |
1083 | AA_BUG(!sock->sk); | |
1084 | AA_BUG(in_interrupt()); | |
1085 | ||
1086 | return af_select(sock->sk->sk_family, | |
1087 | sock_perm(op, request, sock), | |
1088 | aa_sk_perm(op, request, sock->sk)); | |
1089 | } | |
1090 | ||
1091 | /** | |
1092 | * apparmor_socket_getsockname - check perms before getting the local address | |
1093 | */ | |
1094 | static int apparmor_socket_getsockname(struct socket *sock) | |
1095 | { | |
1096 | return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); | |
1097 | } | |
1098 | ||
1099 | /** | |
1100 | * apparmor_socket_getpeername - check perms before getting remote address | |
1101 | */ | |
1102 | static int apparmor_socket_getpeername(struct socket *sock) | |
1103 | { | |
1104 | return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); | |
1105 | } | |
1106 | ||
1107 | /* revaliation, get/set attr, opt */ | |
1108 | static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, | |
1109 | int level, int optname) | |
1110 | { | |
1111 | AA_BUG(!sock); | |
1112 | AA_BUG(!sock->sk); | |
1113 | AA_BUG(in_interrupt()); | |
1114 | ||
1115 | return af_select(sock->sk->sk_family, | |
1116 | opt_perm(op, request, sock, level, optname), | |
1117 | aa_sk_perm(op, request, sock->sk)); | |
1118 | } | |
1119 | ||
1120 | /** | |
1121 | * apparmor_getsockopt - check perms before getting socket options | |
1122 | */ | |
1123 | static int apparmor_socket_getsockopt(struct socket *sock, int level, | |
1124 | int optname) | |
1125 | { | |
1126 | return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, | |
1127 | level, optname); | |
1128 | } | |
1129 | ||
1130 | /** | |
1131 | * apparmor_setsockopt - check perms before setting socket options | |
1132 | */ | |
1133 | static int apparmor_socket_setsockopt(struct socket *sock, int level, | |
1134 | int optname) | |
1135 | { | |
1136 | return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, | |
1137 | level, optname); | |
1138 | } | |
1139 | ||
1140 | /** | |
1141 | * apparmor_socket_shutdown - check perms before shutting down @sock conn | |
1142 | */ | |
1143 | static int apparmor_socket_shutdown(struct socket *sock, int how) | |
1144 | { | |
1145 | return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); | |
1146 | } | |
1147 | ||
e1af4779 | 1148 | #ifdef CONFIG_NETWORK_SECMARK |
56974a6f JJ |
1149 | /** |
1150 | * apparmor_socket_sock_recv_skb - check perms before associating skb to sk | |
1151 | * | |
1152 | * Note: can not sleep may be called with locks held | |
1153 | * | |
1154 | * dont want protocol specific in __skb_recv_datagram() | |
1155 | * to deny an incoming connection socket_sock_rcv_skb() | |
1156 | */ | |
1157 | static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) | |
1158 | { | |
716eca8c | 1159 | struct aa_sk_ctx *ctx = aa_sock(sk); |
ab9f2115 MG |
1160 | |
1161 | if (!skb->secmark) | |
1162 | return 0; | |
1163 | ||
1164 | return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, | |
1165 | skb->secmark, sk); | |
56974a6f | 1166 | } |
e1af4779 | 1167 | #endif |
56974a6f JJ |
1168 | |
1169 | ||
1170 | static struct aa_label *sk_peer_label(struct sock *sk) | |
1171 | { | |
2775e078 | 1172 | struct sock *peer_sk; |
716eca8c | 1173 | struct aa_sk_ctx *ctx = aa_sock(sk); |
9b2a53cd | 1174 | struct aa_label *label = ERR_PTR(-ENOPROTOOPT); |
56974a6f JJ |
1175 | |
1176 | if (ctx->peer) | |
9b2a53cd | 1177 | return aa_get_label(ctx->peer); |
56974a6f | 1178 | |
2775e078 JJ |
1179 | if (sk->sk_family != PF_UNIX) |
1180 | return ERR_PTR(-ENOPROTOOPT); | |
1181 | ||
1182 | /* check for sockpair peering which does not go through | |
1183 | * security_unix_stream_connect | |
1184 | */ | |
9b2a53cd | 1185 | peer_sk = unix_peer_get(sk); |
2775e078 | 1186 | if (peer_sk) { |
dc2011ae | 1187 | ctx = aa_sock(peer_sk); |
2775e078 | 1188 | if (ctx->label) |
9b2a53cd JJ |
1189 | label = aa_get_label(ctx->label); |
1190 | sock_put(peer_sk); | |
2775e078 JJ |
1191 | } |
1192 | ||
9b2a53cd | 1193 | return label; |
56974a6f JJ |
1194 | } |
1195 | ||
1196 | /** | |
1197 | * apparmor_socket_getpeersec_stream - get security context of peer | |
1198 | * | |
1199 | * Note: for tcp only valid if using ipsec or cipso on lan | |
1200 | */ | |
1201 | static int apparmor_socket_getpeersec_stream(struct socket *sock, | |
1202 | char __user *optval, | |
1203 | int __user *optlen, | |
1204 | unsigned int len) | |
1205 | { | |
1206 | char *name; | |
1207 | int slen, error = 0; | |
1208 | struct aa_label *label; | |
1209 | struct aa_label *peer; | |
1210 | ||
1211 | label = begin_current_label_crit_section(); | |
1212 | peer = sk_peer_label(sock->sk); | |
1213 | if (IS_ERR(peer)) { | |
1214 | error = PTR_ERR(peer); | |
1215 | goto done; | |
1216 | } | |
1217 | slen = aa_label_asxprint(&name, labels_ns(label), peer, | |
1218 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | | |
1219 | FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); | |
1220 | /* don't include terminating \0 in slen, it breaks some apps */ | |
1221 | if (slen < 0) { | |
1222 | error = -ENOMEM; | |
1223 | } else { | |
1224 | if (slen > len) { | |
1225 | error = -ERANGE; | |
1226 | } else if (copy_to_user(optval, name, slen)) { | |
1227 | error = -EFAULT; | |
1228 | goto out; | |
1229 | } | |
1230 | if (put_user(slen, optlen)) | |
1231 | error = -EFAULT; | |
1232 | out: | |
1233 | kfree(name); | |
1234 | ||
1235 | } | |
1236 | ||
9b2a53cd | 1237 | aa_put_label(peer); |
56974a6f JJ |
1238 | done: |
1239 | end_current_label_crit_section(label); | |
1240 | ||
1241 | return error; | |
1242 | } | |
1243 | ||
56974a6f JJ |
1244 | /** |
1245 | * apparmor_sock_graft - Initialize newly created socket | |
1246 | * @sk: child sock | |
1247 | * @parent: parent socket | |
1248 | * | |
1249 | * Note: could set off of SOCK_CTX(parent) but need to track inode and we can | |
1250 | * just set sk security information off of current creating process label | |
1251 | * Labeling of sk for accept case - probably should be sock based | |
1252 | * instead of task, because of the case where an implicitly labeled | |
1253 | * socket is shared by different tasks. | |
1254 | */ | |
1255 | static void apparmor_sock_graft(struct sock *sk, struct socket *parent) | |
1256 | { | |
716eca8c | 1257 | struct aa_sk_ctx *ctx = aa_sock(sk); |
56974a6f JJ |
1258 | |
1259 | if (!ctx->label) | |
1260 | ctx->label = aa_get_current_label(); | |
1261 | } | |
1262 | ||
e1af4779 | 1263 | #ifdef CONFIG_NETWORK_SECMARK |
41dd9596 | 1264 | static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, |
ab9f2115 MG |
1265 | struct request_sock *req) |
1266 | { | |
716eca8c | 1267 | struct aa_sk_ctx *ctx = aa_sock(sk); |
ab9f2115 MG |
1268 | |
1269 | if (!skb->secmark) | |
1270 | return 0; | |
1271 | ||
1272 | return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, | |
1273 | skb->secmark, sk); | |
1274 | } | |
e1af4779 | 1275 | #endif |
ab9f2115 | 1276 | |
bbd3662a CS |
1277 | /* |
1278 | * The cred blob is a pointer to, not an instance of, an aa_task_ctx. | |
1279 | */ | |
1280 | struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { | |
1281 | .lbs_cred = sizeof(struct aa_task_ctx *), | |
33bf60ca | 1282 | .lbs_file = sizeof(struct aa_file_ctx), |
f4ad8f2c | 1283 | .lbs_task = sizeof(struct aa_task_ctx), |
716eca8c | 1284 | .lbs_sock = sizeof(struct aa_sk_ctx), |
bbd3662a CS |
1285 | }; |
1286 | ||
f4d92e4c CS |
1287 | static struct lsm_id apparmor_lsmid __lsm_ro_after_init = { |
1288 | .lsm = "apparmor", | |
1289 | .slot = LSMBLOB_NEEDED | |
1290 | }; | |
1291 | ||
ca97d939 | 1292 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
e20b043a CS |
1293 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
1294 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), | |
1295 | LSM_HOOK_INIT(capget, apparmor_capget), | |
1296 | LSM_HOOK_INIT(capable, apparmor_capable), | |
1297 | ||
2ea3ffb7 JJ |
1298 | LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), |
1299 | LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), | |
1300 | LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), | |
1301 | ||
e20b043a CS |
1302 | LSM_HOOK_INIT(path_link, apparmor_path_link), |
1303 | LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), | |
1304 | LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), | |
1305 | LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), | |
1306 | LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), | |
1307 | LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), | |
1308 | LSM_HOOK_INIT(path_rename, apparmor_path_rename), | |
1309 | LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), | |
1310 | LSM_HOOK_INIT(path_chown, apparmor_path_chown), | |
1311 | LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), | |
1312 | LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), | |
1313 | ||
1314 | LSM_HOOK_INIT(file_open, apparmor_file_open), | |
064dc947 | 1315 | LSM_HOOK_INIT(file_receive, apparmor_file_receive), |
e20b043a CS |
1316 | LSM_HOOK_INIT(file_permission, apparmor_file_permission), |
1317 | LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), | |
1318 | LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), | |
1319 | LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), | |
e20b043a CS |
1320 | LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), |
1321 | LSM_HOOK_INIT(file_lock, apparmor_file_lock), | |
1322 | ||
1323 | LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), | |
1324 | LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), | |
1325 | ||
56974a6f JJ |
1326 | LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), |
1327 | LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), | |
1328 | ||
2775e078 JJ |
1329 | LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect), |
1330 | LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send), | |
1331 | ||
56974a6f JJ |
1332 | LSM_HOOK_INIT(socket_create, apparmor_socket_create), |
1333 | LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), | |
1334 | LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), | |
1335 | LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), | |
1336 | LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), | |
1337 | LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), | |
1338 | LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), | |
1339 | LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), | |
1340 | LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), | |
1341 | LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), | |
1342 | LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), | |
1343 | LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), | |
1344 | LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), | |
e1af4779 | 1345 | #ifdef CONFIG_NETWORK_SECMARK |
56974a6f | 1346 | LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), |
e1af4779 | 1347 | #endif |
56974a6f JJ |
1348 | LSM_HOOK_INIT(socket_getpeersec_stream, |
1349 | apparmor_socket_getpeersec_stream), | |
56974a6f | 1350 | LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), |
e1af4779 | 1351 | #ifdef CONFIG_NETWORK_SECMARK |
ab9f2115 | 1352 | LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), |
e1af4779 | 1353 | #endif |
56974a6f | 1354 | |
e20b043a CS |
1355 | LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), |
1356 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), | |
1357 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), | |
1358 | LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), | |
1359 | ||
b8bff599 | 1360 | LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), |
e20b043a CS |
1361 | LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), |
1362 | LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), | |
e20b043a | 1363 | |
3b529a76 JJ |
1364 | LSM_HOOK_INIT(task_free, apparmor_task_free), |
1365 | LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), | |
a7ae3645 | 1366 | LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid), |
e20b043a | 1367 | LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), |
cd1dbf76 | 1368 | LSM_HOOK_INIT(task_kill, apparmor_task_kill), |
c0929212 | 1369 | |
e79c26d0 MG |
1370 | #ifdef CONFIG_AUDIT |
1371 | LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), | |
1372 | LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), | |
1373 | LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), | |
1374 | LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), | |
1375 | #endif | |
1376 | ||
c0929212 JJ |
1377 | LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), |
1378 | LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), | |
1379 | LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), | |
b5e95b48 JJ |
1380 | }; |
1381 | ||
1382 | /* | |
1383 | * AppArmor sysfs module parameters | |
1384 | */ | |
1385 | ||
101d6c82 SR |
1386 | static int param_set_aabool(const char *val, const struct kernel_param *kp); |
1387 | static int param_get_aabool(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1388 | #define param_check_aabool param_check_bool |
9c27847d | 1389 | static const struct kernel_param_ops param_ops_aabool = { |
6a4c2643 | 1390 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1391 | .set = param_set_aabool, |
1392 | .get = param_get_aabool | |
1393 | }; | |
b5e95b48 | 1394 | |
101d6c82 SR |
1395 | static int param_set_aauint(const char *val, const struct kernel_param *kp); |
1396 | static int param_get_aauint(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1397 | #define param_check_aauint param_check_uint |
9c27847d | 1398 | static const struct kernel_param_ops param_ops_aauint = { |
101d6c82 SR |
1399 | .set = param_set_aauint, |
1400 | .get = param_get_aauint | |
1401 | }; | |
b5e95b48 | 1402 | |
63c16c3a CC |
1403 | static int param_set_aacompressionlevel(const char *val, |
1404 | const struct kernel_param *kp); | |
1405 | static int param_get_aacompressionlevel(char *buffer, | |
1406 | const struct kernel_param *kp); | |
1407 | #define param_check_aacompressionlevel param_check_int | |
1408 | static const struct kernel_param_ops param_ops_aacompressionlevel = { | |
1409 | .set = param_set_aacompressionlevel, | |
1410 | .get = param_get_aacompressionlevel | |
1411 | }; | |
1412 | ||
101d6c82 SR |
1413 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); |
1414 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); | |
b8aa09fd | 1415 | #define param_check_aalockpolicy param_check_bool |
9c27847d | 1416 | static const struct kernel_param_ops param_ops_aalockpolicy = { |
6a4c2643 | 1417 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
101d6c82 SR |
1418 | .set = param_set_aalockpolicy, |
1419 | .get = param_get_aalockpolicy | |
1420 | }; | |
b5e95b48 | 1421 | |
e4dca7b7 KC |
1422 | static int param_set_audit(const char *val, const struct kernel_param *kp); |
1423 | static int param_get_audit(char *buffer, const struct kernel_param *kp); | |
b5e95b48 | 1424 | |
e4dca7b7 KC |
1425 | static int param_set_mode(const char *val, const struct kernel_param *kp); |
1426 | static int param_get_mode(char *buffer, const struct kernel_param *kp); | |
b5e95b48 JJ |
1427 | |
1428 | /* Flag values, also controllable via /sys/module/apparmor/parameters | |
1429 | * We define special types as we want to do additional mediation. | |
1430 | */ | |
1431 | ||
1432 | /* AppArmor global enforcement switch - complain, enforce, kill */ | |
1433 | enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; | |
1434 | module_param_call(mode, param_set_mode, param_get_mode, | |
1435 | &aa_g_profile_mode, S_IRUSR | S_IWUSR); | |
1436 | ||
6059f71f | 1437 | /* whether policy verification hashing is enabled */ |
7616ac70 | 1438 | bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); |
3ccb76c5 | 1439 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
6059f71f | 1440 | module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); |
7616ac70 | 1441 | #endif |
6059f71f | 1442 | |
63c16c3a CC |
1443 | /* policy loaddata compression level */ |
1444 | int aa_g_rawdata_compression_level = Z_DEFAULT_COMPRESSION; | |
1445 | module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, | |
1446 | aacompressionlevel, 0400); | |
1447 | ||
b5e95b48 | 1448 | /* Debug mode */ |
eea7a05f | 1449 | bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); |
b5e95b48 JJ |
1450 | module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); |
1451 | ||
1452 | /* Audit mode */ | |
1453 | enum audit_mode aa_g_audit; | |
1454 | module_param_call(audit, param_set_audit, param_get_audit, | |
1455 | &aa_g_audit, S_IRUSR | S_IWUSR); | |
1456 | ||
1457 | /* Determines if audit header is included in audited messages. This | |
1458 | * provides more context if the audit daemon is not running | |
1459 | */ | |
954317fe | 1460 | bool aa_g_audit_header = true; |
b5e95b48 JJ |
1461 | module_param_named(audit_header, aa_g_audit_header, aabool, |
1462 | S_IRUSR | S_IWUSR); | |
1463 | ||
1464 | /* lock out loading/removal of policy | |
1465 | * TODO: add in at boot loading of policy, which is the only way to | |
1466 | * load policy, if lock_policy is set | |
1467 | */ | |
90ab5ee9 | 1468 | bool aa_g_lock_policy; |
b5e95b48 JJ |
1469 | module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, |
1470 | S_IRUSR | S_IWUSR); | |
1471 | ||
1472 | /* Syscall logging mode */ | |
90ab5ee9 | 1473 | bool aa_g_logsyscall; |
b5e95b48 JJ |
1474 | module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); |
1475 | ||
1476 | /* Maximum pathname length before accesses will start getting rejected */ | |
1477 | unsigned int aa_g_path_max = 2 * PATH_MAX; | |
622f6e32 | 1478 | module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); |
b5e95b48 JJ |
1479 | |
1480 | /* Determines how paranoid loading of policy is and how much verification | |
1481 | * on the loaded policy is done. | |
abbf8734 JJ |
1482 | * DEPRECATED: read only as strict checking of load is always done now |
1483 | * that none root users (user namespaces) can load policy. | |
b5e95b48 | 1484 | */ |
954317fe | 1485 | bool aa_g_paranoid_load = true; |
abbf8734 | 1486 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
b5e95b48 | 1487 | |
e33c1b99 KC |
1488 | static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); |
1489 | static int param_set_aaintbool(const char *val, const struct kernel_param *kp); | |
1490 | #define param_check_aaintbool param_check_int | |
1491 | static const struct kernel_param_ops param_ops_aaintbool = { | |
1492 | .set = param_set_aaintbool, | |
1493 | .get = param_get_aaintbool | |
1494 | }; | |
b5e95b48 | 1495 | /* Boot time disable flag */ |
0102fb83 | 1496 | static int apparmor_enabled __lsm_ro_after_init = 1; |
e33c1b99 | 1497 | module_param_named(enabled, apparmor_enabled, aaintbool, 0444); |
b5e95b48 JJ |
1498 | |
1499 | static int __init apparmor_enabled_setup(char *str) | |
1500 | { | |
1501 | unsigned long enabled; | |
29707b20 | 1502 | int error = kstrtoul(str, 0, &enabled); |
b5e95b48 JJ |
1503 | if (!error) |
1504 | apparmor_enabled = enabled ? 1 : 0; | |
1505 | return 1; | |
1506 | } | |
1507 | ||
1508 | __setup("apparmor=", apparmor_enabled_setup); | |
1509 | ||
1510 | /* set global flag turning off the ability to load policy */ | |
101d6c82 | 1511 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1512 | { |
545de8fe JJ |
1513 | if (!apparmor_enabled) |
1514 | return -EINVAL; | |
1515 | if (apparmor_initialized && !policy_admin_capable(NULL)) | |
b5e95b48 | 1516 | return -EPERM; |
b5e95b48 JJ |
1517 | return param_set_bool(val, kp); |
1518 | } | |
1519 | ||
101d6c82 | 1520 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1521 | { |
ca4bd5ae JJ |
1522 | if (!apparmor_enabled) |
1523 | return -EINVAL; | |
545de8fe JJ |
1524 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1525 | return -EPERM; | |
b5e95b48 JJ |
1526 | return param_get_bool(buffer, kp); |
1527 | } | |
1528 | ||
101d6c82 | 1529 | static int param_set_aabool(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1530 | { |
ca4bd5ae JJ |
1531 | if (!apparmor_enabled) |
1532 | return -EINVAL; | |
545de8fe JJ |
1533 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1534 | return -EPERM; | |
b5e95b48 JJ |
1535 | return param_set_bool(val, kp); |
1536 | } | |
1537 | ||
101d6c82 | 1538 | static int param_get_aabool(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1539 | { |
ca4bd5ae JJ |
1540 | if (!apparmor_enabled) |
1541 | return -EINVAL; | |
545de8fe JJ |
1542 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1543 | return -EPERM; | |
b5e95b48 JJ |
1544 | return param_get_bool(buffer, kp); |
1545 | } | |
1546 | ||
101d6c82 | 1547 | static int param_set_aauint(const char *val, const struct kernel_param *kp) |
b5e95b48 | 1548 | { |
39d84824 JJ |
1549 | int error; |
1550 | ||
ca4bd5ae JJ |
1551 | if (!apparmor_enabled) |
1552 | return -EINVAL; | |
39d84824 JJ |
1553 | /* file is ro but enforce 2nd line check */ |
1554 | if (apparmor_initialized) | |
545de8fe | 1555 | return -EPERM; |
39d84824 JJ |
1556 | |
1557 | error = param_set_uint(val, kp); | |
df323337 | 1558 | aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); |
39d84824 JJ |
1559 | pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); |
1560 | ||
1561 | return error; | |
b5e95b48 JJ |
1562 | } |
1563 | ||
101d6c82 | 1564 | static int param_get_aauint(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1565 | { |
ca4bd5ae JJ |
1566 | if (!apparmor_enabled) |
1567 | return -EINVAL; | |
545de8fe JJ |
1568 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1569 | return -EPERM; | |
b5e95b48 JJ |
1570 | return param_get_uint(buffer, kp); |
1571 | } | |
1572 | ||
e33c1b99 KC |
1573 | /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ |
1574 | static int param_set_aaintbool(const char *val, const struct kernel_param *kp) | |
1575 | { | |
1576 | struct kernel_param kp_local; | |
1577 | bool value; | |
1578 | int error; | |
1579 | ||
1580 | if (apparmor_initialized) | |
1581 | return -EPERM; | |
1582 | ||
1583 | /* Create local copy, with arg pointing to bool type. */ | |
1584 | value = !!*((int *)kp->arg); | |
1585 | memcpy(&kp_local, kp, sizeof(kp_local)); | |
1586 | kp_local.arg = &value; | |
1587 | ||
1588 | error = param_set_bool(val, &kp_local); | |
1589 | if (!error) | |
1590 | *((int *)kp->arg) = *((bool *)kp_local.arg); | |
1591 | return error; | |
1592 | } | |
1593 | ||
1594 | /* | |
1595 | * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to | |
1596 | * 1/0, this converts the "int that is actually bool" back to bool for | |
1597 | * display in the /sys filesystem, while keeping it "int" for the LSM | |
1598 | * infrastructure. | |
1599 | */ | |
1600 | static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) | |
1601 | { | |
1602 | struct kernel_param kp_local; | |
1603 | bool value; | |
1604 | ||
1605 | /* Create local copy, with arg pointing to bool type. */ | |
1606 | value = !!*((int *)kp->arg); | |
1607 | memcpy(&kp_local, kp, sizeof(kp_local)); | |
1608 | kp_local.arg = &value; | |
1609 | ||
1610 | return param_get_bool(buffer, &kp_local); | |
1611 | } | |
1612 | ||
63c16c3a CC |
1613 | static int param_set_aacompressionlevel(const char *val, |
1614 | const struct kernel_param *kp) | |
1615 | { | |
1616 | int error; | |
1617 | ||
1618 | if (!apparmor_enabled) | |
1619 | return -EINVAL; | |
1620 | if (apparmor_initialized) | |
1621 | return -EPERM; | |
1622 | ||
1623 | error = param_set_int(val, kp); | |
1624 | ||
1625 | aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, | |
1626 | Z_NO_COMPRESSION, | |
1627 | Z_BEST_COMPRESSION); | |
1628 | pr_info("AppArmor: policy rawdata compression level set to %u\n", | |
1629 | aa_g_rawdata_compression_level); | |
1630 | ||
1631 | return error; | |
1632 | } | |
1633 | ||
1634 | static int param_get_aacompressionlevel(char *buffer, | |
1635 | const struct kernel_param *kp) | |
1636 | { | |
1637 | if (!apparmor_enabled) | |
1638 | return -EINVAL; | |
1639 | if (apparmor_initialized && !policy_view_capable(NULL)) | |
1640 | return -EPERM; | |
1641 | return param_get_int(buffer, kp); | |
1642 | } | |
1643 | ||
e4dca7b7 | 1644 | static int param_get_audit(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1645 | { |
b5e95b48 JJ |
1646 | if (!apparmor_enabled) |
1647 | return -EINVAL; | |
545de8fe JJ |
1648 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1649 | return -EPERM; | |
b5e95b48 JJ |
1650 | return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); |
1651 | } | |
1652 | ||
e4dca7b7 | 1653 | static int param_set_audit(const char *val, const struct kernel_param *kp) |
b5e95b48 JJ |
1654 | { |
1655 | int i; | |
b5e95b48 JJ |
1656 | |
1657 | if (!apparmor_enabled) | |
1658 | return -EINVAL; | |
b5e95b48 JJ |
1659 | if (!val) |
1660 | return -EINVAL; | |
545de8fe JJ |
1661 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1662 | return -EPERM; | |
b5e95b48 | 1663 | |
5d8779a5 AS |
1664 | i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); |
1665 | if (i < 0) | |
1666 | return -EINVAL; | |
b5e95b48 | 1667 | |
5d8779a5 AS |
1668 | aa_g_audit = i; |
1669 | return 0; | |
b5e95b48 JJ |
1670 | } |
1671 | ||
e4dca7b7 | 1672 | static int param_get_mode(char *buffer, const struct kernel_param *kp) |
b5e95b48 | 1673 | { |
b5e95b48 JJ |
1674 | if (!apparmor_enabled) |
1675 | return -EINVAL; | |
545de8fe JJ |
1676 | if (apparmor_initialized && !policy_view_capable(NULL)) |
1677 | return -EPERM; | |
b5e95b48 | 1678 | |
0d259f04 | 1679 | return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); |
b5e95b48 JJ |
1680 | } |
1681 | ||
e4dca7b7 | 1682 | static int param_set_mode(const char *val, const struct kernel_param *kp) |
b5e95b48 JJ |
1683 | { |
1684 | int i; | |
b5e95b48 JJ |
1685 | |
1686 | if (!apparmor_enabled) | |
1687 | return -EINVAL; | |
b5e95b48 JJ |
1688 | if (!val) |
1689 | return -EINVAL; | |
545de8fe JJ |
1690 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
1691 | return -EPERM; | |
b5e95b48 | 1692 | |
5d8779a5 AS |
1693 | i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, |
1694 | val); | |
1695 | if (i < 0) | |
1696 | return -EINVAL; | |
b5e95b48 | 1697 | |
5d8779a5 AS |
1698 | aa_g_profile_mode = i; |
1699 | return 0; | |
b5e95b48 JJ |
1700 | } |
1701 | ||
341c1fda | 1702 | char *aa_get_buffer(bool in_atomic) |
df323337 SAS |
1703 | { |
1704 | union aa_buffer *aa_buf; | |
1705 | bool try_again = true; | |
341c1fda | 1706 | gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); |
df323337 SAS |
1707 | |
1708 | retry: | |
1709 | spin_lock(&aa_buffers_lock); | |
341c1fda JJ |
1710 | if (buffer_count > reserve_count || |
1711 | (in_atomic && !list_empty(&aa_global_buffers))) { | |
df323337 SAS |
1712 | aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, |
1713 | list); | |
1714 | list_del(&aa_buf->list); | |
341c1fda | 1715 | buffer_count--; |
df323337 SAS |
1716 | spin_unlock(&aa_buffers_lock); |
1717 | return &aa_buf->buffer[0]; | |
1718 | } | |
341c1fda JJ |
1719 | if (in_atomic) { |
1720 | /* | |
1721 | * out of reserve buffers and in atomic context so increase | |
1722 | * how many buffers to keep in reserve | |
1723 | */ | |
1724 | reserve_count++; | |
1725 | flags = GFP_ATOMIC; | |
1726 | } | |
df323337 SAS |
1727 | spin_unlock(&aa_buffers_lock); |
1728 | ||
341c1fda JJ |
1729 | if (!in_atomic) |
1730 | might_sleep(); | |
1731 | aa_buf = kmalloc(aa_g_path_max, flags); | |
df323337 SAS |
1732 | if (!aa_buf) { |
1733 | if (try_again) { | |
1734 | try_again = false; | |
1735 | goto retry; | |
1736 | } | |
1737 | pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); | |
1738 | return NULL; | |
1739 | } | |
1740 | return &aa_buf->buffer[0]; | |
1741 | } | |
1742 | ||
1743 | void aa_put_buffer(char *buf) | |
1744 | { | |
1745 | union aa_buffer *aa_buf; | |
1746 | ||
1747 | if (!buf) | |
1748 | return; | |
1749 | aa_buf = container_of(buf, union aa_buffer, buffer[0]); | |
1750 | ||
1751 | spin_lock(&aa_buffers_lock); | |
1752 | list_add(&aa_buf->list, &aa_global_buffers); | |
341c1fda | 1753 | buffer_count++; |
df323337 SAS |
1754 | spin_unlock(&aa_buffers_lock); |
1755 | } | |
1756 | ||
b5e95b48 JJ |
1757 | /* |
1758 | * AppArmor init functions | |
1759 | */ | |
1760 | ||
1761 | /** | |
55a26ebf | 1762 | * set_init_ctx - set a task context and profile on the first task. |
b5e95b48 JJ |
1763 | * |
1764 | * TODO: allow setting an alternate profile than unconfined | |
1765 | */ | |
55a26ebf | 1766 | static int __init set_init_ctx(void) |
b5e95b48 | 1767 | { |
bf1d2ee7 | 1768 | struct cred *cred = (__force struct cred *)current->real_cred; |
b5e95b48 | 1769 | |
69b5a44a | 1770 | set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); |
b5e95b48 JJ |
1771 | |
1772 | return 0; | |
1773 | } | |
1774 | ||
d4669f0b JJ |
1775 | static void destroy_buffers(void) |
1776 | { | |
df323337 | 1777 | union aa_buffer *aa_buf; |
d4669f0b | 1778 | |
df323337 SAS |
1779 | spin_lock(&aa_buffers_lock); |
1780 | while (!list_empty(&aa_global_buffers)) { | |
1781 | aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, | |
1782 | list); | |
1783 | list_del(&aa_buf->list); | |
1784 | spin_unlock(&aa_buffers_lock); | |
1785 | kfree(aa_buf); | |
1786 | spin_lock(&aa_buffers_lock); | |
d4669f0b | 1787 | } |
df323337 | 1788 | spin_unlock(&aa_buffers_lock); |
d4669f0b JJ |
1789 | } |
1790 | ||
1791 | static int __init alloc_buffers(void) | |
1792 | { | |
df323337 SAS |
1793 | union aa_buffer *aa_buf; |
1794 | int i, num; | |
1795 | ||
1796 | /* | |
1797 | * A function may require two buffers at once. Usually the buffers are | |
1798 | * used for a short period of time and are shared. On UP kernel buffers | |
1799 | * two should be enough, with more CPUs it is possible that more | |
1800 | * buffers will be used simultaneously. The preallocated pool may grow. | |
1801 | * This preallocation has also the side-effect that AppArmor will be | |
1802 | * disabled early at boot if aa_g_path_max is extremly high. | |
1803 | */ | |
1804 | if (num_online_cpus() > 1) | |
341c1fda | 1805 | num = 4 + RESERVE_COUNT; |
df323337 | 1806 | else |
341c1fda | 1807 | num = 2 + RESERVE_COUNT; |
df323337 SAS |
1808 | |
1809 | for (i = 0; i < num; i++) { | |
1810 | ||
1811 | aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | | |
1812 | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); | |
1813 | if (!aa_buf) { | |
1814 | destroy_buffers(); | |
1815 | return -ENOMEM; | |
d4669f0b | 1816 | } |
df323337 | 1817 | aa_put_buffer(&aa_buf->buffer[0]); |
d4669f0b | 1818 | } |
d4669f0b JJ |
1819 | return 0; |
1820 | } | |
1821 | ||
e3ea1ca5 TH |
1822 | #ifdef CONFIG_SYSCTL |
1823 | static int apparmor_dointvec(struct ctl_table *table, int write, | |
32927393 | 1824 | void *buffer, size_t *lenp, loff_t *ppos) |
e3ea1ca5 TH |
1825 | { |
1826 | if (!policy_admin_capable(NULL)) | |
1827 | return -EPERM; | |
1828 | if (!apparmor_enabled) | |
1829 | return -EINVAL; | |
1830 | ||
1831 | return proc_dointvec(table, write, buffer, lenp, ppos); | |
1832 | } | |
1833 | ||
1834 | static struct ctl_path apparmor_sysctl_path[] = { | |
1835 | { .procname = "kernel", }, | |
1836 | { } | |
1837 | }; | |
1838 | ||
1839 | static struct ctl_table apparmor_sysctl_table[] = { | |
1840 | { | |
1841 | .procname = "unprivileged_userns_apparmor_policy", | |
1842 | .data = &unprivileged_userns_apparmor_policy, | |
1843 | .maxlen = sizeof(int), | |
1844 | .mode = 0600, | |
1845 | .proc_handler = apparmor_dointvec, | |
1846 | }, | |
072d5a9a JJ |
1847 | { |
1848 | .procname = "apparmor_display_secid_mode", | |
1849 | .data = &apparmor_display_secid_mode, | |
1850 | .maxlen = sizeof(int), | |
1851 | .mode = 0600, | |
1852 | .proc_handler = apparmor_dointvec, | |
1853 | }, | |
1854 | ||
e3ea1ca5 TH |
1855 | { } |
1856 | }; | |
1857 | ||
1858 | static int __init apparmor_init_sysctl(void) | |
1859 | { | |
1860 | return register_sysctl_paths(apparmor_sysctl_path, | |
1861 | apparmor_sysctl_table) ? 0 : -ENOMEM; | |
1862 | } | |
1863 | #else | |
1864 | static inline int apparmor_init_sysctl(void) | |
1865 | { | |
1866 | return 0; | |
1867 | } | |
1868 | #endif /* CONFIG_SYSCTL */ | |
1869 | ||
e1af4779 | 1870 | #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) |
ab9f2115 MG |
1871 | static unsigned int apparmor_ip_postroute(void *priv, |
1872 | struct sk_buff *skb, | |
1873 | const struct nf_hook_state *state) | |
1874 | { | |
1875 | struct aa_sk_ctx *ctx; | |
1876 | struct sock *sk; | |
1877 | ||
1878 | if (!skb->secmark) | |
1879 | return NF_ACCEPT; | |
1880 | ||
1881 | sk = skb_to_full_sk(skb); | |
1882 | if (sk == NULL) | |
1883 | return NF_ACCEPT; | |
1884 | ||
716eca8c | 1885 | ctx = aa_sock(sk); |
ab9f2115 MG |
1886 | if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, |
1887 | skb->secmark, sk)) | |
1888 | return NF_ACCEPT; | |
1889 | ||
1890 | return NF_DROP_ERR(-ECONNREFUSED); | |
1891 | ||
1892 | } | |
1893 | ||
1894 | static unsigned int apparmor_ipv4_postroute(void *priv, | |
1895 | struct sk_buff *skb, | |
1896 | const struct nf_hook_state *state) | |
1897 | { | |
1898 | return apparmor_ip_postroute(priv, skb, state); | |
1899 | } | |
1900 | ||
a1a02062 | 1901 | #if IS_ENABLED(CONFIG_IPV6) |
ab9f2115 MG |
1902 | static unsigned int apparmor_ipv6_postroute(void *priv, |
1903 | struct sk_buff *skb, | |
1904 | const struct nf_hook_state *state) | |
1905 | { | |
1906 | return apparmor_ip_postroute(priv, skb, state); | |
1907 | } | |
a1a02062 | 1908 | #endif |
ab9f2115 MG |
1909 | |
1910 | static const struct nf_hook_ops apparmor_nf_ops[] = { | |
1911 | { | |
1912 | .hook = apparmor_ipv4_postroute, | |
1913 | .pf = NFPROTO_IPV4, | |
1914 | .hooknum = NF_INET_POST_ROUTING, | |
1915 | .priority = NF_IP_PRI_SELINUX_FIRST, | |
1916 | }, | |
1917 | #if IS_ENABLED(CONFIG_IPV6) | |
1918 | { | |
1919 | .hook = apparmor_ipv6_postroute, | |
1920 | .pf = NFPROTO_IPV6, | |
1921 | .hooknum = NF_INET_POST_ROUTING, | |
1922 | .priority = NF_IP6_PRI_SELINUX_FIRST, | |
1923 | }, | |
1924 | #endif | |
1925 | }; | |
1926 | ||
1927 | static int __net_init apparmor_nf_register(struct net *net) | |
1928 | { | |
1929 | int ret; | |
1930 | ||
1931 | ret = nf_register_net_hooks(net, apparmor_nf_ops, | |
1932 | ARRAY_SIZE(apparmor_nf_ops)); | |
1933 | return ret; | |
1934 | } | |
1935 | ||
1936 | static void __net_exit apparmor_nf_unregister(struct net *net) | |
1937 | { | |
1938 | nf_unregister_net_hooks(net, apparmor_nf_ops, | |
1939 | ARRAY_SIZE(apparmor_nf_ops)); | |
1940 | } | |
1941 | ||
1942 | static struct pernet_operations apparmor_net_ops = { | |
1943 | .init = apparmor_nf_register, | |
1944 | .exit = apparmor_nf_unregister, | |
1945 | }; | |
1946 | ||
1947 | static int __init apparmor_nf_ip_init(void) | |
1948 | { | |
1949 | int err; | |
1950 | ||
1951 | if (!apparmor_enabled) | |
1952 | return 0; | |
1953 | ||
1954 | err = register_pernet_subsys(&apparmor_net_ops); | |
1955 | if (err) | |
1956 | panic("Apparmor: register_pernet_subsys: error %d\n", err); | |
1957 | ||
1958 | return 0; | |
1959 | } | |
1960 | __initcall(apparmor_nf_ip_init); | |
e1af4779 | 1961 | #endif |
ab9f2115 | 1962 | |
b5e95b48 JJ |
1963 | static int __init apparmor_init(void) |
1964 | { | |
1965 | int error; | |
1966 | ||
a4c3f89c JJ |
1967 | aa_secids_init(); |
1968 | ||
11c236b8 JJ |
1969 | error = aa_setup_dfa_engine(); |
1970 | if (error) { | |
1971 | AA_ERROR("Unable to setup dfa engine\n"); | |
1972 | goto alloc_out; | |
1973 | } | |
1974 | ||
b5e95b48 JJ |
1975 | error = aa_alloc_root_ns(); |
1976 | if (error) { | |
1977 | AA_ERROR("Unable to allocate default profile namespace\n"); | |
1978 | goto alloc_out; | |
1979 | } | |
1980 | ||
e3ea1ca5 TH |
1981 | error = apparmor_init_sysctl(); |
1982 | if (error) { | |
1983 | AA_ERROR("Unable to register sysctls\n"); | |
1984 | goto alloc_out; | |
1985 | ||
1986 | } | |
1987 | ||
d4669f0b JJ |
1988 | error = alloc_buffers(); |
1989 | if (error) { | |
1990 | AA_ERROR("Unable to allocate work buffers\n"); | |
df323337 | 1991 | goto alloc_out; |
d4669f0b JJ |
1992 | } |
1993 | ||
55a26ebf | 1994 | error = set_init_ctx(); |
b5e95b48 JJ |
1995 | if (error) { |
1996 | AA_ERROR("Failed to set context on init task\n"); | |
b1d9e6b0 | 1997 | aa_free_root_ns(); |
d4669f0b | 1998 | goto buffers_out; |
b5e95b48 | 1999 | } |
d69dece5 | 2000 | security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), |
f4d92e4c | 2001 | &apparmor_lsmid); |
b5e95b48 JJ |
2002 | |
2003 | /* Report that AppArmor successfully initialized */ | |
2004 | apparmor_initialized = 1; | |
2005 | if (aa_g_profile_mode == APPARMOR_COMPLAIN) | |
2006 | aa_info_message("AppArmor initialized: complain mode enabled"); | |
2007 | else if (aa_g_profile_mode == APPARMOR_KILL) | |
2008 | aa_info_message("AppArmor initialized: kill mode enabled"); | |
2009 | else | |
2010 | aa_info_message("AppArmor initialized"); | |
2011 | ||
2012 | return error; | |
2013 | ||
d4669f0b JJ |
2014 | buffers_out: |
2015 | destroy_buffers(); | |
b5e95b48 JJ |
2016 | alloc_out: |
2017 | aa_destroy_aafs(); | |
11c236b8 | 2018 | aa_teardown_dfa_engine(); |
b5e95b48 | 2019 | |
954317fe | 2020 | apparmor_enabled = false; |
b5e95b48 | 2021 | return error; |
b5e95b48 JJ |
2022 | } |
2023 | ||
3d6e5f6d | 2024 | DEFINE_LSM(apparmor) = { |
07aed2f2 | 2025 | .name = "apparmor", |
b1bfb6e2 | 2026 | .flags = LSM_FLAG_LEGACY_MAJOR, |
c5459b82 | 2027 | .enabled = &apparmor_enabled, |
bbd3662a | 2028 | .blobs = &apparmor_blob_sizes, |
3d6e5f6d KC |
2029 | .init = apparmor_init, |
2030 | }; |