Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2156741lqo; Sun, 19 May 2024 16:52:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVPZ/IaZjoqtbGSc3aW6/vShQLBeavwXjq2YQjanel7KMNkQRAnLvTPm505yxTBNJpOr5bDhjqCo1+rstcKZZY7ituChbcUMx/qcQ0/jg== X-Google-Smtp-Source: AGHT+IF9HnMLXfsYPBPy08D4JJBdzJn85oEmehPAPIGNCmdHvajtrlJYZfcbsr7A+iMsAv91kCyQ X-Received: by 2002:a17:902:a3c5:b0:1e4:9ad5:a666 with SMTP id d9443c01a7336-1ef43d16ef6mr272238085ad.25.1716162722820; Sun, 19 May 2024 16:52:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716162722; cv=pass; d=google.com; s=arc-20160816; b=JICxz90BhzWj/ttdT9umgi8w4Ns8FQKZH951wiQ6nuqWL82oaUil86z61T2L9nSJgi 70KT5vb26U+uojmDV25EUkpgZaI7LAXoNPAcGrNmxejObdvnvG6WLLBfSv5UPy8TY2vO IMT1UPISdlS+WEmsisBsiwRCsVUPN2p6Rs+W3ZFgcPswgEouHD6hf6td1b62dZN6pPkT jJRe/6LXsEbImJ6A1mshBCAfnT+VJmWw/nMhTCMtNbtK1NlAIL6VzEr9fU5U8dtBYKGA W5wf+lIEemZGJv6Cvbx9ERsRwwR0HR9DBVsM8rvSTj12NsGKkmcENFXZEXyiGaYESiK4 HR8A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=P5Mkh54h/60xOomdkcXvjS/n3ToMT5Ab60t1MTtjYwI=; fh=hVg4LrrhFSDzbriqHJpcMiUtLePPU0VRrh7QdWe4jXM=; b=q7KY6XVhUsnSbzQ7fxhmNCljBd7FOnwqkhIT/HZz3biZKl7rWa4NPy2Jb/RjxZkm5z 9q06nljL38kCJWaBUUJyU7Fg+AyxqVs+2OzpKOdGLl7dK60M/PcmyGmzKAaNw7mcQQSW VvKAwghXgHNLqFzLziVKK2YaLdHjvohiTbjZ0cqX8lOanClgtQJzyP4Rlc+wcvWgNDgd QhfNKm0sYfVmAz85U+9AAd5swRBiPa5auDvMba6f7vBoI5kAglMPMxQZ0qLCpzdFFwMB 4Qyr0Q2Kx06y1JL1k7A25C1OSeBuVSLbYw8LD4tVsUJwJMnzhZyGlmSjBnVtIBvu8/Ms 6rSQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FvOoPXd4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-183309-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183309-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c15f4dcsi203841545ad.538.2024.05.19.16.52.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 16:52:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183309-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FvOoPXd4; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-183309-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183309-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 798782811AE for ; Sun, 19 May 2024 23:52:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2DAB74C1B; Sun, 19 May 2024 23:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FvOoPXd4" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5E6BFC01; Sun, 19 May 2024 23:51:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716162696; cv=none; b=iVlQH3c1y9dxfU8xzfHxVj4+Sv5GmUlAIdqti2JKRf+Ty6jsK4SUr7RJG97T6dhxusmz/vweIAVzcfu2cr+kA5k6FXhBxPl9JhiEHVuC/I4l0Al7kcfVih4ksrs+WIiru/BWdwq5skN8oBPBEtC/ml/coUPydDNv1nm1/QHEAlM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716162696; c=relaxed/simple; bh=FZnEkMOpn0PCjEi882Nayoei2+TEsEgYzwl1ZagOIgM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D33OTg7jAZu0k+cEoJ26F8XUeTkK8aJ+STd5N55vtaGgGL3UH9omvVErDE2QOshvYLCALcYHa/JqMgWdpEKOVj+hXGln5QjJI07osSl/oALV65RW15oZ6TNhDIiw1ccn1DewPKgxR0pAFm6LvyZUApC/oqAy4eHKKAxMT1uW4/s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FvOoPXd4; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0533FC32789; Sun, 19 May 2024 23:51:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716162696; bh=FZnEkMOpn0PCjEi882Nayoei2+TEsEgYzwl1ZagOIgM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FvOoPXd4i7knzZOO3yoJKYEeWHwsCBqia2TSZJ2mE3C2cGqxRK9te1oxF7nGx6syh bZtCmdNKdmQDfXKbjpcR/MFUPGSe92TI1DOe0CBMzE2K11p6eVLWlvz9Gf58QV0W32 AzvsMUPMPUlKfU8o4i25HGZf9KZKY2iBbnMGzeYyWrW0Zp/yzxHWQbC0eGo9WAVVmA 61Rt/EZzME3p8iGOsUVzg5dWc0O892dR4pB2XqznxepaKI3Xggx5ZHD4v8KwtwQTez Hhk4lKSIesf5x9hA8PZsU4fFeBSeD7CdOAQubP896SUdWTly843gNDntKsrSPawwXQ w35kCqhPO0FaQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: keyrings@vger.kernel.org, James.Bottomley@HansenPartnership.com, Jarkko Sakkinen , stable@vger.kernel.org, Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] KEYS: trusted: Fix memory leak in tpm2_key_encode() Date: Mon, 20 May 2024 02:51:20 +0300 Message-ID: <20240519235122.3380-3-jarkko@kernel.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240519235122.3380-1-jarkko@kernel.org> References: <20240519235122.3380-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 'scratch' is never freed. Fix this by calling kfree() in the success, and in the error case. Cc: stable@vger.kernel.org # +v5.13 Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs") Signed-off-by: Jarkko Sakkinen --- security/keys/trusted-keys/trusted_tpm2.c | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index dfeec06301ce..c6882f5d094f 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -38,6 +38,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, u8 *end_work = scratch + SCRATCH_SIZE; u8 *priv, *pub; u16 priv_len, pub_len; + int ret; priv_len = get_unaligned_be16(src) + 2; priv = src; @@ -57,8 +58,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")) - return PTR_ERR(w); + if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) { + ret = PTR_ERR(w); + goto err; + } work = asn1_encode_tag(work, end_work, 0, bool, w - bool); } @@ -69,8 +72,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")) - return -EINVAL; + "BUG: scratch buffer is too small")) { + ret = -EINVAL; + goto err; + } work = asn1_encode_integer(work, end_work, options->keyhandle); work = asn1_encode_octet_string(work, end_work, pub, pub_len); @@ -79,10 +84,17 @@ 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")) - return PTR_ERR(work1); + if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) { + ret = PTR_ERR(work1); + goto err; + } + kfree(scratch); return work1 - payload->blob; + +err: + kfree(scratch); + return ret; } struct tpm2_key_context { -- 2.45.1