Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4819930pxj; Wed, 9 Jun 2021 02:38:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHVYdutpkdJRDsDcCTjbIwxsLjJ3fz1wKfJMqwg9YHfvDXG6nfB33eVOx5D3DVWr9gNzB8 X-Received: by 2002:a05:6602:2283:: with SMTP id d3mr22593395iod.121.1623231524767; Wed, 09 Jun 2021 02:38:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623231524; cv=none; d=google.com; s=arc-20160816; b=pEVFjYwE6uABHmEbrEvcMAfBSB2sX57/jZ9xIwNSi44fKdo0z0uPQprCaY638sc9mw qEnW3xt+rv1dpQTP32tmg/WyR+/QLuDqOQXAHTE3VCqKoXQjF49Dl/GwUCmOnIkmbDyR JwTf+krggr60rjeQzswqvlsARFetQPs4LlrwD4wRw4VzRWGoo5v0AwKnP4UGpB7yu8JD EHrY/htlnlTMqw/x1JM7YVaS6l257qQPUJAyTYBevzBE+tRF54rfEjc1VVJ7E0mw24E5 yC+O9UP6EdLkSVLNJW1RuJHP1sqPMp1ahWEeLHZ9l0565a+0gNT3qd7jklFUEXak6828 5RDg== 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=EHpdibaykwcOj8P8WquHwTPcmYxDnoCDrMY+RrcxBdk=; b=k0AnigD8ftx82mkHHbPq/qsgmFEFx8Q0oOwibzK5OH3kCs0bpPZv4lXM+u34kZ4L9J pjF5ma+d7AroEvBuzw79O836qnOs1E1yVxmBKFL64sIkwJ5dhgd5otnMm3/aPpc5XN4o Tfwjw+0bLR72rl+GRL0/wIM75UNTemK+oLW/bsJqPZjRhO6Vs4NQxbv0nTVf9reuGBqL hUIzalXAP4uZCEEubamMbtU/+2p96G2I8uGc70/w3tnoy14oC1N6MOd8B70DWZ4H1DCQ hOALhn/lFd7gRiItb2cSayt8W0bzxG2UmVwnSOy/yEaIrVobz2v3KLmh03XpGe6tSa5e XQUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1YRcpeph; 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 d12si2619715jam.9.2021.06.09.02.38.31; Wed, 09 Jun 2021 02:38:44 -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=1YRcpeph; 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 S234715AbhFHSfw (ORCPT + 99 others); Tue, 8 Jun 2021 14:35:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:56100 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234327AbhFHSd7 (ORCPT ); Tue, 8 Jun 2021 14:33:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CC72A61027; Tue, 8 Jun 2021 18:31:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177100; bh=swranWKbJfXXxA87ZrGrbZM4uxnWLlUQHpSNll+jfu4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1YRcpephlGCXBDB8HVNCnp3AROZr6I1BcwUIOJbSyq74YJkSUBpYKOTT8kcGXi52j hqktpSag3gdhfmy1tuMMgkPPQsEeYrcWbKRScgwrMntTvQeSodfgEiXC/zAmpQDEsk WeWjMbag48RiIXsuSGEoL1kdTN2ScvE/qF1YVzyg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Daniel Borkmann , John Fastabend , Alexei Starovoitov , Frank van der Linden Subject: [PATCH 4.14 32/47] bpf: Refactor and streamline bounds check into helper Date: Tue, 8 Jun 2021 20:27:15 +0200 Message-Id: <20210608175931.530429217@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175930.477274100@linuxfoundation.org> References: <20210608175930.477274100@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 commit 073815b756c51ba9d8384d924c5d1c03ca3d1ae4 upstream. Move the bounds check in adjust_ptr_min_max_vals() into a small helper named sanitize_check_bounds() in order to simplify the former a bit. Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov [fllinden@amazon.com: backport to 4.14] Signed-off-by: Frank van der Linden Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 54 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 17 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2206,6 +2206,41 @@ static int sanitize_err(struct bpf_verif return -EACCES; } +static int sanitize_check_bounds(struct bpf_verifier_env *env, + const struct bpf_insn *insn, + const struct bpf_reg_state *dst_reg) +{ + u32 dst = insn->dst_reg; + + /* For unprivileged we require that resulting offset must be in bounds + * in order to be able to sanitize access later on. + */ + if (env->allow_ptr_leaks) + return 0; + + switch (dst_reg->type) { + case PTR_TO_STACK: + if (check_stack_access(env, dst_reg, dst_reg->off + + dst_reg->var_off.value, 1)) { + verbose("R%d stack pointer arithmetic goes out of range, " + "prohibited for !root\n", dst); + return -EACCES; + } + break; + case PTR_TO_MAP_VALUE: + if (check_map_access(env, dst, dst_reg->off, 1)) { + verbose("R%d pointer arithmetic of map value goes out of range, " + "prohibited for !root\n", dst); + return -EACCES; + } + break; + default: + break; + } + + return 0; +} + /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. * Caller should also handle BPF_MOV case separately. * If we return -EACCES, caller may want to try again treating pointer as a @@ -2421,23 +2456,8 @@ static int adjust_ptr_min_max_vals(struc __reg_deduce_bounds(dst_reg); __reg_bound_offset(dst_reg); - /* For unprivileged we require that resulting offset must be in bounds - * in order to be able to sanitize access later on. - */ - if (!env->allow_ptr_leaks) { - if (dst_reg->type == PTR_TO_MAP_VALUE && - check_map_access(env, dst, dst_reg->off, 1)) { - verbose("R%d pointer arithmetic of map value goes out of range, " - "prohibited for !root\n", dst); - return -EACCES; - } else if (dst_reg->type == PTR_TO_STACK && - check_stack_access(env, dst_reg, dst_reg->off + - dst_reg->var_off.value, 1)) { - verbose("R%d stack pointer arithmetic goes out of range, " - "prohibited for !root\n", dst); - return -EACCES; - } - } + if (sanitize_check_bounds(env, insn, dst_reg) < 0) + return -EACCES; return 0; }