block, scsi: final compat_ioctl cleanup
Subsystems (29)
The following changes since commit e42617b825f8073569da76dc4510bfa019b1c35a:
Linux 5.5-rc1 (2019-12-08 14:57:55 -0800)
are available in the Git repository at:
git://git.kernel.org:/pub/scm/linux/kernel/git/arnd/playground.git tags/block-ioctl-cleanup-5.6
for you to fetch changes up to 3462d5c19dac7c062c5a2db727775116e6d2b28e:
Documentation: document ioctl interfaces better (2019-12-17 22:45:18 +0100)
----------------------------------------------------------------
block, scsi: final compat_ioctl cleanup
This series concludes the work I did for linux-5.5 on the compat_ioctl() cleanup, killing off fs/compat_ioctl.c and block/compat_ioctl.c by moving everything into drivers.
Overall this would be a reduction both in complexity and line count, but as I'm also adding documentation the overall number of lines increases in the end.
My plan was originally to keep the SCSI and block parts separate. This did not work easily because of interdependencies: I cannot do the final SCSI cleanup in a good way without first addressing the CDROM ioctls, so this is one series that I hope could be merged through either the block or the scsi git trees, or possibly both if you can pull in the same branch.
The series comes in these steps:
1. clean up the sg v3 interface as suggested by Linus. I have talked about this with Doug Gilbert as well, and he would rebase his sg v4 patches on top of "compat: scsi: sg: fix v3 compat read/write interface"
2. Actually moving handlers out of block/compat_ioctl.c and block/scsi_ioctl.c into drivers, mixed in with cleanup patches
3. Document how to do this right. I keep getting asked about this, and it helps to point to some documentation file.
The branch is based on another one that fixes a couple of bugs found during the creation of this series.
Changes since the original version [1]:
- move out the bugfixes into a branch for itself
- clean up scsi sg driver further as suggested by Christoph Hellwig
- avoid some ifdefs by moving compat_ptr() out of asm/compat.h
- split out the blkdev_compat_ptr_ioctl function; bug spotted by Ben Hutchings
- Improve formatting of documentation
[1] https://lore.kernel.org/linux-block/[email protected]/T/#m9f89df30565fc66abbded5d01f4db553b16f129f
----------------------------------------------------------------
Arnd Bergmann (22): (plus five from the first pull request)
compat: ARM64: always include asm-generic/compat.h
compat: provide compat_ptr() on all architectures
compat: scsi: sg: fix v3 compat read/write interface
compat_ioctl: block: add blkdev_compat_ptr_ioctl
compat_ioctl: ubd, aoe: use blkdev_compat_ptr_ioctl
compat_ioctl: move CDROM_SEND_PACKET handling into scsi
compat_ioctl: move CDROMREADADIO to cdrom.c
compat_ioctl: cdrom: handle CDROM_LAST_WRITTEN
compat_ioctl: block: handle cdrom compat ioctl in non-cdrom drivers
compat_ioctl: add scsi_compat_ioctl
compat_ioctl: bsg: add handler
compat_ioctl: ide: floppy: add handler
compat_ioctl: scsi: move ioctl handling into drivers
compat_ioctl: move sys_compat_ioctl() to ioctl.c
compat_ioctl: simplify the implementation
compat_ioctl: move cdrom commands into cdrom.c
compat_ioctl: scsi: handle HDIO commands from drivers
compat_ioctl: move HDIO ioctl handling into drivers/ide
compat_ioctl: block: move blkdev_compat_ioctl() into ioctl.c
compat_ioctl: block: simplify compat_blkpg_ioctl()
compat_ioctl: simplify up block/ioctl.c
Documentation: document ioctl interfaces better
Documentation/core-api/index.rst | 1 + Documentation/core-api/ioctl.rst | 248 +++++++++++++++ arch/arm64/include/asm/compat.h | 22 +- arch/mips/include/asm/compat.h | 18 -- arch/parisc/include/asm/compat.h | 17 - arch/powerpc/include/asm/compat.h | 17 - arch/powerpc/oprofile/backtrace.c | 2 +- arch/s390/include/asm/compat.h | 6 +- arch/sparc/include/asm/compat.h | 17 - arch/um/drivers/ubd_kern.c | 1 + arch/x86/include/asm/compat.h | 17 - block/Makefile | 1 - block/bsg.c | 1 + block/compat_ioctl.c | 411 ------------------------- block/ioctl.c | 319 +++++++++++++++---- block/scsi_ioctl.c | 214 ++++++++----- drivers/ata/libata-scsi.c | 9 + drivers/block/aoe/aoeblk.c | 1 + drivers/block/floppy.c | 3 + drivers/block/paride/pcd.c | 3 + drivers/block/paride/pd.c | 1 + drivers/block/paride/pf.c | 1 + drivers/block/pktcdvd.c | 26 +- drivers/block/sunvdc.c | 1 + drivers/block/virtio_blk.c | 3 + drivers/block/xen-blkfront.c | 1 + drivers/cdrom/cdrom.c | 35 ++- drivers/cdrom/gdrom.c | 3 + drivers/ide/ide-cd.c | 37 +++ drivers/ide/ide-disk.c | 1 + drivers/ide/ide-floppy.c | 4 + drivers/ide/ide-floppy.h | 2 + drivers/ide/ide-floppy_ioctl.c | 35 +++ drivers/ide/ide-gd.c | 14 + drivers/ide/ide-ioctls.c | 44 ++- drivers/ide/ide-tape.c | 11 + drivers/scsi/aic94xx/aic94xx_init.c | 3 + drivers/scsi/ch.c | 9 +- drivers/scsi/hisi_sas/hisi_sas_v1_hw.c | 3 + drivers/scsi/hisi_sas/hisi_sas_v2_hw.c | 3 + drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 3 + drivers/scsi/ipr.c | 3 + drivers/scsi/isci/init.c | 3 + drivers/scsi/mvsas/mv_init.c | 3 + drivers/scsi/pm8001/pm8001_init.c | 3 + drivers/scsi/scsi_ioctl.c | 54 +++- drivers/scsi/sd.c | 50 ++- drivers/scsi/sg.c | 170 +++++----- drivers/scsi/sr.c | 53 +++- drivers/scsi/st.c | 51 +-- fs/Makefile | 2 +- fs/compat_ioctl.c | 261 ---------------- fs/internal.h | 6 - fs/ioctl.c | 131 +++++--- include/linux/blkdev.h | 7 + include/linux/compat.h | 18 ++ include/linux/falloc.h | 2 - include/linux/fs.h | 4 - include/linux/ide.h | 2 + include/linux/libata.h | 6 + include/scsi/scsi_ioctl.h | 1 + include/scsi/sg.h | 30 ++ 62 files changed, 1257 insertions(+), 1171 deletions(-) create mode 100644 Documentation/core-api/ioctl.rst delete mode 100644 block/compat_ioctl.c delete mode 100644 fs/compat_ioctl.c
Signature · 1 line
2.20.0
PATCH v2 1/27 pktcdvd: fix regression on 64-bit architectures
The support for the compat ioctl did not actually do what it was supposed to do because of a typo, instead it broke native support for CDROM_LAST_WRITTEN and CDROM_SEND_PACKET on all architectures with CONFIG_COMPAT enabled.
----
Please apply for v5.5, I just noticed the regression while rebasing some of the patches I created on top.
--- drivers/block/pktcdvd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/pktcdvd.c +1 -1
@@ -2707,7 +2707,7 @@ static const struct block_device_operations pktcdvd_ops = { .release = pkt_close, .ioctl = pkt_ioctl, #ifdef CONFIG_COMPAT - .ioctl = pkt_compat_ioctl, + .compat_ioctl = pkt_compat_ioctl, #endif .check_events = pkt_check_events, };
Signature · 1 line
2.20.0
PATCH v2 2/27 compat_ioctl: block: handle BLKREPORTZONE/BLKRESETZONE
In reply to Arnd BergmannThese were added to blkdev_ioctl() but not blkdev_compat_ioctl, so add them now.
--- block/compat_ioctl.c | 2 ++ 1 file changed, 2 insertions(+)
block/compat_ioctl.c +2 -0
@@ -354,6 +354,8 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)* but we call blkdev_ioctl, which gets the lock for us */ case BLKRRPART : +case BLKREPORTZONE : +case BLKRESETZONE :return blkdev_ioctl (bdev, mode, cmd, (unsigned long )compat_ptr (arg));case BLKBSZSET_32 :
Signature · 1 line
2.20.0
PATCH v2 3/27 compat_ioctl: block: handle BLKGETZONESZ/BLKGETNRZONES
In reply to Arnd BergmannThese were added to blkdev_ioctl() in v4.20 but not blkdev_compat_ioctl, so add them now.
--- block/compat_ioctl.c | 2 ++ 1 file changed, 2 insertions(+)
block/compat_ioctl.c +2 -0
@@ -356,6 +356,8 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)case BLKRRPART :case BLKREPORTZONE :case BLKRESETZONE : +case BLKGETZONESZ : +case BLKGETNRZONES :return blkdev_ioctl (bdev, mode, cmd, (unsigned long )compat_ptr (arg));case BLKBSZSET_32 :
Signature · 1 line
2.20.0
PATCH v2 4/27 compat_ioctl: block: handle add zone open, close and finish ioctl
In reply to Arnd BergmannThese were added to blkdev_ioctl() in linux-5.5 but not blkdev_compat_ioctl, so add them now.
--- block/compat_ioctl.c | 3 +++ 1 file changed, 3 insertions(+)
block/compat_ioctl.c +3 -0
@@ -356,6 +356,9 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)case BLKRRPART :case BLKREPORTZONE :case BLKRESETZONE : +case BLKOPENZONE : +case BLKCLOSEZONE : +case BLKFINISHZONE :case BLKGETZONESZ :case BLKGETNRZONES : return blkdev_ioctl(bdev, mode, cmd,
Signature · 1 line
2.20.0
PATCH v2 5/27 compat_ioctl: block: handle Persistent Reservations
In reply to Arnd BergmannThese were added to blkdev_ioctl() in linux-5.5 but not blkdev_compat_ioctl, so add them now.
--- block/compat_ioctl.c | 8 ++++++++ 1 file changed, 8 insertions(+)
block/compat_ioctl.c +8 -0
@@ -408,6 +408,14 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)case BLKTRACETEARDOWN :/* compatible */ ret =blk_trace_ioctl (bdev, cmd,compat_ptr (arg));return ret; +case IOC_PR_REGISTER : +case IOC_PR_RESERVE : +case IOC_PR_RELEASE : +case IOC_PR_PREEMPT : +case IOC_PR_PREEMPT_ABORT : +case IOC_PR_CLEAR : +return blkdev_ioctl (bdev, mode, cmd, + (unsigned long )compat_ptr (arg));default :if (disk->fops->compat_ioctl) ret = disk->fops->compat_ioctl (bdev, mode, cmd, arg);
Signature · 1 line
2.20.0
PATCH v2 6/27 compat: ARM64: always include asm-generic/compat.h
In reply to Arnd BergmannIn order to use compat_* type defininitions in device drivers outside of CONFIG_COMPAT, move the inclusion of asm-generic/compat.h ahead of the #ifdef.
All other architectures already do this.
--- arch/arm64/include/asm/compat.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/include/asm/compat.h +3 -2
@@ -4,6 +4,9 @@*/ #ifndef __ASM_COMPAT_H#define __ASM_COMPAT_H + +#include <asm-generic/compat.h> +#ifdef CONFIG_COMPAT /*
@@ -13,8 +16,6 @@#include <linux/sched.h> #include <linux/sched/task_stack.h> -#include <asm-generic/compat.h> -#define COMPAT_USER_HZ 100#ifdef __AARCH64EB__#define COMPAT_UTS_MACHINE "armv8b\0\0"
Signature · 1 line
2.20.0
PATCH v2 7/27 compat: provide compat_ptr() on all architectures
In reply to Arnd BergmannIn order to avoid needless #ifdef CONFIG_COMPAT checks, move the compat_ptr() definition to linux/compat.h where it can be seen by any file regardless of the architecture.
Only s390 needs a special definition, this can use the self-#define trick we have elsewhere.
--- arch/arm64/include/asm/compat.h | 17 ----------------- arch/mips/include/asm/compat.h | 18 ------------------ arch/parisc/include/asm/compat.h | 17 ----------------- arch/powerpc/include/asm/compat.h | 17 ----------------- arch/powerpc/oprofile/backtrace.c | 2 +- arch/s390/include/asm/compat.h | 6 +----- arch/sparc/include/asm/compat.h | 17 ----------------- arch/x86/include/asm/compat.h | 17 ----------------- include/linux/compat.h | 18 ++++++++++++++++++ 9 files changed, 20 insertions(+), 109 deletions(-)
arch/arm64/include/asm/compat.h +0 -17
@@ -114,23 +114,6 @@ typedef u32 compat_sigset_word;#define COMPAT_OFF_T_MAX 0x7fffffff -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -return (void __user *)(unsigned long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -#define compat_user_stack_pointer () (user_stack_pointer(task_pt_regs(current)))#define COMPAT_MINSIGSTKSZ 2048
arch/mips/include/asm/compat.h +0 -18
@@ -100,24 +100,6 @@ typedef u32 compat_sigset_word;#define COMPAT_OFF_T_MAX 0x7fffffff -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -/* cast to a __user pointer via "unsigned long" makes sparse happy */ -return (void __user *)(unsigned long )(long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -static inline void __user *arch_compat_alloc_user_space (long len) { struct pt_regs *regs = (struct pt_regs *)
arch/parisc/include/asm/compat.h +0 -17
@@ -173,23 +173,6 @@ struct compat_shmid64_ds {#define COMPAT_ELF_NGREG 80typedef compat_ulong_t compat_elf_gregset_t [COMPAT_ELF_NGREG ]; -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -return (void __user *)(unsigned long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -static __inline__void __user *arch_compat_alloc_user_space (long len) {struct pt_regs *regs = ¤t->thread.regs;
arch/powerpc/include/asm/compat.h +0 -17
@@ -96,23 +96,6 @@ typedef u32 compat_sigset_word;#define COMPAT_OFF_T_MAX 0x7fffffff -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -return (void __user *)(unsigned long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -static inline void __user *arch_compat_alloc_user_space (long len) {struct pt_regs *regs = current->thread.regs;
arch/powerpc/oprofile/backtrace.c +1 -1
@@ -9,7 +9,7 @@#include <linux/sched.h> #include <asm/processor.h> #include <linux/uaccess.h> -#include <asm/compat.h> +#include <linux/compat.h> #include <asm/oprofile_impl.h> #define STACK_SP (STACK ) *(STACK)
arch/s390/include/asm/compat.h +1 -5
@@ -177,11 +177,7 @@ static inline void __user *compat_ptr(compat_uptr_t uptr) {return (void __user *)(unsigned long )(uptr &0x7fffffffUL ); } - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} +#define compat_ptr (uptr) compat_ptr(uptr)#ifdef CONFIG_COMPAT
arch/sparc/include/asm/compat.h +0 -17
@@ -125,23 +125,6 @@ typedef u32 compat_sigset_word;#define COMPAT_OFF_T_MAX 0x7fffffff -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -return (void __user *)(unsigned long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -#ifdef CONFIG_COMPAT static inline void __user *arch_compat_alloc_user_space (long len) {
arch/x86/include/asm/compat.h +0 -17
@@ -177,23 +177,6 @@ typedef struct user_regs_struct compat_elf_gregset_t; (!!(task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT))#endif -/* -* A pointer passed in from user mode. This should not -* be used for syscall parameters, just declare them -* as pointers because the syscall entry code will have -* appropriately converted them already. -*/ - -static inline void __user *compat_ptr (compat_uptr_t uptr) -{ -return (void __user *)(unsigned long )uptr; -} - -static inline compat_uptr_t ptr_to_compat (void __user *uptr) -{ -return (u32 )(unsigned long )uptr; -} -static inline void __user *arch_compat_alloc_user_space (long len) {compat_uptr_t sp;
include/linux/compat.h +18 -0
@@ -958,4 +958,22 @@ static inline bool in_compat_syscall(void) { return false; }#endif /* CONFIG_COMPAT */ +/* +* A pointer passed in from user mode. This should not +* be used for syscall parameters, just declare them +* as pointers because the syscall entry code will have +* appropriately converted them already. +*/ +#ifndef compat_ptr +static inline void __user *compat_ptr (compat_uptr_t uptr) +{ +return (void __user *)(unsigned long )uptr; +} +#endif + +static inline compat_uptr_t ptr_to_compat (void __user *uptr) +{ +return (u32 )(unsigned long )uptr; +} +#endif /* _LINUX_COMPAT_H */
Signature · 1 line
2.20.0
PATCH v2 8/27 compat: scsi: sg: fix v3 compat read/write interface
In reply to Arnd BergmannIn the v5.4 merge window, a cleanup patch from Al Viro conflicted with my rework of the compat handling for sg.c read(). Linus Torvalds did a correct merge but pointed out that the resulting code is still unsatisfactory.
I later noticed that the sg_new_read() function still gets the compat mode wrong, when the 'count' argument is large enough to pass a compat_sg_io_hdr object, but not a nativ sg_io_hdr.
To address both of these, move the definition of compat_sg_io_hdr into a scsi/sg.h to make it visible to sg.c and rewrite the logic for reading req_pack_id as well as the size check to a simpler version that gets the expected results.
--- block/scsi_ioctl.c | 29 +---------- drivers/scsi/sg.c | 126 +++++++++++++++++++++------------------------ include/scsi/sg.h | 30 +++++++++++ 3 files changed, 90 insertions(+), 95 deletions(-)
block/scsi_ioctl.c +1 -28
@@ -20,6 +20,7 @@#include <scsi/scsi.h> #include <scsi/scsi_ioctl.h> #include <scsi/scsi_cmnd.h> +#include <scsi/sg.h> struct blk_cmd_filter {unsigned long read_ok[BLK_SCSI_CMD_PER_LONG ];
@@ -550,34 +551,6 @@ static inline int blk_send_start_stop(struct request_queue *q,return __blk_send_generic (q, bd_disk,GPCMD_START_STOP_UNIT , data); } -#ifdef CONFIG_COMPAT -struct compat_sg_io_hdr { -compat_int_t interface_id;/* [i] 'S' for SCSI generic (required) */ -compat_int_t dxfer_direction;/* [i] data transfer direction */ -unsigned char cmd_len;/* [i] SCSI command length ( <= 16 bytes) */ -unsigned char mx_sb_len;/* [i] max length to write to sbp */ -unsigned short iovec_count;/* [i] 0 implies no scatter gather */ -compat_uint_t dxfer_len;/* [i] byte count of data transfer */ -compat_uint_t dxferp;/* [i], [*io] points to data transfer memory -or scatter gather list */ -compat_uptr_t cmdp;/* [i], [*i] points to command to perform */ -compat_uptr_t sbp;/* [i], [*o] points to sense_buffer memory */ -compat_uint_t timeout;/* [i] MAX_UINT->no timeout (unit: millisec) */ -compat_uint_t flags;/* [i] 0 -> default, see SG_FLAG... */ -compat_int_t pack_id;/* [i->o] unused internally (normally) */ -compat_uptr_t usr_ptr;/* [i->o] unused internally */ -unsigned char status;/* [o] scsi status */ -unsigned char masked_status;/* [o] shifted, masked scsi status */ -unsigned char msg_status;/* [o] messaging level data (optional) */ -unsigned char sb_len_wr;/* [o] byte count actually written to sbp */ -unsigned short host_status;/* [o] errors from host adapter */ -unsigned short driver_status;/* [o] errors from software driver */ -compat_int_t resid;/* [o] dxfer_len - actual_transferred */ -compat_uint_t duration;/* [o] time taken by cmd (unit: millisec) */ -compat_uint_t info;/* [o] auxiliary information */ -}; -#endif -int put_sg_io_hdr (const struct sg_io_hdr *hdr,void __user *argp) {#ifdef CONFIG_COMPAT
drivers/scsi/sg.c +59 -67
159 lines not on this page · raw message
include/scsi/sg.h +30 -0
@@ -68,6 +68,36 @@ typedef struct sg_io_hdrunsigned int info;/* [o] auxiliary information */ } sg_io_hdr_t;/* 64 bytes long (on i386) */ +#if defined(__KERNEL__) +#include <linux/compat.h> + +struct compat_sg_io_hdr { +compat_int_t interface_id;/* [i] 'S' for SCSI generic (required) */ +compat_int_t dxfer_direction;/* [i] data transfer direction */ +unsigned char cmd_len;/* [i] SCSI command length ( <= 16 bytes) */ +unsigned char mx_sb_len;/* [i] max length to write to sbp */ +unsigned short iovec_count;/* [i] 0 implies no scatter gather */ +compat_uint_t dxfer_len;/* [i] byte count of data transfer */ +compat_uint_t dxferp;/* [i], [*io] points to data transfer memory +or scatter gather list */ +compat_uptr_t cmdp;/* [i], [*i] points to command to perform */ +compat_uptr_t sbp;/* [i], [*o] points to sense_buffer memory */ +compat_uint_t timeout;/* [i] MAX_UINT->no timeout (unit: millisec) */ +compat_uint_t flags;/* [i] 0 -> default, see SG_FLAG... */ +compat_int_t pack_id;/* [i->o] unused internally (normally) */ +compat_uptr_t usr_ptr;/* [i->o] unused internally */ +unsigned char status;/* [o] scsi status */ +unsigned char masked_status;/* [o] shifted, masked scsi status */ +unsigned char msg_status;/* [o] messaging level data (optional) */ +unsigned char sb_len_wr;/* [o] byte count actually written to sbp */ +unsigned short host_status;/* [o] errors from host adapter */ +unsigned short driver_status;/* [o] errors from software driver */ +compat_int_t resid;/* [o] dxfer_len - actual_transferred */ +compat_uint_t duration;/* [o] time taken by cmd (unit: millisec) */ +compat_uint_t info;/* [o] auxiliary information */ +}; +#endif +#define SG_INTERFACE_ID_ORIG 'S'/* Use negative values to flag difference from original sg_header structure */
Signature · 1 line
2.20.0
PATCH v2 9/27 compat_ioctl: block: add blkdev_compat_ptr_ioctl
In reply to Arnd BergmannA lot of block drivers need only a trivial .compat_ioctl callback.
Add a helper function that can be set as the callback pointer to only convert the argument using the compat_ptr() conversion and otherwise assume all input and output data is compatible, or handled using in_compat_syscall() checks.
This mirrors the compat_ptr_ioctl() helper function used in character devices.
--- block/ioctl.c | 21 +++++++++++++++++++++ include/linux/blkdev.h | 7 +++++++ 2 files changed, 28 insertions(+)
block/ioctl.c +21 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/capability.h> +#include <linux/compat.h> #include <linux/blkdev.h> #include <linux/export.h> #include <linux/gfp.h>
@@ -285,6 +286,26 @@ int __blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,*/ EXPORT_SYMBOL_GPL (__blkdev_driver_ioctl); +#ifdef CONFIG_COMPAT +/* +* This is the equivalent of compat_ptr_ioctl(), to be used by block +* drivers that implement only commands that are completely compatible +* between 32-bit and 64-bit user space +*/ +int blkdev_compat_ptr_ioctl (struct block_device *bdev,fmode_t mode, +unsigned cmd,unsigned long arg) +{ +struct gendisk *disk = bdev->bd_disk; + +if (disk->fops->ioctl) +return disk->fops->ioctl (bdev, mode, cmd, + (unsigned long )compat_ptr (arg)); + +return -ENOIOCTLCMD ; +} +EXPORT_SYMBOL (blkdev_compat_ptr_ioctl); +#endif +static int blkdev_pr_register (struct block_device *bdev,struct pr_registration __user *arg) {
include/linux/blkdev.h +7 -0
@@ -1711,6 +1711,13 @@ struct block_device_operations {const struct pr_ops *pr_ops; }; +#ifdef CONFIG_COMPAT +extern int blkdev_compat_ptr_ioctl (struct block_device *,fmode_t , +unsigned int ,unsigned long ); +#else +#define blkdev_compat_ptr_ioctl NULL +#endif +extern int __blkdev_driver_ioctl (struct block_device *,fmode_t ,unsigned int ,unsigned long );extern int bdev_read_page (struct block_device *,sector_t ,struct page *);
Signature · 1 line
2.20.0
PATCH v2 10/27 compat_ioctl: ubd, aoe: use blkdev_compat_ptr_ioctl
In reply to Arnd BergmannThese drivers implement the HDIO_GET_IDENTITY and CDROMVOLREAD ioctl commands, which are compatible between 32-bit and 64-bit user space and traditionally handled by compat_blkdev_driver_ioctl().
As a prerequisite to removing that function, make both drivers use blkdev_compat_ptr_ioctl() as their .compat_ioctl callback.
--- arch/um/drivers/ubd_kern.c | 1 + drivers/block/aoe/aoeblk.c | 1 + 2 files changed, 2 insertions(+)
arch/um/drivers/ubd_kern.c +1 -0
@@ -113,6 +113,7 @@ static const struct block_device_operations ubd_blops = { .open = ubd_open, .release = ubd_release, .ioctl = ubd_ioctl, + .compat_ioctl = blkdev_compat_ptr_ioctl, .getgeo = ubd_getgeo, };
drivers/block/aoe/aoeblk.c +1 -0
@@ -329,6 +329,7 @@ static const struct block_device_operations aoe_bdops = { .open = aoeblk_open, .release = aoeblk_release, .ioctl = aoeblk_ioctl, + .compat_ioctl = blkdev_compat_ptr_ioctl, .getgeo = aoeblk_getgeo, .owner = THIS_MODULE, };
Signature · 1 line
2.20.0
PATCH v2 11/27 compat_ioctl: move CDROM_SEND_PACKET handling into scsi
In reply to Arnd BergmannThere is only one implementation of this ioctl, so move the handling out of the common block layer code into the place where it's actually needed.
It also gets called indirectly through pktcdvd, which needs to be aware of this change.
As I noticed, the old implementation of the compat handler failed to convert the structure on the way out, so the updated fields never got written back to user space. This is either not important, or it has never worked and should be fixed now.
--- block/compat_ioctl.c | 47 +--------- block/scsi_ioctl.c | 185 ++++++++++++++++++++++++++++------------ drivers/block/pktcdvd.c | 6 +- 3 files changed, 135 insertions(+), 103 deletions(-)
block/compat_ioctl.c +1 -46
@@ -102,18 +102,6 @@ struct compat_cdrom_read_audio {compat_caddr_t buf; }; -struct compat_cdrom_generic_command { -unsigned char cmd[CDROM_PACKET_SIZE ]; -compat_caddr_t buffer; -compat_uint_t buflen; -compat_int_t stat; -compat_caddr_t sense; -unsigned char data_direction; -compat_int_t quiet; -compat_int_t timeout; -compat_caddr_t reserved[1 ]; -}; -static int compat_cdrom_read_audio (struct block_device *bdev,fmode_t mode,unsigned int cmd,unsigned long arg) {
@@ -141,38 +129,6 @@ static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode, (unsigned long)cdread_audio); } -static int compat_cdrom_generic_command (struct block_device *bdev,fmode_t mode, -unsigned int cmd,unsigned long arg) -{ -struct cdrom_generic_command __user *cgc; -struct compat_cdrom_generic_command __user *cgc32; -u32 data; -unsigned char dir; -int itmp; - - cgc =compat_alloc_user_space (sizeof (*cgc)); - cgc32 =compat_ptr (arg); - -if (copy_in_user (&cgc->cmd, &cgc32->cmd,sizeof (cgc->cmd)) || -get_user (data, &cgc32->buffer) || -put_user (compat_ptr (data), &cgc->buffer) || -copy_in_user (&cgc->buflen, &cgc32->buflen, - (sizeof (unsigned int ) +sizeof (int ))) || -get_user (data, &cgc32->sense) || -put_user (compat_ptr (data), &cgc->sense) || -get_user (dir, &cgc32->data_direction) || -put_user (dir, &cgc->data_direction) || -get_user (itmp, &cgc32->quiet) || -put_user (itmp, &cgc->quiet) || -get_user (itmp, &cgc32->timeout) || -put_user (itmp, &cgc->timeout) || -get_user (data, &cgc32->reserved[0 ]) || -put_user (compat_ptr (data), &cgc->reserved[0 ])) -return -EFAULT ; - -return __blkdev_driver_ioctl (bdev, mode, cmd, (unsigned long )cgc); -} -struct compat_blkpg_ioctl_arg {compat_int_t op;compat_int_t flags;
@@ -224,8 +180,6 @@ static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,return compat_hdio_ioctl (bdev, mode, cmd, arg);case CDROMREADAUDIO :return compat_cdrom_read_audio (bdev, mode, cmd, arg); -case CDROM_SEND_PACKET : -return compat_cdrom_generic_command (bdev, mode, cmd, arg);/* * No handler required for the ones below, we just need to
@@ -263,6 +217,7 @@ static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,case CDROM_DISC_STATUS :case CDROM_CHANGER_NSLOTS :case CDROM_GET_CAPABILITY : +case CDROM_SEND_PACKET :/* Ignore cdrom.h about these next 5 ioctls, they absolutely do * not take a struct cdrom_read, instead they take a struct cdrom_msf * which is compatible.
block/scsi_ioctl.c +132 -53
198 lines not on this page · raw message
drivers/block/pktcdvd.c +2 -4
@@ -2671,15 +2671,13 @@ static int pkt_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned incase CDROMEJECT :case CDROMMULTISESSION :case CDROMREADTOCENTRY : +case CDROM_SEND_PACKET :/* compat mode handled in scsi_cmd_ioctl */ case SCSI_IOCTL_SEND_COMMAND :return pkt_ioctl (bdev, mode, cmd, (unsigned long )compat_ptr (arg)); -/* FIXME: no handler so far */ -case CDROM_LAST_WRITTEN : -/* handled in compat_blkdev_driver_ioctl */ -case CDROM_SEND_PACKET :default : +case CDROM_LAST_WRITTEN :return -ENOIOCTLCMD ; } }
Signature · 1 line
2.20.0
PATCH v2 12/27 compat_ioctl: move CDROMREADADIO to cdrom.c
In reply to Arnd BergmannAgain, there is only one file that needs this, so move the conversion handler into the native implementation.
--- block/compat_ioctl.c | 36 ------------------------------------ drivers/cdrom/cdrom.c | 28 +++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 39 deletions(-)
block/compat_ioctl.c +0 -36
@@ -95,40 +95,6 @@ static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,return error; } -struct compat_cdrom_read_audio { -union cdrom_addr addr; -u8 addr_format; -compat_int_t nframes; -compat_caddr_t buf; -}; - -static int compat_cdrom_read_audio (struct block_device *bdev,fmode_t mode, -unsigned int cmd,unsigned long arg) -{ -struct cdrom_read_audio __user *cdread_audio; -struct compat_cdrom_read_audio __user *cdread_audio32; -__u32 data; -void __user *datap; - - cdread_audio =compat_alloc_user_space (sizeof (*cdread_audio)); - cdread_audio32 =compat_ptr (arg); - -if (copy_in_user (&cdread_audio->addr, - &cdread_audio32->addr, - (sizeof (*cdread_audio32) - -sizeof (compat_caddr_t)))) -return -EFAULT ; - -if (get_user (data, &cdread_audio32->buf)) -return -EFAULT ; - datap =compat_ptr (data); -if (put_user (datap, &cdread_audio->buf)) -return -EFAULT ; - -return __blkdev_driver_ioctl (bdev, mode, cmd, - (unsigned long )cdread_audio); -} -struct compat_blkpg_ioctl_arg {compat_int_t op;compat_int_t flags;
@@ -178,8 +144,6 @@ static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,case HDIO_GET_ADDRESS :case HDIO_GET_BUSSTATE :return compat_hdio_ioctl (bdev, mode, cmd, arg); -case CDROMREADAUDIO : -return compat_cdrom_read_audio (bdev, mode, cmd, arg);/* * No handler required for the ones below, we just need to
drivers/cdrom/cdrom.c +25 -3
@@ -3017,9 +3017,31 @@ static noinline int mmc_ioctl_cdrom_read_audio(struct cdrom_device_info *cdi,struct cdrom_read_audio ra;int lba; -if (copy_from_user (&ra, (struct cdrom_read_audio __user *)arg, -sizeof (ra))) -return -EFAULT ; +#ifdef CONFIG_COMPAT +if (in_compat_syscall ()) { +struct compat_cdrom_read_audio { +union cdrom_addr addr; +u8 addr_format; +compat_int_t nframes; +compat_caddr_t buf; + } ra32; + +if (copy_from_user (&ra32, arg,sizeof (ra32))) +return -EFAULT ; + + ra = (struct cdrom_read_audio ) { + .addr = ra32.addr, + .addr_format = ra32.addr_format, + .nframes = ra32.nframes, + .buf =compat_ptr (ra32.buf), + }; + } else +#endif + { +if (copy_from_user (&ra, (struct cdrom_read_audio __user *)arg, +sizeof (ra))) +return -EFAULT ; + }if (ra.addr_format ==CDROM_MSF ) lba = msf_to_lba(ra.addr.msf.minute,
Signature · 1 line
2.20.0
PATCH v2 13/27 compat_ioctl: cdrom: handle CDROM_LAST_WRITTEN
In reply to Arnd BergmannThis is the only ioctl command that does not have a proper compat handler. Making the normal implementation do the right thing is actually very simply, so just do that by using an in_compat_syscall() check to avoid the special case in the pkcdvd driver.
--- drivers/block/pktcdvd.c | 24 +----------------------- drivers/cdrom/cdrom.c | 7 ++++--- 2 files changed, 5 insertions(+), 26 deletions(-)
drivers/block/pktcdvd.c +1 -23
@@ -2663,26 +2663,6 @@ static int pkt_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,return ret; } -#ifdef CONFIG_COMPAT -static int pkt_compat_ioctl (struct block_device *bdev,fmode_t mode,unsigned int cmd,unsigned long arg) -{ -switch (cmd) { -/* compatible */ -case CDROMEJECT : -case CDROMMULTISESSION : -case CDROMREADTOCENTRY : -case CDROM_SEND_PACKET :/* compat mode handled in scsi_cmd_ioctl */ -case SCSI_IOCTL_SEND_COMMAND : -return pkt_ioctl (bdev, mode, cmd, (unsigned long )compat_ptr (arg)); - -/* FIXME: no handler so far */ -default : -case CDROM_LAST_WRITTEN : -return -ENOIOCTLCMD ; - } -} -#endif -static unsigned int pkt_check_events (struct gendisk *disk,unsigned int clearing) {
@@ -2704,9 +2684,7 @@ static const struct block_device_operations pktcdvd_ops = { .open = pkt_open, .release = pkt_close, .ioctl = pkt_ioctl, -#ifdef CONFIG_COMPAT - .compat_ioctl = pkt_compat_ioctl, -#endif + .compat_ioctl = blkdev_compat_ptr_ioctl, .check_events = pkt_check_events, };
drivers/cdrom/cdrom.c +4 -3
@@ -3293,9 +3293,10 @@ static noinline int mmc_ioctl_cdrom_last_written(struct cdrom_device_info *cdi, ret =cdrom_get_last_written (cdi, &last);if (ret)return ret; -if (copy_to_user ((long __user *)arg, &last,sizeof (last))) -return -EFAULT ; -return 0 ; +if (in_compat_syscall ()) +return put_user (last, (__s32 __user *)arg); + +return put_user (last, (long __user *)arg); } static int mmc_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
Signature · 1 line
2.20.0
PATCH v2 14/27 compat_ioctl: block: handle cdrom compat ioctl in non-cdrom drivers
In reply to Arnd BergmannVarious block drivers implement the CDROMMULTISESSION, CDROM_GET_CAPABILITY, and CDROMEJECT ioctl commands, relying on the block layer to handle compat_ioctl mode for them.
Move this into the drivers directly as a preparation for simplifying the block layer later.
When only integer arguments or no arguments are passed, the same handler can be used for .ioctl and .compat_ioctl, and when only pointer arguments are passed, the newly added blkdev_compat_ptr_ioctl can be used.
--- drivers/block/floppy.c | 3 +++ drivers/block/paride/pd.c | 1 + drivers/block/paride/pf.c | 1 + drivers/block/sunvdc.c | 1 + drivers/block/xen-blkfront.c | 1 + 5 files changed, 7 insertions(+)
drivers/block/floppy.c +3 -0
@@ -3879,6 +3879,9 @@ static int fd_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int {int drive = (long )bdev->bd_disk->private_data;switch (cmd) { +case CDROMEJECT :/* CD-ROM eject */ +case 0x6470 :/* SunOS floppy eject */ +case FDMSGON :case FDMSGOFF :case FDSETEMSGTRESH :
drivers/block/paride/pd.c +1 -0
@@ -874,6 +874,7 @@ static const struct block_device_operations pd_fops = { .open = pd_open, .release = pd_release, .ioctl = pd_ioctl, + .compat_ioctl = pd_ioctl, .getgeo = pd_getgeo, .check_events = pd_check_events, .revalidate_disk= pd_revalidate
drivers/block/paride/pf.c +1 -0
@@ -276,6 +276,7 @@ static const struct block_device_operations pf_fops = { .open = pf_open, .release = pf_release, .ioctl = pf_ioctl, + .compat_ioctl = pf_ioctl, .getgeo = pf_getgeo, .check_events = pf_check_events, };
drivers/block/sunvdc.c +1 -0
@@ -171,6 +171,7 @@ static const struct block_device_operations vdc_fops = { .owner = THIS_MODULE, .getgeo = vdc_getgeo, .ioctl = vdc_ioctl, + .compat_ioctl = blkdev_compat_ptr_ioctl, };static void vdc_blk_queue_start (struct vdc_port *port)
drivers/block/xen-blkfront.c +1 -0
@@ -2632,6 +2632,7 @@ static const struct block_device_operations xlvbd_block_fops = .release = blkif_release, .getgeo = blkif_getgeo, .ioctl = blkif_ioctl, + .compat_ioctl = blkdev_compat_ptr_ioctl, };
Signature · 1 line
2.20.0
PATCH v2 15/27 compat_ioctl: add scsi_compat_ioctl
In reply to Arnd BergmannIn order to move the compat handling for SCSI ioctl commands out of fs/compat_ioctl.c into the individual drivers, we need a helper function first to match the native ioctl handler called by sd, sr, st, etc.
--- drivers/scsi/scsi_ioctl.c | 54 +++++++++++++++++++++++++++++---------- include/scsi/scsi_ioctl.h | 1 + 2 files changed, 41 insertions(+), 14 deletions(-)
drivers/scsi/scsi_ioctl.c +40 -14
@@ -189,17 +189,7 @@ static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg) } -/** -* scsi_ioctl - Dispatch ioctl to scsi device -* @sdev: scsi device receiving ioctl -* @cmd: which ioctl is it -* @arg: data associated with ioctl -* -* Description: The scsi_ioctl() function differs from most ioctls in that it -* does not take a major/minor number as the dev field. Rather, it takes -* a pointer to a &struct scsi_device. -*/ -int scsi_ioctl (struct scsi_device *sdev,int cmd,void __user *arg) +static int scsi_ioctl_common (struct scsi_device *sdev,int cmd,void __user *arg) {char scsi_cmd[MAX_COMMAND_SIZE ];struct scsi_sense_hdr sense_hdr;
@@ -266,14 +256,50 @@ int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)return scsi_ioctl_get_pci (sdev, arg);case SG_SCSI_RESET :return scsi_ioctl_reset (sdev, arg); -default : -if (sdev->host->hostt->ioctl) -return sdev->host->hostt->ioctl (sdev, cmd, arg); } +return -ENOIOCTLCMD ; +} + +/** +* scsi_ioctl - Dispatch ioctl to scsi device +* @sdev: scsi device receiving ioctl +* @cmd: which ioctl is it +* @arg: data associated with ioctl +* +* Description: The scsi_ioctl() function differs from most ioctls in that it +* does not take a major/minor number as the dev field. Rather, it takes +* a pointer to a &struct scsi_device. +*/ +int scsi_ioctl (struct scsi_device *sdev,int cmd,void __user *arg) +{ +int ret =scsi_ioctl_common (sdev, cmd, arg); + +if (ret != -ENOIOCTLCMD ) +return ret; + +if (sdev->host->hostt->ioctl) +return sdev->host->hostt->ioctl (sdev, cmd, arg); +return -EINVAL ; }EXPORT_SYMBOL (scsi_ioctl); +#ifdef CONFIG_COMPAT +int scsi_compat_ioctl (struct scsi_device *sdev,int cmd,void __user *arg) +{ +int ret =scsi_ioctl_common (sdev, cmd, arg); + +if (ret != -ENOIOCTLCMD ) +return ret; + +if (sdev->host->hostt->compat_ioctl) +return sdev->host->hostt->compat_ioctl (sdev, cmd, arg); + +return ret; +} +EXPORT_SYMBOL (scsi_compat_ioctl); +#endif +/* * We can process a reset even when a device isn't fully operable. */
include/scsi/scsi_ioctl.h +1 -0
@@ -44,6 +44,7 @@ typedef struct scsi_fctargaddress {int scsi_ioctl_block_when_processing_errors (struct scsi_device *sdev,int cmd,bool ndelay);extern int scsi_ioctl (struct scsi_device *,int ,void __user *); +extern int scsi_compat_ioctl (struct scsi_device *sdev,int cmd,void __user *arg);#endif /* __KERNEL__ */ #endif /* _SCSI_IOCTL_H */
Signature · 1 line
2.20.0
PATCH v2 16/27 compat_ioctl: bsg: add handler
In reply to Arnd Bergmannbsg_ioctl() calls into scsi_cmd_ioctl() for a couple of generic commands and relies on fs/compat_ioctl.c to handle it correctly in compat mode.
Adding a private compat_ioctl() handler avoids that round-trip and lets us get rid of the generic emulation once this is done.
Note that bsg implements an SG_IO command that is different from the other drivers and does not need emulation.
--- block/bsg.c | 1 + 1 file changed, 1 insertion(+)
block/bsg.c +1 -0
@@ -382,6 +382,7 @@ static const struct file_operations bsg_fops = { .open = bsg_open, .release = bsg_release, .unlocked_ioctl = bsg_ioctl, + .compat_ioctl = compat_ptr_ioctl, .owner = THIS_MODULE, .llseek = default_llseek, };
Signature · 1 line
2.20.0
PATCH v2 17/27 compat_ioctl: ide: floppy: add handler
In reply to Arnd BergmannRather than relying on fs/compat_ioctl.c, this adds support for a compat_ioctl() callback in the ide-floppy driver directly, which lets it translate the scsi commands.
--- drivers/ide/ide-floppy.c | 4 ++++ drivers/ide/ide-floppy.h | 2 ++ drivers/ide/ide-floppy_ioctl.c | 36 ++++++++++++++++++++++++++++++++++ drivers/ide/ide-gd.c | 14 +++++++++++++ include/linux/ide.h | 2 ++ 5 files changed, 58 insertions(+)
drivers/ide/ide-floppy.c +4 -0
@@ -19,6 +19,7 @@#include <linux/types.h> #include <linux/string.h> #include <linux/kernel.h> +#include <linux/compat.h> #include <linux/delay.h> #include <linux/timer.h> #include <linux/mm.h>
@@ -546,4 +547,7 @@ const struct ide_disk_ops ide_atapi_disk_ops = { .set_doorlock = ide_set_media_lock, .do_request = ide_floppy_do_request, .ioctl = ide_floppy_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = ide_floppy_compat_ioctl, +#endif };
drivers/ide/ide-floppy.h +2 -0
@@ -26,6 +26,8 @@ void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *);/* ide-floppy_ioctl.c */ int ide_floppy_ioctl (ide_drive_t *,struct block_device *,fmode_t ,unsigned int ,unsigned long ); +int ide_floppy_compat_ioctl (ide_drive_t *,struct block_device *,fmode_t , +unsigned int ,unsigned long );#ifdef CONFIG_IDE_PROC_FS /* ide-floppy_proc.c */
drivers/ide/ide-floppy_ioctl.c +36 -0
@@ -5,6 +5,7 @@#include <linux/kernel.h> #include <linux/ide.h> +#include <linux/compat.h> #include <linux/cdrom.h> #include <linux/mutex.h>
@@ -302,3 +303,38 @@ int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,mutex_unlock (&ide_floppy_ioctl_mutex);return err; } + +#ifdef CONFIG_COMPAT +int ide_floppy_compat_ioctl (ide_drive_t *drive,struct block_device *bdev, +fmode_t mode,unsigned int cmd,unsigned long arg) +{ +struct ide_atapi_pc pc; +void __user *argp =compat_ptr (arg); +int err; + +mutex_lock (&ide_floppy_ioctl_mutex); +if (cmd ==CDROMEJECT || cmd ==CDROM_LOCKDOOR ) { + err =ide_floppy_lockdoor (drive, &pc, arg, cmd); + goto out; + } + + err =ide_floppy_format_ioctl (drive, &pc, mode, cmd, argp); +if (err != -ENOTTY ) + goto out; + +/* +* skip SCSI_IOCTL_SEND_COMMAND (deprecated) +* and CDROM_SEND_PACKET (legacy) ioctls +*/ +if (cmd !=CDROM_SEND_PACKET && cmd !=SCSI_IOCTL_SEND_COMMAND ) + err =scsi_cmd_blk_ioctl (bdev, mode, cmd, argp); + +/* +* there is no generic_ide_compat_ioctl(), that is handled +* through compat_blkdev_ioctl(). +*/ +out: +mutex_unlock (&ide_floppy_ioctl_mutex); +return err; +} +#endif
drivers/ide/ide-gd.c +14 -0
@@ -341,11 +341,25 @@ static int ide_gd_ioctl(struct block_device *bdev, fmode_t mode,return drive->disk_ops->ioctl (drive, bdev, mode, cmd, arg); } +#ifdef CONFIG_COMPAT +static int ide_gd_compat_ioctl (struct block_device *bdev,fmode_t mode, +unsigned int cmd,unsigned long arg) +{ +struct ide_disk_obj *idkp =ide_drv_g (bdev->bd_disk, ide_disk_obj); +ide_drive_t *drive = idkp->drive; + +return drive->disk_ops->compat_ioctl (drive, bdev, mode, cmd, arg); +} +#endif +static const struct block_device_operations ide_gd_ops = { .owner =THIS_MODULE , .open = ide_gd_unlocked_open, .release = ide_gd_release, .ioctl = ide_gd_ioctl, +#ifdef CONFIG_COMPAT + .ioctl = ide_gd_compat_ioctl, +#endif .getgeo = ide_gd_getgeo, .check_events = ide_gd_check_events, .unlock_native_capacity = ide_gd_unlock_native_capacity,
include/linux/ide.h +2 -0
@@ -413,6 +413,8 @@ struct ide_disk_ops { sector_t);int (*ioctl)(struct ide_drive_s *,struct block_device *,fmode_t ,unsigned int ,unsigned long ); +int (*compat_ioctl)(struct ide_drive_s *,struct block_device *, +fmode_t ,unsigned int ,unsigned long ); };/* ATAPI device flags */
Signature · 1 line
2.20.0
Re: [PATCH v2 17/27] compat_ioctl: ide: floppy: add handler
Quoted 3 lines · Arnd Bergmann
Rather than relying on
fs/compat_ioctl.c, this adds support for acompat_ioctl()callback in the ide-floppy driver directly, which lets it translate the scsi commands.
[...]
After this, and before "compat_ioctl: move HDIO ioctl handling into drivers/ide", compat ioctls on an IDE hard drive will result in a null pointer dereference in ide_gd_compat_ioctl(). Not sure how much that really matters though.
Ben.
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomRe: [PATCH v2 17/27] compat_ioctl: ide: floppy: add handler
Quoted 11 lines · Ben Hutchings
Quoted 3 lines · Arnd Bergmann
Rather than relying on
fs/compat_ioctl.c, this adds support for acompat_ioctl()callback in the ide-floppy driver directly, which lets it translate the scsi commands.[...]
After this, and before "
compat_ioctl: move HDIO ioctl handling intodrivers/ide", compat ioctls on an IDE hard drive will result in a null pointer dereference inide_gd_compat_ioctl(). Not sure how much that really matters though.
I'm sure it makes no difference in the end, but you are of course right that this is a bug. I've folded in a check now, and leaving that in place even after it is no longer needed:
drivers/ide/ide-gd.c +0 -0
@@ -348,6 +348,9 @@ static int ide_gd_compat_ioctl(struct block_device
*bdev, fmode_t mode,
struct ide_disk_obj *idkp = ide_drv_g(bdev->bd_disk, ide_disk_obj);
ide_drive_t *drive = idkp->drive;+ if (!drive->disk_ops->compat_ioctl)
+ return -ENOIOCTLCMD;
+
return drive->disk_ops->compat_ioctl(drive, bdev, mode, cmd, arg);
}
#endif
I pushed out an updated signed tag with this change.
Thanks for the continued careful review!
Arnd
PATCH v2 18/27 compat_ioctl: scsi: move ioctl handling into drivers
In reply to Arnd BergmannEach driver calling scsi_ioctl() gets an equivalent compat_ioctl() handler that implements the same commands by calling scsi_compat_ioctl().
The scsi_cmd_ioctl() and scsi_cmd_blk_ioctl() functions are compatible at this point, so any driver that calls those can do so for both native and compat mode, with the argument passed through compat_ptr().
With this, we can remove the entries from fs/compat_ioctl.c. The new code is larger, but should be easier to maintain and keep updated with newly added commands.
--- drivers/block/virtio_blk.c | 3 + drivers/scsi/ch.c | 9 ++- drivers/scsi/sd.c | 50 ++++++-------- drivers/scsi/sg.c | 44 ++++++++----- drivers/scsi/sr.c | 57 ++++++++++++++-- drivers/scsi/st.c | 51 ++++++++------ fs/compat_ioctl.c | 132 +------------------------------------ 7 files changed, 142 insertions(+), 204 deletions(-)
drivers/block/virtio_blk.c +3 -0
@@ -405,6 +405,9 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)static const struct block_device_operations virtblk_fops = { .ioctl = virtblk_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = blkdev_compat_ptr_ioctl, +#endif .owner =THIS_MODULE , .getgeo = virtblk_getgeo, };
drivers/scsi/ch.c +6 -3
@@ -872,6 +872,10 @@ static long ch_ioctl_compat(struct file * file, unsigned int cmd, unsigned long arg) {scsi_changer *ch = file->private_data; +int retval =scsi_ioctl_block_when_processing_errors (ch->device, cmd, + file->f_flags &O_NDELAY ); +if (retval) +return retval;switch (cmd) {case CHIOGPARAMS :
@@ -883,7 +887,7 @@ static long ch_ioctl_compat(struct file * file,case CHIOINITELEM :case CHIOSVOLTAG :/* compatible */ -return ch_ioctl (file, cmd, arg); +return ch_ioctl (file, cmd, (unsigned long )compat_ptr (arg));case CHIOGSTATUS32 : {struct changer_element_status32 ces32;
@@ -898,8 +902,7 @@ static long ch_ioctl_compat(struct file * file,return ch_gstatus (ch, ces32.ces_type, data); }default : -// return scsi_ioctl_compat(ch->device, cmd, (void*)arg); -return -ENOIOCTLCMD ; +return scsi_compat_ioctl (ch->device, cmd,compat_ptr (arg)); } }
drivers/scsi/sd.c +19 -31
80 lines not on this page · raw message
drivers/scsi/sg.c +27 -17
75 lines not on this page · raw message
drivers/scsi/sr.c +53 -4
77 lines not on this page · raw message
drivers/scsi/st.c +32 -19
97 lines not on this page · raw message
fs/compat_ioctl.c +2 -130
153 lines not on this page · raw message
Signature · 1 line
2.20.0
Re: [PATCH v2 18/27] compat_ioctl: scsi: move ioctl handling into drivers
On Tue, 2019-12-17 at 23:16 +0100, Arnd Bergmann wrote:
[...]
Quoted 2 lines
drivers/scsi/sr.c +0
-0
[...]
Quoted 30 lines
drivers/scsi/sr.c +26
-0@@ -598,6 +599,55 @@ static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,return ret; } +#ifdef CONFIG_COMPAT +static int sr_block_compat_ioctl (struct block_device *bdev,fmode_t mode,unsigned cmd, +unsigned long arg) +{ +struct scsi_cd *cd =scsi_cd (bdev->bd_disk); +struct scsi_device *sdev = cd->device; +void __user *argp =compat_ptr (arg); +int ret; + +mutex_lock (&sr_mutex); + + ret =scsi_ioctl_block_when_processing_errors (sdev, cmd, + (mode &FMODE_NDELAY ) !=0 ); +if (ret) + goto out; + +scsi_autopm_get_device (sdev); + +/* +* Send SCSI addressing ioctls directly to mid level, send other +* ioctls to cdrom/block level. +*/ +switch (cmd) { +case SCSI_IOCTL_GET_IDLUN : +case SCSI_IOCTL_GET_BUS_NUMBER : + ret =scsi_compat_ioctl (sdev, cmd, argp);
drivers/scsi/sr.c static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, · line 637
Quoted 10 lines
drivers/scsi/sr.c +10
-0+ goto put; + } + +/* +* CDROM ioctls are handled in the block layer, but +* do the scsi blk ioctls here. +*/ + ret =scsi_cmd_blk_ioctl (bdev, mode, cmd, argp); +if (ret != -ENOTTY ) +return ret;
This needs to be be "goto put;"
Quoted 11 lines
drivers/scsi/sr.c +11
-0+ ret =scsi_compat_ioctl (sdev, cmd, argp); + +put: +scsi_autopm_put_device (sdev); + +out: +mutex_unlock (&sr_mutex); + return ret; + +} +#endif
[...]
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomRe: [PATCH v2 18/27] compat_ioctl: scsi: move ioctl handling into drivers
Quoted 11 lines · Ben Hutchings
drivers/scsi/sr.c static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, · line 637
Quoted 8 lines · Arnd Bergmann
drivers/scsi/sr.c +8
-0+ +/* +* CDROM ioctls are handled in the block layer, but +* do the scsi blk ioctls here. +*/ + ret =scsi_cmd_blk_ioctl (bdev, mode, cmd, argp); +if (ret != -ENOTTY ) +return ret;
Quoted 1 line
This needs to be be "goto put;"
Fixed now, thanks!
Arnd
PATCH v2 19/27 compat_ioctl: move sys_compat_ioctl() to ioctl.c
In reply to Arnd BergmannThe rest of the fs/compat_ioctl.c file is no longer useful now, so move the actual syscall as planned.
--- fs/Makefile | 2 +- fs/compat_ioctl.c | 133 ---------------------------------------------- fs/ioctl.c | 90 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 134 deletions(-) delete mode 100644 fs/compat_ioctl.c
fs/Makefile +1 -1
@@ -37,7 +37,7 @@ obj-$(CONFIG_FS_DAX) += dax.o obj-$(CONFIG_FS_ENCRYPTION) += crypto/ obj-$(CONFIG_FS_VERITY) += verity/ obj-$(CONFIG_FILE_LOCKING) += locks.o -obj-$(CONFIG_COMPAT) += compat.o compat_ioctl.o +obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_BINFMT_AOUT) += binfmt_aout.o obj-$(CONFIG_BINFMT_EM86) += binfmt_em86.o obj-$(CONFIG_BINFMT_MISC) += binfmt_misc.o
fs/compat_ioctl.c deleted +0 -133
133 lines not on this page · raw message
fs/ioctl.c +90 -0
94 lines not on this page · raw message
Signature · 1 line
2.20.0
PATCH v2 20/27 compat_ioctl: simplify the implementation
In reply to Arnd BergmannNow that both native and compat ioctl syscalls are in the same file, a couple of simplifications can be made, bringing the implementation closer together:
- do_vfs_ioctl(), ioctl_preallocate(), and compat_ioctl_preallocate() can become static, allowing the compiler to optimize better
- slightly update the coding style for consistency between the functions.
- rather than listing each command in two switch statements for the compat case, just call a single function that has all the common commands.
As a side-effect, FS_IOC_RESVSP/FS_IOC_RESVSP64 are now available to x86 compat tasks, along with FS_IOC_RESVSP_32/FS_IOC_RESVSP64_32. This is harmless for i386 emulation, and can be considered a bugfix for x32 emulation, which never supported these in the past.
--- fs/internal.h | 6 -- fs/ioctl.c | 157 +++++++++++++++++------------------------ include/linux/falloc.h | 2 - include/linux/fs.h | 4 -- 4 files changed, 64 insertions(+), 105 deletions(-)
fs/internal.h +0 -6
@@ -180,11 +180,5 @@ extern void mnt_pin_kill(struct mount *m);*/ extern const struct dentry_operations ns_dentry_operations; -/* -* fs/ioctl.c -*/ -extern int do_vfs_ioctl (struct file *file,unsigned int fd,unsigned int cmd, -unsigned long arg); -/* direct-io.c: */ int sb_init_dio_done_wq (struct super_block *sb);
fs/ioctl.c +64 -93
272 lines not on this page · raw message
include/linux/falloc.h +0 -2
@@ -51,8 +51,6 @@ struct space_resv_32 {#define FS_IOC_UNRESVSP64_32 _IOW ('X', 43, struct space_resv_32)#define FS_IOC_ZERO_RANGE_32 _IOW ('X', 57, struct space_resv_32) -int compat_ioctl_preallocate (struct file *,int ,struct space_resv_32 __user *); -#endif #endif /* _FALLOC_H_ */
include/linux/fs.h +0 -4
@@ -2552,10 +2552,6 @@ extern int finish_open(struct file *file, struct dentry *dentry, int (*open)(struct inode *, struct file *));extern int finish_no_open (struct file *file,struct dentry *dentry); -/* fs/ioctl.c */ - -extern int ioctl_preallocate (struct file *filp,int mode,void __user *argp); -/* fs/dcache.c */ extern void __init vfs_caches_init_early (void );extern void __init vfs_caches_init (void );
Signature · 1 line
2.20.0
Re: [PATCH v2 20/27] compat_ioctl: simplify the implementation
This patch cause a problem on 64bit kernel 32bit userspace.
My 32bit Androidx86 userspace run on 64bit mainline kernel, this patch caused some app not detect root permission.
Re: [PATCH v2 20/27] compat_ioctl: simplify the implementation
Quoted 3 lines · On Fri, Feb 7, 2020 at 8:22 AM youling257 <[email protected]> wrote:
This patch cause a problem on 64bit kernel 32bit userspace.
My 32bit Androidx86 userspace run on 64bit mainline kernel, this patch caused some app not detect root permission.
Thanks for you work in bisecting the issue to my patch, sorry to have caused you trouble. After Christian Zigotzky
also reported a problem in this file, I have been able to find a specific bug and just submitted a patch for it.
Please have a look if that fix addresses your problem, as it's possible that there was more than one bug introduced by the original patch.
Arnd
Re: [PATCH v2 20/27] compat_ioctl: simplify the implementation
Test [PATCH] compat_ioctl: fix FIONREAD on devices, fixed my problem.
2020-02-08 1:06 GMT+08:00, Arnd Bergmann <[email protected]>:
Quoted 17 lines · On Fri, Feb 7, 2020 at 8:22 AM youling257 <[email protected]> wrote:
Quoted 4 lines
This patch cause a problem on 64bit kernel 32bit userspace. My 32bit Androidx86 userspace run on 64bit mainline kernel, this patch caused some app not detect root permission.
Thanks for you work in bisecting the issue to my patch, sorry to have caused you trouble. After Christian Zigotzky
also reported a problem in this file, I have been able to find a specific bug and just submitted a patch for it.Please have a look if that fix addresses your problem, as it's possible that there was more than one bug introduced by the original patch.
Arnd
PATCH v2 21/27 compat_ioctl: move cdrom commands into cdrom.c
In reply to Arnd BergmannThere is no need for the special cases for the cdrom ioctls any more now, so make sure that each cdrom driver has a .compat_ioctl() callback and calls cdrom_compat_ioctl() directly there.
--- block/compat_ioctl.c | 45 -------------------------------------- drivers/block/paride/pcd.c | 3 +++ drivers/cdrom/gdrom.c | 3 +++ drivers/ide/ide-cd.c | 36 ++++++++++++++++++++++++++++++ drivers/scsi/sr.c | 10 +++------ 5 files changed, 45 insertions(+), 52 deletions(-)
block/compat_ioctl.c +0 -45
@@ -159,42 +159,6 @@ static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,case HDIO_DRIVE_CMD :/* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */ case 0x330 : -/* CDROM stuff */ -case CDROMPAUSE : -case CDROMRESUME : -case CDROMPLAYMSF : -case CDROMPLAYTRKIND : -case CDROMREADTOCHDR : -case CDROMREADTOCENTRY : -case CDROMSTOP : -case CDROMSTART : -case CDROMEJECT : -case CDROMVOLCTRL : -case CDROMSUBCHNL : -case CDROMMULTISESSION : -case CDROM_GET_MCN : -case CDROMRESET : -case CDROMVOLREAD : -case CDROMSEEK : -case CDROMPLAYBLK : -case CDROMCLOSETRAY : -case CDROM_DISC_STATUS : -case CDROM_CHANGER_NSLOTS : -case CDROM_GET_CAPABILITY : -case CDROM_SEND_PACKET : -/* Ignore cdrom.h about these next 5 ioctls, they absolutely do -* not take a struct cdrom_read, instead they take a struct cdrom_msf -* which is compatible. -*/ -case CDROMREADMODE2 : -case CDROMREADMODE1 : -case CDROMREADRAW : -case CDROMREADCOOKED : -case CDROMREADALL : -/* DVD ioctls */ -case DVD_READ_STRUCT : -case DVD_WRITE_STRUCT : -case DVD_AUTH : arg = (unsigned long )compat_ptr (arg);/* These intepret arg as an unsigned long, not as a pointer, * so we must not do compat_ptr() conversion. */
@@ -210,15 +174,6 @@ static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,case HDIO_SET_ACOUSTIC :case HDIO_SET_BUSSTATE :case HDIO_SET_ADDRESS : -case CDROMEJECT_SW : -case CDROM_SET_OPTIONS : -case CDROM_CLEAR_OPTIONS : -case CDROM_SELECT_SPEED : -case CDROM_SELECT_DISC : -case CDROM_MEDIA_CHANGED : -case CDROM_DRIVE_STATUS : -case CDROM_LOCKDOOR : -case CDROM_DEBUG :break ;default :/* unknown ioctl number */
drivers/block/paride/pcd.c +3 -0
@@ -275,6 +275,9 @@ static const struct block_device_operations pcd_bdops = { .open = pcd_block_open, .release = pcd_block_release, .ioctl = pcd_block_ioctl, +#ifdef CONFIG_COMPAT + .ioctl = blkdev_compat_ptr_ioctl, +#endif .check_events = pcd_block_check_events, };
drivers/cdrom/gdrom.c +3 -0
@@ -518,6 +518,9 @@ static const struct block_device_operations gdrom_bdops = { .release = gdrom_bdops_release, .check_events = gdrom_bdops_check_events, .ioctl = gdrom_bdops_ioctl, +#ifdef CONFIG_COMPAT + .ioctl = blkdev_compat_ptr_ioctl, +#endif };static irqreturn_t gdrom_command_interrupt (int irq,void *dev_id)
drivers/ide/ide-cd.c +36 -0
@@ -25,6 +25,7 @@#define IDECD_VERSION "5.00" +#include <linux/compat.h> #include <linux/module.h> #include <linux/types.h> #include <linux/kernel.h>
@@ -1710,6 +1711,38 @@ static int idecd_ioctl(struct block_device *bdev, fmode_t mode,return ret; } +#ifdef CONFIG_COMPAT +static int idecd_locked_compat_ioctl (struct block_device *bdev,fmode_t mode, +unsigned int cmd,unsigned long arg) +{ +struct cdrom_info *info =ide_drv_g (bdev->bd_disk, cdrom_info); +int err; + +switch (cmd) { +case CDROMSETSPINDOWN : +return idecd_set_spindown (&info->devinfo, arg); +case CDROMGETSPINDOWN : +return idecd_get_spindown (&info->devinfo, arg); +default : +break ; + } + +return cdrom_ioctl (&info->devinfo, bdev, mode, cmd, + (unsigned long )compat_ptr (arg)); +} + +static int idecd_compat_ioctl (struct block_device *bdev,fmode_t mode, +unsigned int cmd,unsigned long arg) +{ +int ret; + +mutex_lock (&ide_cd_mutex); + ret =idecd_locked_compat_ioctl (bdev, mode, cmd, arg); +mutex_unlock (&ide_cd_mutex); + +return ret; +} +#endif static unsigned int idecd_check_events (struct gendisk *disk,unsigned int clearing)
@@ -1732,6 +1765,9 @@ static const struct block_device_operations idecd_ops = { .open = idecd_open, .release = idecd_release, .ioctl = idecd_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = idecd_compat_ioctl, +#endif .check_events = idecd_check_events, .revalidate_disk = idecd_revalidate_disk };
drivers/scsi/sr.c +3 -7
@@ -628,13 +628,9 @@ static int sr_block_compat_ioctl(struct block_device *bdev, fmode_t mode, unsign goto put; } -/* -* CDROM ioctls are handled in the block layer, but -* do the scsi blk ioctls here. -*/ - ret =scsi_cmd_blk_ioctl (bdev, mode, cmd, argp); -if (ret != -ENOTTY ) -return ret; + ret =cdrom_ioctl (&cd->cdi, bdev, mode, cmd, (unsigned long )argp); +if (ret != -ENOSYS ) + goto put; ret =scsi_compat_ioctl (sdev, cmd, argp);
Signature · 1 line
2.20.0
Re: [PATCH v2 21/27] compat_ioctl: move cdrom commands into cdrom.c
On Tue, 2019-12-17 at 23:17 +0100, Arnd Bergmann wrote:
[...]
Quoted 6 lines
drivers/ide/ide-cd.c +2
-0@@ -1710,6 +1711,38 @@ static int idecd_ioctl(struct block_device *bdev, fmode_t mode,return ret; } +#ifdef CONFIG_COMPAT +static int idecd_locked_compat_ioctl(struct block_device *bdev, fmode_t mode,
drivers/ide/ide-cd.c static int idecd_ioctl(struct block_device *bdev, fmode_t mode, · line 1725
Quoted 10 lines
drivers/ide/ide-cd.c +10
-0+ unsigned int cmd, unsigned long arg) +{ +struct cdrom_info *info =ide_drv_g (bdev->bd_disk, cdrom_info); +int err; + +switch (cmd) { +case CDROMSETSPINDOWN : +return idecd_set_spindown (&info->devinfo, arg); +case CDROMGETSPINDOWN : +return idecd_get_spindown (&info->devinfo, arg);
compat_ptr() should also be applied to the argument for these two commands, though I'm fairly sure IDE drivers have never been useful on s390 so it doesn't matter in practice.
Ben.
Quoted 7 lines
drivers/ide/ide-cd.c +7
-0+default : +break ; + } + +return cdrom_ioctl (&info->devinfo, bdev, mode, cmd, + (unsigned long )compat_ptr (arg)); +}
[...]
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomRe: [PATCH v2 21/27] compat_ioctl: move cdrom commands into cdrom.c
Quoted 7 lines · Ben Hutchings
On Tue, 2019-12-17 at 23:17 +0100, Arnd Bergmann wrote:
[...]Quoted 4 lines
(quoted diff) +0
-0@@ -1710,6 +1711,38 @@ static int idecd_ioctl(struct block_device *bdev, fmode_t mode, return ret; }
Quoted 16 lines
Review static int idecd_ioctl(struct block_device *bdev, fmode_t mode, · line 1725
Quoted 12 lines
(quoted diff) +12
-0+#ifdef CONFIG_COMPAT +static int idecd_locked_compat_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info); + int err; + + switch (cmd) { + case CDROMSETSPINDOWN: + return idecd_set_spindown(&info->devinfo, arg); + case CDROMGETSPINDOWN: + return idecd_get_spindown(&info->devinfo, arg);
compat_ptr()should also be applied to the argument for these two commands, though I'm fairly sure IDE drivers have never been useful on s390 so it doesn't matter in practice.
Agreed on both, fixed by folding in this change:
drivers/ide/ide-cd.c +0 -0
@@ -1716,19 +1716,20 @@ static int idecd_locked_compat_ioctl(struct
block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg)
{
struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
+ void __user *argp = compat_ptr(arg);
int err;
switch (cmd) {
case CDROMSETSPINDOWN:- return idecd_set_spindown(&info->devinfo, arg);
+ return idecd_set_spindown(&info->devinfo, (unsigned long)argp);
case CDROMGETSPINDOWN:
- return idecd_get_spindown(&info->devinfo, arg);
+ return idecd_get_spindown(&info->devinfo, (unsigned long)argp);
default:
break;
}
return cdrom_ioctl(&info->devinfo, bdev, mode, cmd,- (unsigned long)compat_ptr(arg));
+ (unsigned long)argp);
}
static int idecd_compat_ioctl(struct block_device *bdev, fmode_t mode,
Unfortunately the generic_ide_ioctl() call still needs an unconverted arg, so I can't just use compat_ptr() to call the native idecd_locked_ioctl() for all commands.
Thanks,
Arnd
PATCH v2 22/27 compat_ioctl: scsi: handle HDIO commands from drivers
In reply to Arnd BergmannThe ata_sas_scsi_ioctl() function implements a number of HDIO_* commands for SCSI devices, it is used by all libata drivers as well as a few drivers that support SAS attached SATA drives.
The only command that is not safe for compat ioctls here is HDIO_GET_32BIT. Change the implementation to check for in_compat_syscall() in order to do both cases correctly, and change all callers to use it as both native and compat callback pointers, including the indirect callers through sas_ioctl and ata_scsi_ioctl.
--- drivers/ata/libata-scsi.c | 9 +++++++++ drivers/scsi/aic94xx/aic94xx_init.c | 3 +++ drivers/scsi/hisi_sas/hisi_sas_v1_hw.c | 3 +++ drivers/scsi/hisi_sas/hisi_sas_v2_hw.c | 3 +++ drivers/scsi/hisi_sas/hisi_sas_v3_hw.c | 3 +++ drivers/scsi/ipr.c | 3 +++ drivers/scsi/isci/init.c | 3 +++ drivers/scsi/mvsas/mv_init.c | 3 +++ drivers/scsi/pm8001/pm8001_init.c | 3 +++ include/linux/libata.h | 6 ++++++ 10 files changed, 39 insertions(+)
drivers/ata/libata-scsi.c +9 -0
@@ -17,6 +17,7 @@* - http://www.t13.org/ */ +#include <linux/compat.h> #include <linux/slab.h> #include <linux/kernel.h> #include <linux/blkdev.h>
@@ -761,6 +762,10 @@ static int ata_ioc32(struct ata_port *ap)return 0 ; } +/* +* This handles both native and compat commands, so anything added +* here must have a compatible argument, or check in_compat_syscall() +*/ int ata_sas_scsi_ioctl (struct ata_port *ap,struct scsi_device *scsidev,unsigned int cmd,void __user *arg) {
@@ -773,6 +778,10 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,spin_lock_irqsave (ap->lock, flags); val =ata_ioc32 (ap);spin_unlock_irqrestore (ap->lock, flags); +#ifdef CONFIG_COMPAT +if (in_compat_syscall ()) +return put_user (val, (compat_ulong_t __user *)arg); +#endif return put_user (val, (unsigned long __user *)arg);case HDIO_SET_32BIT :
drivers/scsi/aic94xx/aic94xx_init.c +3 -0
@@ -54,6 +54,9 @@ static struct scsi_host_template aic94xx_sht = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .track_queue_depth = 1, };
drivers/scsi/hisi_sas/hisi_sas_v1_hw.c +3 -0
@@ -1772,6 +1772,9 @@ static struct scsi_host_template sht_v1_hw = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = host_attrs_v1_hw, .host_reset = hisi_sas_host_reset, };
drivers/scsi/hisi_sas/hisi_sas_v2_hw.c +3 -0
@@ -3551,6 +3551,9 @@ static struct scsi_host_template sht_v2_hw = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = host_attrs_v2_hw, .host_reset = hisi_sas_host_reset, };
drivers/scsi/hisi_sas/hisi_sas_v3_hw.c +3 -0
@@ -3075,6 +3075,9 @@ static struct scsi_host_template sht_v3_hw = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = host_attrs_v3_hw, .tag_alloc_policy = BLK_TAG_ALLOC_RR, .host_reset = hisi_sas_host_reset,
drivers/scsi/ipr.c +3 -0
@@ -6727,6 +6727,9 @@ static struct scsi_host_template driver_template = { .name = "IPR", .info = ipr_ioa_info, .ioctl = ipr_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = ipr_ioctl, +#endif .queuecommand = ipr_queuecommand, .eh_abort_handler = ipr_eh_abort, .eh_device_reset_handler = ipr_eh_dev_reset,
drivers/scsi/isci/init.c +3 -0
@@ -168,6 +168,9 @@ static struct scsi_host_template isci_sht = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = isci_host_attrs, .track_queue_depth = 1, };
drivers/scsi/mvsas/mv_init.c +3 -0
@@ -47,6 +47,9 @@ static struct scsi_host_template mvs_sht = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = mvst_host_attrs, .track_queue_depth = 1, };
drivers/scsi/pm8001/pm8001_init.c +3 -0
@@ -101,6 +101,9 @@ static struct scsi_host_template pm8001_sht = { .eh_target_reset_handler = sas_eh_target_reset_handler, .target_destroy = sas_target_destroy, .ioctl = sas_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = sas_ioctl, +#endif .shost_attrs = pm8001_host_attrs, .track_queue_depth = 1, };
include/linux/libata.h +6 -0
@@ -1109,6 +1109,11 @@ extern void ata_host_init(struct ata_host *, struct device *, struct ata_port_opextern int ata_scsi_detect (struct scsi_host_template *sht);extern int ata_scsi_ioctl (struct scsi_device *dev,unsigned int cmd,void __user *arg); +#ifdef CONFIG_COMPAT +#define ATA_SCSI_COMPAT_IOCTL .compat_ioctl = ata_scsi_ioctl, +#else +#define ATA_SCSI_COMPAT_IOCTL /* empty */ +#endif extern int ata_scsi_queuecmd (struct Scsi_Host *h,struct scsi_cmnd *cmd);extern int ata_sas_scsi_ioctl (struct ata_port *ap,struct scsi_device *dev,unsigned int cmd,void __user *arg);
@@ -1340,6 +1345,7 @@ extern struct device_attribute *ata_common_sdev_attrs[]; .module = THIS_MODULE, \ .name = drv_name, \ .ioctl = ata_scsi_ioctl, \ +ATA_SCSI_COMPAT_IOCTL \ .queuecommand = ata_scsi_queuecmd, \ .can_queue = ATA_DEF_QUEUE, \ .tag_alloc_policy = BLK_TAG_ALLOC_RR, \
Signature · 1 line
2.20.0
PATCH v2 23/27 compat_ioctl: move HDIO ioctl handling into drivers/ide
In reply to Arnd BergmannMost of the HDIO ioctls are only used by the obsolete drivers/ide subsystem, these can be handled by changing ide_cmd_ioctl() to be aware of compat mode and doing the correct transformations in place and using it as both native and compat handlers for all drivers.
The SCSI drivers implementing the same commands are already doing this in the drivers, so the compat_blkdev_driver_ioctl() function is no longer needed now.
The BLKSECTSET and HDIO_GETGEO_BIG ioctls are not implemented in any driver any more and no longer need any conversion.
--- block/compat_ioctl.c | 75 ---------------------------------- drivers/ide/ide-cd.c | 15 +++---- drivers/ide/ide-disk.c | 1 + drivers/ide/ide-floppy_ioctl.c | 7 ++-- drivers/ide/ide-ioctls.c | 44 ++++++++++++-------- drivers/ide/ide-tape.c | 11 +++++ 6 files changed, 51 insertions(+), 102 deletions(-)
block/compat_ioctl.c +0 -75
@@ -77,24 +77,6 @@ static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,return ret; } -static int compat_hdio_ioctl (struct block_device *bdev,fmode_t mode, -unsigned int cmd,unsigned long arg) -{ -unsigned long __user *p; -int error; - - p =compat_alloc_user_space (sizeof (unsigned long )); - error =__blkdev_driver_ioctl (bdev, mode, - cmd, (unsigned long )p); -if (error ==0 ) { -unsigned int __user *uvp =compat_ptr (arg); -unsigned long v; -if (get_user (v, p) ||put_user (v, uvp)) - error = -EFAULT ; - } -return error; -} -struct compat_blkpg_ioctl_arg {compat_int_t op;compat_int_t flags;
@@ -128,61 +110,6 @@ static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,#define BLKBSZSET_32 _IOW(0x12, 113, int)#define BLKGETSIZE64_32 _IOR(0x12, 114, int) -static int compat_blkdev_driver_ioctl (struct block_device *bdev,fmode_t mode, -unsigned cmd,unsigned long arg) -{ -switch (cmd) { -case HDIO_GET_UNMASKINTR : -case HDIO_GET_MULTCOUNT : -case HDIO_GET_KEEPSETTINGS : -case HDIO_GET_32BIT : -case HDIO_GET_NOWERR : -case HDIO_GET_DMA : -case HDIO_GET_NICE : -case HDIO_GET_WCACHE : -case HDIO_GET_ACOUSTIC : -case HDIO_GET_ADDRESS : -case HDIO_GET_BUSSTATE : -return compat_hdio_ioctl (bdev, mode, cmd, arg); - -/* -* No handler required for the ones below, we just need to -* convert arg to a 64 bit pointer. -*/ -case BLKSECTSET : -/* -* 0x03 -- HD/IDE ioctl's used by hdparm and friends. -* Some need translations, these do not. -*/ -case HDIO_GET_IDENTITY : -case HDIO_DRIVE_TASK : -case HDIO_DRIVE_CMD : -/* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */ -case 0x330 : - arg = (unsigned long )compat_ptr (arg); -/* These intepret arg as an unsigned long, not as a pointer, -* so we must not do compat_ptr() conversion. */ -case HDIO_SET_MULTCOUNT : -case HDIO_SET_UNMASKINTR : -case HDIO_SET_KEEPSETTINGS : -case HDIO_SET_32BIT : -case HDIO_SET_NOWERR : -case HDIO_SET_DMA : -case HDIO_SET_PIO_MODE : -case HDIO_SET_NICE : -case HDIO_SET_WCACHE : -case HDIO_SET_ACOUSTIC : -case HDIO_SET_BUSSTATE : -case HDIO_SET_ADDRESS : -break ; -default : -/* unknown ioctl number */ -return -ENOIOCTLCMD ; - } - -return __blkdev_driver_ioctl (bdev, mode, cmd, arg); -} -/* Most of the generic ioctls are handled in the normal fallback path. This assumes the blkdev's low level compat_ioctl always returns ENOIOCTLCMD for unknown ioctls. */
@@ -293,8 +220,6 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)default :if (disk->fops->compat_ioctl) ret = disk->fops->compat_ioctl (bdev, mode, cmd, arg); -if (ret == -ENOIOCTLCMD ) - ret =compat_blkdev_driver_ioctl (bdev, mode, cmd, arg);return ret; } }
drivers/ide/ide-cd.c +8 -7
@@ -1711,7 +1711,6 @@ static int idecd_ioctl(struct block_device *bdev, fmode_t mode,return ret; } -#ifdef CONFIG_COMPAT static int idecd_locked_compat_ioctl (struct block_device *bdev,fmode_t mode,unsigned int cmd,unsigned long arg) {
@@ -1727,8 +1726,12 @@ static int idecd_locked_compat_ioctl(struct block_device *bdev, fmode_t mode,break ; } -return cdrom_ioctl (&info->devinfo, bdev, mode, cmd, - (unsigned long )compat_ptr (arg)); + err =generic_ide_ioctl (info->drive, bdev, cmd, arg); +if (err == -EINVAL ) + err =cdrom_ioctl (&info->devinfo, bdev, mode, cmd, + (unsigned long )compat_ptr (arg)); + +return err; } static int idecd_compat_ioctl(struct block_device *bdev, fmode_t mode,
@@ -1742,7 +1745,6 @@ static int idecd_compat_ioctl(struct block_device *bdev, fmode_t mode,return ret; } -#endif static unsigned int idecd_check_events (struct gendisk *disk,unsigned int clearing)
@@ -1765,9 +1767,8 @@ static const struct block_device_operations idecd_ops = { .open = idecd_open, .release = idecd_release, .ioctl = idecd_ioctl, -#ifdef CONFIG_COMPAT - .compat_ioctl = idecd_compat_ioctl, -#endif + .compat_ioctl =IS_ENABLED (CONFIG_COMPAT ) ? + idecd_compat_ioctl : NULL, .check_events = idecd_check_events, .revalidate_disk = idecd_revalidate_disk };
drivers/ide/ide-disk.c +1 -0
@@ -794,4 +794,5 @@ const struct ide_disk_ops ide_ata_disk_ops = { .set_doorlock = ide_disk_set_doorlock, .do_request = ide_do_rw_disk, .ioctl = ide_disk_ioctl, + .compat_ioctl = ide_disk_ioctl, };
drivers/ide/ide-floppy_ioctl.c +3 -4
@@ -329,10 +329,9 @@ int ide_floppy_compat_ioctl(ide_drive_t *drive, struct block_device *bdev,if (cmd !=CDROM_SEND_PACKET && cmd !=SCSI_IOCTL_SEND_COMMAND ) err =scsi_cmd_blk_ioctl (bdev, mode, cmd, argp); -/* -* there is no generic_ide_compat_ioctl(), that is handled -* through compat_blkdev_ioctl(). -*/ +if (err == -ENOTTY ) + err =generic_ide_ioctl (drive, bdev, cmd, arg); + out:mutex_unlock (&ide_floppy_ioctl_mutex);return err;
drivers/ide/ide-ioctls.c +28 -16
141 lines not on this page · raw message
drivers/ide/ide-tape.c +11 -0
@@ -1945,11 +1945,22 @@ static int idetape_ioctl(struct block_device *bdev, fmode_t mode,return err; } +static int idetape_compat_ioctl (struct block_device *bdev,fmode_t mode, +unsigned int cmd,unsigned long arg) +{ +if (cmd ==0x0340 || cmd ==0x350 ) + arg = (unsigned long )compat_ptr (arg); + +return idetape_ioctl (bdev, mode, cmd, arg); +} +static const struct block_device_operations idetape_block_ops = { .owner =THIS_MODULE , .open = idetape_open, .release = idetape_release, .ioctl = idetape_ioctl, + .compat_ioctl =IS_ENABLED (CONFIG_COMPAT ) ? + idetape_compat_ioctl :NULL , };static int ide_tape_probe (ide_drive_t *drive)
Signature · 1 line
2.20.0
Re: [PATCH v2 23/27] compat_ioctl: move HDIO ioctl handling into drivers/ide
Quoted 11 lines · Arnd Bergmann
Most of the HDIO ioctls are only used by the obsolete
drivers/idesubsystem, these can be handled by changingide_cmd_ioctl()to be aware of compat mode and doing the correct transformations in place and using it as both native and compat handlers for all drivers.The SCSI drivers implementing the same commands are already doing this in the drivers, so the
compat_blkdev_driver_ioctl()function is no longer needed now.The BLKSECTSET and
HDIO_GETGEO_BIGioctls are not implemented in any driver any more and no longer need any conversion.
[...]
I noticed that HDIO_DRIVE_TASKFILE, handled by ide_taskfile_ioctl() in drivers/ide/ide-taskfile.c, never had compat handling before. After this patch it does, but its argument isn't passed through compat_ptr(). Again, doesn't really matter because IDE isn't a thing on s390.
Ben.
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomRe: [PATCH v2 23/27] compat_ioctl: move HDIO ioctl handling into drivers/ide
Quoted 10 lines · Ben Hutchings
Quoted 9 lines · Arnd Bergmann
Most of the HDIO ioctls are only used by the obsolete
drivers/idesubsystem, these can be handled by changingide_cmd_ioctl()to be aware of compat mode and doing the correct transformations in place and using it as both native and compat handlers for all drivers.The SCSI drivers implementing the same commands are already doing this in the drivers, so the
compat_blkdev_driver_ioctl()function is no longer needed now.
Quoted 8 lines
Quoted 2 lines
The BLKSECTSET and
HDIO_GETGEO_BIGioctls are not implemented in any driver any more and no longer need any conversion.[...]
I noticed that
HDIO_DRIVE_TASKFILE, handled byide_taskfile_ioctl()indrivers/ide/ide-taskfile.c, never had compat handling before. After this patch it does, but its argument isn't passed throughcompat_ptr(). Again, doesn't really matter because IDE isn't a thing on s390.
I checked again, and I think it's worse than that: ide_taskfile_ioctl() takes an ide_task_request_t argument, which is not compatible at all (it has two long members). I suspect what happened here is that I confused it with ide_cmd_ioctl(), which takes a 'struct ide_taskfile' argument that /is/ compatible.
I don't think there is a point in adding a handler now: most users of drivers/ide are 32-bit only, and nobody complained so far, but I would add this change if you agree:
drivers/ide/ide-ioctls.c +0 -0
@@ -270,6 +270,9 @@ int generic_ide_ioctl(ide_drive_t *drive, struct
block_device *bdev,
case HDIO_DRIVE_TASKFILE:
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
return -EACCES;
+ /* missing compat handler for HDIO_DRIVE_TASKFILE */
+ if (in_compat_syscall())
+ return -ENOTTY;
if (drive->media == ide_disk)
return ide_taskfile_ioctl(drive, arg);
return -ENOMSG;
ArndRe: [PATCH v2 23/27] compat_ioctl: move HDIO ioctl handling into drivers/ide
Quoted 27 lines · Arnd Bergmann
Quoted 10 lines · Ben Hutchings
Quoted 9 lines · Arnd Bergmann
Most of the HDIO ioctls are only used by the obsolete
drivers/idesubsystem, these can be handled by changingide_cmd_ioctl()to be aware of compat mode and doing the correct transformations in place and using it as both native and compat handlers for all drivers.The SCSI drivers implementing the same commands are already doing this in the drivers, so the
compat_blkdev_driver_ioctl()function is no longer needed now.Quoted 8 lines
Quoted 2 lines
The BLKSECTSET and
HDIO_GETGEO_BIGioctls are not implemented in any driver any more and no longer need any conversion.[...]
I noticed that
HDIO_DRIVE_TASKFILE, handled byide_taskfile_ioctl()indrivers/ide/ide-taskfile.c, never had compat handling before. After this patch it does, but its argument isn't passed throughcompat_ptr(). Again, doesn't really matter because IDE isn't a thing on s390.I checked again, and I think it's worse than that:
ide_taskfile_ioctl()takes anide_task_request_targument, which is not compatible at all (it has two long members). I suspect what happened here is that I confused it withide_cmd_ioctl(), which takes a 'structide_taskfile' argument that /is/ compatible.
Quoted 3 lines
I don't think there is a point in adding a handler now: most users of
drivers/ideare 32-bit only, and nobody complained so far, but I would add this change if you agree:
Looks good to me.
Ben.
Quoted 20 lines
drivers/ide/ide-ioctls.c +3
-0@@ -270,6 +270,9 @@ int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev, case HDIO_DRIVE_TASKFILE: if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES; +/* missing compat handler for HDIO_DRIVE_TASKFILE */ +if (in_compat_syscall ()) +return -ENOTTY ;if (drive->media == ide_disk)return ide_taskfile_ioctl (drive, arg);return -ENOMSG; Arnd
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomPATCH v2 24/27 compat_ioctl: block: move blkdev_compat_ioctl() into ioctl.c
In reply to Arnd BergmannHaving both in the same file allows a number of simplifications to the compat path, and makes it more likely that changes to the native path get applied to the compat version as well.
--- block/Makefile | 1 - block/compat_ioctl.c | 225 ------------------------------------------- block/ioctl.c | 219 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+), 226 deletions(-) delete mode 100644 block/compat_ioctl.c
block/Makefile +0 -1
@@ -25,7 +25,6 @@ obj-$(CONFIG_MQ_IOSCHED_KYBER) += kyber-iosched.o bfq-y := bfq-iosched.o bfq-wf2q.o bfq-cgroup.o obj-$(CONFIG_IOSCHED_BFQ) += bfq.o -obj-$(CONFIG_BLOCK_COMPAT) += compat_ioctl.o obj-$(CONFIG_BLK_CMDLINE_PARSER) += cmdline-parser.o obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o obj-$(CONFIG_BLK_MQ_PCI) += blk-mq-pci.o
block/compat_ioctl.c deleted +0 -225
225 lines not on this page · raw message
block/ioctl.c +219 -0
234 lines not on this page · raw message
Signature · 1 line
2.20.0
PATCH v2 25/27 compat_ioctl: block: simplify compat_blkpg_ioctl()
In reply to Arnd BergmannThere is no need to go through a compat_alloc_user_space() copy any more, just wrap the function in a small helper that works the same way for native and compat mode.
--- block/ioctl.c | 74 ++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 36 deletions(-)
block/ioctl.c +38 -36
@@ -12,12 +12,12 @@#include <linux/pr.h> #include <linux/uaccess.h> -static int blkpg_ioctl (struct block_device *bdev,struct blkpg_ioctl_arg __user *arg) +static int blkpg_do_ioctl (struct block_device *bdev, +struct blkpg_partition __user *upart,int op) {struct block_device *bdevp;struct gendisk *disk;struct hd_struct *part, *lpart; -struct blkpg_ioctl_arg a;struct blkpg_partition p;struct disk_part_iter piter;long long start, length;
@@ -25,9 +25,7 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __userif (!capable (CAP_SYS_ADMIN ))return -EACCES ; -if (copy_from_user (&a, arg,sizeof (struct blkpg_ioctl_arg ))) -return -EFAULT ; -if (copy_from_user (&p, a.data,sizeof (struct blkpg_partition ))) +if (copy_from_user (&p, upart,sizeof (struct blkpg_partition )))return -EFAULT ; disk = bdev->bd_disk;if (bdev != bdev->bd_contains)
@@ -35,7 +33,7 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user partno = p.pno;if (partno <=0 )return -EINVAL ; -switch (a.op) { +switch (op) {case BLKPG_ADD_PARTITION : start = p.start >>9 ; length = p.length >>9 ;
@@ -156,6 +154,39 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user } } +static int blkpg_ioctl (struct block_device *bdev, +struct blkpg_ioctl_arg __user *arg) +{ +struct blkpg_partition __user *udata; +int op; + +if (get_user (op, &arg->op) ||get_user (udata, &arg->data)) +return -EFAULT ; + +return blkpg_do_ioctl (bdev, udata, op); +} + +#ifdef CONFIG_COMPAT +struct compat_blkpg_ioctl_arg { +compat_int_t op; +compat_int_t flags; +compat_int_t datalen; +compat_caddr_t data; +}; + +static int compat_blkpg_ioctl (struct block_device *bdev, +struct compat_blkpg_ioctl_arg __user *arg) +{ +compat_caddr_t udata; +int op; + +if (get_user (op, &arg->op) ||get_user (udata, &arg->data)) +return -EFAULT ; + +return blkpg_do_ioctl (bdev,compat_ptr (udata), op); +} +#endif +static int blkdev_reread_part (struct block_device *bdev) {int ret;
@@ -676,35 +707,6 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,EXPORT_SYMBOL_GPL (blkdev_ioctl);#ifdef CONFIG_COMPAT -struct compat_blkpg_ioctl_arg { -compat_int_t op; -compat_int_t flags; -compat_int_t datalen; -compat_caddr_t data; -}; - -static int compat_blkpg_ioctl (struct block_device *bdev,fmode_t mode, -unsigned int cmd,struct compat_blkpg_ioctl_arg __user *ua32) -{ -struct blkpg_ioctl_arg __user *a =compat_alloc_user_space (sizeof (*a)); -compat_caddr_t udata; -compat_int_t n; -int err; - - err =get_user (n, &ua32->op); - err |=put_user (n, &a->op); - err |=get_user (n, &ua32->flags); - err |=put_user (n, &a->flags); - err |=get_user (n, &ua32->datalen); - err |=put_user (n, &a->datalen); - err |=get_user (udata, &ua32->data); - err |=put_user (compat_ptr (udata), &a->data); -if (err) -return err; - -return blkdev_ioctl (bdev, mode, cmd, (unsigned long )a); -} -#define BLKBSZGET_32 _IOR(0x12, 112, int)#define BLKBSZSET_32 _IOW(0x12, 113, int)#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
@@ -767,7 +769,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)return blkdev_ioctl (bdev, mode,BLKBSZSET , (unsigned long )compat_ptr (arg));case BLKPG : -return compat_blkpg_ioctl (bdev, mode, cmd,compat_ptr (arg)); +return compat_blkpg_ioctl (bdev,compat_ptr (arg));case BLKRAGET :case BLKFRAGET :if (!arg)
Signature · 1 line
2.20.0
PATCH v2 26/27 compat_ioctl: simplify up block/ioctl.c
In reply to Arnd BergmannHaving separate implementations of blkdev_ioctl() often leads to these getting out of sync, despite the comment at the top.
Since most of the ioctl commands are compatible, and we try very hard not to add any new incompatible ones, move all the common bits into a shared function and leave only the ones that are historically different in separate functions for native/compat mode.
To deal with the compat_ptr() conversion, pass both the integer argument and the pointer argument into the new blkdev_common_ioctl() and make sure to always use the correct one of these.
blkdev_ioctl() is now only kept as a separate exported interfact for drivers/char/raw.c, which lacks a compat_ioctl variant. We should probably either move raw.c to staging if there are no more users, or export blkdev_compat_ioctl() as well.
--- block/ioctl.c | 269 ++++++++++++++++++++++---------------------------- 1 file changed, 117 insertions(+), 152 deletions(-)
block/ioctl.c +117 -152
@@ -270,65 +270,45 @@ static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode, BLKDEV_ZERO_NOUNMAP); } -static int put_ushort (unsigned long arg,unsigned short val) +static int put_ushort (unsigned short __user *argp,unsigned short val) { -return put_user (val, (unsigned short __user *)arg); +return put_user (val, argp); } -static int put_int (unsigned long arg,int val) +static int put_int (int __user *argp,int val) { -return put_user (val, (int __user *)arg); +return put_user (val, argp); } -static int put_uint (unsigned long arg,unsigned int val) +static int put_uint (unsigned int __user *argp,unsigned int val) { -return put_user (val, (unsigned int __user *)arg); +return put_user (val, argp); } -static int put_long (unsigned long arg,long val) +static int put_long (long __user *argp,long val) { -return put_user (val, (long __user *)arg); +return put_user (val, argp); } -static int put_ulong (unsigned long arg,unsigned long val) +static int put_ulong (unsigned long __user *argp,unsigned long val) { -return put_user (val, (unsigned long __user *)arg); +return put_user (val, argp); } -static int put_u64 (unsigned long arg,u64 val) +static int put_u64 (u64 __user *argp,u64 val) { -return put_user (val, (u64 __user *)arg); +return put_user (val, argp); }#ifdef CONFIG_COMPAT -static int compat_put_ushort (unsigned long arg,unsigned short val) +static int compat_put_long (compat_long_t *argp,long val) { -return put_user (val, (unsigned short __user *)compat_ptr (arg)); +return put_user (val, argp); } -static int compat_put_int (unsigned long arg,int val) +static int compat_put_ulong (compat_ulong_t *argp,compat_ulong_t val) { -return put_user (val, (compat_int_t __user *)compat_ptr (arg)); -} - -static int compat_put_uint (unsigned long arg,unsigned int val) -{ -return put_user (val, (compat_uint_t __user *)compat_ptr (arg)); -} - -static int compat_put_long (unsigned long arg,long val) -{ -return put_user (val, (compat_long_t __user *)compat_ptr (arg)); -} - -static int compat_put_ulong (unsigned long arg,compat_ulong_t val) -{ -return put_user (val, (compat_ulong_t __user *)compat_ptr (arg)); -} - -static int compat_put_u64 (unsigned long arg,u64 val) -{ -return put_user (val, (compat_u64 __user *)compat_ptr (arg)); +return put_user (val, argp); }#endif
@@ -547,9 +527,10 @@ struct compat_hd_geometry {u32 start; }; -static int compat_hdio_getgeo (struct gendisk *disk,struct block_device *bdev, -struct compat_hd_geometry __user *ugeo) +static int compat_hdio_getgeo (struct block_device *bdev, +struct compat_hd_geometry __user *ugeo) { +struct gendisk *disk = bdev->bd_disk;struct hd_geometry geo;int ret;
@@ -603,13 +584,13 @@ static int blkdev_bszset(struct block_device *bdev, fmode_t mode, }/* -* always keep this in sync with compat_blkdev_ioctl() +* Common commands that are handled the same way on native and compat +* user space. Note the separate arg/argp parameters that are needed +* to deal with the compat_ptr() conversion. */ -int blkdev_ioctl (struct block_device *bdev,fmode_t mode,unsigned cmd, -unsigned long arg) +static int blkdev_common_ioctl (struct block_device *bdev,fmode_t mode, +unsigned cmd,unsigned long arg,void __user *argp) { -void __user *argp = (void __user *)arg; -loff_t size;unsigned int max_sectors;switch (cmd) {
@@ -632,60 +613,39 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,case BLKFINISHZONE :return blkdev_zone_mgmt_ioctl (bdev, mode, cmd, arg);case BLKGETZONESZ : -return put_uint (arg,bdev_zone_sectors (bdev)); +return put_uint (argp,bdev_zone_sectors (bdev));case BLKGETNRZONES : -return put_uint (arg,blkdev_nr_zones (bdev->bd_disk)); -case HDIO_GETGEO : -return blkdev_getgeo (bdev, argp); -case BLKRAGET : -case BLKFRAGET : -if (!arg) -return -EINVAL ; -return put_long (arg, (bdev->bd_bdi->ra_pages*PAGE_SIZE ) /512 ); +return put_uint (argp,blkdev_nr_zones (bdev->bd_disk));case BLKROGET : -return put_int (arg,bdev_read_only (bdev) !=0 ); -case BLKBSZGET :/* get block device soft block size (cf. BLKSSZGET) */ -return put_int (arg,block_size (bdev)); +return put_int (argp,bdev_read_only (bdev) !=0 );case BLKSSZGET :/* get block device logical block size */ -return put_int (arg,bdev_logical_block_size (bdev)); +return put_int (argp,bdev_logical_block_size (bdev));case BLKPBSZGET :/* get block device physical block size */ -return put_uint (arg,bdev_physical_block_size (bdev)); +return put_uint (argp,bdev_physical_block_size (bdev));case BLKIOMIN : -return put_uint (arg,bdev_io_min (bdev)); +return put_uint (argp,bdev_io_min (bdev));case BLKIOOPT : -return put_uint (arg,bdev_io_opt (bdev)); +return put_uint (argp,bdev_io_opt (bdev));case BLKALIGNOFF : -return put_int (arg,bdev_alignment_offset (bdev)); +return put_int (argp,bdev_alignment_offset (bdev));case BLKDISCARDZEROES : -return put_uint (arg,0 ); +return put_uint (argp,0 );case BLKSECTGET : max_sectors = min_t(unsigned int, USHRT_MAX,queue_max_sectors (bdev_get_queue (bdev))); -return put_ushort (arg, max_sectors); +return put_ushort (argp, max_sectors);case BLKROTATIONAL : -return put_ushort (arg, !blk_queue_nonrot (bdev_get_queue (bdev))); +return put_ushort (argp, !blk_queue_nonrot (bdev_get_queue (bdev)));case BLKRASET :case BLKFRASET :if (!capable (CAP_SYS_ADMIN ))return -EACCES ; bdev->bd_bdi->ra_pages = (arg *512 ) /PAGE_SIZE ;return 0 ; -case BLKBSZSET : -return blkdev_bszset (bdev, mode, argp); -case BLKPG : -return blkpg_ioctl (bdev, argp);case BLKRRPART :return blkdev_reread_part (bdev); -case BLKGETSIZE : - size =i_size_read (bdev->bd_inode); -if ((size >>9 ) > ~0UL ) -return -EFBIG ; -return put_ulong (arg, size >>9 ); -case BLKGETSIZE64 : -return put_u64 (arg,i_size_read (bdev->bd_inode));case BLKTRACESTART :case BLKTRACESTOP : -case BLKTRACESETUP :case BLKTRACETEARDOWN :return blk_trace_ioctl (bdev, cmd, argp);case IOC_PR_REGISTER :
@@ -701,12 +661,67 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,case IOC_PR_CLEAR :return blkdev_pr_clear (bdev, argp);default : -return __blkdev_driver_ioctl (bdev, mode, cmd, arg); +return -ENOIOCTLCMD ; } } -EXPORT_SYMBOL_GPL (blkdev_ioctl); + +/* +* Always keep this in sync with compat_blkdev_ioctl() +* to handle all incompatible commands in both functions. +* +* New commands must be compatible and go into blkdev_common_ioctl +*/ +int blkdev_ioctl (struct block_device *bdev,fmode_t mode,unsigned cmd, +unsigned long arg) +{ +int ret; +loff_t size; +void __user *argp = (void __user *)arg; + +switch (cmd) { +/* These need separate implementations for the data structure */ +case HDIO_GETGEO : +return blkdev_getgeo (bdev, argp); +case BLKPG : +return blkpg_ioctl (bdev, argp); + +/* Compat mode returns 32-bit data instead of 'long' */ +case BLKRAGET : +case BLKFRAGET : +if (!argp) +return -EINVAL ; +return put_long (argp, (bdev->bd_bdi->ra_pages*PAGE_SIZE ) /512 ); +case BLKGETSIZE : + size =i_size_read (bdev->bd_inode); +if ((size >>9 ) > ~0UL ) +return -EFBIG ; +return put_ulong (argp, size >>9 ); + +/* The data is compatible, but the command number is different */ +case BLKBSZGET :/* get block device soft block size (cf. BLKSSZGET) */ +return put_int (argp,block_size (bdev)); +case BLKBSZSET : +return blkdev_bszset (bdev, mode, argp); +case BLKGETSIZE64 : +return put_u64 (argp,i_size_read (bdev->bd_inode)); + +/* Incompatible alignment on i386 */ +case BLKTRACESETUP : +return blk_trace_ioctl (bdev, cmd, argp); +default : +break ; + } + + ret =blkdev_common_ioctl (bdev, mode, cmd, arg, argp); +if (ret == -ENOIOCTLCMD ) +return __blkdev_driver_ioctl (bdev, mode, cmd, arg); + +return ret; +} +EXPORT_SYMBOL_GPL (blkdev_ioctl);/* for /dev/raw */ #ifdef CONFIG_COMPAT +#define BLKBSZGET_32 _IOR(0x12, 112, int)#define BLKBSZSET_32 _IOW(0x12, 113, int)#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
@@ -716,13 +731,13 @@ EXPORT_SYMBOL_GPL(blkdev_ioctl);ENOIOCTLCMD for unknown ioctls. */ long compat_blkdev_ioctl (struct file *file,unsigned cmd,unsigned long arg) { -int ret = -ENOIOCTLCMD ; +int ret; +void __user *argp =compat_ptr (arg);struct inode *inode = file->f_mapping->host;struct block_device *bdev = inode->i_bdev;struct gendisk *disk = bdev->bd_disk;fmode_t mode = file->f_mode;loff_t size; -unsigned int max_sectors;/* * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
@@ -734,94 +749,44 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) mode &= ~FMODE_NDELAY ;switch (cmd) { +/* These need separate implementations for the data structure */ case HDIO_GETGEO : -return compat_hdio_getgeo (disk, bdev,compat_ptr (arg)); -case BLKPBSZGET : -return compat_put_uint (arg,bdev_physical_block_size (bdev)); -case BLKIOMIN : -return compat_put_uint (arg,bdev_io_min (bdev)); -case BLKIOOPT : -return compat_put_uint (arg,bdev_io_opt (bdev)); -case BLKALIGNOFF : -return compat_put_int (arg,bdev_alignment_offset (bdev)); -case BLKDISCARDZEROES : -return compat_put_uint (arg,0 ); -case BLKFLSBUF : -case BLKROSET : -case BLKDISCARD : -case BLKSECDISCARD : -case BLKZEROOUT : -/* -* the ones below are implemented in blkdev_locked_ioctl, -* but we call blkdev_ioctl, which gets the lock for us -*/ -case BLKRRPART : -case BLKREPORTZONE : -case BLKRESETZONE : -case BLKOPENZONE : -case BLKCLOSEZONE : -case BLKFINISHZONE : -case BLKGETZONESZ : -case BLKGETNRZONES : -return blkdev_ioctl (bdev, mode, cmd, - (unsigned long )compat_ptr (arg)); -case BLKBSZSET_32 : -return blkdev_ioctl (bdev, mode,BLKBSZSET , - (unsigned long )compat_ptr (arg)); +return compat_hdio_getgeo (bdev, argp);case BLKPG : -return compat_blkpg_ioctl (bdev,compat_ptr (arg)); +return compat_blkpg_ioctl (bdev, argp); + +/* Compat mode returns 32-bit data instead of 'long' */ case BLKRAGET :case BLKFRAGET : -if (!arg) +if (!argp)return -EINVAL ; -return compat_put_long (arg, +return compat_put_long (argp, (bdev->bd_bdi->ra_pages *PAGE_SIZE ) /512 ); -case BLKROGET :/* compatible */ -return compat_put_int (arg,bdev_read_only (bdev) !=0 ); -case BLKBSZGET_32 :/* get the logical block size (cf. BLKSSZGET) */ -return compat_put_int (arg,block_size (bdev)); -case BLKSSZGET :/* get block device hardware sector size */ -return compat_put_int (arg,bdev_logical_block_size (bdev)); -case BLKSECTGET : - max_sectors = min_t(unsigned int, USHRT_MAX, -queue_max_sectors (bdev_get_queue (bdev))); -return compat_put_ushort (arg, max_sectors); -case BLKROTATIONAL : -return compat_put_ushort (arg, - !blk_queue_nonrot (bdev_get_queue (bdev))); -case BLKRASET :/* compatible, but no compat_ptr (!) */ -case BLKFRASET : -if (!capable (CAP_SYS_ADMIN )) -return -EACCES ; - bdev->bd_bdi->ra_pages = (arg *512 ) /PAGE_SIZE ; -return 0 ;case BLKGETSIZE : size =i_size_read (bdev->bd_inode);if ((size >>9 ) > ~0UL )return -EFBIG ; -return compat_put_ulong (arg, size >>9 ); +return compat_put_ulong (argp, size >>9 ); +/* The data is compatible, but the command number is different */ +case BLKBSZGET_32 :/* get the logical block size (cf. BLKSSZGET) */ +return put_int (argp,bdev_logical_block_size (bdev)); +case BLKBSZSET_32 : +return blkdev_bszset (bdev, mode, argp);case BLKGETSIZE64_32 : -return compat_put_u64 (arg,i_size_read (bdev->bd_inode)); +return put_u64 (argp,i_size_read (bdev->bd_inode)); +/* Incompatible alignment on i386 */ case BLKTRACESETUP32 : -case BLKTRACESTART :/* compatible */ -case BLKTRACESTOP :/* compatible */ -case BLKTRACETEARDOWN :/* compatible */ - ret =blk_trace_ioctl (bdev, cmd,compat_ptr (arg)); -return ret; -case IOC_PR_REGISTER : -case IOC_PR_RESERVE : -case IOC_PR_RELEASE : -case IOC_PR_PREEMPT : -case IOC_PR_PREEMPT_ABORT : -case IOC_PR_CLEAR : -return blkdev_ioctl (bdev, mode, cmd, - (unsigned long )compat_ptr (arg)); +return blk_trace_ioctl (bdev, cmd, argp);default : -if (disk->fops->compat_ioctl) - ret = disk->fops->compat_ioctl (bdev, mode, cmd, arg); -return ret; +break ; } + + ret =blkdev_common_ioctl (bdev, mode, cmd, arg, argp); +if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl) + ret = disk->fops->compat_ioctl (bdev, mode, cmd, arg); + + return ret; }#endif
Signature · 1 line
2.20.0
PATCH v2 27/27 Documentation: document ioctl interfaces better
In reply to Arnd BergmannDocumentation/process/botching-up-ioctls.rst was orignally written as a blog post for DRM driver writers, so it it misses some points while going into a lot of detail on others.
Try to provide a replacement that addresses typical issues across a wider range of subsystems, and follows the style of the core-api documentation better.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/ioctl.rst | 248 +++++++++++++++++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 Documentation/core-api/ioctl.rst
Documentation/core-api/index.rst +1 -0
@@ -39,6 +39,7 @@ Core utilities ../RCU/index gcc-plugins symbol-namespaces + ioctl Interfaces for kernel debugging
Documentation/core-api/ioctl.rst new file +248 -0
248 lines not on this page · raw message
Signature · 1 line
2.20.0
Re: [PATCH v2 27/27] Documentation: document ioctl interfaces better
On Tue, 2019-12-17 at 23:17 +0100, Arnd Bergmann wrote:
[...]
Quoted 17 lines
Documentation/core-api/ioctl.rst new file +14
-0@@ -0,0 +1,248 @@ +====================== +ioctl based interfaces +====================== + +ioctl() is the most common way for applications to interface +with device drivers. It is flexible and easily extended by adding new +commands and can be passed through character devices, block devices as +well as sockets and other special file descriptors. + +However, it is also very easy to get ioctl command definitions wrong, +and hard to fix them later without breaking existing applications, +so this documentation tries to help developers get it right. + +Command number definitions
Documentation/core-api/ioctl.rst line 24
Quoted 10 lines
Documentation/core-api/ioctl.rst new file +10
-0+========================== + +The command number, or request number, is the second argument passed to +the ioctl system call. While this can be any 32-bit number that uniquely +identifies an action for a particular driver, there are a number of +conventions around defining them. + +``include/uapi/asm-generic/ioctl.h`` provides four macros for defining +ioctl commands that follow modern conventions: ``_IO``, ``_IOR``, +``_IOW``, and ``_IORW``. These should be used for all new commands,
Typo: "_IORW" should be "_IOWR".
Documentation/core-api/ioctl.rst line 31
Quoted 7 lines
Documentation/core-api/ioctl.rst new file +7
-0+with the correct parameters: + +_IO/_IOR/_IOW/_IOWR + The macro name determines whether the argument is used for passing + data into kernel (_IOW), from the kernel (_IOR), both (_IOWR) or is + not a pointer (_IO). It is possible but not recommended to pass an + integer value instead of a pointer with _IO.
I feel the explanation of _IO here could be confusing. I think what you meant to say was that it is possible, but not recommended, to pass integers directly (arg is integer) rather than indirectly (arg is pointer to integer). I suggest the alternate wording:
The macro name specifies how the argument will be used. It may be a pointer to data to be passed into the kernel (_IOW), out of the kernel (_IOR), or both (_IOWR). The argument may also be an integer value instead of a pointer (_IO), but this is not recommended.
Quoted 4 lines
Documentation/core-api/ioctl.rst new file +4
-0+type + An 8-bit number, often a character literal, specific to a subsystem + or driver, and listed in :doc:`../userspace-api/ioctl/ioctl-number` +
Documentation/core-api/ioctl.rst line 46
Quoted 10 lines
Documentation/core-api/ioctl.rst new file +10
-0+nr + An 8-bit number identifying the specific command, unique for a give + value of 'type' + +data_type + The name of the data type pointed to by the argument, the command number + encodes the ``sizeof(data_type)`` value in a 13-bit or 14-bit integer, + leading to a limit of 8191 bytes for the maximum size of the argument. + Note: do not pass sizeof(data_type) type into _IOR/IOW, as that will + lead to encoding sizeof(sizeof(data_type)), i.e. sizeof(size_t).
You left out _IOWR here. It might also be worth mentioning that _IO doesn't have this parameter.
[...]
Documentation/core-api/ioctl.rst
Quoted 5 lines
Documentation/core-api/ioctl.rst new file +5
-0+Return code +=========== + +ioctl commands can return negative error codes as documented in errno(3), +these get turned into errno values in user space.
Use a semi-colon instead of a comma, or change "these" to "which".
Documentation/core-api/ioctl.rst line 75
Quoted 10 lines
On success, the return
Documentation/core-api/ioctl.rst new file +9
-0+code should be zero. It is also possible but not recommended to return +a positive 'long' value. + +When the ioctl callback is called with an unknown command number, the +handler returns either -ENOTTY or -ENOIOCTLCMD, which also results in +-ENOTTY being returned from the system call. Some subsystems return +-ENOSYS or -EINVAL here for historic reasons, but this is wrong. + +Prior to Linux-5.5, compat_ioctl handlers were required to return
Space instead of hyphen.
Quoted 4 lines
+-ENOIOCTLCMD in order to use the fallback conversion into native
Documentation/core-api/ioctl.rst new file +3
-0+commands. As all subsystems are now responsible for handling compat +mode themselves, this is no longer needed, but it may be important to +consider when backporting bug fixes to older kernels.
Documentation/core-api/ioctl.rst line 89
Quoted 10 lines
Documentation/core-api/ioctl.rst new file +10
-0+ +Timestamps +========== + +Traditionally, timestamps and timeout values are passed as ``struct +timespec`` or ``struct timeval``, but these are problematic because of +incompatible definitions of these structures in user space after the +move to 64-bit time_t. + +The __kernel_timespec type can be used instead to be embedded in other
It's not a typedef, so ``struct __kernel_timespec``.
[...]
Quoted 5 lines
+32-bit compat mode
+==================Documentation/core-api/ioctl.rst new file +3
-0+ +In order to support 32-bit user space running on a 64-bit machine, each +subsystem or driver that implements an ioctl callback handler must also
Documentation/core-api/ioctl.rst line 123
Quoted 10 lines
Documentation/core-api/ioctl.rst new file +10
-0+implement the corresponding compat_ioctl handler. + +As long as all the rules for data structures are followed, this is as +easy as setting the .compat_ioctl pointer to a helper function such as +compat_ptr_ioctl() or blkdev_compat_ptr_ioctl(). + +compat_ptr() +------------ + +On the s/390 architecture, 31-bit user space has ambiguous representations
IBM never used the name "S/390" for the 64-bit mainframe architecture, but they have rebranded it several times. Rather than trying to follow what it's called this year, maybe just write "s390" to match what we usually call it?
[...]
Quoted 24 lines
Documentation/core-api/ioctl.rst new file +24
-0+Structure layout +---------------- + +Compatible data structures have the same layout on all architectures, +avoiding all problematic members: + +* ``long`` and ``unsigned long`` are the size of a register, so + they can be either 32-bit or 64-bit wide and cannot be used in portable + data structures. Fixed-length replacements are ``__s32``, ``__u32``, + ``__s64`` and ``__u64``. + +* Pointers have the same problem, in addition to requiring the + use of compat_ptr(). The best workaround is to use ``__u64`` + in place of pointers, which requires a cast to ``uintptr_t`` in user + space, and the use of u64_to_user_ptr() in the kernel to convert + it back into a user pointer. + +* On the x86-32 (i386) architecture, the alignment of 64-bit variables + is only 32-bit, but they are naturally aligned on most other + architectures including x86-64. This means a structure like:: + + struct foo { + __u32 a; + __u64 b;
Documentation/core-api/ioctl.rst line 173
Quoted 10 lines
Documentation/core-api/ioctl.rst new file +10
-0+ __u32 c; + }; + + has four bytes of padding between a and b on x86-64, plus another four + bytes of padding at the end, but no padding on i386, and it needs a + compat_ioctl conversion handler to translate between the two formats. + + To avoid this problem, all structures should have their members + naturally aligned, or explicit reserved fields added in place of the + implicit padding.
This should explain how to check that - presumably by running pahole on some sensible architecture.
Documentation/core-api/ioctl.rst line 176
Quoted 2 lines
Documentation/core-api/ioctl.rst new file +2
-0+* On ARM OABI user space, 16-bit member variables have 32-bit + alignment, making them incompatible with modern EABI kernels.
I thought that OABI required structures as a whole to have alignment of 4, not individual members? Which obviously does affect small structures as members of other structures.
[...]
Documentation/core-api/ioctl.rst line 199
Quoted 8 lines
Documentation/core-api/ioctl.rst new file +8
-0+Information leaks +================= + +Uninitialized data must not be copied back to user space, as this can +cause an information leak, which can be used to defeat kernel address +space layout randomization (KASLR), helping in an attack. + +As explained for the compat mode, it is best to not avoid any implicit
Delete "not".
Documentation/core-api/ioctl.rst line 202
Quoted 3 lines
Documentation/core-api/ioctl.rst new file +3
-0+padding in data structures, but if there is already padding in existing +structures, the kernel driver must be careful to zero out the padding +using memset() or similar before copying it to user space.
This sentence is rather too long. Also it can be read as suggesting that one should somehow identify and memset() the padding just before copying to user-space. I suggest an alternate wording:
For this reason (and for compat support) it is best to avoid any implicit padding in data structures. Where there is implicit padding in an existing structure, kernel drivers must be careful to fully initialize an instance of the structure before copying it to user space. This is usually done by calling memset() before assigning to individual members.
[...]
Quoted 2 lines
+Alternatives to ioctl
+=====================
[...]
Documentation/core-api/ioctl.rst line 248
Quoted 2 lines
Documentation/core-api/ioctl.rst new file +2
-0+* A custom file system can provide extra flexibility with a simple + user interface but add a lot of complexity to the implementation.
Typo: "add" should be "adds".
Anyway, it's great to have documentation for this all in one place.
Ben.
Signature · 3 lines
Ben Hutchings, Software Developer Codethink Ltd
https://www.codethink.co.uk/ Dale House, 35 Dale Street
Manchester, M1 2HF, United KingdomRe: [PATCH v2 27/27] Documentation: document ioctl interfaces better
Quoted 8 lines · Ben Hutchings
Documentation/core-api/ioctl.rst line 24
Quoted 5 lines · Arnd Bergmann
Documentation/core-api/ioctl.rst new file +3
-0+``include/uapi/asm-generic/ioctl.h`` provides four macros for defining +ioctl commands that follow modern conventions: ``_IO``, ``_IOR``, +``_IOW``, and ``_IORW``. These should be used for all new commands,Typo: "
_IORW" should be "_IOWR".
Fixed now
Quoted 13 lines
Documentation/core-api/ioctl.rst line 31
Quoted 7 lines
Documentation/core-api/ioctl.rst new file +7
-0+with the correct parameters: + +_IO/_IOR/_IOW/_IOWR + The macro name determines whether the argument is used for passing + data into kernel (_IOW), from the kernel (_IOR), both (_IOWR) or is + not a pointer (_IO). It is possible but not recommended to pass an + integer value instead of a pointer with _IO.I feel the explanation of
_IOhere could be confusing. I think what you meant to say was that it is possible, but not recommended, to pass integers directly (arg is integer) rather than indirectly (arg is pointer to integer). I suggest the alternate wording:
Quoted 4 lines
The macro name specifies how the argument will be used. It may be a pointer to data to be passed into the kernel (
_IOW), out of the kernel (_IOR), or both (_IOWR). The argument may also be an integer value instead of a pointer (_IO), but this is not recommended.
That's probably better than my version, but I find that misleading as well:
it sounds like _IO() is not recommended, but having no argument with _IO() is actually fine. This is what I have now:
The macro name specifies how the argument will be used. It may be a pointer to data to be passed into the kernel (_IOW), out of the kernel (_IOR), or both (_IOWR). _IO can indicate either commands with no argument or those passing an integer value instead of a pointer. It is recommended to only use _IO for commands without arguments, and use pointers for passing data.
Quoted 9 lines
Documentation/core-api/ioctl.rst line 46
Quoted 6 lines
Documentation/core-api/ioctl.rst new file +6
-0+data_type + The name of the data type pointed to by the argument, the command number + encodes the ``sizeof(data_type)`` value in a 13-bit or 14-bit integer, + leading to a limit of 8191 bytes for the maximum size of the argument. + Note: do not pass sizeof(data_type) type into _IOR/IOW, as that will + lead to encoding sizeof(sizeof(data_type)), i.e. sizeof(size_t).You left out
_IOWRhere. It might also be worth mentioning that_IOdoesn't have this parameter.
Changed now.
Quoted 8 lines
[...]
Documentation/core-api/ioctl.rst
Quoted 5 lines
Documentation/core-api/ioctl.rst new file +5
-0+Return code +=========== + +ioctl commands can return negative error codes as documented in errno(3), +these get turned into errno values in user space.Use a semi-colon instead of a comma, or change "these" to "which".
done
Quoted 4 lines
Quoted 4 lines
On success, the return
Documentation/core-api/ioctl.rst new file +3
-0+code should be zero. It is also possible but not recommended to return +a positive 'long' value. +
Quoted 8 lines
Documentation/core-api/ioctl.rst line 75
Quoted 6 lines
Documentation/core-api/ioctl.rst new file +6
-0+When the ioctl callback is called with an unknown command number, the +handler returns either -ENOTTY or -ENOIOCTLCMD, which also results in +-ENOTTY being returned from the system call. Some subsystems return +-ENOSYS or -EINVAL here for historic reasons, but this is wrong. + +Prior to Linux-5.5, compat_ioctl handlers were required to returnSpace instead of hyphen.
done
Quoted 8 lines
Quoted 8 lines
+-ENOIOCTLCMD in order to use the fallback conversion into native
Documentation/core-api/ioctl.rst new file +7
-0+commands. As all subsystems are now responsible for handling compat +mode themselves, this is no longer needed, but it may be important to +consider when backporting bug fixes to older kernels. + +Timestamps +========== +
Quoted 8 lines
Documentation/core-api/ioctl.rst line 89
Quoted 6 lines
Documentation/core-api/ioctl.rst new file +6
-0+Traditionally, timestamps and timeout values are passed as ``struct +timespec`` or ``struct timeval``, but these are problematic because of +incompatible definitions of these structures in user space after the +move to 64-bit time_t. + +The __kernel_timespec type can be used instead to be embedded in otherIt's not a typedef, so ``struct __kernel_timespec``.
done
Quoted 13 lines
[...]
Quoted 12 lines
+32-bit compat mode
+==================Documentation/core-api/ioctl.rst new file +10
-0+ +In order to support 32-bit user space running on a 64-bit machine, each +subsystem or driver that implements an ioctl callback handler must also +implement the corresponding compat_ioctl handler. + +As long as all the rules for data structures are followed, this is as +easy as setting the .compat_ioctl pointer to a helper function such as +compat_ptr_ioctl() or blkdev_compat_ptr_ioctl(). + +compat_ptr()
Quoted 8 lines
Documentation/core-api/ioctl.rst
Quoted 3 lines
+------------
Documentation/core-api/ioctl.rst new file +2
-0+ +On the s/390 architecture, 31-bit user space has ambiguous representationsIBM never used the name "S/390" for the 64-bit mainframe architecture, but they have rebranded it several times. Rather than trying to follow what it's called this year, maybe just write "s390" to match what we usually call it?
ok, done
Quoted 11 lines
Documentation/core-api/ioctl.rst line 173
Quoted 8 lines
Documentation/core-api/ioctl.rst new file +8
-0+ + has four bytes of padding between a and b on x86-64, plus another four + bytes of padding at the end, but no padding on i386, and it needs a + compat_ioctl conversion handler to translate between the two formats. + + To avoid this problem, all structures should have their members + naturally aligned, or explicit reserved fields added in place of the + implicit padding.This should explain how to check that - presumably by running pahole on some sensible architecture.
Ok, added "The ``pahole`` tool can be used for checking the alignment.".
Quoted 6 lines
Documentation/core-api/ioctl.rst line 176
Quoted 2 lines
Documentation/core-api/ioctl.rst new file +2
-0+* On ARM OABI user space, 16-bit member variables have 32-bit + alignment, making them incompatible with modern EABI kernels.I thought that OABI required structures as a whole to have alignment of 4, not individual members? Which obviously does affect small structures as members of other structures.
You are right, I clearly misunderstood that. Changed the paragraph now to
* On ARM OABI user space, structures are padded to multiples of 32-bit, making some structs incompatible with modern EABI kernels if they do not end on a 32-bit boundary.
* On the m68k architecture, struct members are not guaranteed to have an alignment greater than 16-bit, which is a problem when relying on implicit padding.
Quoted 11 lines
[...]
Documentation/core-api/ioctl.rst line 199
Quoted 8 lines
Documentation/core-api/ioctl.rst new file +8
-0+Information leaks +================= + +Uninitialized data must not be copied back to user space, as this can +cause an information leak, which can be used to defeat kernel address +space layout randomization (KASLR), helping in an attack. + +As explained for the compat mode, it is best to not avoid any implicitDelete "not".
Done.
Quoted 8 lines
+padding in data structures, but if there is already padding in existing
Documentation/core-api/ioctl.rst line 202
Quoted 2 lines
Documentation/core-api/ioctl.rst new file +2
-0+structures, the kernel driver must be careful to zero out the padding +using memset() or similar before copying it to user space.This sentence is rather too long. Also it can be read as suggesting that one should somehow identify and
memset()the padding just before copying to user-space. I suggest an alternate wording:
Quoted 6 lines
For this reason (and for compat support) it is best to avoid any implicit padding in data structures. Where there is implicit padding in an existing structure, kernel drivers must be careful to fully initialize an instance of the structure before copying it to user space. This is usually done by calling
memset()before assigning to individual members.
Sounds good, I've taken that paragraph now.
Quoted 8 lines
[...]
Quoted 2 lines
+Alternatives to ioctl
+=====================[...]
Documentation/core-api/ioctl.rst line 248
Quoted 2 lines
Documentation/core-api/ioctl.rst new file +2
-0+* A custom file system can provide extra flexibility with a simple + user interface but add a lot of complexity to the implementation.Typo: "add" should be "adds".
Fixed
Thanks for all the good suggestions!
Arnd
Quoted 6 lines · Arnd Bergmann
My plan was originally to keep the SCSI and block parts separate. This did not work easily because of interdependencies: I cannot do the final SCSI cleanup in a good way without first addressing the CDROM ioctls, so this is one series that I hope could be merged through either the block or the scsi git trees, or possibly both if you can pull in the same branch.
I have included the branch in my y2038 branch now, it should show up in the following linux-next snapshots, but I'm still hoping for the block or scsi maintainers to merge the pull request into their trees for v5.6
Jens, James, Martin:
Any suggestion for how this should be merged?
Arnd
Hi Arnd!
Quoted 1 line
Any suggestion for how this should be merged?
I'm pretty flexible. When you post v3 I'll try to set up a branch to get an idea what conflicts we might have to deal with. And then we can take it from there.
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering