mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luis Henriques <luis.henriques@canonical.com>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	kernel-team@lists.ubuntu.com
Cc: Anatol Pomozov <anatol.pomozov@gmail.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Luis Henriques <luis.henriques@canonical.com>
Subject: [PATCH 096/102] loop: prevent bdev freeing while device in use
Date: Mon,  8 Apr 2013 10:50:51 +0100	[thread overview]
Message-ID: <1365414657-29191-97-git-send-email-luis.henriques@canonical.com> (raw)
In-Reply-To: <1365414657-29191-1-git-send-email-luis.henriques@canonical.com>

3.5.7.10 -stable review patch.  If anyone has any objections, please let me know.

------------------

From: Anatol Pomozov <anatol.pomozov@gmail.com>

commit c1681bf8a7b1b98edee8b862a42c19c4e53205fd upstream.

struct block_device lifecycle is defined by its inode (see fs/block_dev.c) -
block_device allocated first time we access /dev/loopXX and deallocated on
bdev_destroy_inode. When we create the device "losetup /dev/loopXX afile"
we want that block_device stay alive until we destroy the loop device
with "losetup -d".

But because we do not hold /dev/loopXX inode its counter goes 0, and
inode/bdev can be destroyed at any moment. Usually it happens at memory
pressure or when user drops inode cache (like in the test below). When later in
loop_clr_fd() we want to use bdev we have use-after-free error with following
stack:

BUG: unable to handle kernel NULL pointer dereference at 0000000000000280
  bd_set_size+0x10/0xa0
  loop_clr_fd+0x1f8/0x420 [loop]
  lo_ioctl+0x200/0x7e0 [loop]
  lo_compat_ioctl+0x47/0xe0 [loop]
  compat_blkdev_ioctl+0x341/0x1290
  do_filp_open+0x42/0xa0
  compat_sys_ioctl+0xc1/0xf20
  do_sys_open+0x16e/0x1d0
  sysenter_dispatch+0x7/0x1a

To prevent use-after-free we need to grab the device in loop_set_fd()
and put it later in loop_clr_fd().

The issue is reprodusible on current Linus head and v3.3. Here is the test:

  dd if=/dev/zero of=loop.file bs=1M count=1
  while [ true ]; do
    losetup /dev/loop0 loop.file
    echo 2 > /proc/sys/vm/drop_caches
    losetup -d /dev/loop0
  done

[ Doing bdgrab/bput in loop_set_fd/loop_clr_fd is safe, because every
  time we call loop_set_fd() we check that loop_device->lo_state is
  Lo_unbound and set it to Lo_bound If somebody will try to set_fd again
  it will get EBUSY.  And if we try to loop_clr_fd() on unbound loop
  device we'll get ENXIO.

  loop_set_fd/loop_clr_fd (and any other loop ioctl) is called under
  loop_device->lo_ctl_mutex. ]

Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Luis Henriques <luis.henriques@canonical.com>
---
 drivers/block/loop.c | 9 ++++++++-
 fs/block_dev.c       | 1 +
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index a77d9a5..c5e0f73 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -908,6 +908,11 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 		lo->lo_flags |= LO_FLAGS_PARTSCAN;
 	if (lo->lo_flags & LO_FLAGS_PARTSCAN)
 		ioctl_by_bdev(bdev, BLKRRPART, 0);
+
+	/* Grab the block_device to prevent its destruction after we
+	 * put /dev/loopXX inode. Later in loop_clr_fd() we bdput(bdev).
+	 */
+	bdgrab(bdev);
 	return 0;
 
 out_clr:
@@ -1004,8 +1009,10 @@ static int loop_clr_fd(struct loop_device *lo)
 	memset(lo->lo_encrypt_key, 0, LO_KEY_SIZE);
 	memset(lo->lo_crypt_name, 0, LO_NAME_SIZE);
 	memset(lo->lo_file_name, 0, LO_NAME_SIZE);
-	if (bdev)
+	if (bdev) {
+		bdput(bdev);
 		invalidate_bdev(bdev);
+	}
 	set_capacity(lo->lo_disk, 0);
 	loop_sysfs_exit(lo);
 	if (bdev) {
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 77e86b3..1244094 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -604,6 +604,7 @@ struct block_device *bdgrab(struct block_device *bdev)
 	ihold(bdev->bd_inode);
 	return bdev;
 }
