Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp789541imm; Mon, 21 May 2018 14:30:00 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpPssCKny454YkkCc2X9GJioO+u0w8y6ehf1g8Htodu1rNn9/RC8iE3TpvravvbLJVoFT01 X-Received: by 2002:a62:fd12:: with SMTP id p18-v6mr21394355pfh.152.1526938200838; Mon, 21 May 2018 14:30:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526938200; cv=none; d=google.com; s=arc-20160816; b=vPa4cknWPHpuE2JVN26zjJ16AuJd+WoXSuHpd7hS4gLMV+rHYofFEMMRiQW7UTfHuw u4ZAMXiiMtFOShpCmBvWjJDNraMGvTA0UehRfNSlRqTa8SfL5HNpI1GF8D2rq3bRb3PU 2BsCmP5/Mi1p4n2YRBpaHBszO6KRv2puZP4Sqw3EVB9BhPzL9TyWszWkWaQqdbodvHks zlzcmpbiZu+GffzGbsFdQsJgo74/w2gYOwaCq0QM3K+mY9+oE2kHtTZ/LbxTVB3/b3Gi 5/uzF7zvr14B2m6cMGDFZCTcqx1CuS6R/MG7b525Rv17h2P8S8ZAb6y5Jm+0oTfjbL7g Qs/w== 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:dkim-signature :arc-authentication-results; bh=1oZ0t6gpftp9iJm02ejBBZoTWFPvfY8LoDmYv3SaunE=; b=RuT2b2ve99An7S5p6DDO+XvTFmEqW7euuFA64gKnpy59whmBQmMfy1aPtgbk/OIGDN wXN4wAG0PfMUb5CGJsggC/l2R4ixvs9c+FEz7bPpeEEf5IXl3Jx0f8dq4zvBumMNzWso hMcB0PTizCo5QiGIzow9vF4/rST437IWUvfKba0JirQaxNvlj/w424QcCkdTbuAUYJs3 VM23FfST0Lo8fkRm9a8DuCU76bmw3MYQy34ihXT9K98JcReKXUWRNkVls2m5tUkVAkvL uz+P+g2NxDWfdtPFeTkYLZjQFHAo59yHL/VZHE6L5kLqWeCWLIs1mQHXADkbF8DND7AP nKkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yn0r8fzb; 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 g14-v6si14835391plj.146.2018.05.21.14.29.46; Mon, 21 May 2018 14:30:00 -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=yn0r8fzb; 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 S932666AbeEUV1C (ORCPT + 99 others); Mon, 21 May 2018 17:27:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:41768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932660AbeEUV04 (ORCPT ); Mon, 21 May 2018 17:26:56 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (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 C2D1420853; Mon, 21 May 2018 21:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1526938016; bh=SHBqCjtEaLcANM7ojE/8ZSkU0hLC9cBWmsOD3rkeB8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yn0r8fzbTN13TKypiNJ5yl5vkrVYwR67pfKnbtLP/JTIeFY2hAgTsDMVZUQclx1xI 5gxqAApTSKxKaPu6Jp3HQylktRCnlAa3Umz3JRr/neYHaY7KwnrsykRR1f6s13eRzV 6yxV3jxf4qzUxt6uhUD99St/ZS+lOc/TlrYbvFsc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexei Starovoitov , Thomas Gleixner Subject: [PATCH 4.16 110/110] bpf: Prevent memory disambiguation attack Date: Mon, 21 May 2018 23:12:47 +0200 Message-Id: <20180521210515.145840770@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180521210503.823249477@linuxfoundation.org> References: <20180521210503.823249477@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov commit af86ca4e3088fe5eacf2f7e58c01fa68ca067672 upstream Detect code patterns where malicious 'speculative store bypass' can be used and sanitize such patterns. 39: (bf) r3 = r10 40: (07) r3 += -216 41: (79) r8 = *(u64 *)(r7 +0) // slow read 42: (7a) *(u64 *)(r10 -72) = 0 // verifier inserts this instruction 43: (7b) *(u64 *)(r8 +0) = r3 // this store becomes slow due to r8 44: (79) r1 = *(u64 *)(r6 +0) // cpu speculatively executes this load 45: (71) r2 = *(u8 *)(r1 +0) // speculatively arbitrary 'load byte' // is now sanitized Above code after x86 JIT becomes: e5: mov %rbp,%rdx e8: add $0xffffffffffffff28,%rdx ef: mov 0x0(%r13),%r14 f3: movq $0x0,-0x48(%rbp) fb: mov %rdx,0x0(%r14) ff: mov 0x0(%rbx),%rdi 103: movzbq 0x0(%rdi),%rsi Signed-off-by: Alexei Starovoitov Signed-off-by: Thomas Gleixner Signed-off-by: Greg Kroah-Hartman --- include/linux/bpf_verifier.h | 1 kernel/bpf/verifier.c | 59 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 3 deletions(-) --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -146,6 +146,7 @@ struct bpf_insn_aux_data { s32 call_imm; /* saved imm field of call insn */ }; int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ + int sanitize_stack_off; /* stack slot to be cleared */ bool seen; /* this insn was processed by the verifier */ }; --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -970,7 +970,7 @@ static bool register_is_null(struct bpf_ */ static int check_stack_write(struct bpf_verifier_env *env, struct bpf_func_state *state, /* func where register points to */ - int off, int size, int value_regno) + int off, int size, int value_regno, int insn_idx) { struct bpf_func_state *cur; /* state of the current function */ int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; @@ -1009,8 +1009,33 @@ static int check_stack_write(struct bpf_ state->stack[spi].spilled_ptr = cur->regs[value_regno]; state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; - for (i = 0; i < BPF_REG_SIZE; i++) + for (i = 0; i < BPF_REG_SIZE; i++) { + if (state->stack[spi].slot_type[i] == STACK_MISC && + !env->allow_ptr_leaks) { + int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off; + int soff = (-spi - 1) * BPF_REG_SIZE; + + /* detected reuse of integer stack slot with a pointer + * which means either llvm is reusing stack slot or + * an attacker is trying to exploit CVE-2018-3639 + * (speculative store bypass) + * Have to sanitize that slot with preemptive + * store of zero. + */ + if (*poff && *poff != soff) { + /* disallow programs where single insn stores + * into two different stack slots, since verifier + * cannot sanitize them + */ + verbose(env, + "insn %d cannot access two stack slots fp%d and fp%d", + insn_idx, *poff, soff); + return -EINVAL; + } + *poff = soff; + } state->stack[spi].slot_type[i] = STACK_SPILL; + } } else { u8 type = STACK_MISC; @@ -1685,7 +1710,7 @@ static int check_mem_access(struct bpf_v if (t == BPF_WRITE) err = check_stack_write(env, state, off, size, - value_regno); + value_regno, insn_idx); else err = check_stack_read(env, state, off, size, value_regno); @@ -5156,6 +5181,34 @@ static int convert_ctx_accesses(struct b else continue; + if (type == BPF_WRITE && + env->insn_aux_data[i + delta].sanitize_stack_off) { + struct bpf_insn patch[] = { + /* Sanitize suspicious stack slot with zero. + * There are no memory dependencies for this store, + * since it's only using frame pointer and immediate + * constant of zero + */ + BPF_ST_MEM(BPF_DW, BPF_REG_FP, + env->insn_aux_data[i + delta].sanitize_stack_off, + 0), + /* the original STX instruction will immediately + * overwrite the same stack slot with appropriate value + */ + *insn, + }; + + cnt = ARRAY_SIZE(patch); + new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); + if (!new_prog) + return -ENOMEM; + + delta += cnt - 1; + env->prog = new_prog; + insn = new_prog->insnsi + i + delta; + continue; + } + if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) continue;