Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp377757pxy; Fri, 30 Apr 2021 07:21:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQPgZJvmnj9CbyvpAee0i2xTEzcf3Rok1S2fFrkVIJnSC5VpuJ/C9yXu8o+ZvAoPMRNM4X X-Received: by 2002:a62:ee09:0:b029:211:1113:2e7c with SMTP id e9-20020a62ee090000b029021111132e7cmr5355383pfi.49.1619792499336; Fri, 30 Apr 2021 07:21:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619792499; cv=none; d=google.com; s=arc-20160816; b=xNXlqGd3IhxAZkreCZs+eKTZA14nbAaGQ+o7iRCWPqo1HHtdeUJ7tHYuUvWWWAhV7e rMA0JMTUAv4ikEE9S5q2/kcMtjyyKK6d7n/grp+L5hF1wyFa1ahIYlLnG8u0BtiAgRSp pKIoxSoDRUN06Z7xRLg71Wxd4zXxhTedI6y+qCuZPqMDPZcOfNEW8HtUt6evZ9HkBKBR WQr9U29tZmC90iF8vLLd6z7dYZIid0OnaEDK8glU636TDPqJ/iFO5uzRHlcyinBpHNcc fFolRmiMXgn6R+4nVBPrw+VKwUKPgvGTMdQuw63MF8qzcIwGCBl2tGwG/fcZCIvq0sYp 4zBA== 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=M9vDJEXRLnzyvGWm58PkpnptpYw4nC95tClqRvxE6cw=; b=ZQACvPOSUC2uqA2gg/+Q/8HkhX9m+8oUysIzy2OFOtg4ksQXjKgudBSM3PnBKPBoN3 XIaTpXkLww6Yv2SbNzSxKfc7U2J5lFbbf9UfUNG85a2bXauiP4ouHjgsJa2vckRBdsHE Sd9lgs1vicIFBLc9bOY0AxWEv+weCaIaqqUZsykUJtkcgiLM9VY5NHvJcQycDRLuuS1c sZFz1z3yRKsoxag1trlW2mDFHB4Q0c0idg6Oe/oSBdyyHIi5+hpktFd7sU5EUVvb+bgd l0QO2xzMYr7a0JKH9TgRcClk0LuNn0BGQb2E2erIMo2gycrntTQdIXBBZTaH3BD1L0uH ZeQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=btnR9zwv; 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 s16si3469049pgg.313.2021.04.30.07.21.25; Fri, 30 Apr 2021 07:21:39 -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=btnR9zwv; 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 S232936AbhD3OVd (ORCPT + 99 others); Fri, 30 Apr 2021 10:21:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:57648 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232905AbhD3OVc (ORCPT ); Fri, 30 Apr 2021 10:21:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 93002613A9; Fri, 30 Apr 2021 14:20:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1619792440; bh=uN46wmyB98/4ZxDkobe5sC072+jaI5/2HNFrPvkCwek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=btnR9zwvYsqF/zXXfhPdq0glZxIiyifbqpt2JXM31BeMS2m/aWFKpsaCwP1rEeKrI cBpVhHEiu+rY9YdM38PSepw0ekFGmJsCC9f0pGAjib8k11oazVCD7xTAM4YMb2AVbP a/UHQCPuaMsfuDD0nHMx+sXfBs6LYInosEaJ7Iac= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , bpf@vger.kernel.org, Daniel Borkmann , John Fastabend , Alexei Starovoitov , Frank van der Linden Subject: [PATCH 5.4 5/8] bpf: Refactor and streamline bounds check into helper Date: Fri, 30 Apr 2021 16:20:19 +0200 Message-Id: <20210430141911.335781665@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210430141911.137473863@linuxfoundation.org> References: <20210430141911.137473863@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 5.4] 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 @@ -4448,6 +4448,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(env, "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, false)) { + verbose(env, "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 @@ -4664,23 +4699,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, false)) { - verbose(env, "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(env, "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; }