Skip to contentSkip to conversation

device: fix missing check on list iterator

PATCH In review Xiaomeng Tong 2 participants 4 messages Mar 27, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device: fix missing check on list iterator

The bug is here:

	lo = pstate->base.domain[domain->name];

The list iterator 'pstate' will point to a bogus position containing HEAD if the list is empty or no element is found. This case should be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

To fix this bug, add an check. Use a new value 'iter' as the list iterator, while use the old value 'pstate' as a dedicated variable to point to the found element.

Fixes 9838366c1597d ("drm/nouveau/device: initial control object class, with pstate control methods")
---
 drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +8 -3
index ce774579c89d..6b768635e8ba 100644
@@ -72,7 +72,7 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
 	} *args = data;
 	struct nvkm_clk *clk = ctrl->device->clk;
 	const struct nvkm_domain *domain;
-	struct nvkm_pstate *pstate;
+	struct nvkm_pstate *pstate = NULL, *iter;
 	struct nvkm_cstate *cstate;
 	int i = 0, j = -1;
 	u32 lo, hi;
@@ -103,11 +103,16 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
 		return -EINVAL;
 
 	if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
-		list_for_each_entry(pstate, &clk->states, head) {
-			if (i++ == args->v0.state)
+		list_for_each_entry(iter, &clk->states, head) {
+			if (i++ == args->v0.state) {
+				pstate = iter;
 				break;
+			}
 		}
 
+		if (!pstate)
+			return -EINVAL;
+
 		lo = pstate->base.domain[domain->name];
 		hi = lo;
 		list_for_each_entry(cstate, &pstate->list, head) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 33 lines · Xiaomeng Tong

The bug is here:

	lo = pstate->base.domain[domain->name];

The list iterator 'pstate' will point to a bogus position containing HEAD if the list is empty or no element is found. This case should be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

To fix this bug, add an check. Use a new value 'iter' as the list iterator, while use the old value 'pstate' as a dedicated variable to point to the found element.

Fixes 9838366c1597d ("drm/nouveau/device: initial control object class, with pstate control methods")
---
  drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +1 -1
index ce774579c89d..6b768635e8ba 100644
@@ -72,7 +72,7 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
  	} *args = data;
  	struct nvkm_clk *clk = ctrl->device->clk;
  	const struct nvkm_domain *domain;
-	struct nvkm_pstate *pstate;
+	struct nvkm_pstate *pstate = NULL, *iter;
  	struct nvkm_cstate *cstate;
  	int i = 0, j = -1;
  	u32 lo, hi;

drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size) · line 108

Quoted 8 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +3 -2
@@ -103,11 +103,16 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
  		return -EINVAL;
 
  	if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
-		list_for_each_entry(pstate, &clk->states, head) {
-			if (i++ == args->v0.state)
+		list_for_each_entry(iter, &clk->states, head) {
+			if (i++ == args->v0.state) {
+				pstate = iter;

Is iter and the assignment really necessary ? Unless I am missing something, list_for_each_entry() always assigns pos (pstate/iter), even if the list is empty. If nothing is found, pstate would be NULL at the end, so

drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size) · line 115

Quoted 7 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +4 -0
  				break;
+			}
  		}
 
+		if (!pstate)
+			return -EINVAL;
+

... just this check should do to cover both the "not found" and "list empty" cases.

Thanks,
Guenter

Quoted 3 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +0 -0
  		lo = pstate->base.domain[domain->name];
  		hi = lo;
  		list_for_each_entry(cstate, &pstate->list, head) {
#2 lore raw
Quoted 13 lines · Guenter Roeck

drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size) · line 108

Quoted 9 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +3 -2
@@ -103,11 +103,16 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
  		return -EINVAL;
 
  	if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
-		list_for_each_entry(pstate, &clk->states, head) {
-			if (i++ == args->v0.state)
+		list_for_each_entry(iter, &clk->states, head) {
+			if (i++ == args->v0.state) {
+				pstate = iter;

Is iter and the assignment really necessary ? Unless I am missing something, list_for_each_entry() always assigns pos (pstate/iter), even if the list is empty. If nothing is found, pstate would be NULL at the end, so

the pstate will not be NULL at the end! so the assignment is necessary!

#define list_for_each_entry(pos, head, member)                          \
    for (pos = __container_of((head)->next, pos, member);               \
         &pos->member != (head);                                        \
         pos = __container_of(pos->member.next, pos, member))

--
Xiaomeng Tong

#3 lore raw
Quoted 15 lines · Xiaomeng Tong
Quoted 13 lines · Guenter Roeck

drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size) · line 108

Quoted 9 lines
drivers/gpu/drm/nouveau/nvkm/engine/device/ctrl.c +3 -2
@@ -103,11 +103,16 @@ nvkm_control_mthd_pstate_attr(struct nvkm_control *ctrl, void *data, u32 size)
   		return -EINVAL;
 
   	if (args->v0.state != NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT) {
-		list_for_each_entry(pstate, &clk->states, head) {
-			if (i++ == args->v0.state)
+		list_for_each_entry(iter, &clk->states, head) {
+			if (i++ == args->v0.state) {
+				pstate = iter;

Is iter and the assignment really necessary ? Unless I am missing something, list_for_each_entry() always assigns pos (pstate/iter), even if the list is empty. If nothing is found, pstate would be NULL at the end, so

Quoted 6 lines

the pstate will not be NULL at the end! so the assignment is necessary!

#define list_for_each_entry(pos, head, member)                          \
     for (pos = __container_of((head)->next, pos, member);               \
          &pos->member != (head);                                        \
          pos = __container_of(pos->member.next, pos, member))

Uuh, yes, you are correct. Sorry for the noise.

Guenter