|
12 | 12 | * License. |
13 | 13 | */ |
14 | 14 |
|
| 15 | +#include <linux/tty.h> |
| 16 | +#include <linux/fdtable.h> |
| 17 | +#include <linux/file.h> |
| 18 | + |
15 | 19 | #include "include/apparmor.h" |
16 | 20 | #include "include/audit.h" |
| 21 | +#include "include/context.h" |
17 | 22 | #include "include/file.h" |
18 | 23 | #include "include/match.h" |
19 | 24 | #include "include/path.h" |
@@ -445,3 +450,70 @@ int aa_file_perm(const char *op, struct aa_profile *profile, struct file *file, |
445 | 450 | return aa_path_perm(op, profile, &file->f_path, PATH_DELEGATE_DELETED, |
446 | 451 | request, &cond); |
447 | 452 | } |
| 453 | + |
| 454 | +static void revalidate_tty(struct aa_profile *profile) |
| 455 | +{ |
| 456 | + struct tty_struct *tty; |
| 457 | + int drop_tty = 0; |
| 458 | + |
| 459 | + tty = get_current_tty(); |
| 460 | + if (!tty) |
| 461 | + return; |
| 462 | + |
| 463 | + spin_lock(&tty->files_lock); |
| 464 | + if (!list_empty(&tty->tty_files)) { |
| 465 | + struct tty_file_private *file_priv; |
| 466 | + struct file *file; |
| 467 | + /* TODO: Revalidate access to controlling tty. */ |
| 468 | + file_priv = list_first_entry(&tty->tty_files, |
| 469 | + struct tty_file_private, list); |
| 470 | + file = file_priv->file; |
| 471 | + |
| 472 | + if (aa_file_perm(OP_INHERIT, profile, file, |
| 473 | + MAY_READ | MAY_WRITE)) |
| 474 | + drop_tty = 1; |
| 475 | + } |
| 476 | + spin_unlock(&tty->files_lock); |
| 477 | + tty_kref_put(tty); |
| 478 | + |
| 479 | + if (drop_tty) |
| 480 | + no_tty(); |
| 481 | +} |
| 482 | + |
| 483 | +static int match_file(const void *p, struct file *file, unsigned int fd) |
| 484 | +{ |
| 485 | + struct aa_profile *profile = (struct aa_profile *)p; |
| 486 | + |
| 487 | + if (aa_file_perm(OP_INHERIT, profile, file, |
| 488 | + aa_map_file_to_perms(file))) |
| 489 | + return fd + 1; |
| 490 | + return 0; |
| 491 | +} |
| 492 | + |
| 493 | + |
| 494 | +/* based on selinux's flush_unauthorized_files */ |
| 495 | +void aa_inherit_files(const struct cred *cred, struct files_struct *files) |
| 496 | +{ |
| 497 | + struct aa_profile *profile = aa_get_newest_cred_profile(cred); |
| 498 | + struct file *devnull = NULL; |
| 499 | + unsigned int n; |
| 500 | + |
| 501 | + revalidate_tty(profile); |
| 502 | + |
| 503 | + /* Revalidate access to inherited open files. */ |
| 504 | + n = iterate_fd(files, 0, match_file, profile); |
| 505 | + if (!n) /* none found? */ |
| 506 | + goto out; |
| 507 | + |
| 508 | + devnull = dentry_open(&aa_null, O_RDWR, cred); |
| 509 | + if (IS_ERR(devnull)) |
| 510 | + devnull = NULL; |
| 511 | + /* replace all the matching ones with this */ |
| 512 | + do { |
| 513 | + replace_fd(n - 1, devnull, 0); |
| 514 | + } while ((n = iterate_fd(files, n, match_file, profile)) != 0); |
| 515 | + if (devnull) |
| 516 | + fput(devnull); |
| 517 | +out: |
| 518 | + aa_put_profile(profile); |
| 519 | +} |
0 commit comments