Skip to contentSkip to conversation

net: bridge: add compile-time assert for cb struct size

PATCH Applied Florian Westphal 6 participants 9 messages Mar 3, 2015

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

PATCH v2 1/1 net: bridge: add compile-time assert for cb struct size

make build fail if structure no longer fits into ->cb storage.

---
Change since v1: use FIELD_SIZEOF

checkpatch complains about >80 chars, but line break is ugly here.

net/bridge/br.c +2 -0
index fb57ab6..02c24cf 100644
@@ -190,6 +190,8 @@ static int __init br_init(void)
 {
 	int err;
 
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+
 	err = stp_proto_register(&br_stp_proto);
 	if (err < 0) {
 		pr_err("bridge: can't register sap for STP\n");
Signature · 1 line
2.0.5
#1 lore raw

From: Florian Westphal

Quoted 13 lines

make build fail if structure no longer fits into ->cb storage.

---
Change since v1: use FIELD_SIZEOF

checkpatch complains about >80 chars, but line break is ugly here.

net/bridge/br.c +0 -0
index fb57ab6..02c24cf 100644

net/bridge/br.c static int __init br_init(void) · line 194

Quoted 5 lines
net/bridge/br.c +2 -0
@@ -190,6 +190,8 @@ static int __init br_init(void)
 {
 	int err;
 
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

Then all invalid uses are likely to be picked up.

David

#2 lore raw
Quoted 17 lines · David Laight

From: Florian Westphal

Quoted 16 lines

make build fail if structure no longer fits into ->cb storage.

---
Change since v1: use FIELD_SIZEOF

checkpatch complains about >80 chars, but line break is ugly here.

net/bridge/br.c +0 -0
index fb57ab6..02c24cf 100644
@@ -190,6 +190,8 @@ static int __init br_init(void)
 {
 	int err;
 
Quoted 8 lines

net/bridge/br.c static int __init br_init(void) · line 194

Quoted 2 lines
net/bridge/br.c +2 -0
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

Then all invalid uses are likely to be picked up.

We want a build error, not a runtime one.

#3 lore raw

From: Eric Dumazet

Quoted 25 lines · David Laight
Quoted 24 lines

From: Florian Westphal

Quoted 13 lines

make build fail if structure no longer fits into ->cb storage.

---
Change since v1: use FIELD_SIZEOF

checkpatch complains about >80 chars, but line break is ugly here.

net/bridge/br.c +0 -0
index fb57ab6..02c24cf 100644

net/bridge/br.c static int __init br_init(void) · line 194

Quoted 5 lines
net/bridge/br.c +2 -0
@@ -190,6 +190,8 @@ static int __init br_init(void)
 {
 	int err;
 
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

Quoted 3 lines
Quoted 1 line

Then all invalid uses are likely to be picked up.

We want a build error, not a runtime one.

That is a build error.

David

#4 lore raw
Quoted 1 line · David Laight

That is a build error.

Yes, but we want a build error that this obvious to understand.

Like :

BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));

Please send a tested patch so that we can compare the solutions.

#5 lore raw
In reply to David Laight

On 03/03/2015 02:50 PM, David Laight wrote:
...

Quoted 6 lines

net/bridge/br.c static int __init br_init(void) · line 194

Quoted 2 lines
net/bridge/br.c +2 -0
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

That would require to change all skb->cb[] call-sites everywhere.

Besides that, this macro is also buggy, my totally untrained lisp-eye tells me that you got your brackets wrong.

Cheers,
Daniel

#6 lore raw

From: Daniel Borkmann

Quoted 10 lines · David Laight

...

Quoted 6 lines

net/bridge/br.c static int __init br_init(void) · line 194

Quoted 2 lines
net/bridge/br.c +2 -0
+	BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

That would require to change all skb->cb[] call-sites everywhere.

Well, only once for each type.
As opposed to adding an open coded check at all of them.
Clearly if the majority of sites have been changed then the 'cb' field member can be renamed to pick up the stragglers. I wouldn't suggest doing that immediately.

Quoted 2 lines

Besides that, this macro is also buggy, my totally untrained lisp-eye tells me that you got your brackets wrong.

I did say 'based on'....
It is also probably possible to get the name of the type into the error message. Although that might require it always be a 'struct'.

David

#7 lore raw
Quoted 6 lines · David Laight

From: Daniel Borkmann

Quoted 5 lines · David Laight

...

Quoted 3 lines
Quoted 2 lines
net/bridge/br.c +2 -0
+  BUILD_BUG_ON(sizeof(struct br_input_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
+
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

What about using something based on:
#define GET_CB(skb, type) ((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to access skb->sb?

That would require to change all skb->cb[] call-sites everywhere.

Well, only once for each type.

Note that there are cases where skb->cb[] used size is greater than the associated
type.
For example, in packet_rcv() a full hw address is stored in skb->cb[], exceeding the packet_skb_cb struct size. This is also true for can/raw.c (in raw_flags()), though
in that specific case the skb->cb[] use could have been wrapped in a struct.