Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3638019imu; Mon, 28 Jan 2019 08:14:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN4nm/FAYzeKcjF3vUDI5M5uHGQ9FAanH2iso/Aassnjn4inOs30zLf1r0QXTlIlAVWkWdEZ X-Received: by 2002:a62:ae04:: with SMTP id q4mr22451410pff.126.1548692046723; Mon, 28 Jan 2019 08:14:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548692046; cv=none; d=google.com; s=arc-20160816; b=MS2dGUuDC+1uXy9gk1uIcvwkxZ94lqSqt87FGL9QlRuDzlqZ5NOt5Cko9mx8t9+gkm HI1AaQfCMvaKQSlMZemXPZfKmJpLmgsAd3JNob7Kkb9zcw+aP0KP4XXg3VFo/OnAfTEx 0kyU7XGlB8yeaU8s6BSA8y+RmyZC5wrikfzb1A/yoFmBKQUjLRKQzfr7LfrqoJsF/AgP TxpsOP0KfwwYHL3hheWngGhGZ/tXx5Ix9rwPUJ+uoCdB7mvnS+b1kYGNg+GdPCs0w/Up kfgvP/Vyw0KHo9iFUltY4+dFwmLRvuVDW8ydqtXJmorxppzW2KUILHlGuvEFTAxS1YwA 69JQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PF9oL9nKILGNsgZhQpFQjhmCJuJMYqZNUz3AcyRPryc=; b=BSrgk5WsCqkniNGyMa26afVZAGc1Bx3yFaGA/9krWxLvNutHvwV7XWClz7UC8mEh7c g2BIj7+kcnavc/HuUXGdidfCO0+0UKyMb3EJkr/0yGzeta5C8yjT0vneBVnlrC7ptEkw FCUMP9VzRlEBwgmH2L+yzMhpO7HcSBJFBeo+BfTAf5DaV6vwjxnokTMX6KISiqb7907W 3A/FWDnnj0Y7L+uCjRj+rMCPrCrnVmIXAXOBvcMzlrbcXOeOoe+0rchni/g3AvwlKKEv YbLwI3N6SbNmRBBJ62isgCCVl/4nftDk7vcE+2hyXNEmuuz7bojSue7izqzfRKaC65ye ok2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=V4Dna+OG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 85si32194475pfc.145.2019.01.28.08.13.47; Mon, 28 Jan 2019 08:14:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=V4Dna+OG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732969AbfA1QLZ (ORCPT + 99 others); Mon, 28 Jan 2019 11:11:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:38376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732946AbfA1QLY (ORCPT ); Mon, 28 Jan 2019 11:11:24 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E855E2177E; Mon, 28 Jan 2019 16:11:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691883; bh=Qy05m6YU7sijz9HqGN6a7TV+EUkf3eqhCrncOI/zwV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V4Dna+OGZgCxeq1PQqr4dzFDvP1ydCjG+QQjDgetqA6kvu8zE3DiWisadq+FtVrxs ibLb7btfArphUomkgHomqvUIkdoM8aG0jEOTJic9qjdV8tthHbl72/RdYOUw1j3Jfo /Myye70x49gu9+YUETg/+wQzpsyBXF3AFTLWM1KM= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Daniel Borkmann , Alexei Starovoitov , Sasha Levin , netdev@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 248/258] bpf: fix check_map_access smin_value test when pointer contains offset Date: Mon, 28 Jan 2019 10:59:14 -0500 Message-Id: <20190128155924.51521-248-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Borkmann [ Upstream commit b7137c4eab85c1cf3d46acdde90ce1163b28c873 ] In check_map_access() we probe actual bounds through __check_map_access() with offset of reg->smin_value + off for lower bound and offset of reg->umax_value + off for the upper bound. However, even though the reg->smin_value could have a negative value, the final result of the sum with off could be positive when pointer arithmetic with known and unknown scalars is combined. In this case we reject the program with an error such as "R min value is negative, either use unsigned index or do a if (index >=0) check." even though the access itself would be fine. Therefore extend the check to probe whether the actual resulting reg->smin_value + off is less than zero. Signed-off-by: Daniel Borkmann Acked-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 341806668f03..9997723038d3 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1268,13 +1268,17 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, */ if (env->log.level) print_verifier_state(env, state); + /* The minimum value is only important with signed * comparisons where we can't assume the floor of a * value is 0. If we are using signed variables for our * index'es we need to make sure that whatever we use * will have a set floor within our range. */ - if (reg->smin_value < 0) { + if (reg->smin_value < 0 && + (reg->smin_value == S64_MIN || + (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || + reg->smin_value + off < 0)) { verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", regno); return -EACCES; -- 2.19.1