aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Johansen <john.johansen@canonical.com>2017-06-09 17:15:20 -0400
committerJohn Johansen <john.johansen@canonical.com>2017-06-10 20:11:40 -0400
commit86b92cb782b38d71ee344af20fcbe5106dd19dbe (patch)
tree65b18b0c407dfca9b76470a55429f2a3d46aeaa2
parentc70c86c421427fd8487867de66c4104b15abd772 (diff)
apparmor: move resource checks to using labels
Signed-off-by: John Johansen <john.johansen@canonical.com>
-rw-r--r--security/apparmor/include/resource.h4
-rw-r--r--security/apparmor/lsm.c6
-rw-r--r--security/apparmor/resource.c112
3 files changed, 80 insertions, 42 deletions
diff --git a/security/apparmor/include/resource.h b/security/apparmor/include/resource.h
index f6289f335c4d..76f1586c9adb 100644
--- a/security/apparmor/include/resource.h
+++ b/security/apparmor/include/resource.h
@@ -37,10 +37,10 @@ struct aa_rlimit {
37extern struct aa_sfs_entry aa_sfs_entry_rlimit[]; 37extern struct aa_sfs_entry aa_sfs_entry_rlimit[];
38 38
39int aa_map_resource(int resource); 39int aa_map_resource(int resource);
40int aa_task_setrlimit(struct aa_profile *profile, struct task_struct *, 40int aa_task_setrlimit(struct aa_label *label, struct task_struct *task,
41 unsigned int resource, struct rlimit *new_rlim); 41 unsigned int resource, struct rlimit *new_rlim);
42 42
43void __aa_transition_rlimits(struct aa_profile *old, struct aa_profile *new); 43void __aa_transition_rlimits(struct aa_label *old, struct aa_label *new);
44 44
45static inline void aa_free_rlimit_rules(struct aa_rlimit *rlims) 45static inline void aa_free_rlimit_rules(struct aa_rlimit *rlims)
46{ 46{
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index bcfdcdb3eae2..c3e98f74268f 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -625,8 +625,7 @@ static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
625 current->pdeath_signal = 0; 625 current->pdeath_signal = 0;
626 626
627 /* reset soft limits and set hard limits for the new label */ 627 /* reset soft limits and set hard limits for the new label */
628 __aa_transition_rlimits(labels_profile(label), 628 __aa_transition_rlimits(label, new_ctx->label);
629 labels_profile(new_ctx->label));
630} 629}
631 630
632/** 631/**
@@ -646,8 +645,7 @@ static int apparmor_task_setrlimit(struct task_struct *task,
646 int error = 0; 645 int error = 0;
647 646
648 if (!unconfined(label)) 647 if (!unconfined(label))
649 error = aa_task_setrlimit(labels_profile(label), task, 648 error = aa_task_setrlimit(label, task, resource, new_rlim);
650 resource, new_rlim);
651 __end_current_label_crit_section(label); 649 __end_current_label_crit_section(label);
652 650
653 return error; 651 return error;
diff --git a/security/apparmor/resource.c b/security/apparmor/resource.c
index 2474ee0b3467..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"
@@ -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_label *task_label; 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_label = aa_get_newest_cred_label((__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 != labels_profile(task_label) &&
103 aa_capable(&profile->label, 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_label(task_label); 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}