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