Skip to content

Commit bba84ae

Browse files
arndbmartinkpetersen
authored andcommitted
scsi: megaraid_sas: Simplify compat_ioctl handling
There have been several attempts to fix serious problems in the compat handling in megasas_mgmt_compat_ioctl_fw(), and it also uses the compat_alloc_user_space() function. Folding the compat handling into the regular ioctl function with in_compat_syscall() simplifies it a lot and avoids some of the remaining problems: - missing handling of unaligned pointers - overflowing the ioc->frame.raw array from invalid input - compat_alloc_user_space() Link: https://lore.kernel.org/r/20201030164450.1253641-3-arnd@kernel.org Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 381d34e commit bba84ae

2 files changed

Lines changed: 46 additions & 74 deletions

File tree

‎drivers/scsi/megaraid/megaraid_sas.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,6 @@ struct megasas_aen {
26052605
u32 class_locale_word;
26062606
} __attribute__ ((packed));
26072607

2608-
#ifdef CONFIG_COMPAT
26092608
struct compat_megasas_iocpacket {
26102609
u16 host_no;
26112610
u16 __pad1;
@@ -2621,7 +2620,6 @@ struct compat_megasas_iocpacket {
26212620
} __attribute__ ((packed));
26222621

26232622
#define MEGASAS_IOC_FIRMWARE32 _IOWR('M', 1, struct compat_megasas_iocpacket)
2624-
#endif
26252623

26262624
#define MEGASAS_IOC_FIRMWARE _IOWR('M', 1, struct megasas_iocpacket)
26272625
#define MEGASAS_IOC_GET_AEN _IOW('M', 3, struct megasas_aen)

‎drivers/scsi/megaraid/megaraid_sas_base.c‎

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8319,16 +8319,19 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
83198319
* copy out the sense
83208320
*/
83218321
if (ioc->sense_len) {
8322+
void __user *uptr;
83228323
/*
83238324
* sense_ptr points to the location that has the user
83248325
* sense buffer address
83258326
*/
8326-
sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8327-
ioc->sense_off);
8327+
sense_ptr = (void *)ioc->frame.raw + ioc->sense_off;
8328+
if (in_compat_syscall())
8329+
uptr = compat_ptr(get_unaligned((compat_uptr_t *)
8330+
sense_ptr));
8331+
else
8332+
uptr = get_unaligned((void __user **)sense_ptr);
83288333

8329-
if (copy_to_user((void __user *)((unsigned long)
8330-
get_unaligned((unsigned long *)sense_ptr)),
8331-
sense, ioc->sense_len)) {
8334+
if (copy_to_user(uptr, sense, ioc->sense_len)) {
83328335
dev_err(&instance->pdev->dev, "Failed to copy out to user "
83338336
"sense data\n");
83348337
error = -EFAULT;
@@ -8371,6 +8374,38 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
83718374
return error;
83728375
}
83738376

8377+
static struct megasas_iocpacket *
8378+
megasas_compat_iocpacket_get_user(void __user *arg)
8379+
{
8380+
struct megasas_iocpacket *ioc;
8381+
struct compat_megasas_iocpacket __user *cioc = arg;
8382+
size_t size;
8383+
int err = -EFAULT;
8384+
int i;
8385+
8386+
ioc = kzalloc(sizeof(*ioc), GFP_KERNEL);
8387+
if (!ioc)
8388+
return ERR_PTR(-ENOMEM);
8389+
size = offsetof(struct megasas_iocpacket, frame) + sizeof(ioc->frame);
8390+
if (copy_from_user(ioc, arg, size))
8391+
goto out;
8392+
8393+
for (i = 0; i < MAX_IOCTL_SGE; i++) {
8394+
compat_uptr_t iov_base;
8395+
8396+
if (get_user(iov_base, &cioc->sgl[i].iov_base) ||
8397+
get_user(ioc->sgl[i].iov_len, &cioc->sgl[i].iov_len))
8398+
goto out;
8399+
8400+
ioc->sgl[i].iov_base = compat_ptr(iov_base);
8401+
}
8402+
8403+
return ioc;
8404+
out:
8405+
kfree(ioc);
8406+
return ERR_PTR(err);
8407+
}
8408+
83748409
static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
83758410
{
83768411
struct megasas_iocpacket __user *user_ioc =
@@ -8379,7 +8414,11 @@ static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
83798414
struct megasas_instance *instance;
83808415
int error;
83818416

8382-
ioc = memdup_user(user_ioc, sizeof(*ioc));
8417+
if (in_compat_syscall())
8418+
ioc = megasas_compat_iocpacket_get_user(user_ioc);
8419+
else
8420+
ioc = memdup_user(user_ioc, sizeof(struct megasas_iocpacket));
8421+
83838422
if (IS_ERR(ioc))
83848423
return PTR_ERR(ioc);
83858424

@@ -8484,78 +8523,13 @@ megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
84848523
}
84858524

84868525
#ifdef CONFIG_COMPAT
8487-
static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8488-
{
8489-
struct compat_megasas_iocpacket __user *cioc =
8490-
(struct compat_megasas_iocpacket __user *)arg;
8491-
struct megasas_iocpacket __user *ioc =
8492-
compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8493-
int i;
8494-
int error = 0;
8495-
compat_uptr_t ptr;
8496-
u32 local_sense_off;
8497-
u32 local_sense_len;
8498-
u32 user_sense_off;
8499-
8500-
if (clear_user(ioc, sizeof(*ioc)))
8501-
return -EFAULT;
8502-
8503-
if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8504-
copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8505-
copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8506-
copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8507-
copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8508-
copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8509-
return -EFAULT;
8510-
8511-
/*
8512-
* The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8513-
* sense_len is not null, so prepare the 64bit value under
8514-
* the same condition.
8515-
*/
8516-
if (get_user(local_sense_off, &ioc->sense_off) ||
8517-
get_user(local_sense_len, &ioc->sense_len) ||
8518-
get_user(user_sense_off, &cioc->sense_off))
8519-
return -EFAULT;
8520-
8521-
if (local_sense_off != user_sense_off)
8522-
return -EINVAL;
8523-
8524-
if (local_sense_len) {
8525-
void __user **sense_ioc_ptr =
8526-
(void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
8527-
compat_uptr_t *sense_cioc_ptr =
8528-
(compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
8529-
if (get_user(ptr, sense_cioc_ptr) ||
8530-
put_user(compat_ptr(ptr), sense_ioc_ptr))
8531-
return -EFAULT;
8532-
}
8533-
8534-
for (i = 0; i < MAX_IOCTL_SGE; i++) {
8535-
if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8536-
put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8537-
copy_in_user(&ioc->sgl[i].iov_len,
8538-
&cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8539-
return -EFAULT;
8540-
}
8541-
8542-
error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8543-
8544-
if (copy_in_user(&cioc->frame.hdr.cmd_status,
8545-
&ioc->frame.hdr.cmd_status, sizeof(u8))) {
8546-
printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8547-
return -EFAULT;
8548-
}
8549-
return error;
8550-
}
8551-
85528526
static long
85538527
megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
85548528
unsigned long arg)
85558529
{
85568530
switch (cmd) {
85578531
case MEGASAS_IOC_FIRMWARE32:
8558-
return megasas_mgmt_compat_ioctl_fw(file, arg);
8532+
return megasas_mgmt_ioctl_fw(file, arg);
85598533
case MEGASAS_IOC_GET_AEN:
85608534
return megasas_mgmt_ioctl_aen(file, arg);
85618535
}

0 commit comments

Comments
 (0)