Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf

Pull bpf fixes from Alexei Starovoitov:

- Fix selftests/bpf (typo, conflicts) and unbreak BPF CI (Jiri Olsa)

- Remove linux/unaligned.h dependency for libbpf_sha256 (Andrii
Nakryiko) and add a test (Eric Biggers)

- Reject negative offsets for ALU operations in the verifier (Yazhou
Tang) and add a test (Eduard Zingerman)

- Skip scalar adjustment for BPF_NEG operation if destination register
is a pointer (Brahmajit Das) and add a test (KaFai Wan)

* tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf:
libbpf: Fix missing #pragma in libbpf_utils.c
selftests/bpf: Add tests for rejection of ALU ops with negative offsets
selftests/bpf: Add test for libbpf_sha256()
bpf: Reject negative offsets for ALU ops
libbpf: remove linux/unaligned.h dependency for libbpf_sha256()
libbpf: move libbpf_sha256() implementation into libbpf_utils.c
libbpf: move libbpf_errstr() into libbpf_utils.c
libbpf: remove unused libbpf_strerror_r and STRERR_BUFSIZE
libbpf: make libbpf_errno.c into more generic libbpf_utils.c
selftests/bpf: Add test for BPF_NEG alu on CONST_PTR_TO_MAP
bpf: Skip scalar adjustment for BPF_NEG if dst is a pointer
selftests/bpf: Fix realloc size in bpf_get_addrs
selftests/bpf: Fix typo in subtest_basic_usdt after merge conflict
selftests/bpf: Fix open-coded gettid syscall in uprobe syscall tests

