net: bridge: add compile-time assert for cb struct size
Subsystems (3)
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
@@ -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
From: Florian Westphal
Quoted 13 lines
make build fail if structure no longer fits into ->cb storage.
Signed-off-by Florian Westphal <[email protected]>---
Change since v1: useFIELD_SIZEOFcheckpatch complains about >80 chars, but line break is ugly here.
net/bridge/br.c +0
-0
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
Quoted 17 lines · David Laight
From: Florian Westphal
Quoted 16 lines
make build fail if structure no longer fits into ->cb storage.
Signed-off-by Florian Westphal <[email protected]>---
Change since v1: useFIELD_SIZEOFcheckpatch complains about >80 chars, but line break is ugly here.
net/bridge/br.c +0
-0@@ -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:
#defineGET_CB(skb, type)((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to accessskb->sb?Then all invalid uses are likely to be picked up.
We want a build error, not a runtime one.
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.
Signed-off-by Florian Westphal <[email protected]>---
Change since v1: useFIELD_SIZEOFcheckpatch complains about >80 chars, but line break is ugly here.
net/bridge/br.c +0
-0net/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:
#defineGET_CB(skb, type)((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to accessskb->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
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:
#defineGET_CB(skb, type)((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to accessskb->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
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.
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:
#defineGET_CB(skb, type)((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to accessskb->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
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:
#defineGET_CB(skb, type)((type *)((skb)->cb + (sizeof (char[(sizeof (type) <= sizeof (skb->cb)) ? 1 : -1] - 1))) to accessskb->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.
From: Florian Westphal <[email protected]>
Date: Tue, 3 Mar 2015 13:53:31 +0100
Quoted 3 lines
make build fail if structure no longer fits into ->cb storage.
Signed-off-by Florian Westphal <[email protected]>
Applied, thanks Florian.