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; |
| 33 | |
| 34 | audit_log_format(ab, " rlimit=%s value=%lu", |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 35 | rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 36 | if (aad(sa)->peer) { |
| 37 | audit_log_format(ab, " peer="); |
| 38 | aa_label_xaudit(ab, labels_ns(aad(sa)->label), aad(sa)->peer, |
| 39 | FLAGS_NONE, GFP_ATOMIC); |
| 40 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * audit_resource - audit setting resource limit |
| 45 | * @profile: profile being enforced (NOT NULL) |
Colin Ian King | 5933a62 | 2017-08-24 09:31:45 +0100 | [diff] [blame] | 46 | * @resource: rlimit being auditing |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 47 | * @value: value being set |
| 48 | * @error: error value |
| 49 | * |
| 50 | * Returns: 0 or sa->error else other error code on failure |
| 51 | */ |
| 52 | static int audit_resource(struct aa_profile *profile, unsigned int resource, |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 53 | unsigned long value, struct aa_label *peer, |
| 54 | const char *info, int error) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 55 | { |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 56 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 57 | |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 58 | aad(&sa)->rlim.rlim = resource; |
| 59 | aad(&sa)->rlim.max = value; |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 60 | aad(&sa)->peer = peer; |
| 61 | aad(&sa)->info = info; |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 62 | aad(&sa)->error = error; |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 63 | |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 64 | return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * aa_map_resouce - map compiled policy resource to internal # |
| 69 | * @resource: flattened policy resource number |
| 70 | * |
| 71 | * Returns: resource # for the current architecture. |
| 72 | * |
| 73 | * rlimit resource can vary based on architecture, map the compiled policy |
| 74 | * resource # to the internal representation for the architecture. |
| 75 | */ |
| 76 | int aa_map_resource(int resource) |
| 77 | { |
| 78 | return rlim_map[resource]; |
| 79 | } |
| 80 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 81 | static int profile_setrlimit(struct aa_profile *profile, unsigned int resource, |
| 82 | struct rlimit *new_rlim) |
| 83 | { |
| 84 | int e = 0; |
| 85 | |
| 86 | if (profile->rlimits.mask & (1 << resource) && new_rlim->rlim_max > |
| 87 | profile->rlimits.limits[resource].rlim_max) |
| 88 | e = -EACCES; |
| 89 | return audit_resource(profile, resource, new_rlim->rlim_max, NULL, NULL, |
| 90 | e); |
| 91 | } |
| 92 | |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 93 | /** |
| 94 | * aa_task_setrlimit - test permission to set an rlimit |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 95 | * @label - label confining the task (NOT NULL) |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 96 | * @task - task the resource is being set on |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 97 | * @resource - the resource being set |
| 98 | * @new_rlim - the new resource limit (NOT NULL) |
| 99 | * |
| 100 | * Control raising the processes hard limit. |
| 101 | * |
| 102 | * Returns: 0 or error code if setting resource failed |
| 103 | */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 104 | int aa_task_setrlimit(struct aa_label *label, struct task_struct *task, |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 105 | unsigned int resource, struct rlimit *new_rlim) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 106 | { |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 107 | struct aa_profile *profile; |
| 108 | struct aa_label *peer; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 109 | int error = 0; |
| 110 | |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 111 | rcu_read_lock(); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 112 | peer = aa_get_newest_cred_label(__task_cred(task)); |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 113 | rcu_read_unlock(); |
| 114 | |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 115 | /* TODO: extend resource control to handle other (non current) |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 116 | * profiles. AppArmor rules currently have the implicit assumption |
| 117 | * that the task is setting the resource of a task confined with |
Jeff Mahoney | ff11847 | 2015-11-06 15:17:30 -0500 | [diff] [blame] | 118 | * the same profile or that the task setting the resource of another |
| 119 | * task has CAP_SYS_RESOURCE. |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 120 | */ |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 121 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 122 | if (label != peer && |
Micah Morton | c1a85a0 | 2019-01-07 16:10:53 -0800 | [diff] [blame] | 123 | aa_capable(label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 124 | error = fn_for_each(label, profile, |
| 125 | audit_resource(profile, resource, |
| 126 | new_rlim->rlim_max, peer, |
Colin Ian King | 5933a62 | 2017-08-24 09:31:45 +0100 | [diff] [blame] | 127 | "cap_sys_resource", -EACCES)); |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 128 | else |
| 129 | error = fn_for_each_confined(label, profile, |
| 130 | profile_setrlimit(profile, resource, new_rlim)); |
| 131 | aa_put_label(peer); |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 132 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 133 | return error; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * __aa_transition_rlimits - apply new profile rlimits |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 138 | * @old_l: old label on task (NOT NULL) |
| 139 | * @new_l: new label with rlimits to apply (NOT NULL) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 140 | */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 141 | 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] | 142 | { |
| 143 | unsigned int mask = 0; |
| 144 | struct rlimit *rlim, *initrlim; |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 145 | struct aa_profile *old, *new; |
| 146 | struct label_it i; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 147 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 148 | old = labels_profile(old_l); |
| 149 | new = labels_profile(new_l); |
| 150 | |
| 151 | /* for any rlimits the profile controlled, reset the soft limit |
| 152 | * 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] | 153 | */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 154 | label_for_each_confined(i, old_l, old) { |
| 155 | if (old->rlimits.mask) { |
| 156 | int j; |
| 157 | |
| 158 | for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, |
| 159 | mask <<= 1) { |
| 160 | if (old->rlimits.mask & mask) { |
| 161 | rlim = current->signal->rlim + j; |
| 162 | initrlim = init_task.signal->rlim + j; |
| 163 | rlim->rlim_cur = min(rlim->rlim_max, |
| 164 | initrlim->rlim_cur); |
| 165 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* set any new hard limits as dictated by the new profile */ |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 171 | label_for_each_confined(i, new_l, new) { |
| 172 | int j; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 173 | |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 174 | if (!new->rlimits.mask) |
| 175 | continue; |
| 176 | for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) { |
| 177 | if (!(new->rlimits.mask & mask)) |
| 178 | continue; |
| 179 | |
| 180 | rlim = current->signal->rlim + j; |
| 181 | rlim->rlim_max = min(rlim->rlim_max, |
| 182 | new->rlimits.limits[j].rlim_max); |
| 183 | /* soft limit should not exceed hard limit */ |
| 184 | rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max); |
| 185 | } |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 186 | } |
| 187 | } |