|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <linux/kernel.h> |
| 3 | +#include <linux/sched.h> |
| 4 | +#include <linux/file.h> |
| 5 | +#include <linux/fs.h> |
| 6 | +#include <linux/cred.h> |
| 7 | +#include <linux/printk.h> |
| 8 | + |
| 9 | +#define TPE_GLOBAL_UID(x) from_kuid_munged(&init_user_ns, (x)) |
| 10 | +#define TPE_GLOBAL_GID(x) from_kgid_munged(&init_user_ns, (x)) |
| 11 | +#define tpe_is_global_root(x) uid_eq((x), GLOBAL_ROOT_UID) |
| 12 | +#define tpe_is_global_nonroot(x) (!uid_eq((x), GLOBAL_ROOT_UID)) |
| 13 | +#define tpe_is_global_nonroot_gid(x) (!gid_eq((x), GLOBAL_ROOT_GID)) |
| 14 | + |
| 15 | +int security_tpe = IS_ENABLED(CONFIG_SECURITY_TPE); |
| 16 | +int security_tpe_all = IS_ENABLED(CONFIG_SECURITY_TPE_ALL); |
| 17 | +int security_tpe_invert = IS_ENABLED(CONFIG_SECURITY_TPE_INVERT); |
| 18 | +kgid_t security_tpe_gid = KGIDT_INIT(CONFIG_SECURITY_TPE_GID); |
| 19 | + |
| 20 | +int |
| 21 | +tpe_allow(const struct file *file) |
| 22 | +{ |
| 23 | + struct inode *inode = d_backing_inode(file->f_path.dentry->d_parent); |
| 24 | + struct inode *file_inode = d_backing_inode(file->f_path.dentry); |
| 25 | + const struct cred *cred = current_cred(); |
| 26 | + char *msg = NULL; |
| 27 | + char *msg2 = NULL; |
| 28 | + |
| 29 | + if (!security_tpe) |
| 30 | + return 1; |
| 31 | + |
| 32 | + // never restrict root |
| 33 | + if (tpe_is_global_root(cred->uid)) |
| 34 | + return 1; |
| 35 | + |
| 36 | + if (security_tpe_all) { |
| 37 | + if (tpe_is_global_nonroot(inode->i_uid) && !uid_eq(inode->i_uid, cred->uid)) |
| 38 | + msg = "directory not owned by user"; |
| 39 | + else if (inode->i_mode & S_IWOTH) |
| 40 | + msg = "file in world-writable directory"; |
| 41 | + else if ((inode->i_mode & S_IWGRP) && tpe_is_global_nonroot_gid(inode->i_gid)) |
| 42 | + msg = "file in group-writable directory"; |
| 43 | + else if (file_inode->i_mode & S_IWOTH) |
| 44 | + msg = "file is world-writable"; |
| 45 | + } else { |
| 46 | + if (security_tpe_invert && !in_group_p(security_tpe_gid)) |
| 47 | + msg2 = "not being in trusted group"; |
| 48 | + else if (!security_tpe_invert && in_group_p(security_tpe_gid)) |
| 49 | + msg2 = "being in untrusted group"; |
| 50 | + else |
| 51 | + return 1; |
| 52 | + |
| 53 | + if (tpe_is_global_nonroot(inode->i_uid)) |
| 54 | + msg = "file in non-root-owned directory"; |
| 55 | + else if (inode->i_mode & S_IWOTH) |
| 56 | + msg = "file in world-writable directory"; |
| 57 | + else if ((inode->i_mode & S_IWGRP) && tpe_is_global_nonroot_gid(inode->i_gid)) |
| 58 | + msg = "file in group-writable directory"; |
| 59 | + else if (file_inode->i_mode & S_IWOTH) |
| 60 | + msg = "file is world-writable"; |
| 61 | + } |
| 62 | + |
| 63 | + if (msg) { |
| 64 | + char fullmsg[70] = {0}; |
| 65 | + |
| 66 | + if (msg2) |
| 67 | + snprintf(fullmsg, sizeof(fullmsg)-1, "%s and %s", msg, msg2); |
| 68 | + else |
| 69 | + snprintf(fullmsg, sizeof(fullmsg)-1, "%s", msg); |
| 70 | + |
| 71 | + pr_warn_ratelimited("TPE: denied attempt to execute file Reason: %s\n", fullmsg); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + return 1; |
| 75 | +} |
0 commit comments