aboutsummaryrefslogtreecommitdiffstats
path: root/security/apparmor/resource.c
diff options
context:
space:
mode:
Diffstat (limited to 'security/apparmor/resource.c')
-rw-r--r--security/apparmor/resource.c116
1 files changed, 78 insertions, 38 deletions
diff --git a/security/apparmor/resource.c b/security/apparmor/resource.c
index 86a941afd956..d8bc842594ed 100644
--- a/security/apparmor/resource.c
+++ b/security/apparmor/resource.c
@@ -13,6 +13,7 @@
13 */ 13 */
14 14
15#include <linux/audit.h> 15#include <linux/audit.h>
16#include <linux/security.h>
16 17
17#include "include/audit.h" 18#include "include/audit.h"
18#include "include/context.h" 19#include "include/context.h"
@@ -24,8 +25,8 @@
24 */ 25 */
25#include "rlim_names.h" 26#include "rlim_names.h"
26 27
27struct aa_fs_entry aa_fs_entry_rlimit[] = { 28struct aa_sfs_entry aa_sfs_entry_rlimit[] = {
28 AA_FS_FILE_STRING("mask", AA_FS_RLIMIT_MASK), 29 AA_SFS_FILE_STRING("mask", AA_SFS_RLIMIT_MASK),
29 { } 30 { }
30}; 31};
31 32
@@ -36,6 +37,11 @@ static void audit_cb(struct audit_buffer *ab, void *va)
36 37
37 audit_log_format(ab, " rlimit=%s value=%lu", 38 audit_log_format(ab, " rlimit=%s value=%lu",
38 rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max); 39 rlim_names[aad(sa)->rlim.rlim], aad(sa)->rlim.max);
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 }
39} 45}
40 46
41/** 47/**
@@ -48,13 +54,17 @@ static void audit_cb(struct audit_buffer *ab, void *va)
48 * Returns: 0 or sa->error else other error code on failure 54 * Returns: 0 or sa->error else other error code on failure
49 */ 55 */
50static int audit_resource(struct aa_profile *profile, unsigned int resource, 56static int audit_resource(struct aa_profile *profile, unsigned int resource,
51 unsigned long value, int error) 57 unsigned long value, struct aa_label *peer,
58 const char *info, int error)
52{ 59{
53 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT); 60 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETRLIMIT);
54 61
55 aad(&sa)->rlim.rlim = resource; 62 aad(&sa)->rlim.rlim = resource;
56 aad(&sa)->rlim.max = value; 63 aad(&sa)->rlim.max = value;
64 aad(&sa)->peer = peer;
65 aad(&sa)->info = info;
57 aad(&sa)->error = error; 66 aad(&sa)->error = error;
67
58 return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb); 68 return aa_audit(AUDIT_APPARMOR_AUTO, profile, &sa, audit_cb);
59} 69}
60 70
@@ -72,9 +82,21 @@ int aa_map_resource(int resource)
72 return rlim_map[resource]; 82 return rlim_map[resource];
73} 83}
74 84
85static 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
75/** 97/**
76 * aa_task_setrlimit - test permission to set an rlimit 98 * aa_task_setrlimit - test permission to set an rlimit
77 * @profile - profile confining the task (NOT NULL) 99 * @label - label confining the task (NOT NULL)
78 * @task - task the resource is being set on 100 * @task - task the resource is being set on
79 * @resource - the resource being set 101 * @resource - the resource being set
80 * @new_rlim - the new resource limit (NOT NULL) 102 * @new_rlim - the new resource limit (NOT NULL)
@@ -83,14 +105,15 @@ int aa_map_resource(int resource)
83 * 105 *
84 * Returns: 0 or error code if setting resource failed 106 * Returns: 0 or error code if setting resource failed
85 */ 107 */
86int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task, 108int aa_task_setrlimit(struct aa_label *label, struct task_struct *task,
87 unsigned int resource, struct rlimit *new_rlim) 109 unsigned int resource, struct rlimit *new_rlim)
88{ 110{
89 struct aa_profile *task_profile; 111 struct aa_profile *profile;
112 struct aa_label *peer;
90 int error = 0; 113 int error = 0;
91 114
92 rcu_read_lock(); 115 rcu_read_lock();
93 task_profile = aa_get_profile(aa_cred_profile(__task_cred(task))); 116 peer = aa_get_newest_cred_label(__task_cred(task));
94 rcu_read_unlock(); 117 rcu_read_unlock();
95 118
96 /* TODO: extend resource control to handle other (non current) 119 /* TODO: extend resource control to handle other (non current)
@@ -99,53 +122,70 @@ int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *task,
99 * the same profile or that the task setting the resource of another 122 * the same profile or that the task setting the resource of another
100 * task has CAP_SYS_RESOURCE. 123 * task has CAP_SYS_RESOURCE.
101 */ 124 */
102 if ((profile != task_profile &&
103 aa_capable(profile, CAP_SYS_RESOURCE, 1)) ||
104 (profile->rlimits.mask & (1 << resource) &&
105 new_rlim->rlim_max > profile->rlimits.limits[resource].rlim_max))
106 error = -EACCES;
107 125
108 aa_put_profile(task_profile); 126 if (label != peer &&
109 127 !aa_capable(label, CAP_SYS_RESOURCE, SECURITY_CAP_NOAUDIT))
110 return audit_resource(profile, resource, new_rlim->rlim_max, error); 128 error = fn_for_each(label, profile,
129 audit_resource(profile, resource,
130 new_rlim->rlim_max, peer,
131 "cap_sys_resoure", -EACCES));
132 else
133 error = fn_for_each_confined(label, profile,
134 profile_setrlimit(profile, resource, new_rlim));
135 aa_put_label(peer);
136
137 return error;
111} 138}
112 139
113/** 140/**
114 * __aa_transition_rlimits - apply new profile rlimits 141 * __aa_transition_rlimits - apply new profile rlimits
115 * @old: old profile on task (NOT NULL) 142 * @old_l: old label on task (NOT NULL)
116 * @new: new profile with rlimits to apply (NOT NULL) 143 * @new_l: new label with rlimits to apply (NOT NULL)
117 */ 144 */
118void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new) 145void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l)
119{ 146{
120 unsigned int mask = 0; 147 unsigned int mask = 0;
121 struct rlimit *rlim, *initrlim; 148 struct rlimit *rlim, *initrlim;
122 int i; 149 struct aa_profile *old, *new;
150 struct label_it i;
151
152 old = labels_profile(old_l);
153 new = labels_profile(new_l);
123 154
124 /* for any rlimits the profile controlled reset the soft limit 155 /* for any rlimits the profile controlled, reset the soft limit
125 * to the less of the tasks hard limit and the init tasks soft limit 156 * to the lesser of the tasks hard limit and the init tasks soft limit
126 */ 157 */
127 if (old->rlimits.mask) { 158 label_for_each_confined(i, old_l, old) {
128 for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) { 159 if (old->rlimits.mask) {
129 if (old->rlimits.mask & mask) { 160 int j;
130 rlim = current->signal->rlim + i; 161
131 initrlim = init_task.signal->rlim + i; 162 for (j = 0, mask = 1; j < RLIM_NLIMITS; j++,
132 rlim->rlim_cur = min(rlim->rlim_max, 163 mask <<= 1) {
133 initrlim->rlim_cur); 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 }
134 } 170 }
135 } 171 }
136 } 172 }
137 173
138 /* set any new hard limits as dictated by the new profile */ 174 /* set any new hard limits as dictated by the new profile */
139 if (!new->rlimits.mask) 175 label_for_each_confined(i, new_l, new) {
140 return; 176 int j;
141 for (i = 0, mask = 1; i < RLIM_NLIMITS; i++, mask <<= 1) {
142 if (!(new->rlimits.mask & mask))
143 continue;
144 177
145 rlim = current->signal->rlim + i; 178 if (!new->rlimits.mask)
146 rlim->rlim_max = min(rlim->rlim_max, 179 continue;
147 new->rlimits.limits[i].rlim_max); 180 for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) {
148 /* soft limit should not exceed hard limit */ 181 if (!(new->rlimits.mask & mask))
149 rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max); 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 }
150 } 190 }
151} 191}