Skip to contentSkip to conversation

video_usercopy() enforces change of VideoText IOCTLs since 2.6.8

DISCUSSION Michael Geng 4 participants 11 messages Oct 7, 2004 – Oct 13, 2004

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

Hello Gerd,

from kernel 2.6.7 to 2.6.8 function video_usercopy() in videodev.c was modified in a way that IOCTLs which do not use the _IO macros defined in linux/ioctl.h will get their driver's ioctl function called with arg = 0.

This means that the videotext drivers (saa5246a and saa5249) only produce segmentation faults in kernel version >= 2.6.8.

Shall we change the IOCTL definitions in include/linux/videotext.h? This would mean that existing userspace programs will have to be recompiled in order to work with the latest kernel version.

Nevertheless I suggest changing these IOCTLs to the new method. I will work out a patch if you agree.

Below is the change I'm talking about:

int
video_usercopy(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg,
int (*func)(struct inode *inode, struct file *file,
unsigned int cmd, void *arg))

{
	char	sbuf[128];
	void    *mbuf = NULL;
	void	*parg = NULL;
	int	err  = -EINVAL;

	cmd = video_fix_command(cmd);

	/*  Copy arguments into temp kernel buffer  */
	switch (_IOC_DIR(cmd)) {
	case _IOC_NONE:
===> 2.6.7:
		parg = (void *)arg;
===> 2.6.8:
		parg = NULL;
        ...

	/* call driver */
	err = func(inode, file, cmd, parg);

Bye,
Michael

#1 lore raw
Quoted 9 lines · Michael Geng

from kernel 2.6.7 to 2.6.8 function video_usercopy() in videodev.c was modified

case _IOC_NONE:
===> 2.6.7:

		parg = (void *)arg;
===> 2.6.8:
		parg = NULL;
        ...

Yes, that change is wrong. It breaks all ioctls which use arg to pass a integer value directly (i.e. not a pointer to a integer).

The patch below reverses it. Please apply,

Gerd

drivers/media/video/videodev.c +1 -1
Index: linux-2.6.8/drivers/media/video/videodev.c
===================================================================
@@ -183,7 +183,7 @@ video_usercopy(struct inode *inode, stru
 	/*  Copy arguments into temp kernel buffer  */
 	switch (_IOC_DIR(cmd)) {
 	case _IOC_NONE:
-		parg = NULL;
+		parg = (void*)arg;
 		break;
 	case _IOC_READ:
 	case _IOC_WRITE:
#2 lore raw
Quoted 13 lines · Gerd Knorr

Yes, that change is wrong. It breaks all ioctls which use arg to pass a integer value directly (i.e. not a pointer to a integer).

The patch below reverses it. Please apply,

Gerd

Index: linux-2.6.8/drivers/media/video/videodev.c
===================================================================

drivers/media/video/videodev.c video_usercopy(struct inode *inode, stru · line 187

Quoted 6 lines
drivers/media/video/videodev.c +1 -1
@@ -183,7 +183,7 @@ video_usercopy(struct inode *inode, stru
 	/*  Copy arguments into temp kernel buffer  */
 	switch (_IOC_DIR(cmd)) {
 	case _IOC_NONE:
-		parg = NULL;
+		parg = (void*)arg;
 		break;

(the typecast is unneeded)

Seems that with this change we are now sometimes passing a user pointer into (*func)(). And we're sometimes passing a kernel pointer, yes?

Are all the implementations of (*func)() handling that correctly?

It all looks fishy.

#3 lore raw
Quoted 11 lines

drivers/media/video/videodev.c video_usercopy(struct inode *inode, stru · line 187

Quoted 6 lines
drivers/media/video/videodev.c +1 -1
 	/*  Copy arguments into temp kernel buffer  */
 	switch (_IOC_DIR(cmd)) {
 	case _IOC_NONE:
-		parg = NULL;
+		parg = (void*)arg;
 		break;

(the typecast is unneeded)

Seems that with this change we are now sometimes passing a user pointer into (*func)(). And we're sometimes passing a kernel pointer, yes?

Assuming that ioctls passing _pointers_ are declared correctly with _IO* that shouldn't be the case: _IOC_DIR(cmd) == _IOC_NONE means _IO() means no pointer passed in.

Quoted 1 line

Are all the implementations of (*func)() handling that correctly?

Hmm, it broke for videotext, checking ...

Ok, you can drop it. The videotext ioctls (include/videotext.h) don't use the _IO*() macros but pass around pointers anyway, thats bad.

Michael, you'll have to fix the saa5246a driver. video_usercopy() will not work for you because the videotext ioctls doesn't use the _IO() macros. You have to do the userspace copying in the driver yourself.

Gerd

Signature · 1 line
return -ENOSIG;
#4 lore raw
Quoted 18 lines · Gerd Knorr
Quoted 11 lines

drivers/media/video/videodev.c video_usercopy(struct inode *inode, stru · line 187

Quoted 6 lines
drivers/media/video/videodev.c +1 -1
 	/*  Copy arguments into temp kernel buffer  */
 	switch (_IOC_DIR(cmd)) {
 	case _IOC_NONE:
-		parg = NULL;
+		parg = (void*)arg;
 		break;

(the typecast is unneeded)

Seems that with this change we are now sometimes passing a user pointer into (*func)(). And we're sometimes passing a kernel pointer, yes?

Assuming that ioctls passing _pointers_ are declared correctly with _IO* that shouldn't be the case: _IOC_DIR(cmd) == _IOC_NONE means _IO() means no pointer passed in.

Quoted 1 line

Are all the implementations of (*func)() handling that correctly?

Quoted 8 lines

Hmm, it broke for videotext, checking ...

Ok, you can drop it. The videotext ioctls (include/videotext.h) don't use the _IO*() macros but pass around pointers anyway, thats bad.

Michael, you'll have to fix the saa5246a driver. video_usercopy() will not work for you because the videotext ioctls doesn't use the _IO() macros. You have to do the userspace copying in the driver yourself.

I would prefer fixing the IOCTLs in include/linux/videotext.h. These definitions are older than the _IO macros in linux/ioctl.h AFAIK. So it is clear that they can't conform to that definition. I would prefer redefining them with respect to the arguments passed to the IOCTLs.

This would be a little step towards unifying the kernel. Implementing a private usercopy in saa5246a.c and saa5249.c would be the opposite.

The concept with the _IO() macros is good and helps preventing errors in the use of IOCTLs. So also the videotext drivers should use it.

Nevertheless there is one big disadvantage: The userspace programs have to be recompiled because they of course have to use the same IOCTL definitions.

Nevertheless, affected are only saa5246a.c and saa5249.c. I think that there are not many people left in the world using these drivers. So I think we can afford to make a break here.

Do you agree? If so then I will work out a patch.

Michael

#5 lore raw
Quoted 2 lines

I would prefer redefining them with respect
to the arguments passed to the IOCTLs.

Thats possible as well.

Quoted 3 lines

Nevertheless there is one big disadvantage: The userspace programs have to be recompiled because they of course have to use the same IOCTL definitions.

You can just keep the old ones and map them, so both old and new ones will work (and internally only the new ones are used). video_fix_command() does that for a number of v4l2 ioctls which initially got a _IO* define with wrong read/write bits. You can either just add the videotext ones there or translate them in the drivers right before calling video_usercopy().

Gerd

Signature · 1 line
return -ENOSIG;
#6 lore raw
Quoted 9 lines · Gerd Knorr
Quoted 2 lines

I would prefer redefining them with respect
to the arguments passed to the IOCTLs.

Thats possible as well.

Quoted 3 lines

Nevertheless there is one big disadvantage: The userspace programs have to be recompiled because they of course have to use the same IOCTL definitions.

Quoted 6 lines

You can just keep the old ones and map them, so both old and new ones will work (and internally only the new ones are used). video_fix_command() does that for a number of v4l2 ioctls which initially got a _IO* define with wrong read/write bits. You can either just add the videotext ones there or translate them in the drivers right before calling video_usercopy().

That's a good idea. The patch below maps old IOCTLs to new ones in the functions vtx_fix_command() in saa5246a.c and saa5249.c. The drivers now internally use the new IOCTLs but old userspace programs will still work without recompiling them.

For the new IOCTLs I need an identifying letter. According to Documentation/ioctl-number.txt the letter 'q', seq# 0-0x1F is presently allocated to the videotext drivers. I want to change this because linux/serio.h also uses the letter 'q', seq# 0.

I now allocated 0x81 (the major number of /dev/vtx<n>), seq# 0-0x1F to linux/videotext.h. I also added 'q', seq# 0-0x1F to linux/serio.h. In the present ioctl-number.txt only a conflict is indicated but the allocation for linux/serio.h is missing. I hope that's ok. Who else must I ask for this change in Documentation/ioctl-number.txt?

In include/linux/videotext.h I additionally cleaned up some definitions for a tuner because these definitions are not used any more anywhere in the kernel.

Michael

----------------------

Documentation/ioctl-number.txt +2 -1
diff -u -N -r linux-2.6.8.1/Documentation/ioctl-number.txt linux/Documentation/ioctl-number.txt
@@ -144,7 +144,7 @@
 'p'	40-7F	linux/nvram.h
 'p'	80-9F				user-space parport
 					<mailto:[email protected]>
-'q'	00-1F	linux/videotext.h	conflict!
+'q'	00-1F	linux/serio.h
 'q'	80-FF				Internet PhoneJACK, Internet LineJACK
 					<http://www.quicknet.net>
 'r'	00-1F	linux/msdos_fs.h
@@ -162,6 +162,7 @@
 'z'	40-7F				CAN bus card
 					<mailto:[email protected]>
 0x80	00-1F	linux/fb.h
+0x81	00-1F	linux/videotext.h
 0x89	00-06	asm-i386/sockios.h
 0x89	0B-DF	linux/sockios.h
 0x89	E0-EF	linux/sockios.h		SIOCPROTOPRIVATE range
drivers/media/video/saa5246a.c +49 -0

61 lines not on this page · raw message

drivers/media/video/saa5246a.h +1 -1
diff -u -N -r linux-2.6.8.1/drivers/media/video/saa5246a.h linux/drivers/media/video/saa5246a.h
@@ -23,7 +23,7 @@
 #define __SAA5246A_H__
 
 #define MAJOR_VERSION 1		/* driver major version number */
-#define MINOR_VERSION 7		/* driver minor version number */
+#define MINOR_VERSION 8		/* driver minor version number */
 
 #define IF_NAME "SAA5246A"
 
drivers/media/video/saa5249.c +53 -1

76 lines not on this page · raw message

include/linux/videotext.h +33 -52

102 lines not on this page · raw message

#7 lore raw

include/linux/videotext.h line 27

Quoted 7 lines
include/linux/videotext.h +7 -0
+#define VTXIOCGETINFO	_IOR  (0x81,  1, vtx_info_t)
+#define VTXIOCCLRPAGE	_IOW  (0x81,  2, vtx_pagereq_t)
+#define VTXIOCCLRFOUND	_IOW  (0x81,  3, vtx_pagereq_t)
+#define VTXIOCPAGEREQ	_IOW  (0x81,  4, vtx_pagereq_t)
+#define VTXIOCGETSTAT	_IOW  (0x81,  5, vtx_pagereq_t)
+#define VTXIOCGETPAGE	_IOW  (0x81,  6, vtx_pagereq_t)
+#define VTXIOCSTOPDAU	_IOW  (0x81,  7, vtx_pagereq_t)

Hmm, _IOW for VTXIOCGET* looks bogous, is that really correct?

Note that you often need RW for read/get ioctls because even these often pass data to the driver as well (for example the vtx page number you want query the status for). Please double-check that. Otherwise the patch looks ok to me.

Gerd

Signature · 1 line
return -ENOSIG;
#8 lore raw
Quoted 9 lines · Gerd Knorr

include/linux/videotext.h line 27

Quoted 7 lines
include/linux/videotext.h +7 -0
+#define VTXIOCGETINFO	_IOR  (0x81,  1, vtx_info_t)
+#define VTXIOCCLRPAGE	_IOW  (0x81,  2, vtx_pagereq_t)
+#define VTXIOCCLRFOUND	_IOW  (0x81,  3, vtx_pagereq_t)
+#define VTXIOCPAGEREQ	_IOW  (0x81,  4, vtx_pagereq_t)
+#define VTXIOCGETSTAT	_IOW  (0x81,  5, vtx_pagereq_t)
+#define VTXIOCGETPAGE	_IOW  (0x81,  6, vtx_pagereq_t)
+#define VTXIOCSTOPDAU	_IOW  (0x81,  7, vtx_pagereq_t)

Hmm, _IOW for VTXIOCGET* looks bogous, is that really correct?

This is the definition of the argument to the VTXIOCGET* IOCTLs:

typedef struct
{
int page; /* number of requested page (hexadecimal) */
int hour; /* requested hour (hexadecimal) */
int minute; /* requested minute (hexadecimal) */
int pagemask; /* mask defining which values of the above are set */
int pgbuf; /* buffer where page will be stored */
int start; /* start of requested part of page */
int end; /* end of requested part of page */
void __user *buffer; /* pointer to beginning of destination buffer */
}
vtx_pagereq_t;

The driver returns all data in the buffer field. Copying there is done by a seperate call to copy_to_user(). All other fields are never changed by the driver. So the _IOW definition is ok.

Quoted 4 lines

Note that you often need RW for read/get ioctls because even these often pass data to the driver as well (for example the vtx page number you want query the status for). Please double-check that. Otherwise the patch looks ok to me.

Thank you! Andrew, could you please forward the patch? Suggestion for a comment line:
Videotext: IOCTLs changed to match _IO macros in linux/ioctl.h

Michael

#9 lore raw
Quoted 3 lines · Michael Geng

Thank you! Andrew, could you please forward the patch? Suggestion for a comment line:
Videotext: IOCTLs changed to match _IO macros in linux/ioctl.h

But have any of them changed ioctl number in the process and broken compatibility ?

#10 lore raw
Quoted 7 lines · Alan Cox
Quoted 3 lines · Michael Geng

Thank you! Andrew, could you please forward the patch? Suggestion for a comment line:
Videotext: IOCTLs changed to match _IO macros in linux/ioctl.h

But have any of them changed ioctl number in the process and broken compatibility ?

No compatibility is broken, because old IOCTLs are remapped to new ones in the driver. The driver properly handles both old and new IOCTLs after applying the patch. No user space programs have to be changed or recompiled.

Michael