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