Received: by 10.223.176.5 with SMTP id f5csp3394989wra; Mon, 29 Jan 2018 12:33:35 -0800 (PST) X-Google-Smtp-Source: AH8x226a4Yv8/NP1figNSDVauwUV+FC2CPT7mycd5GKMf8Zo0sZqpM56VNbGP1DljiHuSvm25gZI X-Received: by 10.99.170.73 with SMTP id x9mr22296536pgo.393.1517258015384; Mon, 29 Jan 2018 12:33:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517258015; cv=none; d=google.com; s=arc-20160816; b=uULd/Ew1mGYN0rjnHS8evo0nS6MJ5x3KKcQonEnyZAp0bKUNDN5RZgLnVqZBbtXj9/ XR9onYmNlWCRnGjUds/Dlr4ZwXw484mH+wWbyt7sW1CRt1qfkRdJCS2i7VB82O8C/eBU iegCOJxkHVE3IxQfWU9QcbQbyiNAohFGg+lTzFbYyKazL+WuX80+0bReK0W5dah7I+cy NZIRrEYTEss9NYj97v1iTq5e3HHd/Lp2iqdYysxEHbt1pg5zUXI2MkIUAlHCSaGNl4WY d0BzfYO4NCwuMBn0PV/2VDrC6Y7yKykKqXztjUOYCpqEnhKCMTlu4Q5zPXsNzcZ7LG3R 5czQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=/jOLGG2HaFIy6cgxYyQbAFVNHwyxLbAEz9NvpuSkeUg=; b=pJi8f1BKbR/VjQv5rGHFOUJnEBMYvxzuYSsoimqZqax8CeKlskB1EWCeQKieZLpB50 aUO1LgRac9L7Mnla5XYLx62JDGqJhLOWOXoQCHEHwfgja8R/FtBB+BiNp4pODsb8af+l RzczX88Hatpmq3j/9rsKPH9zbG9C9CS0xRV0fq2eU+Cb0oCT4fZ4PDfTX1G/xh/rZWIA WnNYxdvs4oVuszRVBoG5WGutwQAWmy+m166fU8dXywiaf53k7Pqx0rjKXDiJldYTJ+2d 6PVADv8W3DJftCFrGttwSsY6m9jarEE9B5Rir/HHUxKO3GE54OlfdDs7OOaHXXjeJpv6 rEAQ== ARC-Authentication-Results: i=1; mx.google.com; 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 b23si7995628pgn.527.2018.01.29.12.33.20; Mon, 29 Jan 2018 12:33:35 -0800 (PST) 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; 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 S1754262AbeA2UOy (ORCPT + 99 others); Mon, 29 Jan 2018 15:14:54 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:41068 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754230AbeA2UOr (ORCPT ); Mon, 29 Jan 2018 15:14:47 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id CFA102FC5; Mon, 29 Jan 2018 13:07:15 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "ast@kernel.org, stable@vger.kernel.org, Daniel Borkmann" , Alexei Starovoitov , Daniel Borkmann Subject: [PATCH 4.9 66/66] bpf: reject stores into ctx via st and xadd Date: Mon, 29 Jan 2018 13:57:30 +0100 Message-Id: <20180129123843.369270247@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180129123839.842860149@linuxfoundation.org> References: <20180129123839.842860149@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Borkmann [ upstream commit f37a8cb84cce18762e8f86a70bd6a49a66ab964c ] Alexei found that verifier does not reject stores into context via BPF_ST instead of BPF_STX. And while looking at it, we also should not allow XADD variant of BPF_STX. The context rewriter is only assuming either BPF_LDX_MEM- or BPF_STX_MEM-type operations, thus reject anything other than that so that assumptions in the rewriter properly hold. Add test cases as well for BPF selftests. Fixes: d691f9e8d440 ("bpf: allow programs to write to certain skb fields") Reported-by: Alexei Starovoitov Signed-off-by: Daniel Borkmann Signed-off-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -702,6 +702,13 @@ static bool is_pointer_value(struct bpf_ return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]); } +static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) +{ + const struct bpf_reg_state *reg = &env->cur_state.regs[regno]; + + return reg->type == PTR_TO_CTX; +} + static int check_ptr_alignment(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int off, int size) { @@ -896,6 +903,12 @@ static int check_xadd(struct bpf_verifie return -EACCES; } + if (is_ctx_reg(env, insn->dst_reg)) { + verbose("BPF_XADD stores into R%d context is not allowed\n", + insn->dst_reg); + return -EACCES; + } + /* check whether atomic_add can read the memory */ err = check_mem_access(env, insn->dst_reg, insn->off, BPF_SIZE(insn->code), BPF_READ, -1); @@ -3012,6 +3025,12 @@ static int do_check(struct bpf_verifier_ if (err) return err; + if (is_ctx_reg(env, insn->dst_reg)) { + verbose("BPF_ST stores into R%d context is not allowed\n", + insn->dst_reg); + return -EACCES; + } + /* check that memory (dst_reg + off) is writeable */ err = check_mem_access(env, insn->dst_reg, insn->off, BPF_SIZE(insn->code), BPF_WRITE,