Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp273047ybb; Tue, 7 Apr 2020 22:40:14 -0700 (PDT) X-Google-Smtp-Source: APiQypIYsjV/DoKxR5/Ik+PhzG4jgexeejVrHPg+mIIxemf2xz3ZtcxuJhvbtsaudxCwasJhf96b X-Received: by 2002:a9d:22e9:: with SMTP id y96mr4447101ota.217.1586324414341; Tue, 07 Apr 2020 22:40:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586324414; cv=none; d=google.com; s=arc-20160816; b=wWbbAi9QXrzpzeVndElbwrgJrIeCNI5T5XX3iptR1Onzhwdu3ZrGkEANumYphFqfov 8t+tJR1Yr4qgfQrnrANOyftNyUziga7ay9/Pj/Pk6oA0rF8F0TV3Q1ySxm1Z79FxyMOv HwvH9h77LcDYUgqw4UFdIWnBAm0ciSY+ok7ba459hf+dHWc5IIs/ixFJqYMvgAXnoBX/ 12Kq+uCxMFZXh1Q71F8TH8Cv9rHUn+ew4BkphfuQh2urBOSujA8+rHxbXrijqXx2kW+i zIuc1+ZoIOCCQt4j/84m5bcL0dg9pvnayNXwXqqib3iNFpOwIdLBSAKiRUEO3sgpZL+z cGug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Okc82vqS7N+cAm2fC1T1A+xd/pJAgTWbJlOtDSH21Rg=; b=s1Dw9A1Pu31EBnYoRLvoez/8Yh+IbkrAj2QSfrJSsqOXNPslJ/7Ba3HTuOgTy6PiD4 awIuu3jCM3Ul7URpcdzcYNxI1ar77izTnR6Ohj0EV9VXYJ5NBqcC4R4emPw5IY7wck6E nyFhk81P800EPp6jP9nOfiuPmedoqMZmiCOVGzD+iKCov/N/fw0KZYmDv5c8Bq0qVBRi 0fU6qVV4/0Mb2O23rxvDz8FCRqGxbPU7Hp0XfK3/JUXPyIuGodiUnSO8uqdIAZkERqbF gwYLiewbxSzBv2JAApBk0/2KcZ1VIFGmSgBan2B3QsiPw7NAUJIRa6Fyjrj+wYNqGXxo 48Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NGjPIN3p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a6si2050020oto.34.2020.04.07.22.39.59; Tue, 07 Apr 2020 22:40:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NGjPIN3p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726566AbgDHFjU (ORCPT + 99 others); Wed, 8 Apr 2020 01:39:20 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:39256 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725879AbgDHFjU (ORCPT ); Wed, 8 Apr 2020 01:39:20 -0400 Received: by mail-wr1-f67.google.com with SMTP id p10so6312472wrt.6; Tue, 07 Apr 2020 22:39:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Okc82vqS7N+cAm2fC1T1A+xd/pJAgTWbJlOtDSH21Rg=; b=NGjPIN3pcaFl/PAoh+/QRIXJUQi67BRhQXiHheHL6FKEhBjcg638Yd5wqiN9Z9yzop VLX4y+CU3TUGzKxd8u21ww8cA32q/2uUZi0Cb1Q8BljyD929VSo2RJhuzsIYGiZdPyjB AsvH2uHEv5ygVlRkUV4j/sLvRQpwKXZg4ZLiJXe1mNE51phrSZiYTEkF9RXoZSI/Se5u yFcRuXDwtrW75zBa3E30rmV0IP3CLT89nbijAfeh41UmSpnCjOF95CUO8S8pvom+6INC kbu1Cj8SB3XQNOie8ZrjVMD2doQGnMkae6fol4+qmN3s7SYNyj/FEPmx+4bsWwMTZW4D +4Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Okc82vqS7N+cAm2fC1T1A+xd/pJAgTWbJlOtDSH21Rg=; b=sfU4WmRnSk98h2eHeQiulXNQSUQb2wwHYjlcwjFS72CXI1jKeE4XC2Dd3kHbfx+A4x gIIwsMQBcJQIAvwnyGzMh8Fs2m1idZfS7FgP814zV2MjewDRs9ISz0dhc1gKiQKzzYyz qBDxsjvLP1+dHp6d6jUR8H6eJFNyDr87PoXtMPzjEJbSzHFfavaRHj2zAR1kkO+l0/A5 T0VAOzFNjdDSNBtu2Q9gEbtKCqc8fzniRN2lLI0pGsSS/f1Cj9b7rJSgZaGkbVE931Rl +k4vLlHzb1LJ4VTrreuYa6HPrYV420kQnkE1aB/QLCP7hXRj/bqWq9BXUBSz1KdZyQmU 6atQ== X-Gm-Message-State: AGi0PuYFINnuZgxqBbziCfeCx2/RFd5bLuO51Pot68VqD/y+1EMxl7EH kJvwjtncSOZ4B5t19lTr2T7jXiWUrXnWt99MWcw= X-Received: by 2002:a5d:4988:: with SMTP id r8mr6283912wrq.248.1586324357903; Tue, 07 Apr 2020 22:39:17 -0700 (PDT) MIME-Version: 1.0 References: <20200406221604.18547-1-luke.r.nels@gmail.com> In-Reply-To: <20200406221604.18547-1-luke.r.nels@gmail.com> From: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= Date: Wed, 8 Apr 2020 07:39:06 +0200 Message-ID: Subject: Re: [PATCH bpf] riscv, bpf: Fix offset range checking for auipc+jalr on RV64 To: Luke Nelson Cc: bpf , Xi Wang , Luke Nelson , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Netdev , linux-riscv@lists.infradead.org, LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 7 Apr 2020 at 00:16, Luke Nelson wrote= : > > The existing code in emit_call on RV64 checks that the PC-relative offset > to the function fits in 32 bits before calling emit_jump_and_link to emit > an auipc+jalr pair. However, this check is incorrect because offsets in > the range [2^31 - 2^11, 2^31 - 1] cannot be encoded using auipc+jalr on > RV64 (see discussion [1]). The RISC-V spec has recently been updated > to reflect this fact [2, 3]. > > This patch fixes the problem by moving the check on the offset into > emit_jump_and_link and modifying it to the correct range of encodable > offsets, which is [-2^31 - 2^11, 2^31 - 2^11). This also enforces the > check on the offset to other uses of emit_jump_and_link (e.g., BPF_JA) > as well. > > Currently, this bug is unlikely to be triggered, because the memory > region from which JITed images are allocated is close enough to kernel > text for the offsets to not become too large; and because the bounds on > BPF program size are small enough. This patch prevents this problem from > becoming an issue if either of these change. > > [1]: https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/b= wWFhBnnZFQ > [2]: https://github.com/riscv/riscv-isa-manual/commit/b1e42e09ac55116dbf9= de5e4fb326a5a90e4a993 > [3]: https://github.com/riscv/riscv-isa-manual/commit/4c1b2066ebd2965a422= e41eb262d0a208a7fea07 > Wow! Interesting! Thanks for fixing this! Too late to Ack, but still: Acked-by: Bj=C3=B6rn T=C3=B6pel > Signed-off-by: Luke Nelson > --- > arch/riscv/net/bpf_jit_comp64.c | 49 +++++++++++++++++++++------------ > 1 file changed, 32 insertions(+), 17 deletions(-) > > diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_com= p64.c > index cc1985d8750a..d208a9fd6c52 100644 > --- a/arch/riscv/net/bpf_jit_comp64.c > +++ b/arch/riscv/net/bpf_jit_comp64.c > @@ -110,6 +110,16 @@ static bool is_32b_int(s64 val) > return -(1L << 31) <=3D val && val < (1L << 31); > } > > +static bool in_auipc_jalr_range(s64 val) > +{ > + /* > + * auipc+jalr can reach any signed PC-relative offset in the rang= e > + * [-2^31 - 2^11, 2^31 - 2^11). > + */ > + return (-(1L << 31) - (1L << 11)) <=3D val && > + val < ((1L << 31) - (1L << 11)); > +} > + > static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx) > { > /* Note that the immediate from the add is sign-extended, > @@ -380,20 +390,24 @@ static void emit_sext_32_rd(u8 *rd, struct rv_jit_c= ontext *ctx) > *rd =3D RV_REG_T2; > } > > -static void emit_jump_and_link(u8 rd, s64 rvoff, bool force_jalr, > - struct rv_jit_context *ctx) > +static int emit_jump_and_link(u8 rd, s64 rvoff, bool force_jalr, > + struct rv_jit_context *ctx) > { > s64 upper, lower; > > if (rvoff && is_21b_int(rvoff) && !force_jalr) { > emit(rv_jal(rd, rvoff >> 1), ctx); > - return; > + return 0; > + } else if (in_auipc_jalr_range(rvoff)) { > + upper =3D (rvoff + (1 << 11)) >> 12; > + lower =3D rvoff & 0xfff; > + emit(rv_auipc(RV_REG_T1, upper), ctx); > + emit(rv_jalr(rd, RV_REG_T1, lower), ctx); > + return 0; > } > > - upper =3D (rvoff + (1 << 11)) >> 12; > - lower =3D rvoff & 0xfff; > - emit(rv_auipc(RV_REG_T1, upper), ctx); > - emit(rv_jalr(rd, RV_REG_T1, lower), ctx); > + pr_err("bpf-jit: target offset 0x%llx is out of range\n", rvoff); > + return -ERANGE; > } > > static bool is_signed_bpf_cond(u8 cond) > @@ -407,18 +421,16 @@ static int emit_call(bool fixed, u64 addr, struct r= v_jit_context *ctx) > s64 off =3D 0; > u64 ip; > u8 rd; > + int ret; > > if (addr && ctx->insns) { > ip =3D (u64)(long)(ctx->insns + ctx->ninsns); > off =3D addr - ip; > - if (!is_32b_int(off)) { > - pr_err("bpf-jit: target call addr %pK is out of r= ange\n", > - (void *)addr); > - return -ERANGE; > - } > } > > - emit_jump_and_link(RV_REG_RA, off, !fixed, ctx); > + ret =3D emit_jump_and_link(RV_REG_RA, off, !fixed, ctx); > + if (ret) > + return ret; > rd =3D bpf_to_rv_reg(BPF_REG_0, ctx); > emit(rv_addi(rd, RV_REG_A0, 0), ctx); > return 0; > @@ -429,7 +441,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, > { > bool is64 =3D BPF_CLASS(insn->code) =3D=3D BPF_ALU64 || > BPF_CLASS(insn->code) =3D=3D BPF_JMP; > - int s, e, rvoff, i =3D insn - ctx->prog->insnsi; > + int s, e, rvoff, ret, i =3D insn - ctx->prog->insnsi; > struct bpf_prog_aux *aux =3D ctx->prog->aux; > u8 rd =3D -1, rs =3D -1, code =3D insn->code; > s16 off =3D insn->off; > @@ -699,7 +711,9 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, > /* JUMP off */ > case BPF_JMP | BPF_JA: > rvoff =3D rv_offset(i, off, ctx); > - emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); > + ret =3D emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx= ); > + if (ret) > + return ret; > break; > > /* IF (dst COND src) JUMP off */ > @@ -801,7 +815,6 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, > case BPF_JMP | BPF_CALL: > { > bool fixed; > - int ret; > u64 addr; > > mark_call(ctx); > @@ -826,7 +839,9 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, st= ruct rv_jit_context *ctx, > break; > > rvoff =3D epilogue_offset(ctx); > - emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); > + ret =3D emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx= ); > + if (ret) > + return ret; > break; > > /* dst =3D imm64 */ > -- > 2.17.1 >