From nobody Sun May 24 20:33:08 2026 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (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 EEA663EE1DA for ; Thu, 21 May 2026 14:29:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373779; cv=none; b=VBq/OIeWfd5svPF1891NXsZijGi+cObfUTJkwi3Aa5tk05eEGt6hFvpna0m1dWm8+yy+tLm/oqJ8yGDQCbLCHX3ssaWDU1fNIjqhMC57FB9Q7yvq9jp4mhWUEvGIoeE+QMjE/ttBFZk1NeCahyR9qpbacgLnHT0NwhePaF6j5mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373779; c=relaxed/simple; bh=L8X/Vtnahfr1UKdViXhLKWhf9qHUBPeIKLu97RI+aL8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=VnZYd20SurKSLfJm9jDM1hsVSSgO+EAuVcKwSVyBNijd45kVTGfYBAYbPwbw7ln6n/n2awJJ/aWjog6L3WDD3iitTeb5Le0v+PIl82VB8WFE9ECVPhfuITnnmPo/1GsTk6aRjLGHL4p+Guz8U6VZGzKKBpR4Ukwyy2BkvAKQfCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=pWkyhPoa; arc=none smtp.client-ip=91.218.175.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="pWkyhPoa" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779373775; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Hw2iSi5nOOVvlhwvxYfHRrDn76QQRLFbRQOPEaOi5gE=; b=pWkyhPoa58XOqB8Oismhu3yUX3jShmcFhalKAS/TS+Citaz/A1tJ+73T+8S/tJuoAbqs8N q9zI0SN6YFaVcx4J4m3dhRl9wpOZI3VDd4w/a6ZoCRe+pMzxbbd8qQsjTd9b9C++2T/Na4 1hly5Lsh2FI7jTKFVK1jwW+enKsbRdg= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Shuah Khan , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v2] bpf: Fix concurrent regression in map_create() Date: Thu, 21 May 2026 22:29:09 +0800 Message-ID: <20260521142909.95818-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Because there is time gap between bpf_map_new_fd() and close_fd(), a concurrent thread is able to close the new fd and opens a new, unrelated file with the exact same fd number. Thereafter, this close_fd() might inadvertently close the unrelated file. To avoid such regression, do finalize log before security_bpf_map_create(). However, in order to achieve it, move bpf_get_file_flag(), security_bpf_map_create(), bpf_map_alloc_id(), and bpf_map_new_fd() from __map_create() to map_create(). And, rename __map_create() to map_create_alloc() meanwhile. Then, in order to reuse the map and token when all checks pass in map_create_alloc(), pass "struct bpf_map **" and "struct bpf_token **" to map_create_alloc(). Fixes: 49f9b2b2a18c ("bpf: Add syscall common attributes support for map_cr= eate") Signed-off-by: Leon Hwang --- v1 -> v2: * Finalize log before security_bpf_map_create() (per Alexei). * v1: https://lore.kernel.org/bpf/20260518145446.6794-3-leon.hwang@linux.de= v/ --- kernel/bpf/syscall.c | 80 ++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 83de8fb9b9aa..28489e2fea04 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1359,7 +1359,8 @@ static int map_check_btf(struct bpf_map *map, struct = bpf_token *token, =20 #define BPF_MAP_CREATE_LAST_FIELD excl_prog_hash_size /* called via syscall */ -static int __map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_v= erifier_log *log) +static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct b= pf_verifier_log *log, + struct bpf_map **mapp, struct bpf_token **tokenp) { const struct bpf_map_ops *ops; struct bpf_token *token =3D NULL; @@ -1367,7 +1368,6 @@ static int __map_create(union bpf_attr *attr, bpfptr_= t uattr, struct bpf_verifie u32 map_type =3D attr->map_type; struct bpf_map *map; bool token_flag; - int f_flags; int err; =20 err =3D CHECK_ATTR(BPF_MAP_CREATE); @@ -1403,10 +1403,6 @@ static int __map_create(union bpf_attr *attr, bpfptr= _t uattr, struct bpf_verifie return -EINVAL; } =20 - f_flags =3D bpf_get_file_flag(attr->map_flags); - if (f_flags < 0) - return f_flags; - if (numa_node !=3D NUMA_NO_NODE && ((unsigned int)numa_node >=3D nr_node_ids || !node_online(numa_node))) { @@ -1598,6 +1594,49 @@ static int __map_create(union bpf_attr *attr, bpfptr= _t uattr, struct bpf_verifie goto free_map; } =20 + *mapp =3D map; + *tokenp =3D token; + return 0; + +free_map: + bpf_map_free(map); +put_token: + bpf_token_put(token); + return err; +} + +static int map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_com= mon_attr *attr_common, + bpfptr_t uattr_common, u32 size_common) +{ + struct bpf_token *token =3D NULL; + struct bpf_verifier_log *log; + struct bpf_log_attr attr_log; + struct bpf_map *map =3D NULL; + int err, ret; + int f_flags; + + log =3D bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, si= ze_common); + if (IS_ERR(log)) + return PTR_ERR(log); + + err =3D map_create_alloc(attr, uattr, log, &map, &token); + + /* preserve original error even if log finalization is successful */ + ret =3D bpf_log_attr_finalize(&attr_log, log); + if (ret) + err =3D ret; + + kfree(log); + + if (err) + goto free_map; + + f_flags =3D bpf_get_file_flag(attr->map_flags); + if (f_flags < 0) { + err =3D f_flags; + goto free_map; + } + err =3D security_bpf_map_create(map, attr, token, uattr.is_kernel); if (err) goto free_map_sec; @@ -1626,37 +1665,12 @@ static int __map_create(union bpf_attr *attr, bpfpt= r_t uattr, struct bpf_verifie free_map_sec: security_bpf_map_free(map); free_map: - bpf_map_free(map); -put_token: + if (map) + bpf_map_free(map); bpf_token_put(token); return err; } =20 -static int map_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_com= mon_attr *attr_common, - bpfptr_t uattr_common, u32 size_common) -{ - struct bpf_verifier_log *log; - struct bpf_log_attr attr_log; - int err, ret; - - log =3D bpf_log_attr_create_vlog(&attr_log, attr_common, uattr_common, si= ze_common); - if (IS_ERR(log)) - return PTR_ERR(log); - - err =3D __map_create(attr, uattr, log); - - /* preserve original error even if log finalization is successful */ - ret =3D bpf_log_attr_finalize(&attr_log, log); - if (ret) { - if (err >=3D 0) - close_fd(err); - err =3D ret; - } - - kfree(log); - return err; -} - void bpf_map_inc(struct bpf_map *map) { atomic64_inc(&map->refcnt); --=20 2.54.0