From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 6A8B93BA22E for ; Tue, 7 Jul 2026 06:52:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783407167; cv=none; b=qoVhnsngS3wfYGfSSRimoqBjNY135lKEwPYDqRnUZk/KNMDlYiAjtPPjdkbusKD+EZPJRzwP5QIimmB3I/Ssw3t69NuBXzHOyYNZe7Uf1D9+jKAAdSoF1gS065A0QQnl9Lhx97EslW3BoC94rjlB//kQVHHlEtc2g1wKo7O2nzc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783407167; c=relaxed/simple; bh=mhdDcd6URVxpfo91I5Qyv3Eb0UvP2jLGecqvmCd3mrA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WM7VQQC+r7T8gHPTllLHWK5F53GwbGWXaTzWWkAzJsbn6rfM7/wmA9FxzD0qvVLVVJW0wnu93IjeKGiMkyA6yUoD3pyU+73JjChmW38Z1CEDtr01yrno9MzGo9KtUFP+qzYexPZ4Zxgl6GwREcIrOotMGi9u6+Qo0woadRvLhwc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=dgJ02Vs/; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="dgJ02Vs/" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4938d5f86f3so1702565e9.1 for ; Mon, 06 Jul 2026 23:52:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1783407159; x=1784011959; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=WfzRI5F82+Jj3jQo8VQaqjjU0jNv4SWrjulOFBbp/Nk=; b=dgJ02Vs/aRyieeeK5ePOM2d98uTWuFifBd+sPhhc8pihWuf+7J0agaZSMnoceFoNBo /7tV5CCHriCFZHuGMNeITuOuu+uAfAQYDRExfkSpQOIGhHfvRrIMsG+kZOIg3PV3QW/e mEc/TaVgEbmTnJBbUtGeynCpV2OedgqMKkhXmRiG8TgG3JQ4Xk23R4YlbSvJtVQGfTlS U37GazADHuElg3+f9HaCWIIkglJ2F1yvARnBMEaqjB4o9u6R8rzsbUvs4hRwcVPHoFFI a2a522l+yr+zCnUJCUWvpwkYgGyKSldIZdPMaJ8jPyeyxGaMvfPOIRvQuHSHQgk/VqZz jJTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783407159; x=1784011959; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WfzRI5F82+Jj3jQo8VQaqjjU0jNv4SWrjulOFBbp/Nk=; b=FS7q/k5gIzpPqeMu0KEN366Lkhg4m5Yd8aXAlOEBDQZHmP0Gq//17gqo8WDoySRQzV 5SPK9BCPnk0/oztmnP4QIdMaHi0NyaCzrtMSN8mVaKmeScdO3VS0l2jXJiA4Glen3ePz nB7CtMlOIKur/GIK4Yf7/UmMvu0jl7tdwudx04lrKJncubfd0FXzeFd28mgKcBJlo70+ cMtjNJVwoUjq0ZOHM75iTksqZhtcGs2wuXDjRwxutwe8ZrTuimlZ9nERyzHgOjbftL7u yg/g6SnAW6PMPt1e1YX6DlwpdIDSPw6fUe0aHksTYF/UZvqmoo8eq1TwidMiek6L3lko YGtg== X-Forwarded-Encrypted: i=1; AHgh+Roa96qO8D9u2xYCZIvbHBjZQarv0VGXTbihX+LUd+DRrHjxc10e2iOO3aa+unKOIASaPPFN7m0/+G07Uew=@vger.kernel.org X-Gm-Message-State: AOJu0YzcrLD+CTAlnY95Uf6hZBgFCN6DixaLAii/RYLbj5gEBhgD81E7 z/2wsmWdOSdVBeRDiBKlR2mbVU6axRsWWXh3/7dSYBVScbbuUeZ1rA5UXdy9BjbkFr0= X-Gm-Gg: AfdE7cnGRFYpE77ewqi2SMZJrSHBOSOR/CQR3FFvk4hAkEP1AJGhLhozlD4aGATmJVV MASOk6Jpg3JnqJYNkhKB6+S+x8Sm38cCCeL7bR2eMlwnxZJvwR6Be2KC6IfaCVD6PStzDngwtVQ WINDR/JDl3o0QmxpdVSEXHxX6NZP8RFoH3bgx5pxxPhG+QQnz8b0qJClbCrI3LM7p/etLYjkxwc mihK9/qrsHT4TelU6aOZkSjxWbZMx8F2WRbGEB6It0tgUo0WIvm7Tytr22Odeg6KZBl3p7LAKOs K9FVEiFPeKRQfZnG0+lJWhTQQ+bTU1csN/CtV2pWG4vJuSpcyRP2VEFizvf0CiAB0q4lxYJnO/B XjsJATuU2+0YQ/nhil/ng2ngmpYlQrxLyzIrKpT8zAk5NCETvY/tfleBmDaM29A6CJ1owFSBKL7 BOXTEoAVPwFX0JpV7MlMpLSwnadZbJkiW+ufBhxieB9WHd1Q== X-Received: by 2002:a05:600c:2284:b0:493:ca5d:b9b3 with SMTP id 5b1f17b1804b1-493e1029736mr8337345e9.12.1783407159009; Mon, 06 Jul 2026 23:52:39 -0700 (PDT) Received: from u94a (27-53-112-249.adsl.fetnet.net. [27.53.112.249]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9d1e279sm6305125ad.45.2026.07.06.23.52.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 23:52:37 -0700 (PDT) Date: Tue, 7 Jul 2026 14:52:24 +0800 From: Shung-Hsi Yu To: Sun Jian Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, shuah@kernel.org, mmullins@fb.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH bpf] bpf: Reject negative const offsets for buffer pointers Message-ID: References: <20260703035137.109608-1-sun.jian.kdev@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260703035137.109608-1-sun.jian.kdev@gmail.com> On Thu, Jul 02, 2026 at 08:51:37PM -0700, Sun Jian wrote: [...] > @@ -5344,6 +5348,29 @@ static int __check_buffer_access(struct bpf_verifier_env *env, > return -EACCES; > } > > + var_off = (s64)reg->var_off.value; > + if (check_add_overflow(var_off, (s64)off, &start)) { > + verbose(env, > + "%s invalid %s buffer access: off=%d, var_off=%lld\n", > + reg_arg_name(env, argno), buf_info, off, var_off); > + return -EACCES; > + } Other pointer offset checks (check_mem_region_access and adjust_ptr_min_max_vals) seem to check var_off against BPF_MAX_VAR_OFF, and that `off` being >= 0. It would be great if the convention can be kept for consistency, albeit being more conservative. >From a blind guess I would guess that compiler does not product a pattern where `off` encoded in the instruction is negative but the net overall offset is positive for PTR_TO_TP_BUFFER/PTR_TO_BUF, and rejecting such pattern is fine. r6 = *(u64 *)(r1 + 0); r6 += 16; r0 = *(u64 *)(r6 - 8); One more comment below. > + > + if (start < 0) { > + verbose(env, > + "%s invalid negative %s buffer offset: off=%d, var_off=%lld\n", > + reg_arg_name(env, argno), buf_info, off, var_off); > + return -EACCES; > + } > + > + if (start > U32_MAX || size < 0 || > + check_add_overflow((u32)start, (u32)size, access_end)) { > + verbose(env, > + "%s invalid %s buffer access: off=%lld, size=%d\n", > + reg_arg_name(env, argno), buf_info, start, size); > + return -EACCES; > + } If we check that var_off is within BPF_MAX_VAR_OFF and that `off` is not negative, then `var_off + off + size` should be guaranteed to not overflow max_tp_access, since the maximum of `off` is S16_MAX and the maximum of `size` is 8. > + > return 0; > } [...]