Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Vishnu Reddy <busanna.reddy@oss.qualcomm.com>
To: bod@kernel.org
Cc: Vikash Garodia <vikash.garodia@oss.qualcomm.com>,
	Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>,
	Abhinav Kumar <abhinav.kumar@linux.dev>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Hans Verkuil <hverkuil@kernel.org>,
	Stefan Schmidt <stefan.schmidt@linaro.org>,
	Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>,
	Hungyu Lin <dennylin0707@gmail.com>,
	linux-media@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Bryan O'Donoghue <bryan.odonoghue@linaro.org>,
	sashiko-bot <sashiko-bot@kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH v3 3/4] media: iris: Fix use-after-free on unbind with open sessions
Date: Wed, 2 Dec 2026 14:45:33 +0530	[thread overview]
Message-ID: <bc61ef6f-c8cb-3a4b-9dfb-2b86a259610f@oss.qualcomm.com> (raw)
In-Reply-To: <179604427888.1329975.5206765366800243564.b4-reply@b4>


On 11/30/2026 6:41 PM, bod@kernel.org wrote:
> On 2026-11-19 17:15 +0530, Vishnu Reddy wrote:
>> The driver can be unbound through sysfs while a client has a video
>> session open. The core is devm allocated and freed when remove()
>> returns, so the open sessions then use freed memory.
>>
>> Only the core structure needs to outlive remove(), because it is the
>> only thing the file operations of an open session still use. Allocate
>> it with kzalloc and tie it to the reference count of its v4l2_device,
>> which every registered video device holds. It is freed when the last
>> file descriptor is closed. The other devm resources (register mapping,
>> response packet, clocks, power domains, interconnects) are only used by
>> the power on/off sequence, the clock and bandwidth votes and the
>> interrupt handler. All of them are released with the unbind, after the
>> core is shut down.
>>
>> To make sure nothing uses them afterwards, set a removing flag at the
>> start of iris_remove(). It makes core init, new sessions, command
>> writes and the clock and interconnect votes fail with -ENODEV, so a
>> client still holding a file descriptor cannot power the hardware on or
>> vote for resources that are gone. A client waiting for the firmware
>> only times out and returns an error without touching them. Runtime PM
>> is also disabled explicitly in iris_remove() instead of with devm,
>> because its callbacks use the core.
>>
>> Callers that hold a runtime PM reference, such as iris_open(),
>> iris_hfi_queue_cmd_write() and iris_scale_power(), can still be running
>> when remove() starts, as video_unregister_device() does not wait for
>> them. Take an rw_semaphore for read around the reference and wait for
>> it in iris_remove(), so that the references are dropped before runtime
>> PM is disabled and the unbind releases the resources of the device.
>>
>> Fixes: 38506cb7e8d2 ("media: iris: add platform driver for iris video device")
>> Reported-by: sashiko-bot <sashiko-bot@kernel.org>
>> Closes: https://lore.kernel.org/all/20260926064921.976721F000FF@smtp.kernel.org
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Vishnu Reddy <busanna.reddy@oss.qualcomm.com>
> A patch this size reworking object lifetimes ~ 102 insertions can't really
> be presented to stable. This is a rearch.

Will drop stable.

>>  drivers/media/platform/qcom/iris/iris_core.c      |  5 +++
>>  drivers/media/platform/qcom/iris/iris_core.h      |  4 ++
>>  drivers/media/platform/qcom/iris/iris_hfi_queue.c | 18 ++++++++-
>>  drivers/media/platform/qcom/iris/iris_power.c     | 24 +++++++++++-
>>  drivers/media/platform/qcom/iris/iris_probe.c     | 48 ++++++++++++++++++-----
>>  drivers/media/platform/qcom/iris/iris_vidc.c      | 15 ++++++-
>>  6 files changed, 102 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/media/platform/qcom/iris/iris_core.c b/drivers/media/platform/qcom/iris/iris_core.c
>> index d558475986ff0..9c0a9a2e4be07 100644
>> --- a/drivers/media/platform/qcom/iris/iris_core.c
>> +++ b/drivers/media/platform/qcom/iris/iris_core.c
>> @@ -56,6 +56,11 @@ int iris_core_init(struct iris_core *core)
>>  	int ret;
>>  
>>  	mutex_lock(&core->lock);
>> +	if (core->removing) {
>> +		ret = -ENODEV;
>> +		goto exit;
>> +	}
>> +
> This flag stuff in the code - another flag on top of an existing state
> screams hack.
>
> If there is an interdependence between allocations we roll by hand and
> allocations we let devm take care of then having to flag that to the driver
> ane expecting to maintain that flag long term is asking for bugs.
>
> To be honest, I'm already dubious about core->state so I just don't get why
> we need another meta state on top of our existing state.

