From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21E79CD8CA2 for ; Tue, 10 Oct 2023 15:27:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233167AbjJJP1X (ORCPT ); Tue, 10 Oct 2023 11:27:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229955AbjJJP1U (ORCPT ); Tue, 10 Oct 2023 11:27:20 -0400 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9A3A93; Tue, 10 Oct 2023 08:27:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iogearbox.net; s=default2302; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=xGw5JKNGroV+nJlTtMZo29gp/zDG1Ff4DcIXxL9FLSg=; b=e5cMpPzV0p2fC8a6LvrbQnL+CV umzdLmpEaEjXgawmLVM4bPQhb3wweNa3SWdHUb+Y4RWf0MJyW+C41v73dZWMFy/1zoIKWSnLSRNaV cHwLlUKg1uUfy6N9Y52A6TM8B5J5tpxV+aA+hbV7g6WPeindOmuzdX9oR3oiIQm65nQfDwd9b5yW8 f88T3T63gJdcU5sraWh+bUN7R2NQLnRK4/Rl9xz+hjp6qa2pYsz1JLz0HrncQ7Gwc3pgSG8npaPVA C/+RB1yNEjhkBiVsddurbhMdY5xCq8RbRSNtL4w8nVr9EgRZ+AYT+QCGLBrk79NzV77XOJHfNjoTA +jqYhLGQ==; Received: from sslproxy02.your-server.de ([78.47.166.47]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qqEdX-0005Xu-4d; Tue, 10 Oct 2023 17:27:07 +0200 Received: from [178.197.249.27] (helo=linux.home) by sslproxy02.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qqEdW-00083o-JE; Tue, 10 Oct 2023 17:27:06 +0200 Subject: Re: [PATCH bpf-next v2] bpf: Detect jumping to reserved code during check_cfg() To: Eduard Zingerman , Hao Sun , Alexei Starovoitov , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org References: <20231010-jmp-into-reserved-fields-v2-1-3dd5a94d1e21@gmail.com> From: Daniel Borkmann Message-ID: Date: Tue, 10 Oct 2023 17:27:05 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.10/27057/Tue Oct 10 09:39:11 2023) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/10/23 4:46 PM, Eduard Zingerman wrote: > On Tue, 2023-10-10 at 14:03 +0200, 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=ctx(off=0,imm=0) R10=fp0 >> 0: (18) r4 = 0xffff888103436000 ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0) >> 2: (18) r1 = 0x1d ; R1_w=29 >> 4: (55) if r4 != 0x0 goto pc+4 ; R4_w=map_ptr(off=0,ks=4,vs=128,imm=0) >> 5: (1c) w1 -= w1 ; R1_w=0 >> 6: (18) r5 = 0x32 ; R5_w=50 >> 8: (56) if w5 != 0xfffffff4 goto pc-2 >> mark_precise: frame0: last_idx 8 first_idx 0 subseq_idx -1 >> mark_precise: frame0: regs=r5 stack= before 6: (18) r5 = 0x32 >> 7: R5_w=50 >> 7: BUG_ld_00 >> invalid BPF_LD_IMM insn >> >> Here the verifier rejects the program because it thinks insn at 7 is 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 invalid insn. >> Therefore, make the verifier check the jump target during check_cfg(). For >> the same program, the verifier reports the following log: >> >> func#0 @0 >> jump to reserved code from insn 8 to 7 >> >> Also adjust existing tests in ld_imm64.c, testing forward/back jump to >> reserved code. >> >> Signed-off-by: Hao Sun > > Please see a nitpick below. > > Acked-by: Eduard Zingerman > >> --- >> Changes in v2: >> - Adjust existing test cases >> - Link to v1: https://lore.kernel.org/bpf/20231009-jmp-into-reserved-fields-v1-1-d8006e2ac1f6@gmail.com/ >> --- >> kernel/bpf/verifier.c | 7 +++++++ >> tools/testing/selftests/bpf/verifier/ld_imm64.c | 8 +++----- >> 2 files changed, 10 insertions(+), 5 deletions(-) >> >> 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, struct bpf_verifier_env *env, >> { >> int *insn_stack = env->cfg.insn_stack; >> int *insn_state = env->cfg.insn_state; >> + struct bpf_insn *insns = env->prog->insnsi; >> >> if (e == FALLTHROUGH && insn_state[t] >= (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 == BRANCH && insns[w].code == 0) { >> + verbose_linfo(env, t, "%d", t); >> + verbose(env, "jump to reserved code from insn %d to %d\n", t, w); >> + return -EINVAL; >> + } >> + >> if (e == BRANCH) { >> /* mark branch target for state pruning */ >> mark_prune_point(env, w); >> diff --git a/tools/testing/selftests/bpf/verifier/ld_imm64.c b/tools/testing/selftests/bpf/verifier/ld_imm64.c >> index f9297900cea6..c34aa78f1877 100644 >> --- a/tools/testing/selftests/bpf/verifier/ld_imm64.c >> +++ b/tools/testing/selftests/bpf/verifier/ld_imm64.c >> @@ -9,22 +9,20 @@ >> BPF_MOV64_IMM(BPF_REG_0, 2), >> BPF_EXIT_INSN(), >> }, >> - .errstr = "invalid BPF_LD_IMM insn", >> - .errstr_unpriv = "R1 pointer comparison", >> + .errstr = "jump to reserved code", >> .result = REJECT, >> }, >> { >> "test2 ld_imm64", >> .insns = { >> - BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, 1), >> BPF_LD_IMM64(BPF_REG_0, 0), >> + BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0, -2), > > This change is not really necessary, the test reports same error > either way. If we don't have a backward jump covered, we could probably also make this a new test case rather than modifying an existing one. Aside from that it would probably also make sense to make this a separate commit, so it eases backporting a bit. >> BPF_LD_IMM64(BPF_REG_0, 0), >> BPF_LD_IMM64(BPF_REG_0, 1), >> BPF_LD_IMM64(BPF_REG_0, 1), >> BPF_EXIT_INSN(), >> }, >> - .errstr = "invalid BPF_LD_IMM insn", >> - .errstr_unpriv = "R1 pointer comparison", >> + .errstr = "jump to reserved code", >> .result = REJECT, >> }, >> { >> >> --- >> base-commit: 3157b7ce14bbf468b0ca8613322a05c37b5ae25d >> change-id: 20231009-jmp-into-reserved-fields-fc1a98a8e7dc >> >> Best regards, >