From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 BF6BC3F483D for ; Mon, 3 Aug 2026 11:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785756342; cv=none; b=A14F/6IBOz+O9EpOjPzoc8K8vpnstTPJs+B50CYBv/1B0K4pCVbeZPtISzW/w/XOb0JXBT/jNiqrAx1ywv1/RVWqSlpGNcsonj6W+9hWFB313ulNzS1a22y+83pmk9NNQnzd2T5Jtk4NBVhr8mO/sNGbokAadbgYTodTWJDHZnA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785756342; c=relaxed/simple; bh=Sv49XeCz+zvRsKncftIKHLkr4SWJDrNLElmC9AiNb6Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fMtKDGlVemfzSn0UbhixV6Aum0+saiLAhZHWhMsV2GUmlW/aoU6ClJgvRfMegp4cUotArzK1I9S5rHDvqgxb1lQ+L4T/nIIosyoUyOzoDnGuTQ8IfR9u3kEtSa7SdXhjoDAE+vZ6mMSzCOP6waFIL9k5Xsz219mTx5wtQeNRvLs= 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=UYBNDD59; arc=none smtp.client-ip=209.85.214.182 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="UYBNDD59" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2ceab75934dso36872505ad.2 for ; Mon, 03 Aug 2026 04:25:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785756340; x=1786361140; 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=8dm8iv1zOENNE2/4OPaqxGyIJkWC3+aS57l3uyF+i70=; b=UYBNDD59RB0/ZMKLGrbAoa8X927gIhnznrGMDSJQvehxOexQQrm43S55D4doc/HQDx 7ZFAFDQ9iK9q5FAPvjIO+IGO3JuiUHs89+PH350XdcAiHlSB3Q1waWDK5iMuD4PrTbKc rLHXZ9v4SRjYB+c8ENTTCYKerLznLzUkd3GTdGFaH4aANxicirjDfWjlM7NpFBS+bJvQ 5KtbqoV4dNfB3VGj2N3OtgFC1yVqRSTox9vxl1quHfRgKgFeS0oU4ZDjkCuBZ+2rBUS5 rL7mwegTifc5DegPpSsDAqXXVH7R1E4rZaErwwTaD2JpH/ETcuyJeaYyf1QOxvS9GzBB wySA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785756340; x=1786361140; 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=8dm8iv1zOENNE2/4OPaqxGyIJkWC3+aS57l3uyF+i70=; b=m/s7NGJlTVB0wDBD+w8Nx+OSVUZeBrjpg2oglkK41Ae/ZJKxHgGB23bkOA8p2STgpQ 4YlQKMwkj6px2CM/dGSsUOsV8QDhP7psRB4UdJf7JIzoJAhVvloCu/vCTCOu62qPlB1Y 0ib5nKPLORoGp1LFRarn2z/CrdN9kovSUIpzbsveBvJ76DfeY0p8QmQ7D2KIv+eXZKk4 D50XVqDz5Un0ykWKeT9fSuQ623sacBtzph+blBez69gzmSAmGXRl/Q5Ka19s3UXl5z8m o0C3kSxDvN2mG3SY3LL8WKlVPJcX4HYJ1978ppHNC1QJVI0AzITEruwwBsYLyK+iyYhc iFYA== X-Forwarded-Encrypted: i=1; AHgh+RpaDk+63fv6+nCoXO79t7svMIxazC5fO8jXB6NPo12A54V65eucSQWxm1ExznVsJUffdF8R70wFg2a58aU=@vger.kernel.org X-Gm-Message-State: AOJu0YwEL5rgAYv6CpCYM8q3M0yaxkeNvD/80PBOarE4ib/ZhJsfbUBA W2vEdluNKDkkhhcwriXMN09L/ZrDklgp+DIaUJ1wFCmZVpZptEXX6P/n X-Gm-Gg: AR+sD11XxPMLWkEkimaiO/ycP7U30Sea6811wE3rGHNGWa+47Zg3AX1nzs8a2u7NvlA n3NfSJnnBIteVlRtqe9kkiolHDw21MmFF8JY6ek9JPskVCLSXaL9rYGXQWcZfsQNG/T8V93nPfN 8lq7pWuEPOWkmIBADDXKF4lPuseTtDnDdEKKy/a9vaPFLkLAMQ8IxYYQgHHZ6e+QZbPrh6WPtY7 1fkg1hO9HdMd4NyRrHFiojDmTL0IL4GTrsTHIGrb9AodqxW5jTOUfdDjG5uyPNZ679kAoEfqd7B MXQNoiESoXxuzdiBkMs10SSq1OjeR91NcPRYyWtIjLboJxRKLeXg1uDY8htct+SgyWJQtKQTCUu SgzSRZvruZptUxaQcSI3rOr7fZ5qIVd5iS5ouixhjAMjplExbxaWwNMqbFIyyLXlH8hHI6iOHac hAtQQhSKZo6hboc8tPBmAmCK8WbDn0OHn0urPqohyzeNMtSNhyhEIJcs22pUQn2g2PMj9rpZToz +us9dtk8PG3rWEL X-Received: by 2002:a17:903:2f0e:b0:2cc:fe03:dc75 with SMTP id d9443c01a7336-2d05246fd98mr93329845ad.27.1785756340137; Mon, 03 Aug 2026 04:25:40 -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.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 04:25:39 -0700 (PDT) From: Ning Ding To: bpf@vger.kernel.org Cc: memxor@gmail.com, greg@kroah.com, Ning Ding , sashiko-bot@kernel.org, stable@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Dave Marchevsky , linux-kernel@vger.kernel.org Subject: [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers Date: Mon, 3 Aug 2026 04:22:10 -0700 Message-ID: <20260803112218.3361213-4-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 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 Cc: stable@vger.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 39a851e690ec4..7014d62fe3d1c 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1307,7 +1307,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 c47328be2505e..8b35c63780798 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5809,7 +5809,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