From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (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 ACB2F3A2E12 for ; Thu, 13 Aug 2026 21:16:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786655802; cv=none; b=pNwO7oh1exncga5aI8wwE0XLd4dXUAXnkP8Y6zOH9i+wkPt4zlW8gj9bkYrdnmjue1u6vaRgiDskj+WLPyXQIdH4TDlfUdx2UpB7g4e1eN7jLPNb4Swp6sGD1wlcEr920lzO0WNv5VI8o2YwgjdjCdgi11ew/dKJuyFyPL+g4qo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786655802; c=relaxed/simple; bh=sAusXOdwVtn6WF9NS8nJSIu1JOY/31ExSEj2AGQ5TPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ETeYBg0Qmwovui1gtgJP5QpHYBKf5OoDgfEKWHQHhDK9gyOpCzbIv3WOZDj9huj/JYgs+EzbcW1RgcpsxHbwy1lPVyCYILf1ZllTh6MMc+U+qPu7SInFuZe7edexJMFaPYTXlWE/HXX/EMBvEJoi98ujRmTTSu2DKRCQNH5mKag= 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=dNVXmvHB; arc=none smtp.client-ip=209.85.216.54 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="dNVXmvHB" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-3811f512167so341581a91.3 for ; Thu, 13 Aug 2026 14:16:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786655795; x=1787260595; 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=iPxZbADhGWNayp+pc6kSWXesxZmSrTwPZr9c/8FZz0Y=; b=dNVXmvHB2zMB6zNV/+Xx4F9O48yXwH1CWjVd0ObJ99iA9O7F+vxQyEDUQtVURRNiwJ y83k7SPLRpX13O7PZjR2Srp8hs0W+n9Shbe4oK3KXpFTlHcsNK5I4+N9eCS17GE5mKEg KDRZNPu2r2Y9VnbGPgq0JnnIXdwB9wt+hxsl4uhcgHQeScXM2tb5AtX7TTALNEy5FRvV zkwfNXqwVhJd41RwDHmLS3cIyYCIzzzPy16n+yK3ZRun10i+FqiwZw3sPfV4EZaXu2hh AtSMTsFuj83VGG0fqXdedGd0vsCNKCYNoX12QBB6Tu4SgBWpxywUnsMfKdYykA08fsUb X/lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786655795; x=1787260595; 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=iPxZbADhGWNayp+pc6kSWXesxZmSrTwPZr9c/8FZz0Y=; b=qG77okVUcqUfXZkzrfv7QQbJ1eaj/kLAg/SStEwGYYFLVOw2LOYcMaZNIv9ih+qunS RYVL5M6KhJClyUL+3wDIZsfh/b8J+90lfv2AnscfnVufo1Rb3P/UTE8eHoWd8KPv+vSx /H925dklsJF7oGPgM3KmAcQ6Wum6Q2uQWGCP1U3vgrYdUg6nSOayywUiJGyp3fbSjOsA gSyxN+bo23kAHzFsEZnvIIPWPptq+CJrsNFMK4GYivm9112kFnh6pJ/UJsT2FqQzSyvS IpKTW17vjl5P+JwwzbAwkHvgGXroJYxOxPc499ykr+ZoVmmHh33RgGb7GA3OCDQ8ZMZS rXWQ== X-Forwarded-Encrypted: i=1; AHgh+Rq7/Hk7QJPB+d8EsZ+c6KsMYaHQ2Xtm+egwwLr2yMo+LO/OZEyVFLuLIOSHP8161g51uptklHtm1q4inMI=@vger.kernel.org X-Gm-Message-State: AOJu0Ywp0tRiGfpB3hIiSdx90HKkbqR7fiqc1UBNS+G8ROpI82kQv/Sp Ds1tnLqOIFOW8jDeQVm48Ct7yGfOwoZk/p1W23AQr08Y2jX81QnHk3Jl X-Gm-Gg: AR+sD13dvCol3JmU6EzNZadXyiQJ+fkfQQqzaeSavj4yMvSSCZPNHkCM2ZFwjd7qA90 S9Kj0m020bAS2H7Ouw8hD8dniwZfObH79AjFtB/ijel6v7QrPtktOX9HEV+0kSEA4bqrYKHv2BE uOvU7Waadk3+qhqPqu+aV7uBPqh7cy2DU00JXQUXBs/QJUFFilsSvxvlPHHUB2WvVZDrLNbnAcj Ez4tvuLf7OkgY+IM+UBAYN1VvMUxhTTJw783BwrpnwCvnIEZF+e/LKOLvuQK0TznnankXMPPIsH R1wCPnXIq1hxCzQUP7oao/8012vhzouvk3bla7lx/0GRKyewCyokzHJPwbHlU4/E/j56Ip3hDSH tumGimAl5B08UMMsQa81HHadMBw6XmZ3/3ZTTxNhv4deaunEDDry3pto98qW+mD+fiBMN0DHEwh sEhCXNxX/v1Re43CJ9dfmbY7js3sN28JyzPMbENOlsASdamZdTz37dN4ekvrUduLOpB7Pa+5iE5 sf786xa5m90sYMu X-Received: by 2002:a17:90b:3d8b:b0:38e:1497:af5b with SMTP id 98e67ed59e1d1-3933b77ece8mr1072781a91.1.1786655794693; Thu, 13 Aug 2026 14:16:34 -0700 (PDT) Received: from patterson.cs.ubc.ca (patterson.cs.ubc.ca. [198.162.52.65]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3933a03e288sm853985a91.17.2026.08.13.14.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 14:16:34 -0700 (PDT) From: Ning Ding To: bpf@vger.kernel.org Cc: memxor@gmail.com, greg@kroah.com, dingning04@gmail.com, Andrii Nakryiko , Eduard Zingerman , Ihor Solodrai , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Shuah Khan , Justin Suess , Kaitao Cheng , Viktor Malik , Leon Hwang , Yiyang Chen , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v4 2/4] selftests/bpf: Test refcount_acquire return nullability Date: Thu, 13 Aug 2026 14:15:24 -0700 Message-ID: <20260813211533.290256-3-dingning04@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260813211533.290256-1-dingning04@gmail.com> References: <20260813211533.290256-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 61906f48025c..fd35093285c0 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 024ef2aae200..acd3e81a3916 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