Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Alexei Starovoitov says:

====================
pull-request: bpf 2022-04-06

We've added 8 non-merge commits during the last 8 day(s) which contain
a total of 9 files changed, 139 insertions(+), 36 deletions(-).

The main changes are:

1) rethook related fixes, from Jiri and Masami.

2) Fix the case when tracing bpf prog is attached to struct_ops, from Martin.

3) Support dual-stack sockets in bpf_tcp_check_syncookie, from Maxim.

* https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf:
  bpf: Adjust bpf_tcp_check_syncookie selftest to test dual-stack sockets
  bpf: Support dual-stack sockets in bpf_tcp_check_syncookie
  bpf: selftests: Test fentry tracing a struct_ops program
  bpf: Resolve to prog->aux->dst_prog->type only for BPF_PROG_TYPE_EXT
  rethook: Fix to use WRITE_ONCE() for rethook:: Handler
  selftests/bpf: Fix warning comparing pointer to 0
  bpf: Fix sparse warnings in kprobe_multi_resolve_syms
  bpftool: Explicit errno handling in skeletons
====================

Link: https://lore.kernel.org/r/20220407031245.73026-1-alexei.starovoitov@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
kuba-moo committed Apr 7, 2022
2 parents b423e54 + 53968da commit 8e9d0d7
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 36 deletions.
4 changes: 3 additions & 1 deletion include/linux/bpf_verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,11 @@ static inline u32 type_flag(u32 type)
return type & ~BPF_BASE_TYPE_MASK;
}

/* only use after check_attach_btf_id() */
static inline enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog)
{
return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type;
return prog->type == BPF_PROG_TYPE_EXT ?
prog->aux->dst_prog->type : prog->type;
}

#endif /* _LINUX_BPF_VERIFIER_H */
4 changes: 2 additions & 2 deletions kernel/trace/bpf_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -2349,11 +2349,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip,
}

