Skip to content

Commit 0bfc96c

Browse files
bonzinitorvalds
authored andcommitted
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>
1 parent 577ebb3 commit 0bfc96c

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎block/scsi_ioctl.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/capability.h>
2525
#include <linux/completion.h>
2626
#include <linux/cdrom.h>
27+
#include <linux/ratelimit.h>
2728
#include <linux/slab.h>
2829
#include <linux/times.h>
2930
#include <asm/uaccess.h>
@@ -690,9 +691,53 @@ int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mod
690691
}
691692
EXPORT_SYMBOL(scsi_cmd_ioctl);
692693

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+
693732
int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode,
694733
unsigned int cmd, void __user *arg)
695734
{
735+
int ret;
736+
737+
ret = scsi_verify_blk_ioctl(bd, cmd);
738+
if (ret < 0)
739+
return ret;
740+
696741
return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg);
697742
}
698743
EXPORT_SYMBOL(scsi_cmd_blk_ioctl);

‎drivers/scsi/sd.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,10 @@ static int sd_ioctl(struct block_device *bdev, fmode_t mode,
10751075
SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
10761076
"cmd=0x%x\n", disk->disk_name, cmd));
10771077

1078+
error = scsi_verify_blk_ioctl(bdev, cmd);
1079+
if (error < 0)
1080+
return error;
1081+
10781082
/*
10791083
* If we are in the middle of error recovery, don't let anyone
10801084
* else try and use this device. Also, if error recovery fails, it
@@ -1267,6 +1271,11 @@ static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
12671271
unsigned int cmd, unsigned long arg)
12681272
{
12691273
struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1274+
int ret;
1275+
1276+
ret = scsi_verify_blk_ioctl(bdev, cmd);
1277+
if (ret < 0)
1278+
return ret;
12701279

12711280
/*
12721281
* If we are in the middle of error recovery, don't let anyone
@@ -1278,8 +1287,6 @@ static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
12781287
return -ENODEV;
12791288

12801289
if (sdev->host->hostt->compat_ioctl) {
1281-
int ret;
1282-
12831290
ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
12841291

12851292
return ret;

‎include/linux/blkdev.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ extern int blk_insert_cloned_request(struct request_queue *q,
675675
struct request *rq);
676676
extern void blk_delay_queue(struct request_queue *, unsigned long);
677677
extern void blk_recount_segments(struct request_queue *, struct bio *);
678+
extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
678679
extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
679680
unsigned int, void __user *);
680681
extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,

0 commit comments

Comments
 (0)