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