[PATCH v3] module: Add log info for verifying module signature
Add log information in kernel-space when loading module failures. Try to load the unsigned module and the module with bad signature when set 1 to /sys/module/module/parameters/sig_enforce. Unsigned module case: (linux) insmod unsigned.ko [ 18.714661] Loading of unsigned module is rejected insmod: can't insert 'unsigned.ko': Key was rejected by service (linux) Bad signature module case: (linux) insmod bad_signature.ko insmod: can't insert 'bad_signature.ko': Key was rejected by service (linux) There have different logging behavior the bad signature case only log in user-space, add log info for fatal errors in module_sig_check(). Signed-off-by: Yusong Gao --- V3: Clarify the message type and the error code meaning. V2: Change print level from notice to debug. --- kernel/module/signing.c | 27 +++ 1 file changed, 27 insertions(+) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index a2ff4242e623..826cdab8e3e4 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -67,6 +67,31 @@ int mod_verify_sig(const void *mod, struct load_info *info) NULL, NULL); } +static const char *mod_decode_error(int errno) +{ + char *errstr = "Unrecognized error"; + + switch (errno) { + case -ENOMEM: + errstr = "Out of memory"; + break; + case -EINVAL: + errstr = "Invalid argument"; + break; + case -EBADMSG: + errstr = "Invaild module signature format"; + break; + case -EMSGSIZE: + errstr = "Message too long"; + break; + case -EKEYREJECTED: + errstr = "Key was rejected by service"; + break; + } + + return errstr; +} + int module_sig_check(struct load_info *info, int flags) { int err = -ENODATA; @@ -113,6 +138,8 @@ int module_sig_check(struct load_info *info, int flags) * unparseable signatures, and signature check failures -- * even if signatures aren't required. */ + pr_debug("Verifying module signature failed: %s\n", +mod_decode_error(err)); return err; } -- 2.34.1
[PATCH v2] module: Add log information for loading module failures
Add log information in kernel-space when loading module failures. Try to load the unsigned module and the module with bad signature when set 1 to /sys/module/module/parameters/sig_enforce. Unsigned module case: (linux) insmod unsigned.ko [ 18.714661] Loading of unsigned module is rejected insmod: can't insert 'unsigned.ko': Key was rejected by service (linux) Bad signature module case: (linux) insmod bad_signature.ko insmod: can't insert 'bad_signature.ko': Key was rejected by service (linux) There have different logging behavior the bad signature case only log in user-space, add log info for fatal errors in module_sig_check(). Signed-off-by: Yusong Gao --- V2: Change print level from notice to debug. --- kernel/module/signing.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index a2ff4242e623..b0c7b30e4d89 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -113,6 +113,7 @@ int module_sig_check(struct load_info *info, int flags) * unparseable signatures, and signature check failures -- * even if signatures aren't required. */ + pr_debug("Loading module failed (errno=%d)\n", -err); return err; } -- 2.34.1
Re: [PATCH] module: Add log information for loading module failures
On 6/19/24 02:54, Luis Chamberlain wrote: On Fri, Jun 14, 2024 at 09:25:19AM +, Yusong Gao wrote: Add log information in kernel-space when loading module failures. Try to load the unsigned module and the module with bad signature when set 1 to /sys/module/module/parameters/sig_enforce. Unsigned module case: (linux) insmod unsigned.ko [ 18.714661] Loading of unsigned module is rejected insmod: can't insert 'unsigned.ko': Key was rejected by service (linux) Bad signature module case: (linux) insmod bad_signature.ko insmod: can't insert 'bad_signature.ko': Key was rejected by service (linux) There have different logging behavior the bad signature case only log in user-space, add log info for fatal errors in module_sig_check(). Signed-off-by: Yusong Gao --- kernel/module/signing.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index a2ff4242e623..6a6493c8f7e4 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -113,6 +113,7 @@ int module_sig_check(struct load_info *info, int flags) * unparseable signatures, and signature check failures -- * even if signatures aren't required. */ + pr_notice("Loading module failed (errno=%d)\n", -err); return err; I welcome pr_debug() messages but if we were to add a regular print for every single type of failure we'd clutter the code, we don't want that. Luis Thanks for your reply! Agreed. I'll make that change. The reason I select the notice print level is because I found the codes in module_sig_check() use notice level when signature enforced. In fact the pr_debug() is more suitable for this scene. BR, Yusong Gao
[PATCH] module: Add log information for loading module failures
Add log information in kernel-space when loading module failures. Try to load the unsigned module and the module with bad signature when set 1 to /sys/module/module/parameters/sig_enforce. Unsigned module case: (linux) insmod unsigned.ko [ 18.714661] Loading of unsigned module is rejected insmod: can't insert 'unsigned.ko': Key was rejected by service (linux) Bad signature module case: (linux) insmod bad_signature.ko insmod: can't insert 'bad_signature.ko': Key was rejected by service (linux) There have different logging behavior the bad signature case only log in user-space, add log info for fatal errors in module_sig_check(). Signed-off-by: Yusong Gao --- kernel/module/signing.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index a2ff4242e623..6a6493c8f7e4 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -113,6 +113,7 @@ int module_sig_check(struct load_info *info, int flags) * unparseable signatures, and signature check failures -- * even if signatures aren't required. */ + pr_notice("Loading module failed (errno=%d)\n", -err); return err; } -- 2.34.1