From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 29F5C34251B for ; Mon, 3 Aug 2026 11:25:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785756339; cv=none; b=XQQZ1nHsVkB2jEAwuQyMtv6JiPbA+fog8KnwmUuMcyyPRYsC0k3XNHNhtDqiCNze2UQzj1PP0xLWy0+xeIqjh87kIRwDy3If8H0apRUrbu79DXZpEMz9Y019nK0FPD9j8d98CCNZ+Uo275VDK16b4UB+t9E7NRYVHT48cmWPxHg= 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.177 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-f177.google.com with SMTP id d9443c01a7336-2cc7ef7ec27so35640475ad.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=KTn69NFARaGp4/Vz8heBX9BqsA991kH03slvDmwr8P2S97bW+DFvK1ySbyeewqi2tB YjJGN0EZpwQ05rDPZBdT3V8xY6rOkSOF4eW3nD65q6DY17mm8SxgpKEF1eBNIBdvVnuL XBJZ2njuG96NrXTLpMWognMKI/cm6oCWqLXJE+Gcv+YxsPTpjYzfotBDhsJHP57TAnLX YuSnOwAfUFPQvgfmpyvpAO7cnIY7ZWBVbRE5niZTTnR87rFHhDPn4IzGdbmF1qnRJV+S eH+sPz4ABK0lHHihdVmEuL/YtXli0PobfsImaIMsNmhKTKq5/vp5CFXFGIwwefusy0ZH AjUg== X-Forwarded-Encrypted: i=1; AHgh+RpOMTs+vy3tommfVih5/PM5XRUfV75L5POtS1Qe+MLEUZhDfoCj6gkGFswkX87kr3G91qfjn5T5I5tssoo=@vger.kernel.org X-Gm-Message-State: AOJu0YwGaElIqqJS3uyc/Ow6wydfnD0ZMYAA52f4H23z2e0KzOJWjj0A 9paqJg6mdfyj7CgKoBxN8y2YWsC1k2weaqqKt1EkWQUlAw8cuMxkcGSe X-Gm-Gg: AR+sD11zn5Qlak895kQof4cRApH3Fv6ohDIdTkrk2b9ilTvAhs90IJqmUVKCe+PoGED AhiRxNJVgX9z4D9a5ARdUvrnpEXIm4Lsga+biT2+whptr0T1qnobiVURs2eeXVgWSTc+7HMD84l GmHzaY05Vj97RvcbT91RJvuqN200kPcjhjxT6DV4X+j/ALVcyyFMDDSCEOW5G/FofEW2hzXS+7P watwS/juJ8PVEKn7YP1NelwaX/442AE0AOrlCi7oYfz7ZjYlqWQRnioOq77TcpIminw//hnje+G U5QVLtCUWmCqpbd0xQyP8py1xBV2c0b/nEu2CqqRaCCvJ/vlq4oNTPX799aZZaYPJ3locvxO64L 42GqDP3SXInMoX4jXJzEiiTHu6Y2qxjIbUTp8OnXr/s2AownSV8mnWN/b1bvDxVu7DtpgV0k6op vZp9cCf5Tq57ZRaOkSu+w55hAD1z6AoO0Qf6FUno6lN+1MMOHUC2e0pbeHV8yUnbGxLJvYVSgmV ocenDERs0ZDYkV5qviE8YvEUVg= 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-kernel@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