Re: [PATCH] kheaders: Use array declaration instead of char

From: Alexander Lobakin
Date: Mon Mar 06 2023 - 05:40:42 EST


From: Kees Cook <keescook@xxxxxxxxxxxx>
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@xxxxxxxxxxxx>
>> 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