Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp1525726pxv; Fri, 23 Jul 2021 10:24:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFkJMnLw1IG9IZV/xyJhtmyNt+BxO70XjvFl3gP/pkAZq6zpKKpubC7T9Lc/pRh9weqXjP X-Received: by 2002:a02:c013:: with SMTP id y19mr5017061jai.36.1627061044798; Fri, 23 Jul 2021 10:24:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627061044; cv=none; d=google.com; s=arc-20160816; b=dvp43VoQux8Wg805zDEb6KtfhCMoW6Ayq2JNjo0DK9DzwCguK8srgmJOuiXsGdh72V QtyKwsp7wtTMzbmrx18NUeNBKO9yfTmHb0EqJP/tZCRIpIVBEi3O6YrzPemBF+QKc2tV /nziZTM5HbDWCZHZ8fdIrr/kJr/stlKu/GRxqppORsu2BojYBo1+hZzzZXIz0hEJIhkl ZOuUo9YHLicYGvXObItRjBcibkKoLIg4bgttMQXtTQxHAKzh6yu7WD04R/pm8isrKYwL pLF4FJt5S8pEcD932e+586wp2UW3mMmhV2tUApfdOtzl5mME+sgNjFv04l/SCebRmPP+ WoXA== 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=7FQD/B711wUP78UGWXd/xXfRyIQyD6tfXLIddfxdlcc=; b=rk373h1WUo+aqIYHJrAJafkp4oY0qK+9q2d4UJ8Jhb1mobhqyNMGGqZEEp9G7UA8Yx z5OTpe73qXFoHM1eKv6mA2P+Jp627RRWTKOJ+oCqjr04czQ3XNVpG1QUdGS5QNNcmEuQ oAr3elTeo/7tsQgZ2Jtmun6kOvV+2szt3lMkLEBAUigpYBKrmD7WAy1JNJUDsE+ij6gN KPpOP8KE/+fcbkulplqsAkrSkUrpmQjkLvrFYmHDLUeF0fA1upC7B1W8KxnXIxkFyJx0 hCSXLCVDZk8Dw9YhYN9FUS/90tbMyYFBApSekj2ZbLJJbyvN9qoVolHIytsVTNqtxp29 qFMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=MIpg2zVy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f10si39328019ila.58.2021.07.23.10.23.45; Fri, 23 Jul 2021 10:24:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=MIpg2zVy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231440AbhGWQky (ORCPT + 99 others); Fri, 23 Jul 2021 12:40:54 -0400 Received: from smtp-relay-canonical-1.canonical.com ([185.125.188.121]:51134 "EHLO smtp-relay-canonical-1.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231166AbhGWQkx (ORCPT ); Fri, 23 Jul 2021 12:40:53 -0400 Received: from localhost (1.general.cking.uk.vpn [10.172.193.212]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-1.canonical.com (Postfix) with ESMTPSA id AC9073F325; Fri, 23 Jul 2021 17:21:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1627060884; bh=7FQD/B711wUP78UGWXd/xXfRyIQyD6tfXLIddfxdlcc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=MIpg2zVyane9SunkaQ6m9KeX6+ptvvm8nBjPPToZ+jE+O8Udpx2/Hbt3KIM9AsnKT 15av06BGurLcNcGDj6vbShhx7AEj2S5U1YUJVBo9G4n5qkVO+o03wYR+ClVinO47rd YMzTHOBW3XCPNf1b6cwBNLfQTObnplThPTRn+cf7LMBC9zC8dIWVJNMT5cCleyilum +jYVy0XJ0ILlsAQMEspBu4tpIAeQsH2wBw3i+HsmtuGkTjr3kJUlkfl4wsxcDgIDcY /D1H9HfoBkcbFQEKQWuVU5VAosMEY1/JylzJ5x8vS2kVVbFDoK7MVAjg+GNLXzAPKS yPzFytvYsyq/g== From: Colin King To: James Bottomley , Jarkko Sakkinen , Mimi Zohar , David Howells , James Morris , "Serge E . Hallyn" , linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] security: keys: trusted: Fix memory leaks on allocated blob Date: Fri, 23 Jul 2021 18:21:21 +0100 Message-Id: <20210723172121.156687-1-colin.king@canonical.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Colin Ian King There are several error return paths that don't kfree the allocated blob, leading to memory leaks. Ensure blob is initialized to null as some of the error return paths in function tpm2_key_decode do not change blob. Add an error return path to kfree blob and use this on the current leaky returns. Addresses-Coverity: ("Resource leak") Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs") Signed-off-by: Colin Ian King --- security/keys/trusted-keys/trusted_tpm2.c | 30 ++++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index 0165da386289..930c67f98611 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -366,7 +366,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip, unsigned int private_len; unsigned int public_len; unsigned int blob_len; - u8 *blob, *pub; + u8 *blob = NULL, *pub; int rc; u32 attrs; @@ -378,22 +378,30 @@ static int tpm2_load_cmd(struct tpm_chip *chip, } /* new format carries keyhandle but old format doesn't */ - if (!options->keyhandle) - return -EINVAL; + if (!options->keyhandle) { + rc = -EINVAL; + goto err; + } /* must be big enough for at least the two be16 size counts */ - if (payload->blob_len < 4) - return -EINVAL; + if (payload->blob_len < 4) { + rc = -EINVAL; + goto err; + } private_len = get_unaligned_be16(blob); /* must be big enough for following public_len */ - if (private_len + 2 + 2 > (payload->blob_len)) - return -E2BIG; + if (private_len + 2 + 2 > (payload->blob_len)) { + rc = -E2BIG; + goto err; + } public_len = get_unaligned_be16(blob + 2 + private_len); - if (private_len + 2 + public_len + 2 > payload->blob_len) - return -E2BIG; + if (private_len + 2 + public_len + 2 > payload->blob_len) { + rc = -E2BIG; + goto err; + } pub = blob + 2 + private_len + 2; /* key attributes are always at offset 4 */ @@ -441,6 +449,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip, rc = -EPERM; return rc; + +err: + kfree(blob); + return rc; } /** -- 2.31.1