Re: [PATCH 4/5] staging: nvec: fix pm_power_off teardown in tegra_nvec_remove()

From: Dan Carpenter

Date: Mon Apr 13 2026 - 06:34:36 EST


On Mon, Apr 13, 2026 at 03:02:50AM -0700, Alexandru Hossu wrote:
> On Mon, Apr 13, 2026, Dan Carpenter wrote:
> > At this point, we're unloading the driver so nvec_power_handle is
> > about to be freed. Is there any benefit to setting it to NULL?
>
> nvec_power_off() dereferences nvec_power_handle to send the power-off
> command to the EC. If pm_power_off somehow gets reassigned to
> nvec_power_off after our driver unloads (e.g. by a re-probe), the stale
> nvec_power_handle would point to freed memory.

I like to believe it's impossible to reprobe a driver before the
rmmod has completed. I'm not going to check on this, I'm just going
to take it on faith. :P

>
> Setting it to NULL makes the potential failure mode explicit rather than
> a silent use-after-free. Since we are already inside the if() guard,
> the cost is a single pointer store.

So the bug here is that we're racing an rmmod against a poweroff and we
trigger a bug. And the fix is to change the use after free bug into a
NULL dereference. Both of rmmod and poweroff are privileged operations
so you kind of get what you deserve if you do that.

I understand that it costs nothing to do the nvec_power_handle = NULL;
and if this were a new driver, I wouldn't comment on it. (Although I
know other people who would). But for a new patch, I'm just not sold
on this. It makes the patch more confusing for no benefit.

regards,
dan carpenter