Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2588715pxk; Sun, 20 Sep 2020 09:24:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwaZw3fsz6bMi7u+WFfaZWu1nnFJ9C6uzclfVtrmX/X926CydtxQTrAS1qu8fGx/b02OepI X-Received: by 2002:a17:906:4c42:: with SMTP id d2mr44553869ejw.506.1600619074450; Sun, 20 Sep 2020 09:24:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600619074; cv=none; d=google.com; s=arc-20160816; b=Z2nE8oN7AKStoswSQOXpTR9NGaYOYlxMMM+zTYf0o8+3F9YkBweAf3CQvJpnQTDKEm i7FIyh760tZZNIv0CaHMdWujfxbCqix2Z8G3dYgpt4xQRDj7IykOi5IuZgMNeajIFvlX 1ouR/HbH+HqIihNpXLtCpaOuwmakerI5e+S8TUDC4jsdfyOqxQaKDQp8RfmC2FZlhEN+ q/zQQYyz1d0JYvrtFFqn+vCtJFlfouOqyBgSebC54AvFByVPLC7LGgc/wIPZB8GFYcH2 TAg7lZLpVjabk1ysVNoFWJK3kDu9RZd0bbRuDJv0yY5dmxeyVCeE158fbTZaCNRGw85w 9zIw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Bcjy9RFTfx4lfc/ie5lgHVk3BsbmsIgLOG5+BOPkDLM=; b=FSQupYtH+P5ohzXA7WLFSqxSTTnQyGkmIjh6YEIZfgrb2Jgv7N7PfnVk1mxOB/GUuR lXmMlPQCIEW0Gp1wFsM6mqm2YHuARJsn3HjdDXl7A//2wr4Js1BUURcB5onYwBbHYZlj qZi9Svo9Q/BxmT2DdoMuKIIajDoFNc84nya286uSKqN5T49fdHxxg70duU4isFrIBEaB 3LEnDrwufMFNwP1k+Uk9Nr6TBvnTyce3OaC1sdeVN54E6Ubhpsq8HTmNM3VAkjVJfLZS fCZYOqnNPlBGvXaqfa3MybP66Wg7Al01KQsrm+pxoxIy+1giBwJlziVUSQOcW4De1gfe OdNg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n19si6323354edq.82.2020.09.20.09.24.11; Sun, 20 Sep 2020 09:24:34 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726618AbgITQWP (ORCPT + 99 others); Sun, 20 Sep 2020 12:22:15 -0400 Received: from out30-44.freemail.mail.aliyun.com ([115.124.30.44]:47448 "EHLO out30-44.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726375AbgITQWA (ORCPT ); Sun, 20 Sep 2020 12:22:00 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04423;MF=tianjia.zhang@linux.alibaba.com;NM=1;PH=DS;RN=34;SR=0;TI=SMTPD_---0U9VME2p_1600618867; Received: from localhost(mailfrom:tianjia.zhang@linux.alibaba.com fp:SMTPD_---0U9VME2p_1600618867) by smtp.aliyun-inc.com(127.0.0.1); Mon, 21 Sep 2020 00:21:07 +0800 From: Tianjia Zhang To: Herbert Xu , "David S. Miller" , David Howells , Eric Biggers , Jarkko Sakkinen , Maxime Coquelin , Alexandre Torgue , James Morris , "Serge E. Hallyn" , Stephan Mueller , Marcelo Henrique Cerri , "Steven Rostedt (VMware)" , Masahiro Yamada , Brendan Higgins , Andrew Morton , Johannes Weiner , Waiman Long , Mimi Zohar , Lakshmi Ramasubramanian , Colin Ian King , Tushar Sugandhi , Vitaly Chikunov , "Gilad Ben-Yossef" , Pascal van Leeuwen , linux-crypto@vger.kernel.org, keyrings@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Xufeng Zhang , Jia Zhang , Tianjia Zhang Subject: [PATCH v7 06/10] crypto: testmgr - Fix potential memory leak in test_akcipher_one() Date: Mon, 21 Sep 2020 00:20:59 +0800 Message-Id: <20200920162103.83197-7-tianjia.zhang@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.3.ge56e4f7 In-Reply-To: <20200920162103.83197-1-tianjia.zhang@linux.alibaba.com> References: <20200920162103.83197-1-tianjia.zhang@linux.alibaba.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the 'key' allocation fails, the 'req' will not be released, which will cause memory leakage on this path. This patch adds a 'free_req' tag used to solve this problem, and two new err values are added to reflect the real reason of the error. Signed-off-by: Tianjia Zhang --- crypto/testmgr.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crypto/testmgr.c b/crypto/testmgr.c index cd002a030af5..ed8e29efe280 100644 --- a/crypto/testmgr.c +++ b/crypto/testmgr.c @@ -3954,7 +3954,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, key = kmalloc(vecs->key_len + sizeof(u32) * 2 + vecs->param_len, GFP_KERNEL); if (!key) - goto free_xbuf; + goto free_req; memcpy(key, vecs->key, vecs->key_len); ptr = key + vecs->key_len; ptr = test_pack_u32(ptr, vecs->algo); @@ -3966,7 +3966,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, else err = crypto_akcipher_set_priv_key(tfm, key, vecs->key_len); if (err) - goto free_req; + goto free_key; /* * First run test which do not require a private key, such as @@ -3976,7 +3976,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, out_len_max = crypto_akcipher_maxsize(tfm); outbuf_enc = kzalloc(out_len_max, GFP_KERNEL); if (!outbuf_enc) - goto free_req; + goto free_key; if (!vecs->siggen_sigver_test) { m = vecs->m; @@ -3995,6 +3995,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, op = "verify"; } + err = -E2BIG; if (WARN_ON(m_size > PAGE_SIZE)) goto free_all; memcpy(xbuf[0], m, m_size); @@ -4061,6 +4062,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, c_size = req->dst_len; } + err = -E2BIG; op = vecs->siggen_sigver_test ? "sign" : "decrypt"; if (WARN_ON(c_size > PAGE_SIZE)) goto free_all; @@ -4097,9 +4099,10 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, free_all: kfree(outbuf_dec); kfree(outbuf_enc); +free_key: + kfree(key); free_req: akcipher_request_free(req); - kfree(key); free_xbuf: testmgr_free_buf(xbuf); return err; -- 2.19.1.3.ge56e4f7