Message ID | 20231101-fix-check-stack-write-v2-2-cb7c17b869b0@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp242677vqx; Wed, 1 Nov 2023 00:34:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNXivLDcKK16SK97irf1WH055yIKSzw22zQJhHWi9FeND9H5vtWVGavJopRHSb0j8Cv/hU X-Received: by 2002:a17:902:db0f:b0:1cc:5aef:f2d1 with SMTP id m15-20020a170902db0f00b001cc5aeff2d1mr7139246plx.25.1698824075072; Wed, 01 Nov 2023 00:34:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698824075; cv=none; d=google.com; s=arc-20160816; b=kAygX19FAEmP+vVMGsTFc15qw28fbgV6XUad6EsGD+B/KafsCc5oUxGoyTtxBhruIA wAgkN+kzJPyiTI8BlP+6QT7dCz/VliWmUO45hcNXUeAZ8qJRaeuGia2gY+oEkur5idY4 gx9OflD4ADtV57PKa/VCMyYflBZhO4o4BHbBF67yjFCxWO/5UQW8JVYtBzTo4Xyav/dM WJfR9xAp9TH+Rv1jNykcB0P8XdHr07ljnRM9DMe5iih23CC7hb/MBECV0++bMvHQwqh8 e+R42tPsqlZdd3wl8obi/y1zrYqyWOFnSFhySgWb8P1AWW87O+P47RTDBq4iBUDHyR5K Qr3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=yHuPB109KsQAxIHXMrlfX3Zfn2UV329R/EJ5GBjTAOI=; fh=8hmxUlMoo9DjnB1CyzFj79xM7lkYj89TBjjY0Dg9ibY=; b=PbzflWYnU5HosYtm/fK4f5sM96OjbEfo1WzGcbjlaIg7okU2hg33U7Y9U7jz8z+9Im B9JmN/noNk2w9655dF3l49kS7tlbqQ3dFQA9obXkSVdHDOvjo+AgN6e8Tn/Ib8mWcOAI QEC2Iqihd0hVCR0jjcSxi0iVKrhlsBcYkwvnud0Cflm88lRd5Lg0K7HDQV2NDjkOAJDU khl5f1Bq5N0zUfdQCyvZBmpM2uQK4qWy/mIzt/4znipYCQvsL9d7OtonrwjjNKysVcvV AGUdUsTfuvlrH0f9HF0WAJgrRN9XR0fFaoU0iImD3qKQEFdP4leIm97ZFeFqUlNCFsSj HVmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=E8JPvk6f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id l11-20020a170903244b00b001cc467a339esi2579090pls.389.2023.11.01.00.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 00:34:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=E8JPvk6f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id A0949803DAF3; Wed, 1 Nov 2023 00:34:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231276AbjKAHeQ (ORCPT <rfc822;rbbytesnap@gmail.com> + 35 others); Wed, 1 Nov 2023 03:34:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231179AbjKAHeL (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 1 Nov 2023 03:34:11 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1950EC2; Wed, 1 Nov 2023 00:34:09 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-40806e4106dso3227515e9.1; Wed, 01 Nov 2023 00:34:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698824047; x=1699428847; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=yHuPB109KsQAxIHXMrlfX3Zfn2UV329R/EJ5GBjTAOI=; b=E8JPvk6fJHiqZWBVw0kxYop0PlUsEL+0LDnpTssztnyoYP7wFNfI6jDaboazB5aVrW yBd4EOQAiK2MajaVXqVw2B6HtC4FN+vGW5mNGGQ6prnGi9/1yVu+Xt4FMgzsiOqw2aYP OYj0W8rS5AjLRyzpXrjEd8+yAVFQkfB/oC292n+nJZIYG6uqw+te0bQSx5kLTRPuS++K JUxIEb8ubUfgbXbLu2EAQjd7q3Vn+WX07QS4gicGJKASXmnjRxWOFVtdnbQ6fgVspoKw aABYvrciqGgHwz6/DVJ5zgzRFzGlgh6BnAfIm21/d33GiQ15tI1JY00bXfls3SxbmSZ0 rbMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698824047; x=1699428847; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yHuPB109KsQAxIHXMrlfX3Zfn2UV329R/EJ5GBjTAOI=; b=YKQ2b7t31Tb1gP/eTyppwxClTKSp68GIk11+dFrXnAnBxuINoDYrndTs66ittSN+1l 5uSd8ZRb/lReMOG1rKXEpWyjh63WklyGorLphpZHpoMVCmLrhITwfvRdEfgfegOF7oe1 rhLSy//VwgHwNKxO7NmkSq9NJzBDQUlOtL6mLxeAh4FK2GBBYPGIVoopEFs7ygH6RGXN rdwybHGsKPlPV3bG2QRZbFw1LuCSu/i2jud5IUjCD+QKrg+/2t+oYqBjtb71lIwyOS1V u//kDQKu4WDzh8ZkIA2Ph+EwjW5/6v4OgKi/7KaC0ngYZLF+hu4tImdm6xRSD8TJYwCQ Th1Q== X-Gm-Message-State: AOJu0Yy4kbGReFGreefQ5VCucaABJorPiMsg5sukgvulhHopHSlfCgin ZwaHqFP1pt+sbXbQwImuBg== X-Received: by 2002:a05:600c:3146:b0:408:36bb:5b0c with SMTP id h6-20020a05600c314600b0040836bb5b0cmr1999352wmo.7.1698824047195; Wed, 01 Nov 2023 00:34:07 -0700 (PDT) Received: from amdsuplus2.inf.ethz.ch (amdsuplus2.inf.ethz.ch. [129.132.31.88]) by smtp.gmail.com with ESMTPSA id i18-20020adff312000000b0032d893d8dc8sm3401810wro.2.2023.11.01.00.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 00:34:06 -0700 (PDT) From: Hao Sun <sunhao.th@gmail.com> Date: Wed, 01 Nov 2023 08:33:23 +0100 Subject: [PATCH bpf v2 2/2] selftests/bpf: Add test for immediate spilled to stack MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231101-fix-check-stack-write-v2-2-cb7c17b869b0@gmail.com> References: <20231101-fix-check-stack-write-v2-0-cb7c17b869b0@gmail.com> In-Reply-To: <20231101-fix-check-stack-write-v2-0-cb7c17b869b0@gmail.com> To: Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, John Fastabend <john.fastabend@gmail.com>, Andrii Nakryiko <andrii@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>, Shuah Khan <shuah@kernel.org>, Eduard Zingerman <eddyz87@gmail.com>, Shung-Hsi Yu <shung-hsi.yu@suse.com> Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Hao Sun <sunhao.th@gmail.com> X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1698824042; l=1562; i=sunhao.th@gmail.com; s=20231009; h=from:subject:message-id; bh=RsQQJC1kz5RaWMBIQa3UXHDscXwUBIZwC2LrZY94Ug8=; b=2D5CtDRRnmzsb7u21FbfWzwordfaO3zTME3qmQjlmraBAi9/A0FMHxGNxSA46wa/qzpYzel7r UmnA7cRuRo0ArLRb4cHvE6XOa70EBWAn2hWXkDvKy1bqnB/iQqna2wF X-Developer-Key: i=sunhao.th@gmail.com; a=ed25519; pk=AHFxrImGtyqXOuw4f5xTNh4PGReb7hzD86ayyTZCXd4= X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 01 Nov 2023 00:34:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781346153273909397 X-GMAIL-MSGID: 1781346153273909397 |
Series |
bpf: Fix incorrect immediate spill
|
|
Commit Message
Hao Sun
Nov. 1, 2023, 7:33 a.m. UTC
Add a test to check if the verifier correctly reason about the sign
of an immediate spilled to stack by BPF_ST instruction.
Signed-off-by: Hao Sun <sunhao.th@gmail.com>
---
tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++
1 file changed, 32 insertions(+)
Comments
On Wed, 2023-11-01 at 08:33 +0100, Hao Sun wrote: > Add a test to check if the verifier correctly reason about the sign > of an immediate spilled to stack by BPF_ST instruction. > > Signed-off-by: Hao Sun <sunhao.th@gmail.com> > --- > tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++ > 1 file changed, 32 insertions(+) > > diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > index 3af2501082b2..0ba23807c46c 100644 > --- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > +++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > @@ -65,3 +65,35 @@ > .expected_attach_type = BPF_SK_LOOKUP, > .runs = -1, > }, > +{ > + "BPF_ST_MEM stack imm sign", > + /* Check if verifier correctly reasons about sign of an > + * immediate spilled to stack by BPF_ST instruction. > + * > + * fp[-8] = -44; > + * r0 = fp[-8]; > + * if r0 s< 0 goto ret0; > + * r0 = -1; > + * exit; > + * ret0: > + * r0 = 0; > + * exit; > + */ > + .insns = { > + BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44), > + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8), > + BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2), > + BPF_MOV64_IMM(BPF_REG_0, -1), > + BPF_EXIT_INSN(), > + BPF_MOV64_IMM(BPF_REG_0, 0), > + BPF_EXIT_INSN(), > + }, > + /* Use prog type that requires return value in range [0, 1] */ > + .prog_type = BPF_PROG_TYPE_SK_LOOKUP, > + .expected_attach_type = BPF_SK_LOOKUP, > + .result = VERBOSE_ACCEPT, > + .runs = -1, > + .errstr = "0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44\ > + 2: (c5) if r0 s< 0x0 goto pc+2\ > + 2: R0_w=-44", > +}, > Please note that this test case fails on CI [0], full log below: 2023-11-01T07:49:51.2841702Z #116/p BPF_ST_MEM stack imm sign FAIL 2023-11-01T07:49:51.2843456Z Unexpected verifier log! 2023-11-01T07:49:51.2844968Z EXP: 2: R0_w=-44 2023-11-01T07:49:51.2845583Z RES: 2023-11-01T07:49:51.2846693Z func#0 @0 2023-11-01T07:49:51.2848932Z 0: R1=ctx(off=0,imm=0) R10=fp0 2023-11-01T07:49:51.2853045Z 0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44 2023-11-01T07:49:51.2857391Z 1: (79) r0 = *(u64 *)(r10 -8) ; R0_w=-44 R10=fp0 fp-8_w=-44 2023-11-01T07:49:51.2859127Z 2: (c5) if r0 s< 0x0 goto pc+2 2023-11-01T07:49:51.2862943Z mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1 2023-11-01T07:49:51.2867511Z mark_precise: frame0: regs=r0 stack= before 1: (79) r0 = *(u64 *)(r10 -8) 2023-11-01T07:49:51.2872217Z mark_precise: frame0: regs= stack=-8 before 0: (7a) *(u64 *)(r10 -8) = -44 2023-11-01T07:49:51.2872816Z 5: R0_w=-44 2023-11-01T07:49:51.2875653Z 5: (b7) r0 = 0 ; R0_w=0 2023-11-01T07:49:51.2876493Z 6: (95) exit I suspect that after recent logging fixes instruction number printed after jump changed and that's why test case no longer passes. Note: you can check CI status for submitted patch-sets using link [1]. [0] https://github.com/kernel-patches/bpf/actions/runs/6717053909/job/18254330860 [1] https://patchwork.kernel.org/project/netdevbpf/list/
On Wed, Nov 1, 2023 at 12:05 PM Eduard Zingerman <eddyz87@gmail.com> wrote: > > On Wed, 2023-11-01 at 08:33 +0100, Hao Sun wrote: > > Add a test to check if the verifier correctly reason about the sign > > of an immediate spilled to stack by BPF_ST instruction. > > > > Signed-off-by: Hao Sun <sunhao.th@gmail.com> > > --- > > tools/testing/selftests/bpf/verifier/bpf_st_mem.c | 32 +++++++++++++++++++++++ > > 1 file changed, 32 insertions(+) > > > > diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > > index 3af2501082b2..0ba23807c46c 100644 > > --- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > > +++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c > > @@ -65,3 +65,35 @@ > > .expected_attach_type = BPF_SK_LOOKUP, > > .runs = -1, > > }, > > +{ > > + "BPF_ST_MEM stack imm sign", > > + /* Check if verifier correctly reasons about sign of an > > + * immediate spilled to stack by BPF_ST instruction. > > + * > > + * fp[-8] = -44; > > + * r0 = fp[-8]; > > + * if r0 s< 0 goto ret0; > > + * r0 = -1; > > + * exit; > > + * ret0: > > + * r0 = 0; > > + * exit; > > + */ > > + .insns = { > > + BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44), > > + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8), > > + BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2), > > + BPF_MOV64_IMM(BPF_REG_0, -1), > > + BPF_EXIT_INSN(), > > + BPF_MOV64_IMM(BPF_REG_0, 0), > > + BPF_EXIT_INSN(), > > + }, > > + /* Use prog type that requires return value in range [0, 1] */ > > + .prog_type = BPF_PROG_TYPE_SK_LOOKUP, > > + .expected_attach_type = BPF_SK_LOOKUP, > > + .result = VERBOSE_ACCEPT, > > + .runs = -1, > > + .errstr = "0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44\ > > + 2: (c5) if r0 s< 0x0 goto pc+2\ > > + 2: R0_w=-44", > > +}, > > > > Please note that this test case fails on CI [0], full log below: > > 2023-11-01T07:49:51.2841702Z #116/p BPF_ST_MEM stack imm sign FAIL > 2023-11-01T07:49:51.2843456Z Unexpected verifier log! > 2023-11-01T07:49:51.2844968Z EXP: 2: R0_w=-44 > 2023-11-01T07:49:51.2845583Z RES: > 2023-11-01T07:49:51.2846693Z func#0 @0 > 2023-11-01T07:49:51.2848932Z 0: R1=ctx(off=0,imm=0) R10=fp0 > 2023-11-01T07:49:51.2853045Z 0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44 > 2023-11-01T07:49:51.2857391Z 1: (79) r0 = *(u64 *)(r10 -8) ; R0_w=-44 R10=fp0 fp-8_w=-44 > 2023-11-01T07:49:51.2859127Z 2: (c5) if r0 s< 0x0 goto pc+2 > 2023-11-01T07:49:51.2862943Z mark_precise: frame0: last_idx 2 first_idx 0 subseq_idx -1 > 2023-11-01T07:49:51.2867511Z mark_precise: frame0: regs=r0 stack= before 1: (79) r0 = *(u64 *)(r10 -8) > 2023-11-01T07:49:51.2872217Z mark_precise: frame0: regs= stack=-8 before 0: (7a) *(u64 *)(r10 -8) = -44 > 2023-11-01T07:49:51.2872816Z 5: R0_w=-44 > 2023-11-01T07:49:51.2875653Z 5: (b7) r0 = 0 ; R0_w=0 > 2023-11-01T07:49:51.2876493Z 6: (95) exit > > I suspect that after recent logging fixes instruction number printed > after jump changed and that's why test case no longer passes. > Yes, so I guess we can just drop the line number there, will send patch v3. > Note: you can check CI status for submitted patch-sets using link [1]. > > [0] https://github.com/kernel-patches/bpf/actions/runs/6717053909/job/18254330860 > [1] https://patchwork.kernel.org/project/netdevbpf/list/ Thanks.
diff --git a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c index 3af2501082b2..0ba23807c46c 100644 --- a/tools/testing/selftests/bpf/verifier/bpf_st_mem.c +++ b/tools/testing/selftests/bpf/verifier/bpf_st_mem.c @@ -65,3 +65,35 @@ .expected_attach_type = BPF_SK_LOOKUP, .runs = -1, }, +{ + "BPF_ST_MEM stack imm sign", + /* Check if verifier correctly reasons about sign of an + * immediate spilled to stack by BPF_ST instruction. + * + * fp[-8] = -44; + * r0 = fp[-8]; + * if r0 s< 0 goto ret0; + * r0 = -1; + * exit; + * ret0: + * r0 = 0; + * exit; + */ + .insns = { + BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, -44), + BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8), + BPF_JMP_IMM(BPF_JSLT, BPF_REG_0, 0, 2), + BPF_MOV64_IMM(BPF_REG_0, -1), + BPF_EXIT_INSN(), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + /* Use prog type that requires return value in range [0, 1] */ + .prog_type = BPF_PROG_TYPE_SK_LOOKUP, + .expected_attach_type = BPF_SK_LOOKUP, + .result = VERBOSE_ACCEPT, + .runs = -1, + .errstr = "0: (7a) *(u64 *)(r10 -8) = -44 ; R10=fp0 fp-8_w=-44\ + 2: (c5) if r0 s< 0x0 goto pc+2\ + 2: R0_w=-44", +},