From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (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 7BED93E2ACC for ; Fri, 10 Jul 2026 08:00:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783670443; cv=none; b=KgDOsrUyVuXQgVR21TkaQYf+rDWYpUBKF8emQAjJjWEmH17TFrDFso1467fjo3BSSMhbPoxuG3C6YfkfdGWfNWwAyXqMWpoCu/SDnaltN3wBiWPmZdyHcA8Svs4itGkUiZYrm9L30Q0FdQXOZ+VilgByxL4bkpMK4SnTvO+/ccQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783670443; c=relaxed/simple; bh=pUv5EBpxAawNlfz6TfnjQheGLcQNgrCAdjNiETQ0vk4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Nh2xz4fgCycB6RFVnY/BpNRKCVKUc++N0Br8qArnxOX+OFqlN+CpJuxNwgdi7yhUL40Bz1iTyxx/UAESPRyzoxy+fegO0SDWCtCKOskq4zfz4E2Ww/gHpvTGMvlkogcxAGFMrQU9lXPha90QrTxxZskhHsqRVeDJgDTgWULcZhs= 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=Y920Gq/0; arc=none smtp.client-ip=209.85.208.51 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="Y920Gq/0" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-698aa7ba3e6so1171970a12.1 for ; Fri, 10 Jul 2026 01:00:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1783670440; x=1784275240; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=7NAnOHyxoVPYpVNY9WSaqu/dYx79TRxCODHHV36sDoU=; b=Y920Gq/0Is1xsQO+gQ+JFNtT4wO3meRW+Uik2KUObvjEIWQtY4ue4cyttMWXC2KPhn 69yP0fK3c7WImd5NAVtMZzL05ajttAGSkaaqa2bOGC3842Ed3tAxkiH7jBEIZssTkfpT WGcS8LP1omfdw+Apf1YIHFtwJ1XA3lkdpiqvInVBojAs/oWGdgB/rUd0hPF+JjH1unkR pjVP/OUtR39Xi1s9ZPu+ZG2bQCls/D/XMfiz1dFjXpIi2WLio9Zr/Bz8cz4WGJN9A95T rYPu1KQYyzhre6ByPj1LzyUD1gyctCneqEmyfcqK2uGiZ717gcoz1Egngbz9B0PEXo3T NOBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783670440; x=1784275240; h=in-reply-to:content-transfer-encoding:content-disposition :content-type: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:content-type; bh=7NAnOHyxoVPYpVNY9WSaqu/dYx79TRxCODHHV36sDoU=; b=hPy7FjavOOHssJl/EcO3tzWOOMYU3/F2B7admltBNiuGsgBOwbTt7NV6ajjrCtQL+5 2z0bHZa6gWe8LjGzWQYVKOQWswsRRigrIZSthJ5f4zNgmkK24DTQ+iH3dpcCx/LY63IP b9qArpaOzYLmvmK1+KQ2UMih9vYWpftgofwQXa+ZyHJVIk6usE3D+85NUPuu98KOMb7J QznyJkhGNDz4seHCAqWociGPnqAj/3dBVmMwgS2JO/BIJtljl9Rf+02VBzjXRlloS25L SEV9dt03e0ldVP+E4W8cibhCeZwDssUG7RNCEARcZdPw9pp8ZkYaN81W4wd/Lab7RUri QnAA== X-Forwarded-Encrypted: i=1; AHgh+Rq738MC8Eg9bDYVb6PpB+/uqa5RG+vC47Q9zbSE7LBFuBy8UqMnV4QKtIZKYCEi1xt7vASc45n6wIAqcaQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxD045IKP8YPrDSu17R0lhHbiFaIZZzEiQbOvRDf2yw+ROkHN2K gjouq+N9FQQ8Um2hYpX0c1gOZEPmob0OCKh86M39kvi/sDVuCx70F39D8dBJl7Rr1+M= X-Gm-Gg: AfdE7cnyJh3EQxyvrTcy6751uItYBFjWWGRuPXI+z0Lv0VMhj0efC1pkczfI2mHbvE5 GNL/BWBfwmOjdAzpYF1fhLn63VbCDm+SLpY7F0fFHrF9VrZTGndAEqHUzbfgdDOgWqi/e7nknaK 0N66RPYMPRA15yFe5dES70j3+UhxWVqpnvWjYR1s5Kg6b3g9aSsjv3HDwbEHUHM2WdGPeTAYGoF BL4Grbwe6X1CpoRp2hBEG/AERSkvrj2AL++GgkV9p00oka85ZV69dKOizAnS2JiHWZ6bF15w4au Dh+ys+LmvwuAjYXvEOyHJTjjBodCRfNHHuLpJ9KKjMMqZtSNmljzhH3//ygofMXFxU4+hW9dmim zi/uD0cjShicEKx2olrVqIsw2aVhKZukjZR+fD0BAsxqPjxtGIwI4htFkF/K4VDVt3Aj0gOlG1m UoJmXziWncTFX1d8i81yyAwzMdBJwiN5w8ydI= X-Received: by 2002:a17:907:6093:b0:c15:d5f0:dbfd with SMTP id a640c23a62f3a-c15d5f0e8afmr428879566b.62.1783670438684; Fri, 10 Jul 2026 01:00:38 -0700 (PDT) Received: from u94a (27-53-185-121.adsl.fetnet.net. [27.53.185.121]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9bf8f26sm56534285ad.27.2026.07.10.01.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2026 01:00:37 -0700 (PDT) Date: Fri, 10 Jul 2026 16:00:27 +0800 From: Shung-Hsi Yu To: sun jian , Eduard Zingerman Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, 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 v4 1/2] bpf: Reject negative const offsets for buffer pointers Message-ID: References: <20260708090151.151729-1-sun.jian.kdev@gmail.com> <20260708090151.151729-2-sun.jian.kdev@gmail.com> <1f8aad47f61fbf85eb55276e4bb01bfcff61393e.camel@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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Fri, Jul 10, 2026 at 01:52:26PM +0800, sun jian wrote: > On Fri, Jul 10, 2026 at 2:21 AM Eduard Zingerman wrote: > > On Thu, 2026-07-09 at 14:47 +0800, Shung-Hsi Yu wrote: > > > On Wed, Jul 08, 2026 at 10:11:01PM +0800, sun jian wrote: [...] > > > Actually looking again at 022ac0750883, moving the `off < 0` after > > > tnum_is_const() and bringing back the `off += reg->off` removed from > > > check_mem_access() is perhaps the more faithful restoration of the > > > original behavior. > > > > > > Though reg->off no longer exists, we have to use reg->var_off.value > > > instead. IIUC any register of pointer type should already have its > > > var_off bounded to +-BPF_MAX_VAR_OFF by adjust_ptr_min_max_vals() in > > > theory, and thus shouldn't overflow `int off`. > > > > > > See the diff below. [...] > > > > I don't understand what this patch is attempting to fix. > > If you run the selftests from patch #2 against current bpf-next both > > would be rejected. If you extend these test cases to exercise a truly > > negative offset, that would be rejected as well. I tried to exercise a truly negative offset, and it was indeed rejected on bpf-next. I had thought it would pass. But more below. > Thanks for pushing on this. I rechecked the issue more carefully. > > This series targets the bpf tree, with base 12091470c6b4. On that base, > with only the selftest change applied, the negative-offset verifier case > is not rejected at load time. The test fails with an unexpected load > success: > > #664/2 verifier_raw_tp_writable/raw_tracepoint_writable: reject > negative const offset:FAIL > run_subtest:FAIL:unexpected_load_success unexpected success: 0 > > It is possible that current bpf-next rejects this earlier through another > path, but on the target bpf base it does not. So I think I now have a better picture of what's going on. Before 022ac0750883, negative const offset is rejected at *load* time because the const offset is accumulated into `off` before the negative check: // check_mem_access() off += reg->off // __check_buffer_access() if (off < 0) return return -EACCES; if (!tnum_is_const(reg->var_off) || reg->var_off.value) return -EACCES; After 022ac0750883 the const offset accumulation does not happen, and thus now negative const offset is no longer rejected at load time. // __check_buffer_access() if (off < 0) return return -EACCES; if (!tnum_is_const(reg->var_off)) return -EACCES; > > And this does not rely on UB. > > Consider the current code: > > > > env->prog->aux->max_tp_access = max(reg->var_off.value + off + size, > > env->prog->aux->max_tp_access); > > > > > > The types of the expressions involved: > > > > reg->var_off.value + off + size > > u64 int int > > > > The promotion/conversion rules: > > > > u64 + (u64)(s64)int -> u64 > > > > In other words, 'off' and 'size' would be sign extended to s64 and > > then treated as u64. Hence any negative offset would be represented > > as a large unsigned value in max_tp_access. I entirely missed that, yes, the attached-time check does prevented the negative offset going through. So there isn't a bug, but the behavior has changed, and it seems better to restore to the one where we straightout reject negative offset during load time. That can be done by reordering `off < 0` check after tnum_is_const(), similar to how check_ptr_to_btf_access() was update in commit 022ac0750883. [...]