Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp355947ybz; Tue, 21 Apr 2020 10:17:51 -0700 (PDT) X-Google-Smtp-Source: APiQypL+WAsQo1VS/1RrugQEePWomPlDJx5T9/be8j4Rw985FrOA9ZhyBtW/PjPh1129hUMcebA/ X-Received: by 2002:a50:d615:: with SMTP id x21mr17113997edi.62.1587489471121; Tue, 21 Apr 2020 10:17:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587489471; cv=none; d=google.com; s=arc-20160816; b=nmtEv7e1HYFGB+Xy85uHEbO/QlAeP3+mphxzsiqZ8m8ySIdsQJQPid3f4XUSqtCjCv qYaGFD6KtdNh17SKmSW7rHl9XgREPj+y/fFrCII/PiPxhyuSpcHZ7rHca5viFcOVzeB5 DxYWBYYYs/uRhrcPpp0axKC6QiW0R1QWUHD/XlI1Mv1ZC4zG8H2lupj4M5dDI8cpaTis 7PKiHv0Kj9OtTYpo+NjUUWgJ0o4nlRtW0xIXiwMrUaMujNva1URWaiwBicnEV5yravoR T5zffGfTUX1VaWNaOU3NbkyRvzJTmjc/KubBnCpuHbC2Boo2O0zWGenIBRJf12jm+h+A O3nQ== 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=ljRvWFcGm52XgUtMD9But5MqOn9lfvTXOY7h72QTuF4=; b=vqu791/xJqhgckU0hbIm1nahvu3Gqw5IqPEk8yYlwwM80a/B5tD/JCPrB5tURsHjCC gW3y30Qri7kITvtBEkcivX92PeFPpx9eKFuIL5M+5qBxuDtHoHtr3y0QAGKb+KRnBGMU DGYzGg+xhn/UC83QVKcAUR3dFAB96obEL6sWBsTefux/iQI7Hahi7K8EXQgUT4u2c+f9 QfXfJ6Xo+TDRADtCjtgf2C4n+DwaA+W2ll9GzyQTOz/MAbHqDtMkhmclwL431Qa9B9w+ F2Qqq+tzsXWjTQqqnMm0bWtlSXma7YMYYNgl362t0ppwEI8R/bjplSngTw/9LGW6c5mG fATg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=LgOv8iFo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id u2si2100280ejr.370.2020.04.21.10.17.27; Tue, 21 Apr 2020 10:17:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cs.washington.edu header.s=goo201206 header.b=LgOv8iFo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1729408AbgDURQK (ORCPT + 99 others); Tue, 21 Apr 2020 13:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1729324AbgDURQH (ORCPT ); Tue, 21 Apr 2020 13:16:07 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5475C0610D5 for ; Tue, 21 Apr 2020 10:16:07 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id z6so5468472plk.10 for ; Tue, 21 Apr 2020 10:16:07 -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=ljRvWFcGm52XgUtMD9But5MqOn9lfvTXOY7h72QTuF4=; b=LgOv8iFo7ZZEXlbneCiwfho6goWloKWgxGo/Rz0rGHn1OF9OjCxjdtBD93lWXc2ryS oC+ZeG30GjIplDDTb5rlY3sS+29v6Xjreempc3mY7THUY0mn/sCkhG+akNtQspI4YGMi +eOjxBC8WDg43qS9kBWyj0nmuDk5bJie4voxo= 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=ljRvWFcGm52XgUtMD9But5MqOn9lfvTXOY7h72QTuF4=; b=lyzQE7bMPiMJVMv2hymefCOlAVhKxDBbhdZTspAZ4oIvnsyQAiV+SSVAlMMHXhkKJH JvGItk/CYSChwg6Rdhq7fJTqGNsY58T91Prh8KvwZkjVU0JrinXtpSjo1w+R7iNtgNwv Bv2y2EhEl33shDY7wypOlw5TbzK70DEt2rq3ONvxV+DRDE6nE3q5X1LxSCNwh8rwwJ6t v8gVKpImc7gEN7QJ7Grl2ikzwHU6KKI6LGmB+xwtB+3r165uqrDk55P2yFyHuZEVsh2V S4HcW229BXDdgDmfEcvDTlzCQwqb41gPJA72JoC6uhcdR2O/9QXovtPKgQ2ODNZQDdtB /gVg== X-Gm-Message-State: AGi0PuYH5w+J9vJVtjCbCEfp6OKq+jlLRbzV3wYR4fc9FTxBhlAZZHLN DzPGkcEdDQWf84DQhxP0GIxZsw== X-Received: by 2002:a17:90a:1946:: with SMTP id 6mr5214874pjh.42.1587489367134; Tue, 21 Apr 2020 10:16:07 -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 mn1sm2911459pjb.24.2020.04.21.10.16.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2020 10:16:06 -0700 (PDT) From: Luke Nelson X-Google-Original-From: Luke Nelson To: bpf@vger.kernel.org Cc: Luke Nelson , Xi Wang , Wang YanQing , "David S. Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf 1/2] bpf, x32: Fix invalid instruction in BPF_LDX zero-extension Date: Tue, 21 Apr 2020 10:15:51 -0700 Message-Id: <20200421171552.28393-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 current JIT uses the following sequence to zero-extend into the upper 32 bits of the destination register for BPF_LDX BPF_{B,H,W}, when the destination register is not on the stack: EMIT3(0xC7, add_1reg(0xC0, dst_hi), 0); However, this is not a valid instruction on x86. This patch fixes the problem by instead emitting "xor dst_hi,dst_hi" to clear the upper 32 bits. This bug may not be currently triggerable as BPF_REG_AX is the only register not stored on the stack and the verifier uses it in a limited way, and the verifier implements a zero-extension optimization. But the JIT should avoid emitting invalid instructions regardless. Fixes: 03f5781be2c7b ("bpf, x86_32: add eBPF JIT compiler for ia32") Signed-off-by: Xi Wang Signed-off-by: Luke Nelson --- arch/x86/net/bpf_jit_comp32.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c index 4d2a7a764602..cc9ad3892ea6 100644 --- a/arch/x86/net/bpf_jit_comp32.c +++ b/arch/x86/net/bpf_jit_comp32.c @@ -1854,7 +1854,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, STACK_VAR(dst_hi)); EMIT(0x0, 4); } else { - EMIT3(0xC7, add_1reg(0xC0, dst_hi), 0); + /* xor dst_hi,dst_hi */ + EMIT2(0x33, + add_2reg(0xC0, dst_hi, dst_hi)); } break; case BPF_DW: -- 2.17.1