Skip to content

Commit 8ba71db

Browse files
committed
Merge branch 'skbuff-struct-group'
Kees Cook says: ==================== skbuff: Switch structure bounds to struct_group() This is a pair of patches to add struct_group() to struct sk_buff. The first is needed to work around sparse-specific complaints, and is new for v2. The second patch is the same as originally sent as v1. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents cb902b3 + 03f6104 commit 8ba71db

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
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: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ struct sk_buff {
795795
#else
796796
#define CLONED_MASK 1
797797
#endif
798-
#define CLONED_OFFSET() offsetof(struct sk_buff, __cloned_offset)
798+
#define CLONED_OFFSET offsetof(struct sk_buff, __cloned_offset)
799799

800800
/* private: */
801801
__u8 __cloned_offset[0];
@@ -811,25 +811,15 @@ 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: */
820-
821-
/* if you move pkt_type around you also must adapt those constants */
822-
#ifdef __BIG_ENDIAN_BITFIELD
823-
#define PKT_TYPE_MAX (7 << 5)
824-
#else
825-
#define PKT_TYPE_MAX 7
826-
#endif
827-
#define PKT_TYPE_OFFSET() offsetof(struct sk_buff, __pkt_type_offset)
817+
struct_group(headers,
828818

829819
/* private: */
830820
__u8 __pkt_type_offset[0];
831821
/* public: */
832-
__u8 pkt_type:3;
822+
__u8 pkt_type:3; /* see PKT_TYPE_MAX */
833823
__u8 ignore_df:1;
834824
__u8 nf_trace:1;
835825
__u8 ip_summed:2;
@@ -845,16 +835,10 @@ struct sk_buff {
845835
__u8 encap_hdr_csum:1;
846836
__u8 csum_valid:1;
847837

848-
#ifdef __BIG_ENDIAN_BITFIELD
849-
#define PKT_VLAN_PRESENT_BIT 7
850-
#else
851-
#define PKT_VLAN_PRESENT_BIT 0
852-
#endif
853-
#define PKT_VLAN_PRESENT_OFFSET() offsetof(struct sk_buff, __pkt_vlan_present_offset)
854838
/* private: */
855839
__u8 __pkt_vlan_present_offset[0];
856840
/* public: */
857-
__u8 vlan_present:1;
841+
__u8 vlan_present:1; /* See PKT_VLAN_PRESENT_BIT */
858842
__u8 csum_complete_sw:1;
859843
__u8 csum_level:2;
860844
__u8 csum_not_inet:1;
@@ -935,9 +919,7 @@ struct sk_buff {
935919
u64 kcov_handle;
936920
#endif
937921

938-
/* private: */
939-
__u32 headers_end[0];
940-
/* public: */
922+
); /* end headers group */
941923

942924
/* These elements must be at the end, see alloc_skb() for details. */
943925
sk_buff_data_t tail;
@@ -953,6 +935,22 @@ struct sk_buff {
953935
#endif
954936
};
955937

938+
/* if you move pkt_type around you also must adapt those constants */
939+
#ifdef __BIG_ENDIAN_BITFIELD
940+
#define PKT_TYPE_MAX (7 << 5)
941+
#else
942+
#define PKT_TYPE_MAX 7
943+
#endif
944+
#define PKT_TYPE_OFFSET offsetof(struct sk_buff, __pkt_type_offset)
945+
946+
/* if you move pkt_vlan_present around you also must adapt these constants */
947+
#ifdef __BIG_ENDIAN_BITFIELD
948+
#define PKT_VLAN_PRESENT_BIT 7
949+
#else
950+
#define PKT_VLAN_PRESENT_BIT 0
951+
#endif
952+
#define PKT_VLAN_PRESENT_OFFSET offsetof(struct sk_buff, __pkt_vlan_present_offset)
953+
956954
#ifdef __KERNEL__
957955
/*
958956
* Handling routines are only of interest to the kernel

net/core/filter.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
301301
break;
302302

303303
case SKF_AD_PKTTYPE:
304-
*insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
304+
*insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
305305
*insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
306306
#ifdef __BIG_ENDIAN_BITFIELD
307307
*insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
@@ -323,7 +323,7 @@ static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
323323
offsetof(struct sk_buff, vlan_tci));
324324
break;
325325
case SKF_AD_VLAN_TAG_PRESENT:
326-
*insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
326+
*insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET);
327327
if (PKT_VLAN_PRESENT_BIT)
328328
*insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
329329
if (PKT_VLAN_PRESENT_BIT < 7)
@@ -8029,7 +8029,7 @@ static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
80298029
* (Fast-path, otherwise approximation that we might be
80308030
* a clone, do the rest in helper.)
80318031
*/
8032-
*insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
8032+
*insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
80338033
*insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
80348034
*insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
80358035

@@ -8617,7 +8617,7 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
86178617
case offsetof(struct __sk_buff, pkt_type):
86188618
*target_size = 1;
86198619
*insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8620-
PKT_TYPE_OFFSET());
8620+
PKT_TYPE_OFFSET);
86218621
*insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
86228622
#ifdef __BIG_ENDIAN_BITFIELD
86238623
*insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
@@ -8642,7 +8642,7 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
86428642
case offsetof(struct __sk_buff, vlan_present):
86438643
*target_size = 1;
86448644
*insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8645-
PKT_VLAN_PRESENT_OFFSET());
8645+
PKT_VLAN_PRESENT_OFFSET);
86468646
if (PKT_VLAN_PRESENT_BIT)
86478647
*insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
86488648
if (PKT_VLAN_PRESENT_BIT < 7)

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)