xref: /linux/security/apparmor/resource.c (revision bd7bd201)
1 // SPDX-License-Identifier: GPL-2.0-only
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.
9  */
10 
11 #include <linux/audit.h>
12 #include <linux/security.h>
13 
14 #include "include/audit.h"
15 #include "include/cred.h"
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 
24 struct aa_sfs_entry aa_sfs_entry_rlimit[] = {
25 	AA_SFS_FILE_STRING("mask", AA_SFS_RLIMIT_MASK),
26 	{ }
27 };
28 
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 	struct apparmor_audit_data *ad = aad(sa);
34 
35 	audit_log_format(ab, " rlimit=%s value=%lu",
36 			 rlim_names[ad->rlim.rlim], ad->rlim.max);
37 	if (ad->peer) {
38 		audit_log_format(ab, " peer=");
39 		aa_label_xaudit(ab, labels_ns(ad->label), ad->peer,
40 				FLAGS_NONE, GFP_ATOMIC);
41 	}
42 }
43 
44 /**
45  * audit_resource - audit setting resource limit
46  * @profile: profile being enforced  (NOT NULL)
47  * @resource: rlimit being auditing
48  * @value: value being set
49  * @peer: aa_albel of the task being set
50  * @info: info being auditing
51  * @error: error value
52  *
53  * Returns: 0 or ad->error else other error code on failure
54  */
55 static int audit_resource(struct aa_profile *profile, unsigned int resource,
56 			  unsigned long value, struct aa_label *peer,
57 			  const char *info, int error)
58 {
59 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_RLIMITS,
60 			  OP_SETRLIMIT);
61 
62 	ad.rlim.rlim = resource;
63 	ad.rlim.max = value;
64 	ad.peer = peer;
65 	ad.info = info;
66 	ad.error = error;
67 
68 	return aa_audit(AUDIT_APPARMOR_AUTO, profile, &ad, audit_cb);
69 }
70 
71 /**
72  * aa_map_resource - 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 
85 static int profile_setrlimit(struct aa_profile *profile, unsigned int resource,
86 			     struct rlimit *new_rlim)
87 {
88 	struct aa_ruleset *rules = list_first_entry(&profile->rules,
89 						    typeof(*rules), list);
90 	int e = 0;
91 
92 	if (rules->rlimits.mask & (1 << resource) && new_rlim->rlim_max >
93 	    rules->rlimits.limits[resource].rlim_max)
94 		e = -EACCES;
95 	return audit_resource(profile, resource, new_rlim->rlim_max, NULL, NULL,
96 			      e);
97 }
98 
99 /**
100  * aa_task_setrlimit - test permission to set an rlimit
101  * @label: label confining the task  (NOT NULL)
102  * @task: task the resource is being set on
103  * @resource: the resource being set
104  * @new_rlim: the new resource limit  (NOT NULL)
105  *
106  * Control raising the processes hard limit.
107  *
108  * Returns: 0 or error code if setting resource failed
109  */
110 int aa_task_setrlimit(struct aa_label *label, struct task_struct *task,
111 		      unsigned int resource, struct rlimit *new_rlim)
112 {
113 	struct aa_profile *profile;
114 	struct aa_label *peer;
115 	int error = 0;
116 
117 	rcu_read_lock();
118 	peer = aa_get_newest_cred_label(__task_cred(task));
119 	rcu_read_unlock();
120 
121 	/* TODO: extend resource control to handle other (non current)
122 	 * profiles.  AppArmor rules currently have the implicit assumption
123 	 * that the task is setting the resource of a task confined with
124 	 * the same profile or that the task setting the resource of another
125 	 * task has CAP_SYS_RESOURCE.
126 	 */
127 
128 	if (label != peer &&
129 	    aa_capable(label, CAP_SYS_RESOURCE, CAP_OPT_NOAUDIT) != 0)
130 		error = fn_for_each(label, profile,
131 				audit_resource(profile, resource,
132 					       new_rlim->rlim_max, peer,
133 					       "cap_sys_resource", -EACCES));
134 	else
135 		error = fn_for_each_confined(label, profile,
136 				profile_setrlimit(profile, resource, new_rlim));
137 	aa_put_label(peer);
138 
139 	return error;
140 }
141 
142 /**
143  * __aa_transition_rlimits - apply new profile rlimits
144  * @old_l: old label on task  (NOT NULL)
145  * @new_l: new label with rlimits to apply  (NOT NULL)
146  */
147 void __aa_transition_rlimits(struct aa_label *old_l, struct aa_label *new_l)
148 {
149 	unsigned int mask = 0;
150 	struct rlimit *rlim, *initrlim;
151 	struct aa_profile *old, *new;
152 	struct label_it i;
153 
154 	old = labels_profile(old_l);
155 	new = labels_profile(new_l);
156 
157 	/* for any rlimits the profile controlled, reset the soft limit
158 	 * to the lesser of the tasks hard limit and the init tasks soft limit
159 	 */
160 	label_for_each_confined(i, old_l, old) {
161 		struct aa_ruleset *rules = list_first_entry(&old->rules,
162 							    typeof(*rules),
163 							    list);
164 		if (rules->rlimits.mask) {
165 			int j;
166 
167 			for (j = 0, mask = 1; j < RLIM_NLIMITS; j++,
168 				     mask <<= 1) {
169 				if (rules->rlimits.mask & mask) {
170 					rlim = current->signal->rlim + j;
171 					initrlim = init_task.signal->rlim + j;
172 					rlim->rlim_cur = min(rlim->rlim_max,
173 							    initrlim->rlim_cur);
174 				}
175 			}
176 		}
177 	}
178 
179 	/* set any new hard limits as dictated by the new profile */
180 	label_for_each_confined(i, new_l, new) {
181 		struct aa_ruleset *rules = list_first_entry(&new->rules,
182 							    typeof(*rules),
183 							    list);
184 		int j;
185 
186 		if (!rules->rlimits.mask)
187 			continue;
188 		for (j = 0, mask = 1; j < RLIM_NLIMITS; j++, mask <<= 1) {
189 			if (!(rules->rlimits.mask & mask))
190 				continue;
191 
192 			rlim = current->signal->rlim + j;
193 			rlim->rlim_max = min(rlim->rlim_max,
194 					     rules->rlimits.limits[j].rlim_max);
195 			/* soft limit should not exceed hard limit */
196 			rlim->rlim_cur = min(rlim->rlim_cur, rlim->rlim_max);
197 		}
198 	}
199 }
200