Skip to contentSkip to conversation

ioctl_list.2: BLKRASET/BLKRAGET take unsigned long

PATCH In review Cyril Hrubis 3 participants 9 messages Feb 15, 2017 – Apr 10, 2017 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ioctl_list.2: BLKRASET/BLKRAGET take unsigned long

The BLKRASET/BLKRAGET ioctls() take unsigned long, if I pass int * to the BLKRAGET ioctl on x86_64 (or on any other arch where sizeof(int) != sizeof(long)) the BLKRAGET ioctl will rewrite four bytes on the stack.

If you look at block/ioctl.c in kernel sources you can clearly see that BLKRAGET ioctl calls put_long().

Compile following reproducer and run it as ./a.out /dev/sda, you can see that the second member of the array will be zeroed. If you change the array to have only one member you will see stack smashing trace.

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

-------------------------8<---------------------------- #include <sys/mount.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>

static int fd;

int main(int argc, char *argv[])

{
	int ra[] = {100, 100};

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ioctl(fd, BLKRAGET, ra);

	fprintf(stderr, "%i %i\n", ra[0], ra[1]);

	return 0;
}

-------------------------8<----------------------------

---
 man2/ioctl_list.2 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man2/ioctl_list.2 +2 -2
index 0165c77..c8efd66 100644
@@ -311,8 +311,8 @@ l l l l.
 0x0000125F	BLKRRPART	void
 0x00001260	BLKGETSIZE	unsigned long *
 0x00001261	BLKFLSBUF	void
-0x00001262	BLKRASET	int
-0x00001263	BLKRAGET	int *
+0x00001262	BLKRASET	unsigned long
+0x00001263	BLKRAGET	unsigned long *
 0x00000001	FIBMAP	int *	// I-O
 0x00000002	FIGETBSZ	int *
 0x80086601	FS_IOC_GETFLAGS	int *
Signature · 10 lines
2.10.2


-- 
Cyril Hrubis
[email protected]
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

[CCing linux api as well]

Quoted 69 lines

The BLKRASET/BLKRAGET ioctls() take unsigned long, if I pass int * to the BLKRAGET ioctl on x86_64 (or on any other arch where sizeof(int) != sizeof(long)) the BLKRAGET ioctl will rewrite four bytes on the stack.

If you look at block/ioctl.c in kernel sources you can clearly see that BLKRAGET ioctl calls put_long().

Compile following reproducer and run it as ./a.out /dev/sda, you can see that the second member of the array will be zeroed. If you change the array to have only one member you will see stack smashing trace.

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

-------------------------8<---------------------------- #include <sys/mount.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>

static int fd;

int main(int argc, char *argv[])

{
	int ra[] = {100, 100};

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ioctl(fd, BLKRAGET, ra);

	fprintf(stderr, "%i %i\n", ra[0], ra[1]);

	return 0;
}

-------------------------8<----------------------------

---
 man2/ioctl_list.2 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man2/ioctl_list.2 +2 -2
index 0165c77..c8efd66 100644
@@ -311,8 +311,8 @@ l l l l.
 0x0000125F	BLKRRPART	void
 0x00001260	BLKGETSIZE	unsigned long *
 0x00001261	BLKFLSBUF	void
-0x00001262	BLKRASET	int
-0x00001263	BLKRAGET	int *
+0x00001262	BLKRASET	unsigned long
+0x00001263	BLKRAGET	unsigned long *
 0x00000001	FIBMAP	int *	// I-O
 0x00000002	FIGETBSZ	int *
 0x80086601	FS_IOC_GETFLAGS	int *
Signature · 6 lines
2.10.2


