Skip to contentSkip to conversation

btf: Expose kernel BTF only to tasks with CAP_PERFMON

PATCH Andrii Nakryiko 5 participants 5 messages Oct 28, 2020 – Oct 30, 2020 1 message not in the archive BPF

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

+ [email protected]

Quoted 11 lines · Dan Aloni

Commit 341dfcf8d78e ("btf: expose BTF info through sysfs") added a sysfs file that exposes to userspace kernel BTF information which allows userspace to deduce the structure layout of all kernel internal structures.

This file is currently accessible to unprivileged users, without requiring any special capability. Given that knowledge on kernel structure layout is useful for dynamically building local privilege escalation exploit in userspace, access to this file should be restricted.

So is /proc/config.gz, which is also very helpful in understanding what exactly is there in the kernel. So seems to be /boot/vmlinux-$(uname -r), which has exactly the same BTF data and more.

Guarding /sys/kernel/bpf/vmlinux behind CAP_PERFMON would break a lot of users relying on BTF availability to build their BPF applications. We shouldn't expect developers to build their applications under root. But that's what this patch is trying to do.

Quoted 34 lines
Fixes 341dfcf8d78e ("btf: expose BTF info through sysfs")
---
 kernel/bpf/sysfs_btf.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/sysfs_btf.c +4 -1
index 11b3380887fa..c985d42dfa49 100644
@@ -7,6 +7,7 @@
 #include <linux/kobject.h>
 #include <linux/init.h>
 #include <linux/sysfs.h>
+#include <linux/capability.h>
 
 /* See scripts/link-vmlinux.sh, gen_btf() func for details */
 extern char __weak __start_BTF[];
@@ -17,6 +18,9 @@ btf_vmlinux_read(struct file *file, struct kobject *kobj,
                 struct bin_attribute *bin_attr,
                 char *buf, loff_t off, size_t len)
 {
+       if (!perfmon_capable())
+               return -EACCES;
+
        memcpy(buf, __start_BTF + off, len);
        return len;
 }
--

2.26.2

#2 lore raw
Quoted 1 line · Andrii Nakryiko

+ [email protected]

You shouldn't Cc public email lists for a patch submitted to [email protected].

Quoted 7 lines · Dan Aloni
Quoted 5 lines

Commit 341dfcf8d78e ("btf: expose BTF info through sysfs") added a sysfs file that exposes to userspace kernel BTF information which allows userspace to deduce the structure layout of all kernel internal structures.

Quoted 7 lines
Quoted 5 lines

This file is currently accessible to unprivileged users, without requiring any special capability. Given that knowledge on kernel structure layout is useful for dynamically building local privilege escalation exploit in userspace, access to this file should be restricted.

So is /proc/config.gz, which is also very helpful in understanding what exactly is there in the kernel.

Viewing kernel build config is more like querying supported kernel features. I don't consider it as a meaningful information disclosure, as I see disclosing the kernel internal struct layout.

Quoted 3 lines

So seems to be
/boot/vmlinux-$(uname -r), which has exactly the same BTF data and more.

I agree. True. Good enough argument for dropping this patch.

Quoted 3 lines

Guarding /sys/kernel/bpf/vmlinux behind CAP_PERFMON would break a lot of users relying on BTF availability to build their BPF applications.

True. If this patch is applied, would need to at least be behind an optin knob. Similar to dmesg_restrict.

#3 lore raw
Quoted 4 lines · Liran
Quoted 2 lines

Guarding /sys/kernel/bpf/vmlinux behind CAP_PERFMON would break a lot of users relying on BTF availability to build their BPF applications.

True. If this patch is applied, would need to at least be behind an optin knob. Similar to dmesg_restrict.

It's not going to be applied. If a file shouldn't be read by a user it should have appropriate file permissions instead of 444. Checking capable() in read() is very non-unix way to deal with permissions.

#4 lore raw
Quoted 9 lines · Alexei Starovoitov
Quoted 4 lines · Liran
Quoted 2 lines

Guarding /sys/kernel/bpf/vmlinux behind CAP_PERFMON would break a lot of users relying on BTF availability to build their BPF applications.

True. If this patch is applied, would need to at least be behind an optin knob. Similar to dmesg_restrict.

It's not going to be applied. If a file shouldn't be read by a user it should have appropriate file permissions instead of 444. Checking capable() in read() is very non-unix way to deal with permissions.

Not only it's a non-unix way, both don't achieve the same goals at all!

One checks for permissions at open() time and may for example allow a process to drop its uid after opening, while the other one allows to filter who can really read it, particularly in case the FD is inherited between processes. With this said, I don't see why there would be a special case for this one, it should definitely stick to file permissions only.

Willy

#5 lore raw
Quoted 11 lines · Willy Tarreau
Quoted 9 lines · Alexei Starovoitov
Quoted 4 lines · Liran
Quoted 2 lines

Guarding /sys/kernel/bpf/vmlinux behind CAP_PERFMON would break a lot of users relying on BTF availability to build their BPF applications.

True. If this patch is applied, would need to at least be behind an optin knob. Similar to dmesg_restrict.

It's not going to be applied. If a file shouldn't be read by a user it should have appropriate file permissions instead of 444. Checking capable() in read() is very non-unix way to deal with permissions.

Quoted 8 lines

Not only it's a non-unix way, both don't achieve the same goals at all!

One checks for permissions at open() time and may for example allow a process to drop its uid after opening, while the other one allows to filter who can really read it, particularly in case the FD is inherited between processes. With this said, I don't see why there would be a special case for this one, it should definitely stick to file permissions only.

From include/linux/bpf.h:

static inline bool bpf_allow_ptr_leaks(void)

{
	return perfmon_capable();
}

static inline bool bpf_allow_ptr_to_map_access(void)

{
	return perfmon_capable();
}

static inline bool bpf_bypass_spec_v1(void)

{
	return perfmon_capable();
}

static inline bool bpf_bypass_spec_v4(void)

{
	return perfmon_capable();
}

There's also several cases in bpf_base_func_proto().

So it seems entirely reasonable to suggest that perfmon_capable() is the right check here.

cheers