linux-user: ioctl() command type is int
PATCH 1/1 linux-user: ioctl() command type is int
When executing a 64bit target chroot on 64bit host, the ioctl() command can mismatch.
It seems the previous commit doesn't solve the problem in my case:
9c6bf9c7 linux-user: Fix ioctl cmd type mismatch on 64-bit targets
For example, a ppc64 chroot on an x86_64 host:
bash-4.3# ls
Unsupported ioctl: cmd=0x80087467
Unsupported ioctl: cmd=0x802c7415
The origin of the problem is in syscall.c:do_ioctl().
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
In this case (ppc64) abi_long is long (on the x86_64), and
cmd = 0x0000000080087467
then
if (ie->target_cmd == cmd)
target_cmd is int, so target_cmd = 0x80087467
and to compare an int with a long, the sign is extended to 64bit, so the comparison is:
if (0xffffffff80087467 == 0x0000000080087467)
which doesn't match whereas it should.
This patch uses abi_int in the case of the target command type instead of abi_long (and for consistency, update IOCTLEntry).
--- linux-user/syscall.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
linux-user/syscall.c +3 -3
@@ -3293,8 +3293,8 @@ typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, abi_long cmd, abi_long arg);struct IOCTLEntry { -int target_cmd; -unsigned int host_cmd; +abi_int target_cmd; +int host_cmd;const char *name;int access;do_ioctl_fn *do_ioctl;
@@ -3849,7 +3849,7 @@ static IOCTLEntry ioctl_entries[] = {/* ??? Implement proper locking for ioctls. */ /* do_ioctl() Must return target values and target errnos. */ -static abi_long do_ioctl (int fd,abi_long cmd,abi_long arg) +static abi_long do_ioctl (int fd,abi_int cmd,abi_long arg) {const IOCTLEntry *ie;const argtype *arg_type;
Signature · 1 line
2.1.4
Quoted 29 lines · Laurent Vivier
When executing a 64bit target chroot on 64bit host, the
ioctl()command can mismatch.It seems the previous commit doesn't solve the problem in my case:
9c6bf9c7linux-user: Fix ioctl cmd type mismatch on 64-bit targetsFor example, a ppc64 chroot on an
x86_64host:bash-4.3# ls
Unsupported ioctl: cmd=0x80087467
Unsupported ioctl: cmd=0x802c7415The origin of the problem is in syscall.c:do_ioctl().
static
abi_longdo_ioctl(int fd, abi_long cmd, abi_long arg)In this case (ppc64)
abi_longis long (on thex86_64), andcmd = 0x0000000080087467then
if (ie->target_cmd == cmd)
target_cmdis int, sotarget_cmd=0x80087467
and to compare an int with a long, the sign is extended to 64bit, so the comparison is:
Quoted 7 lines
if (0xffffffff80087467 == 0x0000000080087467)which doesn't match whereas it should.
This patch uses
abi_intin the case of the target command type instead ofabi_long(and for consistency, update IOCTLEntry).
abi_int is really only needed in guest-layout structure declarations, since it has alignment attributes but is otherwise guaranteed to be an int32_t (and we assume all over the place that int is 32 bits, so int is ok too). So it's unnecessary in the function prototypes, and I think actively harmful in the IOCTLEntry struct (since that struct is not a shared-with-guest one).
I think also the do_ioctl_fn() prototype and all the do_ioctl_* functions which are of that type need to have the cmd parameter switched from abi_long. This avoids potentially running into the same problem inside a do_ioctl function if it does comparisons on the cmd.
thanks
-- PMM
PATCH v2 1/1 linux-user: ioctl() command type is int
In reply to Laurent VivierWhen executing a 64bit target chroot on 64bit host, the ioctl() command can mismatch.
It seems the previous commit doesn't solve the problem in my case:
9c6bf9c7 linux-user: Fix ioctl cmd type mismatch on 64-bit targets
For example, a ppc64 chroot on an x86_64 host:
bash-4.3# ls
Unsupported ioctl: cmd=0x80087467
Unsupported ioctl: cmd=0x802c7415
The origin of the problem is in syscall.c:do_ioctl().
static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
In this case (ppc64) abi_long is long (on the x86_64), and
cmd = 0x0000000080087467
then
if (ie->target_cmd == cmd)
target_cmd is int, so target_cmd = 0x80087467
and to compare an int with a long, the sign is extended to 64bit, so the comparison is:
if (0xffffffff80087467 == 0x0000000080087467)
which doesn't match whereas it should.
This patch uses int in the case of the target command type instead of abi_long (and for consistency, update IOCTLEntry).
---
v2: use int instead of abi_int, as it is recommended by Peter Maydell.
linux-user/syscall.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
linux-user/syscall.c +9 -9
@@ -3290,11 +3290,11 @@ enum {typedef struct IOCTLEntry IOCTLEntry ;typedef abi_long do_ioctl_fn (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg); +int fd,int cmd,abi_long arg);struct IOCTLEntry {int target_cmd; -unsigned int host_cmd; +int host_cmd;const char *name;int access;do_ioctl_fn *do_ioctl;
@@ -3316,7 +3316,7 @@ struct IOCTLEntry { / sizeof(struct fiemap_extent))static abi_long do_ioctl_fs_ioc_fiemap (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {/* The parameter for this ioctl is a struct fiemap followed * by an array of struct fiemap_extent whose size is set
@@ -3397,7 +3397,7 @@ static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,#endif static abi_long do_ioctl_ifconf (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {const argtype *arg_type = ie->arg_type;int target_size;
@@ -3491,7 +3491,7 @@ static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, }static abi_long do_ioctl_dm (const IOCTLEntry *ie,uint8_t *buf_temp,int fd, -abi_long cmd,abi_long arg) +int cmd,abi_long arg) {void *argptr;struct dm_ioctl *host_dm;
@@ -3716,7 +3716,7 @@ out: }static abi_long do_ioctl_blkpg (const IOCTLEntry *ie,uint8_t *buf_temp,int fd, -abi_long cmd,abi_long arg) +int cmd,abi_long arg) {void *argptr;int target_size;
@@ -3769,7 +3769,7 @@ out: }static abi_long do_ioctl_rt (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {const argtype *arg_type = ie->arg_type;const StructEntry *se;
@@ -3832,7 +3832,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, }static abi_long do_ioctl_kdsigaccept (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {int sig =target_to_host_signal (arg);return get_errno (ioctl (fd, ie->host_cmd, sig));
@@ -3849,7 +3849,7 @@ static IOCTLEntry ioctl_entries[] = {/* ??? Implement proper locking for ioctls. */ /* do_ioctl() Must return target values and target errnos. */ -static abi_long do_ioctl (int fd,abi_long cmd,abi_long arg) +static abi_long do_ioctl (int fd,int cmd,abi_long arg) {const IOCTLEntry *ie;const argtype *arg_type;
Signature · 1 line
2.1.4
Quoted 29 lines · Laurent Vivier
When executing a 64bit target chroot on 64bit host, the
ioctl()command can mismatch.It seems the previous commit doesn't solve the problem in my case:
9c6bf9c7linux-user: Fix ioctl cmd type mismatch on 64-bit targetsFor example, a ppc64 chroot on an
x86_64host:bash-4.3# ls
Unsupported ioctl: cmd=0x80087467
Unsupported ioctl: cmd=0x802c7415The origin of the problem is in syscall.c:do_ioctl().
static
abi_longdo_ioctl(int fd, abi_long cmd, abi_long arg)In this case (ppc64)
abi_longis long (on thex86_64), andcmd = 0x0000000080087467then
if (ie->target_cmd == cmd)
target_cmdis int, sotarget_cmd=0x80087467
and to compare an int with a long, the sign is extended to 64bit, so the comparison is:
Quoted 8 lines
if (0xffffffff80087467 == 0x0000000080087467)which doesn't match whereas it should.
This patch uses int in the case of the target command type instead of
abi_long(and for consistency, update IOCTLEntry).Signed-off-by Laurent Vivier <[email protected]>
thanks
-- PMM
Riku Voipio
AppliedQuoted 29 lines · Laurent Vivier
When executing a 64bit target chroot on 64bit host, the
ioctl()command can mismatch.It seems the previous commit doesn't solve the problem in my case:
9c6bf9c7linux-user: Fix ioctl cmd type mismatch on 64-bit targetsFor example, a ppc64 chroot on an
x86_64host:bash-4.3# ls
Unsupported ioctl: cmd=0x80087467
Unsupported ioctl: cmd=0x802c7415The origin of the problem is in syscall.c:do_ioctl().
static
abi_longdo_ioctl(int fd, abi_long cmd, abi_long arg)In this case (ppc64)
abi_longis long (on thex86_64), andcmd = 0x0000000080087467then
if (ie->target_cmd == cmd)
target_cmdis int, sotarget_cmd=0x80087467
and to compare an int with a long, the sign is extended to 64bit, so the comparison is:
Quoted 8 lines
if (0xffffffff80087467 == 0x0000000080087467)which doesn't match whereas it should.
This patch uses int in the case of the target command type instead of
abi_long(and for consistency, update IOCTLEntry).Signed-off-by Laurent Vivier <[email protected]>
Applied to linux-user que,
Thanks
Quoted 94 lines
---
v2: use int instead ofabi_int, as it is recommended by Peter Maydell.linux-user/syscall.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)linux-user/syscall.c +3
-3@@ -3290,11 +3290,11 @@ enum {typedef struct IOCTLEntry IOCTLEntry ;typedef abi_long do_ioctl_fn (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg); +int fd,int cmd,abi_long arg);struct IOCTLEntry {int target_cmd; -unsigned int host_cmd; +int host_cmd;const char *name;int access;do_ioctl_fn *do_ioctl;@@ -3316,7 +3316,7 @@ struct IOCTLEntry { / sizeof(struct fiemap_extent))static abi_long do_ioctl_fs_ioc_fiemap (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {/* The parameter for this ioctl is a struct fiemap followed * by an array of struct
fiemap_extentwhose size is setlinux-user/syscall.c +1
-1@@ -3397,7 +3397,7 @@ static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp, #endif static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, - int fd, abi_long cmd, abi_long arg) + int fd, int cmd, abi_long arg) { const argtype *arg_type = ie->arg_type;int target_size;linux-user/syscall.c +1
-1@@ -3491,7 +3491,7 @@ static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, }static abi_long do_ioctl_dm (const IOCTLEntry *ie,uint8_t *buf_temp,int fd, -abi_long cmd,abi_long arg) +int cmd,abi_long arg) {void *argptr; struct dm_ioctl *host_dm;linux-user/syscall.c +1
-1@@ -3716,7 +3716,7 @@ out: }static abi_long do_ioctl_blkpg (const IOCTLEntry *ie,uint8_t *buf_temp,int fd, -abi_long cmd,abi_long arg) +int cmd,abi_long arg) {void *argptr;int target_size;linux-user/syscall.c +2
-2@@ -3769,7 +3769,7 @@ out: }static abi_long do_ioctl_rt (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {const argtype *arg_type = ie->arg_type;const StructEntry *se;@@ -3832,7 +3832,7 @@ static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp, }static abi_long do_ioctl_kdsigaccept (const IOCTLEntry *ie,uint8_t *buf_temp, -int fd,abi_long cmd,abi_long arg) +int fd,int cmd,abi_long arg) {int sig = target_to_host_signal(arg); return get_errno(ioctl(fd, ie->host_cmd, sig));linux-user/syscall.c +1
-1@@ -3849,7 +3849,7 @@ static IOCTLEntry ioctl_entries[] = {/* ??? Implement proper locking for ioctls. */ /* do_ioctl() Must return target values and target errnos. */ -static abi_long do_ioctl (int fd,abi_long cmd,abi_long arg) +static abi_long do_ioctl (int fd,int cmd,abi_long arg) {const IOCTLEntry *ie;const argtype *arg_type;