Skip to contentSkip to conversation

use size_t for the broken ioctl numbers

PATCH Arnd Bergmann 4 participants 6 messages Sep 7, 2003 – Sep 8, 2003 2 messages not in the archive

lore
Share & tools
lore

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

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

#2 lore raw
Quoted 10 lines · Andreas Schwab
Quoted 5 lines · Linus Torvalds

Ie, change the (sizeof(x)) to something like

     ({ x __dummy; sizeof(__dummy); })

which should work with all compiler versions.

This won't work with array types, eg. in <linux/random.h>:

#define RNDGETPOOL _IOR( 'R', 0x02, int [2] )

How about changing (sizeof(x)) to (sizeof(x[1]))? It will result in "parse error before `['" when x is not a type or an array type.

        Arnd <><
#3 lore raw
Quoted 4 lines · Arnd Bergmann

How about changing (sizeof(x)) to (sizeof(x[1]))? It will result in "parse error before `['" when x is not a type or an array type.

That sounds like a clever thing to do. Have you tested it with a full configuration?

Linus

#4 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Arnd Bergmann

How about changing (sizeof(x)) to (sizeof(x[1]))? It will result in "parse error before `['" when x is not a type or an array type.

That sounds like a clever thing to do. Have you tested it with a full configuration?

In fact, what you'd want to do is not just verify that it compiles, but also verify that the object code matches.

Because there _is_ one case where adding the [1] will still compile, but generate wrong code: if the "size" argument to the _IOx() was not a type, but a real actual array.

Now, that would have been a bug (or at least a misfeature) before too, but the point of this whole thread was that some people used the _IOx() macros incorrectly, so maybe such broken usage actually exists..

Linus

#5 lore raw
Quoted 2 lines · Linus Torvalds

In fact, what you'd want to do is not just verify that it compiles, but also verify that the object code matches.

I have checked now that the object code for arch/s390/kernel/compat_ioctl.o remains identical and that the whole kernel compiles for s390 and i386, after fixing the broken ioctl numbers.

Quoted 3 lines

Because there _is_ one case where adding the [1] will still compile, but generate wrong code: if the "size" argument to the _IOx() was not a type, but a real actual array.

Yes, there had to be a catch. The new version below catches that error too, making that a link time failure and also checks that the size field does not overflow.

	Arnd <><
include/asm-i386/ioctl.h +13 -3
@@ -52,11 +52,21 @@
 	 ((nr)   << _IOC_NRSHIFT) | \
 	 ((size) << _IOC_SIZESHIFT))
 
+/* provoke compile error for invalid uses of size argument */
+extern int __invalid_size_argument_for_IOC;
+#define _IOC_TYPECHECK(t) \
+	((sizeof(t) == sizeof(t[1]) && \
+	  sizeof(t) < (1 << _IOC_SIZEBITS)) ? \
+	  sizeof(t) : __invalid_size_argument_for_IOC)
+
 /* used to create numbers */
 #define _IO(type,nr)		_IOC(_IOC_NONE,(type),(nr),0)
-#define _IOR(type,nr,size)	_IOC(_IOC_READ,(type),(nr),sizeof(size))
-#define _IOW(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),sizeof(size))
-#define _IOWR(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
+#define _IOR(type,nr,size)	_IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOW(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOWR(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOR_BAD(type,nr,size)	_IOC(_IOC_READ,(type),(nr),sizeof(size))
+#define _IOW_BAD(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),sizeof(size))
+#define _IOWR_BAD(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
 
 /* used to decode ioctl numbers.. */
 #define _IOC_DIR(nr)		(((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
#6 lore raw

include/asm-i386/ioctl.h line 69

Quoted 6 lines · Arnd Bergmann
include/asm-i386/ioctl.h +6 -0
+#define _IOR(type,nr,size)	_IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOW(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOWR(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
+#define _IOR_BAD(type,nr,size)	_IOC(_IOC_READ,(type),(nr),sizeof(size))
+#define _IOW_BAD(type,nr,size)	_IOC(_IOC_WRITE,(type),(nr),sizeof(size))
+#define _IOWR_BAD(type,nr,size)	_IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))

Yes, good.
Then I have another trivial request: change the identifier used for the third parameter. Since it is called "size" people think that it is a size.
The new checking will hit them, but still, it would be good to use
the correct identifiers. What about "argtype"?

Andries

#7 lore raw
In reply to Arnd Bergmann

include/asm-i386/ioctl.h line 60

Quoted 12 lines · Arnd Bergmann
include/asm-i386/ioctl.h +6 -0
@@ -52,11 +52,21 @@
 	 ((nr)   << _IOC_NRSHIFT) | \
 	 ((size) << _IOC_SIZESHIFT))
 
+/* provoke compile error for invalid uses of size argument */
+extern int __invalid_size_argument_for_IOC;
+#define _IOC_TYPECHECK(t) \
+	((sizeof(t) == sizeof(t[1]) && \
+	  sizeof(t) < (1 << _IOC_SIZEBITS)) ? \
+	  sizeof(t) : __invalid_size_argument_for_IOC)

This will fail when compiled unoptimized, which means that glibc could not use <asm/ioctls.h> any more.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."