On Thu, 2 Mar 2023 14:12:31 -0800 Jakub Kicinski wrote: > On Thu, 2 Mar 2023 14:08:14 -0800 Jakub Kicinski wrote: > > Mm. Actually stopping to look at the code - I don't see it bound > > checking against kernel_headers_data_end :| Maybe we need: > > > > @@ -34,6 +35,7 @@ ikheaders_read(struct file *file, struct kobject *kobj, > > struct bin_attribute *bin_attr, > > char *buf, loff_t off, size_t len) > > { > > + len = min_t(size_t, kernel_headers_data_end - kernel_headers_data, len); > > memcpy(buf, &kernel_headers_data + off, len); > > return len; > > } > > Scratch that, the size is set at init time. > My guess was memcpy() thinks the size of kernel_headers_data > is 1 since it's declared as char? Like this? ---->8-------------------- >From 7549153017144281c2475d9370962ad4d0ea8d4c Mon Sep 17 00:00:00 2001 From: Jakub Kicinski <kuba@xxxxxxxxxx> Date: Thu, 2 Mar 2023 11:15:53 -0800 Subject: [PATCH] kheaders: use a flex array for the symbol Kernels with hardened copy hit: detected buffer overflow in memcpy kernel BUG at lib/string_helpers.c:1027! invalid opcode: 0000 [#8] SMP KASAN CPU: 1 PID: 1094 Comm: tar Tainted: G D 6.2.0-12879-g040b4d2ce1ad #646 RIP: 0010:fortify_panic+0xf/0x20 [...] Call Trace: <TASK> ikheaders_read+0x45/0x50 [kheaders] kernfs_fop_read_iter+0x1a4/0x2f0 Repro: $ cat /sys/kernel/kheaders.tar.xz >> /dev/null Fixes: 43d8ce9d65a5 ("Provide in-kernel headers to make extending kernel easier") Signed-off-by: Jakub Kicinski <kuba@xxxxxxxxxx> --- kernel/kheaders.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/kheaders.c b/kernel/kheaders.c index 8f69772af77b..90562eb4093a 100644 --- a/kernel/kheaders.c +++ b/kernel/kheaders.c @@ -26,7 +26,7 @@ asm ( " .popsection \n" ); -extern char kernel_headers_data; +extern char kernel_headers_data[]; extern char kernel_headers_data_end; static ssize_t @@ -34,7 +34,7 @@ ikheaders_read(struct file *file, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len) { - memcpy(buf, &kernel_headers_data + off, len); + memcpy(buf, &kernel_headers_data[off], len); return len; } @@ -49,7 +49,7 @@ static struct bin_attribute kheaders_attr __ro_after_init = { static int __init ikheaders_init(void) { kheaders_attr.size = (&kernel_headers_data_end - - &kernel_headers_data); + &kernel_headers_data[0]); return sysfs_create_bin_file(kernel_kobj, &kheaders_attr); } -- 2.39.2