Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1349310pxf; Fri, 19 Mar 2021 05:25:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrUoA80e0ZVSUgA40HKUd/paJEWxXX3vTavCtfa+6JGixPYzpkQ5sBPO8eoMsW4eG7w4Fq X-Received: by 2002:a05:6402:35d3:: with SMTP id z19mr9138266edc.143.1616156749625; Fri, 19 Mar 2021 05:25:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616156749; cv=none; d=google.com; s=arc-20160816; b=Nwyeh+yk8hQbPnZIYkFQX6y9TItnLCGBSpOyW1gwujhDn1a3VEYP1xJ8rUURBUEW/h aJ0BTdUvfkrEWf/DYi2f8CVb6AZbEAegGZWsQzfM1YoytxT0cG4EyWd8guoqC3aCCBNH Cd707vFXK0CaRjQTqyZIiL8kpQIlNL1nJVBXsehyxhycIH1KUos+J8y/h0ud8b8xxqoo STO3s3/2hM1lp5NjNwgmDw87oxRN+PQyXW4zmscma3BBztt8YDS5t4jeodZKBqsGhha5 9e2p0Z0HEcMkhZXnrCoU/pHSIbEnU+Y7ALOnsCBMSVXW0b1+U2j/3HH2kB7jphD4Ox5u 93EA== 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=MpCsmuX79lydS+DC4PKafIeaD4QuG8jeF9WCZpmtOIY=; b=d/ZMX0QLhCmVAxU2kBRyZESnthIRN1c6gmn8RtM87lv5fItRehZhcF3ydpBYtaBW0p GuK8O4Jpd6sBQwfYqSYTOUMQqnclb6a5dDjuJezHVU2Re4F46ewWbBGq0UGptoGOKzus odjgcAqytXA8fva3+fHx2wq1/yDccWWK5RtuOJnKcXACy9VnYftT7sgw2wUvATolYqjC H5BOcDVH+E1ZgbRW4TbSo4T3OhJVN4VUekmRbaeb1RDrz21z5LBzs8T/CkIkmK3nZWJ4 PipBvZrNTMfvjAwg8N9ZdW8dyDZ6zMarV9FgjqnzzdXnXgEgIp6vmczGDIbMSq9ef759 RZ8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rFtiP1ny; 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 e26si4213136ejy.207.2021.03.19.05.25.26; Fri, 19 Mar 2021 05:25:49 -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=rFtiP1ny; 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 S231732AbhCSMWQ (ORCPT + 99 others); Fri, 19 Mar 2021 08:22:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:60522 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231565AbhCSMVj (ORCPT ); Fri, 19 Mar 2021 08:21:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DAA3264F79; Fri, 19 Mar 2021 12:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1616156499; bh=lQL4bXfs5zYy309K09OEDSizUcvLEgFnmy3g62IN/3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rFtiP1nyWjVzbW1oFCNjH1+4rlUMACl1QbAvbR9TVoVFuCu5551R/Ccln5N45z0MY jFNrsUEADChcrbjC6BifR+42NNVcXREp/dZG8q84EBJLn5tso8kZsAA7xaVg6XEK/W bz+PpsANtOY9fTENLKYfpu0H3kLD25MpfEZ1WuEc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Piotr Krysiuk , Daniel Borkmann , Alexei Starovoitov Subject: [PATCH 5.11 23/31] bpf: Add sanity check for upper ptr_limit Date: Fri, 19 Mar 2021 13:19:17 +0100 Message-Id: <20210319121747.948248574@linuxfoundation.org> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210319121747.203523570@linuxfoundation.org> References: <20210319121747.203523570@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: Piotr Krysiuk commit 1b1597e64e1a610c7a96710fc4717158e98a08b3 upstream. Given we know the max possible value of ptr_limit at the time of retrieving the latter, add basic assertions, so that the verifier can bail out if anything looks odd and reject the program. Nothing triggered this so far, but it also does not hurt to have these. Signed-off-by: Piotr Krysiuk Co-developed-by: Daniel Borkmann Signed-off-by: Daniel Borkmann Acked-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5389,10 +5389,14 @@ static int retrieve_ptr_limit(const stru { bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || (opcode == BPF_SUB && !off_is_neg); - u32 off; + u32 off, max; switch (ptr_reg->type) { case PTR_TO_STACK: + /* Offset 0 is out-of-bounds, but acceptable start for the + * left direction, see BPF_REG_FP. + */ + max = MAX_BPF_STACK + mask_to_left; /* Indirect variable offset stack access is prohibited in * unprivileged mode so it's not handled here. */ @@ -5401,15 +5405,16 @@ static int retrieve_ptr_limit(const stru *ptr_limit = MAX_BPF_STACK + off; else *ptr_limit = -off - 1; - return 0; + return *ptr_limit >= max ? -ERANGE : 0; case PTR_TO_MAP_VALUE: + max = ptr_reg->map_ptr->value_size; if (mask_to_left) { *ptr_limit = ptr_reg->umax_value + ptr_reg->off; } else { off = ptr_reg->smin_value + ptr_reg->off; *ptr_limit = ptr_reg->map_ptr->value_size - off - 1; } - return 0; + return *ptr_limit >= max ? -ERANGE : 0; default: return -EINVAL; }