From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.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 2CF122D94AF for ; Wed, 3 Dec 2025 09:00:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764752455; cv=none; b=QL3wwiadSzlUBUoc3jwz11xer8CjWZBx36up6/cVDZSIS7tRUmpglLBy+DYtIiFeODp/LIqICBW/FMinh5gKPv8yqq600orULpz4m2Bz2PUbtA65yglY9+e6AQJExkpFqbaiqd1pkXok1heC9xBQxzIK59ulyIl1Eqze10McmDY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764752455; c=relaxed/simple; bh=e2W1kyT422lZsnApQ289V6vBlFIkvpuLVttKm4JWqcE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=b4RagLEWUqH9F6dcIujAEFKwvVZB9Zpg0ghgDJMcmYysQHjTMwWiGyN2lkz7OLiUfGLBcpJlJeDgfW/LxrbFOcjaqRlAR9F42Kd1Rf9mxtyhuF67Db5GYfXsa+nH8njwUhMOfsFwdT51XtytkZRHsFgUTT0zW9S0XZ6W4u4wD/0= 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=n7T7X+pn; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=TLA1OENm; arc=none smtp.client-ip=205.220.168.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="n7T7X+pn"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="TLA1OENm" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5B381scE404754 for ; Wed, 3 Dec 2025 09:00:52 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= 9o+5hmQIQFM/sIVASo8uRQ+mODl3UI3mwXQQ6VLH63o=; b=n7T7X+pnJwX1X3VN y1ZJJEIGmFRBx70zhkjo5MzRvooWhp3ei1RNYHMRUBLB015MyvVSx6FdkF6z0aXm ieMzZdzUQqJPYAbRjadHKj/x4phVN15lwtKANoYO/8J4XLl1Z9YfxYsj7MgeZpbY /JlpaBjPiYmZaB87QY3R7z8uwcasxLnv4Ohy+qR5Fml50WAyoGt/0B1R4ogicUm0 kiXe+1oQgeZz6Wu13kmwMNl/IXEYGadwdKJc2gNy20f/NvjWwm1UEKLmz/CdomX0 QSWXHI41Zsn8kgax8tq1XQ+qtNUE7Y7z59NBsviXnUTvD7f9wAJX+vTbt0gtfhe3 BUVxIw== Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4at9fc9hbh-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Wed, 03 Dec 2025 09:00:52 +0000 (GMT) Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-29557f43d56so72312765ad.3 for ; Wed, 03 Dec 2025 01:00:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1764752451; x=1765357251; 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=9o+5hmQIQFM/sIVASo8uRQ+mODl3UI3mwXQQ6VLH63o=; b=TLA1OENmiUQ631LwuyeeerNXZPaiLIr3Nt2rbNk8qfbjNI8OSTnmLZX3EHAm+1frkl FamN8w5BPPs0xBBkWtTRp3hTX+S2fyMQgY6heC96cyKP+0EXG73tTBhEv11v4NQDnDZR cyiRr8+XBCIngHaqK65Mu0hF5prYnM/6i7JLMap+pc9X6prS7CCQMa6sh43hLQAuguUe M/3keFAcKNWdnQwebELe0JoL8fEdZaqZfwK0ai6Wvpa2cB+WaXRYbisAvV1ETk4e4A7l 0UKXiQd2eieVG7QZ1omTQhoUldnBeyEwZ6pmzJWekYp+HGOhGn6OM4J7pQmxJa12jF1p fWYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764752451; x=1765357251; 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=9o+5hmQIQFM/sIVASo8uRQ+mODl3UI3mwXQQ6VLH63o=; b=mWM+12Z4X9ctg6fh8FQTjmZ+6vzyRtSYIuJP4QVuE2ApL7zqEopKko8zFlRmxZqdj3 XQQ7BTD3ihc2M0hZdTWuaYDD+ZAY1LEHOUsBA72T7h0kZ/+v+K/DGLeO4aLOdU6xqnTi OvrzCpebqPBmZBjGQYTuOGyTnmsCd0s7s7Re2M9qiGdM9Lcz37SYUvogpcRaSVWTPGdH /mGhULi9yXY3Ju2vOxc72GEQciKybYtqsUNE6Z7VX+h6gaUacmSu2xOIcKJ83ELPLycf wStafyt6WUHl8SVPBrijrCTsgk26Rj+0cMuAl1+Gfh91hqttnGPBis2nmNKgAd+i3PIf eHqg== X-Forwarded-Encrypted: i=1; AJvYcCXCaqU867vCirgz/YUKc5mYQeSbPmXCEtKbEFQoDAFykFMEB/z3ZukD6/OMhb5KefpRhsciEbI0t1S01XQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwyNz+f/OdE43ZD/C4HdgkYFgV7PyijP3B8oQBO8FPPzAmkoEdh q2A/YQr9R14z0rlBgjUwQ5CmGhZempf9b+BooYjOHXNZWlP27TvBYoU5kZtLhn73VMckBLfQjls hoF5sM93MnuZu9nPbZDMpBlWLBQPBAQzzaY4Kq3pVNQDy5Gs20aYHE9R/wxEOp4WNc7w= X-Gm-Gg: ASbGnctyY021C23VaSPwc3T/nMutDe0FccRxwdrgXWQldiyeRpP7KA0m44RadohTsip tjcF66bsf4j3w6WLNJRIAvHBt9ckmxiQG0SHAGXAqo0YonLXao5cN/VMWvjtCHTJ7DFsJ0A+URC tnnVbohI6CRGOu1ZmuclAgKfus7QisKpGUETaZ+HHbSti8RohtRjhInG5wgJdVsKaKQRhTcwgJS 5jL7V65ysflAGgB7Toyf7vlGxNsbYPdiH8/CH0AlV/2X/nPBUTfnOS7LUSF3UZPxsCPNNJNOpNZ QabgJsOaMhbZaxAygh+dg01q4uIhWPj8r7jsO2b40isZsLDJBmcM2vQeKbyrl5g1lBuHZ7awDrR PhzmZbVUtVxEX3CXj3HYHva8WoiZVdOlQg3/FSgDvlqwH0b53QU7akktWPjxLycpzWLDkLGV280 hYC52lgg== X-Received: by 2002:a17:902:d586:b0:295:2c8e:8e56 with SMTP id d9443c01a7336-29d683f3ec6mr18426385ad.44.1764752451395; Wed, 03 Dec 2025 01:00:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IH4B9P4/ChOKVOH4A1ZFkwuY6wW2UquGMcGNeJ7UjIkbYf/foEWfC18XZboOhL4IHQyYkuD2A== X-Received: by 2002:a17:902:d586:b0:295:2c8e:8e56 with SMTP id d9443c01a7336-29d683f3ec6mr18425955ad.44.1764752450686; Wed, 03 Dec 2025 01:00:50 -0800 (PST) Received: from [10.133.33.183] (tpe-colo-wan-fw-bordernet.qualcomm.com. [103.229.16.4]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29bceb4af8bsm177932185ad.81.2025.12.03.01.00.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Dec 2025 01:00:50 -0800 (PST) Message-ID: Date: Wed, 3 Dec 2025 17:00:46 +0800 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] rpmsg: core: fix race in driver_override_show() and use core helper To: Gui-Dong Han , andersson@kernel.org, mathieu.poirier@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, zhongqiu.han@oss.qualcomm.com References: <20251202174948.12693-1-hanguidong02@gmail.com> Content-Language: en-US From: Zhongqiu Han In-Reply-To: <20251202174948.12693-1-hanguidong02@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMjAzMDA3MCBTYWx0ZWRfXxiz3qdTR8vIY iluaxxlM16M1tJrcSpChTe/5wEqqqr8FuANRfsyq0v3OSGuAVVxbKoIVQak7ekDObd7bgsDrOHa haGCCSW74AzaeVX2aF2tdsG9xf0qOP49oYDN4suKp8I87LWxBIKYWvtZq7tGKYMoAcqJsw+J1yo Xq5GdVFwxQWf2lL9AZoefrjwCCtyIRDDlcId0kC2GanIQACXqLYwhsoJBUALDKnNTGeYKCEGQCU qGaxDdY3rxE9NksJhoeFH8veqyn6PEPYkWLiV413oHg/8oKi1zZk7O7EVKLcaqDdoLGvOdjfb6j vGVtZ3iJ07GppHuDFqJoryPZ4uKHEp6f4i1r7mOXZA5m8qW3+eASyKc4yxLvpdt0CR1ZM1CD9v4 iiJahceNxUINLCZ9p2sfH+Y7a6nONg== X-Authority-Analysis: v=2.4 cv=Qblrf8bv c=1 sm=1 tr=0 ts=692ffc44 cx=c_pps a=IZJwPbhc+fLeJZngyXXI0A==:117 a=nuhDOHQX5FNHPW3J6Bj6AA==:17 a=IkcTkHD0fZMA:10 a=wP3pNCr1ah4A:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=VwQbUJbxAAAA:8 a=pGLkceISAAAA:8 a=EUspDBNiAAAA:8 a=YfPkN0mvrXhG1xwvAmMA:9 a=QEXdDO2ut3YA:10 a=uG9DUKGECoFWVXl0Dc02:22 X-Proofpoint-GUID: 7MYSsezd7ZOUBaNHU84drJ9-sILBXnmY X-Proofpoint-ORIG-GUID: 7MYSsezd7ZOUBaNHU84drJ9-sILBXnmY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-12-02_01,2025-11-27_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 bulkscore=0 spamscore=0 lowpriorityscore=0 impostorscore=0 priorityscore=1501 adultscore=0 phishscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2510240001 definitions=main-2512030070 On 12/3/2025 1:49 AM, Gui-Dong Han wrote: > The driver_override_show function reads the driver_override string > without holding the device_lock. However, the store function modifies > and frees the string while holding the device_lock. This creates a race > condition where the string can be freed by the store function while > being read by the show function, leading to a use-after-free. > > To fix this, replace the rpmsg_string_attr macro with explicit show and > store functions. The new driver_override_store uses the standard > driver_set_override helper. Since the introduction of > driver_set_override, the comments in include/linux/rpmsg.h have stated > that this helper must be used to set or clear driver_override, but the > implementation was not updated until now. > > Because driver_set_override modifies and frees the string while holding > the device_lock, the new driver_override_show now correctly holds the > device_lock during the read operation to prevent the race. > > Additionally, since rpmsg_string_attr has only ever been used for > driver_override, removing the macro simplifies the code. > > Fixes: 39e47767ec9b ("rpmsg: Add driver_override device attribute for rpmsg_device") > Cc: stable@vger.kernel.org > Signed-off-by: Gui-Dong Han Reviewed-by: Zhongqiu Han > --- > I verified this with a stress test that continuously writes/reads the > attribute. It triggered KASAN and leaked bytes like a0 f4 81 9f a3 ff ff > (likely kernel pointers). Since driver_override is world-readable (0644), > this allows unprivileged users to leak kernel pointers and bypass KASLR. > Similar races were fixed in other buses (e.g., commits 9561475db680 and > 91d44c1afc61). Currently, 9 of 11 buses handle this correctly; this patch > fixes one of the remaining two. > --- > drivers/rpmsg/rpmsg_core.c | 66 ++++++++++++++++---------------------- > 1 file changed, 27 insertions(+), 39 deletions(-) > > diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c > index 5d661681a9b6..96964745065b 100644 > --- a/drivers/rpmsg/rpmsg_core.c > +++ b/drivers/rpmsg/rpmsg_core.c > @@ -352,50 +352,38 @@ field##_show(struct device *dev, \ > } \ > static DEVICE_ATTR_RO(field); > > -#define rpmsg_string_attr(field, member) \ > -static ssize_t \ > -field##_store(struct device *dev, struct device_attribute *attr, \ > - const char *buf, size_t sz) \ > -{ \ > - struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ > - const char *old; \ > - char *new; \ > - \ > - new = kstrndup(buf, sz, GFP_KERNEL); \ > - if (!new) \ > - return -ENOMEM; \ > - new[strcspn(new, "\n")] = '\0'; \ > - \ > - device_lock(dev); \ > - old = rpdev->member; \ > - if (strlen(new)) { \ > - rpdev->member = new; \ > - } else { \ > - kfree(new); \ > - rpdev->member = NULL; \ > - } \ > - device_unlock(dev); \ > - \ > - kfree(old); \ > - \ > - return sz; \ > -} \ > -static ssize_t \ > -field##_show(struct device *dev, \ > - struct device_attribute *attr, char *buf) \ > -{ \ > - struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ > - \ > - return sprintf(buf, "%s\n", rpdev->member); \ > -} \ > -static DEVICE_ATTR_RW(field) > - > /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */ > rpmsg_show_attr(name, id.name, "%s\n"); > rpmsg_show_attr(src, src, "0x%x\n"); > rpmsg_show_attr(dst, dst, "0x%x\n"); > rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n"); > -rpmsg_string_attr(driver_override, driver_override); > + > +static ssize_t driver_override_store(struct device *dev, > + struct device_attribute *attr, > + const char *buf, size_t count) > +{ > + struct rpmsg_device *rpdev = to_rpmsg_device(dev); > + int ret; > + > + ret = driver_set_override(dev, &rpdev->driver_override, buf, count); > + if (ret) > + return ret; > + > + return count; > +} > + > +static ssize_t driver_override_show(struct device *dev, > + struct device_attribute *attr, char *buf) > +{ > + struct rpmsg_device *rpdev = to_rpmsg_device(dev); > + ssize_t len; > + > + device_lock(dev); > + len = sysfs_emit(buf, "%s\n", rpdev->driver_override); > + device_unlock(dev); > + return len; > +} > +static DEVICE_ATTR_RW(driver_override); > > static ssize_t modalias_show(struct device *dev, > struct device_attribute *attr, char *buf) -- Thx and BRs, Zhongqiu Han