+376 -317
+4 -3
kernel/bpf/verifier.c
··· 15645 15645 } 15646 15646 15647 15647 /* check dest operand */ 15648 - if (opcode == BPF_NEG) { 15648 + if (opcode == BPF_NEG && 15649 + regs[insn->dst_reg].type == SCALAR_VALUE) { 15649 15650 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); 15650 15651 err = err ?: adjust_scalar_min_max_vals(env, insn, 15651 15652 &regs[insn->dst_reg], ··· 15804 15803 } else { /* all other ALU ops: and, sub, xor, add, ... */ 15805 15804 15806 15805 if (BPF_SRC(insn->code) == BPF_X) { 15807 - if (insn->imm != 0 || insn->off > 1 || 15806 + if (insn->imm != 0 || (insn->off != 0 && insn->off != 1) || 15808 15807 (insn->off == 1 && opcode != BPF_MOD && opcode != BPF_DIV)) { 15809 15808 verbose(env, "BPF_ALU uses reserved fields\n"); 15810 15809 return -EINVAL; ··· 15814 15813 if (err) 15815 15814 return err; 15816 15815 } else { 15817 - if (insn->src_reg != BPF_REG_0 || insn->off > 1 || 15816 + if (insn->src_reg != BPF_REG_0 || (insn->off != 0 && insn->off != 1) || 15818 15817 (insn->off == 1 && opcode != BPF_MOD && opcode != BPF_DIV)) { 15819 15818 verbose(env, "BPF_ALU uses reserved fields\n"); 15820 15819 return -EINVAL;
+1 -1
tools/lib/bpf/Build
··· 1 - libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \ 1 + libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_utils.o \ 2 2 netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \ 3 3 btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \ 4 4 usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o
-1
tools/lib/bpf/btf.c
··· 23 23 #include "libbpf_internal.h" 24 24 #include "hashmap.h" 25 25 #include "strset.h" 26 - #include "str_error.h" 27 26 28 27 #define BTF_MAX_NR_TYPES 0x7fffffffU 29 28 #define BTF_MAX_STR_OFFSET 0x7fffffffU
-1
tools/lib/bpf/btf_dump.c
··· 21 21 #include "hashmap.h" 22 22 #include "libbpf.h" 23 23 #include "libbpf_internal.h" 24 - #include "str_error.h" 25 24 26 25 static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t"; 27 26 static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;
-1
tools/lib/bpf/elf.c
··· 9 9 #include <linux/kernel.h> 10 10 11 11 #include "libbpf_internal.h" 12 - #include "str_error.h" 13 12 14 13 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if 15 14 * the symbol is hidden and can only be seen when referenced using an
-1
tools/lib/bpf/features.c
··· 6 6 #include "libbpf.h" 7 7 #include "libbpf_common.h" 8 8 #include "libbpf_internal.h" 9 - #include "str_error.h" 10 9 11 10 static inline __u64 ptr_to_u64(const void *ptr) 12 11 {
+1 -2
tools/lib/bpf/gen_loader.c
··· 4 4 #include <stdlib.h> 5 5 #include <string.h> 6 6 #include <errno.h> 7 + #include <asm/byteorder.h> 7 8 #include <linux/filter.h> 8 9 #include <sys/param.h> 9 10 #include "btf.h" ··· 14 13 #include "hashmap.h" 15 14 #include "bpf_gen_internal.h" 16 15 #include "skel_internal.h" 17 - #include <asm/byteorder.h> 18 - #include "str_error.h" 19 16 20 17 #define MAX_USED_MAPS 64 21 18 #define MAX_USED_PROGS 32
-101
tools/lib/bpf/libbpf.c
··· 35 35 #include <linux/perf_event.h> 36 36 #include <linux/bpf_perf_event.h> 37 37 #include <linux/ring_buffer.h> 38 - #include <linux/unaligned.h> 39 38 #include <sys/epoll.h> 40 39 #include <sys/ioctl.h> 41 40 #include <sys/mman.h> ··· 50 51 #include "libbpf.h" 51 52 #include "bpf.h" 52 53 #include "btf.h" 53 - #include "str_error.h" 54 54 #include "libbpf_internal.h" 55 55 #include "hashmap.h" 56 56 #include "bpf_gen_internal.h" ··· 316 318 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n", 317 319 buf); 318 320 } 319 - 320 - #define STRERR_BUFSIZE 128 321 321 322 322 /* Copied from tools/perf/util/util.h */ 323 323 #ifndef zfree ··· 14280 14284 free(s->maps); 14281 14285 free(s->progs); 14282 14286 free(s); 14283 - } 14284 - 14285 - static inline __u32 ror32(__u32 v, int bits) 14286 - { 14287 - return (v >> bits) | (v << (32 - bits)); 14288 - } 14289 - 14290 - #define SHA256_BLOCK_LENGTH 64 14291 - #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z))) 14292 - #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 14293 - #define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22)) 14294 - #define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25)) 14295 - #define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3)) 14296 - #define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10)) 14297 - 14298 - static const __u32 sha256_K[64] = { 14299 - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 14300 - 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 14301 - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 14302 - 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 14303 - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 14304 - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 14305 - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 14306 - 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 14307 - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 14308 - 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 14309 - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, 14310 - }; 14311 - 14312 - #define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \ 14313 - { \ 14314 - __u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \ 14315 - d += tmp; \ 14316 - h = tmp + Sigma_0(a) + Maj(a, b, c); \ 14317 - } 14318 - 14319 - static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks) 14320 - { 14321 - while (nblocks--) { 14322 - __u32 a = state[0]; 14323 - __u32 b = state[1]; 14324 - __u32 c = state[2]; 14325 - __u32 d = state[3]; 14326 - __u32 e = state[4]; 14327 - __u32 f = state[5]; 14328 - __u32 g = state[6]; 14329 - __u32 h = state[7]; 14330 - __u32 w[64]; 14331 - int i; 14332 - 14333 - for (i = 0; i < 16; i++) 14334 - w[i] = get_unaligned_be32(&data[4 * i]); 14335 - for (; i < ARRAY_SIZE(w); i++) 14336 - w[i] = sigma_1(w[i - 2]) + w[i - 7] + 14337 - sigma_0(w[i - 15]) + w[i - 16]; 14338 - for (i = 0; i < ARRAY_SIZE(w); i += 8) { 14339 - SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h); 14340 - SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g); 14341 - SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f); 14342 - SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e); 14343 - SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d); 14344 - SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c); 14345 - SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b); 14346 - SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a); 14347 - } 14348 - state[0] += a; 14349 - state[1] += b; 14350 - state[2] += c; 14351 - state[3] += d; 14352 - state[4] += e; 14353 - state[5] += f; 14354 - state[6] += g; 14355 - state[7] += h; 14356 - data += SHA256_BLOCK_LENGTH; 14357 - } 14358 - } 14359 - 14360 - void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]) 14361 - { 14362 - __u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 14363 - 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; 14364 - const __be64 bitcount = cpu_to_be64((__u64)len * 8); 14365 - __u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 }; 14366 - size_t final_len = len % SHA256_BLOCK_LENGTH; 14367 - int i; 14368 - 14369 - sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH); 14370 - 14371 - memcpy(final_data, data + len - final_len, final_len); 14372 - final_data[final_len] = 0x80; 14373 - final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH); 14374 - memcpy(&final_data[final_len - 8], &bitcount, 8); 14375 - 14376 - sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH); 14377 - 14378 - for (i = 0; i < ARRAY_SIZE(state); i++) 14379 - put_unaligned_be32(state[i], &out[4 * i]); 14380 14287 }
-75
tools/lib/bpf/libbpf_errno.c
··· 1 - // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 - 3 - /* 4 - * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 - * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 - * Copyright (C) 2015 Huawei Inc. 7 - * Copyright (C) 2017 Nicira, Inc. 8 - */ 9 - 10 - #undef _GNU_SOURCE 11 - #include <stdio.h> 12 - #include <string.h> 13 - 14 - #include "libbpf.h" 15 - #include "libbpf_internal.h" 16 - 17 - /* make sure libbpf doesn't use kernel-only integer typedefs */ 18 - #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 19 - 20 - #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 21 - #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 22 - #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) 23 - 24 - static const char *libbpf_strerror_table[NR_ERRNO] = { 25 - [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", 26 - [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", 27 - [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", 28 - [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch", 29 - [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", 30 - [ERRCODE_OFFSET(RELOC)] = "Relocation failed", 31 - [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", 32 - [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", 33 - [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", 34 - [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", 35 - [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", 36 - [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", 37 - [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing", 38 - }; 39 - 40 - int libbpf_strerror(int err, char *buf, size_t size) 41 - { 42 - int ret; 43 - 44 - if (!buf || !size) 45 - return libbpf_err(-EINVAL); 46 - 47 - err = err > 0 ? err : -err; 48 - 49 - if (err < __LIBBPF_ERRNO__START) { 50 - ret = strerror_r(err, buf, size); 51 - buf[size - 1] = '\0'; 52 - return libbpf_err_errno(ret); 53 - } 54 - 55 - if (err < __LIBBPF_ERRNO__END) { 56 - const char *msg; 57 - 58 - msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; 59 - ret = snprintf(buf, size, "%s", msg); 60 - buf[size - 1] = '\0'; 61 - /* The length of the buf and msg is positive. 62 - * A negative number may be returned only when the 63 - * size exceeds INT_MAX. Not likely to appear. 64 - */ 65 - if (ret >= size) 66 - return libbpf_err(-ERANGE); 67 - return 0; 68 - } 69 - 70 - ret = snprintf(buf, size, "Unknown libbpf error %d", err); 71 - buf[size - 1] = '\0'; 72 - if (ret >= size) 73 - return libbpf_err(-ERANGE); 74 - return libbpf_err(-ENOENT); 75 - }
+15
tools/lib/bpf/libbpf_internal.h
··· 172 172 #define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__) 173 173 #define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__) 174 174 175 + /** 176 + * @brief **libbpf_errstr()** returns string corresponding to numeric errno 177 + * @param err negative numeric errno 178 + * @return pointer to string representation of the errno, that is invalidated 179 + * upon the next call. 180 + */ 181 + const char *libbpf_errstr(int err); 182 + 183 + #define errstr(err) libbpf_errstr(err) 184 + 175 185 #ifndef __has_builtin 176 186 #define __has_builtin(x) 0 177 187 #endif ··· 720 710 static inline bool is_pow_of_2(size_t x) 721 711 { 722 712 return x && (x & (x - 1)) == 0; 713 + } 714 + 715 + static inline __u32 ror32(__u32 v, int bits) 716 + { 717 + return (v >> bits) | (v << (32 - bits)); 723 718 } 724 719 725 720 #define PROG_LOAD_ATTEMPTS 5
+252
tools/lib/bpf/libbpf_utils.c
··· 1 + // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 + 3 + /* 4 + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 + * Copyright (C) 2015 Huawei Inc. 7 + * Copyright (C) 2017 Nicira, Inc. 8 + */ 9 + 10 + #undef _GNU_SOURCE 11 + #include <stdio.h> 12 + #include <string.h> 13 + #include <errno.h> 14 + #include <inttypes.h> 15 + #include <linux/kernel.h> 16 + 17 + #include "libbpf.h" 18 + #include "libbpf_internal.h" 19 + 20 + #ifndef ENOTSUPP 21 + #define ENOTSUPP 524 22 + #endif 23 + 24 + /* make sure libbpf doesn't use kernel-only integer typedefs */ 25 + #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 26 + 27 + #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 28 + #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 29 + #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) 30 + 31 + static const char *libbpf_strerror_table[NR_ERRNO] = { 32 + [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", 33 + [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", 34 + [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", 35 + [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch", 36 + [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", 37 + [ERRCODE_OFFSET(RELOC)] = "Relocation failed", 38 + [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", 39 + [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", 40 + [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", 41 + [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", 42 + [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", 43 + [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", 44 + [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing", 45 + }; 46 + 47 + int libbpf_strerror(int err, char *buf, size_t size) 48 + { 49 + int ret; 50 + 51 + if (!buf || !size) 52 + return libbpf_err(-EINVAL); 53 + 54 + err = err > 0 ? err : -err; 55 + 56 + if (err < __LIBBPF_ERRNO__START) { 57 + ret = strerror_r(err, buf, size); 58 + buf[size - 1] = '\0'; 59 + return libbpf_err_errno(ret); 60 + } 61 + 62 + if (err < __LIBBPF_ERRNO__END) { 63 + const char *msg; 64 + 65 + msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; 66 + ret = snprintf(buf, size, "%s", msg); 67 + buf[size - 1] = '\0'; 68 + /* The length of the buf and msg is positive. 69 + * A negative number may be returned only when the 70 + * size exceeds INT_MAX. Not likely to appear. 71 + */ 72 + if (ret >= size) 73 + return libbpf_err(-ERANGE); 74 + return 0; 75 + } 76 + 77 + ret = snprintf(buf, size, "Unknown libbpf error %d", err); 78 + buf[size - 1] = '\0'; 79 + if (ret >= size) 80 + return libbpf_err(-ERANGE); 81 + return libbpf_err(-ENOENT); 82 + } 83 + 84 + const char *libbpf_errstr(int err) 85 + { 86 + static __thread char buf[12]; 87 + 88 + if (err > 0) 89 + err = -err; 90 + 91 + switch (err) { 92 + case -E2BIG: return "-E2BIG"; 93 + case -EACCES: return "-EACCES"; 94 + case -EADDRINUSE: return "-EADDRINUSE"; 95 + case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL"; 96 + case -EAGAIN: return "-EAGAIN"; 97 + case -EALREADY: return "-EALREADY"; 98 + case -EBADF: return "-EBADF"; 99 + case -EBADFD: return "-EBADFD"; 100 + case -EBUSY: return "-EBUSY"; 101 + case -ECANCELED: return "-ECANCELED"; 102 + case -ECHILD: return "-ECHILD"; 103 + case -EDEADLK: return "-EDEADLK"; 104 + case -EDOM: return "-EDOM"; 105 + case -EEXIST: return "-EEXIST"; 106 + case -EFAULT: return "-EFAULT"; 107 + case -EFBIG: return "-EFBIG"; 108 + case -EILSEQ: return "-EILSEQ"; 109 + case -EINPROGRESS: return "-EINPROGRESS"; 110 + case -EINTR: return "-EINTR"; 111 + case -EINVAL: return "-EINVAL"; 112 + case -EIO: return "-EIO"; 113 + case -EISDIR: return "-EISDIR"; 114 + case -ELOOP: return "-ELOOP"; 115 + case -EMFILE: return "-EMFILE"; 116 + case -EMLINK: return "-EMLINK"; 117 + case -EMSGSIZE: return "-EMSGSIZE"; 118 + case -ENAMETOOLONG: return "-ENAMETOOLONG"; 119 + case -ENFILE: return "-ENFILE"; 120 + case -ENODATA: return "-ENODATA"; 121 + case -ENODEV: return "-ENODEV"; 122 + case -ENOENT: return "-ENOENT"; 123 + case -ENOEXEC: return "-ENOEXEC"; 124 + case -ENOLINK: return "-ENOLINK"; 125 + case -ENOMEM: return "-ENOMEM"; 126 + case -ENOSPC: return "-ENOSPC"; 127 + case -ENOTBLK: return "-ENOTBLK"; 128 + case -ENOTDIR: return "-ENOTDIR"; 129 + case -ENOTSUPP: return "-ENOTSUPP"; 130 + case -ENOTTY: return "-ENOTTY"; 131 + case -ENXIO: return "-ENXIO"; 132 + case -EOPNOTSUPP: return "-EOPNOTSUPP"; 133 + case -EOVERFLOW: return "-EOVERFLOW"; 134 + case -EPERM: return "-EPERM"; 135 + case -EPIPE: return "-EPIPE"; 136 + case -EPROTO: return "-EPROTO"; 137 + case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT"; 138 + case -ERANGE: return "-ERANGE"; 139 + case -EROFS: return "-EROFS"; 140 + case -ESPIPE: return "-ESPIPE"; 141 + case -ESRCH: return "-ESRCH"; 142 + case -ETXTBSY: return "-ETXTBSY"; 143 + case -EUCLEAN: return "-EUCLEAN"; 144 + case -EXDEV: return "-EXDEV"; 145 + default: 146 + snprintf(buf, sizeof(buf), "%d", err); 147 + return buf; 148 + } 149 + } 150 + 151 + #pragma GCC diagnostic push 152 + #pragma GCC diagnostic ignored "-Wpacked" 153 + #pragma GCC diagnostic ignored "-Wattributes" 154 + struct __packed_u32 { __u32 __val; } __attribute__((packed)); 155 + #pragma GCC diagnostic pop 156 + 157 + #define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val)) 158 + #define put_unaligned_be32(v, p) do { \ 159 + ((struct __packed_u32 *)(p))->__val = cpu_to_be32(v); \ 160 + } while (0) 161 + 162 + #define SHA256_BLOCK_LENGTH 64 163 + #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z))) 164 + #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 165 + #define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22)) 166 + #define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25)) 167 + #define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3)) 168 + #define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10)) 169 + 170 + static const __u32 sha256_K[64] = { 171 + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 172 + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 173 + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 174 + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 175 + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 176 + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 177 + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 178 + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 179 + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 180 + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 181 + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, 182 + }; 183 + 184 + #define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \ 185 + { \ 186 + __u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \ 187 + d += tmp; \ 188 + h = tmp + Sigma_0(a) + Maj(a, b, c); \ 189 + } 190 + 191 + static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks) 192 + { 193 + while (nblocks--) { 194 + __u32 a = state[0]; 195 + __u32 b = state[1]; 196 + __u32 c = state[2]; 197 + __u32 d = state[3]; 198 + __u32 e = state[4]; 199 + __u32 f = state[5]; 200 + __u32 g = state[6]; 201 + __u32 h = state[7]; 202 + __u32 w[64]; 203 + int i; 204 + 205 + for (i = 0; i < 16; i++) 206 + w[i] = get_unaligned_be32(&data[4 * i]); 207 + for (; i < ARRAY_SIZE(w); i++) 208 + w[i] = sigma_1(w[i - 2]) + w[i - 7] + 209 + sigma_0(w[i - 15]) + w[i - 16]; 210 + for (i = 0; i < ARRAY_SIZE(w); i += 8) { 211 + SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h); 212 + SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g); 213 + SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f); 214 + SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e); 215 + SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d); 216 + SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c); 217 + SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b); 218 + SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a); 219 + } 220 + state[0] += a; 221 + state[1] += b; 222 + state[2] += c; 223 + state[3] += d; 224 + state[4] += e; 225 + state[5] += f; 226 + state[6] += g; 227 + state[7] += h; 228 + data += SHA256_BLOCK_LENGTH; 229 + } 230 + } 231 + 232 + void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]) 233 + { 234 + __u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 235 + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; 236 + const __be64 bitcount = cpu_to_be64((__u64)len * 8); 237 + __u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 }; 238 + size_t final_len = len % SHA256_BLOCK_LENGTH; 239 + int i; 240 + 241 + sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH); 242 + 243 + memcpy(final_data, data + len - final_len, final_len); 244 + final_data[final_len] = 0x80; 245 + final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH); 246 + memcpy(&final_data[final_len - 8], &bitcount, 8); 247 + 248 + sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH); 249 + 250 + for (i = 0; i < ARRAY_SIZE(state); i++) 251 + put_unaligned_be32(state[i], &out[4 * i]); 252 + }
-1
tools/lib/bpf/linker.c
··· 25 25 #include "btf.h" 26 26 #include "libbpf_internal.h" 27 27 #include "strset.h" 28 - #include "str_error.h" 29 28 30 29 #define BTF_EXTERN_SEC ".extern" 31 30
-1
tools/lib/bpf/relo_core.c
··· 64 64 #include "libbpf.h" 65 65 #include "bpf.h" 66 66 #include "btf.h" 67 - #include "str_error.h" 68 67 #include "libbpf_internal.h" 69 68 #endif 70 69
-1
tools/lib/bpf/ringbuf.c
··· 21 21 #include "libbpf.h" 22 22 #include "libbpf_internal.h" 23 23 #include "bpf.h" 24 - #include "str_error.h" 25 24 26 25 struct ring { 27 26 ring_buffer_sample_fn sample_cb;
-104
tools/lib/bpf/str_error.c
··· 1 - // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 - #undef _GNU_SOURCE 3 - #include <string.h> 4 - #include <stdio.h> 5 - #include <errno.h> 6 - #include "str_error.h" 7 - 8 - #ifndef ENOTSUPP 9 - #define ENOTSUPP 524 10 - #endif 11 - 12 - /* make sure libbpf doesn't use kernel-only integer typedefs */ 13 - #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 14 - 15 - /* 16 - * Wrapper to allow for building in non-GNU systems such as Alpine Linux's musl 17 - * libc, while checking strerror_r() return to avoid having to check this in 18 - * all places calling it. 19 - */ 20 - char *libbpf_strerror_r(int err, char *dst, int len) 21 - { 22 - int ret = strerror_r(err < 0 ? -err : err, dst, len); 23 - /* on glibc <2.13, ret == -1 and errno is set, if strerror_r() can't 24 - * handle the error, on glibc >=2.13 *positive* (errno-like) error 25 - * code is returned directly 26 - */ 27 - if (ret == -1) 28 - ret = errno; 29 - if (ret) { 30 - if (ret == EINVAL) 31 - /* strerror_r() doesn't recognize this specific error */ 32 - snprintf(dst, len, "unknown error (%d)", err < 0 ? err : -err); 33 - else 34 - snprintf(dst, len, "ERROR: strerror_r(%d)=%d", err, ret); 35 - } 36 - return dst; 37 - } 38 - 39 - const char *libbpf_errstr(int err) 40 - { 41 - static __thread char buf[12]; 42 - 43 - if (err > 0) 44 - err = -err; 45 - 46 - switch (err) { 47 - case -E2BIG: return "-E2BIG"; 48 - case -EACCES: return "-EACCES"; 49 - case -EADDRINUSE: return "-EADDRINUSE"; 50 - case -EADDRNOTAVAIL: return "-EADDRNOTAVAIL"; 51 - case -EAGAIN: return "-EAGAIN"; 52 - case -EALREADY: return "-EALREADY"; 53 - case -EBADF: return "-EBADF"; 54 - case -EBADFD: return "-EBADFD"; 55 - case -EBUSY: return "-EBUSY"; 56 - case -ECANCELED: return "-ECANCELED"; 57 - case -ECHILD: return "-ECHILD"; 58 - case -EDEADLK: return "-EDEADLK"; 59 - case -EDOM: return "-EDOM"; 60 - case -EEXIST: return "-EEXIST"; 61 - case -EFAULT: return "-EFAULT"; 62 - case -EFBIG: return "-EFBIG"; 63 - case -EILSEQ: return "-EILSEQ"; 64 - case -EINPROGRESS: return "-EINPROGRESS"; 65 - case -EINTR: return "-EINTR"; 66 - case -EINVAL: return "-EINVAL"; 67 - case -EIO: return "-EIO"; 68 - case -EISDIR: return "-EISDIR"; 69 - case -ELOOP: return "-ELOOP"; 70 - case -EMFILE: return "-EMFILE"; 71 - case -EMLINK: return "-EMLINK"; 72 - case -EMSGSIZE: return "-EMSGSIZE"; 73 - case -ENAMETOOLONG: return "-ENAMETOOLONG"; 74 - case -ENFILE: return "-ENFILE"; 75 - case -ENODATA: return "-ENODATA"; 76 - case -ENODEV: return "-ENODEV"; 77 - case -ENOENT: return "-ENOENT"; 78 - case -ENOEXEC: return "-ENOEXEC"; 79 - case -ENOLINK: return "-ENOLINK"; 80 - case -ENOMEM: return "-ENOMEM"; 81 - case -ENOSPC: return "-ENOSPC"; 82 - case -ENOTBLK: return "-ENOTBLK"; 83 - case -ENOTDIR: return "-ENOTDIR"; 84 - case -ENOTSUPP: return "-ENOTSUPP"; 85 - case -ENOTTY: return "-ENOTTY"; 86 - case -ENXIO: return "-ENXIO"; 87 - case -EOPNOTSUPP: return "-EOPNOTSUPP"; 88 - case -EOVERFLOW: return "-EOVERFLOW"; 89 - case -EPERM: return "-EPERM"; 90 - case -EPIPE: return "-EPIPE"; 91 - case -EPROTO: return "-EPROTO"; 92 - case -EPROTONOSUPPORT: return "-EPROTONOSUPPORT"; 93 - case -ERANGE: return "-ERANGE"; 94 - case -EROFS: return "-EROFS"; 95 - case -ESPIPE: return "-ESPIPE"; 96 - case -ESRCH: return "-ESRCH"; 97 - case -ETXTBSY: return "-ETXTBSY"; 98 - case -EUCLEAN: return "-EUCLEAN"; 99 - case -EXDEV: return "-EXDEV"; 100 - default: 101 - snprintf(buf, sizeof(buf), "%d", err); 102 - return buf; 103 - } 104 - }
-19
tools/lib/bpf/str_error.h
··· 1 - /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 - #ifndef __LIBBPF_STR_ERROR_H 3 - #define __LIBBPF_STR_ERROR_H 4 - 5 - #define STRERR_BUFSIZE 128 6 - 7 - char *libbpf_strerror_r(int err, char *dst, int len); 8 - 9 - /** 10 - * @brief **libbpf_errstr()** returns string corresponding to numeric errno 11 - * @param err negative numeric errno 12 - * @return pointer to string representation of the errno, that is invalidated 13 - * upon the next call. 14 - */ 15 - const char *libbpf_errstr(int err); 16 - 17 - #define errstr(err) libbpf_errstr(err) 18 - 19 - #endif /* __LIBBPF_STR_ERROR_H */
-1
tools/lib/bpf/usdt.c
··· 20 20 #include "libbpf_common.h" 21 21 #include "libbpf_internal.h" 22 22 #include "hashmap.h" 23 - #include "str_error.h" 24 23 25 24 /* libbpf's USDT support consists of BPF-side state/code and user-space 26 25 * state/code working together in concert. BPF-side parts are defined in
+52
tools/testing/selftests/bpf/prog_tests/sha256.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* Copyright 2025 Google LLC */ 3 + 4 + #include <test_progs.h> 5 + #include "bpf/libbpf_internal.h" 6 + 7 + #define MAX_LEN 4096 8 + 9 + /* Test libbpf_sha256() for all lengths from 0 to MAX_LEN inclusively. */ 10 + void test_sha256(void) 11 + { 12 + /* 13 + * The correctness of this value was verified by running this test with 14 + * libbpf_sha256() replaced by OpenSSL's SHA256(). 15 + */ 16 + static const __u8 expected_digest_of_digests[SHA256_DIGEST_LENGTH] = { 17 + 0x62, 0x30, 0x0e, 0x1d, 0xea, 0x7f, 0xc4, 0x74, 18 + 0xfd, 0x8e, 0x64, 0x0b, 0xd8, 0x5f, 0xea, 0x04, 19 + 0xf3, 0xef, 0x77, 0x42, 0xc2, 0x01, 0xb8, 0x90, 20 + 0x6e, 0x19, 0x91, 0x1b, 0xca, 0xb3, 0x28, 0x42, 21 + }; 22 + __u64 seed = 0; 23 + __u8 *data = NULL, *digests = NULL; 24 + __u8 digest_of_digests[SHA256_DIGEST_LENGTH]; 25 + size_t i; 26 + 27 + data = malloc(MAX_LEN); 28 + if (!ASSERT_OK_PTR(data, "malloc")) 29 + goto out; 30 + digests = malloc((MAX_LEN + 1) * SHA256_DIGEST_LENGTH); 31 + if (!ASSERT_OK_PTR(digests, "malloc")) 32 + goto out; 33 + 34 + /* Generate MAX_LEN bytes of "random" data deterministically. */ 35 + for (i = 0; i < MAX_LEN; i++) { 36 + seed = (seed * 25214903917 + 11) & ((1ULL << 48) - 1); 37 + data[i] = (__u8)(seed >> 16); 38 + } 39 + 40 + /* Calculate a digest for each length 0 through MAX_LEN inclusively. */ 41 + for (i = 0; i <= MAX_LEN; i++) 42 + libbpf_sha256(data, i, &digests[i * SHA256_DIGEST_LENGTH]); 43 + 44 + /* Calculate and verify the digest of all the digests. */ 45 + libbpf_sha256(digests, (MAX_LEN + 1) * SHA256_DIGEST_LENGTH, 46 + digest_of_digests); 47 + ASSERT_MEMEQ(digest_of_digests, expected_digest_of_digests, 48 + SHA256_DIGEST_LENGTH, "digest_of_digests"); 49 + out: 50 + free(data); 51 + free(digests); 52 + }
+2 -2
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
··· 661 661 rounds++; 662 662 } 663 663 664 - printf("tid %d trigger rounds: %lu\n", gettid(), rounds); 664 + printf("tid %ld trigger rounds: %lu\n", sys_gettid(), rounds); 665 665 return NULL; 666 666 } 667 667 ··· 704 704 rounds++; 705 705 } 706 706 707 - printf("tid %d attach rounds: %lu hits: %d\n", gettid(), rounds, skel->bss->executed); 707 + printf("tid %ld attach rounds: %lu hits: %d\n", sys_gettid(), rounds, skel->bss->executed); 708 708 uprobe_syscall_executed__destroy(skel); 709 709 free(ref); 710 710 return NULL;
+1 -1
tools/testing/selftests/bpf/prog_tests/usdt.c
··· 142 142 goto cleanup; 143 143 #endif 144 144 145 - alled = TRIGGER(1); 145 + called = TRIGGER(1); 146 146 147 147 ASSERT_EQ(bss->usdt0_called, called, "usdt0_called"); 148 148 ASSERT_EQ(bss->usdt3_called, called, "usdt3_called");
+47
tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c
··· 3 3 4 4 #include <linux/bpf.h> 5 5 #include <bpf/bpf_helpers.h> 6 + #include "../../../include/linux/filter.h" 6 7 #include "bpf_misc.h" 7 8 8 9 #define MAX_ENTRIES 11 ··· 147 146 : __clobber_all); 148 147 } 149 148 149 + SEC("socket") 150 + __description("map_ptr illegal alu op, map_ptr = -map_ptr") 151 + __failure __msg("R0 invalid mem access 'scalar'") 152 + __failure_unpriv __msg_unpriv("R0 pointer arithmetic prohibited") 153 + __flag(BPF_F_ANY_ALIGNMENT) 154 + __naked void map_ptr_illegal_alu_op(void) 155 + { 156 + asm volatile (" \ 157 + r0 = %[map_hash_48b] ll; \ 158 + r0 = -r0; \ 159 + r1 = 22; \ 160 + *(u64*)(r0 + 0) = r1; \ 161 + exit; \ 162 + " : 163 + : __imm_addr(map_hash_48b) 164 + : __clobber_all); 165 + } 166 + 150 167 SEC("flow_dissector") 151 168 __description("flow_keys illegal alu op with variable offset") 152 169 __failure __msg("R7 pointer arithmetic on flow_keys prohibited") ··· 183 164 : __imm_const(flow_keys_off, offsetof(struct __sk_buff, flow_keys)) 184 165 : __clobber_all); 185 166 } 167 + 168 + #define DEFINE_BAD_OFFSET_TEST(name, op, off, imm) \ 169 + SEC("socket") \ 170 + __failure __msg("BPF_ALU uses reserved fields") \ 171 + __naked void name(void) \ 172 + { \ 173 + asm volatile( \ 174 + "r0 = 1;" \ 175 + ".8byte %[insn];" \ 176 + "r0 = 0;" \ 177 + "exit;" \ 178 + : \ 179 + : __imm_insn(insn, BPF_RAW_INSN((op), 0, 0, (off), (imm))) \ 180 + : __clobber_all); \ 181 + } 182 + 183 + /* 184 + * Offset fields of 0 and 1 are legal for BPF_{DIV,MOD} instructions. 185 + * Offset fields of 0 are legal for the rest of ALU instructions. 186 + * Test that error is reported for illegal offsets, assuming that tests 187 + * for legal offsets exist. 188 + */ 189 + DEFINE_BAD_OFFSET_TEST(bad_offset_divx, BPF_ALU64 | BPF_DIV | BPF_X, -1, 0) 190 + DEFINE_BAD_OFFSET_TEST(bad_offset_modk, BPF_ALU64 | BPF_MOD | BPF_K, -1, 1) 191 + DEFINE_BAD_OFFSET_TEST(bad_offset_addx, BPF_ALU64 | BPF_ADD | BPF_X, -1, 0) 192 + DEFINE_BAD_OFFSET_TEST(bad_offset_divx2, BPF_ALU64 | BPF_DIV | BPF_X, 2, 0) 193 + DEFINE_BAD_OFFSET_TEST(bad_offset_modk2, BPF_ALU64 | BPF_MOD | BPF_K, 2, 1) 194 + DEFINE_BAD_OFFSET_TEST(bad_offset_addx2, BPF_ALU64 | BPF_ADD | BPF_X, 1, 0) 186 195 187 196 char _license[] SEC("license") = "GPL";
+1 -1
tools/testing/selftests/bpf/trace_helpers.c
··· 732 732 733 733 if (cnt == max_cnt) { 734 734 max_cnt += inc_cnt; 735 - tmp_addrs = realloc(addrs, max_cnt); 735 + tmp_addrs = realloc(addrs, max_cnt * sizeof(long)); 736 736 if (!tmp_addrs) { 737 737 err = -ENOMEM; 738 738 goto error;