wireless: at76c50x: allocating too much data
Subsystems (2)
PATCH 1/1 wireless: at76c50x: allocating too much data
This is a cut and paste mistake, sizeof(struct mib_local) was intended instead of sizeof(struct mib_phy). The call to at76_get_mib() uses sizeof(struct mib_local) correctly.
The current code works fine because mib_phy structs are larger than mib_local structs. But we may as well clean it up.
drivers/net/wireless/at76c50x-usb.c +1 -1
@@ -1122,7 +1122,7 @@ exit:static void at76_dump_mib_local (struct at76_priv *priv) {int ret; -struct mib_local *m =kmalloc (sizeof (struct mib_phy ),GFP_KERNEL ); +struct mib_local *m =kmalloc (sizeof (struct mib_local ),GFP_KERNEL );if (!m)return ;
Hi Dan,
Quoted 14 lines · Dan Carpenter
This is a cut and paste mistake,
sizeof(struct mib_local)was intended instead ofsizeof(struct mib_phy). The call toat76_get_mib()usessizeof(struct mib_local)correctly.The current code works fine because
mib_phystructs are larger thanmib_localstructs. But we may as well clean it up.Signed-off-by Dan Carpenter <[email protected]>drivers/net/wireless/at76c50x-usb.c +0
-0
drivers/net/wireless/at76c50x-usb.c exit: · line 1125
Quoted 5 lines
drivers/net/wireless/at76c50x-usb.c +1
-1@@ -1122,7 +1122,7 @@ exit: static void at76_dump_mib_local(struct at76_priv *priv) { int ret; - struct mib_local *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL); + struct mib_local *m = kmalloc(sizeof(struct mib_local), GFP_KERNEL);
Would it be better practice to use sizeof(*m)?
Thanks,
Signature · 9 lines
Julian Calaby Email: [email protected] Profile: http://www.google.com/profiles/julian.calaby/ .Plan: http://sites.google.com/site/juliancalaby/ -- To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Julian Calaby
Quoted 2 lines
- struct
mib_local*m =kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
+ structmib_local*m =kmalloc(sizeof(struct mib_local), GFP_KERNEL);Would it be better practice to use
sizeof(*m)?
That was my temptation as well... But I decided to make it match with the surrounding code. I'm happy to resend if people want.
regards,
dan carpenter
Quoted 9 lines · Dan Carpenter
Quoted 5 lines · Julian Calaby
Quoted 2 lines
- struct
mib_local*m =kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
+ structmib_local*m =kmalloc(sizeof(struct mib_local), GFP_KERNEL);Would it be better practice to use
sizeof(*m)?That was my temptation as well... But I decided to make it match with the surrounding code. I'm happy to resend if people want.
IMHO sizeof(*m) is better and I tend to use it.
Related to this: I have a bad habit of sometimes dropping '*' from sizeof()? Is there a tool which could spot that?
Signature · 1 line
Kalle Valo
PATCH v2 1/1 wireless: at76c50x: allocating too much data
This is a cut and paste mistake, sizeof(struct mib_local) was intended instead of sizeof(struct mib_phy). The call to at76_get_mib() uses sizeof(struct mib_local) correctly, although I changed that to sizeof(*m) for style reasons after discussion with some of the wireless maintainers.
The current code works fine because mib_phy structs are larger than mib_local structs. But we may as well clean it up.
---
v2: use sizeof(*m) instead of sizeof(struct mib_local).
drivers/net/wireless/at76c50x-usb.c +2 -2
@@ -1122,12 +1122,12 @@ exit:static void at76_dump_mib_local (struct at76_priv *priv) {int ret; -struct mib_local *m =kmalloc (sizeof (struct mib_phy ),GFP_KERNEL ); +struct mib_local *m =kmalloc (sizeof (*m),GFP_KERNEL );if (!m)return ; - ret =at76_get_mib (priv->udev,MIB_LOCAL , m,sizeof (struct mib_local )); + ret =at76_get_mib (priv->udev,MIB_LOCAL , m,sizeof (*m));if (ret <0 ) {wiphy_err (priv->hw->wiphy,"at76_get_mib (LOCAL) failed: %d\n" , ret);
Quoted 9 lines · Kalle Valo
Quoted 7 lines · Dan Carpenter
Quoted 5 lines · Julian Calaby
Quoted 2 lines
- struct
mib_local*m =kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
+ structmib_local*m =kmalloc(sizeof(struct mib_local), GFP_KERNEL);Would it be better practice to use
sizeof(*m)?
Quoted 8 lines
Quoted 2 lines
That was my temptation as well... But I decided to make it match with the surrounding code. I'm happy to resend if people want.
IMHO
sizeof(*m)is better and I tend to use it.Related to this: I have a bad habit of sometimes dropping '*' from
sizeof()? Is there a tool which could spot that?
That's what I was working on for Smatch when I sent this patch.
The odd thing is that I can't find any bugs like this in the kernel. If sizeof(foo) is less than sizeof(*foo), which is probably the normal case, then these get caught early on in testing.
Still I think people must have done manual audits as well... It feels too clean to be natural.
regards,
dan carpenter
Quoted 19 lines · Dan Carpenter
Quoted 9 lines · Kalle Valo
Quoted 7 lines · Dan Carpenter
Quoted 5 lines · Julian Calaby
Quoted 2 lines
- struct
mib_local*m =kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
+ structmib_local*m =kmalloc(sizeof(struct mib_local), GFP_KERNEL);Would it be better practice to use
sizeof(*m)?Quoted 8 lines
Quoted 2 lines
That was my temptation as well... But I decided to make it match with the surrounding code. I'm happy to resend if people want.
IMHO
sizeof(*m)is better and I tend to use it.Related to this: I have a bad habit of sometimes dropping '*' from
sizeof()? Is there a tool which could spot that?
Quoted 8 lines
That's what I was working on for Smatch when I sent this patch.
The odd thing is that I can't find any bugs like this in the kernel. If sizeof(foo) is less than
sizeof(*foo), which is probably the normal case, then these get caught early on in testing.Still I think people must have done manual audits as well... It feels too clean to be natural.
I sent some patches with respect to this. But that was probably around a year ago.
julia
Quoted 19 lines · Dan Carpenter
Quoted 9 lines · Kalle Valo
Quoted 7 lines · Dan Carpenter
Quoted 5 lines · Julian Calaby
Quoted 2 lines
- struct
mib_local*m =kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
+ structmib_local*m =kmalloc(sizeof(struct mib_local), GFP_KERNEL);Would it be better practice to use
sizeof(*m)?Quoted 8 lines
Quoted 2 lines
That was my temptation as well... But I decided to make it match with the surrounding code. I'm happy to resend if people want.
IMHO
sizeof(*m)is better and I tend to use it.Related to this: I have a bad habit of sometimes dropping '*' from
sizeof()? Is there a tool which could spot that?
Quoted 8 lines
That's what I was working on for Smatch when I sent this patch.
The odd thing is that I can't find any bugs like this in the kernel. If sizeof(foo) is less than
sizeof(*foo), which is probably the normal case, then these get caught early on in testing.Still I think people must have done manual audits as well... It feels too clean to be natural.
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211
I will send patches for those two.
julia
Quoted 8 lines · Julia Lawall
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211
Clever. You'd need to restrict it to places where x was a pointer. That's better than my check which was specific to kmalloc(). (So uh... I'm going to rewrite mine as well to be more generic. :P)
regards,
dan carpenter
Quoted 11 lines · Dan Carpenter
Quoted 8 lines · Julia Lawall
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211Clever. You'd need to restrict it to places where x was a pointer.
Yes, I didn't put the whole thing:
@r expression@
expression *x;
position p;
@@
x@p = <+... sizeof(x) ...+>
julia
Quoted 7 lines
That's better than my check which was specific to
kmalloc(). (So uh... I'm going to rewrite mine as well to be more generic. :P)regards,
dan carpenter
Quoted 6 lines · Dan Carpenter
Quoted 5 lines · Julia Lawall
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
Quoted 8 lines
Quoted 3 lines
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211Clever. You'd need to restrict it to places where x was a pointer. That's better than my check which was specific to
kmalloc(). (So uh... I'm going to rewrite mine as well to be more generic. :P)
The other thing would be to look for places that do:
func(x, sizeof(x);
Of course, you've found a lot of memset()/memcpy() bugs like that in the past, but probably it could be made more generic so it checks every function.
regards,
dan carpenter
Quoted 6 lines · Dan Carpenter
Quoted 5 lines · Julia Lawall
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
Quoted 8 lines
Quoted 3 lines
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211Clever. You'd need to restrict it to places where x was a pointer. That's better than my check which was specific to
kmalloc(). (So uh... I'm going to rewrite mine as well to be more generic. :P)
Hm... Smatch is not really the right tool here. By the time Sparse gives you the sizeof(foo) information, it just looks like a number 8.
I hacked up Sparse a bit so it works for simple expressions which are one token in from the c tokenizer. So:
foo = kmalloc(sizeof(foo), GFP_KERNEL); => error. foo->bar = kmalloc(sizeof(foo->bar), GFP_KERNEL); => tricky.
It's not ideal. Coccinelle is better for this.
regards,
dan carpenter
Quoted 20 lines · Dan Carpenter
Quoted 6 lines · Dan Carpenter
Quoted 5 lines · Julia Lawall
Looking for x = ... sizeof(x) ... I get 9 reports. In most cases it looks like sizeof(x) is coincidentally the same as the size that is wanted. Two cases that look like they could have some noticible effect are:
Quoted 8 lines
Quoted 3 lines
arch/xtensa/platforms/iss/network.c, line 789drivers/block/cciss.c, line 4211Clever. You'd need to restrict it to places where x was a pointer. That's better than my check which was specific to
kmalloc(). (So uh... I'm going to rewrite mine as well to be more generic. :P)Hm... Smatch is not really the right tool here. By the time Sparse gives you the sizeof(foo) information, it just looks like a number 8.
Quoted 7 lines
I hacked up Sparse a bit so it works for simple expressions which are one token in from the c tokenizer. So:
foo = kmalloc(sizeof(foo), GFP_KERNEL); => error. foo->bar = kmalloc(sizeof(foo->bar), GFP_KERNEL); => tricky.It's not ideal. Coccinelle is better for this.
On the other hand, Coccinelle has no idea what the size is, so it doesn't know how important the problem is.
julia