Skip to content

Commit cbf33b8

Browse files
committed
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
2 parents d955299 + 63d2247 commit cbf33b8

22 files changed

+376
-317
lines changed

kernel/bpf/verifier.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15645,7 +15645,8 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
1564515645
}
1564615646

1564715647
/* check dest operand */
15648-
if (opcode == BPF_NEG) {
15648+
if (opcode == BPF_NEG &&
15649+
regs[insn->dst_reg].type == SCALAR_VALUE) {
1564915650
err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
1565015651
err = err ?: adjust_scalar_min_max_vals(env, insn,
1565115652
&regs[insn->dst_reg],
@@ -15803,7 +15804,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
1580315804
} else { /* all other ALU ops: and, sub, xor, add, ... */
1580415805

1580515806
if (BPF_SRC(insn->code) == BPF_X) {
15806-
if (insn->imm != 0 || insn->off > 1 ||
15807+
if (insn->imm != 0 || (insn->off != 0 && insn->off != 1) ||
1580715808
(insn->off == 1 && opcode != BPF_MOD && opcode != BPF_DIV)) {
1580815809
verbose(env, "BPF_ALU uses reserved fields\n");
1580915810
return -EINVAL;
@@ -15813,7 +15814,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
1581315814
if (err)
1581415815
return err;
1581515816
} else {
15816-
if (insn->src_reg != BPF_REG_0 || insn->off > 1 ||
15817+
if (insn->src_reg != BPF_REG_0 || (insn->off != 0 && insn->off != 1) ||
1581715818
(insn->off == 1 && opcode != BPF_MOD && opcode != BPF_DIV)) {
1581815819
verbose(env, "BPF_ALU uses reserved fields\n");
1581915820
return -EINVAL;

tools/lib/bpf/Build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 \
22
netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
33
btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
44
usdt.o zip.o elf.o features.o btf_iter.o btf_relocate.o

tools/lib/bpf/btf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "libbpf_internal.h"
2424
#include "hashmap.h"
2525
#include "strset.h"
26-
#include "str_error.h"
2726

2827
#define BTF_MAX_NR_TYPES 0x7fffffffU
2928
#define BTF_MAX_STR_OFFSET 0x7fffffffU

tools/lib/bpf/btf_dump.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "hashmap.h"
2222
#include "libbpf.h"
2323
#include "libbpf_internal.h"
24-
#include "str_error.h"
2524

2625
static const char PREFIXES[] = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
2726
static const size_t PREFIX_CNT = sizeof(PREFIXES) - 1;

tools/lib/bpf/elf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <linux/kernel.h>
1010

1111
#include "libbpf_internal.h"
12-
#include "str_error.h"
1312

1413
/* A SHT_GNU_versym section holds 16-bit words. This bit is set if
1514
* the symbol is hidden and can only be seen when referenced using an

tools/lib/bpf/features.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "libbpf.h"
77
#include "libbpf_common.h"
88
#include "libbpf_internal.h"
9-
#include "str_error.h"
109

1110
static inline __u64 ptr_to_u64(const void *ptr)
1211
{

tools/lib/bpf/gen_loader.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66
#include <errno.h>
7+
#include <asm/byteorder.h>
78
#include <linux/filter.h>
89
#include <sys/param.h>
910
#include "btf.h"
@@ -13,8 +14,6 @@
1314
#include "hashmap.h"
1415
#include "bpf_gen_internal.h"
1516
#include "skel_internal.h"
16-
#include <asm/byteorder.h>
17-
#include "str_error.h"
1817

1918
#define MAX_USED_MAPS 64
2019
#define MAX_USED_PROGS 32

tools/lib/bpf/libbpf.c

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/perf_event.h>
3636
#include <linux/bpf_perf_event.h>
3737
#include <linux/ring_buffer.h>
38-
#include <linux/unaligned.h>
3938
#include <sys/epoll.h>
4039
#include <sys/ioctl.h>
4140
#include <sys/mman.h>
@@ -51,7 +50,6 @@
5150
#include "libbpf.h"
5251
#include "bpf.h"
5352
#include "btf.h"
54-
#include "str_error.h"
5553
#include "libbpf_internal.h"
5654
#include "hashmap.h"
5755
#include "bpf_gen_internal.h"
@@ -319,8 +317,6 @@ static void pr_perm_msg(int err)
319317
buf);
320318
}
321319

322-
#define STRERR_BUFSIZE 128
323-
324320
/* Copied from tools/perf/util/util.h */
325321
#ifndef zfree
326322
# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
@@ -14285,100 +14281,3 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
1428514281
free(s->progs);
1428614282
free(s);
1428714283
}
14288-
14289-
static inline __u32 ror32(__u32 v, int bits)
14290-
{
14291-
return (v >> bits) | (v << (32 - bits));
14292-
}
14293-
14294-
#define SHA256_BLOCK_LENGTH 64
14295-
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
14296-
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
14297-
#define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
14298-
#define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
14299-
#define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
14300-
#define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
14301-
14302-
static const __u32 sha256_K[64] = {
14303-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
14304-
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
14305-
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
14306-
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
14307-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
14308-
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
14309-
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
14310-
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
14311-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
14312-
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
14313-
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
14314-
};
14315-
14316-
#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \
14317-
{ \
14318-
__u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
14319-
d += tmp; \
14320-
h = tmp + Sigma_0(a) + Maj(a, b, c); \
14321-
}
14322-
14323-
static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
14324-
{
14325-
while (nblocks--) {
14326-
__u32 a = state[0];
14327-
__u32 b = state[1];
14328-
__u32 c = state[2];
14329-
__u32 d = state[3];
14330-
__u32 e = state[4];
14331-
__u32 f = state[5];
14332-
__u32 g = state[6];
14333-
__u32 h = state[7];
14334-
__u32 w[64];
14335-
int i;
14336-
14337-
for (i = 0; i < 16; i++)
14338-
w[i] = get_unaligned_be32(&data[4 * i]);
14339-
for (; i < ARRAY_SIZE(w); i++)
14340-
w[i] = sigma_1(w[i - 2]) + w[i - 7] +
14341-
sigma_0(w[i - 15]) + w[i - 16];
14342-
for (i = 0; i < ARRAY_SIZE(w); i += 8) {
14343-
SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
14344-
SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
14345-
SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
14346-
SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
14347-
SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
14348-
SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
14349-
SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
14350-
SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
14351-
}
14352-
state[0] += a;
14353-
state[1] += b;
14354-
state[2] += c;
14355-
state[3] += d;
14356-
state[4] += e;
14357-
state[5] += f;
14358-
state[6] += g;
14359-
state[7] += h;
14360-
data += SHA256_BLOCK_LENGTH;
14361-
}
14362-
}
14363-
14364-
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
14365-
{
14366-
__u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
14367-
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
14368-
const __be64 bitcount = cpu_to_be64((__u64)len * 8);
14369-
__u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
14370-
size_t final_len = len % SHA256_BLOCK_LENGTH;
14371-
int i;
14372-
14373-
sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
14374-
14375-
memcpy(final_data, data + len - final_len, final_len);
14376-
final_data[final_len] = 0x80;
14377-
final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
14378-
memcpy(&final_data[final_len - 8], &bitcount, 8);
14379-
14380-
sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
14381-
14382-
for (i = 0; i < ARRAY_SIZE(state); i++)
14383-
put_unaligned_be32(state[i], &out[4 * i]);
14384-
}

tools/lib/bpf/libbpf_errno.c

Lines changed: 0 additions & 75 deletions
This file was deleted.

tools/lib/bpf/libbpf_internal.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ do { \
172172
#define pr_info(fmt, ...) __pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
173173
#define pr_debug(fmt, ...) __pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
174174

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+
175185
#ifndef __has_builtin
176186
#define __has_builtin(x) 0
177187
#endif
@@ -712,6 +722,11 @@ static inline bool is_pow_of_2(size_t x)
712722
return x && (x & (x - 1)) == 0;
713723
}
714724

725+
static inline __u32 ror32(__u32 v, int bits)
726+
{
727+
return (v >> bits) | (v << (32 - bits));
728+
}
729+
715730
#define PROG_LOAD_ATTEMPTS 5
716731
int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
717732

0 commit comments

Comments
 (0)