kheaders: Use array declaration instead of char

Message ID 20230302224946.never.243-kees@kernel.org
State New
Headers
Series kheaders: Use array declaration instead of char |

Commit Message

Kees Cook March 2, 2023, 10:49 p.m. UTC
  Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination
and source buffers. Defining kernel_headers_data as "char" would trip
this check. Since these addresses are treated as byte arrays, define
them as arrays (as done everywhere else).

This was seen with:

  $ cat /sys/kernel/kheaders.tar.xz >> /dev/null

  detected buffer overflow in memcpy
  kernel BUG at lib/string_helpers.c:1027!
  ...
  RIP: 0010:fortify_panic+0xf/0x20
  [...]
  Call Trace:
   <TASK>
   ikheaders_read+0x45/0x50 [kheaders]
   kernfs_fop_read_iter+0x1a4/0x2f0
  ...

Reported-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/bpf/20230302112130.6e402a98@kernel.org/
Tested-by: Jakub Kicinski <kuba@kernel.org>
Fixes: 43d8ce9d65a5 ("Provide in-kernel headers to make extending kernel easier")
Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
Cc: stable@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 kernel/kheaders.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
  

Comments

Joel Fernandes March 3, 2023, 3:08 a.m. UTC | #1
On Thu, Mar 02, 2023 at 02:49:50PM -0800, Kees Cook wrote:
> Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination
> and source buffers. Defining kernel_headers_data as "char" would trip
> this check. Since these addresses are treated as byte arrays, define
> them as arrays (as done everywhere else).
> 
> This was seen with:
> 
>   $ cat /sys/kernel/kheaders.tar.xz >> /dev/null
> 
>   detected buffer overflow in memcpy
>   kernel BUG at lib/string_helpers.c:1027!
>   ...
>   RIP: 0010:fortify_panic+0xf/0x20
>   [...]
>   Call Trace:
>    <TASK>
>    ikheaders_read+0x45/0x50 [kheaders]
>    kernfs_fop_read_iter+0x1a4/0x2f0
>   ...
> 
> Reported-by: Jakub Kicinski <kuba@kernel.org>
> Link: https://lore.kernel.org/bpf/20230302112130.6e402a98@kernel.org/
> Tested-by: Jakub Kicinski <kuba@kernel.org>

Acked-by: Joel Fernandes (Google) <joel@joelfernandes.org>

thanks,

 - Joel


> Fixes: 43d8ce9d65a5 ("Provide in-kernel headers to make extending kernel easier")
> Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
> Cc: stable@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  kernel/kheaders.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/kernel/kheaders.c b/kernel/kheaders.c
> index 8f69772af77b..42163c9e94e5 100644
> --- a/kernel/kheaders.c
> +++ b/kernel/kheaders.c
> @@ -26,15 +26,15 @@ asm (
>  "	.popsection				\n"
>  );
>  
> -extern char kernel_headers_data;
> -extern char kernel_headers_data_end;
> +extern char kernel_headers_data[];
> +extern char kernel_headers_data_end[];
>  
>  static ssize_t
>  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;
>  }
>  
> @@ -48,8 +48,8 @@ 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);
> +	kheaders_attr.size = (kernel_headers_data_end -
> +			      kernel_headers_data);
>  	return sysfs_create_bin_file(kernel_kobj, &kheaders_attr);
>  }
>  
> -- 
> 2.34.1
>
  
Alexander Lobakin March 3, 2023, 3:19 p.m. UTC | #2
From: Kees Cook <keescook@chromium.org>
Date: Thu,  2 Mar 2023 14:49:50 -0800

> Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination
> and source buffers. Defining kernel_headers_data as "char" would trip
> this check. Since these addresses are treated as byte arrays, define
> them as arrays (as done everywhere else).

Yet another array-as-one-char, I wonder how many are still here...

