diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2012-01-12 10:01:28 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-01-14 18:07:24 -0500 |
commit | 0bfc96cb77224736dfa35c3c555d37b3646ef35e (patch) | |
tree | af5135c929ee8e1bdb38c306b2634dbc547d5189 /block | |
parent | 577ebb374c78314ac4617242f509e2f5e7156649 (diff) |
block: fail SCSI passthrough ioctls on partition devices
Linux allows executing the SG_IO ioctl on a partition or LVM volume, and
will pass the command to the underlying block device. This is
well-known, but it is also a large security problem when (via Unix
permissions, ACLs, SELinux or a combination thereof) a program or user
needs to be granted access only to part of the disk.
This patch lets partitions forward a small set of harmless ioctls;
others are logged with printk so that we can see which ioctls are
actually sent. In my tests only CDROM_GET_CAPABILITY actually occurred.
Of course it was being sent to a (partition on a) hard disk, so it would
have failed with ENOTTY and the patch isn't changing anything in
practice. Still, I'm treating it specially to avoid spamming the logs.
In principle, this restriction should include programs running with
CAP_SYS_RAWIO. If for example I let a program access /dev/sda2 and
/dev/sdb, it still should not be able to read/write outside the
boundaries of /dev/sda2 independent of the capabilities. However, for
now programs with CAP_SYS_RAWIO will still be allowed to send the
ioctls. Their actions will still be logged.
This patch does not affect the non-libata IDE driver. That driver
however already tests for bd != bd->bd_contains before issuing some
ioctl; it could be restricted further to forbid these ioctls even for
programs running with CAP_SYS_ADMIN/CAP_SYS_RAWIO.
Cc: linux-scsi@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>
Cc: James Bottomley <JBottomley@parallels.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[ Make it also print the command name when warning - Linus ]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'block')
-rw-r--r-- | block/scsi_ioctl.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c index a2c11f330872..260fa80ef575 100644 --- a/block/scsi_ioctl.c +++ b/block/scsi_ioctl.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <linux/capability.h> | 24 | #include <linux/capability.h> |
25 | #include <linux/completion.h> | 25 | #include <linux/completion.h> |
26 | #include <linux/cdrom.h> | 26 | #include <linux/cdrom.h> |
27 | #include <linux/ratelimit.h> | ||
27 | #include <linux/slab.h> | 28 | #include <linux/slab.h> |
28 | #include <linux/times.h> | 29 | #include <linux/times.h> |
29 | #include <asm/uaccess.h> | 30 | #include <asm/uaccess.h> |
@@ -690,9 +691,53 @@ int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mod | |||
690 | } | 691 | } |
691 | EXPORT_SYMBOL(scsi_cmd_ioctl); | 692 | EXPORT_SYMBOL(scsi_cmd_ioctl); |
692 | 693 | ||
694 | int scsi_verify_blk_ioctl(struct block_device *bd, unsigned int cmd) | ||
695 | { | ||
696 | if (bd && bd == bd->bd_contains) | ||
697 | return 0; | ||
698 | |||
699 | /* Actually none of these is particularly useful on a partition, | ||
700 | * but they are safe. | ||
701 | */ | ||
702 | switch (cmd) { | ||
703 | case SCSI_IOCTL_GET_IDLUN: | ||
704 | case SCSI_IOCTL_GET_BUS_NUMBER: | ||
705 | case SCSI_IOCTL_GET_PCI: | ||
706 | case SCSI_IOCTL_PROBE_HOST: | ||
707 | case SG_GET_VERSION_NUM: | ||
708 | case SG_SET_TIMEOUT: | ||
709 | case SG_GET_TIMEOUT: | ||
710 | case SG_GET_RESERVED_SIZE: | ||
711 | case SG_SET_RESERVED_SIZE: | ||
712 | case SG_EMULATED_HOST: | ||
713 | return 0; | ||
714 | case CDROM_GET_CAPABILITY: | ||
715 | /* Keep this until we remove the printk below. udev sends it | ||
716 | * and we do not want to spam dmesg about it. CD-ROMs do | ||
717 | * not have partitions, so we get here only for disks. | ||
718 | */ | ||
719 | return -ENOIOCTLCMD; | ||
720 | default: | ||
721 | break; | ||
722 | } | ||
723 | |||
724 | /* In particular, rule out all resets and host-specific ioctls. */ | ||
725 | printk_ratelimited(KERN_WARNING | ||
726 | "%s: sending ioctl %x to a partition!\n", current->comm, cmd); | ||
727 | |||
728 | return capable(CAP_SYS_RAWIO) ? 0 : -ENOIOCTLCMD; | ||
729 | } | ||
730 | EXPORT_SYMBOL(scsi_verify_blk_ioctl); | ||
731 | |||
693 | int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode, | 732 | int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode, |
694 | unsigned int cmd, void __user *arg) | 733 | unsigned int cmd, void __user *arg) |
695 | { | 734 | { |
735 | int ret; | ||
736 | |||
737 | ret = scsi_verify_blk_ioctl(bd, cmd); | ||
738 | if (ret < 0) | ||
739 | return ret; | ||
740 | |||
696 | return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg); | 741 | return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg); |
697 | } | 742 | } |
698 | EXPORT_SYMBOL(scsi_cmd_blk_ioctl); | 743 | EXPORT_SYMBOL(scsi_cmd_blk_ioctl); |