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=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham 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 D630EC433F5 for ; Tue, 7 Sep 2021 07:11:54 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E32F060F43 for ; Tue, 7 Sep 2021 07:11:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E32F060F43 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nxp.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EAA3383249; Tue, 7 Sep 2021 09:11:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=nxp.com header.i=@nxp.com header.b="IbCSXGXi"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E8D218324C; Tue, 7 Sep 2021 09:11:47 +0200 (CEST) Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2061e.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1b::61e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5D77982E3F for ; Tue, 7 Sep 2021 09:11:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kshitiz.varshney@nxp.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Jea0v9BWM0PgH+dpJ3b+ir1OYDx1f1cr2IkrFYsIRqxypGvDmWNAsAXYpancGObbqjKdTWwuQFEc0qjwZDMwtvHx+pmgEQp7s67DdZ1wqgamWUcDYDmm9MDf1+LWiX/G+YQL4LmQXEQOF4fUG6T9s9yyZygwS/0KLGtrf9J2PGrarmccUmUO19ogly9ZUD30p8cier5+DNXFNF3985Y+rXsftHhF6Whu5ExvIBMGy4Zj3LWhXmQ1cD7FrU19Z8oT+R/Twy63bbhLK0g+ES1WXDwf3PI1C7eDsnlaTsIi//mMub+IAbYfmI4f/YQ8gS/mNGBo9ca9l9H+NyfW6B67Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=n5nZcL17/TrM29rgduXFEo2hooYsMgGWvcSRsprJRt4=; b=cupA4TNj3DMC45m4RKFty69EAXt0Iy7wcGS4/lbE9KhsgZZlqeaS03+5nkOq2pIypD5MsEV9uR5pexFWkTUlJxYOj9jSdDhxza7bLdrPHW+NyGdKORLC3S4WXx/qKAw+qX1Lb0oZxtygp7Xl0lwi1HNHqvxdZ1iaTRR60xBC1bPBtEBGpHfOZvZ3NdVTGx2vQ/d6AWdA91b2EojTP+vVNAoJvUyF8nlsTsWm4gg+d4EoIDbGmoeH4Rdsk7K7YTN6CDA4k/SXPX61nkaQTeyZHctEylkwSA8/zyx0rS1frXGQqPnCgMbRSLEa4mOJXdrwluPitViEVqEXBciaqaBFDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=n5nZcL17/TrM29rgduXFEo2hooYsMgGWvcSRsprJRt4=; b=IbCSXGXiKH2gBg5X6mTw0PfAWacEO25PiFDUkFSQvbsz6jUBjV1ryqKnfFKLaz5txkeHElqaXwvB6+AMVqjgTFCna8U2g3VZpCSb5UY/Pd2qCFdZKfxFJMpRcG6pijGqvtT3qTTrBMiYLaZGhJmUm7imft7idVWxUHavWPs+bnY= Authentication-Results: lists.denx.de; dkim=none (message not signed) header.d=none;lists.denx.de; dmarc=none action=none header.from=nxp.com; Received: from DBBPR04MB7820.eurprd04.prod.outlook.com (2603:10a6:10:1ec::12) by DBAPR04MB7478.eurprd04.prod.outlook.com (2603:10a6:10:1b3::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.21; Tue, 7 Sep 2021 07:11:43 +0000 Received: from DBBPR04MB7820.eurprd04.prod.outlook.com ([fe80::f0bb:2e7e:8823:1454]) by DBBPR04MB7820.eurprd04.prod.outlook.com ([fe80::f0bb:2e7e:8823:1454%7]) with mapi id 15.20.4478.025; Tue, 7 Sep 2021 07:11:43 +0000 From: Kshitiz Varshney To: u-boot@lists.denx.de, Priyanka Jain Cc: Kshitiz Varshney Subject: [PATCH] board: fsl_validate: Double free Issue Solved Date: Tue, 7 Sep 2021 09:11:29 +0200 Message-Id: <20210907071129.1218395-1-kshitiz.varshney@nxp.com> X-Mailer: git-send-email 2.25.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: AM4PR05CA0029.eurprd05.prod.outlook.com (2603:10a6:205::42) To DBBPR04MB7820.eurprd04.prod.outlook.com (2603:10a6:10:1ec::12) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from lsv05369.swis.nl-cdc01.nxp.com (92.121.36.4) by AM4PR05CA0029.eurprd05.prod.outlook.com (2603:10a6:205::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4478.19 via Frontend Transport; Tue, 7 Sep 2021 07:11:42 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 76435f81-781a-405d-f617-08d971cebece X-MS-TrafficTypeDiagnostic: DBAPR04MB7478: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fzdkMm4NF0mrNp1ldokIws4duvkQjlK0ox5vdb/R0mbaGAMmVCk7Tx3+lkVaHG34zJw9bF5mjmRrBTxduOjieKSQFkX2ejQrTEKKplJJWhczbezKmlUMptCkzhf/iIXHnhorDF8ue45EUc1Q8thVtImn9VM1TGkcr6udeGGcW3aum+9aZ5wFW8CXdGxSYzvjB8eAt3Wrm+3A+JhPL2Lwyjw9gL+ptvWxAsU6MLSlCWt0tgB2D0FX+KKRXss+bmHDLMReNcQFHQp4ErUNL565+Fu6jIX7Eq4A8UV9XnD31K21uzM7A+wPPG7XqEv1x8axm/A2If0elj/Govs3joJU/fusnpse8twTv04RSG9SbFH8QT4ZBGYk1HmadeNtFg8dU+WT2emsvMJd4ZWI5wcWE+z13z9xa0HtQpau9Gkj4WWpfVOYw2WIPvgPeOL7hsboe2qjwUfi15LsXh9XhrvCVIIbLWZthJepQ60xLPL1QO/SvDmVQOYmQts3vg7DluoDosOvLlJQTr4PDGWLrniu3L3qXtW7XKNao2P9QXbAbqwHdjY79F/Xi7SvfEinOBzTiU2GCWc5a0QrMYFBIOCKtfZyalSm9de1AHENruQUrF0EFZODNZGEJBHrzDrpxcWcPFMT48U6gCi9AVvJ/S+oGrAQeHCtazNS4i0lAA8HtxmeIcyAc9uyaIGLZEqUaW0k23V04iHfLaCgedlcHkWETQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DBBPR04MB7820.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(136003)(346002)(39860400002)(366004)(396003)(6862004)(44832011)(7696005)(52116002)(4326008)(2906002)(83380400001)(2616005)(956004)(1076003)(37006003)(8936002)(186003)(8676002)(26005)(38350700002)(38100700002)(6636002)(36756003)(86362001)(316002)(6666004)(478600001)(66556008)(66476007)(66946007)(6486002)(5660300002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?bYM7xGHoPy5YuM1bWy8/wMuKtDWIlj6843XbKssiFXXf8YVlV0On8NL3OXvG?= =?us-ascii?Q?sz59c5CCJkEolqNipSW8T5O8C28VqIcGhZaoArlBAtQOQCzQ0jk2SrNcUW+I?= =?us-ascii?Q?vr9xdod5cYaBChmAMMAHn8I2yk+hUC3nTFGT0vTOgDxQL01ku+pQqsBXz8lP?= =?us-ascii?Q?mBns3FEibJnJee2XjIC8Rgrho4J4TItROGF5Aiv5j+ErFqfUYRWQHGyFqyUW?= =?us-ascii?Q?7QdlvsFT6K1b2cFN2IGyYAxCb8Yb9GcQU0Zw21JMTpuHbai2cULfbsCzfAMh?= =?us-ascii?Q?mg6yr7aq7AvV/ZpWgkQiQKJCr16NxXWiTrJ1fKeNhTE4ZvNfd4BhMRLpbmLY?= =?us-ascii?Q?xzKMr17OwwaCN39u/49emhu6D+rU/XAFtHZNCkSKkwvPyLQoi8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bTJ1ZUYJ05cMpmy094VW8+Sw091L4CC3a?= =?us-ascii?Q?4fgKyg0Zu6ZN1udLdAb3kGSR?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 76435f81-781a-405d-f617-08d971cebece X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB7820.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2021 07:11:42.8768 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: k9suqqJSbHTNx7AlFyVSUHftXHAu8+2flnokAUJlN9AnRfraFq6Dp//1Plo4lEmfMS2/rr/SRywKXjDcfcHqYkil51rZ3HW/yGNh8ZoPyms= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBAPR04MB7478 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Removed Double free issue from calc_img_key_hash() and calc_esbchdr_esbc_hash() function. Verified the secure boot changes using lx2162aqds board. Signed-off-by: Kshitiz Varshney --- board/freescale/common/fsl_validate.c | 28 ++++++--------------------- drivers/crypto/fsl/fsl_hash.c | 14 +++++++------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/board/freescale/common/fsl_validate.c b/board/freescale/common/fsl_validate.c index 5cec0131f2..9a22fb8054 100644 --- a/board/freescale/common/fsl_validate.c +++ b/board/freescale/common/fsl_validate.c @@ -499,12 +499,8 @@ static int calc_img_key_hash(struct fsl_secboot_img_priv *img) return ret; ret = algo->hash_init(algo, &ctx); - if (ret) { - if (ctx) - free(ctx); + if (ret) return ret; - } - /* Update hash for ESBC key */ #ifdef CONFIG_KEY_REVOCATION if (check_srk(img)) { @@ -519,15 +515,12 @@ static int calc_img_key_hash(struct fsl_secboot_img_priv *img) img->img_key, img->key_len, 1); if (ret) return ret; - /* Copy hash at destination buffer */ ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size); if (ret) { - if (ctx) - free(ctx); + free(ctx); return ret; } - for (i = 0; i < SHA256_BYTES; i++) img->img_key_hash[i] = hash_val[i]; @@ -554,18 +547,14 @@ static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img) ret = algo->hash_init(algo, &ctx); /* Copy hash at destination buffer */ - if (ret) { - free(ctx); + if (ret) return ret; - } /* Update hash for CSF Header */ ret = algo->hash_update(algo, ctx, (u8 *)&img->hdr, sizeof(struct fsl_secboot_img_hdr), 0); - if (ret) { - free(ctx); + if (ret) return ret; - } /* Update the hash with that of srk table if srk flag is 1 * If IE Table is selected, key is not added in the hash @@ -592,22 +581,17 @@ static int calc_esbchdr_esbc_hash(struct fsl_secboot_img_priv *img) key_hash = 1; } #endif - if (ret) { - free(ctx); + if (ret) return ret; - } if (!key_hash) { free(ctx); return ERROR_KEY_TABLE_NOT_FOUND; } - /* Update hash for actual Image */ ret = algo->hash_update(algo, ctx, (u8 *)(*(img->img_addr_ptr)), img->img_size, 1); - if (ret) { - free(ctx); + if (ret) return ret; - } /* Copy hash at destination buffer */ ret = algo->hash_finish(algo, ctx, hash_val, algo->digest_size); diff --git a/drivers/crypto/fsl/fsl_hash.c b/drivers/crypto/fsl/fsl_hash.c index 61f953e8a6..43daf1cbe3 100644 --- a/drivers/crypto/fsl/fsl_hash.c +++ b/drivers/crypto/fsl/fsl_hash.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0+ /* * Copyright 2014 Freescale Semiconductor, Inc. - * + * Copyright 2021 NXP */ #include @@ -120,8 +120,8 @@ static int caam_hash_update(void *hash_ctx, const void *buf, * Perform progressive hashing on the given buffer and copy hash at * destination buffer * - * The context is freed after completion of hash operation. - * + * The context is freed after successful completion of hash operation. + * In case of failure, context is not freed. * @hash_ctx: Pointer to the context for hashing * @dest_buf: Pointer to the destination buffer where hash is to be copied * @size: Size of the buffer being hashed @@ -136,7 +136,6 @@ static int caam_hash_finish(void *hash_ctx, void *dest_buf, int i = 0, ret = 0; if (size < driver_hash[caam_algo].digestsize) { - free(ctx); return -EINVAL; } @@ -152,11 +151,12 @@ static int caam_hash_finish(void *hash_ctx, void *dest_buf, ret = run_descriptor_jr(ctx->sha_desc); - if (ret) + if (ret) { debug("Error %x\n", ret); - else + return ret; + } else { memcpy(dest_buf, ctx->hash, sizeof(ctx->hash)); - + } free(ctx); return ret; } -- 2.25.1