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