> 
> This was seen with:
> 
>   $ cat /sys/kernel/kheaders.tar.xz >> /dev/null
> 
>   detected buffer overflow in memcpy
>   kernel BUG at lib/string_helpers.c:1027!
>   ...
>   RIP: 0010:fortify_panic+0xf/0x20
>   [...]
>   Call Trace:
>    <TASK>
>    ikheaders_read+0x45/0x50 [kheaders]
>    kernfs_fop_read_iter+0x1a4/0x2f0
>   ...
> 
> Reported-by: Jakub Kicinski <kuba@kernel.org>
> Link: https://lore.kernel.org/bpf/20230302112130.6e402a98@kernel.org/
> Tested-by: Jakub Kicinski <kuba@kernel.org>
> Fixes: 43d8ce9d65a5 ("Provide in-kernel headers to make extending kernel easier")
> Cc: Joel Fernandes (Google) <joel@joelfernandes.org>
> Cc: stable@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>

Reviewed-by: Alexander Lobakin <aleksander.lobakin@intel.com>

> ---
>  kernel/kheaders.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
[...]

Thanks,
Olek
  
Kees Cook March 3, 2023, 8:36 p.m. UTC | #3
On Fri, Mar 03, 2023 at 04:19:51PM +0100, Alexander Lobakin wrote:
> From: Kees Cook <keescook@chromium.org>
> Date: Thu,  2 Mar 2023 14:49:50 -0800
> 
> > Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination
> > and source buffers. Defining kernel_headers_data as "char" would trip
> > this check. Since these addresses are treated as byte arrays, define
> > them as arrays (as done everywhere else).
> 
> Yet another array-as-one-char, I wonder how many are still here...

Yeah, good point. They do tend to stand out; we could find them:

$ git grep 'extern char [^\*\[\( ]*;'
arch/alpha/boot/bootp.c:extern char _end;
arch/alpha/boot/bootpz.c:extern char _end;
arch/alpha/boot/main.c: extern char _end;
arch/arm/mach-rockchip/core.h:extern char rockchip_secondary_trampoline;
arch/arm/mach-rockchip/core.h:extern char rockchip_secondary_trampoline_end;
arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline;
arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline_jump;
arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline_end;
arch/hexagon/include/uapi/asm/setup.h:extern char external_cmdline_buffer;
arch/ia64/include/asm/smp.h:extern char no_int_routing;
arch/ia64/kernel/process.c:     extern char ia64_ret_from_clone;
arch/mips/dec/prom/memory.c:    extern char genexcept_early;
arch/mips/kernel/traps.c:       extern char except_vec3_generic;
arch/mips/kernel/traps.c:       extern char except_vec4;
arch/mips/kernel/traps.c:       extern char except_vec3_r4000;
arch/mips/mm/c-octeon.c:        extern char except_vec2_octeon;
arch/parisc/boot/compressed/misc.c:extern char output_len;
arch/parisc/boot/compressed/misc.c:extern char _startcode_end;
arch/powerpc/include/asm/smp.h:extern char __secondary_hold;
arch/s390/include/asm/kvm_host.h:extern char sie_exit;
arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_enter_start;
arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_enter_end;
arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_leave_start;
arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_leave_end;
arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_enter_start;
arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_enter_end;
arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_leave_start;
arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_leave_end;
arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_enter_start;
arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_enter_end;
arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_leave_start;
arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_leave_end;
arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_enter_start;
arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_enter_end;
arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_leave_start;
arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_leave_end;
arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_enter_start;
arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_enter_end;
arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_leave_start;
arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_leave_end;
arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_enter_start;
arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_enter_end;
arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_resume_start;
arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_resume_end;
arch/x86/entry/vsyscall/vsyscall_64.c:  extern char __vsyscall_page;
arch/x86/include/asm/vvar.h:extern char __vvar_page;
kernel/configs.c:extern char kernel_config_data;
kernel/configs.c:extern char kernel_config_data_end;
net/bpfilter/bpfilter_kern.c:extern char bpfilter_umh_start;
net/bpfilter/bpfilter_kern.c:extern char bpfilter_umh_end;
samples/bpf/task_fd_query_user.c:       extern char __executable_start;
tools/testing/selftests/kvm/lib/aarch64/processor.c:    extern char vectors;
tools/testing/selftests/x86/test_syscall_vdso.c:extern char int80;

