lockdown,selinux: fix wrong subject in some SELinux lockdown checks
Subsystems (36)
PATCH v4 1/1 lockdown,selinux: fix wrong subject in some SELinux lockdown checks
Commit 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown") added an implementation of the locked_down LSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.
However, in several places the security_locked_down() hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.
To fix this, add an explicit struct cred pointer argument to security_lockdown() and define NULL as a special value to pass instead of current_cred() in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, use SECINITSID_KERNEL in case the cred argument is NULL.
Most of the callers are updated to pass current_cred() as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1. arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink message XFRM_MSG_GETSA with NLM_F_DUMP set - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA via XFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead of current_cred() here faute de mieux.
---
v4:
- rebase on top of TODO
- fix rebase conflicts:
* drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
* kernel/bpf/helpers.c, kernel/trace/bpf_trace.c
- trivial: LOCKDOWN_BPF_READ was renamed to LOCKDOWN_BPF_READ_KERNEL in mainline
* kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition in hibernation_available()
- cover new security_locked_down() call in kernel/bpf/helpers.c (LOCKDOWN_BPF_WRITE_USER in BPF_FUNC_probe_write_user case)
v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument to security_locked_down() and adapt all callers
- keep using current_cred() in BPF, as the hook calls have been shifted to program load time (commit ff40e51043af ("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but use SECINITSID_KERNEL as the subject instead
- update explanations in the commit message
v2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schaufler
v1: https://lore.kernel.org/lkml/[email protected]/
arch/powerpc/xmon/xmon.c | 4 ++-- arch/x86/kernel/ioport.c | 4 ++-- arch/x86/kernel/msr.c | 4 ++-- arch/x86/mm/testmmiotrace.c | 2 +- drivers/acpi/acpi_configfs.c | 2 +- drivers/acpi/custom_method.c | 2 +- drivers/acpi/osl.c | 3 ++- drivers/acpi/tables.c | 2 +- drivers/char/mem.c | 2 +- drivers/cxl/pci.c | 2 +- drivers/firmware/efi/efi.c | 2 +- drivers/firmware/efi/test/efi_test.c | 2 +- drivers/pci/pci-sysfs.c | 6 +++--- drivers/pci/proc.c | 6 +++--- drivers/pci/syscall.c | 2 +- drivers/pcmcia/cistpl.c | 2 +- drivers/tty/serial/serial_core.c | 2 +- fs/debugfs/file.c | 2 +- fs/debugfs/inode.c | 2 +- fs/proc/kcore.c | 2 +- fs/tracefs/inode.c | 2 +- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 1 + include/linux/security.h | 4 ++-- kernel/bpf/helpers.c | 10 ++++++---- kernel/events/core.c | 2 +- kernel/kexec.c | 2 +- kernel/kexec_file.c | 2 +- kernel/module.c | 2 +- kernel/params.c | 2 +- kernel/power/hibernate.c | 2 +- kernel/trace/bpf_trace.c | 25 +++++++++++++++---------- kernel/trace/ftrace.c | 4 ++-- kernel/trace/ring_buffer.c | 2 +- kernel/trace/trace.c | 10 +++++----- kernel/trace/trace_events.c | 2 +- kernel/trace/trace_events_hist.c | 4 ++-- kernel/trace/trace_events_synth.c | 2 +- kernel/trace/trace_events_trigger.c | 2 +- kernel/trace/trace_kprobe.c | 6 +++--- kernel/trace/trace_printk.c | 2 +- kernel/trace/trace_stack.c | 2 +- kernel/trace/trace_stat.c | 2 +- kernel/trace/trace_uprobe.c | 4 ++-- net/xfrm/xfrm_user.c | 11 +++++++++-- security/lockdown/lockdown.c | 3 ++- security/security.c | 4 ++-- security/selinux/hooks.c | 7 +++++-- 48 files changed, 99 insertions(+), 79 deletions(-)
arch/powerpc/xmon/xmon.c +2 -2
@@ -309,7 +309,7 @@ static bool xmon_is_locked_down(void)static bool lockdown;if (!lockdown) { - lockdown = !!security_locked_down (LOCKDOWN_XMON_RW ); + lockdown = !!security_locked_down (NULL ,LOCKDOWN_XMON_RW );if (lockdown) {printf ("xmon: Disabled due to kernel lockdown\n" ); xmon_is_ro = true;
@@ -317,7 +317,7 @@ static bool xmon_is_locked_down(void) }if (!xmon_is_ro) { - xmon_is_ro = !!security_locked_down (LOCKDOWN_XMON_WR ); + xmon_is_ro = !!security_locked_down (NULL ,LOCKDOWN_XMON_WR );if (xmon_is_ro)printf ("xmon: Read-only due to kernel lockdown\n" ); }
arch/x86/kernel/ioport.c +2 -2
@@ -71,7 +71,7 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)if ((from + num <= from) || (from + num >IO_BITMAP_BITS ))return -EINVAL ;if (turn_on && (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_IOPORT ))) +security_locked_down (current_cred (),LOCKDOWN_IOPORT )))return -EPERM ;/*
@@ -187,7 +187,7 @@ SYSCALL_DEFINE1(iopl, unsigned int, level)/* Trying to gain more privileges? */ if (level > old) {if (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_IOPORT )) +security_locked_down (current_cred (),LOCKDOWN_IOPORT ))return -EPERM ; }
arch/x86/kernel/msr.c +2 -2
@@ -116,7 +116,7 @@ static ssize_t msr_write(struct file *file, const char __user *buf,int err =0 ;ssize_t bytes =0 ; - err =security_locked_down (LOCKDOWN_MSR ); + err =security_locked_down (current_cred (),LOCKDOWN_MSR );if (err)return err;
@@ -179,7 +179,7 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) err = -EFAULT ;break ; } - err =security_locked_down (LOCKDOWN_MSR ); + err =security_locked_down (current_cred (),LOCKDOWN_MSR );if (err)break ;
arch/x86/mm/testmmiotrace.c +1 -1
@@ -116,7 +116,7 @@ static void do_test_bulk_ioremapping(void)static int __init init (void ) {unsigned long size = (read_far) ? (8 <<20 ) : (16 <<10 ); -int ret =security_locked_down (LOCKDOWN_MMIOTRACE ); +int ret =security_locked_down (current_cred (),LOCKDOWN_MMIOTRACE );if (ret)return ret;
drivers/acpi/acpi_configfs.c +1 -1
@@ -26,7 +26,7 @@ static ssize_t acpi_table_aml_write(struct config_item *cfg, {const struct acpi_table_header *header = data;struct acpi_table *table; -int ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); +int ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;
drivers/acpi/custom_method.c +1 -1
@@ -30,7 +30,7 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,acpi_status status;int ret; - ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); + ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;
drivers/acpi/osl.c +2 -1
@@ -198,7 +198,8 @@ acpi_physical_address __init acpi_os_get_root_pointer(void)* specific location (if appropriate) so it can be carried * over further kexec()s. */ -if (acpi_rsdp && !security_locked_down (LOCKDOWN_ACPI_TABLES )) { +if (acpi_rsdp && !security_locked_down (current_cred (), +LOCKDOWN_ACPI_TABLES )) {acpi_arch_set_root_pointer (acpi_rsdp);return acpi_rsdp; }
drivers/acpi/tables.c +1 -1
@@ -577,7 +577,7 @@ void __init acpi_table_upgrade(void)if (table_nr ==0 )return ; -if (security_locked_down (LOCKDOWN_ACPI_TABLES )) { +if (security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES )) {pr_notice ("kernel is locked down, ignoring table override\n" );return ; }
drivers/char/mem.c +1 -1
@@ -617,7 +617,7 @@ static int open_port(struct inode *inode, struct file *filp)if (!capable (CAP_SYS_RAWIO ))return -EPERM ; - rc =security_locked_down (LOCKDOWN_DEV_MEM ); + rc =security_locked_down (current_cred (),LOCKDOWN_DEV_MEM );if (rc)return rc;
drivers/cxl/pci.c +1 -1
@@ -575,7 +575,7 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)if (!IS_ENABLED (CONFIG_CXL_MEM_RAW_COMMANDS ))return false; -if (security_locked_down (LOCKDOWN_PCI_ACCESS )) +if (security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return false;if (cxl_raw_allow_all)
drivers/firmware/efi/efi.c +1 -1
@@ -200,7 +200,7 @@ static void generic_ops_unregister(void)static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX ]__initdata ;static int __init efivar_ssdt_setup (char *str) { -int ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); +int ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;
drivers/firmware/efi/test/efi_test.c +1 -1
@@ -722,7 +722,7 @@ static long efi_test_ioctl(struct file *file, unsigned int cmd,static int efi_test_open (struct inode *inode,struct file *file) { -int ret =security_locked_down (LOCKDOWN_EFI_TEST ); +int ret =security_locked_down (current_cred (),LOCKDOWN_EFI_TEST );if (ret)return ret;
drivers/pci/pci-sysfs.c +3 -3
@@ -753,7 +753,7 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,u8 *data = (u8 *) buf;int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;
@@ -1047,7 +1047,7 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,struct resource *res = &pdev->resource[bar];int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;
@@ -1128,7 +1128,7 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, {int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;
drivers/pci/proc.c +3 -3
@@ -119,7 +119,7 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,int size = dev->cfg_size;int cnt, ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;
@@ -202,7 +202,7 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,#endif /* HAVE_PCI_MMAP */ int ret =0 ; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;
@@ -249,7 +249,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)int i, ret, write_combine =0 , res_bit =IORESOURCE_MEM ;if (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_PCI_ACCESS )) +security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return -EPERM ;if (fpriv->mmap_state == pci_mmap_io) {
drivers/pci/syscall.c +1 -1
@@ -93,7 +93,7 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,int err =0 ;if (!capable (CAP_SYS_ADMIN ) || -security_locked_down (LOCKDOWN_PCI_ACCESS )) +security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return -EPERM ; dev =pci_get_domain_bus_and_slot (0 , bus, dfn);
drivers/pcmcia/cistpl.c +1 -1
@@ -1577,7 +1577,7 @@ static ssize_t pccard_store_cis(struct file *filp, struct kobject *kobj,struct pcmcia_socket *s;int error; - error =security_locked_down (LOCKDOWN_PCMCIA_CIS ); + error =security_locked_down (current_cred (),LOCKDOWN_PCMCIA_CIS );if (error)return error;
drivers/tty/serial/serial_core.c +1 -1
@@ -840,7 +840,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port, }if (change_irq || change_port) { - retval =security_locked_down (LOCKDOWN_TIOCSSERIAL ); + retval =security_locked_down (current_cred (),LOCKDOWN_TIOCSSERIAL );if (retval) goto exit; }
fs/debugfs/file.c +1 -1
@@ -154,7 +154,7 @@ static int debugfs_locked_down(struct inode *inode, !real_fops->mmap)return 0 ; -if (security_locked_down (LOCKDOWN_DEBUGFS )) +if (security_locked_down (current_cred (),LOCKDOWN_DEBUGFS ))return -EPERM ;return 0 ;
fs/debugfs/inode.c +1 -1
@@ -48,7 +48,7 @@ static int debugfs_setattr(struct user_namespace *mnt_userns,int ret;if (ia->ia_valid & (ATTR_MODE |ATTR_UID |ATTR_GID )) { - ret =security_locked_down (LOCKDOWN_DEBUGFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_DEBUGFS );if (ret)return ret; }
fs/proc/kcore.c +1 -1
@@ -586,7 +586,7 @@ out:static int open_kcore (struct inode *inode,struct file *filp) { -int ret =security_locked_down (LOCKDOWN_KCORE ); +int ret =security_locked_down (current_cred (),LOCKDOWN_KCORE );if (!capable (CAP_SYS_RAWIO ))return -EPERM ;
fs/tracefs/inode.c +1 -1
@@ -396,7 +396,7 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,struct dentry *dentry;struct inode *inode; -if (security_locked_down (LOCKDOWN_TRACEFS )) +if (security_locked_down (NULL ,LOCKDOWN_TRACEFS ))return NULL ;if (!(mode &S_IFMT ))
include/linux/lsm_hook_defs.h +1 -1
@@ -393,7 +393,7 @@ LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux) LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux)#endif /* CONFIG_BPF_SYSCALL */ -LSM_HOOK(int, 0, locked_down, enum lockdown_reason what) +LSM_HOOK(int, 0, locked_down, const struct cred *cred, enum lockdown_reason what)#ifdef CONFIG_PERF_EVENTS LSM_HOOK(int, 0, perf_event_open, struct perf_event_attr *attr, int type)
include/linux/lsm_hooks.h +1 -0
@@ -1543,6 +1543,7 @@* Determine whether a kernel feature that potentially enables arbitrary * code execution in kernel space should be permitted. * +* @cred: credential asociated with the operation, or NULL if not applicable * @what: kernel feature being accessed * * Security hooks for perf events
include/linux/security.h +2 -2
@@ -471,7 +471,7 @@ void security_inode_invalidate_secctx(struct inode *inode);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); -int security_locked_down (enum lockdown_reason what); +int security_locked_down (const struct cred *cred,enum lockdown_reason what);#else /* CONFIG_SECURITY */ static inline int call_blocking_lsm_notifier (enum lsm_event event,void *data)
@@ -1344,7 +1344,7 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 {return -EOPNOTSUPP ; } -static inline int security_locked_down (enum lockdown_reason what) +static inline int security_locked_down (struct cred *cred,enum lockdown_reason what) {return 0 ; }
kernel/bpf/helpers.c +6 -4
@@ -1424,13 +1424,15 @@ bpf_base_func_proto(enum bpf_func_id func_id)case BPF_FUNC_probe_read_user:return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_proto;case BPF_FUNC_probe_read_user_str:return &bpf_probe_read_user_str_proto;case BPF_FUNC_probe_read_kernel_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_str_proto;case BPF_FUNC_snprintf_btf:return &bpf_snprintf_btf_proto;case BPF_FUNC_snprintf:
kernel/events/core.c +1 -1
@@ -12017,7 +12017,7 @@ SYSCALL_DEFINE5(perf_event_open,/* REGS_INTR can leak data, lockdown must prevent this */ if (attr.sample_type &PERF_SAMPLE_REGS_INTR ) { - err =security_locked_down (LOCKDOWN_PERF ); + err =security_locked_down (current_cred (),LOCKDOWN_PERF );if (err)return err; }
kernel/kexec.c +1 -1
@@ -208,7 +208,7 @@ static inline int kexec_load_check(unsigned long nr_segments,* kexec can be used to circumvent module loading restrictions, so * prevent loading in that case */ - result =security_locked_down (LOCKDOWN_KEXEC ); + result =security_locked_down (current_cred (),LOCKDOWN_KEXEC );if (result)return result;
kernel/kexec_file.c +1 -1
@@ -204,7 +204,7 @@ kimage_validate_signature(struct kimage *image)* down. */ if (!ima_appraise_signature (READING_KEXEC_IMAGE ) && -security_locked_down (LOCKDOWN_KEXEC )) +security_locked_down (current_cred (),LOCKDOWN_KEXEC ))return -EPERM ;pr_debug ("kernel signature verification failed (%d).\n" , ret);
kernel/module.c +1 -1
@@ -2931,7 +2931,7 @@ static int module_sig_check(struct load_info *info, int flags) return -EKEYREJECTED; } -return security_locked_down (LOCKDOWN_MODULE_SIGNATURE ); +return security_locked_down (current_cred (),LOCKDOWN_MODULE_SIGNATURE ); }#else /* !CONFIG_MODULE_SIG */ static int module_sig_check (struct load_info *info,int flags)
kernel/params.c +1 -1
@@ -100,7 +100,7 @@ bool parameq(const char *a, const char *b)static bool param_check_unsafe (const struct kernel_param *kp) {if (kp->flags &KERNEL_PARAM_FL_HWPARAM && -security_locked_down (LOCKDOWN_MODULE_PARAMETERS )) +security_locked_down (current_cred (),LOCKDOWN_MODULE_PARAMETERS ))return false;if (kp->flags &KERNEL_PARAM_FL_UNSAFE ) {
kernel/power/hibernate.c +1 -1
@@ -83,7 +83,7 @@ void hibernate_release(void)bool hibernation_available (void ) {return nohibernate ==0 && - !security_locked_down (LOCKDOWN_HIBERNATION ) && + !security_locked_down (current_cred (),LOCKDOWN_HIBERNATION ) && !secretmem_active (); }
kernel/trace/bpf_trace.c +15 -10
@@ -1066,25 +1066,30 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_prandom_u32:return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_write_user: -return security_locked_down (LOCKDOWN_BPF_WRITE_USER ) <0 ? -NULL :bpf_get_probe_write_proto (); +if (security_locked_down (current_cred (),LOCKDOWN_BPF_WRITE_USER ) <0 ) +return NULL ; +return bpf_get_probe_write_proto ();case BPF_FUNC_probe_read_user:return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_proto;case BPF_FUNC_probe_read_user_str:return &bpf_probe_read_user_str_proto;case BPF_FUNC_probe_read_kernel_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_str_proto;#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE case BPF_FUNC_probe_read: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_compat_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_compat_proto;case BPF_FUNC_probe_read_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_compat_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_compat_str_proto;#endif #ifdef CONFIG_CGROUPS case BPF_FUNC_get_current_cgroup_id:
kernel/trace/ftrace.c +2 -2
@@ -3694,7 +3694,7 @@ ftrace_avail_open(struct inode *inode, struct file *file)struct ftrace_iterator *iter;int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
@@ -5822,7 +5822,7 @@ __ftrace_graph_open(struct inode *inode, struct file *file,int ret;struct ftrace_hash *new_hash =NULL ; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/ring_buffer.c +1 -1
@@ -5880,7 +5880,7 @@ static __init int test_ringbuffer(void)int cpu;int ret =0 ; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Lockdown is enabled, skipping ring buffer tests\n" );return 0 ; }
kernel/trace/trace.c +5 -5
@@ -486,7 +486,7 @@ int tracing_check_open_get_tr(struct trace_array *tr) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
@@ -2071,7 +2071,7 @@ int __init register_tracer(struct tracer *type)return -1 ; } -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Can not register tracer %s due to lockdown\n" , type->name);return -EPERM ;
@@ -9527,7 +9527,7 @@ int tracing_init_dentry(void) {struct trace_array *tr = &global_trace; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Tracing disabled due to lockdown\n" );return -EPERM ; }
@@ -9989,7 +9989,7 @@ __init static int tracer_alloc_buffers(void)int ret = -ENOMEM ; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Tracing disabled due to lockdown\n" );return -EPERM ; }
@@ -10155,7 +10155,7 @@ __init static void tracing_set_default_clock(void) {/* sched_clock_stable() is determined in late_initcall */ if (!trace_boot_clock && !sched_clock_stable ()) { -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Can not set tracing clock due to lockdown\n" );return ; }
kernel/trace/trace_events.c +1 -1
@@ -2130,7 +2130,7 @@ ftrace_event_open(struct inode *inode, struct file *file,struct seq_file *m;int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_events_hist.c +2 -2
@@ -4917,7 +4917,7 @@ static int event_hist_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
@@ -5189,7 +5189,7 @@ static int event_hist_debug_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_events_synth.c +1 -1
@@ -2174,7 +2174,7 @@ static int synth_events_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_events_trigger.c +1 -1
@@ -190,7 +190,7 @@ static int event_trigger_regex_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_kprobe.c +3 -3
@@ -479,7 +479,7 @@ static int __register_trace_kprobe(struct trace_kprobe *tk) {int i, ret; - ret =security_locked_down (LOCKDOWN_KPROBES ); + ret =security_locked_down (current_cred (),LOCKDOWN_KPROBES );if (ret)return ret;
@@ -1141,7 +1141,7 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
@@ -1199,7 +1199,7 @@ static int profile_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_printk.c +1 -1
@@ -362,7 +362,7 @@ ftrace_formats_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_stack.c +1 -1
@@ -477,7 +477,7 @@ static int stack_trace_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_stat.c +1 -1
@@ -236,7 +236,7 @@ static int tracing_stat_open(struct inode *inode, struct file *file)struct seq_file *m;struct stat_session *session = inode->i_private; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
kernel/trace/trace_uprobe.c +2 -2
@@ -781,7 +781,7 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
@@ -836,7 +836,7 @@ static int profile_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;
net/xfrm/xfrm_user.c +9 -2
@@ -850,8 +850,15 @@ static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skbstatic bool xfrm_redact (void ) { -return IS_ENABLED (CONFIG_SECURITY ) && -security_locked_down (LOCKDOWN_XFRM_SECRET ); +/* Don't use current_cred() here, since this may be called when +* broadcasting a notification that an SA has been created/deleted. +* In that case current task is the one triggering the notification, +* but the SA key is actually leaked to the event subscribers. +* Since we can't easily do the redact decision per-subscriber, +* just pass NULL here, indicating to the LSMs that a global lockdown +* decision should be made instead. +*/ +return security_locked_down (NULL ,LOCKDOWN_XFRM_SECRET ); }static int copy_to_user_auth (struct xfrm_algo_auth *auth,struct sk_buff *skb)
security/lockdown/lockdown.c +2 -1
@@ -55,7 +55,8 @@ early_param("lockdown", lockdown_param);* lockdown_is_locked_down - Find out if the kernel is locked down * @what: Tag to use in notice generated if lockdown is in effect */ -static int lockdown_is_locked_down (enum lockdown_reason what) +static int lockdown_is_locked_down (const struct cred *cred, +enum lockdown_reason what) {if (WARN (what >=LOCKDOWN_CONFIDENTIALITY_MAX ,"Invalid lockdown reason" ))
security/security.c +2 -2
@@ -2593,9 +2593,9 @@ void security_bpf_prog_free(struct bpf_prog_aux *aux) }#endif /* CONFIG_BPF_SYSCALL */ -int security_locked_down (enum lockdown_reason what) +int security_locked_down (const struct cred *cred,enum lockdown_reason what) { -return call_int_hook (locked_down,0 , what); +return call_int_hook (locked_down,0 , cred, what); }EXPORT_SYMBOL (security_locked_down);
security/selinux/hooks.c +5 -2
@@ -7013,10 +7013,10 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) }#endif -static int selinux_lockdown (enum lockdown_reason what) +static int selinux_lockdown (const struct cred *cred,enum lockdown_reason what) {struct common_audit_data ad; -u32 sid =current_sid (); +u32 sid;int invalid_reason = (what <=LOCKDOWN_NONE ) || (what ==LOCKDOWN_INTEGRITY_MAX ) || (what >=LOCKDOWN_CONFIDENTIALITY_MAX );
@@ -7028,6 +7028,9 @@ static int selinux_lockdown(enum lockdown_reason what)return -EINVAL ; } +/* Use SECINITSID_KERNEL if there is no relevant cred to check against */ + sid = cred ?cred_sid (cred) :SECINITSID_KERNEL ; + ad.type =LSM_AUDIT_DATA_LOCKDOWN ; ad.u.reason = what;
Signature · 7 lines
2.31.1 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Quoted 54 lines · Ondrej Mosnacek
Commit
59438b46471a("security,lockdown,selinux: implement SELinux lockdown") added an implementation of thelocked_downLSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.However, in several places the
security_locked_down()hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.To fix this, add an explicit struct cred pointer argument to
security_lockdown()and define NULL as a special value to pass instead ofcurrent_cred()in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, useSECINITSID_KERNELin case the cred argument is NULL.Most of the callers are updated to pass
current_cred()as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1.arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink messageXFRM_MSG_GETSAwithNLM_F_DUMPset - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA viaXFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead ofcurrent_cred()here faute de mieux.
Quoted 6 lines
Improvements-suggested-by Casey Schaufler <[email protected]>Improvements-suggested-by Paul Moore <[email protected]>Fixes 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown")Signed-off-by Ondrej Mosnacek <[email protected]>
for the ACPI and hibernation changes.
Quoted 930 lines
---
v4:
- rebase on top of TODO
- fix rebase conflicts:
*drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
*kernel/bpf/helpers.c,kernel/trace/bpf_trace.c
- trivial:LOCKDOWN_BPF_READwas renamed toLOCKDOWN_BPF_READ_KERNELin mainline
*kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition inhibernation_available()
- cover newsecurity_locked_down()call inkernel/bpf/helpers.c(LOCKDOWN_BPF_WRITE_USERinBPF_FUNC_probe_write_usercase)v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument tosecurity_locked_down()and adapt all callers
- keep usingcurrent_cred()in BPF, as the hook calls have been shifted to program load time (commitff40e51043af("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but useSECINITSID_KERNELas the subject instead
- update explanations in the commit messagev2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schauflerv1: https://lore.kernel.org/lkml/[email protected]/
arch/powerpc/xmon/xmon.c | 4 ++-- arch/x86/kernel/ioport.c | 4 ++-- arch/x86/kernel/msr.c | 4 ++-- arch/x86/mm/testmmiotrace.c | 2 +- drivers/acpi/acpi_configfs.c | 2 +- drivers/acpi/custom_method.c | 2 +- drivers/acpi/osl.c | 3 ++- drivers/acpi/tables.c | 2 +- drivers/char/mem.c | 2 +- drivers/cxl/pci.c | 2 +- drivers/firmware/efi/efi.c | 2 +- drivers/firmware/efi/test/efi_test.c | 2 +- drivers/pci/pci-sysfs.c | 6 +++--- drivers/pci/proc.c | 6 +++--- drivers/pci/syscall.c | 2 +- drivers/pcmcia/cistpl.c | 2 +- drivers/tty/serial/serial_core.c | 2 +- fs/debugfs/file.c | 2 +- fs/debugfs/inode.c | 2 +- fs/proc/kcore.c | 2 +- fs/tracefs/inode.c | 2 +- include/linux/lsm_hook_defs.h | 2 +- include/linux/lsm_hooks.h | 1 + include/linux/security.h | 4 ++-- kernel/bpf/helpers.c | 10 ++++++---- kernel/events/core.c | 2 +- kernel/kexec.c | 2 +- kernel/kexec_file.c | 2 +- kernel/module.c | 2 +- kernel/params.c | 2 +- kernel/power/hibernate.c | 2 +- kernel/trace/bpf_trace.c | 25 +++++++++++++++---------- kernel/trace/ftrace.c | 4 ++-- kernel/trace/ring_buffer.c | 2 +- kernel/trace/trace.c | 10 +++++----- kernel/trace/trace_events.c | 2 +- kernel/trace/trace_events_hist.c | 4 ++-- kernel/trace/trace_events_synth.c | 2 +- kernel/trace/trace_events_trigger.c | 2 +- kernel/trace/trace_kprobe.c | 6 +++--- kernel/trace/trace_printk.c | 2 +- kernel/trace/trace_stack.c | 2 +- kernel/trace/trace_stat.c | 2 +- kernel/trace/trace_uprobe.c | 4 ++-- net/xfrm/xfrm_user.c | 11 +++++++++-- security/lockdown/lockdown.c | 3 ++- security/security.c | 4 ++-- security/selinux/hooks.c | 7 +++++-- 48 files changed, 99 insertions(+), 79 deletions(-)arch/powerpc/xmon/xmon.c +2
-2@@ -309,7 +309,7 @@ static bool xmon_is_locked_down(void)static bool lockdown;if (!lockdown) { - lockdown = !!security_locked_down (LOCKDOWN_XMON_RW ); + lockdown = !!security_locked_down (NULL ,LOCKDOWN_XMON_RW );if (lockdown) {printf ("xmon: Disabled due to kernel lockdown\n" ); xmon_is_ro = true;@@ -317,7 +317,7 @@ static bool xmon_is_locked_down(void) }if (!xmon_is_ro) { - xmon_is_ro = !!security_locked_down (LOCKDOWN_XMON_WR ); + xmon_is_ro = !!security_locked_down (NULL ,LOCKDOWN_XMON_WR );if (xmon_is_ro)printf ("xmon: Read-only due to kernel lockdown\n" ); }arch/x86/kernel/ioport.c +2
-2@@ -71,7 +71,7 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)if ((from + num <= from) || (from + num >IO_BITMAP_BITS ))return -EINVAL ;if (turn_on && (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_IOPORT ))) +security_locked_down (current_cred (),LOCKDOWN_IOPORT )))return -EPERM ;/* @@ -187,7 +187,7 @@ SYSCALL_DEFINE1(iopl, unsigned int, level)/* Trying to gain more privileges? */ if (level > old) {if (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_IOPORT )) +security_locked_down (current_cred (),LOCKDOWN_IOPORT ))return -EPERM ; }arch/x86/kernel/msr.c +2
-2@@ -116,7 +116,7 @@ static ssize_t msr_write(struct file *file, const char __user *buf,int err =0 ;ssize_t bytes =0 ; - err =security_locked_down (LOCKDOWN_MSR ); + err =security_locked_down (current_cred (),LOCKDOWN_MSR );if (err)return err;@@ -179,7 +179,7 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) err = -EFAULT ;break ; } - err =security_locked_down (LOCKDOWN_MSR ); + err =security_locked_down (current_cred (),LOCKDOWN_MSR );if (err)break ;arch/x86/mm/testmmiotrace.c +1
-1@@ -116,7 +116,7 @@ static void do_test_bulk_ioremapping(void)static int __init init (void ) {unsigned long size = (read_far) ? (8 <<20 ) : (16 <<10 ); -int ret =security_locked_down (LOCKDOWN_MMIOTRACE ); +int ret =security_locked_down (current_cred (),LOCKDOWN_MMIOTRACE );if (ret)return ret;drivers/acpi/acpi_configfs.c +1
-1@@ -26,7 +26,7 @@ static ssize_t acpi_table_aml_write(struct config_item *cfg, {const struct acpi_table_header *header = data;struct acpi_table *table; -int ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); +int ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;drivers/acpi/custom_method.c +1
-1@@ -30,7 +30,7 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,acpi_status status;int ret; - ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); + ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;drivers/acpi/osl.c +2
-1@@ -198,7 +198,8 @@ acpi_physical_address __init acpi_os_get_root_pointer(void)* specific location (if appropriate) so it can be carried * over further kexec()s. */ -if (acpi_rsdp && !security_locked_down (LOCKDOWN_ACPI_TABLES )) { +if (acpi_rsdp && !security_locked_down (current_cred (), +LOCKDOWN_ACPI_TABLES )) {acpi_arch_set_root_pointer (acpi_rsdp);return acpi_rsdp; }drivers/acpi/tables.c +1
-1@@ -577,7 +577,7 @@ void __init acpi_table_upgrade(void)if (table_nr ==0 )return ; -if (security_locked_down (LOCKDOWN_ACPI_TABLES )) { +if (security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES )) {pr_notice ("kernel is locked down, ignoring table override\n" );return ; }drivers/char/mem.c +1
-1@@ -617,7 +617,7 @@ static int open_port(struct inode *inode, struct file *filp)if (!capable (CAP_SYS_RAWIO ))return -EPERM ; - rc =security_locked_down (LOCKDOWN_DEV_MEM ); + rc =security_locked_down (current_cred (),LOCKDOWN_DEV_MEM );if (rc)return rc;drivers/cxl/pci.c +1
-1@@ -575,7 +575,7 @@ static bool cxl_mem_raw_command_allowed(u16 opcode)if (!IS_ENABLED (CONFIG_CXL_MEM_RAW_COMMANDS ))return false; -if (security_locked_down (LOCKDOWN_PCI_ACCESS )) +if (security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return false;if (cxl_raw_allow_all)drivers/firmware/efi/efi.c +1
-1@@ -200,7 +200,7 @@ static void generic_ops_unregister(void)static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX ]__initdata ;static int __init efivar_ssdt_setup (char *str) { -int ret =security_locked_down (LOCKDOWN_ACPI_TABLES ); +int ret =security_locked_down (current_cred (),LOCKDOWN_ACPI_TABLES );if (ret)return ret;drivers/firmware/efi/test/efi_test.c +1
-1@@ -722,7 +722,7 @@ static long efi_test_ioctl(struct file *file, unsigned int cmd,static int efi_test_open (struct inode *inode,struct file *file) { -int ret =security_locked_down (LOCKDOWN_EFI_TEST ); +int ret =security_locked_down (current_cred (),LOCKDOWN_EFI_TEST );if (ret)return ret;drivers/pci/pci-sysfs.c +3
-3@@ -753,7 +753,7 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,u8 *data = (u8 *) buf;int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;@@ -1047,7 +1047,7 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,struct resource *res = &pdev->resource[bar];int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;@@ -1128,7 +1128,7 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, {int ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;drivers/pci/proc.c +3
-3@@ -119,7 +119,7 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,int size = dev->cfg_size;int cnt, ret; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;@@ -202,7 +202,7 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,#endif /* HAVE_PCI_MMAP */ int ret =0 ; - ret =security_locked_down (LOCKDOWN_PCI_ACCESS ); + ret =security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS );if (ret)return ret;@@ -249,7 +249,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)int i, ret, write_combine =0 , res_bit =IORESOURCE_MEM ;if (!capable (CAP_SYS_RAWIO ) || -security_locked_down (LOCKDOWN_PCI_ACCESS )) +security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return -EPERM ;if (fpriv->mmap_state == pci_mmap_io) {drivers/pci/syscall.c +1
-1@@ -93,7 +93,7 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,int err =0 ;if (!capable (CAP_SYS_ADMIN ) || -security_locked_down (LOCKDOWN_PCI_ACCESS )) +security_locked_down (current_cred (),LOCKDOWN_PCI_ACCESS ))return -EPERM ; dev =pci_get_domain_bus_and_slot (0 , bus, dfn);drivers/pcmcia/cistpl.c +1
-1@@ -1577,7 +1577,7 @@ static ssize_t pccard_store_cis(struct file *filp, struct kobject *kobj,struct pcmcia_socket *s;int error; - error =security_locked_down (LOCKDOWN_PCMCIA_CIS ); + error =security_locked_down (current_cred (),LOCKDOWN_PCMCIA_CIS );if (error)return error;drivers/tty/serial/serial_core.c +1
-1@@ -840,7 +840,7 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port, }if (change_irq || change_port) { - retval =security_locked_down (LOCKDOWN_TIOCSSERIAL ); + retval =security_locked_down (current_cred (),LOCKDOWN_TIOCSSERIAL );if (retval) goto exit; }fs/debugfs/file.c +1
-1@@ -154,7 +154,7 @@ static int debugfs_locked_down(struct inode *inode, !real_fops->mmap)return 0 ; -if (security_locked_down (LOCKDOWN_DEBUGFS )) +if (security_locked_down (current_cred (),LOCKDOWN_DEBUGFS ))return -EPERM ;return 0 ;fs/debugfs/inode.c +1
-1@@ -48,7 +48,7 @@ static int debugfs_setattr(struct user_namespace *mnt_userns,int ret;if (ia->ia_valid & (ATTR_MODE |ATTR_UID |ATTR_GID )) { - ret =security_locked_down (LOCKDOWN_DEBUGFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_DEBUGFS );if (ret)return ret; }fs/proc/kcore.c +1
-1@@ -586,7 +586,7 @@ out:static int open_kcore (struct inode *inode,struct file *filp) { -int ret =security_locked_down (LOCKDOWN_KCORE ); +int ret =security_locked_down (current_cred (),LOCKDOWN_KCORE );if (!capable (CAP_SYS_RAWIO ))return -EPERM ;fs/tracefs/inode.c +1
-1@@ -396,7 +396,7 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,struct dentry *dentry;struct inode *inode; -if (security_locked_down (LOCKDOWN_TRACEFS )) +if (security_locked_down (NULL ,LOCKDOWN_TRACEFS ))return NULL ;if (!(mode &S_IFMT ))include/linux/lsm_hook_defs.h +1
-1@@ -393,7 +393,7 @@ LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux) LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux)#endif /* CONFIG_BPF_SYSCALL */ -LSM_HOOK(int, 0, locked_down, enum lockdown_reason what) +LSM_HOOK(int, 0, locked_down, const struct cred *cred, enum lockdown_reason what)#ifdef CONFIG_PERF_EVENTS LSM_HOOK(int, 0, perf_event_open, struct perf_event_attr *attr, int type)include/linux/lsm_hooks.h +1
-0@@ -1543,6 +1543,7 @@* Determine whether a kernel feature that potentially enables arbitrary * code execution in kernel space should be permitted. * +* @cred: credential asociated with the operation, or NULL if not applicable * @what: kernel feature being accessed * * Security hooks for perf events include/linux/security.h +2
-2@@ -471,7 +471,7 @@ void security_inode_invalidate_secctx(struct inode *inode);int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); -int security_locked_down (enum lockdown_reason what); +int security_locked_down (const struct cred *cred,enum lockdown_reason what);#else /* CONFIG_SECURITY */ static inline int call_blocking_lsm_notifier (enum lsm_event event,void *data)@@ -1344,7 +1344,7 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 {return -EOPNOTSUPP ; } -static inline int security_locked_down (enum lockdown_reason what) +static inline int security_locked_down (struct cred *cred,enum lockdown_reason what) {return 0 ; }kernel/bpf/helpers.c +6
-4@@ -1424,13 +1424,15 @@ bpf_base_func_proto(enum bpf_func_id func_id)case BPF_FUNC_probe_read_user:return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_proto;case BPF_FUNC_probe_read_user_str:return &bpf_probe_read_user_str_proto;case BPF_FUNC_probe_read_kernel_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_str_proto;case BPF_FUNC_snprintf_btf:return &bpf_snprintf_btf_proto;case BPF_FUNC_snprintf:kernel/events/core.c +1
-1@@ -12017,7 +12017,7 @@ SYSCALL_DEFINE5(perf_event_open,/* REGS_INTR can leak data, lockdown must prevent this */ if (attr.sample_type &PERF_SAMPLE_REGS_INTR ) { - err =security_locked_down (LOCKDOWN_PERF ); + err =security_locked_down (current_cred (),LOCKDOWN_PERF );if (err)return err; }kernel/kexec.c +1
-1@@ -208,7 +208,7 @@ static inline int kexec_load_check(unsigned long nr_segments,* kexec can be used to circumvent module loading restrictions, so * prevent loading in that case */ - result =security_locked_down (LOCKDOWN_KEXEC ); + result =security_locked_down (current_cred (),LOCKDOWN_KEXEC );if (result)return result;kernel/kexec_file.c +1
-1@@ -204,7 +204,7 @@ kimage_validate_signature(struct kimage *image)* down. */ if (!ima_appraise_signature (READING_KEXEC_IMAGE ) && -security_locked_down (LOCKDOWN_KEXEC )) +security_locked_down (current_cred (),LOCKDOWN_KEXEC ))return -EPERM ;pr_debug ("kernel signature verification failed (%d).\n" , ret);kernel/module.c +1
-1@@ -2931,7 +2931,7 @@ static int module_sig_check(struct load_info *info, int flags) return -EKEYREJECTED; } -return security_locked_down (LOCKDOWN_MODULE_SIGNATURE ); +return security_locked_down (current_cred (),LOCKDOWN_MODULE_SIGNATURE ); }#else /* !CONFIG_MODULE_SIG */ static int module_sig_check (struct load_info *info,int flags)kernel/params.c +1
-1@@ -100,7 +100,7 @@ bool parameq(const char *a, const char *b)static bool param_check_unsafe (const struct kernel_param *kp) {if (kp->flags &KERNEL_PARAM_FL_HWPARAM && -security_locked_down (LOCKDOWN_MODULE_PARAMETERS )) +security_locked_down (current_cred (),LOCKDOWN_MODULE_PARAMETERS ))return false;if (kp->flags &KERNEL_PARAM_FL_UNSAFE ) {kernel/power/hibernate.c +1
-1@@ -83,7 +83,7 @@ void hibernate_release(void)bool hibernation_available (void ) {return nohibernate ==0 && - !security_locked_down (LOCKDOWN_HIBERNATION ) && + !security_locked_down (current_cred (),LOCKDOWN_HIBERNATION ) && !secretmem_active (); }kernel/trace/bpf_trace.c +15
-10@@ -1066,25 +1066,30 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_prandom_u32:return &bpf_get_prandom_u32_proto;case BPF_FUNC_probe_write_user: -return security_locked_down (LOCKDOWN_BPF_WRITE_USER ) <0 ? -NULL :bpf_get_probe_write_proto (); +if (security_locked_down (current_cred (),LOCKDOWN_BPF_WRITE_USER ) <0 ) +return NULL ; +return bpf_get_probe_write_proto ();case BPF_FUNC_probe_read_user:return &bpf_probe_read_user_proto;case BPF_FUNC_probe_read_kernel: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_proto;case BPF_FUNC_probe_read_user_str:return &bpf_probe_read_user_str_proto;case BPF_FUNC_probe_read_kernel_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_kernel_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_kernel_str_proto;#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE case BPF_FUNC_probe_read: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_compat_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_compat_proto;case BPF_FUNC_probe_read_str: -return security_locked_down (LOCKDOWN_BPF_READ_KERNEL ) <0 ? -NULL : &bpf_probe_read_compat_str_proto; +if (security_locked_down (current_cred (),LOCKDOWN_BPF_READ_KERNEL ) <0 ) +return NULL ; +return &bpf_probe_read_compat_str_proto;#endif #ifdef CONFIG_CGROUPS case BPF_FUNC_get_current_cgroup_id:kernel/trace/ftrace.c +2
-2@@ -3694,7 +3694,7 @@ ftrace_avail_open(struct inode *inode, struct file *file)struct ftrace_iterator *iter;int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;@@ -5822,7 +5822,7 @@ __ftrace_graph_open(struct inode *inode, struct file *file,int ret;struct ftrace_hash *new_hash =NULL ; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/ring_buffer.c +1
-1@@ -5880,7 +5880,7 @@ static __init int test_ringbuffer(void)int cpu;int ret =0 ; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Lockdown is enabled, skipping ring buffer tests\n" );return 0 ; }kernel/trace/trace.c +5
-5@@ -486,7 +486,7 @@ int tracing_check_open_get_tr(struct trace_array *tr) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;@@ -2071,7 +2071,7 @@ int __init register_tracer(struct tracer *type)return -1 ; } -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Can not register tracer %s due to lockdown\n" , type->name);return -EPERM ;@@ -9527,7 +9527,7 @@ int tracing_init_dentry(void) {struct trace_array *tr = &global_trace; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Tracing disabled due to lockdown\n" );return -EPERM ; }@@ -9989,7 +9989,7 @@ __init static int tracer_alloc_buffers(void)int ret = -ENOMEM ; -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Tracing disabled due to lockdown\n" );return -EPERM ; }@@ -10155,7 +10155,7 @@ __init static void tracing_set_default_clock(void) {/* sched_clock_stable() is determined in late_initcall */ if (!trace_boot_clock && !sched_clock_stable ()) { -if (security_locked_down (LOCKDOWN_TRACEFS )) { +if (security_locked_down (current_cred (),LOCKDOWN_TRACEFS )) {pr_warn ("Can not set tracing clock due to lockdown\n" );return ; }kernel/trace/trace_events.c +1
-1@@ -2130,7 +2130,7 @@ ftrace_event_open(struct inode *inode, struct file *file,struct seq_file *m;int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_events_hist.c +2
-2@@ -4917,7 +4917,7 @@ static int event_hist_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;@@ -5189,7 +5189,7 @@ static int event_hist_debug_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_events_synth.c +1
-1@@ -2174,7 +2174,7 @@ static int synth_events_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_events_trigger.c +1
-1@@ -190,7 +190,7 @@ static int event_trigger_regex_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_kprobe.c +3
-3@@ -479,7 +479,7 @@ static int __register_trace_kprobe(struct trace_kprobe *tk) {int i, ret; - ret =security_locked_down (LOCKDOWN_KPROBES ); + ret =security_locked_down (current_cred (),LOCKDOWN_KPROBES );if (ret)return ret;@@ -1141,7 +1141,7 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;@@ -1199,7 +1199,7 @@ static int profile_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_printk.c +1
-1@@ -362,7 +362,7 @@ ftrace_formats_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_stack.c +1
-1@@ -477,7 +477,7 @@ static int stack_trace_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_stat.c +1
-1@@ -236,7 +236,7 @@ static int tracing_stat_open(struct inode *inode, struct file *file)struct seq_file *m;struct stat_session *session = inode->i_private; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;kernel/trace/trace_uprobe.c +2
-2@@ -781,7 +781,7 @@ static int probes_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;@@ -836,7 +836,7 @@ static int profile_open(struct inode *inode, struct file *file) {int ret; - ret =security_locked_down (LOCKDOWN_TRACEFS ); + ret =security_locked_down (current_cred (),LOCKDOWN_TRACEFS );if (ret)return ret;net/xfrm/xfrm_user.c +9
-2@@ -850,8 +850,15 @@ static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skbstatic bool xfrm_redact (void ) { -return IS_ENABLED (CONFIG_SECURITY ) && -security_locked_down (LOCKDOWN_XFRM_SECRET ); +/* Don't use current_cred() here, since this may be called when +* broadcasting a notification that an SA has been created/deleted. +* In that case current task is the one triggering the notification, +* but the SA key is actually leaked to the event subscribers. +* Since we can't easily do the redact decision per-subscriber, +* just pass NULL here, indicating to the LSMs that a global lockdown +* decision should be made instead. +*/ +return security_locked_down (NULL ,LOCKDOWN_XFRM_SECRET ); }static int copy_to_user_auth (struct xfrm_algo_auth *auth,struct sk_buff *skb)security/lockdown/lockdown.c +2
-1@@ -55,7 +55,8 @@ early_param("lockdown", lockdown_param);* lockdown_is_locked_down - Find out if the kernel is locked down * @what: Tag to use in notice generated if lockdown is in effect */ -static int lockdown_is_locked_down (enum lockdown_reason what) +static int lockdown_is_locked_down (const struct cred *cred, +enum lockdown_reason what) {if (WARN (what >=LOCKDOWN_CONFIDENTIALITY_MAX ,"Invalid lockdown reason" ))security/security.c +2
-2@@ -2593,9 +2593,9 @@ void security_bpf_prog_free(struct bpf_prog_aux *aux) }#endif /* CONFIG_BPF_SYSCALL */ -int security_locked_down (enum lockdown_reason what) +int security_locked_down (const struct cred *cred,enum lockdown_reason what) { -return call_int_hook (locked_down,0 , what); +return call_int_hook (locked_down,0 , cred, what); }EXPORT_SYMBOL (security_locked_down);security/selinux/hooks.c +5
-3@@ -7013,10 +7013,10 @@ static void selinux_bpf_prog_free(struct bpf_prog_aux *aux) }#endif -static int selinux_lockdown (enum lockdown_reason what) +static int selinux_lockdown (const struct cred *cred,enum lockdown_reason what) {struct common_audit_data ad; -u32 sid =current_sid (); +u32 sid;int invalid_reason = (what <=LOCKDOWN_NONE ) || (what ==LOCKDOWN_INTEGRITY_MAX ) || (what >=LOCKDOWN_CONFIDENTIALITY_MAX );@@ -7028,6 +7028,9 @@ static int selinux_lockdown(enum lockdown_reason what)return -EINVAL ; } +/* Use SECINITSID_KERNEL if there is no relevant cred to check against */ + sid = cred ?cred_sid (cred) :SECINITSID_KERNEL ; + ad.type =LSM_AUDIT_DATA_LOCKDOWN ; ad.u.reason = what; --2.31.1
Paul Moore
maintainerQuoted 85 lines · Ondrej Mosnacek
Commit
59438b46471a("security,lockdown,selinux: implement SELinux lockdown") added an implementation of thelocked_downLSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.However, in several places the
security_locked_down()hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.To fix this, add an explicit struct cred pointer argument to
security_lockdown()and define NULL as a special value to pass instead ofcurrent_cred()in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, useSECINITSID_KERNELin case the cred argument is NULL.Most of the callers are updated to pass
current_cred()as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1.arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink messageXFRM_MSG_GETSAwithNLM_F_DUMPset - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA viaXFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead ofcurrent_cred()here faute de mieux.Improvements-suggested-by Casey Schaufler <[email protected]>Improvements-suggested-by Paul Moore <[email protected]>Fixes 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown")Signed-off-by Ondrej Mosnacek <[email protected]>---
v4:
- rebase on top of TODO
- fix rebase conflicts:
*drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
*kernel/bpf/helpers.c,kernel/trace/bpf_trace.c
- trivial:LOCKDOWN_BPF_READwas renamed toLOCKDOWN_BPF_READ_KERNELin mainline
*kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition inhibernation_available()
- cover newsecurity_locked_down()call inkernel/bpf/helpers.c(LOCKDOWN_BPF_WRITE_USERinBPF_FUNC_probe_write_usercase)v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument tosecurity_locked_down()and adapt all callers
- keep usingcurrent_cred()in BPF, as the hook calls have been shifted to program load time (commitff40e51043af("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but useSECINITSID_KERNELas the subject instead
- update explanations in the commit message
Quoted 4 lines
v2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schaufler
The changes between v3 and v4 all seem sane to me, but I'm going to let this sit for a few days in hopes that we can collect a few more Reviewed-bys and ACKs. If I don't see any objections I'll merge it mid-week(ish) into selinux/stable-5.15 and plan on sending it to Linus after it goes through a build/test cycle.
Signature · 2 lines
paul moore www.paul-moore.com
Paul Moore
maintainerQuoted 90 lines · Paul Moore
Quoted 88 lines · Ondrej Mosnacek
Commit
59438b46471a("security,lockdown,selinux: implement SELinux lockdown") added an implementation of thelocked_downLSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.However, in several places the
security_locked_down()hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.To fix this, add an explicit struct cred pointer argument to
security_lockdown()and define NULL as a special value to pass instead ofcurrent_cred()in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, useSECINITSID_KERNELin case the cred argument is NULL.Most of the callers are updated to pass
current_cred()as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1.arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink messageXFRM_MSG_GETSAwithNLM_F_DUMPset - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA viaXFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead ofcurrent_cred()here faute de mieux.Improvements-suggested-by Casey Schaufler <[email protected]>Improvements-suggested-by Paul Moore <[email protected]>Fixes 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown")Signed-off-by Ondrej Mosnacek <[email protected]>---
v4:
- rebase on top of TODO
- fix rebase conflicts:
*drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
*kernel/bpf/helpers.c,kernel/trace/bpf_trace.c
- trivial:LOCKDOWN_BPF_READwas renamed toLOCKDOWN_BPF_READ_KERNELin mainline
*kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition inhibernation_available()
- cover newsecurity_locked_down()call inkernel/bpf/helpers.c(LOCKDOWN_BPF_WRITE_USERinBPF_FUNC_probe_write_usercase)v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument tosecurity_locked_down()and adapt all callers
- keep usingcurrent_cred()in BPF, as the hook calls have been shifted to program load time (commitff40e51043af("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but useSECINITSID_KERNELas the subject instead
- update explanations in the commit messagev2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schaufler
Quoted 7 lines
Quoted 1 line
The changes between v3 and v4 all seem sane to me, but I'm going to let this sit for a few days in hopes that we can collect a few more Reviewed-bys and ACKs. If I don't see any objections I'll merge it mid-week(ish) into selinux/stable-5.15 and plan on sending it to Linus after it goes through a build/test cycle.
Time's up, I just merged this into selinux/stable-5.15 and I'll send this to Linus once it passes testing.
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 93 lines · Paul Moore
Quoted 92 lines · Paul Moore
Quoted 89 lines · Ondrej Mosnacek
Commit
59438b46471a("security,lockdown,selinux: implement SELinux lockdown") added an implementation of thelocked_downLSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.However, in several places the
security_locked_down()hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.To fix this, add an explicit struct cred pointer argument to
security_lockdown()and define NULL as a special value to pass instead ofcurrent_cred()in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, useSECINITSID_KERNELin case the cred argument is NULL.Most of the callers are updated to pass
current_cred()as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1.arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink messageXFRM_MSG_GETSAwithNLM_F_DUMPset - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA viaXFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead ofcurrent_cred()here faute de mieux.Improvements-suggested-by Casey Schaufler <[email protected]>Improvements-suggested-by Paul Moore <[email protected]>Fixes 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown")Signed-off-by Ondrej Mosnacek <[email protected]>---
v4:
- rebase on top of TODO
- fix rebase conflicts:
*drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
*kernel/bpf/helpers.c,kernel/trace/bpf_trace.c
- trivial:LOCKDOWN_BPF_READwas renamed toLOCKDOWN_BPF_READ_KERNELin mainline
*kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition inhibernation_available()
- cover newsecurity_locked_down()call inkernel/bpf/helpers.c(LOCKDOWN_BPF_WRITE_USERinBPF_FUNC_probe_write_usercase)v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument tosecurity_locked_down()and adapt all callers
- keep usingcurrent_cred()in BPF, as the hook calls have been shifted to program load time (commitff40e51043af("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but useSECINITSID_KERNELas the subject instead
- update explanations in the commit messagev2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schaufler
Quoted 8 lines
Quoted 5 lines
The changes between v3 and v4 all seem sane to me, but I'm going to let this sit for a few days in hopes that we can collect a few more Reviewed-bys and ACKs. If I don't see any objections I'll merge it mid-week(ish) into selinux/stable-5.15 and plan on sending it to Linus after it goes through a build/test cycle.
Time's up, I just merged this into selinux/stable-5.15 and I'll send this to Linus once it passes testing.
Thanks!
Signature · 3 lines
Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.
Paul Moore
maintainerQuoted 94 lines · Paul Moore
Quoted 92 lines · Paul Moore
Quoted 89 lines · Ondrej Mosnacek
Commit
59438b46471a("security,lockdown,selinux: implement SELinux lockdown") added an implementation of thelocked_downLSM hook to SELinux, with the aim to restrict which domains are allowed to perform operations that would breach lockdown.However, in several places the
security_locked_down()hook is called in situations where the current task isn't doing any action that would directly breach lockdown, leading to SELinux checks that are basically bogus.To fix this, add an explicit struct cred pointer argument to
security_lockdown()and define NULL as a special value to pass instead ofcurrent_cred()in such situations. LSMs that take the subject credentials into account can then fall back to some default or ignore such calls altogether. In the SELinux lockdown hook implementation, useSECINITSID_KERNELin case the cred argument is NULL.Most of the callers are updated to pass
current_cred()as the cred pointer, thus maintaining the same behavior. The following callers are modified to pass NULL as the cred pointer instead:
1.arch/powerpc/xmon/xmon.c
Seems to be some interactive debugging facility. It appears that the lockdown hook is called from interrupt context here, so it should be more appropriate to request a global lockdown decision.
2. fs/tracefs/inode.c:tracefs_create_file()
Here the call is used to prevent creating new tracefs entries when the kernel is locked down. Assumes that locking down is one-way - i.e. if the hook returns non-zero once, it will never return zero again, thus no point in creating these files. Also, the hook is often called by a module's init function when it is loaded by userspace, where it doesn't make much sense to do a check against the current task's creds, since the task itself doesn't actually use the tracing functionality (i.e. doesn't breach lockdown), just indirectly makes some new tracepoints available to whoever is authorized to use them.
3. net/xfrm/xfrm_user.c:copy_to_user_*()
Here a cryptographic secret is redacted based on the value returned from the hook. There are two possible actions that may lead here:
a) A netlink messageXFRM_MSG_GETSAwithNLM_F_DUMPset - here the task context is relevant, since the dumped data is sent back to the current task.
b) When adding/deleting/updating an SA viaXFRM_MSG_xxxSA, the dumped SA is broadcasted to tasks subscribed to XFRM events - here the current task context is not relevant as it doesn't represent the tasks that could potentially see the secret.
It doesn't seem worth it to try to keep using the current task's context in the a) case, since the eventual data leak can be circumvented anyway via b), plus there is no way for the task to indicate that it doesn't care about the actual key value, so the check could generate a lot of "false alert" denials with SELinux. Thus, let's pass NULL instead ofcurrent_cred()here faute de mieux.Improvements-suggested-by Casey Schaufler <[email protected]>Improvements-suggested-by Paul Moore <[email protected]>Fixes 59438b46471a ("security,lockdown,selinux: implement SELinux lockdown")Signed-off-by Ondrej Mosnacek <[email protected]>---
v4:
- rebase on top of TODO
- fix rebase conflicts:
*drivers/cxl/pci.c
- trivial: the lockdown reason was corrected in mainline
*kernel/bpf/helpers.c,kernel/trace/bpf_trace.c
- trivial:LOCKDOWN_BPF_READwas renamed toLOCKDOWN_BPF_READ_KERNELin mainline
*kernel/power/hibernate.c
- trivial: !secretmem_active() was added to the condition inhibernation_available()
- cover newsecurity_locked_down()call inkernel/bpf/helpers.c(LOCKDOWN_BPF_WRITE_USERinBPF_FUNC_probe_write_usercase)v3: https://lore.kernel.org/lkml/[email protected]/
- add the cred argument tosecurity_locked_down()and adapt all callers
- keep usingcurrent_cred()in BPF, as the hook calls have been shifted to program load time (commitff40e51043af("bpf, lockdown, audit: Fix buggy SELinux lockdown permission checks"))
- in SELinux, don't ignore hook calls where cred == NULL, but useSECINITSID_KERNELas the subject instead
- update explanations in the commit messagev2: https://lore.kernel.org/lkml/[email protected]/
- change to a single hook based on suggestions by Casey Schaufler
Quoted 8 lines
Quoted 5 lines
The changes between v3 and v4 all seem sane to me, but I'm going to let this sit for a few days in hopes that we can collect a few more Reviewed-bys and ACKs. If I don't see any objections I'll merge it mid-week(ish) into selinux/stable-5.15 and plan on sending it to Linus after it goes through a build/test cycle.
Time's up, I just merged this into selinux/stable-5.15 and I'll send this to Linus once it passes testing.
... and it's back out of selinux/stable-5.15 in spectacular fashion. I'll be following up with another SELinux patch today or tomorrow.
Signature · 7 lines
paul moore www.paul-moore.com _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec