Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp106045rdg; Wed, 11 Oct 2023 23:23:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHKATaZ6gKX7Yjjuc14GGnPmYpphz8cjiRNMPkAxVLj3YEiePuqJZIRYR8XAGlVfIREZ8P0 X-Received: by 2002:a17:90a:18f:b0:27d:12e1:94e0 with SMTP id 15-20020a17090a018f00b0027d12e194e0mr2534324pjc.36.1697091816369; Wed, 11 Oct 2023 23:23:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697091816; cv=none; d=google.com; s=arc-20160816; b=qaMZusR25T9Q3aIL8iarhhu0PwQ4hyz/5xhkD9mv0oAE2Ow5rUqOFipUte0uE+Igwl B+NvveNSynfB2B4L65yhVTywAE14T0Mj0nVW7S3NB6zivsUkXJZiQYa96MIJFc+bDiNn uTHom2E0m2bxo/01pdBsfyvIICi8FFS8A2vtDn3H8mHiX/kEeWp1H5a4N+EFGdGBDtC9 JC7yBMIEJVQvAdn1L77pExrlvEeqiSHpkuYpa3ejJfaz+7nQik1alYt4179zkmLwF+Km uzGMAq7/4tU4MO5GyJa31gWhoBwzTVNKdImUVk2k/cotA6dpmz2CTkiWodwyk2hPnCo7 Qulw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=IleMAKg+GPM4xdNPivF/aSxe53FBWLj6oWJi1m12Mt4=; fh=/vNgMWiR7YNPSyZJf69MkBFop00JIQ3vWuO4yLtyFp8=; b=bokyFAOCLy/Ie1inviq9LsSPpmeiqqY/+sNYqEJyIp6IlYi06KzXC/rxo7HfCvz9Ld Absn6NuFwJBuBnrNbpsOnWuo5GOfeWHAZiusJFWID9yVKvcVIQu/FuS40vieCi4mLH9a XymCJbRhpmMMH8ZOn3VB1RykKqCBvXrYoRAuvU42ZBDxHpu5uwC2CSbyaBTPJJs/1ptR VoQx0YY+/1tWyusc0Rhmma4PR5bzUy76S5NwdZbEkiRrbPAKByuhztY1hd8ee0vc5AAI ztTLdyYY1BSEFvO0An9yHCYj4BA7PfBf1hApyRYPmldpnhOLwAftjl9jStaA5tHNW5fl 8Yuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SWD77S6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id w23-20020a63fb57000000b0058555ea0a21si1510096pgj.571.2023.10.11.23.23.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 23:23:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SWD77S6r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 2238880DA9B0; Wed, 11 Oct 2023 23:23:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377208AbjJLGXR (ORCPT + 99 others); Thu, 12 Oct 2023 02:23:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343510AbjJLGXQ (ORCPT ); Thu, 12 Oct 2023 02:23:16 -0400 Received: from mail-yw1-x1136.google.com (mail-yw1-x1136.google.com [IPv6:2607:f8b0:4864:20::1136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3FFBBA; Wed, 11 Oct 2023 23:23:14 -0700 (PDT) Received: by mail-yw1-x1136.google.com with SMTP id 00721157ae682-5a7ba0828efso7894707b3.3; Wed, 11 Oct 2023 23:23:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697091794; x=1697696594; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=IleMAKg+GPM4xdNPivF/aSxe53FBWLj6oWJi1m12Mt4=; b=SWD77S6rVHQCriIEd//myP1vy2py+GOk7PbLDOQciq9ygCSRLUMXZdALOufE51XyJo ZOYjhMCivnCFxOHf3ou6uv+xZjYUP3OTYcWISCa1PhQOLC4IjtZswt0HRwxFNKu/dxv2 6K8M2eQ1olXXw7a/GvoDgY2cEGxyftmt9RCsyosUPNJ7ERVhZLmbhkohV3ksXyLrteDq M7RaH1NXcpgFpHNv6NUIPTRYgBNHINtmXDNBdbfFQDFCecWil/0zpW5/OezrXaxr1p3J AC4OPlZYa9xsv6X0288NtAlkJpDu5q14ysc8VIT9bdMJKsOjJiOT9UXGieV3kY/SmVuW UStA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697091794; x=1697696594; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IleMAKg+GPM4xdNPivF/aSxe53FBWLj6oWJi1m12Mt4=; b=hk+PuDX9UHrnjMW8DqEoEk3WnDXw+qd8xqsf6mTaBCoeoqyzVx8Ie+J7nwl415mjxS qtH73uY/v/PUU+I5nbahxRdlNFxim0TCSMeZyhtNbZp/dD5RIxDxYaitvZYAfuETT9vb X6kkC6Y6ZyFEmIrODaxNoGK5y1BtSHuUzNOMGU+Mv1+gvAIJlV88uyJ3yTIKm0kOHnYT 5s7lyhtAUqSt5YX8qJMUE2rUsSA2SUHY/OvMsiiaNIi4vMKMoxPNvM6ZR0zk4/3YvuSE sjw4GZU1S2kUHek3JlCPTWpJ1Z7YZuA8P+gt5oIMSABc8gZrNbUCIQknVl1wKsEwfhQb DE2w== X-Gm-Message-State: AOJu0Ywpu2X9nKM5p2zHeI6pyOHqVgecmBxSutvo0NEJxFkSHy0Yr/Rt FTkKy63NvL7kgLTuI8fYNau3oAIz0/NeBG+XAQ== X-Received: by 2002:a25:800b:0:b0:d9a:39d9:f4fa with SMTP id m11-20020a25800b000000b00d9a39d9f4famr8491995ybk.11.1697091793792; Wed, 11 Oct 2023 23:23:13 -0700 (PDT) MIME-Version: 1.0 References: <20231009-jmp-into-reserved-fields-v1-1-d8006e2ac1f6@gmail.com> <6524f6f77b896_66abc2084d@john.notmuch> <92f824ec-9538-501c-e63e-8483ffe14bad@iogearbox.net> <79dd71a5-446d-9b05-7d37-40e49bbf04ae@iogearbox.net> In-Reply-To: <79dd71a5-446d-9b05-7d37-40e49bbf04ae@iogearbox.net> From: Hao Sun Date: Thu, 12 Oct 2023 08:23:02 +0200 Message-ID: Subject: Re: [PATCH bpf-next] Detect jumping to reserved code during check_cfg() To: Daniel Borkmann Cc: Andrii Nakryiko , John Fastabend , Alexei Starovoitov , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Wed, 11 Oct 2023 23:23:31 -0700 (PDT) On Wed, Oct 11, 2023 at 4:50=E2=80=AFPM Daniel Borkmann wrote: > > On 10/11/23 8:46 AM, Hao Sun wrote: > > On Wed, Oct 11, 2023 at 4:42=E2=80=AFAM Andrii Nakryiko > > wrote: > >> On Tue, Oct 10, 2023 at 1:33=E2=80=AFAM Daniel Borkmann wrote: > >>> On 10/10/23 9:02 AM, John Fastabend wrote: > >>>> Hao Sun wrote: > >>>>> Currently, we don't check if the branch-taken of a jump is reserved= code of > >>>>> ld_imm64. Instead, such a issue is captured in check_ld_imm(). The = verifier > >>>>> gives the following log in such case: > >>>>> > >>>>> func#0 @0 > >>>>> 0: R1=3Dctx(off=3D0,imm=3D0) R10=3Dfp0 > >>>>> 0: (18) r4 =3D 0xffff888103436000 ; R4_w=3Dmap_ptr(off=3D0,ks= =3D4,vs=3D128,imm=3D0) > >>>>> 2: (18) r1 =3D 0x1d ; R1_w=3D29 > >>>>> 4: (55) if r4 !=3D 0x0 goto pc+4 ; R4_w=3Dmap_ptr(off=3D0,ks= =3D4,vs=3D128,imm=3D0) > >>>>> 5: (1c) w1 -=3D w1 ; R1_w=3D0 > >>>>> 6: (18) r5 =3D 0x32 ; R5_w=3D50 > >>>>> 8: (56) if w5 !=3D 0xfffffff4 goto pc-2 > >>>>> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1 > >>>>> mark_precise: frame0: regs=3Dr5 stack=3D before 6: (18) r5 =3D 0x32 > >>>>> 7: R5_w=3D50 > >>>>> 7: BUG_ld_00 > >>>>> invalid BPF_LD_IMM insn > >>>>> > >>>>> Here the verifier rejects the program because it thinks insn at 7 i= s an > >>>>> invalid BPF_LD_IMM, but such a error log is not accurate since the = issue > >>>>> is jumping to reserved code not because the program contains invali= d insn. > >>>>> Therefore, make the verifier check the jump target during check_cfg= (). For > >>>>> the same program, the verifier reports the following log: > >>>> > >>>> I think we at least would want a test case for this. Also how did yo= u create > >>>> this case? Is it just something you did manually and noticed a stran= ge error? > >>> > >>> Curious as well. > >>> > >>> We do have test cases which try to jump into the middle of a double i= nsn as can > >>> be seen that this patch breaks BPF CI with regards to log mismatch be= low (which > >>> still needs to be adapted, too). Either way, it probably doesn't hurt= to also add > >>> the above snippet as a test. > >>> > >>> Hao, as I understand, the patch here is an usability improvement (not= a fix per se) > >>> where we reject such cases earlier during cfg check rather than at a = later point > >>> where we validate ld_imm instruction. Or are there cases you found wh= ich were not > >>> yet captured via current check_ld_imm()? > >>> > >>> test_verifier failure log : > >>> > >>> #458/u test1 ld_imm64 FAIL > >>> Unexpected verifier log! > >>> EXP: R1 pointer comparison > >>> RES: > >>> FAIL > >>> Unexpected error message! > >>> EXP: R1 pointer comparison > >>> RES: jump to reserved code from insn 0 to 2 > >>> verification time 22 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> > >>> jump to reserved code from insn 0 to 2 > >>> verification time 22 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> #458/p test1 ld_imm64 FAIL > >>> Unexpected verifier log! > >>> EXP: invalid BPF_LD_IMM insn > >>> RES: > >>> FAIL > >>> Unexpected error message! > >>> EXP: invalid BPF_LD_IMM insn > >>> RES: jump to reserved code from insn 0 to 2 > >>> verification time 9 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> > >>> jump to reserved code from insn 0 to 2 > >>> verification time 9 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> #459/u test2 ld_imm64 FAIL > >>> Unexpected verifier log! > >>> EXP: R1 pointer comparison > >>> RES: > >>> FAIL > >>> Unexpected error message! > >>> EXP: R1 pointer comparison > >>> RES: jump to reserved code from insn 0 to 2 > >>> verification time 11 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> > >>> jump to reserved code from insn 0 to 2 > >>> verification time 11 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> #459/p test2 ld_imm64 FAIL > >>> Unexpected verifier log! > >>> EXP: invalid BPF_LD_IMM insn > >>> RES: > >>> FAIL > >>> Unexpected error message! > >>> EXP: invalid BPF_LD_IMM insn > >>> RES: jump to reserved code from insn 0 to 2 > >>> verification time 8 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> > >>> jump to reserved code from insn 0 to 2 > >>> verification time 8 usec > >>> stack depth 0 > >>> processed 0 insns (limit 1000000) max_states_per_insn 0 total_sta= tes 0 peak_states 0 mark_read 0 > >>> #460/u test3 ld_imm64 OK > >>> > >>>>> func#0 @0 > >>>>> jump to reserved code from insn 8 to 7 > >>>>> > >>>>> Signed-off-by: Hao Sun > >>> > >>> nit: This needs to be before the "---" line. > >>> > >>>>> --- > >>>>> kernel/bpf/verifier.c | 7 +++++++ > >>>>> 1 file changed, 7 insertions(+) > >>>>> > >>>>> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > >>>>> index eed7350e15f4..725ac0b464cf 100644 > >>>>> --- a/kernel/bpf/verifier.c > >>>>> +++ b/kernel/bpf/verifier.c > >>>>> @@ -14980,6 +14980,7 @@ static int push_insn(int t, int w, int e, s= truct bpf_verifier_env *env, > >>>>> { > >>>>> int *insn_stack =3D env->cfg.insn_stack; > >>>>> int *insn_state =3D env->cfg.insn_state; > >>>>> + struct bpf_insn *insns =3D env->prog->insnsi; > >>>>> > >>>>> if (e =3D=3D FALLTHROUGH && insn_state[t] >=3D (DISCOVERED | = FALLTHROUGH)) > >>>>> return DONE_EXPLORING; > >>>>> @@ -14993,6 +14994,12 @@ static int push_insn(int t, int w, int e, = struct bpf_verifier_env *env, > >>>>> return -EINVAL; > >>>>> } > >>>>> > >>>>> + if (e =3D=3D BRANCH && insns[w].code =3D=3D 0) { > >>>>> + verbose_linfo(env, t, "%d", t); > >>>>> + verbose(env, "jump to reserved code from insn %d to %d= \n", t, w); > >>>>> + return -EINVAL; > >>>>> + } > >>> > >>> Other than that, lgtm. > >> > >> We do rely quite a lot on verifier not complaining eagerly about some > >> potentially invalid instructions if it's provable that some portion of > >> the code won't ever be reached (think using .rodata variables for > >> feature gating, poisoning intructions due to failed CO-RE relocation, > >> which libbpf does actively, except it's using a call to non-existing > >> helper). As such, check_cfg() is a wrong place to do such validity > >> checks because some of the branches might never be run and validated > >> in practice. > > > > Don't really agree. Jump to the middle of ld_imm64 is just like jumping > > out of bounds, both break the CFG integrity immediately. For those > > apparently incorrect jumps, rejecting early makes everything simple; > > otherwise, we probably need some rewrite in the end. > > Could you elaborate on the 'breaking CFG integrity immediately'? This was > what I was trying to gather earlier with log improvement vs actual fix. > > Do you mean /potentially/ breaking CFG integrity, if, say, we had a doubl= e > insn jump in future and there is a back-jump to the 2nd part of the insn? > I mean jumping to the middle of ld_imm64 is similar to jumping out-of-bound= , both are CFG-related issues and can be handled early in one place. For the case you mentioned, the current code would handle such an issue in check_ld_imm64(), and again gives "BAD_LD_IMM" log, which is strange. > > Also, as you mentioned, libbpf relies on non-existing helpers, not jump > > to the middle of ld_imm64. It seems better and easier to not leave this > > hole. > > Thanks, > Daniel