Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3926720pxj; Mon, 21 Jun 2021 09:31:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJycFtdUltyAdhzNwJ3x4Zk1aw/OnEowcY+55v2/5HQObHF+LOoiqwl1Eb8Gy5WAlmLSnxeU X-Received: by 2002:a02:2547:: with SMTP id g68mr18282704jag.24.1624293119071; Mon, 21 Jun 2021 09:31:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293119; cv=none; d=google.com; s=arc-20160816; b=kqSlPeR7QGRlNdMTvCO1Bq79/MvNO1rorOZIkg7sVipC8uQJTxyE1160ept1B0vk4W 9CsmC8yPCaog0KnAJU6NhUW2whbVi4T2SmDVRLD7ou3r83nHe8ZlGOvRP+O0TGjc+CvM /pcovkUmHgB31R02Gfdu07TfT27z3mL2jrV5oLMaNo5hnHAX5khgQ71xsqpIC8LndZa6 H7gcWgMCdOYs7lLX4oNgjMCo22CqeFVyF15XMVYIWeHt72PD84Cj0I9zq17UhlIocNDT yWdG9PNP2TWVmAxabU3I2HiHsnCPKXMHRSwdSdu64k5D2kGJpEdwhtDrLF9izEWhYDtR Isxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=cc5V11Hhhd2Ap4xgMZwqrF70kJMENR+yzn6zL4j0AxE=; b=NJSGVibgDZJ0UR+eo4VM8CbDPAErkr+2niSJZWdQuD7WF0hDndmXGA1838w89kCZWr hocSmvXPVhCyx4g4UhE8Adpj2qcFeSAsYroAYnJGP8xHLuI/PS/VzQxmuvjTdR+Zd+aa iGhSlMMGpv2gIj0ZlnN0MTast5UN4L/C25dsWf0Xx4iXOfbCeXdJF3uxhOYcgPaRKvDM Unz9yCRL4mDLrN8ayGVeveu47zlsLJe7WjEr6tDOcBHJBv/gUET/gVnDAOfxzUeQZvTc RlDN1R4u1afSjbn6sCKKTF1xyWipdif7R/wyMmnm8YHAhKOVXpmUBI4iWUsmxz9rNDCX ricw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dwZ5A+vJ; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y7si20240851jae.125.2021.06.21.09.31.46; Mon, 21 Jun 2021 09:31:59 -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=@linuxfoundation.org header.s=korg header.b=dwZ5A+vJ; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230526AbhFUQdd (ORCPT + 99 others); Mon, 21 Jun 2021 12:33:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:48558 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232172AbhFUQaD (ORCPT ); Mon, 21 Jun 2021 12:30:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AA1E76128A; Mon, 21 Jun 2021 16:24:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292698; bh=fyz4LlRTTGrbLQ1pR9fEG6T7zqGalpkxKdLx6x7nCpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dwZ5A+vJpZXEE361eEnfsoa5/mIa2DRZP04DpZytR/8shmWMWv3ZE5uwOVkP368VC fXfyQuxwqBoqJTtDma8nqcYQw5A1D3w7hqlSxy//WxiLpcXX/hLnpfm3BQ4tlHD9T8 aRVE5z7fhU58fkJqL0eiybV9PdHBujRhlunGX4Rs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , John Fastabend , Benedict Schlueter , Piotr Krysiuk , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.10 097/146] bpf: Do not mark insn as seen under speculative path verification Date: Mon, 21 Jun 2021 18:15:27 +0200 Message-Id: <20210621154917.347582192@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Borkmann [ Upstream commit fe9a5ca7e370e613a9a75a13008a3845ea759d6e ] ... in such circumstances, we do not want to mark the instruction as seen given the goal is still to jmp-1 rewrite/sanitize dead code, if it is not reachable from the non-speculative path verification. We do however want to verify it for safety regardless. With the patch as-is all the insns that have been marked as seen before the patch will also be marked as seen after the patch (just with a potentially different non-zero count). An upcoming patch will also verify paths that are unreachable in the non-speculative domain, hence this extension is needed. Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Reviewed-by: Benedict Schlueter Reviewed-by: Piotr Krysiuk Acked-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 71ac1da127a6..e97724e36dfb 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5851,6 +5851,19 @@ do_sim: return !ret ? REASON_STACK : 0; } +static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) +{ + struct bpf_verifier_state *vstate = env->cur_state; + + /* If we simulate paths under speculation, we don't update the + * insn as 'seen' such that when we verify unreachable paths in + * the non-speculative domain, sanitize_dead_code() can still + * rewrite/sanitize them. + */ + if (!vstate->speculative) + env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; +} + static int sanitize_err(struct bpf_verifier_env *env, const struct bpf_insn *insn, int reason, const struct bpf_reg_state *off_reg, @@ -9847,7 +9860,7 @@ static int do_check(struct bpf_verifier_env *env) } regs = cur_regs(env); - env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; + sanitize_mark_insn_seen(env); prev_insn_idx = env->insn_idx; if (class == BPF_ALU || class == BPF_ALU64) { @@ -10067,7 +10080,7 @@ process_bpf_exit: return err; env->insn_idx++; - env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; + sanitize_mark_insn_seen(env); } else { verbose(env, "invalid BPF_LD mode\n"); return -EINVAL; @@ -11741,6 +11754,9 @@ static void free_states(struct bpf_verifier_env *env) * insn_aux_data was touched. These variables are compared to clear temporary * data from failed pass. For testing and experiments do_check_common() can be * run multiple times even when prior attempt to verify is unsuccessful. + * + * Note that special handling is needed on !env->bypass_spec_v1 if this is + * ever called outside of error path with subsequent program rejection. */ static void sanitize_insn_aux_data(struct bpf_verifier_env *env) { -- 2.30.2