Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp884109yba; Thu, 18 Apr 2019 11:10:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqwkqrMcsz8g8PlSxFp/Bts/ui0Sy8jJxLfXypgNQ9EpoJD5KFEjCdvzgqCHbjU154KIwMwY X-Received: by 2002:a17:902:2927:: with SMTP id g36mr641377plb.6.1555611018623; Thu, 18 Apr 2019 11:10:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555611018; cv=none; d=google.com; s=arc-20160816; b=LrHoaAUdjVVHvRl/z20gpuJ3qlIOwPi12K2VvhgbRYNrnIIQGQ1jck8JyWMzWXokph 19j+UGEKYLfh6bED4dH98fYMTKiORedLm06arXW/JTVuKj7xtX8+M603aQJ8kvF1jf2i QwZs8G1mzYLe0vRTVSwUutevyYqdg/+5adfoooyxEH9b1SuHfh52kgTyWhYEYQBshMcs Owj1DyCOOrs9Q2sNx+htw96Ap8CXy8jCcQUMi+CDEPa36Kp+t4iiJLZdA6Q5yWVPc9aO A85QqZuAymUeDtw6MX0mm9PK2CcyQRhlE6B60S7SkMP8cA92CjbDnrTKiT78sRkITk3x fqHg== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZiVPMyLQOM9mWB6WI7KK5xfgg2pA870Vfo2ILxPtW00=; b=ww5JcuGGHec3TCzDUfBzs/fRbePqIjVspgw8H914yz6Ai8733i16S977TbGKfpJiGW tHjZaBe+wMTs2f3nBt5622LBTv2cDSehNvG5Y3h4ucRjyKJwcm9IZ2ZajlW00yTpUflv 81NJqTyXRIwBCJeJ/nbovqTlapkTwt3hhdIQXsnRW8S9W/p0u4pDx/JUSKgqxvGVKZ+i xhTdzYxQiIofQVzP/lfusR3LWo/WsaLR2eiUUORPc/XxCZQBZvVzq3q9NwQgehHmIxlR 8HWIPQf1Y4MflU1LFA0Wm5EYNwz10nTJUSLz7XGC/DLe0biDdBWWQMzC2yaEABT9qOD6 SJOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Kv3SM8W/"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d25si2616720pgb.229.2019.04.18.11.10.04; Thu, 18 Apr 2019 11:10:18 -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=@kernel.org header.s=default header.b="Kv3SM8W/"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391598AbfDRSHL (ORCPT + 99 others); Thu, 18 Apr 2019 14:07:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:37314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391591AbfDRSHJ (ORCPT ); Thu, 18 Apr 2019 14:07:09 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 70B88218DA; Thu, 18 Apr 2019 18:07:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555610828; bh=cmqXPQJXxvTCRuC2Nqr0FsEmX7kmCJ3YIjWZ23rz1aw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kv3SM8W/liVE1Gvr/xgZiAE91cJEV3mfLfGAiL5iCDNU7bzIxsK4P+r8Srm7n4Wdt UIaeJJl4nosaOPg69NsycDNciiqPrVAV5cTVxh2DF97AlzEwBVQmyOVajDmWnJya8g zjQW1r0IAausrAkwWfyTTNBNn23VKAuNgIsJpVu0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , Alexei Starovoitov , Balbir Singh Subject: [PATCH 4.14 79/92] bpf: enable access to ax register also from verifier rewrite Date: Thu, 18 Apr 2019 19:57:37 +0200 Message-Id: <20190418160437.341757519@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190418160430.325165109@linuxfoundation.org> References: <20190418160430.325165109@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Borkmann commit 9b73bfdd08e73231d6a90ae6db4b46b3fbf56c30 upstream. Right now we are using BPF ax register in JIT for constant blinding as well as in interpreter as temporary variable. Verifier will not be able to use it simply because its use will get overridden from the former in bpf_jit_blind_insn(). However, it can be made to work in that blinding will be skipped if there is prior use in either source or destination register on the instruction. Taking constraints of ax into account, the verifier is then open to use it in rewrites under some constraints. Note, ax register already has mappings in every eBPF JIT. Signed-off-by: Daniel Borkmann Acked-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov [backported to 4.14 sblbir] Signed-off-by: Balbir Singh Signed-off-by: Greg Kroah-Hartman --- include/linux/filter.h | 7 +------ kernel/bpf/core.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -46,12 +46,7 @@ struct bpf_prog_aux; #define BPF_REG_X BPF_REG_7 #define BPF_REG_TMP BPF_REG_8 -/* Kernel hidden auxiliary/helper register for hardening step. - * Only used by eBPF JITs. It's nothing more than a temporary - * register that JITs use internally, only that here it's part - * of eBPF instructions that have been rewritten for blinding - * constants. See JIT pre-step in bpf_jit_blind_constants(). - */ +/* Kernel hidden auxiliary/helper register. */ #define BPF_REG_AX MAX_BPF_REG #define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) #define MAX_BPF_JIT_REG MAX_BPF_EXT_REG --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -553,6 +553,26 @@ static int bpf_jit_blind_insn(const stru BUILD_BUG_ON(BPF_REG_AX + 1 != MAX_BPF_JIT_REG); BUILD_BUG_ON(MAX_BPF_REG + 1 != MAX_BPF_JIT_REG); + /* Constraints on AX register: + * + * AX register is inaccessible from user space. It is mapped in + * all JITs, and used here for constant blinding rewrites. It is + * typically "stateless" meaning its contents are only valid within + * the executed instruction, but not across several instructions. + * There are a few exceptions however which are further detailed + * below. + * + * Constant blinding is only used by JITs, not in the interpreter. + * The interpreter uses AX in some occasions as a local temporary + * register e.g. in DIV or MOD instructions. + * + * In restricted circumstances, the verifier can also use the AX + * register for rewrites as long as they do not interfere with + * the above cases! + */ + if (from->dst_reg == BPF_REG_AX || from->src_reg == BPF_REG_AX) + goto out; + if (from->imm == 0 && (from->code == (BPF_ALU | BPF_MOV | BPF_K) || from->code == (BPF_ALU64 | BPF_MOV | BPF_K))) {