Of those, it looks like only a handful might trip FORTIFY:

$ for i in $(git grep 'extern char [^\*\[\( ]*;' | grep -v boot/ | awk -F' ' '{print $NF}' | cut -d';' -f1); do git grep -E '(strcpy|memcpy|memset).*'"$i",; done
arch/arm/mach-rockchip/platsmp.c:       memcpy_toio(sram_base_addr, &rockchip_secondary_trampoline, trampoline_sz);
arch/arm/mach-zynq/platsmp.c:                   memcpy_toio(zero, &zynq_secondary_trampoline,
arch/mips/dec/prom/memory.c:    memcpy((void *)(CKSEG0 + 0x80), &genexcept_early, 0x80);
arch/sh/kernel/cpu/shmobile/pm.c:       memcpy(vp, &sh_mobile_sleep_enter_start, n);
arch/sh/kernel/cpu/shmobile/pm.c:       memcpy(vp, &sh_mobile_sleep_resume_start, n);
arch/arm64/mm/trans_pgd.c:      memcpy(hyp_stub, &trans_pgd_stub_vectors, ARM64_VECTOR_TABLE_LEN);
  
Alexander Lobakin March 6, 2023, 10:39 a.m. UTC | #4
From: Kees Cook <keescook@chromium.org>
Date: Fri, 3 Mar 2023 12:36:09 -0800

> On Fri, Mar 03, 2023 at 04:19:51PM +0100, Alexander Lobakin wrote:
>> From: Kees Cook <keescook@chromium.org>
>> Date: Thu,  2 Mar 2023 14:49:50 -0800
>>
>>> Under CONFIG_FORTIFY_SOURCE, memcpy() will check the size of destination
>>> and source buffers. Defining kernel_headers_data as "char" would trip
>>> this check. Since these addresses are treated as byte arrays, define
>>> them as arrays (as done everywhere else).
>>
>> Yet another array-as-one-char, I wonder how many are still here...
> 
> Yeah, good point. They do tend to stand out; we could find them:
> 
> $ git grep 'extern char [^\*\[\( ]*;'
> arch/alpha/boot/bootp.c:extern char _end;
> arch/alpha/boot/bootpz.c:extern char _end;
> arch/alpha/boot/main.c: extern char _end;
> arch/arm/mach-rockchip/core.h:extern char rockchip_secondary_trampoline;
> arch/arm/mach-rockchip/core.h:extern char rockchip_secondary_trampoline_end;
> arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline;
> arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline_jump;
> arch/arm/mach-zynq/common.h:extern char zynq_secondary_trampoline_end;
> arch/hexagon/include/uapi/asm/setup.h:extern char external_cmdline_buffer;
> arch/ia64/include/asm/smp.h:extern char no_int_routing;
> arch/ia64/kernel/process.c:     extern char ia64_ret_from_clone;
> arch/mips/dec/prom/memory.c:    extern char genexcept_early;
> arch/mips/kernel/traps.c:       extern char except_vec3_generic;
> arch/mips/kernel/traps.c:       extern char except_vec4;
> arch/mips/kernel/traps.c:       extern char except_vec3_r4000;
> arch/mips/mm/c-octeon.c:        extern char except_vec2_octeon;
> arch/parisc/boot/compressed/misc.c:extern char output_len;
> arch/parisc/boot/compressed/misc.c:extern char _startcode_end;
> arch/powerpc/include/asm/smp.h:extern char __secondary_hold;
> arch/s390/include/asm/kvm_host.h:extern char sie_exit;
> arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_enter_start;
> arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_enter_end;
> arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_leave_start;
> arch/sh/boards/mach-ap325rxa/setup.c:extern char ap325rxa_sdram_leave_end;
> arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_enter_start;
> arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_enter_end;
> arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_leave_start;
> arch/sh/boards/mach-ecovec24/setup.c:extern char ecovec24_sdram_leave_end;
> arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_enter_start;
> arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_enter_end;
> arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_leave_start;
> arch/sh/boards/mach-kfr2r09/setup.c:extern char kfr2r09_sdram_leave_end;
> arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_enter_start;
> arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_enter_end;
> arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_leave_start;
> arch/sh/boards/mach-migor/setup.c:extern char migor_sdram_leave_end;
> arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_enter_start;
> arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_enter_end;
> arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_leave_start;
> arch/sh/boards/mach-se/7724/setup.c:extern char ms7724se_sdram_leave_end;
> arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_enter_start;
> arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_enter_end;
> arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_resume_start;
> arch/sh/kernel/cpu/shmobile/pm.c:extern char sh_mobile_sleep_resume_end;
> arch/x86/entry/vsyscall/vsyscall_64.c:  extern char __vsyscall_page;
> arch/x86/include/asm/vvar.h:extern char __vvar_page;
> kernel/configs.c:extern char kernel_config_data;
> kernel/configs.c:extern char kernel_config_data_end;
> net/bpfilter/bpfilter_kern.c:extern char bpfilter_umh_start;
> net/bpfilter/bpfilter_kern.c:extern char bpfilter_umh_end;
> samples/bpf/task_fd_query_user.c:       extern char __executable_start;
> tools/testing/selftests/kvm/lib/aarch64/processor.c:    extern char vectors;
> tools/testing/selftests/x86/test_syscall_vdso.c:extern char int80;
> 
> Of those, it looks like only a handful might trip FORTIFY:
> 
> $ for i in $(git grep 'extern char [^\*\[\( ]*;' | grep -v boot/ | awk -F' ' '{print $NF}' | cut -d';' -f1); do git grep -E '(strcpy|memcpy|memset).*'"$i",; done
> arch/arm/mach-rockchip/platsmp.c:       memcpy_toio(sram_base_addr, &rockchip_secondary_trampoline, trampoline_sz);
> arch/arm/mach-zynq/platsmp.c:                   memcpy_toio(zero, &zynq_secondary_trampoline,
> arch/mips/dec/prom/memory.c:    memcpy((void *)(CKSEG0 + 0x80), &genexcept_early, 0x80);
> arch/sh/kernel/cpu/shmobile/pm.c:       memcpy(vp, &sh_mobile_sleep_enter_start, n);
> arch/sh/kernel/cpu/shmobile/pm.c:       memcpy(vp, &sh_mobile_sleep_resume_start, n);
> arch/arm64/mm/trans_pgd.c:      memcpy(hyp_stub, &trans_pgd_stub_vectors, ARM64_VECTOR_TABLE_LEN);
> 
> 
This list looks much better than the first one :D

I remember I caught a couple fortify bugs due to such array declaration
on MIPS only when testing with `KCFLAGS=-O3`. The thing was that a
pointer to such array-not-array was passed as a function argument and
that function wasn't inlined on -O2. So I sometimes use this optlevel
for sanity-checking the code :P

Thanks,
Olek
  

Patch

diff --git a/kernel/kheaders.c b/kernel/kheaders.c
index 8f69772af77b..42163c9e94e5 100644
--- a/kernel/kheaders.c
+++ b/kernel/kheaders.c
@@ -26,15 +26,15 @@  asm (
 "	.popsection				\n"
 );
 
-extern char kernel_headers_data;
-extern char kernel_headers_data_end;
+extern char kernel_headers_data[];
+extern char kernel_headers_data_end[];
 
 static ssize_t
 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;
 }
 
@@ -48,8 +48,8 @@  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);
+	kheaders_attr.size = (kernel_headers_data_end -
+			      kernel_headers_data);
 	return sysfs_create_bin_file(kernel_kobj, &kheaders_attr);
 }