Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2321215iof; Wed, 8 Jun 2022 02:23:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzJoV61uN3xbY9bCtiuPMZBQ1qwq7WBwDthpkbY2JoigBpR6nVXmbh0/W7gMpqNOqvmW6D X-Received: by 2002:a63:42:0:b0:3fa:b4d7:eeb5 with SMTP id 63-20020a630042000000b003fab4d7eeb5mr28210359pga.71.1654680233293; Wed, 08 Jun 2022 02:23:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654680233; cv=none; d=google.com; s=arc-20160816; b=EWeZ6739jIToJaRdSAIK0AJYpUZ2jiWa3Ng+ZJGK5hX3eyg8ZsSoVzTJ8I3+r2VXu8 ldqS2AS2u3ECGqHxje5WN3yIDoz6AxiV22foo0rry4YG4b6kdgOtS6rWTmSnTMxnOcyD RVBvaf4CGUUQENYGGJHCdoHey/6HNWKyxh2X4upiJ5e+VJ1BXkWHTEpCSIMY+TsN22NX +W5V0gGgzKsh5ygvj0BGBnZwwlH30oDlxfRirYSjgsJ5hCC34PlU+JgArhi2WHJ+A1Xj +QjXX97TBEOLvaUs9+LttznnZd2/kTdbCr4Fm1u9XhBXn1UKRctusEQ2rXQUHj0QBR83 IpPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=dwplOPNLNvXktQ3iYDbgrxj+COZYty8EfyCngmesU6g=; b=0aYbHoCch/S2CiQuyDldikEX1YmEliWdOo9GzDZRzpZFFP1+VduCZuZVReW17vXKkc aGm4PpIq3i/h/TdWR5jw6aySkJHC6kN0wu2PRP0BzvM3UN4bUMqWMeIECcHSoqiFvV0J KqX+xHKaMJDO+pPdgT47mNHu7xWxIW6hWeKrc0wx9S6Rt/Q3XoFtz3BjlOewrmP5zkSF atXPAVry319BFtIRUV2nuVC+sIaG66ovjgkt8oiEsWjPZMMC0sPd2EKFVT4XaVEfOQ2b woz0cgGDZeG01AbLGad0O0Ue2ITSlKKj3w1L1Pp7i3X1aZ1V79hADKoqdlWAXkbj7rO3 sI5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZCDxQYVm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id g2-20020a170902e38200b00163d77ce6acsi23738579ple.615.2022.06.08.02.23.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 02:23:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZCDxQYVm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4BCBE138934; Wed, 8 Jun 2022 01:54:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232844AbiFHIyE (ORCPT + 99 others); Wed, 8 Jun 2022 04:54:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232087AbiFHIxj (ORCPT ); Wed, 8 Jun 2022 04:53:39 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 872B337E8B1; Wed, 8 Jun 2022 01:12:41 -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 sin.source.kernel.org (Postfix) with ESMTPS id 9BF0ECE2535; Wed, 8 Jun 2022 08:02:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99192C3411D; Wed, 8 Jun 2022 08:02:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654675336; bh=09GflePoermPFREk/n9az26hTfpo0H7Xj5McEsLlFJQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZCDxQYVmYqZidM5WW39gUjkgm4WMrJxNpUzfJj4EhOFOWPvu4V6xp81uL1rsn0o+o QfQLpX+VGhCgIeYK3mtLHVuI8dSweZq8nflfguxlEXwyeOYwDjG7enN91wpByGeRRr dFYK7sIqQQQTMkqrcDMK/aZF7epgSy243V6VEgaasLwJGBF1MgoMG05c8rzdkGeFKS wx+0tVXAfb9C7a6q8HlfqwmkkBgDg8bDaMZIhnkie0eomYRUFvzv/dtsCX4jl4b2GW MNJVIAOcln1Nv2khtLWUW3ofqY348n/xXnIr6/R4mlvggciQt+UjFANuWbgcZBzVq4 xdEPxs0uQM+Jg== Date: Wed, 8 Jun 2022 11:00:19 +0300 From: Jarkko Sakkinen To: Jianglei Nie Cc: jejb@linux.ibm.com, zohar@linux.ibm.com, dhowells@redhat.com, jmorris@namei.org, serge@hallyn.com, linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] security:trusted_tpm2: Fix memory leak in tpm2_key_encode() Message-ID: References: <20220608025938.447908-1-niejianglei2021@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220608025938.447908-1-niejianglei2021@163.com> X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 The short summary (as mentioned in review): "KEYS: trusted: Fix memory leak in tpm2_key_encode()" Also, you should version your patches, and provide a change log. See: https://www.kernel.org/doc/html/v5.18/process/submitting-patches.html#the-canonical-patch-format For git format-patch you can simply supply "-vX" to get the-canonical-patch-format version included. On Wed, Jun 08, 2022 at 10:59:38AM +0800, Jianglei Nie wrote: > tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(), > but it is never freed, which leads to a memory leak. Free the memory > chunk with kfree() in the return path. > > Signed-off-by: Jianglei Nie > --- Here you can write: v3: ... v2: ... > security/keys/trusted-keys/trusted_tpm2.c | 12 +++++++++--- > 1 file changed, 9 insertions(+), 3 deletions(-) > > diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c > index 0165da386289..dc9efd6c8b14 100644 > --- a/security/keys/trusted-keys/trusted_tpm2.c > +++ b/security/keys/trusted-keys/trusted_tpm2.c > @@ -57,8 +57,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, > unsigned char bool[3], *w = bool; > /* tag 0 is emptyAuth */ > w = asn1_encode_boolean(w, w + sizeof(bool), true); > - if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) > + if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) { > + kfree(scratch); > return PTR_ERR(w); > + } > work = asn1_encode_tag(work, end_work, 0, bool, w - bool); > } > > @@ -69,8 +71,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, > * trigger, so if it does there's something nefarious going on > */ > if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE, > - "BUG: scratch buffer is too small")) > + "BUG: scratch buffer is too small")) { > + kfree(scratch); > return -EINVAL; > + } > > work = asn1_encode_integer(work, end_work, options->keyhandle); > work = asn1_encode_octet_string(work, end_work, pub, pub_len); > @@ -79,8 +83,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, > work1 = payload->blob; > work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob), > scratch, work - scratch); > - if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) > + if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) { > + kfree(scratch); > return PTR_ERR(work1); > + } > > return work1 - payload->blob; > } > -- > 2.25.1 > BR, Jarkko