Skip to content

Commit 03f6104

Browse files
keesdavem330
authored andcommitted
skbuff: Switch structure bounds to struct_group()
In preparation for FORTIFY_SOURCE performing compile-time and run-time field bounds checking for memcpy(), memmove(), and memset(), avoid intentionally writing across neighboring fields. Replace the existing empty member position markers "headers_start" and "headers_end" with a struct_group(). This will allow memcpy() and sizeof() to more easily reason about sizes, and improve readability. "pahole" shows no size nor member offset changes to struct sk_buff. "objdump -d" shows no object code changes (outside of WARNs affected by source line number changes). Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Jason A. Donenfeld <[email protected]> # drivers/net/wireguard/* Link: https://lore.kernel.org/lkml/20210728035006.GD35706@embeddedor Signed-off-by: David S. Miller <[email protected]>
1 parent fba8495 commit 03f6104

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

drivers/net/wireguard/queueing.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
7979
u8 sw_hash = skb->sw_hash;
8080
u32 hash = skb->hash;
8181
skb_scrub_packet(skb, true);
82-
memset(&skb->headers_start, 0,
83-
offsetof(struct sk_buff, headers_end) -
84-
offsetof(struct sk_buff, headers_start));
82+
memset(&skb->headers, 0, sizeof(skb->headers));
8583
if (encapsulating) {
8684
skb->l4_hash = l4_hash;
8785
skb->sw_hash = sw_hash;

include/linux/skbuff.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,10 @@ struct sk_buff {
811811
__u8 active_extensions;
812812
#endif
813813

814-
/* fields enclosed in headers_start/headers_end are copied
814+
/* Fields enclosed in headers group are copied
815815
* using a single memcpy() in __copy_skb_header()
816816
*/
817-
/* private: */
818-
__u32 headers_start[0];
819-
/* public: */
817+
struct_group(headers,
820818

821819
/* private: */
822820
__u8 __pkt_type_offset[0];
@@ -921,9 +919,7 @@ struct sk_buff {
921919
u64 kcov_handle;
922920
#endif
923921

924-
/* private: */
925-
__u32 headers_end[0];
926-
/* public: */
922+
); /* end headers group */
927923

928924
/* These elements must be at the end, see alloc_skb() for details. */
929925
sk_buff_data_t tail;

net/core/skbuff.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,10 @@ void napi_consume_skb(struct sk_buff *skb, int budget)
992992
}
993993
EXPORT_SYMBOL(napi_consume_skb);
994994

995-
/* Make sure a field is enclosed inside headers_start/headers_end section */
995+
/* Make sure a field is contained by headers group */
996996
#define CHECK_SKB_FIELD(field) \
997-
BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
998-
offsetof(struct sk_buff, headers_start)); \
999-
BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
1000-
offsetof(struct sk_buff, headers_end)); \
997+
BUILD_BUG_ON(offsetof(struct sk_buff, field) != \
998+
offsetof(struct sk_buff, headers.field)); \
1001999

10021000
static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
10031001
{
@@ -1009,14 +1007,12 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
10091007
__skb_ext_copy(new, old);
10101008
__nf_copy(new, old, false);
10111009

1012-
/* Note : this field could be in headers_start/headers_end section
1010+
/* Note : this field could be in the headers group.
10131011
* It is not yet because we do not want to have a 16 bit hole
10141012
*/
10151013
new->queue_mapping = old->queue_mapping;
10161014

1017-
memcpy(&new->headers_start, &old->headers_start,
1018-
offsetof(struct sk_buff, headers_end) -
1019-
offsetof(struct sk_buff, headers_start));
1015+
memcpy(&new->headers, &old->headers, sizeof(new->headers));
10201016
CHECK_SKB_FIELD(protocol);
10211017
CHECK_SKB_FIELD(csum);
10221018
CHECK_SKB_FIELD(hash);

0 commit comments

Comments
 (0)