Skip to contentSkip to conversation

can: Use correct type in sizeof() in nla_put()

PATCH In review Marek Vasut 3 participants 7 messages Oct 30, 2015

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

PATCH 1/1 can: Use correct type in sizeof() in nla_put()

The sizeof() is invoked on an incorrect variable, likely due to some copy-paste error, and this might result in memory corruption. Fix this.

Cc Wolfgang Grandegger <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
---
 drivers/net/can/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

NOTE: I only compile-tested this.

drivers/net/can/dev.c +1 -1
index aede704..141c2a4 100644
@@ -915,7 +915,7 @@ static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
 	     nla_put(skb, IFLA_CAN_BITTIMING_CONST,
 		     sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
 
-	    nla_put(skb, IFLA_CAN_CLOCK, sizeof(cm), &priv->clock) ||
+	    nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
 	    nla_put_u32(skb, IFLA_CAN_STATE, state) ||
 	    nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
 	    nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
Signature · 1 line
2.1.4
#1 lore raw
Quoted 7 lines · Marek Vasut

The sizeof() is invoked on an incorrect variable, likely due to some copy-paste error, and this might result in memory corruption. Fix this.

Cc Wolfgang Grandegger <[email protected]>
Cc Marc Kleine-Budde <[email protected]>

Applies to can and added stable on Cc.

Thanks,
Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#2 lore raw
Quoted 10 lines · Marc Kleine-Budde
Quoted 7 lines · Marek Vasut

The sizeof() is invoked on an incorrect variable, likely due to some copy-paste error, and this might result in memory corruption. Fix this.

Cc Wolfgang Grandegger <[email protected]>
Cc Marc Kleine-Budde <[email protected]>

Applies to can and added stable on Cc.

Are you absolutelly positive this doesn't break kernel ABI please ?

I am a little worried there, since the size of can_clock and can_ctrlmode structures differ.

Best regards,
Marek Vasut

#3 lore raw
Quoted 10 lines · Marek Vasut
Quoted 9 lines · Marc Kleine-Budde
Quoted 7 lines · Marek Vasut

The sizeof() is invoked on an incorrect variable, likely due to some copy-paste error, and this might result in memory corruption. Fix this.

Cc Wolfgang Grandegger <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Quoted 6 lines
Quoted 1 line

Applies to can and added stable on Cc.

Are you absolutelly positive this doesn't break kernel ABI please ?

I am a little worried there, since the size of can_clock and can_ctrlmode structures differ.

struct can_clock is a u32, see [1]
struct can_ctrlmode is 2 x u32.

in libsocketcan[2] it's accessed like this:

Quoted 3 lines
	memcpy(res,
		RTA_DATA(can_attr[IFLA_CAN_CLOCK]),
		sizeof(struct can_clock));

I think it should be ok.

Marc

[1]
http://lxr.free-electrons.com/source/include/uapi/linux/can/netlink.h#L61
[2]
http://git.pengutronix.de/?p=tools/libsocketcan.git;a=blob;f=src/libsocketcan.c;h=c97a28cca18054c8e63326eeb5a866b79344ebe2;hb=4ea9ec7cf37a0c52f2c39a13887aaad11042ef5c#l453

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#4 lore raw
Quoted 21 lines · Marc Kleine-Budde
Quoted 10 lines · Marek Vasut
Quoted 9 lines · Marc Kleine-Budde
Quoted 7 lines · Marek Vasut

The sizeof() is invoked on an incorrect variable, likely due to some copy-paste error, and this might result in memory corruption. Fix this.

Cc Wolfgang Grandegger <[email protected]>
Cc Marc Kleine-Budde <[email protected]>
Quoted 6 lines
Quoted 1 line

Applies to can and added stable on Cc.

Are you absolutelly positive this doesn't break kernel ABI please ?

I am a little worried there, since the size of can_clock and can_ctrlmode structures differ.

struct can_clock is a u32, see [1]
struct can_ctrlmode is 2 x u32.

Quoted 7 lines

in libsocketcan[2] it's accessed like this:

Quoted 4 lines
	memcpy(res,
	
		RTA_DATA(can_attr[IFLA_CAN_CLOCK]),
		sizeof(struct can_clock));

I think it should be ok.

In that case, yes, it's good. Hopefully, noone wrote his own thing.

Best regards,
Marek Vasut

#5 lore raw
Quoted 12 lines · Marek Vasut
Quoted 11 lines · Marc Kleine-Budde
Quoted 4 lines · Marek Vasut

Are you absolutelly positive this doesn't break kernel ABI please ?

I am a little worried there, since the size of can_clock and can_ctrlmode structures differ.

struct can_clock is a u32, see [1]
struct can_ctrlmode is 2 x u32.

in libsocketcan[2] it's accessed like this:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
	memcpy(res,
	
		RTA_DATA(can_attr[IFLA_CAN_CLOCK]),
		sizeof(struct can_clock));

I think it should be ok.

In that case, yes, it's good. Hopefully, noone wrote his own thing.

Fortunately ip from iproute2 does it similary:

https://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/tree/ip/iplink_can.c#n338

	if (tb[IFLA_CAN_CLOCK]) {
		struct can_clock *clock = RTA_DATA(tb[IFLA_CAN_CLOCK]);

		fprintf(f, "\n	  clock %d", clock->freq);
	}

As the clock is a read-only value kernel->userspace and nla_put creates its own small ID/length information each time we are REALLY LUCKY that this fix doesn't break the ABI in this case.

When can_clock would have been greater then can_ctrlmode we really had a problem ...

Thanks for caching this!

Oliver

#6 lore raw
Quoted 33 lines · Oliver Hartkopp
Quoted 11 lines · Marek Vasut
Quoted 10 lines · Marc Kleine-Budde
Quoted 4 lines · Marek Vasut

Are you absolutelly positive this doesn't break kernel ABI please ?

I am a little worried there, since the size of can_clock and can_ctrlmode structures differ.

struct can_clock is a u32, see [1]
struct can_ctrlmode is 2 x u32.

in libsocketcan[2] it's accessed like this:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
	memcpy(res,
	
		RTA_DATA(can_attr[IFLA_CAN_CLOCK]),
		sizeof(struct can_clock));

I think it should be ok.

In that case, yes, it's good. Hopefully, noone wrote his own thing.

Fortunately ip from iproute2 does it similary:

https://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git/tree/i
p/iplink_can.c#n338

	if (tb[IFLA_CAN_CLOCK]) {
		struct can_clock *clock = RTA_DATA(tb[IFLA_CAN_CLOCK]);

		fprintf(f, "\n	  clock %d", clock->freq);
	}
Quoted 8 lines

As the clock is a read-only value kernel->userspace and nla_put creates its own small ID/length information each time we are REALLY LUCKY that this fix doesn't break the ABI in this case.

When can_clock would have been greater then can_ctrlmode we really had a problem ...

Thanks for caching this!

Yeah, I already had one leg in my asbestos trousers all right. Thanks for double-checking this!

Best regards,
Marek Vasut