Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3631003imu; Mon, 28 Jan 2019 08:08:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN4ErogfwIRjw0jfQlAX+6whNpatlUQuZ+Fexdq2sxtvPH2Q9p6mLWokXARxQWrSRrPJx/Bi X-Received: by 2002:a63:2e88:: with SMTP id u130mr20769965pgu.9.1548691686321; Mon, 28 Jan 2019 08:08:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548691686; cv=none; d=google.com; s=arc-20160816; b=DolqHdI8OxARt8PGZAoVUUnsEvHX/w2nbnbnauhU6ZFbDh0bTP+qtNmYZqL6qVnqw9 CBDWWkQtssvTqsoW5gm4gwy9vwiJwXOfGcgU9ClIqlTAmiKSApOe1N7e5OaqHN5+kck0 WtsuVhokCGxTevcOR6xSv4yA99OidU8AYPsx2NT0wU62pGER7d92uHiC4yd+rcvKCmXz NK0FG9xHKG2lFF8l00ilcI3CwQKU6YxEKbF7Wq/CNfpSMEyyz43h9UK/02P5LL7rdomx ryWIIq3aqoWxu0JBv+OepSRpsCXgqZLqoCJ8tjGwQtZAQdsYMgjnDoUBPgalRx+MF8zL HdWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u193gOYleIpDn5SkC2h0N5Q4wyKPFX05EI1GF2OsrFU=; b=ozApI10OorsmYYlD9LBR6ILBJ3DfhC60Hg47Bmrnir75kbKheEty7i86iWRivCdF1a SCaUCemll5ZXEDnhxYPASj9YoXcfhlBcUAoowADEtPoSvKAc/a71VLzYGrkyr4bvp09w g+SZMjTZ1o7zBF7KV+PGWHX+EhsAVzIkpw9oZvTw7WdGSiZ5iAaagRH2r84fv896d3gC Wh/a2Ap40do4uo359I8h3PBctSPshXGSf68kFA6i1U4iBOfqE4iWi7pJdOANO4deFXBn 2lF94BnQrr+8FTSJ+1LBp48rCwiKmavpSXLM4mnqnm4dAjde64W8UlWUQyCr6lmhXLkE +y4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eVZKSXYE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v25si8171025pgk.341.2019.01.28.08.07.51; Mon, 28 Jan 2019 08:08:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eVZKSXYE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731905AbfA1QFq (ORCPT + 99 others); Mon, 28 Jan 2019 11:05:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:52906 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730074AbfA1QFl (ORCPT ); Mon, 28 Jan 2019 11:05:41 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6FCB82175B; Mon, 28 Jan 2019 16:05:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691540; bh=bN+mdi3GIbdRSrd/hzdW3MpGARdNVV8CVrPJZ0ZNI0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eVZKSXYEP8eoF+dLomMmhooNUn3U/l8AyrYuflJPYwFByYHlsoQhSu77YwcgE5NNJ shUGKN96nvfO1+k2Ee2mNnOFISzSv0VBOkb8j1Erbokw/D905mVr02740wMhcHqCEE Z0q9DZZ3HXmZZGD3cmovj5Ym+ZeIcwUBeUB6N5UI= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhizhou Zhang , Jens Wiklander , Sasha Levin Subject: [PATCH AUTOSEL 4.19 136/258] tee: optee: avoid possible double list_del() Date: Mon, 28 Jan 2019 10:57:22 -0500 Message-Id: <20190128155924.51521-136-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhizhou Zhang [ Upstream commit b2d102bd0146d9eb1fa630ca0cd19a15ef2f74c8 ] This bug occurs when: - a new request arrives, one thread(let's call it A) is pending in optee_supp_req() with req->busy is initial value false. - tee-supplicant is killed, then optee_supp_release() is called, this function calls list_del(&req->link), and set supp->ctx to NULL. And it also wake up process A. - process A continues, it firstly checks supp->ctx which is NULL, then checks req->busy which is false, at last run list_del(&req->link). This triggers double list_del() and results kernel panic. For solve this problem, we rename req->busy to req->in_queue, and associate it with state of whether req is linked to supp->reqs. So we can just only check req->in_queue to make decision calling list_del() or not. Signed-off-by: Zhizhou Zhang Signed-off-by: Jens Wiklander Signed-off-by: Sasha Levin --- drivers/tee/optee/supp.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/tee/optee/supp.c b/drivers/tee/optee/supp.c index df35fc01fd3e..43626e15703a 100644 --- a/drivers/tee/optee/supp.c +++ b/drivers/tee/optee/supp.c @@ -19,7 +19,7 @@ struct optee_supp_req { struct list_head link; - bool busy; + bool in_queue; u32 func; u32 ret; size_t num_params; @@ -54,7 +54,6 @@ void optee_supp_release(struct optee_supp *supp) /* Abort all request retrieved by supplicant */ idr_for_each_entry(&supp->idr, req, id) { - req->busy = false; idr_remove(&supp->idr, id); req->ret = TEEC_ERROR_COMMUNICATION; complete(&req->c); @@ -63,6 +62,7 @@ void optee_supp_release(struct optee_supp *supp) /* Abort all queued requests */ list_for_each_entry_safe(req, req_tmp, &supp->reqs, link) { list_del(&req->link); + req->in_queue = false; req->ret = TEEC_ERROR_COMMUNICATION; complete(&req->c); } @@ -103,6 +103,7 @@ u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params, /* Insert the request in the request list */ mutex_lock(&supp->mutex); list_add_tail(&req->link, &supp->reqs); + req->in_queue = true; mutex_unlock(&supp->mutex); /* Tell an eventual waiter there's a new request */ @@ -130,9 +131,10 @@ u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params, * will serve all requests in a timely manner and * interrupting then wouldn't make sense. */ - interruptable = !req->busy; - if (!req->busy) + if (req->in_queue) { list_del(&req->link); + req->in_queue = false; + } } mutex_unlock(&supp->mutex); @@ -176,7 +178,7 @@ static struct optee_supp_req *supp_pop_entry(struct optee_supp *supp, return ERR_PTR(-ENOMEM); list_del(&req->link); - req->busy = true; + req->in_queue = false; return req; } @@ -318,7 +320,6 @@ static struct optee_supp_req *supp_pop_req(struct optee_supp *supp, if ((num_params - nm) != req->num_params) return ERR_PTR(-EINVAL); - req->busy = false; idr_remove(&supp->idr, id); supp->req_id = -1; *num_meta = nm; -- 2.19.1