Not a new issue. I guess it's time to close it.
How about something like this:
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 50b3f8ab9720..5562e5d746e7 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -1739,6 +1739,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
const struct bpf_prog *tgt_prog,
u32 btf_id,
struct bpf_attach_target_info *tgt_info);
+bool bpf_prog_args_freed(const struct bpf_prog *prog);
int bpf_check_ext_prologue(struct bpf_verifier_log *log,
const struct bpf_prog *tgt_prog, u32 btf_id);
void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab);
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index f2d2f4fdeca5..0e400d63a994 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7285,6 +7285,40 @@ static bool prog_args_trusted(const struct bpf_prog *prog)
}
}
+/* Functions that can free the socket that they take as an argument */
+static const char * const sk_free_funcs[] = {
+ "__sk_free",
+ "__vsock_release",
+ "dgram_close",
+ "l2tp_ip6_close",
+ "l2tp_ip_close",
+ "mctp_sk_close",
+ "pep_sock_close",
+ "ping_close",
+ "pn_sock_close",
+ "raw_close",
+ "rawv6_close",
+ "sctp_close",
+ "sk_common_release",
+ "sk_destruct",
+ "sk_free",
+ "sk_prot_free",
+ "sock_gen_put",
+ "udp_lib_close",
+ "unix_release_sock",
+};
+
+bool bpf_prog_args_freed(const struct bpf_prog *prog)
+{
+ enum bpf_attach_type atype = prog->expected_attach_type;
+
+ if (prog->type != BPF_PROG_TYPE_TRACING || prog->aux->saved_dst_prog_type ||
+ (atype != BPF_TRACE_FEXIT && atype != BPF_TRACE_FSESSION))
+ return false;
+ return match_string(sk_free_funcs, ARRAY_SIZE(sk_free_funcs),
+ prog->aux->attach_func_name) >= 0;
+}
+
int btf_ctx_arg_offset(const struct btf *btf, const struct btf_type *func_proto,
u32 arg_no)
{
@@ -7680,6 +7714,8 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
info->reg_type = PTR_TO_BTF_ID;
if (prog_args_trusted(prog))
info->reg_type |= PTR_TRUSTED;
+ else if (bpf_prog_args_freed(prog))
+ info->reg_type |= PTR_UNTRUSTED;
if (btf_param_match_suffix(btf, &args[arg], "__nullable"))
info->reg_type |= PTR_MAYBE_NULL;
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index facb0d6d2e8d..ec4dd50d2f1a 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -2881,6 +2881,8 @@ static u64 bpf_prog_hook_key(const struct bpf_prog *fp)
{
if (IS_ENABLED(CONFIG_BPF_SYSCALL) && fp->aux->attach_btf_trace)
return btf_raw_tp_null_mask(fp);
+ if (IS_ENABLED(CONFIG_BPF_SYSCALL) && bpf_prog_args_freed(fp))
+ return U64_MAX;
if (fp->type == BPF_PROG_TYPE_TRACING && fp->expected_attach_type != BPF_TRACE_ITER)
return fp->aux->saved_dst_ctx_type;
if (fp->type == BPF_PROG_TYPE_TRACING || fp->type == BPF_PROG_TYPE_LSM)
Re: [BUG] KASAN: slab-use-after-free in bpf_skc_to_* via fexit on sk_common_release
- From
- alex…@gmail.com ("Alexei Starovoitov")
- Organization
- Usenet Network
- Message-ID
- <DN92…@gmail.com>
- Date
- Newsgroups
- linux.kernel.netdev