Skip to contentSkip to conversation

bpf: enforce correct alignment for instructions

PATCH In review Eric Dumazet 4 participants 5 messages Jun 21, 2018

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

PATCH 1/1 bpf: enforce correct alignment for instructions

After commit 9facc336876f ("bpf: reject any prog that failed read-only lock") offsetof(struct bpf_binary_header, image) became 3 instead of 4, breaking powerpc BPF badly, since instructions need to be word aligned.

Fixes 9facc336876f ("bpf: reject any prog that failed read-only lock")
Cc Daniel Borkmann <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Alexei Starovoitov <[email protected]>
---
 include/linux/filter.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/filter.h +3 -1
index b615df57b7d5b2ccb468c411c3a2aae103cd2aea..20f2659dd829256d7fef206087ab3262e1e291f5 100644
@@ -472,7 +472,9 @@ struct sock_fprog_kern {
 struct bpf_binary_header {
 	u16 pages;
 	u16 locked:1;
-	u8 image[];
+
+	/* Some arches need word alignment for their instructions */
+	u8 image[] __aligned(4);
 };
 
 struct bpf_prog {
Signature · 1 line
2.18.0.rc1.244.gcf134e6275-goog
maintainer
#1 lore raw

From: Eric Dumazet <[email protected]>
Date: Wed, 20 Jun 2018 17:24:09 -0700

Quoted 6 lines

After commit 9facc336876f ("bpf: reject any prog that failed read-only lock") offsetof(struct bpf_binary_header, image) became 3 instead of 4, breaking powerpc BPF badly, since instructions need to be word aligned.

Fixes 9facc336876f ("bpf: reject any prog that failed read-only lock")

I'll apply this directly, thanks Eric.

#2 lore raw
Quoted 12 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Wed, 20 Jun 2018 17:24:09 -0700

Quoted 6 lines

After commit 9facc336876f ("bpf: reject any prog that failed read-only lock") offsetof(struct bpf_binary_header, image) became 3 instead of 4, breaking powerpc BPF badly, since instructions need to be word aligned.

Fixes 9facc336876f ("bpf: reject any prog that failed read-only lock")

I'll apply this directly, thanks Eric.

Thanks David :)

maintainer
#3 lore raw
Quoted 8 lines · Eric Dumazet
Quoted 7 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Wed, 20 Jun 2018 17:24:09 -0700

Quoted 4 lines

After commit 9facc336876f ("bpf: reject any prog that failed read-only lock") offsetof(struct bpf_binary_header, image) became 3 instead of 4, breaking powerpc BPF badly, since instructions need to be word aligned.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Fixes 9facc336876f ("bpf: reject any prog that failed read-only lock")

I'll apply this directly, thanks Eric.

Thanks David :)

Sigh, sorry for the breakage, looks like I got fooled by x86 gcc.

struct bpf_binary_header {
        u16                        pages;                /*     0     2 */
        u16                        locked:1;             /*     2:15  2 */

        /* XXX 15 bits hole, try to pack */

        u8                         image[0];             /*     4     0 */

        /* size: 4, cachelines: 1, members: 3 */
        /* bit holes: 1, sum bit holes: 15 bits */
        /* last cacheline: 4 bytes */
};

Thanks Eric!

maintainer
#4 lore raw

From: Daniel Borkmann <[email protected]>
Date: Thu, 21 Jun 2018 23:03:09 +0200

Quoted 14 lines · Eric Dumazet
Quoted 13 lines · David Miller
Quoted 11 lines

From: Eric Dumazet <[email protected]>
Date: Wed, 20 Jun 2018 17:24:09 -0700

Quoted 6 lines

After commit 9facc336876f ("bpf: reject any prog that failed read-only lock") offsetof(struct bpf_binary_header, image) became 3 instead of 4, breaking powerpc BPF badly, since instructions need to be word aligned.

Fixes 9facc336876f ("bpf: reject any prog that failed read-only lock")

I'll apply this directly, thanks Eric.

Quoted 7 lines
Quoted 1 line

Thanks David :)

Sigh, sorry for the breakage, looks like I got fooled by x86 gcc.

struct bpf_binary_header {

        u16                        pages;                /*     0     2 */
        u16                        locked:1;             /*     2:15  2 */

Note that you can also just make locked a plan u16 for now until you need more flag bits, the code generated will be more efficient especially on non-x86.