Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3927425imm; Mon, 8 Oct 2018 11:49:34 -0700 (PDT) X-Google-Smtp-Source: ACcGV60yrF2zzx1LflB7+iMDxZ7y6b3llzpf/nGvKZ0Ix9ixcr06OzvXbuR1S9UtxiJ/m1MGLzld X-Received: by 2002:a63:f744:: with SMTP id f4-v6mr22501784pgk.410.1539024574409; Mon, 08 Oct 2018 11:49:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539024574; cv=none; d=google.com; s=arc-20160816; b=bVtmfhTA7QxsDLK3e596n81vUngUx6y2vUR/7GJYnz8r1NwKB47p7SayXax38dg2UN tQLEGOgFRxdwJsLC+WC8YigN/aW9X4iPbbleBOit93cLWbP+QBwNvV+piuq4R+ediKes rTUGM553/1BzxvIf0mlpIqDMhzmNrBjICKNx49qUwMGgX13LkD1jAUlrHxa4OlIsopJ9 QgEobixqpZfS4FYSx8ZLaEqhgUOGEgiya2GwO8HPY+kvbJXHk8AODNaDkPCqw6h4Kmfu rD2rn/p1MZ2LkokZk5trokvPjqT+dvRfL25eYnN4yyES1NM6mT5ZdqZGgA49cF8cdClB pjew== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Jb5XyQhtMJUKos9KbZeG1lMLMNyfXSLoyiJGWBqOIK4=; b=BpOTrkdv0zWhNUE+ryf/gQsziqRh1Vg5oXR2FaD5LR1oOfuuSPLY6Br9tXyJkQn42f aUqeON3X+p5kndu/QfTAiicWdpKQIRyi1l+W6Y67tk9yyCc1EmeKNWyf3tmluTLzVdC2 bWFo2xxl6anU0HbKxv6Divk6yq1tP1baH6/qlQw4bZT+wkIbOlxqjBizxCoMU7y5nWMD jWdkj2OztOHIR6QUwNE47p3+gvo8jOse6h/EpUCX1JLdIXrAX6M9KkFyf/zNKhOfHZ4M 94xPIXwCKBQd6vzk9G3EcX9rT0B1mzz5lGPe2e/VoF0YUziNaZQABCIa1npkl/zwK/n1 fLmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TFWSGmeZ; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a127-v6si11356933pfb.24.2018.10.08.11.49.19; Mon, 08 Oct 2018 11:49:34 -0700 (PDT) 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=TFWSGmeZ; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731827AbeJICCV (ORCPT + 99 others); Mon, 8 Oct 2018 22:02:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:51574 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726991AbeJICCU (ORCPT ); Mon, 8 Oct 2018 22:02:20 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7B4A821479; Mon, 8 Oct 2018 18:49:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539024554; bh=3xsmlAiZWSj2MHtwpmhvdc8zVXj6pTszw4KMiVq2fPk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TFWSGmeZvO40dFAzXlFMjISmUJdgTfDwWqIPIxpPdHh6+Asye9pw5dExS0U3C0g6y GXNhj18QUaaIzBsArRmPpYovNGr5VLDmnftHZVM6Hb/8WbnBrkYevlQMBAvSl0o91L d6AIiqVrx5q9xKXWllBXqVreI490KYpX6tlh5fQ4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Borkmann , Jann Horn Subject: [PATCH 4.18 078/168] bpf: 32-bit RSH verification must truncate input before the ALU op Date: Mon, 8 Oct 2018 20:30:58 +0200 Message-Id: <20181008175623.031212077@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181008175620.043587728@linuxfoundation.org> References: <20181008175620.043587728@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit b799207e1e1816b09e7a5920fbb2d5fcf6edd681 upstream. When I wrote commit 468f6eafa6c4 ("bpf: fix 32-bit ALU op verification"), I assumed that, in order to emulate 64-bit arithmetic with 32-bit logic, it is sufficient to just truncate the output to 32 bits; and so I just moved the register size coercion that used to be at the start of the function to the end of the function. That assumption is true for almost every op, but not for 32-bit right shifts, because those can propagate information towards the least significant bit. Fix it by always truncating inputs for 32-bit ops to 32 bits. Also get rid of the coerce_reg_to_size() after the ALU op, since that has no effect. Fixes: 468f6eafa6c4 ("bpf: fix 32-bit ALU op verification") Acked-by: Daniel Borkmann Signed-off-by: Jann Horn Signed-off-by: Daniel Borkmann Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/verifier.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2865,6 +2865,15 @@ static int adjust_scalar_min_max_vals(st u64 umin_val, umax_val; u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; + if (insn_bitness == 32) { + /* Relevant for 32-bit RSH: Information can propagate towards + * LSB, so it isn't sufficient to only truncate the output to + * 32 bits. + */ + coerce_reg_to_size(dst_reg, 4); + coerce_reg_to_size(&src_reg, 4); + } + smin_val = src_reg.smin_value; smax_val = src_reg.smax_value; umin_val = src_reg.umin_value; @@ -3100,7 +3109,6 @@ static int adjust_scalar_min_max_vals(st if (BPF_CLASS(insn->code) != BPF_ALU64) { /* 32-bit ALU ops are (32,32)->32 */ coerce_reg_to_size(dst_reg, 4); - coerce_reg_to_size(&src_reg, 4); } __reg_deduce_bounds(dst_reg);