Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: do not include include/config/auto.conf from shell scripts

Richard Weinberger pointed out the risk of sourcing the kernel config
from shell scripts [1], and proposed some patches [2], [3]. It is a good
point, but it took a long time because I was wondering how to fix this.

This commit goes with simple grep approach because there are only a few
scripts including the kernel configuration.

scripts/link_vmlinux.sh has references to a bunch of CONFIG options,
all of which are boolean. I added is_enabled() helper as
scripts/package/{mkdebian,builddeb} do.

scripts/gen_autoksyms.sh uses 'eval', stating "to expand the whitelist
path". I removed it since it is the issue we are trying to fix.

I was a bit worried about the cost of invoking the grep command over
again. I extracted the grep parts from it, and measured the cost. It
was approximately 0.03 sec, which I hope is acceptable.

[test code]

$ cat test-grep.sh
#!/bin/sh

is_enabled() {
grep -q "^$1=y" include/config/auto.conf
}

is_enabled CONFIG_LTO_CLANG
is_enabled CONFIG_LTO_CLANG
is_enabled CONFIG_STACK_VALIDATION
is_enabled CONFIG_UNWINDER_ORC
is_enabled CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
is_enabled CONFIG_VMLINUX_VALIDATION
is_enabled CONFIG_FRAME_POINTER
is_enabled CONFIG_GCOV_KERNEL
is_enabled CONFIG_LTO_CLANG
is_enabled CONFIG_RETPOLINE
is_enabled CONFIG_X86_SMAP
is_enabled CONFIG_LTO_CLANG
is_enabled CONFIG_VMLINUX_MAP
is_enabled CONFIG_KALLSYMS_ALL
is_enabled CONFIG_KALLSYMS_ABSOLUTE_PERCPU
is_enabled CONFIG_KALLSYMS_BASE_RELATIVE
is_enabled CONFIG_DEBUG_INFO_BTF
is_enabled CONFIG_KALLSYMS
is_enabled CONFIG_DEBUG_INFO_BTF
is_enabled CONFIG_BPF
is_enabled CONFIG_BUILDTIME_TABLE_SORT
is_enabled CONFIG_KALLSYMS

$ time ./test-grep.sh
real 0m0.036s
user 0m0.027s
sys m0.009s

[1]: https://lore.kernel.org/all/1919455.eZKeABUfgV@blindfold/
[2]: https://lore.kernel.org/all/20180219092245.26404-1-richard@nod.at/
[3]: https://lore.kernel.org/all/20210920213957.1064-2-richard@nod.at/
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nicolas Schier <n.schier@avm.de>

+31 -36
+3 -8
scripts/gen_autoksyms.sh
··· 16 16 ;; 17 17 esac 18 18 19 - # We need access to CONFIG_ symbols 20 - . include/config/auto.conf 21 - 22 19 needed_symbols= 23 20 24 21 # Special case for modversions (see modpost.c) 25 - if [ -n "$CONFIG_MODVERSIONS" ]; then 22 + if grep -q "^CONFIG_MODVERSIONS=y$" include/config/auto.conf; then 26 23 needed_symbols="$needed_symbols module_layout" 27 24 fi 28 25 29 - ksym_wl= 30 - if [ -n "$CONFIG_UNUSED_KSYMS_WHITELIST" ]; then 31 - # Use 'eval' to expand the whitelist path and check if it is relative 32 - eval ksym_wl="$CONFIG_UNUSED_KSYMS_WHITELIST" 26 + ksym_wl=$(sed -n 's/^CONFIG_UNUSED_KSYMS_WHITELIST="\(.*\)"$/\1/p' include/config/auto.conf) 27 + if [ -n "$ksym_wl" ]; then 33 28 [ "${ksym_wl}" != "${ksym_wl#/}" ] || ksym_wl="$abs_srctree/$ksym_wl" 34 29 if [ ! -f "$ksym_wl" ] || [ ! -r "$ksym_wl" ]; then 35 30 echo "ERROR: '$ksym_wl' whitelist file not found" >&2
+4 -5
scripts/setlocalversion
··· 111 111 exit 112 112 fi 113 113 114 - if test -e include/config/auto.conf; then 115 - . include/config/auto.conf 116 - else 114 + if ! test -e include/config/auto.conf; then 117 115 echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 118 116 exit 1 119 117 fi ··· 123 125 fi 124 126 125 127 # CONFIG_LOCALVERSION and LOCALVERSION (if set) 126 - res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" 128 + config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION="\(.*\)"$/\1/p' include/config/auto.conf) 129 + res="${res}${config_localversion}${LOCALVERSION}" 127 130 128 131 # scm version string if not at a tagged commit 129 - if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then 132 + if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 130 133 # full scm version string 131 134 res="$res$(scm_version)" 132 135 elif [ "${LOCALVERSION+set}" != "set" ]; then