Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4328208pxj; Tue, 8 Jun 2021 11:36:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFVx+XOPFAmXf67+C+nTNSODHPSRY7CWVEL+r/vy59vS43q7qxVmbSVfq5rhBA8RzBNotg X-Received: by 2002:a17:906:2510:: with SMTP id i16mr24390006ejb.218.1623177418618; Tue, 08 Jun 2021 11:36:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623177418; cv=none; d=google.com; s=arc-20160816; b=jOEvIwxuyv72O2ZdZxJCMGoPLgQgY+ApgY+pAJkysp/TDfyfPr+wi7SsFFWuo7q9BU Gv5XFhd+Lrr/g/Txdst/4xaKJ8ulEOtxgnWYVIEl+fq8RAwHfN0Sm5KLciCMT1HbzQ1e iyatXJKd2Uk+I05m2M9iArikNKYU/DyfjYMuzru/zGLYpOml4ld2K7hKuE0hNiKBRBjr tWrMnbkxGeOJrS2xHEzBj+TrYg6KfJVevwmi4away/E+k6VvaVGgk3UGkP646PAohO2z gn9NI6ivRD85Lj5Rs+e4hbXguRPD4/Hy7LVLUTLDXtWDspUmvNeVB0gypGGf7DscF7ik Ghlg== 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=6NYLvI23ZQkiSRRGrgJIZFGkIfaXnEm/lUJvpaljuWo=; b=Nxzr2OA4/S25/VtrR/cTcNsbk+i/cL1/iGA/DGFzLd2U7D9wwyYA40S0YVwLxPE0Gt 9oQvt05UhhLj1flDqKKQdzr4g8cs5z42oPbnYGGVotOnpn5hO/tANnoKUU68hRePqpl/ Vl/tPFwkcwfIROhhL8hLLLM77AUjCRGRuI+Ak8cmhG1nnbHiTG/go1dWtOng/Ci4wCaN KVyGHEZ+QaM1OWz3HJFjK9MmwdmDFSdvfbLUzeS50QKS+VLSs5hDVIUwvT2HTYwbsOWZ 1RqiERo1Hc+UAEzvEsne/isptBvoCjQ9u/UyQKvm/57p1EZvY955d8JIaWtadCiSwZUz XQ8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cdO7cavK; 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 z26si342157eje.396.2021.06.08.11.36.34; Tue, 08 Jun 2021 11:36:58 -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=cdO7cavK; 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 S232056AbhFHSft (ORCPT + 99 others); Tue, 8 Jun 2021 14:35:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:57158 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234325AbhFHSd5 (ORCPT ); Tue, 8 Jun 2021 14:33:57 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 67468613CC; Tue, 8 Jun 2021 18:31:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623177091; bh=p8yMx1k89PtuQYKI+/EhSCxcOlQ4VnLlIA7C4ZYk+0M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cdO7cavKuY1x4WoW7kAAy9t0Z5vXQhJTIK86wHR5sasOhkYaBdnuaON4VViY7nJ2y wtCZao4WE5xLJ1+m2oPT4Y4wQ1KXlWq6BohhvsepiiTsqSiH9Sz8/e76ZCxen1AHW8 pqUajpkxWSf+KS/1Wwagm8HwgCUs4rqngTFu6SaE= 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 29/47] bpf: Ensure off_reg has no mixed signed bounds for all types Date: Tue, 8 Jun 2021 20:27:12 +0200 Message-Id: <20210608175931.434363550@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 24c109bb1537c12c02aeed2d51a347b4d6a9b76e upstream. The mixed signed bounds check really belongs into retrieve_ptr_limit() instead of outside of it in adjust_ptr_min_max_vals(). The reason is that this check is not tied to PTR_TO_MAP_VALUE only, but to all pointer types that we handle in retrieve_ptr_limit() and given errors from the latter propagate back to adjust_ptr_min_max_vals() and lead to rejection of the program, it's a better place to reside to avoid anything slipping through for future types. The reason why we must reject such off_reg is that we otherwise would not be able to derive a mask, see details in 9d7eceede769 ("bpf: restrict unknown scalars of mixed signed bounds for unprivileged"). 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 | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2025,12 +2025,18 @@ static struct bpf_insn_aux_data *cur_aux } static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, - u32 *ptr_limit, u8 opcode, bool off_is_neg) + const struct bpf_reg_state *off_reg, + u32 *ptr_limit, u8 opcode) { + bool off_is_neg = off_reg->smin_value < 0; bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || (opcode == BPF_SUB && !off_is_neg); u32 off, max; + if (!tnum_is_const(off_reg->var_off) && + (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) + return -EACCES; + switch (ptr_reg->type) { case PTR_TO_STACK: /* Offset 0 is out-of-bounds, but acceptable start for the @@ -2121,7 +2127,7 @@ static int sanitize_ptr_alu(struct bpf_v alu_state |= ptr_is_dst_reg ? BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; - err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg); + err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode); if (err < 0) return err; @@ -2164,8 +2170,8 @@ static int adjust_ptr_min_max_vals(struc smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; - u32 dst = insn->dst_reg, src = insn->src_reg; u8 opcode = BPF_OP(insn->code); + u32 dst = insn->dst_reg; int ret; dst_reg = ®s[dst]; @@ -2205,13 +2211,6 @@ static int adjust_ptr_min_max_vals(struc dst); return -EACCES; } - if (ptr_reg->type == PTR_TO_MAP_VALUE) { - if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) { - verbose("R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n", - off_reg == dst_reg ? dst : src); - return -EACCES; - } - } /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. * The id may be overwritten later if we create a new variable offset.