rpmsg: glink: hold idr_lock while completing open_ack
Subsystems (2)
PATCH 1/1 rpmsg: glink: hold idr_lock while completing open_ack
qcom_glink_rx_open_ack() looks up the channel in lcids under idr_lock, drops the lock and then completes channel->open_ack. qcom_glink_create_local() waits for that completion for five seconds. If the wait times out at the same moment, it removes the lcid under idr_lock and drops the last two references, which frees the channel. The OPEN_ACK handler then reads channel->name for the trace event and calls complete_all() on freed memory.
Keep idr_lock held until complete_all() returns. The timeout path removes the lcid under the same lock before dropping its references, so the channel cannot be freed while the lock is held. Taking a channel reference instead would not work here, because the handler runs in interrupt context and the final put may call cancel_work_sync() from qcom_glink_channel_release().
---
Found with a static analysis tool we are developing.
Compile-tested only, with x86_64_defconfig plus MAILBOX, RPMSG_QCOM_GLINK_RPM and W=1.
drivers/rpmsg/qcom_glink_native.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/rpmsg/qcom_glink_native.c +2 -1
@@ -1101,16 +1101,17 @@ static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)spin_lock (&glink->idr_lock); channel =idr_find (&glink->lcids, lcid); -spin_unlock (&glink->idr_lock);trace_qcom_glink_cmd_open_ack_rx (glink->label, channel ? channel->name :NULL , lcid, channel ? channel->rcid :0 );if (!channel) { +spin_unlock (&glink->idr_lock);dev_err (glink->dev,"Invalid open ack packet\n" );return -EINVAL ; }complete_all (&channel->open_ack); +spin_unlock (&glink->idr_lock);return 0 ; }
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.43.0
Quoted 31 lines · Changyul Lee
qcom_glink_rx_open_ack()looks up the channel in lcids underidr_lock, drops the lock and then completeschannel->open_ack.qcom_glink_create_local()waits for that completion for five seconds. If the wait times out at the same moment, it removes the lcid underidr_lockand drops the last two references, which frees the channel. TheOPEN_ACKhandler then readschannel->namefor the trace event and callscomplete_all()on freed memory.Keep
idr_lockheld untilcomplete_all()returns. The timeout path removes the lcid under the same lock before dropping its references, so the channel cannot be freed while the lock is held. Taking a channel reference instead would not work here, because the handler runs in interrupt context and the final put may callcancel_work_sync()fromqcom_glink_channel_release().Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")Signed-off-by Changyul Lee <[email protected]>---
Found with a static analysis tool we are developing.Compile-tested only, with
x86_64_defconfigplus MAILBOX,RPMSG_QCOM_GLINK_RPMand W=1.drivers/rpmsg/qcom_glink_native.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/rpmsg/qcom_glink_native.c +0
-0
drivers/rpmsg/qcom_glink_native.c static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid) · line 1102
Quoted 2 lines
drivers/rpmsg/qcom_glink_native.c +0
-0@@ -1101,16 +1101,17 @@ static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)spin_lock (&glink->idr_lock);
This can become guard(spinlock)(&glink->idr_lock) and you can then drop the manual unlocks
Konrad