• Kees Cook's avatar
    x86/boot/compressed: Avoid duplicate malloc() implementations · 33f98a97
    Kees Cook authored
    The early malloc() and free() implementation in include/linux/decompress/mm.h
    (which is also included by the static decompressors) is static. This is
    fine when the only thing interested in using malloc() is the decompression
    code, but the x86 early boot environment may use malloc() in a couple places,
    leading to a potential collision when the static copies of the available
    memory region ("malloc_ptr") gets reset to the global "free_mem_ptr" value.
    As it happened, the existing usage pattern was accidentally safe because each
    user did 1 malloc() and 1 free() before returning and were not nested:
    
    extract_kernel() (misc.c)
    	choose_random_location() (kaslr.c)
    		mem_avoid_init()
    			handle_mem_options()
    				malloc()
    				...
    				free()
    	...
    	parse_elf() (misc.c)
    		malloc()
    		...
    		free()
    
    Once the future FGKASLR series is added, however, it will insert
    additional malloc() calls local to fgkaslr.c in the middle of
    parse_elf()'s malloc()/free() pair:
    
    	parse_elf() (misc.c)
    		malloc()
    		if (...) {
    			layout_randomized_image(output, &ehdr, phdrs);
    				malloc() <- boom
    				...
    		else
    			layout_image(output, &ehdr, phdrs);
    		free()
    
    To avoid collisions, there must be a single implementation of malloc().
    Adjust include/linux/decompress/mm.h so that visibility can be
    controlled, provide prototypes in misc.h, and implement the functions in
    misc.c. This also results in a small size savings:
    
    $ size vmlinux.before vmlinux.after
       text    data     bss     dec     hex filename
    8842314     468  178320 9021102  89a6ae vmlinux.before
    8842240     468  178320 9021028  89a664 vmlinux.after
    Signed-off-by: default avatarKees Cook <keescook@chromium.org>
    Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lore.kernel.org/r/20211013175742.1197608-4-keescook@chromium.org
    33f98a97
kaslr.c 23.1 KB