core->state is the firmware state, not the device lifetime. After
iris_core_deinit() sets DEINIT, the next open() calls iris_core_init() and
boots the firmware again. Without the removing flag, an open racing with
remove() would re-init the core after it has been torn down. The removing
flag prevents that.

> I think this needs to be looked at again. Are we manually allocating or
> devm'ing.
>
> Commit to one and the code will flow naturally from the design. Having two
> methods is more complexity to think about; having one method reduces the
> bugsurface.
>
> Is the thing to do here to drop the devm allocations and frees so you can
> fully control what you are taking partial control of with the hand
> allocations ?

struct iris_core is the only hand allocation, because open file handles
keep using it after remove(). devm frees at unbind, so it cannot be devm.
Everything else is devm and is not used after remove(), since remove()
stops all users first.

Making everything a hand allocation would not change this. Running sessions
must still not go wrong, and in-flight opens must still be rejected. Simply
allocating in probe and freeing in remove() does not handle either case.
core->removing is what makes both paths fail safely once remove() has started.

> The LLM is babbling at me about specific things that go wrong in this code,
> rather than post that verbatim here, I think the overall conclusion is -
> don't mix and match - commit to one strategy.
>
> If devm can't support unbind, then either drop user-space initiated unbind
> or drop devm.

As explained above, only struct iris_core outlives remove(), and it is
refcounted. Dropping devm would not fix anything.

Setting suppress_bind_attrs to true would block user-space initiated unbind,
but as discussed in v1, it hides the problem instead of fixing remove(). Unbind
is generic driver-core behaviour, and remove() should be safe whenever it is
called.

> ---
> bod
>
>

  reply	other threads:[~2026-12-02  9:15 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 11:45 [PATCH v3 0/4] media: iris: Fix issues in the driver unbind path Vishnu Reddy
2026-11-19 11:45 ` [PATCH v3 1/4] media: iris: Fix runtime PM reference imbalance on failed resume Vishnu Reddy
2026-11-25 10:18   ` Konrad Dybcio
2026-11-19 11:45 ` [PATCH v3 2/4] media: iris: Disable the system error work on removal Vishnu Reddy
2026-11-30 12:51   ` bod
2026-12-02  9:15     ` Vishnu Reddy
2026-11-19 11:45 ` [PATCH v3 3/4] media: iris: Fix use-after-free on unbind with open sessions Vishnu Reddy
2026-11-27 12:28   ` Vikash Garodia
2026-11-30 13:11   ` bod
2026-12-02  9:15     ` Vishnu Reddy [this message]
2026-11-19 11:45 ` [PATCH v3 4/4] media: iris: Wake up blocked clients when the driver is removed Vishnu Reddy
2026-11-27 12:29   ` Vikash Garodia
2026-11-30 14:27   ` bod
2026-12-02  9:15     ` Vishnu Reddy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc61ef6f-c8cb-3a4b-9dfb-2b86a259610f@oss.qualcomm.com \
    --to=busanna.reddy@oss.qualcomm.com \
    --cc=abhinav.kumar@linux.dev \
    --cc=bod@kernel.org \
    --cc=bryan.odonoghue@linaro.org \
    --cc=dennylin0707@gmail.com \
    --cc=dikshita.agarwal@oss.qualcomm.com \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=hverkuil@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=stefan.schmidt@linaro.org \
    --cc=vikash.garodia@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox