From: Mykyta Yatsenko Inline values have no selem, and the list of an object can be empty when all its values are inline. A request that names maps finds them with the lookup, so it must not stop at an empty list. A dump of all maps walks the list only: let it also walk the table of live inline maps. Both walks skip a map that goes away (id 0): user space cannot name it. Signed-off-by: Mykyta Yatsenko --- net/core/bpf_sk_storage.c | 48 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index d0eaf4de8090..cae3606609ae 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -529,7 +529,17 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) } EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc); -static int diag_get(struct bpf_local_storage_map *smap, +/* + * Map free can set the map id to 0 at any time, so read it once. User space + * cannot name a map with id 0: skip it. + */ +static u32 diag_map_id(struct bpf_local_storage_map *smap) +{ + return smap ? READ_ONCE(smap->map.id) : 0; +} + +/* id is the map id that the caller read once */ +static int diag_get(struct bpf_local_storage_map *smap, u32 id, struct bpf_local_storage_data *sdata, struct sk_buff *skb) { struct nlattr *nla_stg, *nla_value; @@ -541,7 +551,7 @@ static int diag_get(struct bpf_local_storage_map *smap, if (!nla_stg) return -EMSGSIZE; - if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, smap->map.id)) + if (nla_put_u32(skb, SK_DIAG_BPF_STORAGE_MAP_ID, id)) goto errout; nla_value = nla_reserve_64bit(skb, SK_DIAG_BPF_STORAGE_MAP_VALUE, @@ -572,16 +582,19 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb, /* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */ unsigned int diag_size = nla_total_size(0); struct bpf_local_storage *sk_storage; + struct bpf_local_storage_data *sdata; struct bpf_local_storage_elem *selem; struct bpf_local_storage_map *smap; struct nlattr *nla_stgs; unsigned int saved_len; + u32 i, id, nr; int err = 0; rcu_read_lock(); sk_storage = rcu_dereference(sk->sk_bpf_storage); - if (!sk_storage || hlist_empty(&sk_storage->list)) { + /* The list can be empty when the values are inline */ + if (!sk_storage) { rcu_read_unlock(); return 0; } @@ -594,15 +607,34 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb, saved_len = skb->len; hlist_for_each_entry_rcu(selem, &sk_storage->list, snode) { smap = rcu_dereference(SDATA(selem)->smap); - if (!smap) + id = diag_map_id(smap); + if (!id) continue; diag_size += nla_value_size(smap->map.value_size); - if (nla_stgs && diag_get(smap, SDATA(selem), skb)) + if (nla_stgs && diag_get(smap, id, SDATA(selem), skb)) /* Continue to learn diag_size */ err = -EMSGSIZE; } + /* + * Inline values have no selem: find them through the inline maps. A + * plain object holds no slot. + */ + nr = bpf_local_storage_has_inline(sk_storage) ? + BPF_LOCAL_STORAGE_INLINE_MAPS : 0; + for (i = 0; i < nr; i++) { + smap = rcu_dereference(sk_cache.inline_maps[i]); + id = diag_map_id(smap); + sdata = id ? bpf_local_storage_inline(sk_storage, smap) : NULL; + if (!sdata || !bpf_local_storage_inline_valid(sdata, smap)) + continue; + diag_size += nla_value_size(smap->map.value_size); + + if (nla_stgs && diag_get(smap, id, sdata, skb)) + err = -EMSGSIZE; + } + rcu_read_unlock(); if (nla_stgs) { @@ -644,7 +676,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag, rcu_read_lock(); sk_storage = rcu_dereference(sk->sk_bpf_storage); - if (!sk_storage || hlist_empty(&sk_storage->list)) { + /* The list can be empty when the values are inline */ + if (!sk_storage) { rcu_read_unlock(); return 0; } @@ -665,7 +698,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag, diag_size += nla_value_size(diag->maps[i]->value_size); - if (nla_stgs && diag_get((struct bpf_local_storage_map *)diag->maps[i], sdata, skb)) + if (nla_stgs && diag_get((struct bpf_local_storage_map *)diag->maps[i], + diag->maps[i]->id, sdata, skb)) /* Continue to learn diag_size */ err = -EMSGSIZE; } -- 2.53.1-Meta