Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1033845ybe; Wed, 4 Sep 2019 11:28:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqwBGx7wDNtU+sOIDEcDU4j0TvWleDXx10yXV/Ff0vvUFNWV9toUUb+lreQG1KLDGqyYHwNN X-Received: by 2002:a62:e50c:: with SMTP id n12mr48119277pff.206.1567621712584; Wed, 04 Sep 2019 11:28:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567621712; cv=none; d=google.com; s=arc-20160816; b=IqsgLU5qmEIe153xHeTiDuqQlvbay55wA3npk9JDRrnJ5Ks0kJQflOU6gs1ln9pn1V FURv5F+Y/+vJJXrbIHCtxgtsoG4JD6bWPGFdzW+p89CVQkyc9Q9IYcaQ81Wg4NDs6AcO oWDkRr82E7QJTsYqWmk78lkdiYl2Yv5Lg8/5EL/HS2+td144X7JtV1W0Ht1vlRkSfsmQ xC+ilxTGdUO7YqlARjFdz5wjGZx0ZS5grSTyQKZOXU5UMKnULOadk8ueEoA/3kPD/vIf qVMTDQ7q44K6J9Z505CnQ4zlc6nuA+K8S/zyczWM0KMQt3mztoU/SpYLChwUse45mDYQ SmGw== 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=i+wQzDujU2Cbct1Z3xShE5lY3AP5FSYrHOvcnA6RN/U=; b=mZyBeyoF8ZSRHeweJ9TtuY3BYvoMv6m6lo6CMaVVK4ZgTsqtgTfO8gtZYzTnt7Kodg 6Dsm3xaOdLWYIzFwx0g+nzmZLeTlzoVoVEcqaB9B4C7gDqPyeu2fchoz5anuuCwtgi6J RBm1B/mR0wLCfzTWms4ExMamb676azFyDUF6RzNklfZQAiW7iaF4rIZSzLGst8mIpgkP Nh1I+uk1jHmOz2A/8Cgzp+dEzLerKMcjEqIoI9DaXfvGq7cR/fgYFlr7/kRHBOnjjWug glt6JGG1YlUN4lMC7ySBwgvlzWg5q0ejB9w7yyIDdcEU4TckIqK8Q1/TmjNOw464hpfj yDuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kZoWJPR0; 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 1si2987271pjf.45.2019.09.04.11.28.17; Wed, 04 Sep 2019 11:28:32 -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=kZoWJPR0; 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 S1732673AbfIDSAK (ORCPT + 99 others); Wed, 4 Sep 2019 14:00:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:39280 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388352AbfIDSAI (ORCPT ); Wed, 4 Sep 2019 14:00:08 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 CF16A22CEA; Wed, 4 Sep 2019 18:00:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620007; bh=Ui+0UrEl6Wdi7CRb8+V3zC+O+5oPfmhX1x6ySkn9Rs0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kZoWJPR0Izd8t6T219ZXxesPcP3VJLij6puVezaVZbiwCkrJD5fU8bRD0Hc3km9fH DWAGvPI0N38/gstKU9n85hDBfSikdPOA5fls766iZmeXmP8r4uYoBQ1qtseXFWDtEY v0cj0bprfE06vwbQ+VXKevzj/CWCJlCxPMwXS04I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhang Tao , Mikulas Patocka , Mike Snitzer Subject: [PATCH 4.9 39/83] dm table: fix invalid memory accesses with too high sector number Date: Wed, 4 Sep 2019 19:53:31 +0200 Message-Id: <20190904175307.275806394@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175303.488266791@linuxfoundation.org> References: <20190904175303.488266791@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Mikulas Patocka commit 1cfd5d3399e87167b7f9157ef99daa0e959f395d upstream. If the sector number is too high, dm_table_find_target() should return a pointer to a zeroed dm_target structure (the caller should test it with dm_target_is_valid). However, for some table sizes, the code in dm_table_find_target() that performs btree lookup will access out of bound memory structures. Fix this bug by testing the sector number at the beginning of dm_table_find_target(). Also, add an "inline" keyword to the function dm_table_get_size() because this is a hot path. Fixes: 512875bd9661 ("dm: table detect io beyond device") Cc: stable@vger.kernel.org Reported-by: Zhang Tao Signed-off-by: Mikulas Patocka Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/dm-table.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/drivers/md/dm-table.c +++ b/drivers/md/dm-table.c @@ -1263,7 +1263,7 @@ void dm_table_event(struct dm_table *t) } EXPORT_SYMBOL(dm_table_event); -sector_t dm_table_get_size(struct dm_table *t) +inline sector_t dm_table_get_size(struct dm_table *t) { return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0; } @@ -1288,6 +1288,9 @@ struct dm_target *dm_table_find_target(s unsigned int l, n = 0, k = 0; sector_t *node; + if (unlikely(sector >= dm_table_get_size(t))) + return &t->targets[t->num_targets]; + for (l = 0; l < t->depth; l++) { n = get_child(n, k); node = get_node(t, l, n);