diff --git a/arch/arm64/configs/gki_defconfig b/arch/arm64/configs/gki_defconfig index bfc6c9a5edb8..e5e76ae4c559 100644 --- a/arch/arm64/configs/gki_defconfig +++ b/arch/arm64/configs/gki_defconfig @@ -81,6 +81,8 @@ CONFIG_SHADOW_CALL_STACK=y CONFIG_MODULES=y CONFIG_MODULE_UNLOAD=y CONFIG_MODVERSIONS=y +CONFIG_MODULE_SIG=y +CONFIG_MODULE_SIG_PROTECT=y CONFIG_BLK_DEV_ZONED=y CONFIG_BLK_INLINE_ENCRYPTION=y CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK=y diff --git a/arch/x86/configs/gki_defconfig b/arch/x86/configs/gki_defconfig index b566102368aa..7ecc97dfb518 100644 --- a/arch/x86/configs/gki_defconfig +++ b/arch/x86/configs/gki_defconfig @@ -71,6 +71,8 @@ CONFIG_JUMP_LABEL=y CONFIG_MODULES=y CONFIG_MODULE_UNLOAD=y CONFIG_MODVERSIONS=y +CONFIG_MODULE_SIG=y +CONFIG_MODULE_SIG_PROTECT=y CONFIG_BLK_DEV_ZONED=y CONFIG_BLK_INLINE_ENCRYPTION=y CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK=y diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 948efea81e85..756419001b30 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -10,6 +10,7 @@ KCOV_INSTRUMENT_module.o := n obj-y += main.o strict_rwx.o obj-$(CONFIG_MODULE_DECOMPRESS) += decompress.o obj-$(CONFIG_MODULE_SIG) += signing.o +obj-$(CONFIG_MODULE_SIG_PROTECT) += gki_module.o obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o obj-$(CONFIG_DEBUG_KMEMLEAK) += debug_kmemleak.o @@ -19,3 +20,14 @@ obj-$(CONFIG_SYSFS) += sysfs.o obj-$(CONFIG_KGDB_KDB) += kdb.o obj-$(CONFIG_MODVERSIONS) += version.o obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) += tracking.o + +# +# ANDROID: GKI: Generate headerfile required for gki_module.o +# +# Dependencies on generated files need to be listed explicitly +$(obj)/gki_module.o: $(obj)/gki_module_unprotected.h + +$(obj)/gki_module_unprotected.h: $(srctree)/scripts/gen_gki_modules_headers.sh \ + $(if $(wildcard ${OUT_DIR}/abi_symbollist.raw), ${OUT_DIR}/abi_symbollist.raw) + $(Q)$(CONFIG_SHELL) $(srctree)/scripts/gen_gki_modules_headers.sh $@ \ + "$(srctree)" diff --git a/kernel/module/gki_module.c b/kernel/module/gki_module.c new file mode 100644 index 000000000000..04989540b7b4 --- /dev/null +++ b/kernel/module/gki_module.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2022 Google LLC + * Author: ramjiyani@google.com (Ramji Jiyani) + */ + +#include +#include +#include +#include +#include + +/* + * Build time generated header files + * + * gki_module_unprotected.h -- Symbols allowed to _access_ by unsigned modules + */ +#include "gki_module_unprotected.h" + +/* bsearch() comparision callback */ +static int cmp_name(const void *sym, const void *protected_sym) +{ + return strncmp(sym, protected_sym, MAX_UNPROTECTED_NAME_LEN); +} + +/** + * gki_is_module_unprotected_symbol - Is a symbol unprotected for unsigned module? + * + * @name: Symbol being checked in list of unprotected symbols + */ +bool gki_is_module_unprotected_symbol(const char *name) +{ + return bsearch(name, gki_unprotected_symbols, NO_OF_UNPROTECTED_SYMBOLS, + MAX_UNPROTECTED_NAME_LEN, cmp_name) != NULL; +} diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2e2bf236f558..6f96a966f387 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -303,3 +303,12 @@ static inline int same_magic(const char *amagic, const char *bmagic, bool has_cr return strcmp(amagic, bmagic) == 0; } #endif /* CONFIG_MODVERSIONS */ + +#ifdef CONFIG_MODULE_SIG_PROTECT +extern bool gki_is_module_unprotected_symbol(const char *name); +#else +static inline bool gki_is_module_unprotected_symbol(const char *name) +{ + return true; +} +#endif /* CONFIG_MODULE_SIG_PROTECT */ diff --git a/kernel/module/main.c b/kernel/module/main.c index d02d39c7174e..eb1d5934b41b 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1095,6 +1095,21 @@ static const struct kernel_symbol *resolve_symbol(struct module *mod, goto getname; } + /* + * ANDROID: GKI: + * In case of an unsigned module symbol resolves only if: + * 1. Symbol is in the list of unprotected symbol list OR + * 2. If symbol owner is not NULL i.e. owner is another module; + * it has to be an unsigned module and not signed GKI module + * to protect symbols exported by signed GKI modules. + */ + if (!mod->sig_ok && + !gki_is_module_unprotected_symbol(name) && + fsa.owner && fsa.owner->sig_ok) { + fsa.sym = ERR_PTR(-EACCES); + goto getname; + } + err = ref_module(mod, fsa.owner); if (err) { fsa.sym = ERR_PTR(err); @@ -1327,9 +1342,15 @@ static int simplify_symbols(struct module *mod, const struct load_info *info) ignore_undef_symbol(info->hdr->e_machine, name))) break; - ret = PTR_ERR(ksym) ?: -ENOENT; - pr_warn("%s: Unknown symbol %s (err %d)\n", - mod->name, name, ret); + if (PTR_ERR(ksym) == -EACCES) { + ret = -EACCES; + pr_warn("%s: Protected symbol: %s (err %d)\n", + mod->name, name, ret); + } else { + ret = PTR_ERR(ksym) ?: -ENOENT; + pr_warn("%s: Unknown symbol %s (err %d)\n", + mod->name, name, ret); + } break; default: @@ -2749,6 +2770,8 @@ static int load_module(struct load_info *info, const char __user *uargs, "kernel\n", mod->name); add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK); } +#else + mod->sig_ok = 0; #endif /* To avoid stressing percpu allocator, do this once we're unique. */ diff --git a/kernel/module/signing.c b/kernel/module/signing.c index a2ff4242e623..7ffb2ae5d493 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -19,8 +19,20 @@ #undef MODULE_PARAM_PREFIX #define MODULE_PARAM_PREFIX "module." +/* + * ANDROID: GKI: + * Only enforce signature if SIG_PROTECT is not set + */ +#ifndef CONFIG_MODULE_SIG_PROTECT static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE); module_param(sig_enforce, bool_enable_only, 0644); +void set_module_sig_enforced(void) +{ + sig_enforce = true; +} +#else +#define sig_enforce false +#endif /* * Export sig_enforce kernel cmdline parameter to allow other subsystems rely @@ -32,11 +44,6 @@ bool is_module_sig_enforced(void) } EXPORT_SYMBOL(is_module_sig_enforced); -void set_module_sig_enforced(void) -{ - sig_enforce = true; -} - /* * Verify the signature on a module. */ @@ -121,5 +128,13 @@ int module_sig_check(struct load_info *info, int flags) return -EKEYREJECTED; } +/* + * ANDROID: GKI: Do not prevent loading of unsigned modules; + * as all modules except GKI modules are not signed. + */ +#ifndef CONFIG_MODULE_SIG_PROTECT return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); +#else + return 0; +#endif } diff --git a/scripts/gen_gki_modules_headers.sh b/scripts/gen_gki_modules_headers.sh new file mode 100755 index 000000000000..99f82bab8a02 --- /dev/null +++ b/scripts/gen_gki_modules_headers.sh @@ -0,0 +1,94 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright 2022 Google LLC +# Author: ramjiyani@google.com (Ramji Jiyani) +# + +# +# Generates header file with list of unprotected symbols +# +# Called By: KERNEL_SRC/kernel/Makefile if CONFIG_MODULE_SIG_PROTECT=y +# +# gki_module_unprotected.h: Symbols allowed to _access_ by unsigned modules +# +# If valid symbol file doesn't exists then still generates valid C header files for +# compilation to proceed with no symbols to protect +# + +# Collect arguments from Makefile +TARGET=$1 +SRCTREE=$2 + +set -e + +# +# Common Definitions +# +# Use "make V=1" to debug this script. +case "$KBUILD_VERBOSE" in +*1*) + set -x + ;; +esac + +# +# generate_header(): +# Args: $1 = Name of the header file +# $2 = Input symbol list +# $3 = Symbol type ("unprotected") +# +generate_header() { + local header_file=$1 + local symbol_file=$2 + local symbol_type=$3 + + if [ -f "${header_file}" ]; then + rm -f -- "${header_file}" + fi + + # Find Maximum symbol name length if valid symbol_file exist + if [ -s "${symbol_file}" ]; then + # Skip 1st line (symbol header), Trim white spaces & +1 for null termination + local max_name_len=$(awk ' + { + $1=$1; + if ( length > L && NR > 1) { + L=length + } + } END { print ++L }' "${symbol_file}") + else + # Set to 1 to generate valid C header file + local max_name_len=1 + fi + + # Header generation + cat > "${header_file}" <<- EOT + /* + * DO NOT EDIT + * + * Build generated header file with unprotected symbols/exports + */ + + #define NO_OF_$(printf ${symbol_type} | tr [:lower:] [:upper:])_SYMBOLS \\ + $(printf '\t')(sizeof(gki_${symbol_type}_symbols) / sizeof(gki_${symbol_type}_symbols[0])) + #define MAX_$(printf ${symbol_type} | tr [:lower:] [:upper:])_NAME_LEN (${max_name_len}) + + static const char gki_${symbol_type}_symbols[][MAX_$(printf ${symbol_type} | + tr [:lower:] [:upper:])_NAME_LEN] = { + EOT + + # If a valid symbol_file present add symbols in an array except the 1st line + if [ -s "${symbol_file}" ]; then + sed -e 's/^[ \t]*/\t"/;s/[ \t]*$/",/' "${symbol_file}" >> "${header_file}" + fi + + # Terminate the file + echo "};" >> "${header_file}" +} + +# Sorted list of vendor symbols +GKI_VENDOR_SYMBOLS="${OUT_DIR}/abi_symbollist.raw" + +generate_header "${TARGET}" "${GKI_VENDOR_SYMBOLS}" "unprotected" +