From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2CCFC433E7 for ; Tue, 13 Oct 2020 22:44:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B124921D40 for ; Tue, 13 Oct 2020 22:44:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729235AbgJMWor (ORCPT ); Tue, 13 Oct 2020 18:44:47 -0400 Received: from smtprelay0146.hostedemail.com ([216.40.44.146]:39850 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728895AbgJMWor (ORCPT ); Tue, 13 Oct 2020 18:44:47 -0400 Received: from filter.hostedemail.com (clb03-v110.bra.tucows.net [216.40.38.60]) by smtprelay04.hostedemail.com (Postfix) with ESMTP id DA480180A8127; Tue, 13 Oct 2020 22:44:45 +0000 (UTC) X-Session-Marker: 6A6F6540706572636865732E636F6D X-HE-Tag: joke67_2600c5827206 X-Filterd-Recvd-Size: 3722 Received: from XPS-9350 (cpe-72-134-80-165.natsow.res.rr.com [72.134.80.165]) (Authenticated sender: joe@perches.com) by omf10.hostedemail.com (Postfix) with ESMTPA; Tue, 13 Oct 2020 22:44:44 +0000 (UTC) Message-ID: <3897a8ce5760affa304952c2d30c1266589407f2.camel@perches.com> Subject: Re: [PATCH 0/2] module: some refactoring in module_sig_check() From: Joe Perches To: Sergey Shtylyov , Jessica Yu , linux-kernel@vger.kernel.org Date: Tue, 13 Oct 2020 15:44:42 -0700 In-Reply-To: <789a4e5c-8efd-bb1c-86e2-eed8b2b7b0af@omprussia.ru> References: <789a4e5c-8efd-bb1c-86e2-eed8b2b7b0af@omprussia.ru> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.36.4-0ubuntu1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2020-10-13 at 23:32 +0300, Sergey Shtylyov wrote: > Here are 2 patches against the 'modules-next' branch of Jessica Yu's 'linux.git' repo. > I'm doing some little refactoring in module_sig_check()... > > [1/2] module: merge repetitive strings in module_sig_check() > [2/2] module: unindent comments in module_sig_check() I think this code is rather cryptic and could be made clearer. How about: --- kernel/module.c | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/kernel/module.c b/kernel/module.c index c075a18103fb..98b3af96a5bd 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -2884,46 +2884,47 @@ static int module_sig_check(struct load_info *info, int flags) * Require flags == 0, as a module with version information * removed is no longer the module that was signed */ - if (flags == 0 && - info->len > markerlen && - memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) { + if (flags == 0 && info->len > markerlen && + !memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen)) { /* 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 trusted kernels + * without a valid signature on them, but if we're not enforcing, + * some errors are non-fatal. + */ switch (err) { - case 0: - info->sig_ok = true; - return 0; - - /* We don't permit modules to be loaded into trusted kernels - * without a valid signature on them, but if we're not - * enforcing, certain errors are non-fatal. - */ case -ENODATA: - reason = "Loading of unsigned module"; - goto decide; + reason = "unsigned module"; + break; case -ENOPKG: - reason = "Loading of module with unsupported crypto"; - goto decide; + reason = "module with unsupported crypto"; + break; case -ENOKEY: - reason = "Loading of module with unavailable key"; - decide: - if (is_module_sig_enforced()) { - pr_notice("%s: %s is rejected\n", info->name, reason); - return -EKEYREJECTED; - } - - return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); - + reason = "module with unavailable key"; + break; + default: /* All other errors are fatal, including nomem, unparseable * signatures and signature check failures - even if signatures * aren't required. */ - default: return err; } + + if (is_module_sig_enforced()) { + pr_notice("%s: loading of %s is rejected\n", + info->name, reason); + return -EKEYREJECTED; + } + + return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); } #else /* !CONFIG_MODULE_SIG */ static int module_sig_check(struct load_info *info, int flags)