BPF List
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>,
	Dave Marchevsky <davemarchevsky@meta.com>
Subject: [PATCH bpf-next v9 02/23] bpf: Do btf_record_free outside map_free callback
Date: Fri, 18 Nov 2022 04:24:49 +0530	[thread overview]
Message-ID: <20221117225510.1676785-3-memxor@gmail.com> (raw)
In-Reply-To: <20221117225510.1676785-1-memxor@gmail.com>

Since the commit being fixed, we now miss freeing btf_record for local
storage maps which will have a btf_record populated in case they have
bpf_spin_lock element.

This was missed because I made the choice of offloading the job to free
kptr_off_tab (now btf_record) to the map_free callback when adding
support for kptrs.

Revisiting the reason for this decision, there is the possibility that
the btf_record gets used inside map_free callback (e.g. in case of maps
embedding kptrs) to iterate over them and free them, hence doing it
before the map_free callback would be leaking special field memory, and
do invalid memory access. The btf_record keeps module references which
is critical to ensure the dtor call made for referenced kptr is safe to
do.

If doing it after map_free callback, the map area is already freed, so
we cannot access bpf_map structure anymore.

To fix this and prevent such lapses in future, move bpf_map_free_record
out of the map_free callback, and do it after map_free by remembering
the btf_record pointer. There is no need to access bpf_map structure in
that case, and we can avoid missing this case when support for new map
types is added for other special fields.

Since a btf_record and its btf_field_offs are used together, for
consistency delay freeing of field_offs as well. While not a problem
right now, a lot of code assumes that either both record and field_offs
are set or none at once.

Note that in case of map of maps (outer maps), inner_map_meta->record is
only used during verification, not to free fields in map value, hence we
simply keep the bpf_map_free_record call as is in bpf_map_meta_free and
never touch map->inner_map_meta in bpf_map_free_deferred.

Add a comment making note of these details.

Fixes: db559117828d ("bpf: Consolidate spin_lock, timer management into btf_record")
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 kernel/bpf/arraymap.c |  1 -
 kernel/bpf/hashtab.c  |  1 -
 kernel/bpf/syscall.c  | 18 ++++++++++++++----
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 672eb17ac421..484706959556 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -430,7 +430,6 @@ static void array_map_free(struct bpf_map *map)
 			for (i = 0; i < array->map.max_entries; i++)
 				bpf_obj_free_fields(map->record, array_map_elem_ptr(array, i));
 		}
-		bpf_map_free_record(map);
 	}
 
 	if (array->map.map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 50d254cd0709..5aa2b5525f79 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -1511,7 +1511,6 @@ static void htab_map_free(struct bpf_map *map)
 		prealloc_destroy(htab);
 	}
 
-	bpf_map_free_record(map);
 	free_percpu(htab->extra_elems);
 	bpf_map_area_free(htab->buckets);
 	bpf_mem_alloc_destroy(&htab->pcpu_ma);
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 8eff51a63af6..4c20dcbc6526 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -659,14 +659,24 @@ void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
 static void bpf_map_free_deferred(struct work_struct *work)
 {
 	struct bpf_map *map = container_of(work, struct bpf_map, work);
+	struct btf_field_offs *foffs = map->field_offs;
+	struct btf_record *rec = map->record;
 
 	security_bpf_map_free(map);
-	kfree(map->field_offs);
 	bpf_map_release_memcg(map);
-	/* implementation dependent freeing, map_free callback also does
-	 * bpf_map_free_record, if needed.
-	 */
+	/* implementation dependent freeing */
 	map->ops->map_free(map);
+	/* Delay freeing of field_offs and btf_record for maps, as map_free
+	 * callback usually needs access to them. It is better to do it here
+	 * than require each callback to do the free itself manually.
+	 *
+	 * Note that the btf_record stashed in map->inner_map_meta->record was
+	 * already freed using the map_free callback for map in map case which
+	 * eventually calls bpf_map_free_meta, since inner_map_meta is only a
+	 * template bpf_map struct used during verification.
+	 */
+	kfree(foffs);
+	btf_record_free(rec);
 }
 
 static void bpf_map_put_uref(struct bpf_map *map)
-- 
2.38.1


  parent reply	other threads:[~2022-11-17 22:55 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17 22:54 [PATCH bpf-next v9 00/23] Allocated objects, BPF linked lists Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 01/23] bpf: Fix early return in map_check_btf Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` Kumar Kartikeya Dwivedi [this message]
2022-11-17 22:54 ` [PATCH bpf-next v9 03/23] bpf: Free inner_map_meta when btf_record_dup fails Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 04/23] bpf: Populate field_offs for inner_map_meta Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 05/23] bpf: Introduce allocated objects support Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 06/23] bpf: Recognize lock and list fields in allocated objects Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 07/23] bpf: Verify ownership relationships for user BTF types Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 08/23] bpf: Allow locking bpf_spin_lock in allocated objects Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 09/23] bpf: Allow locking bpf_spin_lock global variables Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 10/23] bpf: Allow locking bpf_spin_lock in inner map values Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 11/23] bpf: Rewrite kfunc argument handling Kumar Kartikeya Dwivedi
2022-11-17 22:54 ` [PATCH bpf-next v9 12/23] bpf: Support constant scalar arguments for kfuncs Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 13/23] bpf: Introduce bpf_obj_new Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 14/23] bpf: Introduce bpf_obj_drop Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 15/23] bpf: Permit NULL checking pointer with non-zero fixed offset Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 16/23] bpf: Introduce single ownership BPF linked list API Kumar Kartikeya Dwivedi
2022-11-18  0:12   ` Alexei Starovoitov
2022-11-17 22:55 ` [PATCH bpf-next v9 17/23] bpf: Add 'release on unlock' logic for bpf_list_push_{front,back} Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 18/23] bpf: Add comments for map BTF matching requirement for bpf_list_head Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 19/23] selftests/bpf: Add __contains macro to bpf_experimental.h Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 20/23] selftests/bpf: Update spinlock selftest Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 21/23] selftests/bpf: Add failure test cases for spin lock pairing Kumar Kartikeya Dwivedi
2022-11-17 22:55 ` [PATCH bpf-next v9 22/23] selftests/bpf: Add BPF linked list API tests Kumar Kartikeya Dwivedi
2022-11-17 23:05   ` Alexei Starovoitov
2022-11-17 23:27     ` Dave Marchevsky
2022-11-17 23:58       ` Kumar Kartikeya Dwivedi
2022-11-18  0:05         ` Alexei Starovoitov
2022-11-17 23:29     ` Yonghong Song
2022-11-17 22:55 ` [PATCH bpf-next v9 23/23] selftests/bpf: Add BTF sanity tests Kumar Kartikeya Dwivedi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221117225510.1676785-3-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davemarchevsky@meta.com \
    --cc=martin.lau@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox