Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3939750pxj; Mon, 21 Jun 2021 09:48:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJziw7wC0Z/yAeVZCTpKFoNq468jzM1fT0oVwlGJ/q+IHjXMukXVr9ABSXPtjZkYAs8PLff0 X-Received: by 2002:a05:6e02:16c5:: with SMTP id 5mr10777728ilx.72.1624294123733; Mon, 21 Jun 2021 09:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294123; cv=none; d=google.com; s=arc-20160816; b=H02wQZdgS2CDP4DCAnR4U+1/wg0fUkWsUVzIgFEIJnRY4759LBi7ZHubVieuFKMLpY F17TvPZ4g36wwFWcN/vk2JpJPRfc85xjC0LjMbEpOpw6NPYegW/mcyVhtkaeBnXTaexD OLsTwn9Fi6aKZVTCJTyJ1TUMgief3K1lkBERnq8FIG8KT8tVNjXDnQWqU0/35rscCtkE vTZsoXYDmCj6oqyMn39RsLa3+HRtO1qWcj1n19snsZVPCO0QEHx9SVO4ay7D74FgYuG9 dt4VjKMeZMa+RYlSDL6QWqzydvReGY+lTXruTmtKSPXX4NejWMJPttaUZqYoQvWdczLW W8Tw== 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=KK2Rm9kXBjdlZWyLubqY18en+jdgezg4Hji4C0Rmbmk=; b=anz/cpo5rr8XwGpgCzXGdCNahN4MTFwK3OmjsDcfuIr/h19s68MloawHtXk2Q6ecQE 1wfZhBpFsm47N9zafP6eV7KOB1/U/5y+7BnFjcxSLorj2J+0aWEI4zr0aYoO7Yg1Nuf+ jj6utsxGKAfNU1mUBGmDYbqfguaxgJQkIqa1UuFzngaMgMe0N2g7798cEA1gLPN8a3uk vJhz+nMMBEnw+zFfMpFBvNWbsglfnNm9Ku6bPPfgxcZArsAzHx6SSrUaCN0kBi8SgwqG cOuAPPrgqm5F7mnTrJ2seBaiCRZcW8rLq+MlnIb6F5b5ebBXeow4GeIUGutPATwPW4dO 9tng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="S9WfgaZ/"; 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 s11si8310446ioo.1.2021.06.21.09.48.27; Mon, 21 Jun 2021 09:48:43 -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="S9WfgaZ/"; 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 S233096AbhFUQtO (ORCPT + 99 others); Mon, 21 Jun 2021 12:49:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:37918 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233673AbhFUQpL (ORCPT ); Mon, 21 Jun 2021 12:45:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9C44A61452; Mon, 21 Jun 2021 16:32:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293149; bh=Td7cUIKJY3F8pUsaqn1P22T2Q9WA3YGHRGY4mLQv/XM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S9WfgaZ/zNJ3fkGulb+mVGQO/WiHtxqvjMtXusaISbxY14nXiN2PffXDDRweguGs9 dbWjqZTrvF2YPIeyCd1D/U7toiSbbXGekamN9aHPk6JZaeZ3HuGeR4/QS5bqP1YEYM EqYfNpbeT51h/1o3Yx6SuCT5oSF00IreCijElEAE= 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.12 116/178] bpf: Do not mark insn as seen under speculative path verification Date: Mon, 21 Jun 2021 18:15:30 +0200 Message-Id: <20210621154926.722632557@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@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 0ffe85f22887..2423b4e918b9 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6048,6 +6048,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, @@ -10096,7 +10109,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) { @@ -10321,7 +10334,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; @@ -12098,6 +12111,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