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