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