All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl
@ 2016-12-15 17:48 Mauricio Faria de Oliveira
  2016-12-19 10:39 ` Christoph Hellwig
  2016-12-19 15:34 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Mauricio Faria de Oliveira @ 2016-12-15 17:48 UTC (permalink / raw)
  To: axboe, linux-block
  Cc: hch, linux-scsi, linux-kernel, pbonzini, latha, manjuhr1

The WRITE_SAME commands are not present in the blk_default_cmd_filter
write_ok list, and thus are failed with -EPERM when the SG_IO ioctl()
is executed without CAP_SYS_RAWIO capability (e.g., unprivileged users).
[ sg_io() -> blk_fill_sghdr_rq() > blk_verify_command() -> -EPERM ]

The problem can be reproduced with the sg_write_same command

  # sg_write_same --num 1 --xferlen 512 /dev/sda
  #

  # capsh --drop=cap_sys_rawio -- -c \
    'sg_write_same --num 1 --xferlen 512 /dev/sda'
    Write same: pass through os error: Operation not permitted
  #

For comparison, the WRITE_VERIFY command does not observe this problem,
since it is in that list:

  # capsh --drop=cap_sys_rawio -- -c \
    'sg_write_verify --num 1 --ilen 512 --lba 0 /dev/sda'
  #

So, this patch adds the WRITE_SAME commands to the list, in order
for the SG_IO ioctl to finish successfully:

  # capsh --drop=cap_sys_rawio -- -c \
    'sg_write_same --num 1 --xferlen 512 /dev/sda'
  #

That case happens to be exercised by QEMU KVM guests with 'scsi-block' devices
(qemu "-device scsi-block" [1], libvirt "<disk type='block' device='lun'>" [2]),
which employs the SG_IO ioctl() and runs as an unprivileged user (libvirt-qemu).

In that scenario, when a filesystem (e.g., ext4) performs its zero-out calls,
which are translated to write-same calls in the guest kernel, and then into
SG_IO ioctls to the host kernel, SCSI I/O errors may be observed in the guest:

  [...] sd 0:0:0:0: [sda] tag#0 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
  [...] sd 0:0:0:0: [sda] tag#0 Sense Key : Aborted Command [current]
  [...] sd 0:0:0:0: [sda] tag#0 Add. Sense: I/O process terminated
  [...] sd 0:0:0:0: [sda] tag#0 CDB: Write Same(10) 41 00 01 04 e0 78 00 00 08 00
  [...] blk_update_request: I/O error, dev sda, sector 17096824

Links:
[1] http://git.qemu.org/?p=qemu.git;a=commit;h=336a6915bc7089fb20fea4ba99972ad9a97c5f52
[2] https://libvirt.org/formatdomain.html#elementsDisks (see 'disk' -> 'device')

Signed-off-by: Mauricio Faria de Oliveira <mauricfo@linux.vnet.ibm.com>
Signed-off-by: Brahadambal Srinivasan <latha@linux.vnet.ibm.com>
Reported-by: Manjunatha H R <manjuhr1@in.ibm.com>
---
 block/scsi_ioctl.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c
index 0774799..c6fee74 100644
--- a/block/scsi_ioctl.c
+++ b/block/scsi_ioctl.c
@@ -182,6 +182,9 @@ static void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
 	__set_bit(WRITE_16, filter->write_ok);
 	__set_bit(WRITE_LONG, filter->write_ok);
 	__set_bit(WRITE_LONG_2, filter->write_ok);
+	__set_bit(WRITE_SAME, filter->write_ok);
+	__set_bit(WRITE_SAME_16, filter->write_ok);
+	__set_bit(WRITE_SAME_32, filter->write_ok);
 	__set_bit(ERASE, filter->write_ok);
 	__set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
 	__set_bit(MODE_SELECT, filter->write_ok);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl
  2016-12-15 17:48 [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl Mauricio Faria de Oliveira
@ 2016-12-19 10:39 ` Christoph Hellwig
  2016-12-19 15:34 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2016-12-19 10:39 UTC (permalink / raw)
  To: Mauricio Faria de Oliveira
  Cc: axboe, linux-block, hch, linux-scsi, linux-kernel, pbonzini,
	latha, manjuhr1

Looks fine,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl
  2016-12-15 17:48 [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl Mauricio Faria de Oliveira
  2016-12-19 10:39 ` Christoph Hellwig
@ 2016-12-19 15:34 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2016-12-19 15:34 UTC (permalink / raw)
  To: Mauricio Faria de Oliveira, linux-block
  Cc: hch, linux-scsi, linux-kernel, pbonzini, latha, manjuhr1

On 12/15/2016 10:48 AM, Mauricio Faria de Oliveira wrote:
> The WRITE_SAME commands are not present in the blk_default_cmd_filter
> write_ok list, and thus are failed with -EPERM when the SG_IO ioctl()
> is executed without CAP_SYS_RAWIO capability (e.g., unprivileged users).
> [ sg_io() -> blk_fill_sghdr_rq() > blk_verify_command() -> -EPERM ]

Added for 4.10, thanks.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-12-19 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-15 17:48 [PATCH RESEND] block: allow WRITE_SAME commands with the SG_IO ioctl Mauricio Faria de Oliveira
2016-12-19 10:39 ` Christoph Hellwig
2016-12-19 15:34 ` Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.