From patchwork Mon Oct 24 11:31:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8884 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp433466wru; Mon, 24 Oct 2022 05:49:36 -0700 (PDT) X-Google-Smtp-Source: AMsMyM46fgHo1go5u7w4/J1NP8TS3vC9hPnCVGPbpKxK/+KiRdlUSDDi2iZk3Cf++d4Z0A4KvbYv X-Received: by 2002:a17:906:730e:b0:78d:94ab:77c2 with SMTP id di14-20020a170906730e00b0078d94ab77c2mr27414856ejc.639.1666615776173; Mon, 24 Oct 2022 05:49:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666615776; cv=none; d=google.com; s=arc-20160816; b=GjUNRpv9kpEYh85wWi58FyWDbtHQBLQW574KWrWVytmi4lHZeExZkqcAtZh0O0foQb 6yHyoQnHt1N1ylStkKZLYr9YS/WdWDGFBGQ+AarOQxbgZXkZchqV1mneGz9fTGoiFNv2 9uXA36JAhqwr9bv9WlKFGD5F/ML2v5x6QjOwSBLv+t4g8bPMNUxwyTuoF/jhu4TbTbPL z126BxvjpPW0esWWcfMAGXRkjgsMPSn5A8UNq6YVW3QP7+jKI7+QA4slf517lkcHBKLL DLTlog3s6Z2wu1zFpzIdvb7mtOkQZFEpF1v3sQrfgstLdywbe5s/DeReiJ6/kEvgbFQJ B/5g== 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=pGleTUUwvhvVewvSsY5CE6SHE72io1fdzFxvS87ONxg=; b=w5OjD/eJ00OQHtHb2IvsNF3B0oQquDa3wlqb4JJnowGdb/YjhtAqyrqKwLjB2AUKnb uauBhgvv4LjYSA7ho7ONs6Hh5W0NLh/nig2AaYeSz8C1q2OklQMmdwZdHEahlVWP4rKJ hMjpLrsd7RN6dBRWrdWDz6oRN4+BdvD15sm27opxf+qZkbPy00lfereMdt4Uge1gC9WY ILyO5xVrDutbiXFPT7MH5SLFkoaQOP3mESP3EuHczUlBakY8M5Y2EyAH/0aVapfTge6m n+HVhKZypz/R6xI+du1Atdkd/ibdF4fHEdaVTDr8SOYsDQWLmK/38Aa1fgdDWWPxG7UL egQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Zutc/YmS"; 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 qb26-20020a1709077e9a00b0078db3762061si29424938ejc.439.2022.10.24.05.49.10; Mon, 24 Oct 2022 05:49:36 -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="Zutc/YmS"; 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 S230189AbiJXMh4 (ORCPT + 99 others); Mon, 24 Oct 2022 08:37:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234025AbiJXMcd (ORCPT ); Mon, 24 Oct 2022 08:32:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C87CF474C2; Mon, 24 Oct 2022 05:04:57 -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 50C706129D; Mon, 24 Oct 2022 12:01:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FF56C433D7; Mon, 24 Oct 2022 12:01:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612914; bh=7ntdUPCMIhqIzNNSYQ6C+sDToY+GcTkHMTAcv/S2QEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zutc/YmSizbNt0liIr48Qd+uERmzsffoT7MGVdxkFmyKFtllVGpnfeUFdpa09m59N v/Mp+zxeY+YbVCpfhcokvmRCeXZQ2kFE9WMOfICQk28R72907eUVaeVx85Tq+zaE9g wQnQRUGGsQ3EYkabnu8EOkOC31U9ThqJJvtI/d9M= 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 4.19 168/229] crypto: cavium - prevent integer overflow loading firmware Date: Mon, 24 Oct 2022 13:31:27 +0200 Message-Id: <20221024113004.509292196@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@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?1747573303795040607?= X-GMAIL-MSGID: =?utf-8?q?1747573303795040607?= 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 06ad85ab5e86..7416f30ee976 100644 --- a/drivers/crypto/cavium/cpt/cptpf_main.c +++ b/drivers/crypto/cavium/cpt/cptpf_main.c @@ -257,6 +257,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; @@ -267,11 +268,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;