Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5654509img; Wed, 27 Mar 2019 12:32:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqxdc4cWbIgbrFmkWHay0tTHNDkiM25DR8djxeG0YQI/kBrCzmi3Ud8hQlmYiPjO7ypWOi2A X-Received: by 2002:a17:902:e382:: with SMTP id ch2mr14330070plb.94.1553715121070; Wed, 27 Mar 2019 12:32:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553715121; cv=none; d=google.com; s=arc-20160816; b=jrzFirjWbrHAPXtv369kOf7Teuv0loLJ2LEnJkyB4DzFJVykeLomUyw/n859r01fDZ fjo8UEbc1SmZ+X3sI7bGO8HmAvLUY2ZH7Hox9K3q3Fz6EByJdPiPjsJC1p64Nq1ZHFLz Dy1f/cZK6ddy0uZsWYJ/mWQKhtKFrMZw75o8aaJg9bS6wUWCthB8aerwA63ijLlG4Qz8 8c9KXIaTwJLdNxEG+tVJ/oBPmurFWyMdkSKfabIlSOAs6psHlDHwKW3aQpva48XQ4Bam /NwAqCke6S4F5rfJzQcTFGp1VCIX339n2KMcH91/7pdkpSksQXvNHYzVr1CPTBM70Ne1 kCTg== 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=vZO6Rk7X0sBw/+jLRUsQdVmmiUN9nQAJN35Dh4YBFJA=; b=dsSH5UOubyZv8vskGfloxcMw1V25QWGjWAcfBpbL2kciW4SEbb+JXwXLFcZxo/XeO2 tyiyyvGeUcC+FM7MOzAVWqYsG67Mni9Q9hsiQIKes/oLsaSVyTy3DEQ1c1eN0QRJZgaW Ktd1Dgm63jixVLtTmzHP4T6q6WBHYDi/UOJLgqNOp0wwE85mY0OLktrqF9tzZxu0gRV5 8IUWE+t5si5c0St41PxXdIlEU+e53kFBnP8RxJkX/pj+F9rojnAmDxn/MQmaPknVABMA OFifabHMDnJGmt48i3iwoibiVcPNE4HZkaSYPN+eqmpTCCP1eGQobeTHytNZId5puQaS raFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1aXMYMyZ; 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 v23si18445577pfm.187.2019.03.27.12.31.45; Wed, 27 Mar 2019 12:32:01 -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=1aXMYMyZ; 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 S1732499AbfC0SD3 (ORCPT + 99 others); Wed, 27 Mar 2019 14:03:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:43662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732417AbfC0SD0 (ORCPT ); Wed, 27 Mar 2019 14:03:26 -0400 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 AF75E2147C; Wed, 27 Mar 2019 18:03:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553709805; bh=UEz3lvbYUxpdvrxqd8zeSyKgEUiMr+ER7xpjWR3iuT0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1aXMYMyZYRTZlrfcL03HmuTvVHFFRNf/JZk6wqJmt6fMhsia0iSjuNoaALwp8TiGy UWREhXdviPFBbFQfNDIk9BBy9Y4BNP6EiFl40DU0OtQkakx5pDZx90kY4C1aKOFmLQ 5cvTlr1C/rbaPYTr/0pnbQ9FfrSOu2jWhFkrmk8w= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: "Jason Cai (Xiang Feng)" , Jason Cai , Mike Snitzer , Sasha Levin Subject: [PATCH AUTOSEL 5.0 047/262] dm thin: add sanity checks to thin-pool and external snapshot creation Date: Wed, 27 Mar 2019 13:58:22 -0400 Message-Id: <20190327180158.10245-47-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327180158.10245-1-sashal@kernel.org> References: <20190327180158.10245-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: "Jason Cai (Xiang Feng)" [ Upstream commit 70de2cbda8a5d788284469e755f8b097d339c240 ] Invoking dm_get_device() twice on the same device path with different modes is dangerous. Because in that case, upgrade_mode() will alloc a new 'dm_dev' and free the old one, which may be referenced by a previous caller. Dereferencing the dangling pointer will trigger kernel NULL pointer dereference. The following two cases can reproduce this issue. Actually, they are invalid setups that must be disallowed, e.g.: 1. Creating a thin-pool with read_only mode, and the same device as both metadata and data. dmsetup create thinp --table \ "0 41943040 thin-pool /dev/vdb /dev/vdb 128 0 1 read_only" BUG: unable to handle kernel NULL pointer dereference at 0000000000000080 ... Call Trace: new_read+0xfb/0x110 [dm_bufio] dm_bm_read_lock+0x43/0x190 [dm_persistent_data] ? kmem_cache_alloc_trace+0x15c/0x1e0 __create_persistent_data_objects+0x65/0x3e0 [dm_thin_pool] dm_pool_metadata_open+0x8c/0xf0 [dm_thin_pool] pool_ctr.cold.79+0x213/0x913 [dm_thin_pool] ? realloc_argv+0x50/0x70 [dm_mod] dm_table_add_target+0x14e/0x330 [dm_mod] table_load+0x122/0x2e0 [dm_mod] ? dev_status+0x40/0x40 [dm_mod] ctl_ioctl+0x1aa/0x3e0 [dm_mod] dm_ctl_ioctl+0xa/0x10 [dm_mod] do_vfs_ioctl+0xa2/0x600 ? handle_mm_fault+0xda/0x200 ? __do_page_fault+0x26c/0x4f0 ksys_ioctl+0x60/0x90 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x55/0x150 entry_SYSCALL_64_after_hwframe+0x44/0xa9 2. Creating a external snapshot using the same thin-pool device. dmsetup create thinp --table \ "0 41943040 thin-pool /dev/vdc /dev/vdb 128 0 2 ignore_discard" dmsetup message /dev/mapper/thinp 0 "create_thin 0" dmsetup create snap --table \ "0 204800 thin /dev/mapper/thinp 0 /dev/mapper/thinp" BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 ... Call Trace: ? __alloc_pages_nodemask+0x13c/0x2e0 retrieve_status+0xa5/0x1f0 [dm_mod] ? dm_get_live_or_inactive_table.isra.7+0x20/0x20 [dm_mod] table_status+0x61/0xa0 [dm_mod] ctl_ioctl+0x1aa/0x3e0 [dm_mod] dm_ctl_ioctl+0xa/0x10 [dm_mod] do_vfs_ioctl+0xa2/0x600 ksys_ioctl+0x60/0x90 ? ksys_write+0x4f/0xb0 __x64_sys_ioctl+0x16/0x20 do_syscall_64+0x55/0x150 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Signed-off-by: Jason Cai (Xiang Feng) Signed-off-by: Mike Snitzer Signed-off-by: Sasha Levin --- drivers/md/dm-thin.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c index e83b63608262..254c26eb963a 100644 --- a/drivers/md/dm-thin.c +++ b/drivers/md/dm-thin.c @@ -3283,6 +3283,13 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv) as.argc = argc; as.argv = argv; + /* make sure metadata and data are different devices */ + if (!strcmp(argv[0], argv[1])) { + ti->error = "Error setting metadata or data device"; + r = -EINVAL; + goto out_unlock; + } + /* * Set default pool features. */ @@ -4167,6 +4174,12 @@ static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv) tc->sort_bio_list = RB_ROOT; if (argc == 3) { + if (!strcmp(argv[0], argv[2])) { + ti->error = "Error setting origin device"; + r = -EINVAL; + goto bad_origin_dev; + } + r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev); if (r) { ti->error = "Error opening origin device"; -- 2.19.1