Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp824096rdb; Wed, 1 Nov 2023 04:07:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE4JG8Zo37ZVAjSBRI/nrnp7HIniKroXXiamzn+GGegO8uDnMRoUL+tBTLn8CWLLnC27iXv X-Received: by 2002:a05:6300:8002:b0:181:219f:4a66 with SMTP id an2-20020a056300800200b00181219f4a66mr3359909pzc.34.1698836859240; Wed, 01 Nov 2023 04:07:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698836859; cv=none; d=google.com; s=arc-20160816; b=nXNvu2FVm3ou4Dp400wpVfiJE4E6XQ12G1h6CKD+7Z0zuFn73G7G026CVUG9h+xsyS 5xkU5r9SYxmlADBifjNboU+/9O87LVMyBLlGGVzsGxPTW8/KBS8zvmga4VsFOp5DEzY0 kfyMSorv0+urxlrEltLlkpA9y9DW32WKzZ64XISdGS/GnMsRAfKxmgbhtyBYdEUJ1MpY YL1lDtidPMWXtC/Ec8ea/BbXkJ6a+CHnmXeNw4efh/BwmN/V6ouQRlA2SbNeLbs2EyrN NenEzab54u6TFy7EPzqRm1aqHgTMmRRRa63+VBL41hqWDN6CpZEnEokoowV9PyREgM5u qjXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:autocrypt:references:in-reply-to:date:cc :to:from:subject:message-id:dkim-signature; bh=sDsETI9m7t7kgPADCEoFbUHNZWp8HwKd1gKWFMHiraw=; fh=2y5TtVR+pzBcOhWhhJa4PEsn1AJZy7PECdowU0aVPwM=; b=XJaskfuSU4MgENM1c/0HxrYPMQMs8bDt+G3aRxRtDVI1qr6Su+RFerEsj+BpgWYpR1 j/rnH2obuPJ06+OTp4c79NHwr7e4rCLdA+YU33AwMWotz0WPReg9Br9BAJhP/IOJFlBw ioz0+FWoMKscC/zj7A/dszotJrDoKKGyve3YxohSCEaPEL6sa774QrIFplhB7OX5/eNc E+5Jg8+KeoHoRh6ci5ERBovailTP8KPBd1zJ0ej/NvAGUA1ZvROrWQErNdPXvJHaIOdr 5bSoy3qpci20N1WiLJPCBcbSMNXWs/QgaDaczigSqkz0GGk0Yn2gY3sLQcDqCOumCAop OvzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=d6WK6cIX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id j8-20020a170902da8800b001cc3480e703si2985448plx.517.2023.11.01.04.07.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 04:07:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=d6WK6cIX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 14D6D8028A77; Wed, 1 Nov 2023 04:07:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235061AbjKALHh (ORCPT + 99 others); Wed, 1 Nov 2023 07:07:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234993AbjKALHg (ORCPT ); Wed, 1 Nov 2023 07:07:36 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B704BF7; Wed, 1 Nov 2023 04:07:32 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-507a5f2193bso813867e87.1; Wed, 01 Nov 2023 04:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698836851; x=1699441651; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to; bh=sDsETI9m7t7kgPADCEoFbUHNZWp8HwKd1gKWFMHiraw=; b=d6WK6cIXmUSySuT10Jnpvc5Uyz2R3ZZNP7RlLykIWODa7bA0KCgzu3BRl5QvixXzyW jIiGPR+mvutPRCq1mcVt6241nMP/sIarbFs3oqe9+0Jx8uo7iKbxFCxp6Vy+wFb4lM/+ TmM3ejIbYmGy64gL4b3K8s7q/4vUnfYn/Q1gJSpgJS3sG0bAfT+rkeDDGSFmM6AJ0aKU IE9EgTFhX116fSNYQkBz8COAgx8chCIdj53KwHJ51ONpPDcbSVbwyCG7E1VrIneEllLK HoV/s1HwCs8Shue0/NRPCKZ6ygHQvFHjgN1QmE2eBQnjO+STimf1cO2z/lyJDXso2NWz /jew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698836851; x=1699441651; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sDsETI9m7t7kgPADCEoFbUHNZWp8HwKd1gKWFMHiraw=; b=RHHjUo2O6JXVIhKYTi8IoS1nxmuDHAhu0PYOkgrnQQg7jL7HcdkpFSeMtB90YAT/O5 FMqzya4q8XdEC3YZkzibfFiohVOQgAcepVgG9TdBTDp4GtMxdHzMIkNSZ40lsOmBxj38 IkP1mSPkR7cV85XFhw+SeMFccYnMnADIfGE6dMXa3IBb1t3dGMXVcSoULGArDv4eH6VW EhZ5ZoY9S/IcXrQt/356+dw8fxL0hTl4a/b9buaqs1C7Wts1NmfJrey0KdSQTW23bnnr GOdiIzZcwx84H8lYl5brtcDR8JLaqvxKHl9LPmAhalE71XM0kU/Uny77EmgxT54oCBmb a1Ow== X-Gm-Message-State: AOJu0YyUq8J5+Cy91SRVQdJgskqnzlDdAngmklaWdTAOooUktpaEYXxJ 057AQpl5LtHAlXjBAX0C1dc= X-Received: by 2002:a05:6512:4007:b0:509:39c0:7207 with SMTP id br7-20020a056512400700b0050939c07207mr1291389lfb.1.1698836850704; Wed, 01 Nov 2023 04:07:30 -0700 (PDT) Received: from [192.168.1.95] (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id d26-20020a194f1a000000b0050910031a92sm189915lfb.271.2023.11.01.04.07.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 04:07:30 -0700 (PDT) Message-ID: Subject: Re: [PATCH bpf v2 1/2] bpf: Fix check_stack_write_fixed_off() to correctly spill imm From: Eduard Zingerman To: Hao Sun , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Shung-Hsi Yu Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, stable@vger.kernel.org Date: Wed, 01 Nov 2023 13:07:28 +0200 In-Reply-To: <20231101-fix-check-stack-write-v2-1-cb7c17b869b0@gmail.com> References: <20231101-fix-check-stack-write-v2-0-cb7c17b869b0@gmail.com> <20231101-fix-check-stack-write-v2-1-cb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sqgTzAn555h0rkFgay0hAvMU/mAW/CFNSIxV397zm749ZNLA0L2dMy1AKuOqH+/B+/ImBfJMDjmdyJQ8WU/OFRuGLdqOd2oZrA1iuPIa+yUYyZkaZfz/emQwpIL1+Q4p1R/OplA4yc301AqruXXUcVDbEB+joHW3hy5FwK5t5OwTKatrSJBkydSF9zdXy98fYzGniRyRA65P0Ix/8J3BYB4edY2/w0Ip/mdYsYQljBY0A== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.0 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 01 Nov 2023 04:07:38 -0700 (PDT) On Wed, 2023-11-01 at 08:33 +0100, Hao Sun wrote: > In check_stack_write_fixed_off(), imm value is cast to u32 before being > spilled to the stack. Therefore, the sign information is lost, and the > range information is incorrect when load from the stack again. >=20 > For the following prog: > 0: r2 =3D r10 > 1: *(u64*)(r2 -40) =3D -44 > 2: r0 =3D *(u64*)(r2 - 40) > 3: if r0 s<=3D 0xa goto +2 > 4: r0 =3D 1 > 5: exit > 6: r0 =3D 0 > 7: exit >=20 > The verifier gives: > func#0 @0 > 0: R1=3Dctx(off=3D0,imm=3D0) R10=3Dfp0 > 0: (bf) r2 =3D r10 ; R2_w=3Dfp0 R10=3Dfp0 > 1: (7a) *(u64 *)(r2 -40) =3D -44 ; R2_w=3Dfp0 fp-40_w=3D4294967252 > 2: (79) r0 =3D *(u64 *)(r2 -40) ; R0_w=3D4294967252 R2_w=3Dfp0 > fp-40_w=3D4294967252 > 3: (c5) if r0 s< 0xa goto pc+2 > mark_precise: frame0: last_idx 3 first_idx 0 subseq_idx -1 > mark_precise: frame0: regs=3Dr0 stack=3D before 2: (79) r0 =3D *(u64 *)(r= 2 -40) > 3: R0_w=3D4294967252 > 4: (b7) r0 =3D 1 ; R0_w=3D1 > 5: (95) exit > verification time 7971 usec > stack depth 40 > processed 6 insns (limit 1000000) max_states_per_insn 0 total_states 0 > peak_states 0 mark_read 0 >=20 > So remove the incorrect cast, since imm field is declared as s32, and > __mark_reg_known() takes u64, so imm would be correctly sign extended > by compiler. >=20 > Fixes: ecdf985d7615 ("bpf: track immediate values written to stack by BPF= _ST instruction") > Cc: stable@vger.kernel.org > Signed-off-by: Hao Sun Acked-by: Eduard Zingerman > --- > kernel/bpf/verifier.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) >=20 > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 857d76694517..44af69ce1301 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -4674,7 +4674,7 @@ static int check_stack_write_fixed_off(struct bpf_v= erifier_env *env, > insn->imm !=3D 0 && env->bpf_capable) { > struct bpf_reg_state fake_reg =3D {}; > =20 > - __mark_reg_known(&fake_reg, (u32)insn->imm); > + __mark_reg_known(&fake_reg, insn->imm); > fake_reg.type =3D SCALAR_VALUE; > save_register_state(state, spi, &fake_reg, size); > } else if (reg && is_spillable_regtype(reg->type)) { >=20