Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6740864rwi; Mon, 24 Oct 2022 05:39:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5FrSQZCU5Jz2FiNE03yRNUAfZ0llLKQ9F2vOFlXFo+NlK+ecCZRPkX86EQXuRz4H9iSSEq X-Received: by 2002:a63:db49:0:b0:439:246e:8091 with SMTP id x9-20020a63db49000000b00439246e8091mr28396475pgi.295.1666615143089; Mon, 24 Oct 2022 05:39:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666615143; cv=none; d=google.com; s=arc-20160816; b=LF5uJx6XJcKcOvLq1MBJi2L2gqnKqCvN6WQPKO5f4P9Fp/FtrM9JOu/eZ3ZBJX+tHJ FB8e9VgakErDavrmyB7ISQg58DKspYe315Hj1dByp87kmmIC4gCIWSIB3/eQhE2b/1DB CBeg0Bku7ZEg66etmuxWcMwLPw3IiyxqjNz0/bWoOaaq4ra8GnNFhg6xnanxhmupazlJ VUZLy0HyQYqr1Zd8s0+n5aN2PH90yp7jf0ATMbF4a0AkpLYrwajJI8qYjDzX046D9+xH QgpjklwpI17aCpOML3HtugDT1SIX8ggZTVH/ywl9xkxLqj0zx30V/glNaQ3mlzeCmbLC 6VwA== 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=bsyKcbIMDeEFw7/nt7KVuwEqlRP5FXUDAoU7kJYZED4=; b=yzzJFiBhki4PfEHkXiSHvLdEGr4mlNTvZbewqqxDqMS3dw3JYG1N3IqRSUq6Es7S/k Bs+2A/Dgi8Me7HlCHvbXfGCmGjUNC590hj2VKirzQ14C7sTjWT4TT7eCVCesRdQzoXfZ trXdAN5qNDgYd7xW4pGLtCKNO95GVHF0LIfOl0FonBUk5HVqk/gBHWBQf59NZZ1iGzCC zlv1109zavEa9bCJfZj9tXIRqgVpUEgzLmDrOTAm7btkldGKOm6P6/TJE4xLeBsbgfjS MaEUKI2onfu+aO6l1a28Q2CzELQ5fJDe2jS2e+vkBwtA/1R3LvYtM2CWiUeHHvxqkDPI Aidg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=x5pa6o4x; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e17-20020a17090301d100b001869ba21491si6220088plh.8.2022.10.24.05.38.49; Mon, 24 Oct 2022 05:39:03 -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=x5pa6o4x; 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 S233079AbiJXMQI (ORCPT + 99 others); Mon, 24 Oct 2022 08:16:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233244AbiJXMOc (ORCPT ); Mon, 24 Oct 2022 08:14:32 -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 1968E5B071; Mon, 24 Oct 2022 04:55:26 -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 89E99612C5; Mon, 24 Oct 2022 11:52:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F82BC433D6; Mon, 24 Oct 2022 11:52:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612337; bh=nw7eE55o7Cub4k6OxNnyj+f/jSj2zifMDaD65737OUM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=x5pa6o4xcJXgFNSixqVhXjX8fMZm/NgErnyveAYqhiigqjS+MrXele+x5dZr9tj44 z0pz0hr19TUtvZwqoCuOTqo0o2LKrUOLwAJLT8iKVQy8Ni/JtacOK5FftlyhsPaRPu pKYvkadJTolV13i2+sp6Q7nGIPF2mb/hQFhq5zpw= 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.14 159/210] crypto: cavium - prevent integer overflow loading firmware Date: Mon, 24 Oct 2022 13:31:16 +0200 Message-Id: <20221024113002.134861468@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112956.797777597@linuxfoundation.org> References: <20221024112956.797777597@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 34a6d8bf229e..dc34326e80df 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; -- 2.35.1