Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2131483iof; Tue, 7 Jun 2022 20:47:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfcAxmalMwjK0B92EGBs9abwISqni863+I4BBMH4+UoXBi8rpx0iYSbAiejS4RR9QQg+Qp X-Received: by 2002:a17:90a:4209:b0:1df:b907:ed3d with SMTP id o9-20020a17090a420900b001dfb907ed3dmr35957178pjg.40.1654660058868; Tue, 07 Jun 2022 20:47:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654660058; cv=none; d=google.com; s=arc-20160816; b=PRn/VgmY1yHgIFnR+m7jb9/WfJb0H/2wuo23VEq0/+OC8WgEMMhT/RZy3hvRX7WgFa qjbmIVNsKBEsrSLrv3EJ/q+OjuG0hFVAkQ88py+5cVv9D0T9Y0zqI4QTKl2OttIgFBWX ZRm/T79mshv0tYYn/ubFqqlbqfEhKlrPAFuVaslbap7fcWWMq9MM7kuFGhmp4WxDn8sz z6Gz5mAh3bLUeuvVb4w+A1JVZ/+WZkyP6xUvAH2mIKYpQpWLvMi/Q+EVeJYEKjPsUDHr uHz1ZD03x+hXlIDPnUqhp5QLZLUUmTNcErB52GTglsJ6gxqmBYbqFYh9Kk6XTBOu4Pan vOgA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=FzsQeKlfx+PTNhydCXy5arK4m63g42oEcDpVKHz+ar0=; b=O4ChMulRQm+Pvl9rhlrngu9zGwUrXQ68FOGJ1xtrWffw86aAUvlcsuvZk1odavJQZM 8Ph2g5CZPoGg3PL7RD1ZZVWfsOp7q5DJnqT7mgGQXXWwG/DVbnX4fYRog0UKkRQNQ9k3 HrbRDQlQB24AcmqyktlvAg7sh+F3jNSL64wTA1tyZJTaa4qLzze1ZP3n3MIGdTT/J7h0 HkZxY9col56RasLEp11pqDb3KjcIQjC+HeixNaOAfqlgB3shSYNSeS5hQFdgWNrj3xbL LqLpGOB5/Xw4WgURvbwtwfbEhJ069nGhtNeIUVA9W1ZRbFelTTXSB3rN+g0ppmmW9NCl /G3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=Eqd5XGCE; 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=163.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id rj10-20020a17090b3e8a00b001cd51f48295si8911043pjb.174.2022.06.07.20.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 20:47:38 -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=@163.com header.s=s110527 header.b=Eqd5XGCE; 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=163.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5A9E83082CA; Tue, 7 Jun 2022 20:06:33 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237898AbiFGHrp (ORCPT + 99 others); Tue, 7 Jun 2022 03:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237904AbiFGHrm (ORCPT ); Tue, 7 Jun 2022 03:47:42 -0400 Received: from mail-m972.mail.163.com (mail-m972.mail.163.com [123.126.97.2]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2F8B8BF7D for ; Tue, 7 Jun 2022 00:47:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=FzsQe Klfx+PTNhydCXy5arK4m63g42oEcDpVKHz+ar0=; b=Eqd5XGCE4BV50FAUN/N4t q7z/nZMZr7sIwloIGMSpLbKhTA7sDPho1a0gsRfPPiIdh3JUiz1ZfbYx06iFRxKT kr/NKH0YuNIgP0XSHvW99p93YbU/3sNYKGZJw0MJjc+NpPfDPuHx/yVEY2okMPPv G2irPV95sR/DiAtmPSgcxU= Received: from localhost.localdomain (unknown [123.112.69.106]) by smtp2 (Coremail) with SMTP id GtxpCgB3PCJrAp9iIndHHQ--.24586S4; Tue, 07 Jun 2022 15:47:06 +0800 (CST) From: Jianglei Nie To: jejb@linux.ibm.com, jarkko@kernel.org, zohar@linux.ibm.com, dhowells@redhat.com, jmorris@namei.org, serge@hallyn.com Cc: linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Jianglei Nie Subject: [PATCH] security:trusted_tpm2: Fix memory leak in tpm2_key_encode() Date: Tue, 7 Jun 2022 15:46:50 +0800 Message-Id: <20220607074650.432834-1-niejianglei2021@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: GtxpCgB3PCJrAp9iIndHHQ--.24586S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7Ar4xKFyxXF18uFykuFyDKFg_yoW8Zr1kpF ZxKF4aqrZF9F9rAry7JF4fZF13C395Gr47Gwsru39rGasxJFsxtFy7AF4Ygr17CFWftw15 AFWDZFWUWrWqvr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRIPfLUUUUU= X-Originating-IP: [123.112.69.106] X-CM-SenderInfo: xqlhyxxdqjzvrlsqjii6rwjhhfrp/1tbiWxoZjGI0U5oI1wAAsU X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 function allocates a memory chunk for scratch by kmalloc(), but it is never freed through the function, which leads to a memory leak. Handle those cases with kfree(). Signed-off-by: Jianglei Nie --- 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