From patchwork Mon Jul 31 12:08:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13334521 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61C72C001DC for ; Mon, 31 Jul 2023 12:15:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232072AbjGaMPg (ORCPT ); Mon, 31 Jul 2023 08:15:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231436AbjGaMPe (ORCPT ); Mon, 31 Jul 2023 08:15:34 -0400 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A0F710E2 for ; Mon, 31 Jul 2023 05:15:32 -0700 (PDT) Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 5E3B01F547 for ; Mon, 31 Jul 2023 12:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1690805288; bh=VQVCQI4Vw8/ZNlRzh9TR7lvqO2t17c8ikmbmzqe52Ok=; h=From:To:Subject:Date:In-Reply-To:References:From; b=F6WfjpH+bKpd3BOMO2YsleBNGfEbd1HUKffdJnerV68RPttvzXAVz2Ezwl6SAFkUw gWL10RyCIDpC/mRX+/uPG6ADFE5JNl4kTmjwXg7FgOsYo/B7GtSytu3Zk+zw+nI5bw X4e4AV7em0eO1sLOf0UctHPXkp5TdpHh1AmcZw70= From: Eric Wong To: git@vger.kernel.org Subject: [PATCH 1/3] sha256/gcrypt: fix build with SANITIZE=leak Date: Mon, 31 Jul 2023 12:08:06 +0000 Message-Id: <20230731120808.1230210-2-e@80x24.org> In-Reply-To: <20230731120808.1230210-1-e@80x24.org> References: <20230731120808.1230210-1-e@80x24.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Non-static functions cause `undefined reference' errors when building with `SANITIZE=leak' due to the lack of prototypes. Mark all these functions as `static inline' as we do in sha256/nettle.h to avoid the need to maintain prototypes. Signed-off-by: Eric Wong --- sha256/gcrypt.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sha256/gcrypt.h b/sha256/gcrypt.h index 501da5ed91..68cf6b6a54 100644 --- a/sha256/gcrypt.h +++ b/sha256/gcrypt.h @@ -7,22 +7,22 @@ typedef gcry_md_hd_t gcrypt_SHA256_CTX; -inline void gcrypt_SHA256_Init(gcrypt_SHA256_CTX *ctx) +static inline void gcrypt_SHA256_Init(gcrypt_SHA256_CTX *ctx) { gcry_md_open(ctx, GCRY_MD_SHA256, 0); } -inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data, size_t len) +static inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data, size_t len) { gcry_md_write(*ctx, data, len); } -inline void gcrypt_SHA256_Final(unsigned char *digest, gcrypt_SHA256_CTX *ctx) +static inline void gcrypt_SHA256_Final(unsigned char *digest, gcrypt_SHA256_CTX *ctx) { memcpy(digest, gcry_md_read(*ctx, GCRY_MD_SHA256), SHA256_DIGEST_SIZE); } -inline void gcrypt_SHA256_Clone(gcrypt_SHA256_CTX *dst, const gcrypt_SHA256_CTX *src) +static inline void gcrypt_SHA256_Clone(gcrypt_SHA256_CTX *dst, const gcrypt_SHA256_CTX *src) { gcry_md_copy(dst, *src); } From patchwork Mon Jul 31 12:08:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13334522 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0487CC001E0 for ; Mon, 31 Jul 2023 12:15:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231919AbjGaMPl (ORCPT ); Mon, 31 Jul 2023 08:15:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231504AbjGaMPk (ORCPT ); Mon, 31 Jul 2023 08:15:40 -0400 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FD2010E2 for ; Mon, 31 Jul 2023 05:15:39 -0700 (PDT) Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 7B7B11F548 for ; Mon, 31 Jul 2023 12:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1690805288; bh=G4pPpd2+hkKA/g7A6j56XV7zn54kD2jy/m585iVtims=; h=From:To:Subject:Date:In-Reply-To:References:From; b=1kJ6LyTdoZRSJ8uUFNzx7WI6GDdzzCka00wNyCVV11FpbAUKpYNF5eVC8VZdwY7uP t7+ruIaiDCxGJXJGYx0zgsDNxeDMS5cqNnzjDE5sjgpoYUM81ITuyegZuaCBpPLG9H k11UqNlrO/EMVmT/n12MHw9vabUxAsWqkFR+/P8s= From: Eric Wong To: git@vger.kernel.org Subject: [PATCH 2/3] sha256/gcrypt: fix memory leak with SHA-256 repos Date: Mon, 31 Jul 2023 12:08:07 +0000 Message-Id: <20230731120808.1230210-3-e@80x24.org> In-Reply-To: <20230731120808.1230210-1-e@80x24.org> References: <20230731120808.1230210-1-e@80x24.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org `gcry_md_open' needs to be paired with `gcry_md_close' to ensure resources are released. Since our internal APIs don't have separate close/release callbacks, sticking it into the finalization callback seems appropriate. Building with SANITIZE=leak and running `git fsck' on a SHA-256 repository no longer reports leaks. Signed-off-by: Eric Wong --- sha256/gcrypt.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sha256/gcrypt.h b/sha256/gcrypt.h index 68cf6b6a54..1d06a778af 100644 --- a/sha256/gcrypt.h +++ b/sha256/gcrypt.h @@ -20,6 +20,7 @@ static inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data static inline void gcrypt_SHA256_Final(unsigned char *digest, gcrypt_SHA256_CTX *ctx) { memcpy(digest, gcry_md_read(*ctx, GCRY_MD_SHA256), SHA256_DIGEST_SIZE); + gcry_md_close(*ctx); } static inline void gcrypt_SHA256_Clone(gcrypt_SHA256_CTX *dst, const gcrypt_SHA256_CTX *src) From patchwork Mon Jul 31 12:08:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13334523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E811CC001DC for ; Mon, 31 Jul 2023 12:15:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231694AbjGaMPw (ORCPT ); Mon, 31 Jul 2023 08:15:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232204AbjGaMPq (ORCPT ); Mon, 31 Jul 2023 08:15:46 -0400 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94B8F10FA for ; Mon, 31 Jul 2023 05:15:45 -0700 (PDT) Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 97BB11F549 for ; Mon, 31 Jul 2023 12:08:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1690805288; bh=OVWhh2sxzshHMF9XQj+K8FeNofFaIkWsEYXGjr7aNyo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=yrE19kNMwice2hr3DR7QiOu9hw7Brr08P+aLUsj0M7XRhq1YD13bSKpxK5Rm56Kvy X5ZrsiUza9tdFYUSqF26q1wvRvAt6wuOkLTBbBWS3lLwPflSiRRZWydGjqAdd621bf tAdiXCGlriG4jANCyuPL/YlYoDSUoKZ3VbGjf+SE= From: Eric Wong To: git@vger.kernel.org Subject: [PATCH 3/3] sha256/gcrypt: die on gcry_md_open failures Date: Mon, 31 Jul 2023 12:08:08 +0000 Message-Id: <20230731120808.1230210-4-e@80x24.org> In-Reply-To: <20230731120808.1230210-1-e@80x24.org> References: <20230731120808.1230210-1-e@80x24.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org `gcry_md_open' allocates memory and must (like all allocation functions) be checked for failure. Signed-off-by: Eric Wong --- sha256/gcrypt.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sha256/gcrypt.h b/sha256/gcrypt.h index 1d06a778af..17a90f1052 100644 --- a/sha256/gcrypt.h +++ b/sha256/gcrypt.h @@ -9,7 +9,9 @@ typedef gcry_md_hd_t gcrypt_SHA256_CTX; static inline void gcrypt_SHA256_Init(gcrypt_SHA256_CTX *ctx) { - gcry_md_open(ctx, GCRY_MD_SHA256, 0); + gcry_error_t err = gcry_md_open(ctx, GCRY_MD_SHA256, 0); + if (err) + die("gcry_md_open: %s", gcry_strerror(err)); } static inline void gcrypt_SHA256_Update(gcrypt_SHA256_CTX *ctx, const void *data, size_t len)