Commit 11a695a8 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'Bug fix and test case for special map value field '

Xu Kuohai says:

====================

This series is a follow-up to [0]. patch 1 updates sk_storage_map_test to
ensure special map value fields are not copied between user and kernel.
patch 0 fixes a bug found by the updated test.

[0] https://lore.kernel.org/bpf/1ca2e4e8-ed7e-9174-01f6-c14539b8b8b2@huawei.com/
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 6f04180c d59d3b8a
...@@ -74,7 +74,7 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, ...@@ -74,7 +74,7 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner,
gfp_flags | __GFP_NOWARN); gfp_flags | __GFP_NOWARN);
if (selem) { if (selem) {
if (value) if (value)
memcpy(SDATA(selem)->data, value, smap->map.value_size); copy_map_value(&smap->map, SDATA(selem)->data, value);
return selem; return selem;
} }
......
...@@ -458,7 +458,7 @@ static void test_sk_storage_map_basic(void) ...@@ -458,7 +458,7 @@ static void test_sk_storage_map_basic(void)
struct { struct {
int cnt; int cnt;
int lock; int lock;
} value = { .cnt = 0xeB9f, .lock = 0, }, lookup_value; } value = { .cnt = 0xeB9f, .lock = 1, }, lookup_value;
struct bpf_map_create_opts bad_xattr; struct bpf_map_create_opts bad_xattr;
int btf_fd, map_fd, sk_fd, err; int btf_fd, map_fd, sk_fd, err;
...@@ -483,38 +483,41 @@ static void test_sk_storage_map_basic(void) ...@@ -483,38 +483,41 @@ static void test_sk_storage_map_basic(void)
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value, err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value,
BPF_F_LOCK); BPF_F_LOCK);
CHECK(err || lookup_value.cnt != value.cnt, CHECK(err || lookup_value.lock || lookup_value.cnt != value.cnt,
"bpf_map_lookup_elem_flags(BPF_F_LOCK)", "bpf_map_lookup_elem_flags(BPF_F_LOCK)",
"err:%d errno:%d cnt:%x(%x)\n", "err:%d errno:%d lock:%x cnt:%x(%x)\n",
err, errno, lookup_value.cnt, value.cnt); err, errno, lookup_value.lock, lookup_value.cnt, value.cnt);
/* Bump the cnt and update with BPF_EXIST | BPF_F_LOCK */ /* Bump the cnt and update with BPF_EXIST | BPF_F_LOCK */
value.cnt += 1; value.cnt += 1;
value.lock = 2;
err = bpf_map_update_elem(map_fd, &sk_fd, &value, err = bpf_map_update_elem(map_fd, &sk_fd, &value,
BPF_EXIST | BPF_F_LOCK); BPF_EXIST | BPF_F_LOCK);
CHECK(err, "bpf_map_update_elem(BPF_EXIST|BPF_F_LOCK)", CHECK(err, "bpf_map_update_elem(BPF_EXIST|BPF_F_LOCK)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value, err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value,
BPF_F_LOCK); BPF_F_LOCK);
CHECK(err || lookup_value.cnt != value.cnt, CHECK(err || lookup_value.lock || lookup_value.cnt != value.cnt,
"bpf_map_lookup_elem_flags(BPF_F_LOCK)", "bpf_map_lookup_elem_flags(BPF_F_LOCK)",
"err:%d errno:%d cnt:%x(%x)\n", "err:%d errno:%d lock:%x cnt:%x(%x)\n",
err, errno, lookup_value.cnt, value.cnt); err, errno, lookup_value.lock, lookup_value.cnt, value.cnt);
/* Bump the cnt and update with BPF_EXIST */ /* Bump the cnt and update with BPF_EXIST */
value.cnt += 1; value.cnt += 1;
value.lock = 2;
err = bpf_map_update_elem(map_fd, &sk_fd, &value, BPF_EXIST); err = bpf_map_update_elem(map_fd, &sk_fd, &value, BPF_EXIST);
CHECK(err, "bpf_map_update_elem(BPF_EXIST)", CHECK(err, "bpf_map_update_elem(BPF_EXIST)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value, err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value,
BPF_F_LOCK); BPF_F_LOCK);
CHECK(err || lookup_value.cnt != value.cnt, CHECK(err || lookup_value.lock || lookup_value.cnt != value.cnt,
"bpf_map_lookup_elem_flags(BPF_F_LOCK)", "bpf_map_lookup_elem_flags(BPF_F_LOCK)",
"err:%d errno:%d cnt:%x(%x)\n", "err:%d errno:%d lock:%x cnt:%x(%x)\n",
err, errno, lookup_value.cnt, value.cnt); err, errno, lookup_value.lock, lookup_value.cnt, value.cnt);
/* Update with BPF_NOEXIST */ /* Update with BPF_NOEXIST */
value.cnt += 1; value.cnt += 1;
value.lock = 2;
err = bpf_map_update_elem(map_fd, &sk_fd, &value, err = bpf_map_update_elem(map_fd, &sk_fd, &value,
BPF_NOEXIST | BPF_F_LOCK); BPF_NOEXIST | BPF_F_LOCK);
CHECK(!err || errno != EEXIST, CHECK(!err || errno != EEXIST,
...@@ -526,22 +529,23 @@ static void test_sk_storage_map_basic(void) ...@@ -526,22 +529,23 @@ static void test_sk_storage_map_basic(void)
value.cnt -= 1; value.cnt -= 1;
err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value, err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value,
BPF_F_LOCK); BPF_F_LOCK);
CHECK(err || lookup_value.cnt != value.cnt, CHECK(err || lookup_value.lock || lookup_value.cnt != value.cnt,
"bpf_map_lookup_elem_flags(BPF_F_LOCK)", "bpf_map_lookup_elem_flags(BPF_F_LOCK)",
"err:%d errno:%d cnt:%x(%x)\n", "err:%d errno:%d lock:%x cnt:%x(%x)\n",
err, errno, lookup_value.cnt, value.cnt); err, errno, lookup_value.lock, lookup_value.cnt, value.cnt);
/* Bump the cnt again and update with map_flags == 0 */ /* Bump the cnt again and update with map_flags == 0 */
value.cnt += 1; value.cnt += 1;
value.lock = 2;
err = bpf_map_update_elem(map_fd, &sk_fd, &value, 0); err = bpf_map_update_elem(map_fd, &sk_fd, &value, 0);
CHECK(err, "bpf_map_update_elem()", "err:%d errno:%d\n", CHECK(err, "bpf_map_update_elem()", "err:%d errno:%d\n",
err, errno); err, errno);
err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value, err = bpf_map_lookup_elem_flags(map_fd, &sk_fd, &lookup_value,
BPF_F_LOCK); BPF_F_LOCK);
CHECK(err || lookup_value.cnt != value.cnt, CHECK(err || lookup_value.lock || lookup_value.cnt != value.cnt,
"bpf_map_lookup_elem_flags(BPF_F_LOCK)", "bpf_map_lookup_elem_flags(BPF_F_LOCK)",
"err:%d errno:%d cnt:%x(%x)\n", "err:%d errno:%d lock:%x cnt:%x(%x)\n",
err, errno, lookup_value.cnt, value.cnt); err, errno, lookup_value.lock, lookup_value.cnt, value.cnt);
/* Test delete elem */ /* Test delete elem */
err = bpf_map_delete_elem(map_fd, &sk_fd); err = bpf_map_delete_elem(map_fd, &sk_fd);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment