From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 EA13E43C7B4 for ; Thu, 13 Aug 2026 21:16:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786655808; cv=none; b=nRGTEN+xUAx+4ICgg38hCA3x2GW2MM6IerJt3i2tZivWApPdltI32GO1hcgpVWakvtRW7kTRRohRikcgegnA1Wia1ORtQDBLsJNN+BXao0w1/vsNi4BR2t8kqitAdycBidwrcsSKc2fz3fXNugsoTN/yH+ELNl26gn+O7ndBiLE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786655808; c=relaxed/simple; bh=w45m0VNF5Uf8+j350TYz8zJo1MuxvnK/HCXDK5CiZ08=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GaFKUiKWu4WvjUby2UhMNbSY/DVl7546LGbiQS/rCYhV/RfYVww9bXi9aJ1enecY2nK5wYrld9XSGmQrY47OXO2Ng9t/Yoeyh+1O2Oi6LjWudaQIi/7iWvJCTDz8EuwhMsBhSdAPM/bU+357v8tii7P17XIuXG15AXtRmJEnUo8= 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=VDtz+rmE; arc=none smtp.client-ip=209.85.214.176 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="VDtz+rmE" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2d0407aedd6so4947495ad.0 for ; Thu, 13 Aug 2026 14:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786655797; x=1787260597; 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=ww3gNAYbZCYlPca0Huv8ZGVtJX4fMyNTAxRFW4a4o4Y=; b=VDtz+rmEOpzG/ePJF1GBz7h18arWZh6+PbzB9R7mPAfiMa+lZkuRkADMoza2c8pEpi bbDvjT0Vd67ALjcryxo+WVxye5HpCADsFDb7loVyE4lVKkw/dplqixrb9kyzQIEGje62 YIa8TWS/lJs1PuzPfmsBIJmnpiQ6ottS2VOZYWHq0LgG+1ZVtpu+7yFBw1T2GG54kP9Q uEuZdE/Mr8UKojfQDmdhcpSeSQz8bsPmY0fbLTEE8+tvaboIeH7vk90HqzLkD46V9EH5 qMpEGt+9uW2FinSCin1zLFbG4DWYf9IGL0+Czz/vjFsAjBr+mDfSGLC5k+KlfBZBmfd9 IJyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786655797; x=1787260597; 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=ww3gNAYbZCYlPca0Huv8ZGVtJX4fMyNTAxRFW4a4o4Y=; b=baToNsxKH/RNK71zjj95iRJOZwoLEx/f7m2MY9eLfL9/lhFpaqqHbBwYUkC4T2R246 ycLvTbRkpk1eWlSTc5S2J9atRhDjU1J1Bncxwz/oZ5zPDAs2HJMuyD+2dR1cJMysLI3J et6X0EtDnIiiKSzgfNNjsknWU1qD/u8waCihRruSuGcEUlvh0l77JA+HPVzsZbNJf6ia p9KXPwUou/SE5sKNpi6YNn3t2RVDydOA5Ccmla1jEoTK9h4oWM4rSm19Q/L/IsUd/GKt THLBTmFVmKLyvONTGuaeOGRVTUkuaCMZBJb4emcrUzYRWeWfAXDcLOYvdFWnRXZuoARr 7d1Q== X-Forwarded-Encrypted: i=1; AHgh+RpJ62MKmE7n3oV1kqxbdg64ckWRblxeNdwTx1xx1mTRWsCX30yikHFCtGFWcf0JpSJQO8NLUI/QxsVlF34=@vger.kernel.org X-Gm-Message-State: AOJu0YwnAEOebPFoK5ZcMtuiHnW1ThbnORVJH2jppB5N/EiyfWd20wEQ +Hwkkcd6CRF3LZOFOlyatqoVcw5zg4MhhAqVlmW0CM/2sI/rJtrFQe2g X-Gm-Gg: AR+sD12csjLsXt4KSYIIH8UXQ0sp3zX0huvRQ9w5+7PXV3oWfXbmxcL28ps04IdYKPC Rn9xQXh6VjyJ1O/LcDQwmmN9GsIUc05Olikbox/8zvNC50UJSBg+Hqou0ZoS9XbAoaDimoMQq7l sv4gXw9N6qjpSpv6tPnMOGGAFVpf0Uc5sCp4Nj3MbuMYZ6Y/B0loZFTT46QPdpk/9OEB3b0MUrs /9/5Q66tXG4HSa49ALwDCj97p5bSiFox9Ts4y/WPP/a5mj1VsPsAc0F1iuISncoHH1yNfd1v1qK bnszhwVPQ97mdChvCHZH4S/Hi4yq249Fdf9nqKALXPi7IKeOj33n203oYgqjEHtOwxGjuyMuSEf W+zAhrWhU8OXrnzguBS4bbm5ZFg6O8x8V7aArb11Vb86Oq38rSa9/Ug0DyMZI+OG5pLjljB4lVn jl3iajFOfpMHxSrHPeAtlT6lgsumFmniEgx8oh8NLotj50lIZ9IOa/NETLaQIzyMBJzJkw22g40 tF6fjd6FRCmzHK79QBlngvlgfQ= X-Received: by 2002:a17:90b:2f0d:b0:38e:7268:220d with SMTP id 98e67ed59e1d1-3933ed5e238mr829987a91.20.1786655797150; Thu, 13 Aug 2026 14:16:37 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 14:16:36 -0700 (PDT) From: Ning Ding To: bpf@vger.kernel.org Cc: memxor@gmail.com, greg@kroah.com, dingning04@gmail.com, sashiko-bot@kernel.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Dave Marchevsky , linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v4 3/4] bpf: Reject untrusted allocated-object pointers Date: Thu, 13 Aug 2026 14:15:25 -0700 Message-ID: <20260813211533.290256-4-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 type_is_ptr_alloc_obj() currently treats PTR_UNTRUSTED pointers as valid allocated objects. This allows a pointer that is no longer protected by RCU to be passed to bpf_refcount_acquire(). If the object was freed and its address reused, the verifier may acquire a reference through stale memory. Make type_is_ptr_alloc_obj() reject PTR_UNTRUSTED. Add type_is_untrusted_ptr_alloc_obj() for the read path, where these pointers are still allowed for BPF_PROBE_MEM reads. This keeps checks strict without breaking safe reads. Fixes: 1b12171533a9 ("bpf: Mark direct ld of stashed bpf_{rb,list}_node as non-owning ref") Reported-by: sashiko-bot@kernel.org Link: https://lore.kernel.org/r/20260726021304.97ED91F000E9@smtp.kernel.org Assisted-by: Codex:gpt-5 Signed-off-by: Ning Ding --- include/linux/bpf_verifier.h | 11 ++++++++++- kernel/bpf/verifier.c | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 27b43fda9b17..22fcd01c3cce 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1341,7 +1341,16 @@ static inline bool bpf_type_has_unsafe_modifiers(u32 type) static inline bool type_is_ptr_alloc_obj(u32 type) { - return base_type(type) == PTR_TO_BTF_ID && type_flag(type) & MEM_ALLOC; + return base_type(type) == PTR_TO_BTF_ID && + type_flag(type) & MEM_ALLOC && + !(type_flag(type) & PTR_UNTRUSTED); +} + +static inline bool type_is_untrusted_ptr_alloc_obj(u32 type) +{ + return base_type(type) == PTR_TO_BTF_ID && + type_flag(type) & MEM_ALLOC && + type_flag(type) & PTR_UNTRUSTED; } static inline bool type_is_non_owning_ref(u32 type) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index bc021c8c8fbf..c3a11c98cd45 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5820,7 +5820,13 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, return -EACCES; } - if (type_is_alloc(reg->type) && !type_is_non_owning_ref(reg->type) && + /* + * Skip this referenced-ID sanity check for untrusted allocated objects; + * the access check above already rejects writes through them. + */ + if (type_is_alloc(reg->type) && + !type_is_untrusted_ptr_alloc_obj(reg->type) && + !type_is_non_owning_ref(reg->type) && !(reg->type & MEM_RCU) && !reg_is_referenced(env, reg)) { verifier_bug(env, "allocated object must have a referenced id"); return -EFAULT; -- 2.43.0