drm/nouveau: fix list cursor use after loop in the clk pstate paths
Subsystems (3)
v2 cover drm/nouveau: fix list cursor use after loop in the clk pstate paths
This series deals with the three places in the nouveau clk pstate code where the list_for_each_entry() cursor is used after the loop, plus one unrelated fix. They are exactly the three sites Dan Carpenter listed in 2022; his patch for the first one was never applied and the bug is still present in drm-misc-next.
Only patch 1 fixes a bug that can actually be triggered:nvkm_clk_ustate_update() takes an arbitrary pstate id from the user and never checks that a matching entry exists, so if it does not the cursor ends up pointing at the list head and the code reads past it. It carries a Fixes: tag and the credit for spotting it goes to Dan.
Patches 2 and 3 fix the same pattern in nvkm_pstate_prog() and nvkm_control_mthd_pstate_attr(), but neither is triggerable as the code
stands: the callers of nvkm_pstate_prog() clamp the index against clk->state_nr first, and nvkm_control_mthd_pstate_attr() already rejects args->v0.state >= clk->state_nr before the loop. Both are hardening, not bug fixes, and they carry no Fixes: tag on purpose. The point is to stop the two functions from being correct only by virtue of what their callers do.
Patch 4 is unrelated and is a real bug, though a modest one:nvkm_cstate_prog() overwrites the reclock status in 'ret' with the status of the voltage/fan restore calls it makes afterwards. The only consumer of the return value is an error message in nvkm_pstate_work(), so the observable effect is that a failing reclock is never reported in dmesg.
Compile-tested only. All the affected paths are reachable only by root, through the 'pstate' debugfs file, so I could not exercise them in any other way.
v2: v1 (2 patches) was sent on 2026-07-11 with a mail client that mangled the patch text, so it did not apply; it is resent here with git send-email. This version also adds the two remaining sites from Dan's list, and the
Fixes: tags to the two real fixes.
Link to v1:
https://lore.kernel.org/nouveau/CAHwn+vnw=DD=j7umFVdcr8-F6yUz=iM+3yfA+HcHKXJwpNrHRA@mail.gmail.com/
Francesco Magazzu (4):
drm/nouveau/clk: fix list cursor use after loop in
nvkm_clk_ustate_update
drm/nouveau/clk: don't use the pstate cursor after the loop drm/nouveau/device: don't use the pstate cursor after the loop drm/nouveau/clk: don't clobber reclock status when restoring volt/fan
.../gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 8 ++++- .../gpu/drm/nouveau/nvkm/subdev/clk/base.c | 32 +++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-)
base-commit: a284476db2653ae893f46cbea408eb412db54eb0
Signature · 1 line
2.55.0
PATCH v2 1/4 drm/nouveau/clk: fix list cursor use after loop in nvkm_clk_ustate_update
If the requested pstate id is not present in the state list (or the list is empty, e.g. broken/missing perf tables), the list_for_each_entry cursor runs off the end of the list and the subsequent pstate->pstate != req check dereferences the list head cast to a struct nvkm_pstate, which is an out-of-bounds read.
Track whether the entry was actually found instead of inspecting the cursor after the loop.
---
Note: essentially the same fix was posted by Dan Carpenter in 2022 and never picked up; the bug is still present in drm-misc-next. Credit for spotting it goes to him.
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +6 -2
@@ -479,13 +479,17 @@ nvkm_clk_ustate_update(struct nvkm_clk *clk, int req)return -ENOSYS ;if (req !=-1 && req !=-2 ) { +bool found = false; +list_for_each_entry (pstate, &clk->states, head) { -if (pstate->pstate == req) +if (pstate->pstate == req) { + found = true;break ; + } i++; } -if (pstate->pstate != req) +if (!found)return -EINVAL ; req = i; }
Signature · 1 line
2.55.0
[email protected]
maintainerRe: [PATCH v2 1/4] drm/nouveau/clk: fix list cursor use after loop in nvkm_clk_ustate_update
This patch looks fine to me, but but if this was posted by Dan Carpenter originally would you mind switching the authorship over to their name?
Quoted 50 lines · Francesco Magazzu
If the requested pstate id is not present in the state list (or the list is empty, e.g. broken/missing perf tables), the
list_for_each_entry
cursor runs off the end of the list and the subsequentpstate->pstate!= req check dereferences the list head cast to a structnvkm_pstate, which is an out-of-bounds read.Track whether the entry was actually found instead of inspecting the cursor after the loop.
Fixes 7c8565220697 ("drm/nouveau/clk: implement power state andengine clock control in core")
Signed-off-by Francesco Magazzu <[email protected]>---
Note: essentially the same fix was posted by Dan Carpenter in 2022 and
never picked up; the bug is still present in drm-misc-next. Credit for
spotting it goes to him.drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-0@@ -479,13 +479,17 @@ nvkm_clk_ustate_update(struct nvkm_clk *clk, int req) return -ENOSYS;
if (req != -1 && req != -2) {drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +2
-0+bool found = false; +
list_for_each_entry(pstate, &clk->states, head){drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +2
-1-if (pstate->pstate == req) +if (pstate->pstate == req) { + found = true;break;
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +1
-0+ }i++;
}drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +1
-1-if (pstate->pstate != req) +if (!found)return
-EINVAL;
req = i;
}
PATCH v2 2/4 drm/nouveau/clk: don't use the pstate cursor after the loop
In reply to Francesco Magazzunvkm_pstate_prog() walks clk->states looking for the entry at index 'pstatei' and then keeps using the list_for_each_entry cursor after the loop. This is not triggerable today: every caller clamps the index against clk->state_nr before calling, so the loop always breaks on a real entry. It is safe by virtue of what the callers happen to do, not by anything the function itself checks.
Should a caller ever pass an index that is not on the list, the cursor would point at the list head rather than at a pstate, and the pstate->base.domain[] and pstate->fanspeed accesses that follow would read past it. Rather than leave that trap in place for the next caller, track whether the entry was found and return -EINVAL if it was not.
No functional change.
--- drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +7 -1
@@ -270,13 +270,19 @@ nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)struct nvkm_fb *fb = subdev->device->fb;struct nvkm_pci *pci = subdev->device->pci;struct nvkm_pstate *pstate; +bool found = false;int ret, idx =0 ;list_for_each_entry (pstate, &clk->states, head) { -if (idx++ == pstatei) +if (idx++ == pstatei) { + found = true;break ; + } } +if (!found) +return -EINVAL ; +nvkm_debug (subdev,"setting performance state %d\n" , pstatei); clk->pstate = pstatei;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v2 2/4] drm/nouveau/clk: don't use the pstate cursor after the loop
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Clobbered Hardware Clock Programming Error in nvkm_cstate_prog
- [High] State Tracking Desync in nvkm_pstate_prog
- [High] Ignored RAM Clock Programming Error in nvkm_pstate_prog --
--- Patch [2]: [PATCH v2 2/4] drm/nouveau/clk: don't use the pstate cursor after the loop --- commit 9e641d48c614d5036ae19e8053df44c4f5085e05
Author: Francesco Magazzu <[email protected]>
drm/nouveau/clk: don't use the pstate cursor after the loop
nvkm_pstate_prog() walks clk->states looking for an entry and uses the list_for_each_entry cursor after the loop. Although current callers clamp the index to prevent out-of-bounds access, this leaves a trap for future callers. To fix this, the code now tracks whether the entry was found and returns -EINVAL if it was not.
Quoted 4 lines
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-0
[ ... ]
Quoted 1 line
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-0@@ -270,13 +270,19 @@ nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)
[ ... ]
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei) · line 287
Quoted 5 lines
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +3
-0+if (!found) +return -EINVAL ; +nvkm_debug (subdev,"setting performance state %d\n" , pstatei); clk->pstate = pstatei;
[Severity: High]
This isn't a bug introduced by this patch, but could this prematurely update the software state tracker before hardware programming has completed and been verified?
If a hardware failure occurs during nvkm_cstate_prog() or RAM clock programming, the transition fails.
Because clk->pstate is updated here before hardware programming finishes, a failure leaves the hardware out of sync with software.
Future requests for this state might be incorrectly skipped because the software evaluates pstate != clk->pstate to false, leaving the hardware stuck in an inconsistent state.
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c:nvkm_pstate_prog() {
...
do {
ret = ram->func->calc(ram, khz);
if (ret == 0)
ret = ram->func->prog(ram);
} while (ret > 0);
ram->func->tidy(ram);
}
return nvkm_cstate_prog(clk, pstate, NVKM_CLK_CSTATE_HIGHEST);
}[Severity: High]
This is also a pre-existing issue, but does nvkm_pstate_prog() silently discard the RAM clock programming error?
If ram->func->calc() or ram->func->prog() fails and returns a negative error code (like -EINVAL or a hardware timeout), the error stored in 'ret' is discarded when the loop exits.
The function then proceeds to unconditionally return the result of nvkm_cstate_prog().
This decoupling of memory and core clocks could lead to hardware lockups or severe screen corruption due to mismatched frequencies.
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c:nvkm_cstate_prog() {
...
ret = clk->func->calc(clk, cstate);
if (ret == 0) {
ret = clk->func->prog(clk);
clk->func->tidy(clk);
}
if (volt) {
ret = nvkm_volt_set_id(volt, cstate->voltage,
pstate->base.voltage, clk->temp, -1);
...
}[Severity: High]
This is another pre-existing issue, but is the hardware clock programming error clobbered in nvkm_cstate_prog()?
If clk->func->calc() or clk->func->prog() fails and returns a negative error code, the 'ret' variable containing the error is unconditionally overwritten by the return value of nvkm_volt_set_id() (which will be 0 on success).
This causes the transition to report success despite a hardware failure, masking the error and corrupting higher-level state tracking.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/4] drm/nouveau/clk: don't use the pstate cursor after the loop
In reply to Francesco MagazzuQuoted 53 lines · Francesco Magazzu
nvkm_pstate_prog()walksclk->stateslooking for the entry at index 'pstatei' and then keeps using thelist_for_each_entrycursor after the
loop. This is not triggerable today: every caller clamps the index againstclk->state_nrbefore calling, so the loop always breaks on a real
entry. It is safe by virtue of what the callers happen to do, not by anything the function itself checks.Should a caller ever pass an index that is not on the list, the cursor
would point at the list head rather than at a pstate, and the pstate->base.domain[] andpstate->fanspeedaccesses that follow would read
past it. Rather than leave that trap in place for the next caller, track
whether the entry was found and return-EINVALif it was not.No functional change.
Signed-off-by Francesco Magazzu <[email protected]>---
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +1
-0@@ -270,13 +270,19 @@ nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei) struct nvkm_fb *fb = subdev->device->fb; struct nvkm_pci *pci = subdev->device->pci; struct nvkm_pstate *pstate; +bool found = false; int ret, idx = 0;
list_for_each_entry(pstate, &clk->states, head){drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +2
-1-if (idx++ == pstatei) +if (idx++ == pstatei) { + found = true;break;
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +1
-0+ }}
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +3
-0+if (!found) +return -EINVAL ; +
nvkm_debug(subdev, "setting performance state %d\n", pstatei);
clk->pstate= pstatei;
PATCH v2 3/4 drm/nouveau/device: don't use the pstate cursor after the loop
In reply to Francesco Magazzunvkm_control_mthd_pstate_attr() looks up the pstate at the index supplied by userspace by walking clk->states, and then keeps using the list_for_each_entry cursor after the loop. This is not triggerable today:
the function already rejects args->v0.state >= clk->state_nr before the loop, and clk->state_nr is kept in sync with the number of entries on clk->states, so the lookup always breaks on a real entry.
Should the loop ever run to completion, the cursor would point at the list head rather than at a pstate, and the pstate->base.domain[] read and the walk of pstate->list that follow would read past it. Rather than leave that trap in place, track whether the entry was found and return -EINVAL if it was not, like the other lookup failures in this function.
No functional change.
--- drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +7 -1
@@ -74,6 +74,7 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)const struct nvkm_domain *domain;struct nvkm_pstate *pstate;struct nvkm_cstate *cstate; +bool found = false;int i =0 , j =-1 ;u32 lo, hi;int ret = -ENOSYS ;
@@ -104,10 +105,15 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)if (args->v0.state !=NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT ) {list_for_each_entry (pstate, &clk->states, head) { -if (i++ == args->v0.state) +if (i++ == args->v0.state) { + found = true;break ; + } } +if (!found) +return -EINVAL ; + lo = pstate->base.domain[domain->name]; hi = lo;list_for_each_entry (cstate, &pstate->list, head) {
Signature · 1 line
2.55.0
Re: [PATCH v2 3/4] drm/nouveau/device: don't use the pstate cursor after the loop
Quoted 60 lines · Francesco Magazzu
nvkm_control_mthd_pstate_attr()looks up the pstate at the index supplied
by userspace by walkingclk->states, and then keeps using thelist_for_each_entrycursor after the loop. This is not triggerable today:
the function already rejectsargs->v0.state>=clk->state_nrbefore the
loop, andclk->state_nris kept in sync with the number of entries onclk->states, so the lookup always breaks on a real entry.Should the loop ever run to completion, the cursor would point at the list
head rather than at a pstate, and the pstate->base.domain[] read and the
walk ofpstate->listthat follow would read past it. Rather than leave
that trap in place, track whether the entry was found and return - EINVAL if
it was not, like the other lookup failures in this function.No functional change.
Signed-off-by Francesco Magazzu <[email protected]>---
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +1
-0@@ -74,6 +74,7 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size) const struct nvkm_domain *domain; struct nvkm_pstate *pstate; struct nvkm_cstate *cstate; +bool found = false; int i = 0, j = -1; u32 lo, hi;int ret =
-ENOSYS;drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +0
-0@@ -104,10 +105,15 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
if (args->v0.state!=NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {list_for_each_entry(pstate, &clk->states, head){drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +2
-1-if (i++ == args->v0.state) +if (i++ == args->v0.state) { + found = true;break;
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +1
-0+ }}
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +3
-0+if (!found) +return -EINVAL ; +lo = pstate->base.domain[domain->name]; hi = lo;
list_for_each_entry(cstate, &pstate->list, head){
PATCH v2 4/4 drm/nouveau/clk: don't clobber reclock status when restoring volt/fan
In reply to Francesco Magazzunvkm_cstate_prog() reuses 'ret' for the voltage and fan-speed restore calls it makes after reprogramming the clocks. Those calls almost always succeed, so the status of the reclock itself is overwritten and the function reports success even when clk->func->calc() or clk->func->prog() failed. The converse is also true: a successful reclock is reported as an error if the final restore call fails, even though that failure is only logged and otherwise ignored.
The only consumer of the return value is the error message in nvkm_pstate_work(), so in practice a failing reclock is simply never reported. Nothing else changes, but a function that returns success on failure is a trap for the next caller.
Keep the calc/prog status in 'ret' and use a separate local for the restore calls.
--- drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +9 -7
@@ -199,16 +199,18 @@ nvkm_cstate_prog(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei) }if (volt) { - ret =nvkm_volt_set_id (volt, cstate->voltage, - pstate->base.voltage, clk->temp,-1 ); -if (ret && ret != -ENODEV ) -nvkm_error (subdev,"failed to lower voltage: %d\n" , ret); +int err =nvkm_volt_set_id (volt, cstate->voltage, + pstate->base.voltage, clk->temp,-1 ); + +if (err && err != -ENODEV ) +nvkm_error (subdev,"failed to lower voltage: %d\n" , err); }if (therm) { - ret =nvkm_therm_cstate (therm, pstate->fanspeed,-1 ); -if (ret && ret != -ENODEV ) -nvkm_error (subdev,"failed to lower fan speed: %d\n" , ret); +int err =nvkm_therm_cstate (therm, pstate->fanspeed,-1 ); + +if (err && err != -ENODEV ) +nvkm_error (subdev,"failed to lower fan speed: %d\n" , err); }return ret;
Signature · 1 line
2.55.0
Re: [PATCH v2 4/4] drm/nouveau/clk: don't clobber reclock status when restoring volt/fan
Quoted 67 lines · Francesco Magazzu
nvkm_cstate_prog()reuses 'ret' for the voltage and fan-speed restore calls it makes after reprogramming the clocks. Those calls almost always
succeed, so the status of the reclock itself is overwritten and the function reports success even whenclk->func->calc()or clk->func-Quoted 1 line
prog()failed. The converse is also true: a successful reclock is reported as an
error if the final restore call fails, even though that failure is only
logged and otherwise ignored.The only consumer of the return value is the error message in
nvkm_pstate_work(), so in practice a failing reclock is simply never reported. Nothing else changes, but a function that returns success on
failure is a trap for the next caller.Keep the calc/prog status in 'ret' and use a separate local for the restore calls.
Fixes 3eca809b3c05 ("drm/nouveau/clk: cosmetic changes")Signed-off-by Francesco Magazzu <[email protected]>---
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c | 16 +++++++++-------1 file changed, 9 insertions(+), 7 deletions(-)drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-0@@ -199,16 +199,18 @@ nvkm_cstate_prog(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei) }
if (volt) {drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-2- ret = nvkm_volt_set_id(volt, cstate->voltage, - pstate->base.voltage, clk-Quoted 1 line
temp, -1);
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-2-if (ret && ret != -ENODEV ) - nvkm_error(subdev, "failed to lower voltage:%d\n", ret);
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +2
-0+ int err = nvkm_volt_set_id(volt, cstate->voltage, + pstate->base.voltage,
clk->temp, -1);drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +3
-0+ +if (err && err != -ENODEV ) + nvkm_error(subdev, "failed to lower voltage:%d\n", err);
}
if (therm) {drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-1- ret = nvkm_therm_cstate(therm, pstate->fanspeed, -1);
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +0
-2-if (ret && ret != -ENODEV ) - nvkm_error(subdev, "failed to lower fanspeed: %d\n", ret);
drivers/gpu/drm/nouveau/nvkm/subdev/clk/base.c +4
-1+ int err = nvkm_therm_cstate(therm, pstate->fanspeed, -1); + + if (err && err != -ENODEV) + nvkm_error(subdev, "failed to lower fanspeed: %d\n", err);
}
return ret;