-- 
Cyril Hrubis
[email protected]
Signature · 2 lines
Cyril Hrubis
[email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 7 lines · Cyril Hrubis

[CCing linux api as well]

Quoted 6 lines

The BLKRASET/BLKRAGET ioctls() take unsigned long, if I pass int * to the BLKRAGET ioctl on x86_64 (or on any other arch where sizeof(int) != sizeof(long)) the BLKRAGET ioctl will rewrite four bytes on the stack.

If you look at block/ioctl.c in kernel sources you can clearly see that BLKRAGET ioctl calls put_long().

BLKFRAGET as well, fwiw, see compat_blkdev_ioctl(). We might as well include them in the list.

Quoted 3 lines
Quoted 3 lines

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

This shouldn't matter, if you pass an 'int' into a function that takes a 'long', it will be extended if necessary. The question is more about how it gets interpreted, and in this case it's done by assigning to

struct backing_dev_info {
...
unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
...
};

Large values will not be used in practice (these are capped by the I/O sizes), but I guess you can set a value larger than UINT_MAX and read it back later.

Quoted 18 lines
Quoted 8 lines
 man2/ioctl_list.2 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man2/ioctl_list.2 +0 -0
index 0165c77..c8efd66 100644

man2/ioctl_list.2 l l l l. · line 318

Quoted 10 lines
man2/ioctl_list.2 +2 -2
@@ -311,8 +311,8 @@ l l l l.
 0x0000125F   BLKRRPART       void
 0x00001260   BLKGETSIZE      unsigned long *
 0x00001261   BLKFLSBUF       void
-0x00001262   BLKRASET        int
-0x00001263   BLKRAGET        int *
+0x00001262   BLKRASET        unsigned long
+0x00001263   BLKRAGET        unsigned long *
 0x00000001   FIBMAP  int *   // I-O
 0x00000002   FIGETBSZ        int *
 0x80086601   FS_IOC_GETFLAGS int *

Looks correct to me.

Arnd
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAK8P3a0KzhqFZqvPH4q7_Nb6+TMmhCWXDi_-wQG=mi-1U=Ccxw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw

Hi!

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

This shouldn't matter, if you pass an 'int' into a function that takes a 'long', it will be extended if necessary. The question is more about how it gets interpreted, and in this case it's done by assigning to

I guess that it may, since the ioctl() prototype is defined with ...

Quoted 1 line

From /usr/include/sys/ioctl.h:

...
extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
...

Looking at dissasembly we do:

4005bb: 89 c7 mov %eax,%edi
(%eax holds fd from open())

...

4005bb:       89 c7                   mov    %eax,%edi
4005bd:       ba 00 02 00 00          mov    $0x200,%edx

^
We do 32bit load here

4005c2:       be 62 12 00 00          mov    $0x1262,%esi
4005c7:       31 c0                   xor    %eax,%eax
4005c9:       e8 72 ff ff ff          callq  400540 <ioctl@plt>

The ioctl assembly just sets %eax and then syscall.

So as far as I can tell we depend here on a fact that upper part of $rdx is zeroed. Not sure if that is guaranteed or not.

Signature · 6 lines
Cyril Hrubis
[email protected]
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

Hi!

Quoted 2 lines

So as far as I can tell we depend here on a fact that upper part of $rdx is zeroed. Not sure if that is guaranteed or not.

Which seems to be the case for x86_64 processors, so passing int value to ioctl() will work fine there. Not sure about the rest of 64bit archs.

Signature · 6 lines
Cyril Hrubis
[email protected]
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw
In reply to a message not in the archive
Quoted 42 lines · Arnd Bergmann
Quoted 7 lines · Cyril Hrubis

[CCing linux api as well]

Quoted 6 lines

The BLKRASET/BLKRAGET ioctls() take unsigned long, if I pass int * to the BLKRAGET ioctl on x86_64 (or on any other arch where sizeof(int) != sizeof(long)) the BLKRAGET ioctl will rewrite four bytes on the stack.

If you look at block/ioctl.c in kernel sources you can clearly see that BLKRAGET ioctl calls put_long().

BLKFRAGET as well, fwiw, see compat_blkdev_ioctl(). We might as well include them in the list.

Quoted 3 lines
Quoted 3 lines

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

This shouldn't matter, if you pass an 'int' into a function that takes a 'long', it will be extended if necessary. The question is more about how it gets interpreted, and in this case it's done by assigning to

struct backing_dev_info {
...
unsigned long ra_pages; /* max readahead in PAGE_SIZE units */
...
};

Large values will not be used in practice (these are capped by the I/O sizes), but I guess you can set a value larger than UINT_MAX and read it back later.

Quoted 12 lines
Quoted 12 lines
 man2/ioctl_list.2 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man2/ioctl_list.2 +0 -1
index 0165c77..c8efd66 100644
@@ -311,8 +311,8 @@ l l l l.
 0x0000125F   BLKRRPART       void
 0x00001260   BLKGETSIZE      unsigned long *
 0x00001261   BLKFLSBUF       void
-0x00001262   BLKRASET        int
Quoted 8 lines
Quoted 6 lines

man2/ioctl_list.2 l l l l. · line 318

Quoted 6 lines
man2/ioctl_list.2 +2 -1
-0x00001263   BLKRAGET        int *
+0x00001262   BLKRASET        unsigned long
+0x00001263   BLKRAGET        unsigned long *
 0x00000001   FIBMAP  int *   // I-O
 0x00000002   FIGETBSZ        int *
 0x80086601   FS_IOC_GETFLAGS int *

Looks correct to me.

Thanks for reviewing this, Arnd.

Cheers,

Michael

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw
In reply to a message not in the archive
Quoted 7 lines · Cyril Hrubis

The BLKRASET/BLKRAGET ioctls() take unsigned long, if I pass int * to the BLKRAGET ioctl on x86_64 (or on any other arch where sizeof(int) != sizeof(long)) the BLKRAGET ioctl will rewrite four bytes on the stack.

If you look at block/ioctl.c in kernel sources you can clearly see that BLKRAGET ioctl calls put_long().

Quoted 7 lines

Compile following reproducer and run it as ./a.out /dev/sda, you can see that the second member of the array will be zeroed. If you change the array to have only one member you will see stack smashing trace.

I also wonder if it's OK to pass int value to ioctl() at all, the arg value seems to be unsigned long in the syscall definition in fs/ioctl.c and there does not seem to be any glibc magic around the syscall.

Thanks Cyril. Applied now. Sorry for the delayed response...

Cheers,

Michael

Quoted 48 lines

-------------------------8<---------------------------- #include <sys/mount.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>

static int fd;

int main(int argc, char *argv[])

{
	int ra[] = {100, 100};

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ioctl(fd, BLKRAGET, ra);

	fprintf(stderr, "%i %i\n", ra[0], ra[1]);

	return 0;
}

-------------------------8<----------------------------

---
 man2/ioctl_list.2 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
man2/ioctl_list.2 +2 -2
index 0165c77..c8efd66 100644
@@ -311,8 +311,8 @@ l l l l.
 0x0000125F	BLKRRPART	void
 0x00001260	BLKGETSIZE	unsigned long *
 0x00001261	BLKFLSBUF	void
-0x00001262	BLKRASET	int
-0x00001263	BLKRAGET	int *
+0x00001262	BLKRASET	unsigned long
+0x00001263	BLKRAGET	unsigned long *
 0x00000001	FIBMAP	int *	// I-O
 0x00000002	FIGETBSZ	int *
 0x80086601	FS_IOC_GETFLAGS	int *
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/