From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 9A1EC2C15BE for ; Mon, 20 Jul 2026 18:47:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784573235; cv=none; b=N07MpjHIkAdzslOnuFQH06wifDSt8G08qlJHcy5TLmkSyLBhnTgnkkiM4nXG7VrJ1nmrk0FT60csbCEtuqYPqgDQxYPHGMARtn/i/YvQTo84vjDcWqhA4paxu2pTn4J9Vt0BGEryWc5QXN0V60tPcIHw06fxyaMXmeeOrqVvtp0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784573235; c=relaxed/simple; bh=2JEVIKRjuogw3CiJj97/97MbKmbSMBHFdJ8CWOymg3Y=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=rl+vclgoY0mvRyDl3eV7BSUtsZVKyro71/pJZjjqdKNE/qFsp0jG4dl2PRnnvS5pn7GSQJ8MHIE/2VJjVQd8PO2V7m3679HFhOcjgSrqE/PAFXaznFAQxBBBIiRy8AzBIlHSEK9sUY7o6D5ijTpYy5FPXBxX1v1lxqm1Vho6uoM= 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=c7WTa0jS; arc=none smtp.client-ip=209.85.216.53 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="c7WTa0jS" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-381b831d535so8312214a91.0 for ; Mon, 20 Jul 2026 11:47:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784573234; x=1785178034; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to:content-type; bh=wYNGagPb4YA1YB5zrq43PT6RJzgokAlH9V1waITYWGM=; b=c7WTa0jSJR7YlJdgNC5nm4+tQSXpI3lu1p674oiopBy5vkAthEwpX/hH5CP0vE8HPW SPnGb4KXxZuTugL4haC8tpOQ5aPxyf3pqdtkX9V/Q9rAdCMYfW3feUCPpWdxbc5WYh0x ncrWQllHFUkyA2ppWLkVZvsm6FctdSyBI8ArViSnNbtcW0ycF5a144UesLedIOLgSPSy seZTeLZAzZRvUtonNZaOw12r4NO3zBiLzQ25M612efnFuWMahcIziRBUsmi4Z+ITn1BD 38Sp88OZHrGIa+fFB27UI0QSkxK9tP1YEcEP+xpTbAW1I0aXtkC0QkmyCFBh1VH3tMGp s6zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784573234; x=1785178034; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=wYNGagPb4YA1YB5zrq43PT6RJzgokAlH9V1waITYWGM=; b=IXSQPrrLBch1S1j7mxI7WUjNxLMFpitDoy3Fyz6EZSgMNnFjy1wFjKcwuggUEj6WVE I24PMH0Q8xkuwnIKPu8wYfrcYfg76V5tvknldRc8hvq1W/EzLhFVfaZyi8FqzkTetSx8 k/ixuUGJ7NATtCYbmCeJspp+DeH92NtFNPehUw+xJq9ASyNvPART2P5Se9WTCfv16vA5 QuR65JlYJvnrwQ9r6iqtJU8IItoB3uYNrPqUggkhnFZmaXQWkVnrNc7ehAzEY9h4C3cL MVFbzaPrrZWF1kYc6NKLRCVlx8+uVT32zEKUP/oac+CISNfXSYbGhLD7yCicJ78u0OG0 aYug== X-Forwarded-Encrypted: i=1; AHgh+Ro/xXGYID7tGPJrYgcRwqt9OPr4IdX0scnskb4X8qjaCYEnVWe2hpURkXWr/SiLyv0ObbLng4I1DkLM9Ks=@vger.kernel.org X-Gm-Message-State: AOJu0Yyu8hz2pNSRS3FP8XgxnyVfIrBPXNFIseEVjD8oUF4OJYARGiZC jTEGtrb/ZdRQAEvb984tQm0/WW8VI/v/yKIKibSrM5JdfnQT6m5Jc0HH X-Gm-Gg: AR+sD11k4H6A4yCFQmy1OIpRz11eC0QVpoc5G1s2OnEGWVIfu8adRv36a33R9BhqdmV /mzEx2KzGjWTeCvJ8v6cG6r0Ftq1NPSL+3qnaCNCGGLSXKF1Anaw1Mif3IpEy5KAmS4mzeOT0vi zmy6P5PoPYVw34ynvSuZs3e9LzF9TsrcoH9LbbpueAAZR48gzThIUHa9VKnOulfrkPoLKVEVHzx 25Ovm/ykWc7H0SpbsjT/tJOHzYPhxJisKEuZgrlawrEWh7/a5k6ZVfhCKPDXW6/WaGsYe4+oksC x2S+uKJgL75fS9KYGuYlYyL3DyRFkLQzd2zOeUWOkqiTIDbZo21y8cohSiD2f/wnoQrJB8292uO 9aPhT0piLquYXEDortCIPt47yxYzfnUaXtYSJlPT+6GUNiRUHMBwkqqnF+i8uxflEAOrLS5CTbz EC7hOilcfR8dXdJuBweUqbrEUdPnPcpQ== X-Received: by 2002:a17:90b:2691:b0:387:e0db:3fb2 with SMTP id 98e67ed59e1d1-38e4b602948mr16389172a91.43.1784573233855; Mon, 20 Jul 2026 11:47:13 -0700 (PDT) Received: from [192.168.0.13] ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-38e92396445sm158611a91.17.2026.07.20.11.47.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2026 11:47:13 -0700 (PDT) Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf: Preserve stack frame number for commuted arithmetic From: Eduard Zingerman To: Yiyang Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Kumar Kartikeya Dwivedi Cc: John Fastabend , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Shuah Khan , Emil Tsalapatis , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 20 Jul 2026 11:47:09 -0700 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2-10 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Mon, 2026-07-20 at 16:37 +0000, Yiyang Chen wrote: > When scalar +=3D pointer is handled in adjust_ptr_min_max_vals(), the > destination register inherits the pointer type and id from the source > pointer. For PTR_TO_STACK, the inherited pointer state also has to carry > the stack frame number. >=20 > Without the frame number copy, a stack pointer derived inside a callee > through scalar +=3D fp can be recorded as pointing to frame 0. Stack read= s > and writes through that register can then update or consult the caller > frame while the actual instruction uses the callee frame. >=20 > Copy the frame number when PTR_TO_STACK state is inherited by the > commuted arithmetic form. Could you please infer a "Fixes" tag? > Signed-off-by: Yiyang Chen > --- > =C2=A0kernel/bpf/verifier.c | 7 +++++-- > =C2=A01 file changed, 5 insertions(+), 2 deletions(-) >=20 > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 52be0a118cce0..58017141d52b7 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -13796,11 +13796,14 @@ static int adjust_ptr_min_max_vals(struct bpf_v= erifier_env *env, > =C2=A0 return -EACCES; > =C2=A0 } > =C2=A0 > - /* In case of 'scalar +=3D pointer', dst_reg inherits pointer type and = id. > - * The id may be overwritten later if we create a new variable offset. > + /* In case of 'scalar +=3D pointer', dst_reg inherits pointer type, id, > + * and for stack pointers also the frame number. The id may be overwrit= ten > + * later if we create a new variable offset. > =C2=A0 */ > =C2=A0 dst_reg->type =3D ptr_reg->type; > =C2=A0 dst_reg->id =3D ptr_reg->id; > + if (base_type(ptr_reg->type) =3D=3D PTR_TO_STACK) > + dst_reg->frameno =3D ptr_reg->frameno; This patch fixes a real issue and this a surprisingly buggy piece of code. Looking at other fields defined in bpf_reg_state, it appears that several additional modifications are necessary: - dst_reg->delta =3D 0 - dst_reg->parent_id =3D ptr_reg->parent_id Given such an error prone nature, I think it would be better to: - stash the value of the off_reg in a temporary variable and adjust off_reg pointer accordingly (see bpf_verifier_env for a collection of similar temporaries). - do *dst_reg =3D *ptr_reg instead of the fixup. Wdyt? > =C2=A0 > =C2=A0 if (!check_reg_sane_offset_scalar(env, off_reg, ptr_reg->type) || > =C2=A0 =C2=A0=C2=A0=C2=A0 !check_reg_sane_offset_ptr(env, ptr_reg, ptr_re= g->type))