BPF Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH bpf-next] libbpf: poison kernel-only integer types
@ 2020-01-10 18:19 Andrii Nakryiko
  2020-01-10 18:29 ` [Potential Spoof] " Martin Lau
  2020-01-10 18:39 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2020-01-10 18:19 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel, kafai
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.

This patch also fixes a new u32 occurence in libbpf.c, added recently.

Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/bpf.c            | 3 +++
 tools/lib/bpf/bpf_prog_linfo.c | 3 +++
 tools/lib/bpf/btf.c            | 3 +++
 tools/lib/bpf/btf_dump.c       | 3 +++
 tools/lib/bpf/hashmap.c        | 3 +++
 tools/lib/bpf/libbpf.c         | 5 ++++-
 tools/lib/bpf/libbpf_errno.c   | 3 +++
 tools/lib/bpf/libbpf_probes.c  | 3 +++
 tools/lib/bpf/netlink.c        | 3 +++
 tools/lib/bpf/nlattr.c         | 3 +++
 tools/lib/bpf/str_error.c      | 3 +++
 tools/lib/bpf/xsk.c            | 3 +++
 12 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index b0ecbe9ef2d4..500afe478e94 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -32,6 +32,9 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 /*
  * When building perf, unistd.h is overridden. __NR_bpf is
  * required to be defined explicitly.
diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
index 3ed1a27b5f7c..bafca49cb1e6 100644
--- a/tools/lib/bpf/bpf_prog_linfo.c
+++ b/tools/lib/bpf/bpf_prog_linfo.c
@@ -8,6 +8,9 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 struct bpf_prog_linfo {
 	void *raw_linfo;
 	void *raw_jited_linfo;
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 5f04f56e1eb6..cfeb6a44480b 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -17,6 +17,9 @@
 #include "libbpf_internal.h"
 #include "hashmap.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 #define BTF_MAX_NR_TYPES 0x7fffffff
 #define BTF_MAX_STR_OFFSET 0x7fffffff
 
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index e95f7710f210..885acebd4396 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -18,6 +18,9 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
 
diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
index 6122272943e6..54c30c802070 100644
--- a/tools/lib/bpf/hashmap.c
+++ b/tools/lib/bpf/hashmap.c
@@ -12,6 +12,9 @@
 #include <linux/err.h>
 #include "hashmap.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 /* start with 4 buckets */
 #define HASHMAP_MIN_CAP_BITS 2
 
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3afd780b0f06..0c229f00a67e 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -55,6 +55,9 @@
 #include "libbpf_internal.h"
 #include "hashmap.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 #ifndef EM_BPF
 #define EM_BPF 247
 #endif
@@ -6475,7 +6478,7 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
 	Elf_Data *symbols;
 	unsigned int moff;
 	const char *name;
-	u32 member_idx;
+	__u32 member_idx;
 	GElf_Sym sym;
 	GElf_Rel rel;
 	int i, nrels;
diff --git a/tools/lib/bpf/libbpf_errno.c b/tools/lib/bpf/libbpf_errno.c
index 4343e40588c6..0afb51f7a919 100644
--- a/tools/lib/bpf/libbpf_errno.c
+++ b/tools/lib/bpf/libbpf_errno.c
@@ -13,6 +13,9 @@
 
 #include "libbpf.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 #define ERRNO_OFFSET(e)		((e) - __LIBBPF_ERRNO__START)
 #define ERRCODE_OFFSET(c)	ERRNO_OFFSET(LIBBPF_ERRNO__##c)
 #define NR_ERRNO	(__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c
index 320697f8e4c7..8cc992bc532a 100644
--- a/tools/lib/bpf/libbpf_probes.c
+++ b/tools/lib/bpf/libbpf_probes.c
@@ -17,6 +17,9 @@
 #include "libbpf.h"
 #include "libbpf_internal.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 static bool grep(const char *buffer, const char *pattern)
 {
 	return !!strstr(buffer, pattern);
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 5065c1aa1061..431bd25c6cdb 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -15,6 +15,9 @@
 #include "libbpf_internal.h"
 #include "nlattr.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 #ifndef SOL_NETLINK
 #define SOL_NETLINK 270
 #endif
diff --git a/tools/lib/bpf/nlattr.c b/tools/lib/bpf/nlattr.c
index 8db44bbfc66d..0ad41dfea8eb 100644
--- a/tools/lib/bpf/nlattr.c
+++ b/tools/lib/bpf/nlattr.c
@@ -13,6 +13,9 @@
 #include <string.h>
 #include <stdio.h>
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 static uint16_t nla_attr_minlen[LIBBPF_NLA_TYPE_MAX+1] = {
 	[LIBBPF_NLA_U8]		= sizeof(uint8_t),
 	[LIBBPF_NLA_U16]	= sizeof(uint16_t),
diff --git a/tools/lib/bpf/str_error.c b/tools/lib/bpf/str_error.c
index b8064eedc177..146da01979c7 100644
--- a/tools/lib/bpf/str_error.c
+++ b/tools/lib/bpf/str_error.c
@@ -4,6 +4,9 @@
 #include <stdio.h>
 #include "str_error.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 /*
  * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl
  * libc, while checking strerror_r() return to avoid having to check this in
diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
index 8e0ffa800a71..9807903f121e 100644
--- a/tools/lib/bpf/xsk.c
+++ b/tools/lib/bpf/xsk.c
@@ -32,6 +32,9 @@
 #include "libbpf_internal.h"
 #include "xsk.h"
 
+/* make sure libbpf doesn't use kernel-only integer typedefs */
+#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
+
 #ifndef SOL_XDP
  #define SOL_XDP 283
 #endif
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Potential Spoof] [PATCH bpf-next] libbpf: poison kernel-only integer types
  2020-01-10 18:19 [PATCH bpf-next] libbpf: poison kernel-only integer types Andrii Nakryiko
@ 2020-01-10 18:29 ` " Martin Lau
  2020-01-10 18:39 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Martin Lau @ 2020-01-10 18:29 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, netdev, Alexei Starovoitov, daniel, andrii.nakryiko, Kernel Team

