From patchwork Mon Sep 5 18:55:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Kumar Singh X-Patchwork-Id: 603458 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE57AC6FA83 for ; Mon, 5 Sep 2022 18:56:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232012AbiIESz7 (ORCPT ); Mon, 5 Sep 2022 14:55:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231383AbiIESz6 (ORCPT ); Mon, 5 Sep 2022 14:55:58 -0400 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F34112A426; Mon, 5 Sep 2022 11:55:56 -0700 (PDT) Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 285IpOor013121; Mon, 5 Sep 2022 18:55:54 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=qcppdkim1; bh=xokcUehziQh2CEwSHfQ5j2vgoUPPaEwvs3OtXW+Uar0=; b=JR/KXZ2m2Nhpy8lxogeXae0gulwt8SFTp9g3JJlTtKOog9FA6SsINSeBfuSvhTPm2NxM WHi7DxBQqqv+e6g0iqRCMJzTxtOqb8YsplYDbNb7ecSYQsepWttqTKwvzdxLDQ00IeDs nWOiAR9CahN6dbbo1rfa9sgzW94aokWtPxExYFYukpMB6cQrdmeeLIT7bhS56mVGpiL1 9uUOwxhGV1bSn+I4I0I23OLsvfxgJW4G8/AdIKiYlNUXeSyBGlHRTJ0pNhNlDUcZM3Lo sZk52iv+qBpQfpdus63GRTHFaJVRi0OFKKAKHMBvRrk64OgHJQ1S1HNtc0P412WGgL+Y rQ== Received: from nalasppmta01.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3jby58w0ca-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 05 Sep 2022 18:55:54 +0000 Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 285ItrZ5001203 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 5 Sep 2022 18:55:53 GMT Received: from deesin-linux.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 5 Sep 2022 11:55:50 -0700 From: Deepak Kumar Singh To: , , , CC: , , , Deepak Kumar Singh Subject: [PATCH V2 1/2] rpmsg: glink: Add lock to avoid race when rpmsg device is released Date: Tue, 6 Sep 2022 00:25:19 +0530 Message-ID: <1662404120-24338-1-git-send-email-quic_deesin@quicinc.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: BkxiAMGpwY7PJly-e8-Cc14p3Y3KqlFo X-Proofpoint-GUID: BkxiAMGpwY7PJly-e8-Cc14p3Y3KqlFo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-05_14,2022-09-05_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 spamscore=0 malwarescore=0 adultscore=0 priorityscore=1501 mlxscore=0 phishscore=0 clxscore=1011 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2209050092 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org When remote host goes down glink char device channel is freed, At the same time user space apps can still try to open/poll rpmsg char device which will result in calling rpmsg_create_ept. This may cause reference to already freed context of glink chardev channel and result in below crash signatures - 1) rpmsg_create_ept+0x40/0xa0 rpmsg_eptdev_open+0x88/0x138 chrdev_open+0xc4/0x1c8 do_dentry_open+0x230/0x378 2) rpmsg_poll+0x5c/0x80 rpmsg_eptdev_poll+0x84/0xa4 do_sys_poll+0x22c/0x5c8 This patch adds proper lock and check condition to avoid such crash. Signed-off-by: Deepak Kumar Singh --- drivers/rpmsg/rpmsg_char.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 4f21891..5500dc0 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -75,6 +75,7 @@ int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data) struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev); mutex_lock(&eptdev->ept_lock); + eptdev->rpdev = NULL; if (eptdev->ept) { rpmsg_destroy_ept(eptdev->ept); eptdev->ept = NULL; @@ -126,6 +127,11 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp) return -EBUSY; } + if (!eptdev->rpdev) { + mutex_unlock(&eptdev->ept_lock); + return -ENETRESET; + } + get_device(dev); /* @@ -277,7 +283,9 @@ static __poll_t rpmsg_eptdev_poll(struct file *filp, poll_table *wait) if (!skb_queue_empty(&eptdev->queue)) mask |= EPOLLIN | EPOLLRDNORM; + mutex_lock(&eptdev->ept_lock); mask |= rpmsg_poll(eptdev->ept, filp, wait); + mutex_unlock(&eptdev->ept_lock); return mask; } From patchwork Mon Sep 5 18:55:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Kumar Singh X-Patchwork-Id: 602774 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17C70C6FA89 for ; Mon, 5 Sep 2022 18:56:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232315AbiIES4B (ORCPT ); Mon, 5 Sep 2022 14:56:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232263AbiIES4A (ORCPT ); Mon, 5 Sep 2022 14:56:00 -0400 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68A712A426; Mon, 5 Sep 2022 11:55:59 -0700 (PDT) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 285Ik89h026486; Mon, 5 Sep 2022 18:55:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=qcppdkim1; bh=NmgrSPTzg48CBHlab3ChVbglQtqav8ym8MQg7TLtMEw=; b=Cx4WbjmfLawbxkZg+soUgDqrbpoe60Nbphj2No9qwX7S+lR2rHloGdxcBTjCb/vUhiLB 7UBNqZYPZA8c4PnQHEGcPhkBgALprDZcRNBwOKxOh/rO/nyFlcdw24r1kZxPqGW3FIrK 4RRPtzlEZzltBJOsgIy0a3a3TBVyV8vyjzWRgYfNDhTSEB1kHYzTbclCBtYFj7IaGiMS OiyrcbmLopv1F31MLTeH9ZyOROyEnANYsXgCarSNTiRcyRr5l3l3T1gBAzG/pZViy6Rk Ou/F3l4SszRmZZWtAUUsntHNQE7hoC+zqr0blPaz1XtFIpMwJOid9369eFo99gF6aeyN FA== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3jbxh750et-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 05 Sep 2022 18:55:57 +0000 Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 285ItuFT008861 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 5 Sep 2022 18:55:56 GMT Received: from deesin-linux.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Mon, 5 Sep 2022 11:55:53 -0700 From: Deepak Kumar Singh To: , , , CC: , , , Deepak Kumar Singh Subject: [PATCH V2 2/2] rpmsg: glink: Add lock to rpmsg_ctrldev_remove Date: Tue, 6 Sep 2022 00:25:20 +0530 Message-ID: <1662404120-24338-2-git-send-email-quic_deesin@quicinc.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1662404120-24338-1-git-send-email-quic_deesin@quicinc.com> References: <1662404120-24338-1-git-send-email-quic_deesin@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: 16y-isuszVKabiCJsl1xhn-NeoAB89U0 X-Proofpoint-GUID: 16y-isuszVKabiCJsl1xhn-NeoAB89U0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-05_14,2022-09-05_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 phishscore=0 adultscore=0 lowpriorityscore=0 bulkscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 spamscore=0 mlxlogscore=856 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2209050092 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Hold ctrl device lock in rpmsg_ctrldev_remove to avoid any new create ept call to proceed, otherwise new ept creation and associted char device may suceed. Any further call from user space for rpmsg_eptdev_open will reference already freed rpdev and will result in crash. Below crash signature was observed - rpmsg_create_ept+0x40/0xa0 rpmsg_eptdev_open+0x88/0x138 chrdev_open+0xc4/0x1c8 do_dentry_open+0x230/0x378 vfs_open+0x3c/0x48 path_openat+0x93c/0xa78 do_filp_open+0x98/0x118 do_sys_openat2+0x90/0x220 do_sys_open+0x64/0x8c Signed-off-by: Deepak Kumar Singh --- drivers/rpmsg/rpmsg_ctrl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/rpmsg/rpmsg_ctrl.c b/drivers/rpmsg/rpmsg_ctrl.c index 107da70..4332538 100644 --- a/drivers/rpmsg/rpmsg_ctrl.c +++ b/drivers/rpmsg/rpmsg_ctrl.c @@ -194,10 +194,12 @@ static void rpmsg_ctrldev_remove(struct rpmsg_device *rpdev) struct rpmsg_ctrldev *ctrldev = dev_get_drvdata(&rpdev->dev); int ret; + mutex_lock(&ctrldev->ctrl_lock); /* Destroy all endpoints */ ret = device_for_each_child(&ctrldev->dev, NULL, rpmsg_chrdev_eptdev_destroy); if (ret) dev_warn(&rpdev->dev, "failed to nuke endpoints: %d\n", ret); + mutex_unlock(&ctrldev->ctrl_lock); cdev_device_del(&ctrldev->cdev, &ctrldev->dev); put_device(&ctrldev->dev);