From patchwork Wed Oct 19 08: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: 5384 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp302860wrs; Wed, 19 Oct 2022 05:39:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4yVNYxCYaTu8kyrAOf7s3U3K7NuF3OavU8iyR6TQAhSDorvdovWhebpiPH2lBAyvjRScb1 X-Received: by 2002:a17:902:b942:b0:178:4a7e:da04 with SMTP id h2-20020a170902b94200b001784a7eda04mr8292964pls.8.1666183187994; Wed, 19 Oct 2022 05:39:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666183187; cv=none; d=google.com; s=arc-20160816; b=vOI5W1WHn6jOKRcOuceotu9HkA4WwXco4T1FwwAegAnDPBcVEgY2Ah8SgzkrOtRtIM v3TUaQqLFT4ZoTSxDGZX2IeVCQdT+sgYNWUKVUzXuCVCGwaXHTTaCh3F9ZIOl8TPQ1yj XUKxdsVU1PJXqedUduKGhiybVFV5wV23EVy3oyMjOo3kEcCOJyWw5Eb3JpPVrGoul2rW fwAjJbRhGjIL1Yc1nNZnU/ueOS4/piK0JFTe5y4qhedx5pkeMlXoU+Aps1B9VeykAnrv JFInnnYWf6HOMPLS4A1eLcQZkQeIcnfdMqcd5OgL11JNYkHDOhxQXLEBtfzoGViilkhA 2BPQ== 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=sJqMjokhF5x7IoW+sB5NU1BhqYWJl5hEs+sbpzlFTE9Nj44BauwnTs5GGXvDtl6n8O w+ZxuxoA6IfYLcwCr1OqN11BrtAaxHKq5bfQ/EJW22baSzQ1uoerUwW9sNvJ9AHCjmsj srelTOCg/+sjGYBVgrPY/+cagas8PKU8IK0LhqFW3G+jFkTHb7O7RD80E3Pnr+8taoY2 K47eVQ0cVt7/VhfoJUmcgNZvofxf8o/k5VPPx1BVUREvViTlaS648qRhcNU0mJQoI5zA dAEj1vpuLL7TY0UgutDdXTE4FOm+5WBqx2tgELBRazb1FWyZIr5qDRvP/KRmGYFxeEjB 5qHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WMtQrTq5; 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 w12-20020a63fb4c000000b0043541f027e0si17361099pgj.877.2022.10.19.05.39.29; Wed, 19 Oct 2022 05:39:47 -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=WMtQrTq5; 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 S231709AbiJSMeh (ORCPT + 99 others); Wed, 19 Oct 2022 08:34:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231608AbiJSMeO (ORCPT ); Wed, 19 Oct 2022 08:34:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4381106923; Wed, 19 Oct 2022 05:14:00 -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 F0BF761852; Wed, 19 Oct 2022 09:07:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0E195C433B5; Wed, 19 Oct 2022 09:07:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170425; bh=R5xFzSHogEP2NN2e/wL2tnIj+3tgTrpSBrWmiZXApdA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WMtQrTq5MwDsvEYRTkXFEQq5DtFqXMgOcdA7LpYhJ51qsL1mIV82P+do+VIATsmzn +emrgSRX/BGMS3hbWEFdEZKwZyqHC+QEgHT2FGin3RVFpI1WYtepcYKJ/nuD2pizss GZkgbeEK7d8bs5W9kbrUP/kFfSNw0CNSoyNdrAlQ= 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 6.0 651/862] crypto: cavium - prevent integer overflow loading firmware Date: Wed, 19 Oct 2022 10:32:18 +0200 Message-Id: <20221019083318.699495645@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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?1747119702636879822?= X-GMAIL-MSGID: =?utf-8?q?1747119702636879822?= 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;