From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E8D23B6C18 for ; Mon, 3 Aug 2026 11:25:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785756339; cv=none; b=TYLo903hUHPVvvulHtkQx3A8xgYBSLEMRmeakO3UwqnIAB2F4jMYBbdFKcDrwb1GlhyicnqGFGmGE4ybloaT4CNS4qqtGS/PA4B6IQBmMsgG4NLJyLt9gt27rJ5AxOa/ZfLtqFI+cvy7GG0eCL4XDfzGYE9oMdmk0MXeOhgkjVM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785756339; c=relaxed/simple; bh=3D5E0Lg5JRXAFqtPciAWQdBsJA6gxPLeVVy3eBFgJWw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RzwKhWWI/sg6eV98iYJTU6KJQzRbpOFJKm+wuEq8Xr71ih2xmYmF3c9QYiBHmHxIN129vXQACsn4ujITUDM0R6bDHU+n1KfnGkdfc0TsPijWx9QLHBS8LqnAI1/ZgYgSNxrMx9BSswJvBZua/hmFK+LL8XdLCFY+A11s7TwEdgQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bmk9Wrpk; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bmk9Wrpk" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2cc7ef7ec27so35640495ad.1 for ; Mon, 03 Aug 2026 04:25:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785756337; x=1786361137; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Ff8M+EQ6msWxWzt89+Hwii7UDIx1WOzy+ofanQLYJRM=; b=bmk9WrpkeR/pHSdMq+DQUhUKI25DBP9CqCMreF9NyDwkTE4jDS+ZqKz3I9yTrMbvUc fYFgauIGuym/mVVFU1lDg9KPvgy2t39N4nOwZRJPrziG37Yj8gcMyl/TIYeF7zJPMsJO dBCeYamCWdJAWT8wagm+ED0CL9VqN9xUWSLvio+hYjrLZbAiolWT+aK2+fddOQcdmPos AXv/Zf6b7cKbgF/BZFpRF5DCBREy3e9IgsBlsS48wr9x7how26Y4zzwIDhohnC1KwAvr L6H05UwrE0gwEtufEQh5QK1FZQNs/KeS06uylRCcs04X7YeUtmVmd0q2qX2HuNZch/Xb UWhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785756337; x=1786361137; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Ff8M+EQ6msWxWzt89+Hwii7UDIx1WOzy+ofanQLYJRM=; b=jVCaexHRlMCfyGRvuc1J/TsjuXZCzuy8E/WLulSA2+WQSkNSqpNjwpTLDQu1KaTl3T fbtxtlGFtNvkiv98G12FQ6Ylq1SPdNogX7O6Lf0/qWYjx1o6Fpk5vbuh0FXvug7kfRJD UuwKcVleCKk7F1MJ8l3SNzX7oGbiwWLsEqSxcXjzWOoeQCUMOt7NyKLJXP/YEGig9eW6 cCdZA56OeOdENIA8YA2oTlx0fkosR/3ABFmG7CnK+WpxpoIq4joI2G4+8oIMvLbtyJr9 rnXQId88RxTF2BbIrCXvtgCV7zkpraFoH0cQZyt0WwyNd/PbSvd0Gko1CgKysXsQGWPw k7QQ== X-Forwarded-Encrypted: i=1; AHgh+RrPYFytyR3UmwG9rJOJ52mK+TFyD6go22DP7k28B+Rhprd8rw3T++NCPM0wK4zS+awnynE8imZSTQnhVKjsWKU=@vger.kernel.org X-Gm-Message-State: AOJu0YxBQ+FYuCLcNJU8WPMxDvmdqbwBFKw96ndvd8FjYP4LrhdEG0z/ HC9fIvbyRVYG7cW530Br0PjFMcX+7DnlPrDPHayuc2HwWp3NoRq/7IJg X-Gm-Gg: AR+sD124lMZUnCgZIwptDHNtlPwhG8/MJiappT7HPZ4zn7S7upgD+oelyG5D+Fqf1QE noJVEe32RIeN9L31047vAwFoUKMkTYx6US53EsUS8pLxL555SgwmK8gkSEIgenNgQvkdZcY482R l8NNxJUpQvT8V4kzkTcErFIAPQt/q/82iYjr7kpZ4YdjLpFnkU9hIne5dO95k4XiA0xbVQruHvJ 3RVdQ9SJHaIQGlvi83R+2airthvkoX5VxbjyfSfqpvSpt2P5XuLk0D6OjJPH2KLNe/1wcq2TtfP 3qcxL3rQEiG0CZL2nt+/sGwqYYKq9InpRz3C6tq6JHXsT9zMYqCRcqiJFay4dCrVFMfM5O6iUBx rBjeo8mxLfE0RaBUbUqK0Kyp9moor3Y2OucsNwF3rp5qKWv2QrG6ueJcadvQPqL23KYfs8tQ5bR SRXEJvHGHVYdhPXfK6M8+iEoPt/ckSVeQYlolvrh8yqaW2X9QE+s3pAAnU1mty9xzBZNwz8eWOh VfJ1adtSjZpFDkqYC/sXLq87j4= X-Received: by 2002:a17:902:e950:b0:2c6:90ec:f601 with SMTP id d9443c01a7336-2d05219d40emr97049545ad.8.1785756337361; Mon, 03 Aug 2026 04:25:37 -0700 (PDT) Received: from patterson.cs.ubc.ca (patterson.cs.ubc.ca. [198.162.52.65]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04ae5a91asm36190685ad.21.2026.08.03.04.25.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 04:25:36 -0700 (PDT) From: Ning Ding To: bpf@vger.kernel.org Cc: memxor@gmail.com, greg@kroah.com, Ning Ding , Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Shuah Khan , Viktor Malik , Justin Suess , Leon Hwang , Kaitao Cheng , Yiyang Chen , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf v3 2/4] selftests/bpf: Test refcount_acquire return nullability Date: Mon, 3 Aug 2026 04:22:09 -0700 Message-ID: <20260803112218.3361213-3-dingning04@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260803112218.3361213-1-dingning04@gmail.com> References: <20260803112218.3361213-1-dingning04@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The verifier could accept an unchecked bpf_refcount_acquire() result for a borrowed RCU-loaded map kptr. If the call returns NULL, passing the result to bpf_obj_drop() can crash the kernel. Add tests showing that an owned input remains non-NULL, a checked borrowed result is accepted, and an unchecked borrowed result is rejected. Assisted-by: Codex:gpt-5.5 Assisted-by: ChatGPT:GPT-5.6-Thinking Signed-off-by: Ning Ding --- .../selftests/bpf/progs/refcounted_kptr.c | 61 +++++++++++++++++++ .../bpf/progs/refcounted_kptr_fail.c | 47 ++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/testing/selftests/bpf/progs/refcounted_kptr.c index 61906f48025cc..fd35093285c0d 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -23,6 +23,15 @@ struct map_value { struct node_data __kptr *node; }; +struct node_refcount_only { + long key; + struct bpf_refcount refcount; +}; + +struct map_value_refcount_only { + struct node_refcount_only __kptr *node; +}; + struct { __uint(type, BPF_MAP_TYPE_ARRAY); __type(key, int); @@ -30,6 +39,13 @@ struct { __uint(max_entries, 2); } stashed_nodes SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, int); + __type(value, struct map_value_refcount_only); + __uint(max_entries, 1); +} stashed_refcount_only SEC(".maps"); + struct node_acquire { long key; long data; @@ -832,6 +848,51 @@ long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx) return 0; } +SEC("tc") +__success +long refcount_acquire_owning_input_no_null_check(void *ctx) +{ + struct node_refcount_only *n, *m; + + n = bpf_obj_new(typeof(*n)); + if (!n) + return 1; + + m = bpf_refcount_acquire(n); + bpf_obj_drop(m); + bpf_obj_drop(n); + + return 0; +} + +SEC("tc") +__success +long refcount_acquire_rcu_map_kptr_null_checked(void *ctx) +{ + struct map_value_refcount_only *mapval; + struct node_refcount_only *n, *m; + int idx = 0; + + mapval = bpf_map_lookup_elem(&stashed_refcount_only, &idx); + if (!mapval) + return 1; + + bpf_rcu_read_lock(); + n = mapval->node; + if (!n) { + bpf_rcu_read_unlock(); + return 2; + } + m = bpf_refcount_acquire(n); + bpf_rcu_read_unlock(); + + if (!m) + return 3; + bpf_obj_drop(m); + + return 0; +} + static long __stash_map_empty_xchg(struct node_data *n, int idx) { struct map_value *mapval = bpf_map_lookup_elem(&stashed_nodes, &idx); diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c b/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c index 024ef2aae2008..acd3e81a39168 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c @@ -19,6 +19,15 @@ struct node_refcounted { struct bpf_refcount refcount; }; +struct node_refcount_only { + long key; + struct bpf_refcount refcount; +}; + +struct map_value_refcount_only { + struct node_refcount_only __kptr *node; +}; + extern void bpf_rcu_read_lock(void) __ksym; extern void bpf_rcu_read_unlock(void) __ksym; @@ -28,6 +37,13 @@ private(A) struct bpf_rb_root groot __contains(node_acquire, node); private(B) struct bpf_spin_lock lock; private(B) struct bpf_list_head head __contains(node_refcounted, list); +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, int); + __type(value, struct map_value_refcount_only); + __uint(max_entries, 1); +} stashed_refcount_only SEC(".maps"); + static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b) { struct node_acquire *node_a; @@ -80,6 +96,37 @@ long refcount_acquire_maybe_null(void *ctx) return 0; } +SEC("?tc") +__failure __msg("Possibly NULL pointer passed to trusted R1") +long refcount_acquire_rcu_map_kptr_unchecked_drop(void *ctx) +{ + struct map_value_refcount_only *mapval; + struct node_refcount_only *tmp, *n, *m; + int idx = 0; + + tmp = bpf_obj_new(typeof(*tmp)); + if (!tmp) + return 3; + bpf_obj_drop(tmp); + + mapval = bpf_map_lookup_elem(&stashed_refcount_only, &idx); + if (!mapval) + return 1; + + bpf_rcu_read_lock(); + n = mapval->node; + if (!n) { + bpf_rcu_read_unlock(); + return 2; + } + m = bpf_refcount_acquire(n); + bpf_rcu_read_unlock(); + + bpf_obj_drop(m); + + return 0; +} + SEC("?tc") __failure __msg("Unreleased reference id=3 alloc_insn={{[0-9]+}}") long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx) -- 2.43.0