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