Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp3036093pxb; Tue, 13 Apr 2021 17:07:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxTZyLaNzwbEAejZqopVscZ4SlscLI9xwBVAwp6POTOSw2GJcnBQy6KsczzWkLK9Q3JA1Cy X-Received: by 2002:a63:1556:: with SMTP id 22mr35717834pgv.142.1618358879138; Tue, 13 Apr 2021 17:07:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618358879; cv=none; d=google.com; s=arc-20160816; b=LrlLNQG4GzaJOe4qq/Mo1lOkg0BNngiEM/iN7LzZ8o8JwLoU8wUiKGYYvDWYc/nTG6 pe2JD89vtdkSmO946ENq6QlKM6UncaF4kpkDANtzoSmy5TsYY4SD2awy7SOKPbeGwVjw p3r28sGm6yefJTupVprYVuVEZiwTE9ild4g0H34s71e1tEtax0proTU3DKGCxoAQBiXD mw7uqEiSyIzqqAonlDOXhkm8HzbuvsOkUkoImRlAG+1bU80HDDV/+ZbYkZN8r/FMWjuL 8YhYwtcM5xN5exRBLMH3u2BdtKgYaEcU7Ynh3UaRV1m/fzgMsBspaW5yVtSjcUl3NG6T wq7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=VLF6/P7EZjK4b9JVKvC4s0oerk5rpO6dK9mxoGQATgk=; b=L0sz+NbKS0hWphavw/io+TXBfoBGYIQ4gv5xXZ/oLouj88st0QALvVASfYBl7/HmuG lPbYYweArNNk5pz8SqPwFkNaltKUZeX4GMAO7USBz0oR4p6zuV7JJtF15AFY4cunCb98 WODpsN+fLt8t7utLcRRvZf3qmq2ZYTZETDUNwp0Aa3BNijSgVcrqRp9FDe6egoEngeZZ Bnk041Ijed4h5NTpKSDFOmCnNVAyqzk5Pfcl1hmfx4GK4PbuYPT2Xzn1hzyyjsFa/c/p siUZSqSH0TBy1YbxnUsfdIhXY69/3luSfm4dB5JKq9l9jHErikvMBpDR/8G1KjLVLMYD SDMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QBMerjyf; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cq16si4562889pjb.38.2021.04.13.17.07.45; Tue, 13 Apr 2021 17:07:59 -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=@gmail.com header.s=20161025 header.b=QBMerjyf; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230342AbhDMUN5 (ORCPT + 99 others); Tue, 13 Apr 2021 16:13:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229590AbhDMUNz (ORCPT ); Tue, 13 Apr 2021 16:13:55 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82518C061574; Tue, 13 Apr 2021 13:13:35 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id w4so13919578wrt.5; Tue, 13 Apr 2021 13:13:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=VLF6/P7EZjK4b9JVKvC4s0oerk5rpO6dK9mxoGQATgk=; b=QBMerjyfmkBCZjkkYTsAlMSuXiu3Z0Jzf81vhQdmkJxcRdnOPQdw53ibPDLTQm9beD 9crudOocqZowDvYl+ESSSmVQFQ3xNWXlQTWJJKOB2S3ISVv1SyHDSZhc5c6lLTfs3Icj G6A/wmFfv4bdKBHVP8N+ctQoLWa+iArTfjJEqDvabCAH2DHrLspZiy4CbZaoHwqwYbP6 Cf3HSrWabCPq9XOE67ePcQmxGuZejRRh08VOdANdzCU4PC2z19O9ESjqdtUK71fTj+Db ZlU0xtlwgrryRWP3Uet6aw6DY19FdSnztVZ+SPmwyHxWR4byHksNnHgaoudGaencyEYs Y0kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=VLF6/P7EZjK4b9JVKvC4s0oerk5rpO6dK9mxoGQATgk=; b=r6zFhe6PC44fdrahOFLNfYhbosy/TK7UiPttp3UGm/22Zd1n+oyZUzLVGM+3c0gddt ezyP8GwHdS6WAhMkuo/YkAJxpuZarEzoMv38p6bpVnWqpKUKI9Yh+Myo6+GU+dU5KAzo Pth4m751EVWmDpo9gORHLSazMpmBNGzktUBJOSkhcIrX3u/i3h/+vOGWvnj/QqUJ92Ak 3dy0GsUZCB8usjdhVrJ9OwLwFWb+3GgiwV7ZcZIkjtZgMJ3nNHAzS5/VgEYE/PJW7fsB pIK2U5BKpnm7+4qYaGzTghAsmZjAV9ge1MdUlhXyFEDxs8V45HGmtjr3ciyhthydmL/O qNgw== X-Gm-Message-State: AOAM5302k6fCWM638mROFWRVknEaju5lwa+7ussrXeltiJ4YPdo4aXbj 2qVapT8SWfupO6y3AhEAPYU= X-Received: by 2002:a5d:6a90:: with SMTP id s16mr19299060wru.163.1618344814180; Tue, 13 Apr 2021 13:13:34 -0700 (PDT) Received: from LEGION ([39.46.65.172]) by smtp.gmail.com with ESMTPSA id f6sm9820799wrt.19.2021.04.13.13.13.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Apr 2021 13:13:33 -0700 (PDT) Date: Wed, 14 Apr 2021 01:13:26 +0500 From: Muhammad Usama Anjum To: James.Bottomley@HansenPartnership.com, James Bottomley , Jarkko Sakkinen , Mimi Zohar , David Howells , James Morris , "Serge E. Hallyn" , "open list:KEYS-TRUSTED" , "open list:KEYS-TRUSTED" , "open list:SECURITY SUBSYSTEM" , open list Cc: musamaanjum@gmail.com, kernel-janitors@vger.kernel.org, dan.carpenter@oracle.com, colin.king@canonical.com Subject: [PATCH] security: keys: trusted: prevent memory leak in error path Message-ID: <20210413201326.GA649679@LEGION> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org tpm2_key_decode sometimes allocates blob. This blob should be freed if some error occurs later in the function. Free the blob before returning from this function if it was allocated. Addresses-Coverity: ("Prevent memory leak") Fixes: 14676f1eb796 ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs") Signed-off-by: Muhammad Usama Anjum --- This is only build tested. security/keys/trusted-keys/trusted_tpm2.c | 37 +++++++++++++++-------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index d225ad140960..4551384124e0 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -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 */ @@ -406,12 +414,14 @@ static int tpm2_load_cmd(struct tpm_chip *chip, payload->migratable = 1; blob_len = private_len + public_len + 4; - if (blob_len > payload->blob_len) - return -E2BIG; + if (blob_len > payload->blob_len) { + rc = -E2BIG; + goto err; + } rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); if (rc) - return rc; + goto err; tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, @@ -433,12 +443,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip, (__be32 *) &buf.data[TPM_HEADER_SIZE]); out: - if (blob != payload->blob) - kfree(blob); tpm_buf_destroy(&buf); if (rc > 0) rc = -EPERM; +err: + if (blob != payload->blob) + kfree(blob); return rc; } -- 2.25.1