Skip to contentSkip to conversation

wireless: at76c50x: allocating too much data

PATCH Superseded Dan Carpenter 4 participants 13 messages Apr 20, 2012 – Apr 21, 2012

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

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
index faa8bcb..0bba5ea 100644
@@ -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;
#1 lore raw

Hi Dan,

Quoted 14 lines · Dan Carpenter

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 +0 -0
index faa8bcb..0bba5ea 100644

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
#2 lore raw
Quoted 5 lines · Julian Calaby
Quoted 2 lines

-       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)?

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

#3 lore raw
Quoted 9 lines · Dan Carpenter
Quoted 5 lines · Julian Calaby
Quoted 2 lines

-       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)?

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
#4 lore raw

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
index faa8bcb..3036c0b 100644
@@ -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);
#5 lore raw
In reply to Kalle Valo
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);
+       struct mib_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

#6 lore raw
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);
+       struct mib_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

#7 lore raw
In reply to 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);
+       struct mib_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 789
drivers/block/cciss.c, line 4211

I will send patches for those two.

julia

#8 lore raw
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 789
drivers/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

#9 lore raw
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 789
drivers/block/cciss.c, line 4211

Clever. 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

#10 lore raw
In reply to 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 789
drivers/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)

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

#11 lore raw
In reply to 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 789
drivers/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)

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

#12 lore raw
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 789
drivers/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)

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