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