File Index
Files and directories
Sign in
FILE

file.c

File preview with syntax highlighting.

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