public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Anatol Pomozov <anatol.pomozov@gmail.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [ 080/124] loop: prevent bdev freeing while device in use
Date: Tue,  2 Apr 2013 15:11:02 -0700	[thread overview]
Message-ID: <20130402221113.417233666@linuxfoundation.org> (raw)
In-Reply-To: <20130402221104.163133110@linuxfoundation.org>

3.8-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: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 drivers/block/loop.c |    9 ++++++++-
 fs/block_dev.c       |    1 +
 2 files changed, 9 insertions(+), 1 deletion(-)

--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -917,6 +917,11 @@ static int loop_set_fd(struct loop_devic
 		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:
@@ -1026,8 +1031,10 @@ static int loop_clr_fd(struct loop_devic
 	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) {
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -551,6 +551,7 @@ struct block_device *bdgrab(struct block
 	ihold(bdev->bd_inode);
 	return bdev;
 }
+EXPORT_SYMBOL(bdgrab);
 
 long nr_blockdev_pages(void)
 {



  parent reply	other threads:[~2013-04-02 22:44 UTC|newest]

Thread overview: 157+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-02 22:09 [ 000/124] 3.8.6-stable review Greg Kroah-Hartman
2013-04-02 22:09 ` [ 001/124] SUNRPC: Add barriers to ensure read ordering in rpc_wake_up_task_queue_locked Greg Kroah-Hartman
2013-04-02 22:09 ` [ 002/124] tile: expect new initramfs name from hypervisor file system Greg Kroah-Hartman
2013-04-02 22:09 ` [ 003/124] Bluetooth: Fix not closing SCO sockets in the BT_CONNECT2 state Greg Kroah-Hartman
2013-04-02 22:09 ` [ 004/124] Bluetooth: Add support for Dell[QCA 0cf3:0036] Greg Kroah-Hartman
2013-04-02 22:09 ` [ 005/124] Bluetooth: Add support for Dell[QCA 0cf3:817a] Greg Kroah-Hartman
2013-04-02 22:09 ` [ 006/124] staging: comedi: s626: fix continuous acquisition Greg Kroah-Hartman
2013-04-02 22:09 ` [ 007/124] Revert "crypto: talitos - add IPsec ESN support" Greg Kroah-Hartman
2013-04-02 22:09 ` [ 008/124] Revert "crypto: caam " Greg Kroah-Hartman
2013-04-02 22:09 ` [ 009/124] sysfs: fix race between readdir and lseek Greg Kroah-Hartman
2013-04-02 22:09 ` [ 010/124] sysfs: handle failure path correctly for readdir() Greg Kroah-Hartman
2013-04-02 22:09 ` [ 011/124] can: sja1000: fix define conflict on SH Greg Kroah-Hartman
2013-04-02 22:09 ` [ 012/124] ath9k_hw: revert chainmask to user configuration after calibration Greg Kroah-Hartman
2013-04-02 22:09 ` [ 013/124] ath9k: limit tx path hang check to normal data queues Greg Kroah-Hartman
2013-04-02 22:09 ` [ 014/124] ath9k: avoid queueing hw check work when suspended Greg Kroah-Hartman
2013-04-02 22:09 ` [ 015/124] HID: usbhid: quirk for Realtek Multi-card reader Greg Kroah-Hartman
2013-04-02 22:09 ` [ 016/124] HID: usbhid: quirk for MSI GX680R led panel Greg Kroah-Hartman
2013-04-02 22:09 ` [ 017/124] HID: usbhid: fix build problem Greg Kroah-Hartman
2013-04-02 22:10 ` [ 018/124] rtlwifi: usb: add missing freeing of skbuff Greg Kroah-Hartman
2013-04-02 22:10 ` [ 019/124] iwlwifi: fix length check in multi-TB HCMD Greg Kroah-Hartman
2013-04-02 22:10 ` [ 020/124] b43: N-PHY: increase initial value of "mind" in RSSI calibration Greg Kroah-Hartman
2013-04-02 22:10 ` [ 021/124] b43: A fix for DMA transmission sequence errors Greg Kroah-Hartman
2013-04-02 22:10 ` [ 022/124] b43: N-PHY: use more bits for offset in RSSI calibration Greg Kroah-Hartman
2013-04-02 22:10 ` [ 023/124] tg3: fix length overflow in VPD firmware parsing Greg Kroah-Hartman
2013-04-02 22:10 ` [ 024/124] mac80211: always synchronize_net() during station removal Greg Kroah-Hartman
2013-04-02 22:10 ` [ 025/124] iommu/amd: Make sure dma_ops are set for hotplug devices Greg Kroah-Hartman
2013-04-02 22:10 ` [ 026/124] xen-pciback: notify hypervisor about devices intended to be assigned to guests Greg Kroah-Hartman
2013-04-02 22:10 ` [ 027/124] xen/blkback: correctly respond to unknown, non-native requests Greg Kroah-Hartman
2013-04-02 22:10 ` [ 028/124] xen-blkback: fix dispatch_rw_block_io() error path Greg Kroah-Hartman
2013-04-02 22:10 ` [ 029/124] xen-blkfront: switch from llist to list Greg Kroah-Hartman
2013-04-02 22:10 ` [ 030/124] tty: atmel_serial_probe(): index of atmel_ports[] fix Greg Kroah-Hartman
2013-04-02 22:10 ` [ 031/124] usb: ftdi_sio: Add support for Mitsubishi FX-USB-AW/-BD Greg Kroah-Hartman
2013-04-02 22:10 ` [ 032/124] vt: synchronize_rcu() under spinlock is not nice Greg Kroah-Hartman
2013-04-02 22:10 ` [ 033/124] mwifiex: fix race when queuing commands Greg Kroah-Hartman
2013-04-02 22:10 ` [ 034/124] mwifiex: skip pending commands after function shutdown Greg Kroah-Hartman
2013-04-02 22:10 ` [ 035/124] mwifiex: cancel cmd timer and free curr_cmd in shutdown process Greg Kroah-Hartman
2013-04-02 22:10 ` [ 036/124] pnfs-block: removing DM device maybe cause oops when call dev_remove Greg Kroah-Hartman
2013-04-02 22:10 ` [ 037/124] NFSv4: Fix the string length returned by the idmapper Greg Kroah-Hartman
2013-04-02 22:10 ` [ 038/124] NFSv4.1: Fix a race in pNFS layoutcommit Greg Kroah-Hartman
2013-04-02 22:10 ` [ 039/124] NFSv4.1: Always clear the NFS_INO_LAYOUTCOMMIT in layoutreturn Greg Kroah-Hartman
2013-04-02 22:10 ` [ 040/124] net/irda: add missing error path release_sock call Greg Kroah-Hartman
2013-04-02 22:10 ` [ 041/124] Nest rename_lock inside vfsmount_lock Greg Kroah-Hartman
2013-04-02 22:10 ` [ 042/124] USB: EHCI: fix bug in iTD/siTD DMA pool allocation Greg Kroah-Hartman
2013-04-02 22:10 ` [ 043/124] usb: xhci: Fix TRB transfer length macro used for Event TRB Greg Kroah-Hartman
2013-04-02 22:10 ` [ 044/124] target: Fix RESERVATION_CONFLICT status regression for iscsi-target special case Greg Kroah-Hartman
2013-04-02 22:10 ` [ 045/124] media: [REGRESSION] bt8xx: Fix too large height in cropcap Greg Kroah-Hartman
2013-04-02 22:10 ` [ 046/124] mac80211: prevent spurious HT/VHT downgrade message Greg Kroah-Hartman
2013-04-02 22:10 ` [ 047/124] scm: Require CAP_SYS_ADMIN over the current pidns to spoof pids Greg Kroah-Hartman
2013-04-02 22:10 ` [ 048/124] pid: Handle the exit of a multi-threaded init Greg Kroah-Hartman
2013-04-02 22:10 ` [ 049/124] userns: Dont allow creation if the user is chrooted Greg Kroah-Hartman
2013-04-02 22:10 ` [ 050/124] vfs: Add a mount flag to lock read only bind mounts Greg Kroah-Hartman
2013-04-02 22:10 ` [ 051/124] vfs: Carefully propogate mounts across user namespaces Greg Kroah-Hartman
2013-04-02 22:10 ` [ 052/124] ipc: Restrict mounting the mqueue filesystem Greg Kroah-Hartman
2013-04-02 22:10 ` [ 053/124] userns: Restrict when proc and sysfs can be mounted Greg Kroah-Hartman
2013-04-02 22:10 ` [ 054/124] Btrfs: use set_nlink if our i_nlink is 0 Greg Kroah-Hartman
2013-04-02 22:10 ` [ 055/124] Btrfs: fix locking on ROOT_REPLACE operations in tree mod log Greg Kroah-Hartman
2013-04-02 22:10 ` [ 056/124] Btrfs: fix race between mmap writes and compression Greg Kroah-Hartman
2013-04-02 22:10 ` [ 057/124] Btrfs: limit the global reserve to 512mb Greg Kroah-Hartman
2013-04-02 22:10 ` [ 058/124] Btrfs: dont drop path when printing out tree errors in scrub Greg Kroah-Hartman
2013-04-02 22:10 ` [ 059/124] USB: serial: add modem-status-change wait queue Greg Kroah-Hartman
2013-04-02 22:10 ` [ 060/124] USB: serial: fix hang when opening port Greg Kroah-Hartman
2013-04-02 22:10 ` [ 061/124] USB: quatech2: fix use-after-free in TIOCMIWAIT Greg Kroah-Hartman
2013-04-02 22:10 ` [ 062/124] USB: mos7840: fix broken TIOCMIWAIT Greg Kroah-Hartman
2013-04-02 22:10 ` [ 063/124] USB: io_ti: fix use-after-free in TIOCMIWAIT Greg Kroah-Hartman
2013-04-02 22:10 ` [ 064/124] USB: oti6858: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 065/124] USB: ftdi_sio: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 066/124] USB: pl2303: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 067/124] USB: mos7840: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 068/124] USB: spcp8x5: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 069/124] USB: ssu100: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 070/124] USB: ch341: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 071/124] USB: io_edgeport: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 072/124] USB: f81232: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 073/124] USB: ark3116: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 074/124] USB: mct_u232: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 075/124] USB: cypress_m8: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 076/124] USB: ti_usb_3410_5052: " Greg Kroah-Hartman
2013-04-02 22:10 ` [ 077/124] usb: gadget: udc-core: fix a regression during gadget driver unbinding Greg Kroah-Hartman
2013-04-02 22:11 ` [ 078/124] signal: Define __ARCH_HAS_SA_RESTORER so we know whether to clear sa_restorer Greg Kroah-Hartman
2013-04-02 22:11 ` [ 079/124] kernel/signal.c: use __ARCH_HAS_SA_RESTORER instead of SA_RESTORER Greg Kroah-Hartman
2013-04-02 22:11 ` Greg Kroah-Hartman [this message]
2013-04-02 22:11 ` [ 081/124] ARM: cns3xxx: fix mapping of private memory region Greg Kroah-Hartman
2013-04-02 22:11 ` [ 082/124] ARM: imx: fix sync issue between imx_cpu_die and imx_cpu_kill Greg Kroah-Hartman
2013-04-02 22:11 ` [ 083/124] ARM: kirkwood: Fix chip-delay for GoFlex Net Greg Kroah-Hartman
2013-04-02 22:11 ` [ 084/124] ARM: OMAP: clocks: Delay clk inits atleast until slab is initialized Greg Kroah-Hartman
2013-04-02 22:11 ` [ 085/124] virtio: console: rename cvq_lock to c_ivq_lock Greg Kroah-Hartman
2013-04-02 22:11 ` [ 086/124] virtio: console: add locking around c_ovq operations Greg Kroah-Hartman
2013-04-02 22:11 ` [ 087/124] nfsd4: reject "negative" acl lengths Greg Kroah-Hartman
2013-04-02 22:11 ` [ 088/124] drm/i915: Use the fixed pixel clock for eDP in intel_dp_set_m_n() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 089/124] drm/i915: Dont clobber crtc->fb when queue_flip fails Greg Kroah-Hartman
2013-04-02 22:11 ` [ 090/124] iwlwifi: dvm: dont send HCMD in restart flow Greg Kroah-Hartman
2013-04-02 22:11 ` [ 091/124] Btrfs: fix space leak when we fail to reserve metadata space Greg Kroah-Hartman
2013-04-02 22:11 ` [ 092/124] xen/events: avoid race with raising an event in unmask_evtchn() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 093/124] tracing: Prevent buffer overwrite disabled for latency tracers Greg Kroah-Hartman
2013-04-02 22:11 ` [ 094/124] efivars: explicitly calculate length of VariableName Greg Kroah-Hartman
2013-04-02 22:11 ` [ 095/124] efivars: Handle duplicate names from get_next_variable() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 096/124] regulator: Fix memory garbage dev_err printout Greg Kroah-Hartman
2013-04-02 22:11 ` [ 097/124] net: remove a WARN_ON() in net_enable_timestamp() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 098/124] sky2: Receive Overflows not counted Greg Kroah-Hartman
2013-04-02 22:11 ` [ 099/124] sky2: Threshold for Pause Packet is set wrong Greg Kroah-Hartman
2013-04-02 22:11 ` [ 100/124] tcp: preserve ACK clocking in TSO Greg Kroah-Hartman
2013-04-02 22:11 ` [ 101/124] tcp: undo spurious timeout after SACK reneging Greg Kroah-Hartman
2013-04-02 22:11 ` [ 102/124] 8021q: fix a potential use-after-free Greg Kroah-Hartman
2013-04-02 22:11 ` [ 103/124] thermal: shorten too long mcast group name Greg Kroah-Hartman
2013-04-02 22:11 ` [ 104/124] unix: fix a race condition in unix_release() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 105/124] af_unix: dont send SCM_CREDENTIAL when dest socket is NULL Greg Kroah-Hartman
2013-04-03 11:41   ` Sven Joachim
2013-04-03 14:00     ` Eric Dumazet
2013-04-03 15:10       ` Sven Joachim
2013-04-03 15:27         ` Greg Kroah-Hartman
2013-04-03 19:11           ` Stefan Lippers-Hollmann
2013-04-03 15:35         ` Eric Dumazet
2013-04-03 17:58           ` Andy Lutomirski
2013-04-03 18:43             ` Eric Dumazet
2013-04-03 19:01               ` Andy Lutomirski
2013-04-04  0:14                 ` Eric W. Biederman
2013-04-04  0:05     ` Eric W. Biederman
2013-04-04  0:19       ` Eric Dumazet
2013-04-04  0:30         ` Eric Dumazet
2013-04-04  1:01           ` Eric W. Biederman
2013-04-04  2:13             ` [PATCH 1/2] Revert "af_unix: dont send SCM_CREDENTIAL when dest socket is NULL" Eric W. Biederman
2013-04-04  2:14               ` [PATCH 2/2] af_unix: If we don't care about credentials coallesce all messages Eric W. Biederman
2013-04-04  3:28                 ` [PATCH 3/2] scm: Stop passing struct cred Eric W. Biederman
2013-04-05  4:47                   ` David Miller
2013-04-04  7:56                 ` [PATCH 2/2] af_unix: If we don't care about credentials coallesce all messages dingtianhong
2013-04-04 10:36                   ` Eric W. Biederman
2013-04-05  4:47                 ` David Miller
2013-04-04  7:51               ` [PATCH 1/2] Revert "af_unix: dont send SCM_CREDENTIAL when dest socket is NULL" dingtianhong
2013-04-04 10:22                 ` Eric W. Biederman
2013-04-05  4:47               ` David Miller
2013-04-04  0:47         ` [ 105/124] af_unix: dont send SCM_CREDENTIAL when dest socket is NULL Eric W. Biederman
2013-04-04  0:53           ` Andy Lutomirski
2013-04-04  2:24             ` Eric W. Biederman
2013-04-05  4:47     ` David Miller
2013-04-05 16:24       ` Greg KH
2013-04-07 21:17       ` Ben Hutchings
2013-04-02 22:11 ` [ 106/124] bonding: remove already created master sysfs link on failure Greg Kroah-Hartman
2013-04-02 22:11 ` [ 107/124] bonding: fix disabling of arp_interval and miimon Greg Kroah-Hartman
2013-04-02 22:11 ` [ 108/124] drivers: net: ethernet: davinci_emac: use netif_wake_queue() while restarting tx queue Greg Kroah-Hartman
2013-04-02 22:11 ` [ 109/124] drivers: net: ethernet: cpsw: " Greg Kroah-Hartman
2013-04-02 22:11 ` [ 110/124] net: fix *_DIAG_MAX constants Greg Kroah-Hartman
2013-04-02 22:11 ` [ 111/124] aoe: reserve enough headroom on skbs Greg Kroah-Hartman
2013-04-02 22:11 ` [ 112/124] atl1e: drop pci-msi support because of packet corruption Greg Kroah-Hartman
2013-04-02 22:11 ` [ 113/124] DM9000B: driver initialization upgrade Greg Kroah-Hartman
2013-04-02 22:11 ` [ 114/124] fec: Fix the build as module Greg Kroah-Hartman
2013-04-02 22:11 ` [ 115/124] ipv6: fix bad free of addrconf_init_net Greg Kroah-Hartman
2013-04-02 22:11 ` [ 116/124] ipv6: dont accept multicast traffic with scope 0 Greg Kroah-Hartman
2013-04-02 22:11 ` [ 117/124] ipv6: dont accept node local multicast traffic from the wire Greg Kroah-Hartman
2013-04-02 22:11 ` [ 118/124] ks8851: Fix interpretation of rxlen field Greg Kroah-Hartman
2013-04-02 22:11 ` [ 119/124] net: add a synchronize_net() in netdev_rx_handler_unregister() Greg Kroah-Hartman
2013-04-02 22:11 ` [ 120/124] net: ethernet: cpsw: fix erroneous condition in error check Greg Kroah-Hartman
2013-04-02 22:11 ` [ 121/124] net: fix the use of this_cpu_ptr Greg Kroah-Hartman
2013-04-02 22:11 ` [ 122/124] net: fq_codel: Fix off-by-one error Greg Kroah-Hartman
2013-04-02 22:11 ` [ 123/124] pch_gbe: fix ip_summed checksum reporting on rx Greg Kroah-Hartman
2013-04-02 22:11 ` [ 124/124] smsc75xx: fix jumbo frame support Greg Kroah-Hartman
2013-04-03 15:17 ` [ 000/124] 3.8.6-stable review Shuah Khan
2013-04-03 15:28   ` Greg Kroah-Hartman

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=20130402221113.417233666@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=anatol.pomozov@gmail.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