Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3758738ybb; Mon, 6 Apr 2020 15:18:07 -0700 (PDT) X-Google-Smtp-Source: APiQypKmgDLpeOtytJFdXiL+Bz3CAhglpdDrukbEO1mqdEO+VsPPayXLV/8X5Wbg1at9L9MU9dma X-Received: by 2002:a05:6830:19a:: with SMTP id q26mr20146404ota.285.1586211487483; Mon, 06 Apr 2020 15:18:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586211487; cv=none; d=google.com; s=arc-20160816; b=b+8u/3icxJWK6tDR01SVeBjuMqGidb+hAbPXaN9Mob29tqpfIJbCWVa/8F9crOBmlZ IS9hID77JsiwGqeTs3oojBX8mH7T48rIKGn5BDe7BbwpeUU9rezK17DVjo3WATRIVmTn 8QjLQurAYnsZkF6VjreyWFyaxrxHF/t0dz9Cuxewzp+4dEh6PkVvvI9Zk0ktb9zFMJWd dh7usnRtjJGU92OBTdOdMOs4pkyjw3xGfcP3e+E15ydDJ675uEyrpddbgHhSdgeJrJpt T6B930iC78gPlZWD4UvTL0QpUkY5kDQ674KTCTH7I9ikor2dsUeaPs50Uq3k/oqrLawU 5Lvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=I8Ag8RJmxCZk7aftpHG1/aOT8pIgsaZRRBMA74l4szs=; b=rsAHT5wDdCKxvm35Atc78/HB9hqLC2JBMv2yrBNGeMgfxOagNUgA0fzrFVSmiGleTg ydlYkgIZV+bGPZCDBU3qTpcDtEuGMwI0UzAgWXJwXHlQP6mVdofsL4BsO89/9/wprfDX DyLf7AB8EfMiR6zDJ1t26d7DqgRHQyu6ghFfyULyGKaPX6YuRJysokZE/AzsV/AdOJhO rGdwMMIX+OSLH46PU6XmE6fhI1gefmhPWf4rM6JiiLodGN0/zGR+gGQKp3X5fc0Qjy1k vFcrMQmomzLe2FIoKxUTRGUBa53qQlVxosCiSXaUVfFGHxe5b1lIuE5Kdd3IJrBPzBG+ 0CqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=M8J8NhOn; 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=NONE dis=NONE) header.from=cs.washington.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b19si506869ots.224.2020.04.06.15.17.55; Mon, 06 Apr 2020 15:18:07 -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=@cs.washington.edu header.s=goo201206 header.b=M8J8NhOn; 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=NONE dis=NONE) header.from=cs.washington.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726636AbgDFWQa (ORCPT + 99 others); Mon, 6 Apr 2020 18:16:30 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:36072 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726112AbgDFWQa (ORCPT ); Mon, 6 Apr 2020 18:16:30 -0400 Received: by mail-pg1-f194.google.com with SMTP id c23so722909pgj.3 for ; Mon, 06 Apr 2020 15:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; h=from:to:cc:subject:date:message-id; bh=I8Ag8RJmxCZk7aftpHG1/aOT8pIgsaZRRBMA74l4szs=; b=M8J8NhOnd6VsGu2mROnqMiucZGBJHkCFpdPuRUXTHrWBoiRpSX/zdOAAme47f8IpmO LbxP9RDtmYBA4sFMzviQSaX6l/c2ApCo5J9ZISFFgE3ic036efVrwB2nD7wkS1aBm/gE pEWLs6KKpMP1482IJ5rRxQ3ubJH7OiJ35zFuM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=I8Ag8RJmxCZk7aftpHG1/aOT8pIgsaZRRBMA74l4szs=; b=iU3NmA6LOoE6MyAXncK2WqxBfxX9EG08kFbq3iq4OLC/lBVUZsYrrPMJU8trykmI2u iWVKLSi2FWH4z1q8PlsdeM7/uyfKXMr/TLmBZmvOgsIzSacn3nQqMHae4JFQ47fc+uJe LrJL/Ia7BIBRzBNrFgm9lWX/rqKRM6pg8IhhoP6qXlceiMAihV1MFOiDOg+ry8myul4G zMlGDbLD04R5QtALYoU8WBdLd8Kv0UxEUOXj+XFVCibfAJ1+/cwZpRUdI287Nk2w/9Uh lEwjQbwZx0kH/we6IOA1QxFAP/974RdL70s5MqZrN6N6S/GynSTDO+ybNTPOJL3/5Yql eOOQ== X-Gm-Message-State: AGi0PuaYcjDzUhaRVJ46hIHV/GvLoukGIXipXRRnOQI0wOJLBCVPxRM1 WQw1QhbctDhoQgeh+RMZqL3g+g== X-Received: by 2002:a63:dd09:: with SMTP id t9mr1182732pgg.432.1586211388598; Mon, 06 Apr 2020 15:16:28 -0700 (PDT) Received: from localhost.localdomain (c-73-53-94-119.hsd1.wa.comcast.net. [73.53.94.119]) by smtp.gmail.com with ESMTPSA id s12sm11714998pgi.38.2020.04.06.15.16.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Apr 2020 15:16:28 -0700 (PDT) From: Luke Nelson X-Google-Original-From: Luke Nelson To: bpf@vger.kernel.org Cc: Xi Wang , Luke Nelson , =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , netdev@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf] riscv, bpf: Fix offset range checking for auipc+jalr on RV64 Date: Mon, 6 Apr 2020 22:16:04 +0000 Message-Id: <20200406221604.18547-1-luke.r.nels@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/bwWFhBnnZFQ [2]: https://github.com/riscv/riscv-isa-manual/commit/b1e42e09ac55116dbf9de5e4fb326a5a90e4a993 [3]: https://github.com/riscv/riscv-isa-manual/commit/4c1b2066ebd2965a422e41eb262d0a208a7fea07 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_comp64.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) <= val && val < (1L << 31); } +static bool in_auipc_jalr_range(s64 val) +{ + /* + * auipc+jalr can reach any signed PC-relative offset in the range + * [-2^31 - 2^11, 2^31 - 2^11). + */ + return (-(1L << 31) - (1L << 11)) <= 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_context *ctx) *rd = 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 = (rvoff + (1 << 11)) >> 12; + lower = rvoff & 0xfff; + emit(rv_auipc(RV_REG_T1, upper), ctx); + emit(rv_jalr(rd, RV_REG_T1, lower), ctx); + return 0; } - upper = (rvoff + (1 << 11)) >> 12; - lower = 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 rv_jit_context *ctx) s64 off = 0; u64 ip; u8 rd; + int ret; if (addr && ctx->insns) { ip = (u64)(long)(ctx->insns + ctx->ninsns); off = addr - ip; - if (!is_32b_int(off)) { - pr_err("bpf-jit: target call addr %pK is out of range\n", - (void *)addr); - return -ERANGE; - } } - emit_jump_and_link(RV_REG_RA, off, !fixed, ctx); + ret = emit_jump_and_link(RV_REG_RA, off, !fixed, ctx); + if (ret) + return ret; rd = 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, struct rv_jit_context *ctx, { bool is64 = BPF_CLASS(insn->code) == BPF_ALU64 || BPF_CLASS(insn->code) == BPF_JMP; - int s, e, rvoff, i = insn - ctx->prog->insnsi; + int s, e, rvoff, ret, i = insn - ctx->prog->insnsi; struct bpf_prog_aux *aux = ctx->prog->aux; u8 rd = -1, rs = -1, code = insn->code; s16 off = insn->off; @@ -699,7 +711,9 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, /* JUMP off */ case BPF_JMP | BPF_JA: rvoff = rv_offset(i, off, ctx); - emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); + ret = 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, struct 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, struct rv_jit_context *ctx, break; rvoff = epilogue_offset(ctx); - emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); + ret = emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx); + if (ret) + return ret; break; /* dst = imm64 */ -- 2.17.1