bpf: sockmap: Enable map_update_elem from bpf_iter
authorLorenz Bauer <lmb@cloudflare.com>
Mon, 28 Sep 2020 09:08:02 +0000 (10:08 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 28 Sep 2020 23:40:46 +0000 (16:40 -0700)
Allow passing a pointer to a BTF struct sock_common* when updating
a sockmap or sockhash. Since BTF pointers can fault and therefore be
NULL at runtime we need to add an additional !sk check to
sock_map_update_elem. Since we may be passed a request or timewait
socket we also need to check sk_fullsock. Doing this allows calling
map_update_elem on sockmap from bpf_iter context, which uses
BTF pointers.

Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200928090805.23343-2-lmb@cloudflare.com
kernel/bpf/verifier.c
net/core/sock_map.c

index b25ba98..cc9c90d 100644 (file)
@@ -3943,7 +3943,7 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,
        case BPF_MAP_TYPE_SOCKMAP:
        case BPF_MAP_TYPE_SOCKHASH:
                if (*arg_type == ARG_PTR_TO_MAP_VALUE) {
-                       *arg_type = ARG_PTR_TO_SOCKET;
+                       *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON;
                } else {
                        verbose(env, "invalid arg_type for sockmap/sockhash\n");
                        return -EINVAL;
index e1f05e3..08bc86f 100644 (file)
@@ -610,6 +610,9 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
        struct sock *sk = (struct sock *)value;
        int ret;
 
+       if (unlikely(!sk || !sk_fullsock(sk)))
+               return -EINVAL;
+
        if (!sock_map_sk_is_suitable(sk))
                return -EOPNOTSUPP;