Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 2 | /* |
| 3 | * AppArmor security module |
| 4 | * |
| 5 | * This file contains AppArmor resource mediation and attachment |
| 6 | * |
| 7 | * Copyright (C) 1998-2008 Novell/SUSE |
| 8 | * Copyright 2009-2010 Canonical Ltd. |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/audit.h> |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 12 | #include <linux/security.h> |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 13 | |
| 14 | #include "include/audit.h" |
John Johansen | d8889d4 | 2017-10-11 01:04:48 -0700 | [diff] [blame] | 15 | #include "include/cred.h" |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 16 | #include "include/resource.h" |
| 17 | #include "include/policy.h" |
| 18 | |
| 19 | /* |
| 20 | * Table of rlimit names: we generate it from resource.h. |
| 21 | */ |
| 22 | #include "rlim_names.h" |
| 23 | |
John Johansen | c97204b | 2017-05-25 06:23:42 -0700 | [diff] [blame] | 24 | struct aa_sfs_entry aa_sfs_entry_rlimit[] = { |
| 25 | AA_SFS_FILE_STRING("mask", AA_SFS_RLIMIT_MASK), |
Kees Cook | d384b0a | 2012-01-26 16:29:23 -0800 | [diff] [blame] | 26 | { } |
| 27 | }; |
| 28 | |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 29 | /* audit callback for resource specific fields */ |
| 30 | static void audit_cb(struct audit_buffer *ab, void *va) |
| 31 | { |
| 32 | struct common_audit_data *sa = va; |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 33 | struct apparmor_audit_data *ad = aad(sa); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 34 | |
| 35 | audit_log_format(ab, " rlimit=%s value=%lu", |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 36 | rlim_names[ad->rlim.rlim], ad->rlim.max); |
| 37 | if (ad->peer) { |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 38 | audit_log_format(ab, " peer="); |
John Johansen | d20f5a1 | 2022-09-19 00:46:09 -0700 | [diff] [blame] | 39 | aa_label_xaudit(ab, labels_ns(ad->subj_label), ad->peer, |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 40 | FLAGS_NONE, GFP_ATOMIC); |
| 41 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * audit_resource - audit setting resource limit |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 46 | * @subj_cred: cred setting the resource |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 47 | * @profile: profile being enforced (NOT NULL) |
Colin Ian King | 5933a62 | 2017-08-24 09:31:45 +0100 | [diff] [blame] | 48 | * @resource: rlimit being auditing |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 49 | * @value: value being set |
Gaosheng Cui | 1f2bc06 | 2022-09-26 19:48:38 +0800 | [diff] [blame] | 50 | * @peer: aa_albel of the task being set |
| 51 | * @info: info being auditing |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 52 | * @error: error value |
| 53 | * |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 54 | * Returns: 0 or ad->error else other error code on failure |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 55 | */ |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 56 | static int audit_resource(const struct cred *subj_cred, |
| 57 | struct aa_profile *profile, unsigned int resource, |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 58 | unsigned long value, struct aa_label *peer, |
| 59 | const char *info, int error) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 60 | { |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 61 | DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_RLIMITS, |
John Johansen | 8c4b785 | 2022-04-19 16:25:55 -0700 | [diff] [blame] | 62 | OP_SETRLIMIT); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 63 | |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 64 | ad.subj_cred = subj_cred; |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 65 | ad.rlim.rlim = resource; |
| 66 | ad.rlim.max = value; |
| 67 | ad.peer = peer; |
| 68 | ad.info = info; |
| 69 | ad.error = error; |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 70 | |
John Johansen | bd7bd20 | 2022-09-14 00:20:12 -0700 | [diff] [blame] | 71 | return aa_audit(AUDIT_APPARMOR_AUTO, profile, &ad, audit_cb); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Yang Li | d44c692 | 2022-10-14 16:42:55 +0800 | [diff] [blame] | 75 | * aa_map_resource - map compiled policy resource to internal # |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 76 | * @resource: flattened policy resource number |
| 77 | * |
| 78 | * Returns: resource # for the current architecture. |
| 79 | * |
| 80 | * rlimit resource can vary based on architecture, map the compiled policy |
| 81 | * resource # to the internal representation for the architecture. |
| 82 | */ |
| 83 | int aa_map_resource(int resource) |
| 84 | { |
| 85 | return rlim_map[resource]; |
| 86 | } |
| 87 | |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 88 | static int profile_setrlimit(const struct cred *subj_cred, |
| 89 | struct aa_profile *profile, unsigned int resource, |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 90 | struct rlimit *new_rlim) |
| 91 | { |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 92 | struct aa_ruleset *rules = list_first_entry(&profile->rules, |
| 93 | typeof(*rules), list); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 94 | int e = 0; |
| 95 | |
John Johansen | 217af7e | 2022-07-29 17:17:31 -0700 | [diff] [blame] | 96 | if (rules->rlimits.mask & (1 << resource) && new_rlim->rlim_max > |
| 97 | rules->rlimits.limits[resource].rlim_max) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 98 | e = -EACCES; |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 99 | return audit_resource(subj_cred, profile, resource, new_rlim->rlim_max, |
| 100 | NULL, NULL, e); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 101 | } |
| 102 | |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 103 | /** |
| 104 | * aa_task_setrlimit - test permission to set an rlimit |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 105 | * @subj_cred: cred setting the limit |
Gaosheng Cui | 13c1748 | 2023-06-25 09:13:46 +0800 | [diff] [blame] | 106 | * @label: label confining the task (NOT NULL) |
| 107 | * @task: task the resource is being set on |
| 108 | * @resource: the resource being set |
| 109 | * @new_rlim: the new resource limit (NOT NULL) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 110 | * |
| 111 | * Control raising the processes hard limit. |
| 112 | * |
| 113 | * Returns: 0 or error code if setting resource failed |
| 114 | */ |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 115 | int aa_task_setrlimit(const struct cred *subj_cred, struct aa_label *label, |
| 116 | struct task_struct *task, |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 117 | unsigned int resource, struct rlimit *new_rlim) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 118 | { |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 119 | struct aa_profile *profile; |
| 120 | struct aa_label *peer; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 121 | int error = 0; |
| 122 | |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 123 | rcu_read_lock(); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 124 | peer = aa_get_newest_cred_label(__task_cred(task)); |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 125 | rcu_read_unlock(); |
| 126 | |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 127 | /* TODO: extend resource control to handle other (non current) |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 128 | * profiles. AppArmor rules currently have the implicit assumption |
| 129 | * that the task is setting the resource of a task confined with |
Jeff Mahoney | ff11847 | 2015-11-06 15:17:30 -0500 | [diff] [blame] | 130 | * the same profile or that the task setting the resource of another |
| 131 | * task has CAP_SYS_RESOURCE. |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 132 | */ |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 133 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 134 | if (label != peer && |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 135 | aa_capable(subj_cred, label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 136 | error = fn_for_each(label, profile, |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 137 | audit_resource(subj_cred, profile, resource, |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 138 | new_rlim->rlim_max, peer, |
Colin Ian King | 5933a62 | 2017-08-24 09:31:45 +0100 | [diff] [blame] | 139 | "cap_sys_resource", -EACCES)); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 140 | else |
| 141 | error = fn_for_each_confined(label, profile, |
John Johansen | 90c436a | 2022-09-19 20:48:48 -0700 | [diff] [blame] | 142 | profile_setrlimit(subj_cred, profile, resource, |
| 143 | new_rlim)); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 144 | aa_put_label(peer); |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 145 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 146 | return error; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * __aa_transition_rlimits - apply new profile rlimits |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 151 | * @old_l: old label on task (NOT NULL) |
| 152 | * @new_l: new label with rlimits to apply (NOT NULL) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 153 | */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 154 | void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 155 | { |
| 156 | unsigned int mask = 0; |
| 157 | struct rlimit *rlim, *initrlim; |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 158 | struct aa_profile *old, *new; |
| 159 | struct label_it i; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 160 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 161 | old = labels_profile(old_l); |
| 162 | new = labels_profile(new_l); |
| 163 | |
| 164 | /* for any rlimits the profile controlled, reset the soft limit |
| 165 | * to the lesser of the tasks hard limit and the init tasks soft limit |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 166 | */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 167 | label_for_each_confined(i, old_l, old) { |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 168 | struct aa_ruleset *rules = list_first_entry(&old->rules, |
| 169 | typeof(*rules), |
| 170 | list); |
| 171 | if (rules->rlimits.mask) { |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 172 | int j; |
| 173 | |
| 174 | for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, |
| 175 | mask <<= 1) { |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 176 | if (rules->rlimits.mask & mask) { |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 177 | rlim = current->signal->rlim + j; |
| 178 | initrlim = init_task.signal->rlim + j; |
| 179 | rlim->rlim_cur = min(rlim->rlim_max, |
| 180 | initrlim->rlim_cur); |
| 181 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* set any new hard limits as dictated by the new profile */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 187 | label_for_each_confined(i, new_l, new) { |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 188 | struct aa_ruleset *rules = list_first_entry(&new->rules, |
| 189 | typeof(*rules), |
| 190 | list); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 191 | int j; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 192 | |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 193 | if (!rules->rlimits.mask) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 194 | continue; |
| 195 | for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) { |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 196 | if (!(rules->rlimits.mask & mask)) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 197 | continue; |
| 198 | |
| 199 | rlim = current->signal->rlim + j; |
| 200 | rlim->rlim_max = min(rlim->rlim_max, |
John Johansen | 1ad22fc | 2022-09-05 20:47:36 -0700 | [diff] [blame] | 201 | rules->rlimits.limits[j].rlim_max); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 202 | /* soft limit should not exceed hard limit */ |
| 203 | rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max); |
| 204 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 205 | } |
| 206 | } |