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> |
| 16 | |
| 17 | #include "include/audit.h" |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 18 | #include "include/context.h" |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 19 | #include "include/resource.h" |
| 20 | #include "include/policy.h" |
| 21 | |
| 22 | /* |
| 23 | * Table of rlimit names: we generate it from resource.h. |
| 24 | */ |
| 25 | #include "rlim_names.h" |
| 26 | |
Kees Cook | d384b0a | 2012-01-26 16:29:23 -0800 | [diff] [blame] | 27 | struct aa_fs_entry aa_fs_entry_rlimit[] = { |
| 28 | AA_FS_FILE_STRING("mask", AA_FS_RLIMIT_MASK), |
| 29 | { } |
| 30 | }; |
| 31 | |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 32 | /* audit callback for resource specific fields */ |
| 33 | static void audit_cb(struct audit_buffer *ab, void *va) |
| 34 | { |
| 35 | struct common_audit_data *sa = va; |
| 36 | |
| 37 | audit_log_format(ab, " rlimit=%s value=%lu", |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 38 | rlim_names[sa->aad->rlim.rlim], sa->aad->rlim.max); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * audit_resource - audit setting resource limit |
| 43 | * @profile: profile being enforced (NOT NULL) |
| 44 | * @resoure: rlimit being auditing |
| 45 | * @value: value being set |
| 46 | * @error: error value |
| 47 | * |
| 48 | * Returns: 0 or sa->error else other error code on failure |
| 49 | */ |
| 50 | static int audit_resource(struct aa_profile *profile, unsigned int resource, |
| 51 | unsigned long value, int error) |
| 52 | { |
| 53 | struct common_audit_data sa; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 54 | struct apparmor_audit_data aad = {0,}; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 55 | |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 56 | sa.type = LSM_AUDIT_DATA_NONE; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 57 | sa.aad = &aad; |
| 58 | aad.op = OP_SETRLIMIT, |
| 59 | aad.rlim.rlim = resource; |
| 60 | aad.rlim.max = value; |
| 61 | aad.error = error; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 62 | return aa_audit(AUDIT_APPARMOR_AUTO, profile, GFP_KERNEL, &sa, |
| 63 | audit_cb); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * aa_map_resouce - map compiled policy resource to internal # |
| 68 | * @resource: flattened policy resource number |
| 69 | * |
| 70 | * Returns: resource # for the current architecture. |
| 71 | * |
| 72 | * rlimit resource can vary based on architecture, map the compiled policy |
| 73 | * resource # to the internal representation for the architecture. |
| 74 | */ |
| 75 | int aa_map_resource(int resource) |
| 76 | { |
| 77 | return rlim_map[resource]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * aa_task_setrlimit - test permission to set an rlimit |
| 82 | * @profile - profile confining the task (NOT NULL) |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 83 | * @task - task the resource is being set on |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 84 | * @resource - the resource being set |
| 85 | * @new_rlim - the new resource limit (NOT NULL) |
| 86 | * |
| 87 | * Control raising the processes hard limit. |
| 88 | * |
| 89 | * Returns: 0 or error code if setting resource failed |
| 90 | */ |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 91 | int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task, |
| 92 | unsigned int resource, struct rlimit *new_rlim) |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 93 | { |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 94 | struct aa_profile *task_profile; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 95 | int error = 0; |
| 96 | |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 97 | rcu_read_lock(); |
| 98 | task_profile = aa_get_profile(aa_cred_profile(__task_cred(task))); |
| 99 | rcu_read_unlock(); |
| 100 | |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 101 | /* TODO: extend resource control to handle other (non current) |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 102 | * profiles. AppArmor rules currently have the implicit assumption |
| 103 | * that the task is setting the resource of a task confined with |
| 104 | * the same profile. |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 105 | */ |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 106 | if (profile != task_profile || |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 107 | (profile->rlimits.mask & (1 << resource) && |
| 108 | new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max)) |
| 109 | error = -EACCES; |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 110 | |
John Johansen | cf47aed | 2013-02-18 16:07:34 -0800 | [diff] [blame] | 111 | aa_put_profile(task_profile); |
| 112 | |
John Johansen | 3a2dc83 | 2010-09-06 10:10:20 -0700 | [diff] [blame] | 113 | return audit_resource(profile, resource, new_rlim->rlim_max, error); |
John Johansen | 0ed3b28 | 2010-07-29 14:48:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * __aa_transition_rlimits - apply new profile rlimits |
| 118 | * @old: old profile on task (NOT NULL) |
| 119 | * @new: new profile with rlimits to apply (NOT NULL) |
| 120 | */ |
| 121 | void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new) |
| 122 | { |
| 123 | unsigned int mask = 0; |
| 124 | struct rlimit *rlim, *initrlim; |
| 125 | int i; |
| 126 | |
| 127 | /* for any rlimits the profile controlled reset the soft limit |
| 128 | * to the less of the tasks hard limit and the init tasks soft limit |
| 129 | */ |
| 130 | if (old->rlimits.mask) { |
| 131 | for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) { |
| 132 | if (old->rlimits.mask & mask) { |
| 133 | rlim = current->signal->rlim + i; |
| 134 | initrlim = init_task.signal->rlim + i; |
| 135 | rlim->rlim_cur = min(rlim->rlim_max, |
| 136 | initrlim->rlim_cur); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* set any new hard limits as dictated by the new profile */ |
| 142 | if (!new->rlimits.mask) |
| 143 | return; |
| 144 | for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) { |
| 145 | if (!(new->rlimits.mask & mask)) |
| 146 | continue; |
| 147 | |
| 148 | rlim = current->signal->rlim + i; |
| 149 | rlim->rlim_max = min(rlim->rlim_max, |
| 150 | new->rlimits.limits[i].rlim_max); |
| 151 | /* soft limit should not exceed hard limit */ |
| 152 | rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max); |
| 153 | } |
| 154 | } |