From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 613794C7F for ; Thu, 9 Nov 2023 06:36:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Knsk2FZN" Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B381326B0 for ; Wed, 8 Nov 2023 22:36:19 -0800 (PST) Message-ID: <6125c508-82fe-37a4-3aa2-a6c2727c071b@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1699511777; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ICVEYfnNgGy9vxtKj3QeCNP+EarIDdX8hEsb4xbm+qg=; b=Knsk2FZNdFZqwnfwJlS+IePFB2e8RkxfWetdvOerR/UA8ZrBgBPT1IPNNEBb2jYKmMPqlx NEavvUCHV7V6KI8nKO9XeTqtoYUXbiVLEE7yFBb50MV63KClHeFkkrgxkqy+ph/SkfJI/b xaSppLe23Iy6qS1RjTnplYSXWKGTFgI= Date: Wed, 8 Nov 2023 22:36:10 -0800 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH bpf 05/11] bpf: Add bpf_map_of_map_fd_{get,put}_ptr() helpers Content-Language: en-US To: Hou Tao Cc: Alexei Starovoitov , Andrii Nakryiko , Song Liu , Hao Luo , Yonghong Song , Daniel Borkmann , KP Singh , Stanislav Fomichev , Jiri Olsa , John Fastabend , houtao1@huawei.com, bpf@vger.kernel.org References: <20231107140702.1891778-1-houtao@huaweicloud.com> <20231107140702.1891778-6-houtao@huaweicloud.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: <20231107140702.1891778-6-houtao@huaweicloud.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On 11/7/23 6:06 AM, Hou Tao wrote: > From: Hou Tao > > bpf_map_of_map_fd_get_ptr() will convert the map fd to the pointer > saved in map-in-map. bpf_map_of_map_fd_put_ptr() will release the > pointer saved in map-in-map. These two helpers will be used by the > following patches to fix the use-after-free problems for map-in-map. > > Signed-off-by: Hou Tao > --- > kernel/bpf/map_in_map.c | 51 +++++++++++++++++++++++++++++++++++++++++ > kernel/bpf/map_in_map.h | 11 +++++++-- > 2 files changed, 60 insertions(+), 2 deletions(-) > > diff --git a/kernel/bpf/map_in_map.c b/kernel/bpf/map_in_map.c > index 8323ce201159d..96e32f4167c4e 100644 > --- a/kernel/bpf/map_in_map.c > +++ b/kernel/bpf/map_in_map.c > @@ -4,6 +4,7 @@ > #include > #include > #include > +#include > > #include "map_in_map.h" > > @@ -139,3 +140,53 @@ u32 bpf_map_fd_sys_lookup_elem(void *ptr) > { > return ((struct bpf_map *)ptr)->id; > } > + > +void *bpf_map_of_map_fd_get_ptr(struct bpf_map *map, struct file *map_file, > + int ufd) > +{ > + struct bpf_inner_map_element *element; > + struct bpf_map *inner_map; > + > + element = kmalloc(sizeof(*element), GFP_KERNEL); > + if (!element) > + return ERR_PTR(-ENOMEM); > + > + inner_map = bpf_map_fd_get_ptr(map, map_file, ufd); > + if (IS_ERR(inner_map)) { > + kfree(element); > + return inner_map; > + } > + > + element->map = inner_map; > + return element; > +} > + > +static void bpf_inner_map_element_free_rcu(struct rcu_head *rcu) > +{ > + struct bpf_inner_map_element *elem = container_of(rcu, struct bpf_inner_map_element, rcu); > + > + bpf_map_put(elem->map); > + kfree(elem); > +} > + > +static void bpf_inner_map_element_free_tt_rcu(struct rcu_head *rcu) > +{ > + if (rcu_trace_implies_rcu_gp()) > + bpf_inner_map_element_free_rcu(rcu); > + else > + call_rcu(rcu, bpf_inner_map_element_free_rcu); > +} > + > +void bpf_map_of_map_fd_put_ptr(void *ptr, bool need_defer) > +{ > + struct bpf_inner_map_element *element = ptr; > + > + /* Do bpf_map_put() after a RCU grace period and a tasks trace > + * RCU grace period, so it is certain that the bpf program which is > + * manipulating the map now has exited when bpf_map_put() is called. > + */ > + if (need_defer) "need_defer" should only happen from the syscall cmd? Instead of adding rcu_head to each element, how about "synchronize_rcu_mult(call_rcu, call_rcu_tasks)" here? > + call_rcu_tasks_trace(&element->rcu, bpf_inner_map_element_free_tt_rcu); > + else > + bpf_inner_map_element_free_rcu(&element->rcu); > +} > diff --git a/kernel/bpf/map_in_map.h b/kernel/bpf/map_in_map.h > index 63872bffd9b3c..8d38496e5179b 100644 > --- a/kernel/bpf/map_in_map.h > +++ b/kernel/bpf/map_in_map.h > @@ -9,11 +9,18 @@ > struct file; > struct bpf_map; > > +struct bpf_inner_map_element { > + struct bpf_map *map; > + struct rcu_head rcu; > +}; > + > struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd); > void bpf_map_meta_free(struct bpf_map *map_meta); > -void *bpf_map_fd_get_ptr(struct bpf_map *map, struct file *map_file, > - int ufd); > +void *bpf_map_fd_get_ptr(struct bpf_map *map, struct file *map_file, int ufd); > void bpf_map_fd_put_ptr(void *ptr, bool need_defer); > u32 bpf_map_fd_sys_lookup_elem(void *ptr); > > +void *bpf_map_of_map_fd_get_ptr(struct bpf_map *map, struct file *map_file, int ufd); > +void bpf_map_of_map_fd_put_ptr(void *ptr, bool need_defer); > + > #endif