From patchwork Mon Oct 24 11:32:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp480121wru; Mon, 24 Oct 2022 07:20:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5roUo3knwMVICAviSFaAEXHXXURhcNR7VNc88A409Ec+7sWLcGvWNlgvknt+mxXRGSMn31 X-Received: by 2002:a05:6402:1cc1:b0:45c:3a90:9499 with SMTP id ds1-20020a0564021cc100b0045c3a909499mr31531988edb.61.1666621228179; Mon, 24 Oct 2022 07:20:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666621228; cv=none; d=google.com; s=arc-20160816; b=sRCKQOZtY/VYvJMV+DtlmUgR7+sMcPZaCVuDZOd6sz04vMy7eSMA4DubAOxw+uhJff QaN5Oq+xvKL8SCdFJL5Ek8st7xlJqKd+JDoVldjPOLVZTmsIXv1VSPIlKfkYK2j7g9vx pekVZoG6wdjc4TQomc7xSMcd1DzMajGlQNsk3zyBMKm2FYHg7PbKF7b70ng2zJbYTfMb fVkhzeIl/vointjDXvvxBzNyREZF4Bllol1fVpEJnhWMiNq7QP6UYoa1mQhsu7ltTol6 deABNLkYUvUterGQNnGMmufVSQYIK9WC0ees/bLupRP/L7aV7zbNCCT0+QqWM//OxWOE o4Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hJv7IUM6kJVsPIVNE+HRCOwEHEJ86PcnO/llMYLz+4M=; b=pEvZLWqSbaIIKUF72f7vZ5BoHaMh/Dgxyvocd4kbC/rHZ3ILRL61AVm8BWYUBMKwQX bP+K5CO6FsXBKulbHSruAeBTHRkZ3epwHkf2FYFg6G+d+8k/peu51iQk4YtI1/0gjPZd fu0bQvOh1wv6yreWYgY5OVkMiKBkc3+ih+VkjoWR+ktFHKAst5RuTShM9PaSkgsh73g4 3B6HwdWTyjO+GjYD1gWCs3TR+B6qwWuv7rZTsdU5YKXBVsGlN+3hV2sOSaYlSmFm/YZ4 skgdzzMtWofjUQhGO3MVu9gb+hC6u4tZZcdc+JZuMTIZFI0myasj2j8nYfdmgZpI8Uh5 GjfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eFoR6PUu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cs12-20020a0564020c4c00b004618343b140si6501212edb.199.2022.10.24.07.19.59; Mon, 24 Oct 2022 07:20:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eFoR6PUu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234111AbiJXOQ5 (ORCPT + 99 others); Mon, 24 Oct 2022 10:16:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234235AbiJXOMw (ORCPT ); Mon, 24 Oct 2022 10:12:52 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95F1FCABC6; Mon, 24 Oct 2022 05:53:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 362DB61298; Mon, 24 Oct 2022 12:51:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49F10C433D6; Mon, 24 Oct 2022 12:51:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615861; bh=R5xFzSHogEP2NN2e/wL2tnIj+3tgTrpSBrWmiZXApdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eFoR6PUuPQqTmwjHoYHcwLOrGdmZJa2Uc0595IAAoqroSeSWSYPN1vk6EAwhMAxgG rlZUMkDQXPrivUUpPZHJBER0muort1MPz/MKSC80IwumudFW3gav1RFb7z5+Vv1rlX bRQCAPc8M9bSjefg4fxaPnic8BXPRvA1KYuJnCwY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Herbert Xu , Sasha Levin Subject: [PATCH 5.15 394/530] crypto: cavium - prevent integer overflow loading firmware Date: Mon, 24 Oct 2022 13:32:18 +0200 Message-Id: <20221024113102.914681985@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747579021044655669?= X-GMAIL-MSGID: =?utf-8?q?1747579021044655669?= From: Dan Carpenter [ Upstream commit 2526d6bf27d15054bb0778b2f7bc6625fd934905 ] The "code_length" value comes from the firmware file. If your firmware is untrusted realistically there is probably very little you can do to protect yourself. Still we try to limit the damage as much as possible. Also Smatch marks any data read from the filesystem as untrusted and prints warnings if it not capped correctly. The "ntohl(ucode->code_length) * 2" multiplication can have an integer overflow. Fixes: 9e2c7d99941d ("crypto: cavium - Add Support for Octeon-tx CPT Engine") Signed-off-by: Dan Carpenter Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- drivers/crypto/cavium/cpt/cptpf_main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/cavium/cpt/cptpf_main.c b/drivers/crypto/cavium/cpt/cptpf_main.c index 8c32d0eb8fcf..6872ac344001 100644 --- a/drivers/crypto/cavium/cpt/cptpf_main.c +++ b/drivers/crypto/cavium/cpt/cptpf_main.c @@ -253,6 +253,7 @@ static int cpt_ucode_load_fw(struct cpt_device *cpt, const u8 *fw, bool is_ae) const struct firmware *fw_entry; struct device *dev = &cpt->pdev->dev; struct ucode_header *ucode; + unsigned int code_length; struct microcode *mcode; int j, ret = 0; @@ -263,11 +264,12 @@ static int cpt_ucode_load_fw(struct cpt_device *cpt, const u8 *fw, bool is_ae) ucode = (struct ucode_header *)fw_entry->data; mcode = &cpt->mcode[cpt->next_mc_idx]; memcpy(mcode->version, (u8 *)fw_entry->data, CPT_UCODE_VERSION_SZ); - mcode->code_size = ntohl(ucode->code_length) * 2; - if (!mcode->code_size) { + code_length = ntohl(ucode->code_length); + if (code_length == 0 || code_length >= INT_MAX / 2) { ret = -EINVAL; goto fw_release; } + mcode->code_size = code_length * 2; mcode->is_ae = is_ae; mcode->core_mask = 0ULL;