From patchwork Sat Nov 29 22:46:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 416022 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id D1C45140082 for ; Sun, 30 Nov 2014 09:46:34 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751825AbaK2WqR (ORCPT ); Sat, 29 Nov 2014 17:46:17 -0500 Received: from mail-pa0-f42.google.com ([209.85.220.42]:57473 "EHLO mail-pa0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751735AbaK2WqP (ORCPT ); Sat, 29 Nov 2014 17:46:15 -0500 Received: by mail-pa0-f42.google.com with SMTP id et14so8727811pad.29 for ; Sat, 29 Nov 2014 14:46:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Nli+hjDaDgO/llX8ZxPKs+jo2DE/uLnF5icx4g8ObgA=; b=TkUfXnhW//5nxNP6aoAmTgEfcQhgaLRl4E70PSG2BexgZrv5P7S1cQ/mrPfwsdS4hv 9N0//v/WH4DCIcggOt3JBoOu0YxsDAQQ7nqdzG9ux0SEsG7sPHAGWXeZhem+dY/OqC9Q mVMGW4hVvhCZRenq7Kg+w1pwQFi5Znny3uqf5LNalfOH7wfVChcdOIgrM77sGzeILpMU rVkI/EO7B5qBZMggtRdWAWIz7TXJSJuMkWqneXWY8n/66y3rlOBYJ0ThoFh6VUymNYEP r4CHAKEW2my0cIyjvBJu2TsxMSur+zFCFSKvZRJMNNmGbSPBneRusbr8C6707UAKdOr7 iFXQ== X-Gm-Message-State: ALoCoQkjz56ZbmlByptVa7mLD6YoWAnAcgCYMLQQgbpJTMI4XoZvTUFbaZ6FvYUV3LmM7ZDHok2N X-Received: by 10.70.133.41 with SMTP id oz9mr86133584pdb.46.1417301175202; Sat, 29 Nov 2014 14:46:15 -0800 (PST) Received: from localhost.localdomain ([12.229.56.226]) by mx.google.com with ESMTPSA id ws4sm9799926pbc.53.2014.11.29.14.46.14 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 29 Nov 2014 14:46:14 -0800 (PST) From: Alexei Starovoitov To: "David S. Miller" Cc: Zi Shen Lim , Eric Dumazet , Daniel Borkmann , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 net] bpf: x86: fix epilogue generation for eBPF programs Date: Sat, 29 Nov 2014 14:46:13 -0800 Message-Id: <1417301173-23691-1-git-send-email-ast@plumgrid.com> X-Mailer: git-send-email 1.7.9.5 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org classic BPF has a restriction that last insn is always BPF_RET. eBPF doesn't have BPF_RET instruction and this restriction. It has BPF_EXIT insn which can appear anywhere in the program one or more times and it doesn't have to be last insn. Fix eBPF JIT to emit epilogue when first BPF_EXIT is seen and all other BPF_EXIT instructions will be emitted as jump. Since jump offset to epilogue is computed as: jmp_offset = ctx->cleanup_addr - addrs[i] we need to change type of cleanup_addr to signed to compute the offset as: (long long) ((int)20 - (int)30) instead of: (long long) ((unsigned int)20 - (int)30) Fixes: 622582786c9e ("net: filter: x86: internal BPF JIT") Signed-off-by: Alexei Starovoitov --- Note, this bug is applicable only to native eBPF programs which first were introduced in 3.18, so no need for stable. arm64 JIT has the same problem, but the fix is not as trivial, so will be done as separate patch. Since 3.18 can only load eBPF programs and cannot execute them, this patch can even be done in net-next only, but I think it's worth to apply it to 3.18(net), so that JITed output for native eBPF programs is correct when bpf syscall loads it with net.core.bpf_jit_enable=2 arch/x86/net/bpf_jit_comp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 3f62734..7e90244 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -178,7 +178,7 @@ static void jit_fill_hole(void *area, unsigned int size) } struct jit_context { - unsigned int cleanup_addr; /* epilogue code offset */ + int cleanup_addr; /* epilogue code offset */ bool seen_ld_abs; }; @@ -192,6 +192,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, struct bpf_insn *insn = bpf_prog->insnsi; int insn_cnt = bpf_prog->len; bool seen_ld_abs = ctx->seen_ld_abs | (oldproglen == 0); + bool seen_exit = false; u8 temp[BPF_MAX_INSN_SIZE + BPF_INSN_SAFETY]; int i; int proglen = 0; @@ -854,10 +855,11 @@ common_load: goto common_load; case BPF_JMP | BPF_EXIT: - if (i != insn_cnt - 1) { + if (seen_exit) { jmp_offset = ctx->cleanup_addr - addrs[i]; goto emit_jmp; } + seen_exit = true; /* update cleanup_addr */ ctx->cleanup_addr = proglen; /* mov rbx, qword ptr [rbp-X] */