+EXPORT_SYMBOL(bdgrab);
 
 long nr_blockdev_pages(void)
 {
-- 
1.8.1.2


  parent reply	other threads:[~2013-04-08  9:53 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-08  9:49 [ 3.5.y.z extended stable ] Linux 3.5.7.10 stable review Luis Henriques
2013-04-08  9:49 ` [PATCH 001/102] clockevents: Don't allow dummy broadcast timers Luis Henriques
2013-04-08  9:49 ` [PATCH 002/102] Bluetooth: Add support for Dell[QCA 0cf3:0036] Luis Henriques
2013-04-08  9:49 ` [PATCH 003/102] Bluetooth: Add support for Dell[QCA 0cf3:817a] Luis Henriques
2013-04-08  9:49 ` [PATCH 004/102] ath9k_hw: revert chainmask to user configuration after calibration Luis Henriques
2013-04-08  9:49 ` [PATCH 005/102] ath9k: limit tx path hang check to normal data queues Luis Henriques
2013-04-08  9:49 ` [PATCH 006/102] rtlwifi: usb: add missing freeing of skbuff Luis Henriques
2013-04-08  9:49 ` [PATCH 007/102] net/irda: add missing error path release_sock call Luis Henriques
2013-04-08  9:49 ` [PATCH 008/102] NFSv4: Fix the string length returned by the idmapper Luis Henriques
2013-04-08  9:49 ` [PATCH 009/102] pnfs-block: removing DM device maybe cause oops when call dev_remove Luis Henriques
2013-04-08  9:49 ` [PATCH 010/102] NFSv4.1: Fix a race in pNFS layoutcommit Luis Henriques
2013-04-08  9:49 ` [PATCH 011/102] IPoIB: Fix send lockup due to missed TX completion Luis Henriques
2013-04-08  9:49 ` [PATCH 012/102] SUNRPC: Add barriers to ensure read ordering in rpc_wake_up_task_queue_locked Luis Henriques
2013-04-08  9:49 ` [PATCH 013/102] Nest rename_lock inside vfsmount_lock Luis Henriques
2013-04-08  9:49 ` [PATCH 014/102] vt: synchronize_rcu() under spinlock is not nice Luis Henriques
2013-04-08  9:49 ` [PATCH 015/102] iommu/amd: Make sure dma_ops are set for hotplug devices Luis Henriques
2013-04-08  9:49 ` [PATCH 016/102] i915: initialize CADL in opregion Luis Henriques
2013-04-08  9:49 ` [PATCH 017/102] tracing: Protect tracer flags with trace_types_lock Luis Henriques
2013-04-08  9:49 ` [PATCH 018/102] tracing: Prevent buffer overwrite disabled for latency tracers Luis Henriques
2013-04-08  9:49 ` [PATCH 019/102] nohz: Make tick_nohz_irq_exit() irq safe Luis Henriques
2013-04-08  9:49 ` [PATCH 020/102] udf: Fix bitmap overflow on large filesystems with small block size Luis Henriques
2013-04-08  9:49 ` [PATCH 021/102] xen/blkback: correctly respond to unknown, non-native requests Luis Henriques
2013-04-08  9:49 ` [PATCH 022/102] tty: atmel_serial_probe(): index of atmel_ports[] fix Luis Henriques
2013-04-08  9:49 ` [PATCH 023/102] HID: usbhid: quirk for Realtek Multi-card reader Luis Henriques
2013-04-08  9:49 ` [PATCH 024/102] HID: usbhid: quirk for MSI GX680R led panel Luis Henriques
2013-04-08  9:49 ` [PATCH 025/102] xen-blkback: fix dispatch_rw_block_io() error path Luis Henriques
2013-04-08  9:49 ` [PATCH 026/102] sysfs: handle failure path correctly for readdir() Luis Henriques
2013-04-08  9:49 ` [PATCH 027/102] usb: xhci: Fix TRB transfer length macro used for Event TRB Luis Henriques
2013-04-08  9:49 ` [PATCH 028/102] staging: comedi: s626: fix continuous acquisition Luis Henriques
2013-04-08  9:49 ` [PATCH 029/102] USB: serial: fix hang when opening port Luis Henriques
2013-04-08  9:49 ` [PATCH 030/102] Btrfs: fix race between mmap writes and compression Luis Henriques
2013-04-08  9:49 ` [PATCH 031/102] Btrfs: fix space leak when we fail to reserve metadata space Luis Henriques
2013-04-08  9:49 ` [PATCH 032/102] Btrfs: limit the global reserve to 512mb Luis Henriques
2013-04-08  9:49 ` [PATCH 033/102] usb: ftdi_sio: Add support for Mitsubishi FX-USB-AW/-BD Luis Henriques
2013-04-08  9:49 ` [PATCH 034/102] Btrfs: don't drop path when printing out tree errors in scrub Luis Henriques
2013-04-08  9:49 ` [PATCH 035/102] USB: serial: add modem-status-change wait queue Luis Henriques
2013-04-08 10:01   ` Johan Hovold
2013-04-08 10:15     ` Luis Henriques
2013-04-08  9:49 ` [PATCH 036/102] USB: ark3116: fix use-after-free in TIOCMIWAIT Luis Henriques
2013-04-08  9:49 ` [PATCH 037/102] USB: ch341: " Luis Henriques
2013-04-08  9:49 ` [PATCH 038/102] USB: cypress_m8: " Luis Henriques
2013-04-08  9:49 ` [PATCH 039/102] USB: f81232: " Luis Henriques
2013-04-08  9:49 ` [PATCH 040/102] USB: ftdi_sio: " Luis Henriques
2013-04-08  9:49 ` [PATCH 041/102] USB: io_edgeport: " Luis Henriques
2013-04-08  9:49 ` [PATCH 042/102] USB: io_ti: " Luis Henriques
2013-04-08  9:49 ` [PATCH 043/102] USB: mct_u232: " Luis Henriques
2013-04-08  9:49 ` [PATCH 044/102] USB: mos7840: fix broken TIOCMIWAIT Luis Henriques
2013-04-08  9:50 ` [PATCH 045/102] USB: mos7840: fix use-after-free in TIOCMIWAIT Luis Henriques
2013-04-08  9:50 ` [PATCH 046/102] USB: oti6858: " Luis Henriques
2013-04-08  9:50 ` [PATCH 047/102] USB: pl2303: " Luis Henriques
2013-04-08  9:50 ` [PATCH 048/102] USB: quatech2: " Luis Henriques
2013-04-08  9:50 ` [PATCH 049/102] USB: spcp8x5: " Luis Henriques
2013-04-08  9:50 ` [PATCH 050/102] USB: ssu100: " Luis Henriques
2013-04-08  9:50 ` [PATCH 051/102] USB: ti_usb_3410_5052: " Luis Henriques
2013-04-08  9:50 ` [PATCH 052/102] Btrfs: use set_nlink if our i_nlink is 0 Luis Henriques
2013-04-08  9:50 ` [PATCH 053/102] Bluetooth: Fix not closing SCO sockets in the BT_CONNECT2 state Luis Henriques
2013-04-08  9:50 ` [PATCH 054/102] mwifiex: cancel cmd timer and free curr_cmd in shutdown process Luis Henriques
2013-04-08 18:03   ` Bing Zhao
2013-04-09  8:51     ` Luis Henriques
2013-04-08  9:50 ` [PATCH 055/102] HID: usbhid: fix build problem Luis Henriques
2013-04-08  9:50 ` [PATCH 056/102] sysfs: fix race between readdir and lseek Luis Henriques
2013-04-08  9:50 ` [PATCH 057/102] net: remove a WARN_ON() in net_enable_timestamp() Luis Henriques
2013-04-08  9:50 ` [PATCH 058/102] sky2: Receive Overflows not counted Luis Henriques
2013-04-08  9:50 ` [PATCH 059/102] sky2: Threshold for Pause Packet is set wrong Luis Henriques
2013-04-08  9:50 ` [PATCH 060/102] tcp: preserve ACK clocking in TSO Luis Henriques
2013-04-08  9:50 ` [PATCH 061/102] tcp: undo spurious timeout after SACK reneging Luis Henriques
2013-04-08  9:50 ` [PATCH 062/102] 8021q: fix a potential use-after-free Luis Henriques
2013-04-08  9:50 ` [PATCH 063/102] thermal: shorten too long mcast group name Luis Henriques
2013-04-08  9:50 ` [PATCH 064/102] genetlink: trigger BUG_ON if a group name is too long Luis Henriques
2013-04-08  9:50 ` [PATCH 065/102] unix: fix a race condition in unix_release() Luis Henriques
2013-04-08  9:50 ` [PATCH 066/102] bonding: remove already created master sysfs link on failure Luis Henriques
2013-04-08  9:50 ` [PATCH 067/102] bonding: fix miimon and arp_interval delayed work race conditions Luis Henriques
2013-04-08  9:50 ` [PATCH 068/102] bonding: fix disabling of arp_interval and miimon Luis Henriques
2013-04-08  9:50 ` [PATCH 069/102] drivers: net: ethernet: davinci_emac: use netif_wake_queue() while restarting tx queue Luis Henriques
2013-04-08  9:50 ` [PATCH 070/102] drivers: net: ethernet: cpsw: " Luis Henriques
2013-04-08  9:50 ` [PATCH 071/102] net: fix *_DIAG_MAX constants Luis Henriques
2013-04-08  9:50 ` [PATCH 072/102] aoe: reserve enough headroom on skbs Luis Henriques
2013-04-08  9:50 ` [PATCH 073/102] atl1e: drop pci-msi support because of packet corruption Luis Henriques
2013-04-08  9:50 ` [PATCH 074/102] DM9000B: driver initialization upgrade Luis Henriques
2013-04-08  9:50 ` [PATCH 075/102] ipv6: don't accept multicast traffic with scope 0 Luis Henriques
2013-04-08  9:50 ` [PATCH 076/102] ipv6: fix bad free of addrconf_init_net Luis Henriques
2013-04-08  9:50 ` [PATCH 077/102] ipv6: don't accept node local multicast traffic from the wire Luis Henriques
2013-04-08  9:50 ` [PATCH 078/102] ks8851: Fix interpretation of rxlen field Luis Henriques
2013-04-08  9:50 ` [PATCH 079/102] net: add a synchronize_net() in netdev_rx_handler_unregister() Luis Henriques
2013-04-08  9:50 ` [PATCH 080/102] pch_gbe: fix ip_summed checksum reporting on rx Luis Henriques
2013-04-08  9:50 ` [PATCH 081/102] smsc75xx: fix jumbo frame support Luis Henriques
2013-04-08  9:50 ` [PATCH 082/102] bonding: get netdev_rx_handler_unregister out of locks Luis Henriques
2013-04-08  9:50 ` [PATCH 083/102] mac80211: always synchronize_net() during station removal Luis Henriques
2013-04-08  9:50 ` [PATCH 084/102] regmap: cache Fix regcache-rbtree sync Luis Henriques
2013-04-08  9:50 ` [PATCH 085/102] iwlwifi: dvm: don't send HCMD in restart flow Luis Henriques
2013-04-08  9:50 ` [PATCH 086/102] nfsd4: reject "negative" acl lengths Luis Henriques
2013-04-08  9:50 ` [PATCH 087/102] can: sja1000: fix define conflict on SH Luis Henriques
2013-04-08  9:50 ` [PATCH 088/102] b43: N-PHY: increase initial value of "mind" in RSSI calibration Luis Henriques
2013-04-08  9:50 ` [PATCH 089/102] b43: A fix for DMA transmission sequence errors Luis Henriques
2013-04-08  9:50 ` [PATCH 090/102] b43: N-PHY: use more bits for offset in RSSI calibration Luis Henriques
2013-04-08  9:50 ` [PATCH 091/102] tg3: fix length overflow in VPD firmware parsing Luis Henriques
2013-04-08  9:50 ` [PATCH 092/102] tile: expect new initramfs name from hypervisor file system Luis Henriques
2013-04-08  9:50 ` [PATCH 093/102] virtio: console: rename cvq_lock to c_ivq_lock Luis Henriques
2013-04-08  9:50 ` [PATCH 094/102] virtio: console: add locking around c_ovq operations Luis Henriques
2013-04-08  9:50 ` [PATCH 095/102] ARM: cns3xxx: fix mapping of private memory region Luis Henriques
2013-04-08  9:50 ` Luis Henriques [this message]
2013-04-08  9:50 ` [PATCH 097/102] efivars: explicitly calculate length of VariableName Luis Henriques
2013-04-09 22:45   ` Ben Hutchings
2013-04-10  9:35     ` Luis Henriques
2013-04-10 10:27     ` Lingzhu Xiang
2013-04-10 12:17       ` Luis Henriques
2013-04-10 15:57         ` Seiji Aguchi
2013-04-11  9:12           ` Luis Henriques
2013-04-16 10:33             ` Luis Henriques
2013-04-17  4:37               ` Lingzhu Xiang
2013-04-17 11:56                 ` Andy Whitcroft
2013-04-17 12:13                   ` Lingzhu Xiang
2013-04-17 13:28                     ` Luis Henriques
2013-04-18  3:27                       ` Lingzhu Xiang
2013-04-18  8:58                         ` Luis Henriques
2013-04-08  9:50 ` [PATCH 098/102] efivars: Handle duplicate names from get_next_variable() Luis Henriques
2013-04-08  9:50 ` [PATCH 099/102] thermal: return an error on failure to register thermal class Luis Henriques
2013-04-08  9:50 ` [PATCH 100/102] UBIFS: make space fixup work in the remount case Luis Henriques
2013-04-08  9:50 ` [PATCH 101/102] reiserfs: Fix warning and inode leak when deleting inode with xattrs Luis Henriques
2013-04-08  9:50 ` [PATCH 102/102] mm: prevent mmap_cache race in find_vma() Luis Henriques

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1365414657-29191-97-git-send-email-luis.henriques@canonical.com \
    --to=luis.henriques@canonical.com \
    --cc=anatol.pomozov@gmail.com \
    --cc=kernel-team@lists.ubuntu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®