From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E4452DEA6B for ; Mon, 9 Feb 2026 20:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770667872; cv=none; b=MEDos/4w4QZhSMCrAF+cTF+SK4Jth4oyg+inEYqb8lUmib4Gqyx3QnhiRi8rcPee+xKPC0vS0pD3VqrlkNGQf5JHbHnjo+iPUYZtfjSU4uis5zV1DcwahVqAqh4RoNerwUBJIbm4Xw542LZYiqS8v60Bp6Hq3n5/qIzvVrvU92A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770667872; c=relaxed/simple; bh=fjQyBAxABYnsO50mltUfs7H9ApBzErpw/KcD0KJMF7Y=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=PMslZkaD/q5PVnCES6qH+vcoTEJg2uNGkktbhxP0RWCnXJ+WVPmDzuAhfWQIWrtw1YSzBRer2m/2IXP/TMF0ZSl/fyMnWeOxbbZcEhDDzWptC13XlTx+ccsCTFbfFrlmO0gQu12vJQxrb/XyS2De9FuerYx98oGACARyS5yXpYA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=MUctPnGe; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=KbGF/6J4; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="MUctPnGe"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="KbGF/6J4" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 619JaY773347712 for ; Mon, 9 Feb 2026 20:11:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= ppqvcaXrHUsmWddFWzepuOB36WFXUK3MUMjHqdkSFTM=; b=MUctPnGex54TOF8O kYN5Lg1kglVyuE3xCaMteZTqkMARjXgUkLu5dtclWCcBDT/tDtMBm+6PCP3oWKxd /grE9Z1Ic2xTaTsyTQtmjtG+Q5TlIEMbyXsSeOaWZoxgAXFZ9p1BhgOS1kf8A1lr fgtoPZkHEkERFEF7atBS0lTw1xWOlqJsYxsicEiWtQtv+kbGfZE8YGts+Xo3bjZB o0eB7QWVnRFgdJlOmuWMFvUJNAljGFfMNndVkp5he4yMFFGevI+o1FmYRRiyQLt3 bnA4jiHtzJ/rU0GcfEOaMAEpfv/bhKyZ33UWNpZn0Y3dUAiGzzTPwDCyAHun0aG6 doMNCw== Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4c79cy2ufe-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Mon, 09 Feb 2026 20:11:11 +0000 (GMT) Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2a75ed2f89dso35771085ad.1 for ; Mon, 09 Feb 2026 12:11:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1770667870; x=1771272670; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ppqvcaXrHUsmWddFWzepuOB36WFXUK3MUMjHqdkSFTM=; b=KbGF/6J40lG4fyG2OGUfsyDwJkFcUE/zNuBHAAy2AODmkq6/Jufhk2Ax0czf5UtrXQ fZxLxqM1YYj5e45sCNl+SwKXO5Dc8X9QqsHIXYwOXsjctq8r/5xeUCjaiEGkaE0Q3Gj5 Y47qff8BAtzDsO+/SNC23gsFN6R+ERRlkTL95awkL9a6pb7nKe6LKT8T+kORk5SEiEqo pkA/mxmHLwsjKsNLNN/MNKJk+luGBk7sZzMxIQ6fFRv/6uTO3ybhKS+l9GWnfjAmcT4g D6fptg0BcjWIMziOSd/T/sdkuJ9lmFfq5eEoCl/JtQFjVQJEdc8kL+Zh59AfTfnfHNTW doeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770667870; x=1771272670; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ppqvcaXrHUsmWddFWzepuOB36WFXUK3MUMjHqdkSFTM=; b=KrqGDBzOMJ798JxuSuxLsAK5KtCKB8wxI7uZ8tmnvRNSULkDbZ/go7whwEZdWFzSSw xMsywr8xrTiUyjkH3M+7u+0rBX4xD/vCBM7hik7naZehcu7QqPYBxioAyg09JkvKjj7e aZ3MtPJtD6AAYlAJUqq2+r6CO6dl232opyWbUqtvJuZrdRoGCOxzQn47zZB1kLWG6Uu7 d0sjKCml/D9Ls+nxUrQZ04ci2GOwSYiqdwlwQ7iLOVQgXaRBeyEghzmoajJjEPCQXAzp DYgZzzWmrHmbQo9fEHMYQza5694m4uVorDQE+ebijiRPoM/lDjiyQTJSC9x/qI8/MD3B HSjA== X-Forwarded-Encrypted: i=1; AJvYcCUM42f5TjB1k8M4tRyg7ED5XRD62gSJbwS2YVcZkMN5fpdF8S6ZdYlzSQxP2OKlz4zBhvoQIXsQ3f2XiBQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzfBWrRxQoTmSCVb+43GX1K9I1ZFqHWkhtK/u+mcoS29mU1ou7+ WTVc+Fp6ioGgfq7LxgYeVAD1KiUjLPGQY5+8M8oQmkp4R+4Dx3OR2DJ/pBHAyHmgALSUPeJox06 1GqqygGyppLgvQ6uWLxIzzRV+WaB4n5I5LlqisK1pSsknTm97Fp2xf46LQ1bVHDDw9yw3k0eylw == X-Gm-Gg: AZuq6aLLmadFMpOMzKCFzt3hIBzX8kIsnvZQjntHd5TjCc4nbMIJw5k7vz6r16r6Y5k Xaj0Ik8rElwHn6sY47KOciEvcMtnwZGjraL70+iU1k1OnPq/k1TEMQ4T5GpAsh1oDsaep4Zc36F 3Im5uz9KpEY/M2duiIwvrjfWUUKReoejE7DurDQY0eAEkzmFi7k7lWp5Nx0NwucEnmtY5J0ynXw xGJi9oOT0kS/jHGrmtfcEOsBq6GBOPgMAUxnXmDHNNafk5tyI77TUyOALPY6ctDiVD29AfbzXti M4yNzMHUEU1xCefgyPDXdqLOIe+01tsC+FuQtVEB1LnXmP90oc2kiNmrbNuuEwXGHAgpAD6PH5B UTM+zDosTw+UTqEbEMnYaYOXrq5GBf1qd9+dgqM66+bIDVoiYWgXTPZhVepaeX8xVxJEMDOYRdM 8WrPw1ERM= X-Received: by 2002:a17:902:cec2:b0:2a0:8e35:969d with SMTP id d9443c01a7336-2a951821446mr129161485ad.39.1770667869805; Mon, 09 Feb 2026 12:11:09 -0800 (PST) X-Received: by 2002:a17:902:cec2:b0:2a0:8e35:969d with SMTP id d9443c01a7336-2a951821446mr129161135ad.39.1770667869046; Mon, 09 Feb 2026 12:11:09 -0800 (PST) Received: from [192.168.0.74] (n1-41-240-65.bla22.nsw.optusnet.com.au. [1.41.240.65]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a951c71edasm121492045ad.22.2026.02.09.12.11.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Feb 2026 12:11:08 -0800 (PST) Message-ID: Date: Tue, 10 Feb 2026 07:11:03 +1100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] tee: optee: prevent use-after-free when the client exits before the supplicant To: Jens Wiklander Cc: Sumit Garg , Arnd Bergmann , Michael Wu , linux-arm-msm@vger.kernel.org, op-tee@lists.trustedfirmware.org, linux-kernel@vger.kernel.org, Jerome Forissier References: <20260128-fix-use-after-free-v3-1-b0786670d927@oss.qualcomm.com> <55546b03-cbe6-40dd-b794-b2e81efde33a@oss.qualcomm.com> <4c9e4f5f-e0f3-42aa-852f-064f4024af26@oss.qualcomm.com> Content-Language: en-US From: Amirreza Zarrabi In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-ORIG-GUID: izkMHpXZbJHIrR0RyQtla00FGHvJQSaV X-Proofpoint-GUID: izkMHpXZbJHIrR0RyQtla00FGHvJQSaV X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMjA5MDE2NyBTYWx0ZWRfXygOoJnzh1Ytf mSarENKTxxqO3yTtIZmNj6S1kLOUk5qWJg10n82HrvYeaLbpK4P/thxgEpmjqCvfWWfOW0bLxbK I6T5qzRJP/sKb96GS5JrSmPTJUt/bF+QzusR3z1Tlvi6JXcJ7kZWn6ek9xLPcWhMgjiYLtlimk7 WIexcfYhUacGWhwH+tYhGagYvQmdT/fr2B/huDHT3hHqyoZIriM/Vv9/S13vBdO9YeeG8liANWZ ChTdgJN+QcHBhYOQYRVvPNZqL3dhafTt6O0NixmboGUV/t1ubBpCWsiMNgjyK9/gXPBco760tfR ZWq9b/YxmWfPGmr+Lq495aUSpxeeAIXECytDQdCmyGV//dkmcHgR7gkCxpfpf8W4aq8ZyedJfPH n0O8r7A8/0bBjZh9CXEltfFQmHWILWCN+coNyxwAJvk/SAgQLvFCLNAlDFuoPZyFPYCJixm04yb xcSU5oTPLHbsIbD0FEA== X-Authority-Analysis: v=2.4 cv=EtvfbCcA c=1 sm=1 tr=0 ts=698a3f5f cx=c_pps a=JL+w9abYAAE89/QcEU+0QA==:117 a=hi51d+lTLNy/RbqRqnOomQ==:17 a=IkcTkHD0fZMA:10 a=HzLeVaNsDn8A:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=Mpw57Om8IfrbqaoTuvik:22 a=GgsMoib0sEa3-_RKJdDe:22 a=VwQbUJbxAAAA:8 a=EUspDBNiAAAA:8 a=6BXAE0hZp0f7nhlt_LIA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=324X-CrmTo6CU4MGRt3R:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-02-09_01,2026-02-09_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 phishscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 spamscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2602090167 Hi Jens, On 2/6/2026 7:54 PM, Jens Wiklander wrote: > Hi Amir, > > On Thu, Feb 5, 2026 at 3:13 AM Amirreza Zarrabi > wrote: >> >> Hi Jens, >> >> On 2/4/2026 6:46 PM, Jens Wiklander wrote: >>> Hi Amir, >>> >>> On Tue, Feb 3, 2026 at 11:56 PM Amirreza Zarrabi >>> wrote: >>>> >>>> Hi Jens, >>>> >>>> On 2/3/2026 5:59 PM, Jens Wiklander wrote: >>>>> Hi, >>>>> >>>>> On Tue, Feb 3, 2026 at 3:09 AM Amirreza Zarrabi >>>>> wrote: >>>>>> >>>>>> Hi Jens, >>>>>> >>>>>> On 2/2/2026 10:36 PM, Jens Wiklander wrote: >>>>>>> Hi Amir, >>>>>>> >>>>>>> On Thu, Jan 29, 2026 at 4:22 AM Amirreza Zarrabi >>>>>>> wrote: >>>>>>>> >>>>>>>> Commit 70b0d6b0a199 ("tee: optee: Fix supplicant wait loop") made the >>>>>>>> client wait as killable so it can be interrupted during shutdown or >>>>>>>> after a supplicant crash. This changes the original lifetime expectations: >>>>>>>> the client task can now terminate while the supplicant is still processing >>>>>>>> its request. >>>>>>>> >>>>>>>> If the client exits first it removes the request from its queue and >>>>>>>> kfree()s it, while the request ID remains in supp->idr. A subsequent >>>>>>>> lookup on the supplicant path then dereferences freed memory, leading to >>>>>>>> a use-after-free. >>>>>>>> >>>>>>>> Serialise access to the request with supp->mutex: >>>>>>>> >>>>>>>> * Hold supp->mutex in optee_supp_recv() and optee_supp_send() while >>>>>>>> looking up and touching the request. >>>>>>>> * Let optee_supp_thrd_req() notice that the client has terminated and >>>>>>>> signal optee_supp_send() accordingly. >>>>>>>> >>>>>>>> With these changes the request cannot be freed while the supplicant still >>>>>>>> has a reference, eliminating the race. >>>>>>>> >>>>>>>> Fixes: 70b0d6b0a199 ("tee: optee: Fix supplicant wait loop") >>>>>>>> Signed-off-by: Amirreza Zarrabi >>>>>>>> --- >>>>>>>> Changes in v3: >>>>>>>> - Introduce processed flag instead of -1 for req->id. >>>>>>>> - Update optee_supp_release() as reported by Michael Wu. >>>>>>>> - Use mutex instead of guard. >>>>>>>> - Link to v2: https://lore.kernel.org/r/20250617-fix-use-after-free-v2-1-1fbfafec5917@oss.qualcomm.com >>>>>>>> >>>>>>>> Changes in v2: >>>>>>>> - Replace the static variable with a sentinel value. >>>>>>>> - Fix the issue with returning the popped request to the supplicant. >>>>>>>> - Link to v1: https://lore.kernel.org/r/20250605-fix-use-after-free-v1-1-a70d23bff248@oss.qualcomm.com >>>>>>>> --- >>>>>>>> drivers/tee/optee/supp.c | 122 +++++++++++++++++++++++++++++++++-------------- >>>>>>>> 1 file changed, 86 insertions(+), 36 deletions(-) >>>>>>> > [snip] > > >>>>>>>> @@ -328,9 +378,8 @@ int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, >>>>>>>> >>>>>>>> mutex_lock(&supp->mutex); >>>>>>>> req = supp_pop_req(supp, num_params, param, &num_meta); >>>>>>>> - mutex_unlock(&supp->mutex); >>>>>>>> - >>>>>>>> if (IS_ERR(req)) { >>>>>>>> + mutex_unlock(&supp->mutex); >>>>>>> >>>>>>> We need a way to tell the difference between an id not found and an id >>>>>>> removed because of a killed requester. >>>>>>> How about storing NULL for revoked requests instead of an err-pointer? >>>>>>> >>>>>> >>>>>> Not sure I'm following correctly. Are you expecting supp_pop_req() >>>>>> to return NULL instead of an err-pointer when a request has been revoked? >>>>> >>>>> I was looking at it again, and storing an err-pointer as you do in >>>>> this patch has the advantage that we can tell whether the ID has been >>>>> revoked or was never supplied. In the latter case, it suggests that >>>>> the supplicant is doing something wrong and might as well restart in >>>>> an attempt to recover. So, please keep using an err-pointer as a >>>>> placeholder, but we must be able to distinguish a revoked request from >>>>> other errors to make sure that the supplicant doesn't restart due to a >>>>> revoked request. >>>>> >>>> >>>> Understood. What if I switch the stored err-pointer to EBADF instead of ENOENT >>>> (which seems more natural), so it doesn't overlap with other supp_pop_req() error >>>> codes and the supplicant can reliably detect it. >>> >>> Any error returned by TEE_IOC_SUPPL_SEND (or TEE_IOC_SUPPL_RECV) will >>> cause the tee-supplicant to exit. Even if we update it to ignore >>> certain codes, we must also consider the installed base. There's not >>> much tee-supplicant could do with this error, except logging it. But I >>> don't think that is very useful either. Unless the tee-supplicant does >>> anything wrong or if the device isn't working any longer, we shouldn't >>> return an error. >>> >> >> The direction of data flow in optee_supp_send is from the supplicant to optee, >> so the only way I can return meaningful information back to the supplicant is >> through the return value. I suppose I could simply ignore the revoked request >> and return success, but it might be useful for the supplicant to know about it >> in case it needs to roll back something. >> >> At this point I'm out of ideas :). Do you have any suggestions on how I can >> inform the supplicant about a revoked request in optee_supp_send while returning >> success return value? > > This became a bit harder than I first thought. At this point, to fix > the possible use-after-free, we have two options: > 1. Returning an error code: tee-supplicant will exit > 2. Returning OK: worst case, tee-supplicant can leak memory > > During reboot, neither case is a problem. During normal operation, > it's annoying if tee-supplicant exists, but you still need some > privileges to kill the client. If we return an error, it's enough to > update tee-supplicant to handle that error case, and we're done. The > advantage is no added code to the kernel. > > I think we should do what you suggested and return an error. This will > not happen during normal operation. We'll fix tee-supplicant to handle > the return error properly. tee-supplicant doesn't care about what > error code it gets. If there's an error in TEE_IOC_SUPPL_SEND, it > knows that no one will receive whatever was sent, and cleanup is > needed. > > Sumit and Jerome, what do you think? > Thanks Jens, I'll proceed with v4. Best Regards, Amir > Cheers, > Jens