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