Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4733344pxj; Wed, 9 Jun 2021 00:02:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUonKAYQ18aEYQGb8lqDP06ARBsFKMPjAdiyyZYKD14qk/OHyYfFtZDGgDHHSaWvKPAFRE X-Received: by 2002:a17:906:af55:: with SMTP id ly21mr26417182ejb.276.1623222135359; Wed, 09 Jun 2021 00:02:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623222135; cv=none; d=google.com; s=arc-20160816; b=aOv8xRxY5FB9YoNZ0aDoRlKIn7oJwLpDJa9B57nAUurUC3b6y4Q7cxCQ5IIsTJ1dOx 15w2Y+IMjvp8eEFBaV4art027bi8W6YKlH3mOV8gG9UP5mjhJtocavP3VGgZ1tDUPWr5 v56y+bxm5kpuxNElL7zgBL+Ra/g27AnF7pkzos6aZtojE2tva/NI84AGyM1s1gBdkjIS ZSgk32T0C4GodrNgqFeve/d1YknkuvzTWlxyfXKzhzSj6/u1tK0R0t4/aemjd0OPgoQI bVoKTpjxGk9ooKNg0IOYpxQKodKrjOJibksurRcNaG5DnC5+yX6j5dyov/x1Fr+00FM9 3Dyw== 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=ca6rINeU6SbzZLH5H+GevhBP2UsOAcanFsFl04bVe2o=; b=BMefeWC6gnUi5zC+28TpMEoC2yiVNNgoUhZgNThfTgX3uWLayHr/lDCnrRWsmEfxL3 8sevfMpqLkZdYJOwV/ieX4BywY3B6VeUlowRWTMecQ82uNTlvQk3OBnodbPgY8D7RQAY VOwnknUCFRtKoflyyzw0E7uXX6Ynnp9v+SIPV0caqNhpZmF/UIPqArr+cm3LK9X8A8z+ FKD6IQQZT3MuIggRYtqQT+mPRfabmYh4X2rKbngS12Xc3202jNRJoxftSLiNORXxxHRj m6IW8zqV0dx1mvat8najNIqgKTM29zYWPBT+hjsa8aH6Nt0f550jVJVqTv73daFfbFGw 4g5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JCXeGfqM; 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 u8si1717177eju.465.2021.06.09.00.01.51; Wed, 09 Jun 2021 00:02:15 -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=JCXeGfqM; 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 S234906AbhFHSgD (ORCPT + 99 others); Tue, 8 Jun 2021 14:36:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:57434 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234702AbhFHSd7 (ORCPT ); Tue, 8 Jun 2021 14:33:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3295E61073; Tue, 8 Jun 2021 18:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177102; bh=+/X7+aU5pDIrJ6eKlKtAahFvbdbU0mYDHX8vRv/MVxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JCXeGfqMH/0u831tmvckiHC6vsiXOuro+eb/LUKwyxCm1x5Oo5Xz8IB3lbOquwjq4 bXQBcfy7Iz15a6IMODNwjCnH3Unumow7CENE9PWNaJO6S9czTXyeG3KBmKsh2vkQGz cjvB5zZ70erWkR26UYfPIoHaT0ioZfhcr6FF+Eww= 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 33/47] bpf: Move sanitize_val_alu out of op switch Date: Tue, 8 Jun 2021 20:27:16 +0200 Message-Id: <20210608175931.561848968@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 f528819334881fd622fdadeddb3f7edaed8b7c9b upstream. Add a small sanitize_needed() helper function and move sanitize_val_alu() out of the main opcode switch. In upcoming work, we'll move sanitize_ptr_alu() as well out of its opcode switch so this helps to streamline both. Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov [fllinden@amazon.com: backported to 4.14] Signed-off-by: Frank van der Linden Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2110,6 +2110,11 @@ static int sanitize_val_alu(struct bpf_v return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); } +static bool sanitize_needed(u8 opcode) +{ + return opcode == BPF_ADD || opcode == BPF_SUB; +} + static int sanitize_ptr_alu(struct bpf_verifier_env *env, struct bpf_insn *insn, const struct bpf_reg_state *ptr_reg, @@ -2510,11 +2515,14 @@ static int adjust_scalar_min_max_vals(st return 0; } - switch (opcode) { - case BPF_ADD: + if (sanitize_needed(opcode)) { ret = sanitize_val_alu(env, insn); if (ret < 0) return sanitize_err(env, insn, ret, NULL, NULL); + } + + switch (opcode) { + case BPF_ADD: if (signed_add_overflows(dst_reg->smin_value, smin_val) || signed_add_overflows(dst_reg->smax_value, smax_val)) { dst_reg->smin_value = S64_MIN; @@ -2534,9 +2542,6 @@ static int adjust_scalar_min_max_vals(st dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); break; case BPF_SUB: - ret = sanitize_val_alu(env, insn); - if (ret < 0) - return sanitize_err(env, insn, ret, NULL, NULL); if (signed_sub_overflows(dst_reg->smin_value, smax_val) || signed_sub_overflows(dst_reg->smax_value, smin_val)) { /* Overflow possible, we know nothing */