From patchwork Mon Nov 5 12:35:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Garry X-Patchwork-Id: 150186 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2544996ljp; Mon, 5 Nov 2018 04:33:53 -0800 (PST) X-Google-Smtp-Source: AJdET5fDzyiTzW2QYGrnj5SwXgpjhZJJPBYJRXgSIILFov+S4VP5tWpzu60Pplp9r3E32E+7QWQY X-Received: by 2002:a17:902:2909:: with SMTP id g9-v6mr21533564plb.223.1541421233786; Mon, 05 Nov 2018 04:33:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541421233; cv=none; d=google.com; s=arc-20160816; b=VqOFdzVW6Kx47D6qGEhsPakWVwWTiwjvP63BXd7SiJ+uyh9AsfElFtK+52E8iOuaJ5 3le8MOoSo8+IUgfb02V3cgwOd8w6TjzHLdOxcqjpcUaKiq80H7MZhidNrVjgnT0ddNjh sNT3j0FJKGHUEzK4JCQTCw9j5N5613paZr+hmOID2qFNZq3p5x1UayD3GYYOFauiu7gA GNzDdj3Hv6DRQcnitOZB3/k59jgB2bLfsyOKxLk/eHhhrjYZMIQS72NAZ12kB/IKogOW c2hoFWcEXMJFOsAYhRZ4vR6hTZmkEiJXNMrdp4xbnm1iIIX2PAZyBblGbgDeXx0OhW+3 np9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=w/0ypWwb0/blSn9cSwZq4GgjP+4HBMHbvzqz+B8uqCY=; b=M6oH9SgbSr4k9URUksc6ma3V/ZpU3RbJsLAPnYcgcHR+DMMR6bo7GK9SIaRfWxnlM2 e1vh/kX336/nSSc/leJfZ5DRfvCF1jL5MDtvpJghDKhhlh6yZVBXQiKwDloxcWX/KCKY KDqnSQECz4Hhg2vik2KMLvR6GvpDeBsr0FQQhR6ClImdP2QDCPzIFl9BycSsQtZ41VkQ tebyUOPQbumgcMGmJG85gaj3XASeAjDh9J+KsvptW+wIE3QuCvOh9yjwEm2bc7/YPMkq lT/H+BV3D1vMKxbpogulA1zksQk9yBXz0aXpPELIelC2/4KRX6WJP9C5SYx4BXCn/1po wZ8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k3-v6si39876186pld.25.2018.11.05.04.33.53; Mon, 05 Nov 2018 04:33:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729078AbeKEVxY (ORCPT + 2 others); Mon, 5 Nov 2018 16:53:24 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:14594 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729047AbeKEVxY (ORCPT ); Mon, 5 Nov 2018 16:53:24 -0500 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 5539AD677B1D2; Mon, 5 Nov 2018 20:33:48 +0800 (CST) Received: from localhost.localdomain (10.67.212.75) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.408.0; Mon, 5 Nov 2018 20:33:43 +0800 From: John Garry To: , CC: , , , , John Garry Subject: [PATCH 2/2] crypto: hisilicon - Fix reference after free of memories on error path Date: Mon, 5 Nov 2018 20:35:15 +0800 Message-ID: <1541421315-25623-3-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1541421315-25623-1-git-send-email-john.garry@huawei.com> References: <1541421315-25623-1-git-send-email-john.garry@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.212.75] X-CFilter-Loop: Reflected Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org coccicheck currently warns of the following issues in the driver: drivers/crypto/hisilicon/sec/sec_algs.c:864:51-66: ERROR: reference preceded by free on line 812 drivers/crypto/hisilicon/sec/sec_algs.c:864:40-49: ERROR: reference preceded by free on line 813 drivers/crypto/hisilicon/sec/sec_algs.c:861:8-24: ERROR: reference preceded by free on line 814 drivers/crypto/hisilicon/sec/sec_algs.c:860:41-51: ERROR: reference preceded by free on line 815 drivers/crypto/hisilicon/sec/sec_algs.c:867:7-18: ERROR: reference preceded by free on line 816 It would appear than on certain error paths that we may attempt reference- after-free some memories. This patch fixes those issues. The solution doesn't look perfect, but having same memories free'd possibly from separate functions makes it tricky. Fixes: 915e4e8413da ("crypto: hisilicon - SEC security accelerator driver") Reviewed-by: Jonathan Cameron Signed-off-by: John Garry --- drivers/crypto/hisilicon/sec/sec_algs.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) -- 1.9.1 diff --git a/drivers/crypto/hisilicon/sec/sec_algs.c b/drivers/crypto/hisilicon/sec/sec_algs.c index 32c6c02..cdc4f9a 100644 --- a/drivers/crypto/hisilicon/sec/sec_algs.c +++ b/drivers/crypto/hisilicon/sec/sec_algs.c @@ -808,13 +808,6 @@ static int sec_alg_skcipher_crypto(struct skcipher_request *skreq, * more refined but this is unlikely to happen so no need. */ - /* Cleanup - all elements in pointer arrays have been coppied */ - kfree(splits_in_nents); - kfree(splits_in); - kfree(splits_out_nents); - kfree(splits_out); - kfree(split_sizes); - /* Grab a big lock for a long time to avoid concurrency issues */ mutex_lock(&queue->queuelock); @@ -829,13 +822,13 @@ static int sec_alg_skcipher_crypto(struct skcipher_request *skreq, (!queue->havesoftqueue || kfifo_avail(&queue->softqueue) > steps)) || !list_empty(&ctx->backlog)) { + ret = -EBUSY; if ((skreq->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { list_add_tail(&sec_req->backlog_head, &ctx->backlog); mutex_unlock(&queue->queuelock); - return -EBUSY; + goto out; } - ret = -EBUSY; mutex_unlock(&queue->queuelock); goto err_free_elements; } @@ -844,7 +837,15 @@ static int sec_alg_skcipher_crypto(struct skcipher_request *skreq, if (ret) goto err_free_elements; - return -EINPROGRESS; + ret = -EINPROGRESS; +out: + /* Cleanup - all elements in pointer arrays have been copied */ + kfree(splits_in_nents); + kfree(splits_in); + kfree(splits_out_nents); + kfree(splits_out); + kfree(split_sizes); + return ret; err_free_elements: list_for_each_entry_safe(el, temp, &sec_req->elements, head) {