static int
kprobe_multi_resolve_syms(const void *usyms, u32 cnt,
kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt,
unsigned long *addrs)
{
unsigned long addr, size;
const char **syms;
const char __user **syms;
int err = -ENOMEM;
unsigned int i;
char *func;
Expand Down
2 changes: 1 addition & 1 deletion kernel/trace/rethook.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void rethook_free_rcu(struct rcu_head *head)
*/
void rethook_free(struct rethook *rh)
{
rcu_assign_pointer(rh->handler, NULL);
WRITE_ONCE(rh->handler, NULL);

call_rcu(&rh->rcu, rethook_free_rcu);
}
Expand Down
17 changes: 13 additions & 4 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -7016,24 +7016,33 @@ BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len
if (!th->ack || th->rst || th->syn)
return -ENOENT;

if (unlikely(iph_len < sizeof(struct iphdr)))
return -EINVAL;

if (tcp_synq_no_recent_overflow(sk))
return -ENOENT;

cookie = ntohl(th->ack_seq) - 1;

switch (sk->sk_family) {
case AF_INET:
if (unlikely(iph_len < sizeof(struct iphdr)))
/* Both struct iphdr and struct ipv6hdr have the version field at the
* same offset so we can cast to the shorter header (struct iphdr).
*/
switch (((struct iphdr *)iph)->version) {
case 4:
if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
return -EINVAL;

ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
break;

#if IS_BUILTIN(CONFIG_IPV6)
case AF_INET6:
case 6:
if (unlikely(iph_len < sizeof(struct ipv6hdr)))
return -EINVAL;

if (sk->sk_family != AF_INET6)
return -EINVAL;

ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
break;
#endif /* CONFIG_IPV6 */
Expand Down
22 changes: 15 additions & 7 deletions tools/bpf/bpftool/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,10 @@ codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped)
s->map_cnt = %zu; \n\
s->map_skel_sz = sizeof(*s->maps); \n\
s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\
if (!s->maps) \n\
if (!s->maps) { \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
",
map_cnt
);
Expand Down Expand Up @@ -870,8 +872,10 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
s->prog_cnt = %zu; \n\
s->prog_skel_sz = sizeof(*s->progs); \n\
s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\
if (!s->progs) \n\
if (!s->progs) { \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
",
prog_cnt
);
Expand Down Expand Up @@ -1182,10 +1186,13 @@ static int do_skeleton(int argc, char **argv)
%1$s__create_skeleton(struct %1$s *obj) \n\
{ \n\
struct bpf_object_skeleton *s; \n\
int err; \n\
\n\
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
if (!s) \n\
if (!s) { \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
\n\
s->sz = sizeof(*s); \n\
s->name = \"%1$s\"; \n\
Expand All @@ -1206,7 +1213,7 @@ static int do_skeleton(int argc, char **argv)
return 0; \n\
err: \n\
bpf_object__destroy_skeleton(s); \n\
return -ENOMEM; \n\
return err; \n\
} \n\
\n\
static inline const void *%2$s__elf_bytes(size_t *sz) \n\
Expand Down Expand Up @@ -1466,12 +1473,12 @@ static int do_subskeleton(int argc, char **argv)
\n\
obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\
if (!obj) { \n\
errno = ENOMEM; \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));\n\
if (!s) { \n\
errno = ENOMEM; \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
s->sz = sizeof(*s); \n\
Expand All @@ -1483,7 +1490,7 @@ static int do_subskeleton(int argc, char **argv)
s->var_cnt = %2$d; \n\
s->vars = (struct bpf_var_skeleton *)calloc(%2$d, sizeof(*s->vars));\n\
if (!s->vars) { \n\
errno = ENOMEM; \n\
err = -ENOMEM; \n\
goto err; \n\
} \n\
",
Expand Down Expand Up @@ -1538,6 +1545,7 @@ static int do_subskeleton(int argc, char **argv)
return obj; \n\
err: \n\
%1$s__destroy(obj); \n\
errno = -err; \n\
return NULL; \n\
} \n\
\n\
Expand Down
23 changes: 23 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
#include <test_progs.h>
#include "dummy_st_ops.skel.h"
#include "trace_dummy_st_ops.skel.h"

/* Need to keep consistent with definition in include/linux/bpf.h */
struct bpf_dummy_ops_state {
Expand Down Expand Up @@ -56,6 +57,7 @@ static void test_dummy_init_ptr_arg(void)
.ctx_in = args,
.ctx_size_in = sizeof(args),
);
struct trace_dummy_st_ops *trace_skel;
struct dummy_st_ops *skel;
int fd, err;

Expand All @@ -64,12 +66,33 @@ static void test_dummy_init_ptr_arg(void)
return;

fd = bpf_program__fd(skel->progs.test_1);

trace_skel = trace_dummy_st_ops__open();
if (!ASSERT_OK_PTR(trace_skel, "trace_dummy_st_ops__open"))
goto done;

err = bpf_program__set_attach_target(trace_skel->progs.fentry_test_1,
fd, "test_1");
if (!ASSERT_OK(err, "set_attach_target(fentry_test_1)"))
goto done;

err = trace_dummy_st_ops__load(trace_skel);
if (!ASSERT_OK(err, "load(trace_skel)"))
goto done;

err = trace_dummy_st_ops__attach(trace_skel);
if (!ASSERT_OK(err, "attach(trace_skel)"))
goto done;

err = bpf_prog_test_run_opts(fd, &attr);
ASSERT_OK(err, "test_run");
ASSERT_EQ(in_state.val, 0x5a, "test_ptr_ret");
ASSERT_EQ(attr.retval, exp_retval, "test_ret");
ASSERT_EQ(trace_skel->bss->val, exp_retval, "fentry_val");

done:
dummy_st_ops__destroy(skel);
trace_dummy_st_ops__destroy(trace_skel);
}

static void test_dummy_multiple_args(void)
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/map_ptr_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ static inline int check_array_of_maps(void)

VERIFY(check_default(&array_of_maps->map, map));
inner_map = bpf_map_lookup_elem(array_of_maps, &key);
VERIFY(inner_map != 0);
VERIFY(inner_map != NULL);
VERIFY(inner_map->map.max_entries == INNER_MAX_ENTRIES);

return 1;
Expand All @@ -394,7 +394,7 @@ static inline int check_hash_of_maps(void)

VERIFY(check_default(&hash_of_maps->map, map));
inner_map = bpf_map_lookup_elem(hash_of_maps, &key);
VERIFY(inner_map != 0);
VERIFY(inner_map != NULL);
VERIFY(inner_map->map.max_entries == INNER_MAX_ENTRIES);

return 1;
Expand Down
21 changes: 21 additions & 0 deletions tools/testing/selftests/bpf/progs/trace_dummy_st_ops.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

int val = 0;

SEC("fentry/test_1")
int BPF_PROG(fentry_test_1, __u64 *st_ops_ctx)
{
__u64 state;

/* Read the traced st_ops arg1 which is a pointer */
bpf_probe_read_kernel(&state, sizeof(__u64), (void *)st_ops_ctx);
/* Read state->val */
bpf_probe_read_kernel(&val, sizeof(__u32), (void *)state);

return 0;
}

char _license[] SEC("license") = "GPL";
Loading

0 comments on commit 8e9d0d7

Please sign in to comment.