On Fri, Jan 10, 2020 at 10:19:16AM -0800, Andrii Nakryiko wrote:
> It's been a recurring issue with types like u32 slipping into libbpf source
> code accidentally. This is not detected during builds inside kernel source
> tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
> supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
> explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
> inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
> libbpf_internal.h.
> 
> This patch also fixes a new u32 occurence in libbpf.c, added recently.
> 
> Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
>  tools/lib/bpf/bpf.c            | 3 +++
>  tools/lib/bpf/bpf_prog_linfo.c | 3 +++
>  tools/lib/bpf/btf.c            | 3 +++
>  tools/lib/bpf/btf_dump.c       | 3 +++
>  tools/lib/bpf/hashmap.c        | 3 +++
>  tools/lib/bpf/libbpf.c         | 5 ++++-
>  tools/lib/bpf/libbpf_errno.c   | 3 +++
>  tools/lib/bpf/libbpf_probes.c  | 3 +++
>  tools/lib/bpf/netlink.c        | 3 +++
>  tools/lib/bpf/nlattr.c         | 3 +++
>  tools/lib/bpf/str_error.c      | 3 +++
>  tools/lib/bpf/xsk.c            | 3 +++
>  12 files changed, 37 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index b0ecbe9ef2d4..500afe478e94 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -32,6 +32,9 @@
>  #include "libbpf.h"
>  #include "libbpf_internal.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  /*
>   * When building perf, unistd.h is overridden. __NR_bpf is
>   * required to be defined explicitly.
> diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
> index 3ed1a27b5f7c..bafca49cb1e6 100644
> --- a/tools/lib/bpf/bpf_prog_linfo.c
> +++ b/tools/lib/bpf/bpf_prog_linfo.c
> @@ -8,6 +8,9 @@
>  #include "libbpf.h"
>  #include "libbpf_internal.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  struct bpf_prog_linfo {
>  	void *raw_linfo;
>  	void *raw_jited_linfo;
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 5f04f56e1eb6..cfeb6a44480b 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -17,6 +17,9 @@
>  #include "libbpf_internal.h"
>  #include "hashmap.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  #define BTF_MAX_NR_TYPES 0x7fffffff
>  #define BTF_MAX_STR_OFFSET 0x7fffffff
>  
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index e95f7710f210..885acebd4396 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -18,6 +18,9 @@
>  #include "libbpf.h"
>  #include "libbpf_internal.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
>  static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
>  
> diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
> index 6122272943e6..54c30c802070 100644
> --- a/tools/lib/bpf/hashmap.c
> +++ b/tools/lib/bpf/hashmap.c
> @@ -12,6 +12,9 @@
>  #include <linux/err.h>
>  #include "hashmap.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  /* start with 4 buckets */
>  #define HASHMAP_MIN_CAP_BITS 2
>  
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 3afd780b0f06..0c229f00a67e 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -55,6 +55,9 @@
>  #include "libbpf_internal.h"
>  #include "hashmap.h"
>  
> +/* make sure libbpf doesn't use kernel-only integer typedefs */
> +#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
> +
>  #ifndef EM_BPF
>  #define EM_BPF 247
>  #endif
> @@ -6475,7 +6478,7 @@ static int bpf_object__collect_struct_ops_map_reloc(struct bpf_object *obj,
>  	Elf_Data *symbols;
>  	unsigned int moff;
>  	const char *name;
> -	u32 member_idx;
> +	__u32 member_idx;
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] libbpf: poison kernel-only integer types
  2020-01-10 18:19 [PATCH bpf-next] libbpf: poison kernel-only integer types Andrii Nakryiko
  2020-01-10 18:29 ` [Potential Spoof] " Martin Lau
@ 2020-01-10 18:39 ` Alexei Starovoitov
  1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2020-01-10 18:39 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Network Development, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Andrii Nakryiko, Kernel Team

On Fri, Jan 10, 2020 at 10:19 AM Andrii Nakryiko <andriin@fb.com> wrote:
>
> It's been a recurring issue with types like u32 slipping into libbpf source
> code accidentally. This is not detected during builds inside kernel source
> tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
> supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
> explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
> inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
> libbpf_internal.h.
>
> This patch also fixes a new u32 occurence in libbpf.c, added recently.
>
> Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Applied. Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-10 18:19 [PATCH bpf-next] libbpf: poison kernel-only integer types Andrii Nakryiko
2020-01-10 18:29 ` [Potential Spoof] " Martin Lau
2020-01-10 18:39 ` Alexei Starovoitov

BPF Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \
		bpf@vger.kernel.org
	public-inbox-index bpf

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.bpf


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git