Files
kernel_realme_nemo/include/linux
Andrey Ignatov 9592f395bf UPSTREAM: bpf: Allow narrow loads with offset > 0
[ Upstream commit 46f53a65d2de3e1591636c22b626b09d8684fd71 ]

Currently BPF verifier allows narrow loads for a context field only with
offset zero. E.g. if there is a __u32 field then only the following
loads are permitted:
  * off=0, size=1 (narrow);
  * off=0, size=2 (narrow);
  * off=0, size=4 (full).

On the other hand LLVM can generate a load with offset different than
zero that make sense from program logic point of view, but verifier
doesn't accept it.

E.g. tools/testing/selftests/bpf/sendmsg4_prog.c has code:

  #define DST_IP4			0xC0A801FEU /* 192.168.1.254 */
  ...
  	if ((ctx->user_ip4 >> 24) == (bpf_htonl(DST_IP4) >> 24) &&

where ctx is struct bpf_sock_addr.

Some versions of LLVM can produce the following byte code for it:

       8:       71 12 07 00 00 00 00 00         r2 = *(u8 *)(r1 + 7)
       9:       67 02 00 00 18 00 00 00         r2 <<= 24
      10:       18 03 00 00 00 00 00 fe 00 00 00 00 00 00 00 00         r3 = 4261412864 ll
      12:       5d 32 07 00 00 00 00 00         if r2 != r3 goto +7 <LBB0_6>

where `*(u8 *)(r1 + 7)` means narrow load for ctx->user_ip4 with size=1
and offset=3 (7 - sizeof(ctx->user_family) = 3). This load is currently
rejected by verifier.

Verifier code that rejects such loads is in bpf_ctx_narrow_access_ok()
what means any is_valid_access implementation, that uses the function,
works this way, e.g. bpf_skb_is_valid_access() for __sk_buff or
sock_addr_is_valid_access() for bpf_sock_addr.

The patch makes such loads supported. Offset can be in [0; size_default)
but has to be multiple of load size. E.g. for __u32 field the following
loads are supported now:
  * off=0, size=1 (narrow);
  * off=1, size=1 (narrow);
  * off=2, size=1 (narrow);
  * off=3, size=1 (narrow);
  * off=0, size=2 (narrow);
  * off=2, size=2 (narrow);
  * off=0, size=4 (full).

Reported-by: Yonghong Song <yhs@fb.com>
Change-Id: I970b6297a5f6761d5f899bf19da5e01a4c889f6c
Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Naveen <133593113+elohim-etz@users.noreply.github.com>
2025-12-24 11:48:32 +00:00
..
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2025-12-24 11:16:57 +00:00
2022-08-17 15:18:46 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2022-10-26 13:17:05 +02:00
2025-09-21 09:20:40 +00:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2025-12-24 11:35:46 +00:00
2020-03-03 00:04:10 +08:00
2025-12-24 11:20:53 +00:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2023-12-20 15:32:35 +01:00
2019-11-24 08:22:48 +01:00
2021-12-08 08:46:55 +01:00
2021-12-25 18:03:37 +05:30
2025-12-24 11:35:13 +00:00
2020-04-02 16:34:21 +02:00
2021-12-25 18:03:37 +05:30
2020-03-03 00:04:10 +08:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-03-03 18:58:56 +01:00
2020-01-13 19:40:11 +00:00
2025-12-24 11:35:14 +00:00
2019-12-17 21:13:36 +01:00
2021-03-03 18:58:56 +01:00
2022-08-25 11:11:25 +02:00
2021-02-07 14:47:41 +01:00
2020-09-09 19:35:54 +02:00
2025-09-21 09:20:40 +00:00
2021-12-25 18:03:37 +05:30
2019-12-31 12:37:21 +01:00
2021-12-25 18:03:37 +05:30
2021-07-11 12:48:09 +02:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2020-06-16 12:53:13 -07:00
2020-03-20 10:54:12 +01:00
2019-08-12 13:29:46 -04:00
2021-12-25 18:03:37 +05:30
2020-06-05 16:00:11 +08:00
2021-12-25 18:03:37 +05:30
2025-12-24 11:23:19 +00:00
2019-06-11 12:21:48 +02:00
2023-11-28 16:45:44 +00:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30
2020-04-02 16:34:36 +02:00
2021-12-25 18:03:37 +05:30
2020-08-01 00:48:35 +08:00
2020-01-09 14:58:35 -08:00
2025-12-24 11:20:53 +00:00
2021-12-25 18:03:37 +05:30
2025-12-24 11:16:57 +00:00
2020-04-02 16:34:36 +02:00
2021-12-14 10:16:55 +01:00
2021-12-25 18:03:37 +05:30
2021-12-25 18:03:37 +05:30