scsi: aacraid: improve compat_ioctl handlers
Subsystems (4)
PATCH v2 1/3 scsi: aacraid: improve compat_ioctl handlers
The use of compat_alloc_user_space() can be easily replaced by handling compat arguments in the regular handler, and this will make it work for big-endian kernels as well, which at the moment get an invalid indirect pointer argument.
Calling aac_ioctl() instead of aac_compat_do_ioctl() means the compat and native code paths behave the same way again, which they stopped when the adapter health check was added only in the native function.
--- drivers/scsi/aacraid/commctrl.c | 22 ++++++++++-- drivers/scsi/aacraid/linit.c | 61 ++------------------------------- 2 files changed, 22 insertions(+), 61 deletions(-)
drivers/scsi/aacraid/commctrl.c +20 -2
@@ -25,6 +25,7 @@#include <linux/completion.h> #include <linux/dma-mapping.h> #include <linux/blkdev.h> +#include <linux/compat.h> #include <linux/delay.h> /* ssleep prototype */ #include <linux/kthread.h> #include <linux/uaccess.h>
@@ -226,6 +227,12 @@ static int open_getadapter_fib(struct aac_dev * dev, void __user *arg)return status; } +struct compat_fib_ioctl { +u32 fibctx; +s32 wait; +compat_uptr_t fib; +}; +/** * next_getadapter_fib - get the next fib * @dev: adapter to use
@@ -243,8 +250,19 @@ static int next_getadapter_fib(struct aac_dev * dev, void __user *arg)struct list_head * entry;unsigned long flags; -if (copy_from_user ((void *)&f, arg,sizeof (struct fib_ioctl ))) -return -EFAULT ; +if (in_compat_syscall ()) { +struct compat_fib_ioctl cf; + +if (copy_from_user (&cf, arg,sizeof (struct compat_fib_ioctl ))) +return -EFAULT ; + + f.fibctx = cf.fibctx; + f.wait = cf.wait; + f.fib =compat_ptr (cf.fib); + }else { +if (copy_from_user (&f, arg,sizeof (struct fib_ioctl ))) +return -EFAULT ; + }/* * Verify that the HANDLE passed in was a valid AdapterFibContext *
drivers/scsi/aacraid/linit.c +2 -59
79 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v2 2/3 scsi: megaraid_sas: check user-provided offsets
It sounds unwise to let user space pass an unchecked 32-bit offset into a kernel structure in an ioctl. This is an unsigned variable, so checking the upper bound for the size of the structure it points into is sufficient to avoid data corruption, but as the pointer might also be unaligned, it has to be written carefully as well.
While I stumbled over this problem by reading the code, I did not continue checking the function for further problems like it.
--- drivers/scsi/megaraid/megaraid_sas_base.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/scsi/megaraid/megaraid_sas_base.c +10 -5
@@ -8095,7 +8095,7 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,int error =0 , i;void *sense =NULL ;dma_addr_t sense_handle; -unsigned long *sense_ptr; +void *sense_ptr;u32 opcode =0 ;int ret =DCMD_SUCCESS ;
@@ -8218,6 +8218,12 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance, }if (ioc->sense_len) { +/* make sure the pointer is part of the frame */ +if (ioc->sense_off > (sizeof (union megasas_frame ) -sizeof (__le64))) { + error = -EINVAL ; + goto out; + } + sense =dma_alloc_coherent (&instance->pdev->dev, ioc->sense_len, &sense_handle,GFP_KERNEL );if (!sense) {
@@ -8225,12 +8231,11 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance, goto out; } - sense_ptr = - (unsigned long *) ((unsigned long )cmd->frame + ioc->sense_off); + sense_ptr = (void *)cmd->frame + ioc->sense_off;if (instance->consistent_mask_64bit) - *sense_ptr =cpu_to_le64 (sense_handle); +put_unaligned_le64 (sense_handle, sense_ptr);else - *sense_ptr =cpu_to_le32 (sense_handle); +put_unaligned_le32 (sense_handle, sense_ptr); }/*
Signature · 1 line
2.27.0
Re: [PATCH v2 2/3] scsi: megaraid_sas: check user-provided offsets
Quoted 32 lines · Arnd Bergmann
It sounds unwise to let user space pass an unchecked 32-bit offset into a kernel structure in an ioctl. This is an unsigned variable, so checking the upper bound for the size of the structure it points into is sufficient to avoid data corruption, but as the pointer might also be unaligned, it has to be written carefully as well.
While I stumbled over this problem by reading the code, I did not continue checking the function for further problems like it.
Cc <[email protected]> # v2.6.15+Fixes c4a3e0a529ab ("[SCSI] MegaRAID SAS RAID: new driver")Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by Arnd Bergmann <[email protected]>--- drivers/scsi/megaraid/megaraid_sas_base.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)drivers/scsi/megaraid/megaraid_sas_base.c +1
-1@@ -8095,7 +8095,7 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,int error =0 , i;void *sense =NULL ;dma_addr_t sense_handle; -unsigned long *sense_ptr; +void *sense_ptr;u32 opcode =0 ;int ret =DCMD_SUCCESS ;
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8222
Quoted 5 lines
drivers/scsi/megaraid/megaraid_sas_base.c +2
-0@@ -8218,6 +8218,12 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance, }if (ioc->sense_len) { +/* make sure the pointer is part of the frame */ +if (ioc->sense_off > (sizeof (union megasas_frame ) -sizeof (__le64))) {
Add a line break to avoid the overly long line - also the braces around the arithmetics aren't actually needed.
Sasha Levin
maintainerRe: [PATCH v2 2/3] scsi: megaraid_sas: check user-provided offsets
In reply to Arnd BergmannHi
[This is an automated email]
This commit has been processed because it contains a "Fixes:" tag fixing commit: c4a3e0a529ab ("[SCSI] MegaRAID SAS RAID: new driver").
The bot has tested the following trees: v5.8.10, v5.4.66, v4.19.146, v4.14.198, v4.9.236, v4.4.236.
v5.8.10: Build OK!
v5.4.66: Build OK!
v4.19.146: Build OK!
v4.14.198: Failed to apply! Possible dependencies:
107a60dd71b5 ("scsi: megaraid_sas: Add support for 64bit consistent DMA")
1b4bed206159 ("scsi: megaraid_sas: Create separate functions for allocating and freeing controller DMA buffers") 201a810cc188 ("scsi: megaraid_sas: Re-Define enum DCMD_RETURN_STATUS")
2ce435087902 ("scsi: megaraid_sas: Enhance internal DCMD timeout prints")
7535f27d1f14 ("scsi: megaraid_sas: Move initialization of instance parameters inside newly created function megasas_init_ctrl_params") 82add4e1b354 ("scsi: megaraid_sas: Incorrect processing of IOCTL frames for SMP/STP commands")
e5d65b4b81af ("scsi: megaraid_sas: Move controller memory allocations and DMA mask settings from probe to megasas_init_fw") e97e673ca63b ("scsi: megaraid_sas: Retry with reduced queue depth when alloc fails for higher QD")
v4.9.236: Failed to apply! Possible dependencies:
201a810cc188 ("scsi: megaraid_sas: Re-Define enum DCMD_RETURN_STATUS")
2493c67e518c ("scsi: megaraid_sas: 128 MSIX Support")
3e5eadb1a881 ("scsi: megaraid_sas: Enable or Disable Fast path based on the PCI Threshold Bandwidth") 45b8a35eed7b ("scsi: megaraid_sas: 32 bit descriptor fire cmd optimization") 45f4f2eb3da3 ("scsi: megaraid_sas: Add new pci device Ids for SAS3.5 Generic Megaraid Controllers") 82add4e1b354 ("scsi: megaraid_sas: Incorrect processing of IOCTL frames for SMP/STP commands") 8823abeddbbc ("scsi: megaraid_sas: Fix endianness issues in DCMD handling")
95c060869e68 ("scsi: megaraid_sas: latest controller OCR capability from FW before sending shutdown DCMD") d0fc91d67c59 ("scsi: megaraid_sas: Send SYNCHRONIZE_CACHE for VD to firmware") f4fc209326c7 ("scsi: megaraid_sas: change issue_dcmd to return void from int") fad119b707f8 ("scsi: megaraid_sas: switch to pci_alloc_irq_vectors")
v4.4.236: Failed to apply! Possible dependencies:
201a810cc188 ("scsi: megaraid_sas: Re-Define enum DCMD_RETURN_STATUS")
6d40afbc7d13 ("megaraid_sas: MFI IO timeout handling")
82add4e1b354 ("scsi: megaraid_sas: Incorrect processing of IOCTL frames for SMP/STP commands") 8823abeddbbc ("scsi: megaraid_sas: Fix endianness issues in DCMD handling")
8a01a41d8647 ("megaraid_sas: Make adprecovery variable atomic")
95c060869e68 ("scsi: megaraid_sas: latest controller OCR capability from FW before sending shutdown DCMD") f4fc209326c7 ("scsi: megaraid_sas: change issue_dcmd to return void from int")
NOTE: The patch will not be queued to stable trees until it is upstream.
How should we proceed with this patch?
Signature · 2 lines
Thanks Sasha
PATCH v2 3/3 scsi: megaraid_sas: simplify compat_ioctl handling
In reply to Arnd BergmannThere 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()
---
v2: address review comments from hch
--- drivers/scsi/megaraid/megaraid_sas.h | 2 - drivers/scsi/megaraid/megaraid_sas_base.c | 117 +++++++++------------- include/linux/compat.h | 10 +- 3 files changed, 50 insertions(+), 79 deletions(-)
drivers/scsi/megaraid/megaraid_sas.h +0 -2
@@ -2605,7 +2605,6 @@ struct megasas_aen {u32 class_locale_word; } __attribute__ ((packed)); -#ifdef CONFIG_COMPATstruct compat_megasas_iocpacket {u16 host_no;u16 __pad1;
@@ -2621,7 +2620,6 @@ struct compat_megasas_iocpacket { } __attribute__ ((packed));#define MEGASAS_IOC_FIRMWARE32 _IOWR('M', 1, struct compat_megasas_iocpacket) -#endif #define MEGASAS_IOC_FIRMWARE _IOWR('M', 1, struct megasas_iocpacket)#define MEGASAS_IOC_GET_AEN _IOW('M', 3, struct megasas_aen)
drivers/scsi/megaraid/megaraid_sas_base.c +45 -72
152 lines not on this page · raw message
include/linux/compat.h +5 -5
@@ -91,6 +91,11 @@ static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))#endif /* COMPAT_SYSCALL_DEFINEx */ +struct compat_iovec { +compat_uptr_t iov_base; +compat_size_t iov_len; +}; +#ifdef CONFIG_COMPAT #ifndef compat_user_stack_pointer
@@ -248,11 +253,6 @@ typedef struct compat_siginfo { } _sifields; } compat_siginfo_t; -struct compat_iovec { -compat_uptr_t iov_base; -compat_size_t iov_len; -}; -struct compat_rlimit {compat_ulong_t rlim_cur;compat_ulong_t rlim_max;
Signature · 1 line
2.27.0
Re: [PATCH v2 3/3] scsi: megaraid_sas: simplify compat_ioctl handling
Quoted 48 lines · Arnd Bergmann
There have been several attempts to fix serious problems in the compat handling in
megasas_mgmt_compat_ioctl_fw(), and it also uses thecompat_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 theioc->frame.rawarray from
invalid input
-compat_alloc_user_space()Signed-off-by Arnd Bergmann <[email protected]>---
v2: address review comments from hch--- drivers/scsi/megaraid/megaraid_sas.h | 2 - drivers/scsi/megaraid/megaraid_sas_base.c | 117 +++++++++------------- include/linux/compat.h | 10 +- 3 files changed, 50 insertions(+), 79 deletions(-)drivers/scsi/megaraid/megaraid_sas.h +0
-2@@ -2605,7 +2605,6 @@ struct megasas_aen {u32 class_locale_word; } __attribute__ ((packed)); -#ifdef CONFIG_COMPATstruct compat_megasas_iocpacket {u16 host_no;u16 __pad1;@@ -2621,7 +2620,6 @@ struct compat_megasas_iocpacket { } __attribute__ ((packed));#define MEGASAS_IOC_FIRMWARE32 _IOWR('M', 1, struct compat_megasas_iocpacket) -#endif #define MEGASAS_IOC_FIRMWARE _IOWR('M', 1, struct megasas_iocpacket)#define MEGASAS_IOC_GET_AEN _IOW('M', 3, struct megasas_aen)drivers/scsi/megaraid/megaraid_sas_base.c +0
-0@@ -8279,16 +8279,18 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,* copy out the sense
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8289
Quoted 10 lines
drivers/scsi/megaraid/megaraid_sas_base.c +4
-0*/ if (ioc->sense_len) { +void __user *uptr;/* * sense_ptr points to the location that has the user * sense buffer address */ + sense_ptr = (void *)ioc->frame.raw + ioc->sense_off; +if (in_compat_syscall ()) + uptr =compat_ptr (get_unaligned ((u32 *)sense_ptr));
should the u32 * here by a compat_uptr *? Not tat it would make a difference, just better document what we are doing.
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8357
Quoted 6 lines
drivers/scsi/megaraid/megaraid_sas_base.c +6
-0+for (i =0 ; i <MAX_IOCTL_SGE ; i++) { +compat_uptr_t iov_base; +if (get_user (iov_base, &cioc->sgl[i].iov_base) || +get_user (ioc->sgl[i].iov_len, &cioc->sgl[i].iov_len)) { + goto out; + }
I don't think we need the braces here.
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8365
Quoted 5 lines
drivers/scsi/megaraid/megaraid_sas_base.c +5
-0+return ioc; +out: +kfree (ioc); + +return ERR_PTR (err);
spurious empty line.
Quoted 15 lines
include/linux/compat.h +5
-0@@ -91,6 +91,11 @@ static inline long __do_compat_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))#endif /* COMPAT_SYSCALL_DEFINEx */ +struct compat_iovec { +compat_uptr_t iov_base; +compat_size_t iov_len; +}; +#ifdef CONFIG_COMPAT #ifndef compat_user_stack_pointer
include/linux/compat.h typedef struct compat_siginfo { · line 254
Quoted 7 lines
include/linux/compat.h +0
-4@@ -248,11 +253,6 @@ typedef struct compat_siginfo { } _sifields; } compat_siginfo_t; -struct compat_iovec { -compat_uptr_t iov_base; -compat_size_t iov_len; -};
This should probably go into a separate patch instead of being hidden in a driver patch.
But except for these nitpicks the change looks good:
Re: [PATCH v2 3/3] scsi: megaraid_sas: simplify compat_ioctl handling
Quoted 16 lines · Christoph Hellwig
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8289
Quoted 12 lines · Arnd Bergmann
drivers/scsi/megaraid/megaraid_sas_base.c +4
-0@@ -8279,16 +8279,18 @@ megasas_mgmt_fw_ioctl(struct megasas_instance *instance,* copy out the sense */ if (ioc->sense_len) { +void __user *uptr;/* * sense_ptr points to the location that has the user * sense buffer address */ + sense_ptr = (void *)ioc->frame.raw + ioc->sense_off; +if (in_compat_syscall ()) + uptr =compat_ptr (get_unaligned ((u32 *)sense_ptr));should the u32 * here by a
compat_uptr*? Not tat it would make a difference, just better document what we are doing.
Ok, changed now. The longer type name leads to a slightly ugly line break, but you are right that is is the correct thing to do.
Quoted 8 lines
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8357
Quoted 6 lines
drivers/scsi/megaraid/megaraid_sas_base.c +6
-0+for (i =0 ; i <MAX_IOCTL_SGE ; i++) { +compat_uptr_t iov_base; +if (get_user (iov_base, &cioc->sgl[i].iov_base) || +get_user (ioc->sgl[i].iov_len, &cioc->sgl[i].iov_len)) { + goto out; + }I don't think we need the braces here.
ok.
Quoted 7 lines
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8365
Quoted 5 lines
drivers/scsi/megaraid/megaraid_sas_base.c +5
-0+return ioc; +out: +kfree (ioc); + +return ERR_PTR (err);spurious empty line.
ok
Arnd
Re: [PATCH v2 3/3] scsi: megaraid_sas: simplify compat_ioctl handling
Quoted 2 lines · Arnd Bergmann
Quoted 1 line · Christoph Hellwig
On Fri, Sep 18, 2020 at 02:15:43PM +0200, Arnd Bergmann wrote:
[]
Quoted 10 lines
Quoted 8 lines
drivers/scsi/megaraid/megaraid_sas_base.c megasas_mgmt_fw_ioctl(struct megasas_instance *instance, · line 8365
Quoted 6 lines
drivers/scsi/megaraid/megaraid_sas_base.c +5
-0+return ioc; +out: +kfree (ioc); + +return ERR_PTR (err);spurious empty line.
ok
I suggest moving the blank line so it's between
the return ioc and out: label.