xref: /linux-6.15/security/apparmor/task.c (revision a7e405a2)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2de62de59SJohn Johansen /*
3de62de59SJohn Johansen  * AppArmor security module
4de62de59SJohn Johansen  *
5de62de59SJohn Johansen  * This file contains AppArmor task related definitions and mediation
6de62de59SJohn Johansen  *
7de62de59SJohn Johansen  * Copyright 2017 Canonical Ltd.
8de62de59SJohn Johansen  *
9de62de59SJohn Johansen  * TODO
10de62de59SJohn Johansen  * If a task uses change_hat it currently does not return to the old
11de62de59SJohn Johansen  * cred or task context but instead creates a new one.  Ideally the task
12de62de59SJohn Johansen  * should return to the previous cred if it has not been modified.
13de62de59SJohn Johansen  */
14de62de59SJohn Johansen 
15eac93125SJohn Johansen #include <linux/gfp.h>
16eac93125SJohn Johansen #include <linux/ptrace.h>
17eac93125SJohn Johansen 
18eac93125SJohn Johansen #include "include/audit.h"
19d8889d49SJohn Johansen #include "include/cred.h"
20eac93125SJohn Johansen #include "include/policy.h"
21de62de59SJohn Johansen #include "include/task.h"
22de62de59SJohn Johansen 
23de62de59SJohn Johansen /**
24de62de59SJohn Johansen  * aa_get_task_label - Get another task's label
25de62de59SJohn Johansen  * @task: task to query  (NOT NULL)
26de62de59SJohn Johansen  *
27de62de59SJohn Johansen  * Returns: counted reference to @task's label
28de62de59SJohn Johansen  */
aa_get_task_label(struct task_struct * task)29de62de59SJohn Johansen struct aa_label *aa_get_task_label(struct task_struct *task)
30de62de59SJohn Johansen {
31de62de59SJohn Johansen 	struct aa_label *p;
32de62de59SJohn Johansen 
33de62de59SJohn Johansen 	rcu_read_lock();
34adaa9a3fSGaosheng Cui 	p = aa_get_newest_cred_label(__task_cred(task));
35de62de59SJohn Johansen 	rcu_read_unlock();
36de62de59SJohn Johansen 
37de62de59SJohn Johansen 	return p;
38de62de59SJohn Johansen }
39de62de59SJohn Johansen 
40de62de59SJohn Johansen /**
41de62de59SJohn Johansen  * aa_replace_current_label - replace the current tasks label
42de62de59SJohn Johansen  * @label: new label  (NOT NULL)
43de62de59SJohn Johansen  *
44de62de59SJohn Johansen  * Returns: 0 or error on failure
45de62de59SJohn Johansen  */
aa_replace_current_label(struct aa_label * label)46de62de59SJohn Johansen int aa_replace_current_label(struct aa_label *label)
47de62de59SJohn Johansen {
48de62de59SJohn Johansen 	struct aa_label *old = aa_current_raw_label();
499fcf78ccSJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
50de62de59SJohn Johansen 	struct cred *new;
51de62de59SJohn Johansen 
52de62de59SJohn Johansen 	AA_BUG(!label);
53de62de59SJohn Johansen 
54de62de59SJohn Johansen 	if (old == label)
55de62de59SJohn Johansen 		return 0;
56de62de59SJohn Johansen 
57de62de59SJohn Johansen 	if (current_cred() != current_real_cred())
58de62de59SJohn Johansen 		return -EBUSY;
59de62de59SJohn Johansen 
60de62de59SJohn Johansen 	new  = prepare_creds();
61de62de59SJohn Johansen 	if (!new)
62de62de59SJohn Johansen 		return -ENOMEM;
63de62de59SJohn Johansen 
649fcf78ccSJohn Johansen 	if (ctx->nnp && label_is_stale(ctx->nnp)) {
659fcf78ccSJohn Johansen 		struct aa_label *tmp = ctx->nnp;
669fcf78ccSJohn Johansen 
679fcf78ccSJohn Johansen 		ctx->nnp = aa_get_newest_label(tmp);
689fcf78ccSJohn Johansen 		aa_put_label(tmp);
699fcf78ccSJohn Johansen 	}
70de62de59SJohn Johansen 	if (unconfined(label) || (labels_ns(old) != labels_ns(label)))
71de62de59SJohn Johansen 		/*
72de62de59SJohn Johansen 		 * if switching to unconfined or a different label namespace
73de62de59SJohn Johansen 		 * clear out context state
74de62de59SJohn Johansen 		 */
75de62de59SJohn Johansen 		aa_clear_task_ctx_trans(task_ctx(current));
76de62de59SJohn Johansen 
77de62de59SJohn Johansen 	/*
78de62de59SJohn Johansen 	 * be careful switching cred label, when racing replacement it
79de62de59SJohn Johansen 	 * is possible that the cred labels's->proxy->label is the reference
80de62de59SJohn Johansen 	 * keeping @label valid, so make sure to get its reference before
81de62de59SJohn Johansen 	 * dropping the reference on the cred's label
82de62de59SJohn Johansen 	 */
83de62de59SJohn Johansen 	aa_get_label(label);
84de62de59SJohn Johansen 	aa_put_label(cred_label(new));
8569b5a44aSCasey Schaufler 	set_cred_label(new, label);
86de62de59SJohn Johansen 
87de62de59SJohn Johansen 	commit_creds(new);
88de62de59SJohn Johansen 	return 0;
89de62de59SJohn Johansen }
90de62de59SJohn Johansen 
91de62de59SJohn Johansen 
92de62de59SJohn Johansen /**
93de62de59SJohn Johansen  * aa_set_current_onexec - set the tasks change_profile to happen onexec
94de62de59SJohn Johansen  * @label: system label to set at exec  (MAYBE NULL to clear value)
95de62de59SJohn Johansen  * @stack: whether stacking should be done
96de62de59SJohn Johansen  */
aa_set_current_onexec(struct aa_label * label,bool stack)970897fcb1SQuanfa Fu void aa_set_current_onexec(struct aa_label *label, bool stack)
98de62de59SJohn Johansen {
99de62de59SJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
100de62de59SJohn Johansen 
101de62de59SJohn Johansen 	aa_get_label(label);
102de62de59SJohn Johansen 	aa_put_label(ctx->onexec);
103de62de59SJohn Johansen 	ctx->onexec = label;
104de62de59SJohn Johansen 	ctx->token = stack;
105de62de59SJohn Johansen }
106de62de59SJohn Johansen 
107de62de59SJohn Johansen /**
108de62de59SJohn Johansen  * aa_set_current_hat - set the current tasks hat
109de62de59SJohn Johansen  * @label: label to set as the current hat  (NOT NULL)
110de62de59SJohn Johansen  * @token: token value that must be specified to change from the hat
111de62de59SJohn Johansen  *
112de62de59SJohn Johansen  * Do switch of tasks hat.  If the task is currently in a hat
113de62de59SJohn Johansen  * validate the token to match.
114de62de59SJohn Johansen  *
115de62de59SJohn Johansen  * Returns: 0 or error on failure
116de62de59SJohn Johansen  */
aa_set_current_hat(struct aa_label * label,u64 token)117de62de59SJohn Johansen int aa_set_current_hat(struct aa_label *label, u64 token)
118de62de59SJohn Johansen {
119de62de59SJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
120de62de59SJohn Johansen 	struct cred *new;
121de62de59SJohn Johansen 
122de62de59SJohn Johansen 	new = prepare_creds();
123de62de59SJohn Johansen 	if (!new)
124de62de59SJohn Johansen 		return -ENOMEM;
125de62de59SJohn Johansen 	AA_BUG(!label);
126de62de59SJohn Johansen 
127de62de59SJohn Johansen 	if (!ctx->previous) {
128de62de59SJohn Johansen 		/* transfer refcount */
129de62de59SJohn Johansen 		ctx->previous = cred_label(new);
130de62de59SJohn Johansen 		ctx->token = token;
131de62de59SJohn Johansen 	} else if (ctx->token == token) {
132de62de59SJohn Johansen 		aa_put_label(cred_label(new));
133de62de59SJohn Johansen 	} else {
134de62de59SJohn Johansen 		/* previous_profile && ctx->token != token */
135de62de59SJohn Johansen 		abort_creds(new);
136de62de59SJohn Johansen 		return -EACCES;
137de62de59SJohn Johansen 	}
138de62de59SJohn Johansen 
13969b5a44aSCasey Schaufler 	set_cred_label(new, aa_get_newest_label(label));
140de62de59SJohn Johansen 	/* clear exec on switching context */
141de62de59SJohn Johansen 	aa_put_label(ctx->onexec);
142de62de59SJohn Johansen 	ctx->onexec = NULL;
143de62de59SJohn Johansen 
144de62de59SJohn Johansen 	commit_creds(new);
145de62de59SJohn Johansen 	return 0;
146de62de59SJohn Johansen }
147de62de59SJohn Johansen 
148de62de59SJohn Johansen /**
149de62de59SJohn Johansen  * aa_restore_previous_label - exit from hat context restoring previous label
150de62de59SJohn Johansen  * @token: the token that must be matched to exit hat context
151de62de59SJohn Johansen  *
152de62de59SJohn Johansen  * Attempt to return out of a hat to the previous label.  The token
153de62de59SJohn Johansen  * must match the stored token value.
154de62de59SJohn Johansen  *
155de62de59SJohn Johansen  * Returns: 0 or error of failure
156de62de59SJohn Johansen  */
aa_restore_previous_label(u64 token)157de62de59SJohn Johansen int aa_restore_previous_label(u64 token)
158de62de59SJohn Johansen {
159de62de59SJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
160de62de59SJohn Johansen 	struct cred *new;
161de62de59SJohn Johansen 
162de62de59SJohn Johansen 	if (ctx->token != token)
163de62de59SJohn Johansen 		return -EACCES;
164de62de59SJohn Johansen 	/* ignore restores when there is no saved label */
165de62de59SJohn Johansen 	if (!ctx->previous)
166de62de59SJohn Johansen 		return 0;
167de62de59SJohn Johansen 
168de62de59SJohn Johansen 	new = prepare_creds();
169de62de59SJohn Johansen 	if (!new)
170de62de59SJohn Johansen 		return -ENOMEM;
171de62de59SJohn Johansen 
172de62de59SJohn Johansen 	aa_put_label(cred_label(new));
17369b5a44aSCasey Schaufler 	set_cred_label(new, aa_get_newest_label(ctx->previous));
174de62de59SJohn Johansen 	AA_BUG(!cred_label(new));
175de62de59SJohn Johansen 	/* clear exec && prev information when restoring to previous context */
176de62de59SJohn Johansen 	aa_clear_task_ctx_trans(ctx);
177de62de59SJohn Johansen 
178de62de59SJohn Johansen 	commit_creds(new);
179de62de59SJohn Johansen 
180de62de59SJohn Johansen 	return 0;
181de62de59SJohn Johansen }
182eac93125SJohn Johansen 
183eac93125SJohn Johansen /**
184eac93125SJohn Johansen  * audit_ptrace_mask - convert mask to permission string
185eac93125SJohn Johansen  * @mask: permission mask to convert
186eac93125SJohn Johansen  *
187eac93125SJohn Johansen  * Returns: pointer to static string
188eac93125SJohn Johansen  */
audit_ptrace_mask(u32 mask)189eac93125SJohn Johansen static const char *audit_ptrace_mask(u32 mask)
190eac93125SJohn Johansen {
191eac93125SJohn Johansen 	switch (mask) {
192eac93125SJohn Johansen 	case MAY_READ:
193eac93125SJohn Johansen 		return "read";
194eac93125SJohn Johansen 	case MAY_WRITE:
195eac93125SJohn Johansen 		return "trace";
196eac93125SJohn Johansen 	case AA_MAY_BE_READ:
197eac93125SJohn Johansen 		return "readby";
198eac93125SJohn Johansen 	case AA_MAY_BE_TRACED:
199eac93125SJohn Johansen 		return "tracedby";
200eac93125SJohn Johansen 	}
201eac93125SJohn Johansen 	return "";
202eac93125SJohn Johansen }
203eac93125SJohn Johansen 
204eac93125SJohn Johansen /* call back to audit ptrace fields */
audit_ptrace_cb(struct audit_buffer * ab,void * va)205eac93125SJohn Johansen static void audit_ptrace_cb(struct audit_buffer *ab, void *va)
206eac93125SJohn Johansen {
207eac93125SJohn Johansen 	struct common_audit_data *sa = va;
208bd7bd201SJohn Johansen 	struct apparmor_audit_data *ad = aad(sa);
209eac93125SJohn Johansen 
210bd7bd201SJohn Johansen 	if (ad->request & AA_PTRACE_PERM_MASK) {
211eac93125SJohn Johansen 		audit_log_format(ab, " requested_mask=\"%s\"",
212bd7bd201SJohn Johansen 				 audit_ptrace_mask(ad->request));
213eac93125SJohn Johansen 
214bd7bd201SJohn Johansen 		if (ad->denied & AA_PTRACE_PERM_MASK) {
215eac93125SJohn Johansen 			audit_log_format(ab, " denied_mask=\"%s\"",
216bd7bd201SJohn Johansen 					 audit_ptrace_mask(ad->denied));
217eac93125SJohn Johansen 		}
218eac93125SJohn Johansen 	}
219eac93125SJohn Johansen 	audit_log_format(ab, " peer=");
220d20f5a1aSJohn Johansen 	aa_label_xaudit(ab, labels_ns(ad->subj_label), ad->peer,
221eac93125SJohn Johansen 			FLAGS_NONE, GFP_ATOMIC);
222eac93125SJohn Johansen }
223eac93125SJohn Johansen 
224217af7e2SJohn Johansen /* assumes check for RULE_MEDIATES is already done */
225eac93125SJohn Johansen /* TODO: conditionals */
profile_ptrace_perm(const struct cred * cred,struct aa_profile * profile,struct aa_label * peer,u32 request,struct apparmor_audit_data * ad)22690c436a6SJohn Johansen static int profile_ptrace_perm(const struct cred *cred,
22790c436a6SJohn Johansen 			       struct aa_profile *profile,
228eac93125SJohn Johansen 			       struct aa_label *peer, u32 request,
229bd7bd201SJohn Johansen 			       struct apparmor_audit_data *ad)
230eac93125SJohn Johansen {
2311ad22fccSJohn Johansen 	struct aa_ruleset *rules = list_first_entry(&profile->rules,
2321ad22fccSJohn Johansen 						    typeof(*rules), list);
233eac93125SJohn Johansen 	struct aa_perms perms = { };
234eac93125SJohn Johansen 
23590c436a6SJohn Johansen 	ad->subj_cred = cred;
236bd7bd201SJohn Johansen 	ad->peer = peer;
2371ad22fccSJohn Johansen 	aa_profile_match_label(profile, rules, peer, AA_CLASS_PTRACE, request,
2381ad22fccSJohn Johansen 			       &perms);
239eac93125SJohn Johansen 	aa_apply_modes_to_perms(profile, &perms);
240bd7bd201SJohn Johansen 	return aa_check_perms(profile, &perms, request, ad, audit_ptrace_cb);
241eac93125SJohn Johansen }
242eac93125SJohn Johansen 
profile_tracee_perm(const struct cred * cred,struct aa_profile * tracee,struct aa_label * tracer,u32 request,struct apparmor_audit_data * ad)24390c436a6SJohn Johansen static int profile_tracee_perm(const struct cred *cred,
24490c436a6SJohn Johansen 			       struct aa_profile *tracee,
245eac93125SJohn Johansen 			       struct aa_label *tracer, u32 request,
246bd7bd201SJohn Johansen 			       struct apparmor_audit_data *ad)
247eac93125SJohn Johansen {
248eac93125SJohn Johansen 	if (profile_unconfined(tracee) || unconfined(tracer) ||
2491ad22fccSJohn Johansen 	    !ANY_RULE_MEDIATES(&tracee->rules, AA_CLASS_PTRACE))
250eac93125SJohn Johansen 		return 0;
251eac93125SJohn Johansen 
25290c436a6SJohn Johansen 	return profile_ptrace_perm(cred, tracee, tracer, request, ad);
253eac93125SJohn Johansen }
254eac93125SJohn Johansen 
profile_tracer_perm(const struct cred * cred,struct aa_profile * tracer,struct aa_label * tracee,u32 request,struct apparmor_audit_data * ad)25590c436a6SJohn Johansen static int profile_tracer_perm(const struct cred *cred,
25690c436a6SJohn Johansen 			       struct aa_profile *tracer,
257eac93125SJohn Johansen 			       struct aa_label *tracee, u32 request,
258bd7bd201SJohn Johansen 			       struct apparmor_audit_data *ad)
259eac93125SJohn Johansen {
260eac93125SJohn Johansen 	if (profile_unconfined(tracer))
261eac93125SJohn Johansen 		return 0;
262eac93125SJohn Johansen 
2631ad22fccSJohn Johansen 	if (ANY_RULE_MEDIATES(&tracer->rules, AA_CLASS_PTRACE))
26490c436a6SJohn Johansen 		return profile_ptrace_perm(cred, tracer, tracee, request, ad);
265eac93125SJohn Johansen 
266eac93125SJohn Johansen 	/* profile uses the old style capability check for ptrace */
267eac93125SJohn Johansen 	if (&tracer->label == tracee)
268eac93125SJohn Johansen 		return 0;
269eac93125SJohn Johansen 
270d20f5a1aSJohn Johansen 	ad->subj_label = &tracer->label;
271bd7bd201SJohn Johansen 	ad->peer = tracee;
272bd7bd201SJohn Johansen 	ad->request = 0;
27390c436a6SJohn Johansen 	ad->error = aa_capable(cred, &tracer->label, CAP_SYS_PTRACE,
274eac93125SJohn Johansen 			       CAP_OPT_NONE);
275eac93125SJohn Johansen 
276bd7bd201SJohn Johansen 	return aa_audit(AUDIT_APPARMOR_AUTO, tracer, ad, audit_ptrace_cb);
277eac93125SJohn Johansen }
278eac93125SJohn Johansen 
279eac93125SJohn Johansen /**
280eac93125SJohn Johansen  * aa_may_ptrace - test if tracer task can trace the tracee
281*a7e405a2SJohn Johansen  * @tracer_cred: cred of task doing the tracing  (NOT NULL)
282eac93125SJohn Johansen  * @tracer: label of the task doing the tracing  (NOT NULL)
283*a7e405a2SJohn Johansen  * @tracee_cred: cred of task to be traced
284eac93125SJohn Johansen  * @tracee: task label to be traced
285eac93125SJohn Johansen  * @request: permission request
286eac93125SJohn Johansen  *
287eac93125SJohn Johansen  * Returns: %0 else error code if permission denied or error
288eac93125SJohn Johansen  */
aa_may_ptrace(const struct cred * tracer_cred,struct aa_label * tracer,const struct cred * tracee_cred,struct aa_label * tracee,u32 request)28990c436a6SJohn Johansen int aa_may_ptrace(const struct cred *tracer_cred, struct aa_label *tracer,
29090c436a6SJohn Johansen 		  const struct cred *tracee_cred, struct aa_label *tracee,
291eac93125SJohn Johansen 		  u32 request)
292eac93125SJohn Johansen {
293eac93125SJohn Johansen 	struct aa_profile *profile;
294eac93125SJohn Johansen 	u32 xrequest = request << PTRACE_PERM_SHIFT;
2958c4b785aSJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_PTRACE, OP_PTRACE);
296eac93125SJohn Johansen 
297eac93125SJohn Johansen 	return xcheck_labels(tracer, tracee, profile,
29890c436a6SJohn Johansen 			profile_tracer_perm(tracer_cred, profile, tracee,
29990c436a6SJohn Johansen 					    request, &sa),
30090c436a6SJohn Johansen 			profile_tracee_perm(tracee_cred, profile, tracer,
30190c436a6SJohn Johansen 					    xrequest, &sa));
302eac93125SJohn Johansen }
303fa9b63adSJohn Johansen 
304fa9b63adSJohn Johansen /* call back to audit ptrace fields */
audit_ns_cb(struct audit_buffer * ab,void * va)305fa9b63adSJohn Johansen static void audit_ns_cb(struct audit_buffer *ab, void *va)
306fa9b63adSJohn Johansen {
307fa9b63adSJohn Johansen 	struct apparmor_audit_data *ad = aad_of_va(va);
308fa9b63adSJohn Johansen 
309fa9b63adSJohn Johansen 	if (ad->request & AA_USERNS_CREATE)
310fa9b63adSJohn Johansen 		audit_log_format(ab, " requested=\"userns_create\"");
311fa9b63adSJohn Johansen 
312fa9b63adSJohn Johansen 	if (ad->denied & AA_USERNS_CREATE)
313fa9b63adSJohn Johansen 		audit_log_format(ab, " denied=\"userns_create\"");
314fa9b63adSJohn Johansen }
315fa9b63adSJohn Johansen 
aa_profile_ns_perm(struct aa_profile * profile,struct apparmor_audit_data * ad,u32 request)316fa9b63adSJohn Johansen int aa_profile_ns_perm(struct aa_profile *profile,
317fa9b63adSJohn Johansen 		       struct apparmor_audit_data *ad,
318fa9b63adSJohn Johansen 		       u32 request)
319fa9b63adSJohn Johansen {
320fa9b63adSJohn Johansen 	struct aa_perms perms = { };
321fa9b63adSJohn Johansen 	int error = 0;
322fa9b63adSJohn Johansen 
323fa9b63adSJohn Johansen 	ad->subj_label = &profile->label;
324fa9b63adSJohn Johansen 	ad->request = request;
325fa9b63adSJohn Johansen 
326fa9b63adSJohn Johansen 	if (!profile_unconfined(profile)) {
327fa9b63adSJohn Johansen 		struct aa_ruleset *rules = list_first_entry(&profile->rules,
328fa9b63adSJohn Johansen 							    typeof(*rules),
329fa9b63adSJohn Johansen 							    list);
330fa9b63adSJohn Johansen 		aa_state_t state;
331fa9b63adSJohn Johansen 
332fa9b63adSJohn Johansen 		state = RULE_MEDIATES(rules, ad->class);
333fa9b63adSJohn Johansen 		if (!state)
334fa9b63adSJohn Johansen 			/* TODO: add flag to complain about unmediated */
335fa9b63adSJohn Johansen 			return 0;
336fa9b63adSJohn Johansen 		perms = *aa_lookup_perms(rules->policy, state);
337fa9b63adSJohn Johansen 		aa_apply_modes_to_perms(profile, &perms);
338fa9b63adSJohn Johansen 		error = aa_check_perms(profile, &perms, request, ad,
339fa9b63adSJohn Johansen 				       audit_ns_cb);
340fa9b63adSJohn Johansen 	}
341fa9b63adSJohn Johansen 
342fa9b63adSJohn Johansen 	return error;
343fa9b63adSJohn Johansen }
344