Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2471567pxb; Mon, 19 Apr 2021 06:34:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOOtqhrklR7lRvDodvgWtaf1A5ZL/N900CKx5vKZXh632ewqATxQ9QfYOCw4tb2evF1a2J X-Received: by 2002:a17:906:2cd1:: with SMTP id r17mr21799194ejr.429.1618839260869; Mon, 19 Apr 2021 06:34:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618839260; cv=none; d=google.com; s=arc-20160816; b=GK1tchc9hUpNHcCX8KA0eezhadRiFj1nVCrtHTv+pFGidhIH00/QzrXTlm9rbqUEgL HgN/U2llC70U+jaVDb9Pa+zvsKt1LmnpfUeDn3qym5KsdJLvbb88UtUFFcAvzWdvAFg6 zH57LuvlBQmkxEK7m0nT+T8Y7Ubs2UO0Ak8t3VH+sfhYcyc4gAOWWwWMmtx20D9ZLwD2 qLqZ4R7A1G2Q2tk+AbpwrsL4qIV2BghpSivpgVF+uzar/cqwK9q5NU3bl+mO26j3oL2r gGDyugyZtAwl5toaaY/TM5W9Zkf1wJ0WHecEWA71xhZKyJR5I/OFB0WywMD1h4LrLphz NRrA== 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=2c4ewMjt2MJ0fZtwlZ2NbswPSdAYx7YgpESNV075jpQ=; b=zFJKG0ODEuWJhQCW8NK9za61REnFa1fxupIrjw6/8JDUuzn29fVa1P03JpKPpQd/9T OAQvB+HJ2eL34+Qtx2TTRDV4dSsMO4WcuErzHIFmY7X5+lxRTPXdS0AOnoepE54vs6rw kNROsFMBgrCqsybTcC+4oIzpUTPcdgkOfgnmDdZp9PQ99xfczrzzbBdXlIYC+M0AxLP2 OuiFdt9MnolK4B08tPGNAwbqt5mqtGGxr7/k4KtNDJkkHGs8sWJt3GLm+qS3BZz7dYM5 H0zXchqUrUNlfRdEiSTx8ZLgD+b0bTeMqzy2MkDuFvfv2dDJplUhHRp2B6DHhaCh3Qul LMkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=t8de60Ey; 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 gu24si11929276ejb.678.2021.04.19.06.33.56; Mon, 19 Apr 2021 06:34:20 -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=t8de60Ey; 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 S243008AbhDSNa2 (ORCPT + 99 others); Mon, 19 Apr 2021 09:30:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:54806 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240433AbhDSNVJ (ORCPT ); Mon, 19 Apr 2021 09:21:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 808BD61405; Mon, 19 Apr 2021 13:17:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1618838267; bh=R7XCqP7XTZJvBsJrHLeb8KVdTgFXBKZweGM7evBzVRk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t8de60EyuRnLZ6OdgG32gUkwvh1RSt+3eRa2gh7joVznp4de1WxX+/dCZll/epE2s VRhVjeJLZXLfWI8guTLGUCp1kco3xzZyxLbimoIrfnNlbHrrTRC2vYzSpvFLK21yXW oNMyzjf83vGRLjbiQn3fb+r89kV9UpCVYUOBYn3k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , John Fastabend , Alexei Starovoitov Subject: [PATCH 5.10 102/103] bpf: Improve verifier error messages for users Date: Mon, 19 Apr 2021 15:06:53 +0200 Message-Id: <20210419130531.287463361@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210419130527.791982064@linuxfoundation.org> References: <20210419130527.791982064@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 a6aaece00a57fa6f22575364b3903dfbccf5345d upstream. Consolidate all error handling and provide more user-friendly error messages from sanitize_ptr_alu() and sanitize_val_alu(). Signed-off-by: Daniel Borkmann Reviewed-by: John Fastabend Acked-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 86 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 23 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5328,6 +5328,14 @@ static struct bpf_insn_aux_data *cur_aux return &env->insn_aux_data[env->insn_idx]; } +enum { + REASON_BOUNDS = -1, + REASON_TYPE = -2, + REASON_PATHS = -3, + REASON_LIMIT = -4, + REASON_STACK = -5, +}; + static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, const struct bpf_reg_state *off_reg, u32 *alu_limit, u8 opcode) @@ -5339,7 +5347,7 @@ static int retrieve_ptr_limit(const stru if (!tnum_is_const(off_reg->var_off) && (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) - return -EACCES; + return REASON_BOUNDS; switch (ptr_reg->type) { case PTR_TO_STACK: @@ -5366,11 +5374,11 @@ static int retrieve_ptr_limit(const stru } break; default: - return -EINVAL; + return REASON_TYPE; } if (ptr_limit >= max) - return -ERANGE; + return REASON_LIMIT; *alu_limit = ptr_limit; return 0; } @@ -5390,7 +5398,7 @@ static int update_alu_sanitation_state(s if (aux->alu_state && (aux->alu_state != alu_state || aux->alu_limit != alu_limit)) - return -EACCES; + return REASON_PATHS; /* Corresponding fixup done in fixup_bpf_calls(). */ aux->alu_state = alu_state; @@ -5463,7 +5471,46 @@ do_sim: ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true); if (!ptr_is_dst_reg && ret) *dst_reg = tmp; - return !ret ? -EFAULT : 0; + return !ret ? REASON_STACK : 0; +} + +static int sanitize_err(struct bpf_verifier_env *env, + const struct bpf_insn *insn, int reason, + const struct bpf_reg_state *off_reg, + const struct bpf_reg_state *dst_reg) +{ + static const char *err = "pointer arithmetic with it prohibited for !root"; + const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; + u32 dst = insn->dst_reg, src = insn->src_reg; + + switch (reason) { + case REASON_BOUNDS: + verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", + off_reg == dst_reg ? dst : src, err); + break; + case REASON_TYPE: + verbose(env, "R%d has pointer with unsupported alu operation, %s\n", + off_reg == dst_reg ? src : dst, err); + break; + case REASON_PATHS: + verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", + dst, op, err); + break; + case REASON_LIMIT: + verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", + dst, op, err); + break; + case REASON_STACK: + verbose(env, "R%d could not be pushed for speculative verification, %s\n", + dst, err); + break; + default: + verbose(env, "verifier internal error: unknown reason (%d)\n", + reason); + break; + } + + return -EACCES; } /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. @@ -5553,10 +5600,9 @@ static int adjust_ptr_min_max_vals(struc switch (opcode) { case BPF_ADD: ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg); - if (ret < 0) { - verbose(env, "R%d tried to add from different maps, paths, or prohibited types\n", dst); - return ret; - } + if (ret < 0) + return sanitize_err(env, insn, ret, off_reg, dst_reg); + /* We can take a fixed offset as long as it doesn't overflow * the s32 'off' field */ @@ -5608,10 +5654,9 @@ static int adjust_ptr_min_max_vals(struc break; case BPF_SUB: ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg); - if (ret < 0) { - verbose(env, "R%d tried to sub from different maps, paths, or prohibited types\n", dst); - return ret; - } + if (ret < 0) + return sanitize_err(env, insn, ret, off_reg, dst_reg); + if (dst_reg == off_reg) { /* scalar -= pointer. Creates an unknown scalar */ verbose(env, "R%d tried to subtract pointer from scalar\n", @@ -6301,9 +6346,8 @@ static int adjust_scalar_min_max_vals(st s32 s32_min_val, s32_max_val; u32 u32_min_val, u32_max_val; u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; - u32 dst = insn->dst_reg; - int ret; bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); + int ret; smin_val = src_reg.smin_value; smax_val = src_reg.smax_value; @@ -6362,20 +6406,16 @@ static int adjust_scalar_min_max_vals(st switch (opcode) { case BPF_ADD: ret = sanitize_val_alu(env, insn); - if (ret < 0) { - verbose(env, "R%d tried to add from different pointers or scalars\n", dst); - return ret; - } + if (ret < 0) + return sanitize_err(env, insn, ret, NULL, NULL); scalar32_min_max_add(dst_reg, &src_reg); scalar_min_max_add(dst_reg, &src_reg); 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) { - verbose(env, "R%d tried to sub from different pointers or scalars\n", dst); - return ret; - } + if (ret < 0) + return sanitize_err(env, insn, ret, NULL, NULL); scalar32_min_max_sub(dst_reg, &src_reg); scalar_min_max_sub(dst_reg, &src_reg); dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);