Skip to contentSkip to conversation

drm/nouveau/clk: avoid potential null-dereference

PATCH In review Andy Shevchenko 3 participants 7 messages Jan 7, 2015 – Jan 8, 2015

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

PATCH 1/1 drm/nouveau/clk: avoid potential null-dereference

We have to check pointer before usage.

---
 drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +3 -1
index e51b72d..2e84436 100644
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
 		return 0;
 
 	pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-	cstate = &pstate->base;
 	if (!pstate)
 		return -ENOMEM;
 
@@ -330,6 +329,9 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
 
 	pstate->pstate = perfE.pstate;
 	pstate->fanspeed = perfE.fanspeed;
+
+	cstate = &pstate->base;
+
 	cstate->voltage = perfE.voltage;
 	cstate->domain[nv_clk_src_core] = perfE.core;
 	cstate->domain[nv_clk_src_shader] = perfE.shader;
Signature · 6 lines
1.8.3.101.g727a46b

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 13 lines · Andy Shevchenko

We have to check pointer before usage.

---
 drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int idx) · line 325

Quoted 4 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
                return 0;
 
        pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

Quoted 16 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +3 -1
        if (!pstate)
                return -ENOMEM;
 
@@ -330,6 +329,9 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
 
        pstate->pstate = perfE.pstate;
        pstate->fanspeed = perfE.fanspeed;
+
+       cstate = &pstate->base;
+
        cstate->voltage = perfE.voltage;
        cstate->domain[nv_clk_src_core] = perfE.core;
        cstate->domain[nv_clk_src_shader] = perfE.shader;
--

1.8.3.101.g727a46b

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 17 lines · Ilia Mirkin
Quoted 15 lines · Andy Shevchenko

We have to check pointer before usage.

---
  drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
                 return 0;
 
Quoted 4 lines

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int idx) · line 325

Quoted 2 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
         pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

If pstate == NULL (kzalloc returned NULL), pstate->base triggers a null pointer deference error?

Thanks,
Vince

Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +3 -1
         if (!pstate)
                 return -ENOMEM;
 
@@ -330,6 +329,9 @@ nouveau_pstate_new(struct nouveau_clock *clk, int idx)
 
         pstate->pstate = perfE.pstate;
         pstate->fanspeed = perfE.fanspeed;
+
+       cstate = &pstate->base;
+
         cstate->voltage = perfE.voltage;
         cstate->domain[nv_clk_src_core] = perfE.core;
         cstate->domain[nv_clk_src_shader] = perfE.shader;
--

1.8.3.101.g727a46b

Quoted 8 lines
Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 29 lines · Vince Hsu
Quoted 21 lines · Ilia Mirkin
Quoted 18 lines · Andy Shevchenko

We have to check pointer before usage.

---
  drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int
 idx)
                 return 0;
 
Quoted 5 lines

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int idx) · line 325

Quoted 2 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
         pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

Quoted 3 lines

If pstate == NULL (kzalloc returned NULL), pstate->base triggers a null pointer
deference error?

Where do you see "pstate->base"? I only see "&pstate->base" which merely computes an offset into a structure... No reason to dereference pstate.

-ilia
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 25 lines · Ilia Mirkin
Quoted 24 lines · Vince Hsu
Quoted 23 lines · Ilia Mirkin
Quoted 14 lines · Andy Shevchenko

We have to check pointer before usage.

---
   drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int · line 326

Quoted 5 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int
 idx)
                  return 0;
 
          pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

Quoted 6 lines
Quoted 3 lines

If pstate == NULL (kzalloc returned NULL), pstate->base triggers a null pointer
deference error?

Where do you see "pstate->base"? I only see "&pstate->base" which merely computes an offset into a structure... No reason to dereference pstate.

Sorry that I don't quite understand. We do need deference pstate to get the member base which has type nouveau_cstate, and then assign the address of base to cstate for later use.

struct nouveau_pstate {
    struct list_head head;
    struct list_head list; /* c-states */
    struct nouveau_cstate base;
    u8 pstate;
    u8 fanspeed;
};

Thanks,
Vince

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 44 lines · Vince Hsu
Quoted 31 lines · Ilia Mirkin
Quoted 29 lines · Vince Hsu
Quoted 26 lines · Ilia Mirkin
Quoted 15 lines · Andy Shevchenko

We have to check pointer before usage.

---
   drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
   1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int · line 326

Quoted 5 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int
 idx)
                  return 0;
 
          pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

Quoted 7 lines
Quoted 3 lines

If pstate == NULL (kzalloc returned NULL), pstate->base triggers a null pointer
deference error?

Where do you see "pstate->base"? I only see "&pstate->base" which merely computes an offset into a structure... No reason to dereference pstate.

Sorry that I don't quite understand. We do need deference pstate to get the member base which has type nouveau_cstate, and then assign the address of base to cstate for later use.

Quoted 7 lines
struct nouveau_pstate {
    struct list_head head;
    struct list_head list; /* c-states */
    struct nouveau_cstate base;
    u8 pstate;
    u8 fanspeed;
};

&pstate->base is the same thing as

(void *)pstate + offsetof(struct nouveau_pstate, base)

At no point is pstate dereferenced. In fact, take a look at

http://en.wikipedia.org/wiki/Offsetof

which says that the traditional implementation of offsetof is

#define offsetof(st, m) ((size_t)(&((st *)0)->m))

Cheers,

-ilia
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw
Quoted 48 lines · Ilia Mirkin
Quoted 36 lines · Vince Hsu
Quoted 25 lines · Ilia Mirkin
Quoted 24 lines · Vince Hsu
Quoted 23 lines · Ilia Mirkin
Quoted 14 lines · Andy Shevchenko

We have to check pointer before usage.

---
    drivers/gpu/drm/nouveau/core/subdev/clock/base.c | 4 +++-
    1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -0
index e51b72d..2e84436 100644

drivers/gpu/drm/nouveau/core/subdev/clock/base.c nouveau_pstate_new(struct nouveau_clock *clk, int · line 326

Quoted 5 lines
drivers/gpu/drm/nouveau/core/subdev/clock/base.c +0 -1
@@ -322,7 +322,6 @@ nouveau_pstate_new(struct nouveau_clock *clk, int
 idx)
                   return 0;
 
           pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
-       cstate = &pstate->base;

What's wrong with this line? If pstate == NULL, &pstate->base == NULL as well and we return.

Quoted 6 lines
Quoted 3 lines

If pstate == NULL (kzalloc returned NULL), pstate->base triggers a null pointer
deference error?

Where do you see "pstate->base"? I only see "&pstate->base" which merely computes an offset into a structure... No reason to dereference pstate.

Sorry that I don't quite understand. We do need deference pstate to get the member base which has type nouveau_cstate, and then assign the address of base to cstate for later use.

Quoted 7 lines
struct nouveau_pstate {
     struct list_head head;
     struct list_head list; /* c-states */
     struct nouveau_cstate base;
     u8 pstate;
     u8 fanspeed;
};

&pstate->base is the same thing as

(void *)pstate + offsetof(struct nouveau_pstate, base)

Quoted 8 lines

At no point is pstate dereferenced. In fact, take a look at

http://en.wikipedia.org/wiki/Offsetof

which says that the traditional implementation of offsetof is

#define offsetof(st, m) ((size_t)(&((st *)0)->m))

Oh I got your point. You're absolutely right. Thanks for the patience. :)

Vince

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel