module : fix signature checker pointer arithmetic and bounds check
PATCH 1/1 module : fix signature checker pointer arithmetic and bounds check
From: Fidal palamparambil <[email protected]>
This patch fixes :
- invalid module_param type (bool_enable_only → bool)
- unsafe pointer arithmetic on void *
- missing bounds check for sig_len, preventing underflow/OOB
- export set_module_sig_enforced for consistency
--- kernel/module/signing.c | 48 ++++++++------ kernel/module/signing.orig | 125 +++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 18 deletions(-) create mode 100644 kernel/module/signing.orig
kernel/module/signing.c +30 -18
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-or-later -/* Module signature checker +/* +* Module signature checker * * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected])
@@ -20,11 +21,11 @@#define MODULE_PARAM_PREFIX "module."static bool sig_enforce =IS_ENABLED (CONFIG_MODULE_SIG_FORCE ); -module_param (sig_enforce, bool_enable_only,0644 ); +module_param (sig_enforce, bool,0644 );/* -* Export sig_enforce kernel cmdline parameter to allow other subsystems rely -* on that instead of directly to CONFIG_MODULE_SIG_FORCE config. +* Export sig_enforce kernel cmdline parameter to allow other subsystems to +* rely on that instead of directly on CONFIG_MODULE_SIG_FORCE config. */ bool is_module_sig_enforced (void ) {
@@ -36,6 +37,7 @@ void set_module_sig_enforced(void) { sig_enforce = true; } +EXPORT_SYMBOL (set_module_sig_enforced);/* * Verify the signature on a module.
@@ -45,44 +47,55 @@ int mod_verify_sig(const void *mod, struct load_info *info)struct module_signature ms;size_t sig_len, modlen = info->len;int ret; +const unsigned char *data = mod;pr_devel ("==>%s(,%zu)\n" , __func__, modlen);if (modlen <=sizeof (ms))return -EBADMSG ; -memcpy (&ms, mod + (modlen -sizeof (ms)),sizeof (ms)); +memcpy (&ms, data + (modlen -sizeof (ms)),sizeof (ms)); ret =mod_check_sig (&ms, modlen,"module" );if (ret)return ret; sig_len =be32_to_cpu (ms.sig_len); + +/* Ensure sig_len is valid to prevent underflow/oob */ +if (sig_len > modlen -sizeof (ms)) +return -EBADMSG ; + modlen -= sig_len +sizeof (ms); info->len = modlen; -return verify_pkcs7_signature (mod, modlen, mod + modlen, sig_len, +return verify_pkcs7_signature (data, modlen, data + modlen, sig_len,VERIFY_USE_SECONDARY_KEYRING ,VERIFYING_MODULE_SIGNATURE ,NULL ,NULL ); } +/* +* Check signature validity of a module during load. +*/ int module_sig_check (struct load_info *info,int flags) {int err = -ENODATA ;const unsigned long markerlen =sizeof (MODULE_SIG_STRING ) -1 ;const char *reason; -const void *mod = info->hdr; +const unsigned char *mod = info->hdr;bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS |MODULE_INIT_IGNORE_VERMAGIC ); +/* -* Do not allow mangled modules as a module with version information -* removed is no longer the module that was signed. +* Do not allow mangled modules: a module with version info removed +* is no longer the module that was signed. */ if (!mangled_module && info->len > markerlen && -memcmp (mod + info->len - markerlen,MODULE_SIG_STRING , markerlen) ==0 ) { -/* We truncate the module to discard the signature */ +memcmp (mod + info->len - markerlen, +MODULE_SIG_STRING , markerlen) ==0 ) { +/* Truncate the module to discard the signature marker */ info->len -= markerlen; err =mod_verify_sig (mod, info);if (!err) {
@@ -92,9 +105,8 @@ int module_sig_check(struct load_info *info, int flags) }/* -* We don't permit modules to be loaded into the trusted kernels -* without a valid signature on them, but if we're not enforcing, -* certain errors are non-fatal. +* Enforced mode: only allow modules with a valid signature. +* Non-enforced mode: certain errors are downgraded to warnings. */ switch (err) {case -ENODATA :
@@ -106,12 +118,12 @@ int module_sig_check(struct load_info *info, int flags)case -ENOKEY : reason ="module with unavailable key" ;break ; -default :/* -* All other errors are fatal, including lack of memory, -* unparseable signatures, and signature check failures -- -* even if signatures aren't required. +* All other errors are fatal, including: +* - OOM +* - unparseable signatures +* - invalid signature failures */ return err; }
kernel/module/signing.orig new file +125 -0
125 lines not on this page · raw message
Signature · 1 line
2.50.1.windows.1
Luis Chamberlain
maintainerQuoted 10 lines · Fidal Palamparambil
From: Fidal palamparambil <[email protected]>
This patch fixes :
- invalidmodule_paramtype (bool_enable_only→ bool)
- unsafe pointer arithmetic on void *
- missing bounds check forsig_len, preventing underflow/OOB
- exportset_module_sig_enforcedfor consistencySigned-off-by Fidal Palamparambil <[email protected]>Signed-off-by Fidal palamparambil <[email protected]>
I will ask that other maintainer ignore your patches moving forward. Clearly this is garbage gen AI crap code. The list, the double SOB, and your clear wreckless post is a good example to just never want to apply any patch from you.
Luis
Le 05/09/2025 à 17:45, Fidal Palamparambil a écrit :
Quoted 4 lines
From: Fidal palamparambil <[email protected]>
This patch fixes :
- invalidmodule_paramtype (bool_enable_only→ bool)
Can you explain what the problem is ? Why do you say bool_enable_only is invalid ? Was generalised by commit d19f05d8a8fa ("kernel/params.c: generalize bool_enable_only")
Quoted 1 line
- unsafe pointer arithmetic on void *
Why is it unsafe in Linux Kernel ? See https://lkml.org/lkml/2022/2/24/978
Quoted 1 line
- missing bounds check for
sig_len, preventing underflow/OOB
This is checked by mod_check_sig(), why check it again ?
Quoted 1 line
- export
set_module_sig_enforcedfor consistency
Consistency with what ? Can you tell which module needs it ?
Quoted 3 lines
Signed-off-by Fidal Palamparambil <[email protected]>Signed-off-by Fidal palamparambil <[email protected]>
Why a double sob ?
Quoted 3 lines
--- kernel/module/signing.c | 48 ++++++++------ kernel/module/signing.orig | 125 +++++++++++++++++++++++++++++++++++++
Why adding this .orig file into the kernel at all ?
kernel/module/signing.c line 3
Quoted 12 lines
2 files changed, 155 insertions(+), 18 deletions(-)create mode 100644
kernel/module/signing.origkernel/module/signing.c +2
-1@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-or-later -/* Module signature checker +/* +* Module signature checker
Don't mix cosmetic changes and real changes, you are making bisectability more difficult.
Quoted 27 lines
kernel/module/signing.c +4
-3* * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected]) @@ -20,11 +21,11 @@#define MODULE_PARAM_PREFIX "module."static bool sig_enforce =IS_ENABLED (CONFIG_MODULE_SIG_FORCE ); -module_param (sig_enforce, bool_enable_only,0644 ); +module_param (sig_enforce, bool,0644 );/* -* Export sig_enforce kernel cmdline parameter to allow other subsystems rely -* on that instead of directly to CONFIG_MODULE_SIG_FORCE config. +* Export sig_enforce kernel cmdline parameter to allow other subsystems to +* rely on that instead of directly on CONFIG_MODULE_SIG_FORCE config. */ bool is_module_sig_enforced (void ) {@@ -36,6 +37,7 @@ void set_module_sig_enforced(void) { sig_enforce = true; } +EXPORT_SYMBOL (set_module_sig_enforced);/* * Verify the signature on a module.
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 50
Quoted 4 lines
kernel/module/signing.c +1
-0@@ -45,44 +47,55 @@ int mod_verify_sig(const void *mod, struct load_info *info)struct module_signature ms;size_t sig_len, modlen = info->len;int ret; +const unsigned char *data = mod;
Pointless change.
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 57
Quoted 8 lines
kernel/module/signing.c +1
-1pr_devel ("==>%s(,%zu)\n" , __func__, modlen);if (modlen <=sizeof (ms))return -EBADMSG ; -memcpy (&ms, mod + (modlen -sizeof (ms)),sizeof (ms)); +memcpy (&ms, data + (modlen -sizeof (ms)),sizeof (ms));
Pointless change
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 67
Quoted 10 lines
kernel/module/signing.c +4
-0ret =mod_check_sig (&ms, modlen,"module" );if (ret)return ret; sig_len =be32_to_cpu (ms.sig_len); + +/* Ensure sig_len is valid to prevent underflow/oob */ +if (sig_len > modlen -sizeof (ms)) +return -EBADMSG ;
Already verified by mod_check_sig()
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 72
Quoted 6 lines
kernel/module/signing.c +2
-1+ modlen -= sig_len +sizeof (ms); info->len = modlen; - return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, + return verify_pkcs7_signature(data, modlen, data + modlen, sig_len,
pointless change
Quoted 5 lines
kernel/module/signing.c +0
-0VERIFY_USE_SECONDARY_KEYRING ,VERIFYING_MODULE_SIGNATURE , NULL, NULL); }
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 86
Quoted 10 lines
kernel/module/signing.c +4
-1+/* +* Check signature validity of a module during load. +*/ int module_sig_check (struct load_info *info,int flags) {int err = -ENODATA ;const unsigned long markerlen =sizeof (MODULE_SIG_STRING ) -1 ;const char *reason; -const void *mod = info->hdr; +const unsigned char *mod = info->hdr;
info->hdr is not void*, how can this work without a cast ?
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 89
Quoted 3 lines
kernel/module/signing.c +1
-0bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS |MODULE_INIT_IGNORE_VERMAGIC ); +
Unrelated cosmetic change
kernel/module/signing.c int mod_verify_sig(const void *mod, struct load_info *info) · line 98
Quoted 13 lines
kernel/module/signing.c +5
-4/* -* Do not allow mangled modules as a module with version information -* removed is no longer the module that was signed. +* Do not allow mangled modules: a module with version info removed +* is no longer the module that was signed. */ if (!mangled_module && info->len > markerlen && -memcmp (mod + info->len - markerlen,MODULE_SIG_STRING , markerlen) ==0 ) { -/* We truncate the module to discard the signature */ +memcmp (mod + info->len - markerlen, +MODULE_SIG_STRING , markerlen) ==0 ) { +/* Truncate the module to discard the signature marker */
Cosmetic and pointless change.
Quoted 16 lines
kernel/module/signing.c +2
-3info->len -= markerlen; err =mod_verify_sig (mod, info);if (!err) {@@ -92,9 +105,8 @@ int module_sig_check(struct load_info *info, int flags) }/* -* We don't permit modules to be loaded into the trusted kernels -* without a valid signature on them, but if we're not enforcing, -* certain errors are non-fatal. +* Enforced mode: only allow modules with a valid signature. +* Non-enforced mode: certain errors are downgraded to warnings. */ switch (err) {case -ENODATA :
kernel/module/signing.c int module_sig_check(struct load_info *info, int flags) · line 109
Quoted 4 lines
kernel/module/signing.c +0
-1@@ -106,12 +118,12 @@ int module_sig_check(struct load_info *info, int flags)case -ENOKEY : reason ="module with unavailable key" ;break ; -
Cosmetic
Quoted 147 lines
kernel/module/signing.c +4
-3default :/* -* All other errors are fatal, including lack of memory, -* unparseable signatures, and signature check failures -- -* even if signatures aren't required. +* All other errors are fatal, including: +* - OOM +* - unparseable signatures +* - invalid signature failures */ return err; }kernel/module/signing.orig new file +125
-1@@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* Module signature checker + * + * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. + * Written by David Howells ([email protected]) + */ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/module.h> +#include <linux/module_signature.h> +#include <linux/string.h> +#include <linux/verification.h> +#include <linux/security.h> +#include <crypto/public_key.h> +#include <uapi/linux/module.h> +#include "internal.h" + +#undef MODULE_PARAM_PREFIX +#define MODULE_PARAM_PREFIX "module." + +static bool sig_enforce = IS_ENABLED(CONFIG_MODULE_SIG_FORCE); +module_param(sig_enforce, bool_enable_only, 0644); + +/* + * Export sig_enforce kernel cmdline parameter to allow other subsystems rely + * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. + */ +bool is_module_sig_enforced(void) +{ + return sig_enforce; +} +EXPORT_SYMBOL(is_module_sig_enforced); + +void set_module_sig_enforced(void) +{ + sig_enforce = true; +} + +/* + * Verify the signature on a module. + */ +int mod_verify_sig(const void *mod, struct load_info *info) +{ + struct module_signature ms; + size_t sig_len, modlen = info->len; + int ret; + + pr_devel("==>%s(,%zu)\n", __func__, modlen); + + if (modlen <= sizeof(ms)) + return -EBADMSG; + + memcpy(&ms, mod + (modlen - sizeof(ms)), sizeof(ms)); + + ret = mod_check_sig(&ms, modlen, "module"); + if (ret) + return ret; + + sig_len = be32_to_cpu(ms.sig_len); + modlen -= sig_len + sizeof(ms); + info->len = modlen; + + return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, + VERIFY_USE_SECONDARY_KEYRING, + VERIFYING_MODULE_SIGNATURE, + NULL, NULL); +} + +int module_sig_check(struct load_info *info, int flags) +{ + int err = -ENODATA; + const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1; + const char *reason; + const void *mod = info->hdr; + bool mangled_module = flags & (MODULE_INIT_IGNORE_MODVERSIONS | + MODULE_INIT_IGNORE_VERMAGIC); + /* + * Do not allow mangled modules as a module with version information + * removed is no longer the module that was signed. + */ + if (!mangled_module && + info->len > markerlen && + memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { + /* We truncate the module to discard the signature */ + info->len -= markerlen; + err = mod_verify_sig(mod, info); + if (!err) { + info->sig_ok = true; + return 0; + } + } + + /* + * We don't permit modules to be loaded into the trusted kernels + * without a valid signature on them, but if we're not enforcing, + * certain errors are non-fatal. + */ + switch (err) { + case -ENODATA: + reason = "unsigned module"; + break; + case -ENOPKG: + reason = "module with unsupported crypto"; + break; + case -ENOKEY: + reason = "module with unavailable key"; + break; + + default: + /* + * All other errors are fatal, including lack of memory, + * unparseable signatures, and signature check failures -- + * even if signatures aren't required. + */ + return err; + } + + if (is_module_sig_enforced()) { + pr_notice("Loading of %s is rejected\n", reason); + return -EKEYREJECTED; + } + + return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); +} --2.50.1.windows.1
Luis Chamberlain
maintainerQuoted 11 lines · Christophe Leroy
Le 05/09/2025 à 17:45, Fidal Palamparambil a écrit :
Quoted 4 lines
From: Fidal palamparambil <[email protected]>
This patch fixes :
- invalidmodule_paramtype (bool_enable_only→ bool)Can you explain what the problem is ? Why do you say
bool_enable_onlyis invalid ? Was generalised by commitd19f05d8a8fa("kernel/params.c:
generalizebool_enable_only")
Christophe, I will try to save you time. You can ignore the submitter's patch. They are a troll. I recommend you add them to your ignore list.
Luis