xref: /linux/security/apparmor/file.c (revision 3d28e2397af7a89ac3de33c686ed404cda59b5d5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor mediation of files
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  */
10 
11 #include <linux/tty.h>
12 #include <linux/fdtable.h>
13 #include <linux/file.h>
14 #include <linux/fs.h>
15 #include <linux/mount.h>
16 
17 #include "include/af_unix.h"
18 #include "include/apparmor.h"
19 #include "include/audit.h"
20 #include "include/cred.h"
21 #include "include/file.h"
22 #include "include/match.h"
23 #include "include/net.h"
24 #include "include/path.h"
25 #include "include/policy.h"
26 #include "include/label.h"
27 
28 static u32 map_mask_to_chr_mask(u32 mask)
29 {
30 	u32 m = mask & PERMS_CHRS_MASK;
31 
32 	if (mask & AA_MAY_GETATTR)
33 		m |= MAY_READ;
34 	if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
35 		m |= MAY_WRITE;
36 
37 	return m;
38 }
39 
40 /**
41  * file_audit_cb - call back for file specific audit fields
42  * @ab: audit_buffer  (NOT NULL)
43  * @va: audit struct to audit values of  (NOT NULL)
44  */
45 static void file_audit_cb(struct audit_buffer *ab, void *va)
46 {
47 	struct common_audit_data *sa = va;
48 	struct apparmor_audit_data *ad = aad(sa);
49 	kuid_t fsuid = ad->subj_cred ? ad->subj_cred->fsuid : current_fsuid();
50 	char str[10];
51 
52 	if (ad->request & AA_AUDIT_FILE_MASK) {
53 		aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
54 				    map_mask_to_chr_mask(ad->request));
55 		audit_log_format(ab, " requested_mask=\"%s\"", str);
56 	}
57 	if (ad->denied & AA_AUDIT_FILE_MASK) {
58 		aa_perm_mask_to_str(str, sizeof(str), aa_file_perm_chrs,
59 				    map_mask_to_chr_mask(ad->denied));
60 		audit_log_format(ab, " denied_mask=\"%s\"", str);
61 	}
62 	if (ad->request & AA_AUDIT_FILE_MASK) {
63 		audit_log_format(ab, " fsuid=%d",
64 				 from_kuid(&init_user_ns, fsuid));
65 		audit_log_format(ab, " ouid=%d",
66 				 from_kuid(&init_user_ns, ad->fs.ouid));
67 	}
68 
69 	if (ad->peer) {
70 		audit_log_format(ab, " target=");
71 		aa_label_xaudit(ab, labels_ns(ad->subj_label), ad->peer,
72 				FLAG_VIEW_SUBNS, GFP_KERNEL);
73 	} else if (ad->fs.target) {
74 		audit_log_format(ab, " target=");
75 		audit_log_untrustedstring(ab, ad->fs.target);
76 	}
77 }
78 
79 /**
80  * aa_audit_file - handle the auditing of file operations
81  * @subj_cred: cred of the subject
82  * @profile: the profile being enforced  (NOT NULL)
83  * @perms: the permissions computed for the request (NOT NULL)
84  * @op: operation being mediated
85  * @request: permissions requested
86  * @name: name of object being mediated (MAYBE NULL)
87  * @target: name of target (MAYBE NULL)
88  * @tlabel: target label (MAY BE NULL)
89  * @ouid: object uid
90  * @info: extra information message (MAYBE NULL)
91  * @error: 0 if operation allowed else failure error code
92  *
93  * Returns: %0 or error on failure
94  */
95 int aa_audit_file(const struct cred *subj_cred,
96 		  struct aa_profile *profile, struct aa_perms *perms,
97 		  const char *op, u32 request, const char *name,
98 		  const char *target, struct aa_label *tlabel,
99 		  kuid_t ouid, const char *info, int error)
100 {
101 	int type = AUDIT_APPARMOR_AUTO;
102 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_FILE, op);
103 
104 	ad.subj_cred = subj_cred;
105 	ad.request = request;
106 	ad.tags = perms->tag;
107 	ad.name = name;
108 	ad.fs.target = target;
109 	ad.peer = tlabel;
110 	ad.fs.ouid = ouid;
111 	ad.info = info;
112 	ad.error = error;
113 	ad.common.u.tsk = NULL;
114 
115 	if (likely(!ad.error)) {
116 		u32 mask = perms->audit;
117 
118 		if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
119 			mask = 0xffff;
120 
121 		/* mask off perms that are not being force audited */
122 		ad.request &= mask;
123 
124 		if (likely(!ad.request))
125 			return 0;
126 		type = AUDIT_APPARMOR_AUDIT;
127 	} else {
128 		/* only report permissions that were denied */
129 		ad.request = ad.request & ~perms->allow;
130 		AA_BUG(!ad.request);
131 
132 		if (ad.request & perms->kill)
133 			type = AUDIT_APPARMOR_KILL;
134 
135 		/* quiet known rejects, assumes quiet and kill do not overlap */
136 		if ((ad.request & perms->quiet) &&
137 		    AUDIT_MODE(profile) != AUDIT_NOQUIET &&
138 		    AUDIT_MODE(profile) != AUDIT_ALL)
139 			ad.request &= ~perms->quiet;
140 
141 		if (!ad.request)
142 			return ad.error;
143 	}
144 
145 	ad.denied = ad.request & ~perms->allow;
146 	return aa_audit(type, profile, &ad, file_audit_cb);
147 }
148 
149 static int path_name(const char *op, const struct cred *subj_cred,
150 		     struct aa_label *label,
151 		     const struct path *path, int flags, char *buffer,
152 		     const char **name, struct path_cond *cond, u32 request)
153 {
154 	struct aa_profile *profile;
155 	const char *info = NULL;
156 	int error;
157 
158 	error = aa_path_name(path, flags, buffer, name, &info,
159 			     labels_profile(label)->disconnected);
160 	if (error) {
161 		fn_for_each_confined(label, profile,
162 			aa_audit_file(subj_cred,
163 				      profile, &nullperms, op, request, *name,
164 				      NULL, NULL, cond->uid, info, error));
165 		return error;
166 	}
167 
168 	return 0;
169 }
170 
171 struct aa_perms default_perms = {};
172 /**
173  * aa_lookup_condperms - convert dfa compressed perms to internal perms
174  * @subj_uid: uid to use for subject owner test
175  * @rules: the aa_policydb to lookup perms for  (NOT NULL)
176  * @state: state in dfa
177  * @cond:  conditions to consider  (NOT NULL)
178  *
179  * TODO: convert from dfa + state to permission entry
180  *
181  * Returns: a pointer to a file permission set
182  */
183 struct aa_perms *aa_lookup_condperms(kuid_t subj_uid, struct aa_policydb *rules,
184 				     aa_state_t state, struct path_cond *cond)
185 {
186 	unsigned int index = ACCEPT_TABLE(rules->dfa)[state];
187 
188 	if (!(rules->perms))
189 		return &default_perms;
190 
191 	if ((ACCEPT_TABLE2(rules->dfa)[state] & ACCEPT_FLAG_OWNER)) {
192 		if (uid_eq(subj_uid, cond->uid))
193 			return &(rules->perms[index]);
194 		return &(rules->perms[index + 1]);
195 	}
196 
197 	return &(rules->perms[index]);
198 }
199 
200 /**
201  * aa_str_perms - find permission that match @name
202  * @file_rules: the aa_policydb to match against  (NOT NULL)
203  * @start: state to start matching in
204  * @name: string to match against dfa  (NOT NULL)
205  * @cond: conditions to consider for permission set computation  (NOT NULL)
206  * @perms: Returns - the permissions found when matching @name
207  *
208  * Returns: the final state in @dfa when beginning @start and walking @name
209  */
210 aa_state_t aa_str_perms(struct aa_policydb *file_rules, aa_state_t start,
211 			const char *name, struct path_cond *cond,
212 			struct aa_perms *perms)
213 {
214 	aa_state_t state;
215 	state = aa_dfa_match(file_rules->dfa, start, name);
216 	*perms = *(aa_lookup_condperms(current_fsuid(), file_rules, state,
217 				       cond));
218 
219 	return state;
220 }
221 
222 int __aa_path_perm(const char *op, const struct cred *subj_cred,
223 		   struct aa_profile *profile, const char *name,
224 		   u32 request, struct path_cond *cond, int flags,
225 		   struct aa_perms *perms)
226 {
227 	struct aa_ruleset *rules = profile->label.rules[0];
228 	int e = 0;
229 
230 	if (profile_unconfined(profile) ||
231 	    ((flags & PATH_SOCK_COND) && !RULE_MEDIATES_v9NET(rules)))
232 		return 0;
233 	aa_str_perms(rules->file, rules->file->start[AA_CLASS_FILE],
234 		     name, cond, perms);
235 	if (request & ~perms->allow)
236 		e = -EACCES;
237 	return aa_audit_file(subj_cred,
238 			     profile, perms, op, request, name, NULL, NULL,
239 			     cond->uid, NULL, e);
240 }
241 
242 
243 static int profile_path_perm(const char *op, const struct cred *subj_cred,
244 			     struct aa_profile *profile,
245 			     const struct path *path, char *buffer, u32 request,
246 			     struct path_cond *cond, int flags,
247 			     struct aa_perms *perms)
248 {
249 	const char *name;
250 	int error;
251 
252 	if (profile_unconfined(profile))
253 		return 0;
254 
255 	error = path_name(op, subj_cred, &profile->label, path,
256 			  flags | profile->path_flags, buffer, &name, cond,
257 			  request);
258 	if (error)
259 		return error;
260 	return __aa_path_perm(op, subj_cred, profile, name, request, cond,
261 			      flags, perms);
262 }
263 
264 /**
265  * aa_path_perm - do permissions check & audit for @path
266  * @op: operation being checked
267  * @subj_cred: subject cred
268  * @label: profile being enforced  (NOT NULL)
269  * @path: path to check permissions of  (NOT NULL)
270  * @flags: any additional path flags beyond what the profile specifies
271  * @request: requested permissions
272  * @cond: conditional info for this request  (NOT NULL)
273  *
274  * Returns: %0 else error if access denied or other error
275  */
276 int aa_path_perm(const char *op, const struct cred *subj_cred,
277 		 struct aa_label *label,
278 		 const struct path *path, int flags, u32 request,
279 		 struct path_cond *cond)
280 {
281 	struct aa_perms perms = {};
282 	struct aa_profile *profile;
283 	char *buffer = NULL;
284 	int error;
285 
286 	flags |= PATH_DELEGATE_DELETED | (S_ISDIR(cond->mode) ? PATH_IS_DIR :
287 								0);
288 	buffer = aa_get_buffer(false);
289 	if (!buffer)
290 		return -ENOMEM;
291 	error = fn_for_each_confined(label, profile,
292 			profile_path_perm(op, subj_cred, profile, path, buffer,
293 					  request, cond, flags, &perms));
294 
295 	aa_put_buffer(buffer);
296 
297 	return error;
298 }
299 
300 /**
301  * xindex_is_subset - helper for aa_path_link
302  * @link: link permission set
303  * @target: target permission set
304  *
305  * test target x permissions are equal OR a subset of link x permissions
306  * this is done as part of the subset test, where a hardlink must have
307  * a subset of permissions that the target has.
308  *
309  * Returns: true if subset else false
310  */
311 static inline bool xindex_is_subset(u32 link, u32 target)
312 {
313 	if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
314 	    ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
315 		return false;
316 
317 	return true;
318 }
319 
320 static int profile_path_link(const struct cred *subj_cred,
321 			     struct aa_profile *profile,
322 			     const struct path *link, char *buffer,
323 			     const struct path *target, char *buffer2,
324 			     struct path_cond *cond)
325 {
326 	struct aa_ruleset *rules = profile->label.rules[0];
327 	const char *lname, *tname = NULL;
328 	struct aa_perms lperms = {}, perms;
329 	const char *info = NULL;
330 	u32 request = AA_MAY_LINK;
331 	aa_state_t state;
332 	int error;
333 
334 	error = path_name(OP_LINK, subj_cred, &profile->label, link,
335 			  profile->path_flags,
336 			  buffer, &lname, cond, AA_MAY_LINK);
337 	if (error)
338 		goto audit;
339 
340 	/* buffer2 freed below, tname is pointer in buffer2 */
341 	error = path_name(OP_LINK, subj_cred, &profile->label, target,
342 			  profile->path_flags,
343 			  buffer2, &tname, cond, AA_MAY_LINK);
344 	if (error)
345 		goto audit;
346 
347 	error = -EACCES;
348 	/* aa_str_perms - handles the case of the dfa being NULL */
349 	state = aa_str_perms(rules->file,
350 			     rules->file->start[AA_CLASS_FILE], lname,
351 			     cond, &lperms);
352 
353 	if (!(lperms.allow & AA_MAY_LINK))
354 		goto audit;
355 
356 	/* test to see if target can be paired with link */
357 	state = aa_dfa_null_transition(rules->file->dfa, state);
358 	aa_str_perms(rules->file, state, tname, cond, &perms);
359 
360 	/* force audit/quiet masks for link are stored in the second entry
361 	 * in the link pair.
362 	 */
363 	lperms.audit = perms.audit;
364 	lperms.quiet = perms.quiet;
365 	lperms.kill = perms.kill;
366 
367 	if (!(perms.allow & AA_MAY_LINK)) {
368 		info = "target restricted";
369 		lperms = perms;
370 		goto audit;
371 	}
372 
373 	/* done if link subset test is not required */
374 	if (!(perms.allow & AA_LINK_SUBSET))
375 		goto done_tests;
376 
377 	/* Do link perm subset test requiring allowed permission on link are
378 	 * a subset of the allowed permissions on target.
379 	 */
380 	aa_str_perms(rules->file, rules->file->start[AA_CLASS_FILE],
381 		     tname, cond, &perms);
382 
383 	/* AA_MAY_LINK is not considered in the subset test */
384 	request = lperms.allow & ~AA_MAY_LINK;
385 	lperms.allow &= perms.allow | AA_MAY_LINK;
386 
387 	request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
388 	if (request & ~lperms.allow) {
389 		goto audit;
390 	} else if ((lperms.allow & MAY_EXEC) &&
391 		   !xindex_is_subset(lperms.xindex, perms.xindex)) {
392 		lperms.allow &= ~MAY_EXEC;
393 		request |= MAY_EXEC;
394 		info = "link not subset of target";
395 		goto audit;
396 	}
397 
398 done_tests:
399 	error = 0;
400 
401 audit:
402 	return aa_audit_file(subj_cred,
403 			     profile, &lperms, OP_LINK, request, lname, tname,
404 			     NULL, cond->uid, info, error);
405 }
406 
407 /**
408  * aa_path_link - Handle hard link permission check
409  * @subj_cred: subject cred
410  * @label: the label being enforced  (NOT NULL)
411  * @old_dentry: the target dentry  (NOT NULL)
412  * @new_dir: directory the new link will be created in  (NOT NULL)
413  * @new_dentry: the link being created  (NOT NULL)
414  *
415  * Handle the permission test for a link & target pair.  Permission
416  * is encoded as a pair where the link permission is determined
417  * first, and if allowed, the target is tested.  The target test
418  * is done from the point of the link match (not start of DFA)
419  * making the target permission dependent on the link permission match.
420  *
421  * The subset test if required forces that permissions granted
422  * on link are a subset of the permission granted to target.
423  *
424  * Returns: %0 if allowed else error
425  */
426 int aa_path_link(const struct cred *subj_cred,
427 		 struct aa_label *label, struct dentry *old_dentry,
428 		 const struct path *new_dir, struct dentry *new_dentry)
429 {
430 	struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
431 	struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
432 	struct inode *inode = d_backing_inode(old_dentry);
433 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(target.mnt), inode);
434 	struct path_cond cond = {
435 		.uid = vfsuid_into_kuid(vfsuid),
436 		.mode = inode->i_mode,
437 	};
438 	char *buffer = NULL, *buffer2 = NULL;
439 	struct aa_profile *profile;
440 	int error;
441 
442 	/* buffer freed below, lname is pointer in buffer */
443 	buffer = aa_get_buffer(false);
444 	buffer2 = aa_get_buffer(false);
445 	error = -ENOMEM;
446 	if (!buffer || !buffer2)
447 		goto out;
448 
449 	error = fn_for_each_confined(label, profile,
450 			profile_path_link(subj_cred, profile, &link, buffer,
451 					  &target, buffer2, &cond));
452 out:
453 	aa_put_buffer(buffer);
454 	aa_put_buffer(buffer2);
455 	return error;
456 }
457 
458 static void update_file_ctx(struct aa_file_ctx *fctx, struct aa_label *label,
459 			    u32 request)
460 {
461 	struct aa_label *l, *old;
462 
463 	/* update caching of label on file_ctx */
464 	spin_lock(&fctx->lock);
465 	old = rcu_dereference_protected(fctx->label,
466 					lockdep_is_held(&fctx->lock));
467 	l = aa_label_merge(old, label, GFP_ATOMIC);
468 	if (l) {
469 		if (l != old) {
470 			rcu_assign_pointer(fctx->label, l);
471 			aa_put_label(old);
472 		} else
473 			aa_put_label(l);
474 		fctx->allow |= request;
475 	}
476 	spin_unlock(&fctx->lock);
477 }
478 
479 static int __file_path_perm(const char *op, const struct cred *subj_cred,
480 			    struct aa_label *label,
481 			    struct aa_label *flabel, struct file *file,
482 			    u32 request, u32 denied, bool in_atomic)
483 {
484 	struct aa_profile *profile;
485 	struct aa_perms perms = {};
486 	vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(file),
487 					    file_inode(file));
488 	struct path_cond cond = {
489 		.uid = vfsuid_into_kuid(vfsuid),
490 		.mode = file_inode(file)->i_mode
491 	};
492 	char *buffer;
493 	int flags, error;
494 
495 	/* revalidation due to label out of date. No revocation at this time */
496 	if (!denied && aa_label_is_subset(flabel, label))
497 		/* TODO: check for revocation on stale profiles */
498 		return 0;
499 
500 	flags = PATH_DELEGATE_DELETED | (S_ISDIR(cond.mode) ? PATH_IS_DIR : 0);
501 	buffer = aa_get_buffer(in_atomic);
502 	if (!buffer)
503 		return -ENOMEM;
504 
505 	/* check every profile in task label not in current cache */
506 	error = fn_for_each_not_in_set(flabel, label, profile,
507 			profile_path_perm(op, subj_cred, profile,
508 					  &file->f_path, buffer,
509 					  request, &cond, flags, &perms));
510 	if (denied && !error) {
511 		/*
512 		 * check every profile in file label that was not tested
513 		 * in the initial check above.
514 		 *
515 		 * TODO: cache full perms so this only happens because of
516 		 * conditionals
517 		 * TODO: don't audit here
518 		 */
519 		if (label == flabel)
520 			error = fn_for_each(label, profile,
521 				profile_path_perm(op, subj_cred,
522 						  profile, &file->f_path,
523 						  buffer, request, &cond, flags,
524 						  &perms));
525 		else
526 			error = fn_for_each_not_in_set(label, flabel, profile,
527 				profile_path_perm(op, subj_cred,
528 						  profile, &file->f_path,
529 						  buffer, request, &cond, flags,
530 						  &perms));
531 	}
532 	if (!error)
533 		update_file_ctx(file_ctx(file), label, request);
534 
535 	aa_put_buffer(buffer);
536 
537 	return error;
538 }
539 
540 static int __file_sock_perm(const char *op, const struct cred *subj_cred,
541 			    struct aa_label *label,
542 			    struct aa_label *flabel, struct file *file,
543 			    u32 request, u32 denied)
544 {
545 	int error;
546 
547 	/* revalidation due to label out of date. No revocation at this time */
548 	if (!denied && aa_label_is_subset(flabel, label))
549 		return 0;
550 
551 	/* TODO: improve to skip profiles cached in flabel */
552 	error = aa_sock_file_perm(subj_cred, label, op, request, file);
553 	if (denied) {
554 		/* TODO: improve to skip profiles checked above */
555 		/* check every profile in file label to is cached */
556 		last_error(error, aa_sock_file_perm(subj_cred, flabel, op,
557 						    request, file));
558 	}
559 	if (!error)
560 		update_file_ctx(file_ctx(file), label, request);
561 
562 	return error;
563 }
564 
565 /* for now separate fn to indicate semantics of the check */
566 static bool __file_is_delegated(struct aa_label *obj_label)
567 {
568 	return unconfined(obj_label);
569 }
570 
571 static bool __unix_needs_revalidation(struct file *file, struct aa_label *label,
572 				      u32 request)
573 {
574 	struct socket *sock = (struct socket *) file->private_data;
575 
576 	lockdep_assert_in_rcu_read_lock();
577 
578 	if (!S_ISSOCK(file_inode(file)->i_mode))
579 		return false;
580 	if (request & NET_PEER_MASK)
581 		return false;
582 	/* sock and sock->sk can be NULL for sockets being set up or torn down */
583 	if (!sock || !sock->sk)
584 		return false;
585 	if (sock->sk->sk_family == PF_UNIX) {
586 		struct aa_sk_ctx *ctx = aa_sock(sock->sk);
587 
588 		if (rcu_access_pointer(ctx->peer) !=
589 		    rcu_access_pointer(ctx->peer_lastupdate))
590 			return true;
591 		return !__aa_subj_label_is_cached(rcu_dereference(ctx->label),
592 						  label);
593 	}
594 	return false;
595 }
596 
597 /**
598  * aa_file_perm - do permission revalidation check & audit for @file
599  * @op: operation being checked
600  * @subj_cred: subject cred
601  * @label: label being enforced   (NOT NULL)
602  * @file: file to revalidate access permissions on  (NOT NULL)
603  * @request: requested permissions
604  * @in_atomic: whether allocations need to be done in atomic context
605  *
606  * Returns: %0 if access allowed else error
607  */
608 int aa_file_perm(const char *op, const struct cred *subj_cred,
609 		 struct aa_label *label, struct file *file,
610 		 u32 request, bool in_atomic)
611 {
612 	struct aa_file_ctx *fctx;
613 	struct aa_label *flabel;
614 	u32 denied;
615 	int error = 0;
616 
617 	AA_BUG(!label);
618 	AA_BUG(!file);
619 
620 	fctx = file_ctx(file);
621 
622 	rcu_read_lock();
623 	flabel  = rcu_dereference(fctx->label);
624 	AA_BUG(!flabel);
625 
626 	/* revalidate access, if task is unconfined, or the cached cred
627 	 * doesn't match or if the request is for more permissions than
628 	 * was granted.
629 	 *
630 	 * Note: the test for !unconfined(flabel) is to handle file
631 	 *       delegation from unconfined tasks
632 	 */
633 	denied = request & ~fctx->allow;
634 	if (unconfined(label) || __file_is_delegated(flabel) ||
635 	    __unix_needs_revalidation(file, label, request) ||
636 	    (!denied && __aa_subj_label_is_cached(label, flabel))) {
637 		rcu_read_unlock();
638 		goto done;
639 	}
640 
641 	/* slow path - revalidate access */
642 	flabel  = aa_get_newest_label(flabel);
643 	rcu_read_unlock();
644 
645 	if (path_mediated_fs(file->f_path.dentry))
646 		error = __file_path_perm(op, subj_cred, label, flabel, file,
647 					 request, denied, in_atomic);
648 
649 	else if (S_ISSOCK(file_inode(file)->i_mode))
650 		error = __file_sock_perm(op, subj_cred, label, flabel, file,
651 					 request, denied);
652 	aa_put_label(flabel);
653 
654 done:
655 	return error;
656 }
657 
658 static void revalidate_tty(const struct cred *subj_cred, struct aa_label *label)
659 {
660 	struct tty_struct *tty;
661 	int drop_tty = 0;
662 
663 	tty = get_current_tty();
664 	if (!tty)
665 		return;
666 
667 	spin_lock(&tty->files_lock);
668 	if (!list_empty(&tty->tty_files)) {
669 		struct tty_file_private *file_priv;
670 		struct file *file;
671 		/* TODO: Revalidate access to controlling tty. */
672 		file_priv = list_first_entry(&tty->tty_files,
673 					     struct tty_file_private, list);
674 		file = file_priv->file;
675 
676 		if (aa_file_perm(OP_INHERIT, subj_cred, label, file,
677 				 MAY_READ | MAY_WRITE, IN_ATOMIC))
678 			drop_tty = 1;
679 	}
680 	spin_unlock(&tty->files_lock);
681 	tty_kref_put(tty);
682 
683 	if (drop_tty)
684 		no_tty();
685 }
686 
687 struct cred_label {
688 	const struct cred *cred;
689 	struct aa_label *label;
690 };
691 
692 static int match_file(const void *p, struct file *file, unsigned int fd)
693 {
694 	struct cred_label *cl = (struct cred_label *)p;
695 
696 	if (aa_file_perm(OP_INHERIT, cl->cred, cl->label, file,
697 			 aa_map_file_to_perms(file), IN_ATOMIC))
698 		return fd + 1;
699 	return 0;
700 }
701 
702 
703 /* based on selinux's flush_unauthorized_files */
704 void aa_inherit_files(const struct cred *cred, struct files_struct *files)
705 {
706 	struct aa_label *label = aa_get_newest_cred_label(cred);
707 	struct cred_label cl = {
708 		.cred = cred,
709 		.label = label,
710 	};
711 	struct file *devnull = NULL;
712 	unsigned int n;
713 
714 	revalidate_tty(cred, label);
715 
716 	/* Revalidate access to inherited open files. */
717 	n = iterate_fd(files, 0, match_file, &cl);
718 	if (!n) /* none found? */
719 		goto out;
720 
721 	devnull = dentry_open(&aa_null, O_RDWR, cred);
722 	if (IS_ERR(devnull))
723 		devnull = NULL;
724 	/* replace all the matching ones with this */
725 	do {
726 		replace_fd(n - 1, devnull, 0);
727 	} while ((n = iterate_fd(files, n, match_file, &cl)) != 0);
728 	if (devnull)
729 		fput(devnull);
730 out:
731 	aa_put_label(label);
732 }
733