linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] Input: serport: Add compat_ioctl routine to support 32bit inputattach in 64bit systems
@ 2014-09-09  5:20 John Sung
  2014-09-09 18:14 ` Dmitry Torokhov
  0 siblings, 1 reply; 3+ messages in thread
From: John Sung @ 2014-09-09  5:20 UTC (permalink / raw)
  To: Dmitry Torokhov, linux-input, linux-kernel; +Cc: penmount.touch

When running a 32-bit inputattach utility in a 64-bit system, there will be error code "inputattach: can't set device type". This is caused by the serport device driver not supporting compat_ioctl, so that SPIOCSTYPE ioctl fails.

Changes in v2:
(1) Codes of the compat_ioctl are protected by #ifdef CONFIG_COMPAT.
(2) Add a new function serport_set_type() for common codes used by serport_ldisc_ioctl() and serport_ldisc_compat_ioctl().

Signed-off-by: John Sung <penmount.touch@gmail.com>
---
 drivers/input/serio/serport.c |   57 +++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 11 deletions(-)

diff --git a/drivers/input/serio/serport.c b/drivers/input/serio/serport.c
index 0cb7ef5..1decaa2 100644
--- a/drivers/input/serio/serport.c
+++ b/drivers/input/serio/serport.c
@@ -22,6 +22,10 @@
 #include <linux/serio.h>
 #include <linux/tty.h>
 
+#ifdef CONFIG_COMPAT
+#include <linux/compat.h>
+#endif
+
 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION("Input device TTY line discipline");
 MODULE_LICENSE("GPL");
@@ -40,6 +44,30 @@ struct serport {
 	unsigned long flags;
 };
 
+#ifdef CONFIG_COMPAT
+#define SPIOCSTYPE32 (_IOW('q', 0x01, compat_ulong_t))
+#endif
+
+/*
+ * serport_set_type() is called by serport_ldisc_ioctl() and
+ * serport_ldisc_compat_ioctl() to set up the serio_device_id values
+ */
+
+static int serport_set_type(struct tty_struct *tty, unsigned long arg)
+{
+	struct serport *serport = (struct serport *) tty->disc_data;
+	unsigned long type;
+
+	if (get_user(type, (unsigned long __user *) arg))
+		return -EFAULT;
+
+	serport->id.proto = type & 0x000000ff;
+	serport->id.id	  = (type & 0x0000ff00) >> 8;
+	serport->id.extra = (type & 0x00ff0000) >> 16;
+
+	return 0;
+}
+
 /*
  * Callback functions from the serio code.
  */
@@ -204,18 +232,8 @@ static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, u
 
 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
 {
-	struct serport *serport = (struct serport*) tty->disc_data;
-	unsigned long type;
-
 	if (cmd == SPIOCSTYPE) {
-		if (get_user(type, (unsigned long __user *) arg))
-			return -EFAULT;
-
-		serport->id.proto = type & 0x000000ff;
-		serport->id.id	  = (type & 0x0000ff00) >> 8;
-		serport->id.extra = (type & 0x00ff0000) >> 16;
-
-		return 0;
+		return serport_set_type(tty, arg);
 	}
 
 	return -EINVAL;
@@ -232,6 +250,20 @@ static void serport_ldisc_write_wakeup(struct tty_struct * tty)
 	spin_unlock_irqrestore(&serport->lock, flags);
 }
 
+#ifdef CONFIG_COMPAT
+/*
+ * serport_ldisc_compat_ioctl() allows to set the port protocol, and device ID
+ */
+
+static long serport_ldisc_compat_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
+{
+	if (cmd == SPIOCSTYPE32)
+		return serport_set_type(tty, arg);
+
+	return -EINVAL;
+}
+#endif
+
 /*
  * The line discipline structure.
  */
@@ -243,6 +275,9 @@ static struct tty_ldisc_ops serport_ldisc = {
 	.close =	serport_ldisc_close,
 	.read =		serport_ldisc_read,
 	.ioctl =	serport_ldisc_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl =	serport_ldisc_compat_ioctl,
+#endif
 	.receive_buf =	serport_ldisc_receive,
 	.write_wakeup =	serport_ldisc_write_wakeup
 };
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] Input: serport: Add compat_ioctl routine to support 32bit inputattach in 64bit systems
  2014-09-09  5:20 [PATCH v2] Input: serport: Add compat_ioctl routine to support 32bit inputattach in 64bit systems John Sung
@ 2014-09-09 18:14 ` Dmitry Torokhov
       [not found]   ` <CAKw_OUUdUA8v=e6=A4EEDpesskbOsZuQrO1Qqm87W6ayurz4Fg@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Torokhov @ 2014-09-09 18:14 UTC (permalink / raw)
  To: John Sung; +Cc: linux-input, linux-kernel

Hi John,

On Tue, Sep 09, 2014 at 01:20:28PM +0800, John Sung wrote:
> When running a 32-bit inputattach utility in a 64-bit system, there will be error code "inputattach: can't set device type". This is caused by the serport device driver not supporting compat_ioctl, so that SPIOCSTYPE ioctl fails.
> 
> Changes in v2:
> (1) Codes of the compat_ioctl are protected by #ifdef CONFIG_COMPAT.
> (2) Add a new function serport_set_type() for common codes used by serport_ldisc_ioctl() and serport_ldisc_compat_ioctl().
> 
> Signed-off-by: John Sung <penmount.touch@gmail.com>
> ---
>  drivers/input/serio/serport.c |   57 +++++++++++++++++++++++++++++++++--------
>  1 file changed, 46 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/input/serio/serport.c b/drivers/input/serio/serport.c
> index 0cb7ef5..1decaa2 100644
> --- a/drivers/input/serio/serport.c
> +++ b/drivers/input/serio/serport.c
> @@ -22,6 +22,10 @@
>  #include <linux/serio.h>
>  #include <linux/tty.h>
>  
> +#ifdef CONFIG_COMPAT
> +#include <linux/compat.h>
> +#endif
> +
>  MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
>  MODULE_DESCRIPTION("Input device TTY line discipline");
>  MODULE_LICENSE("GPL");
> @@ -40,6 +44,30 @@ struct serport {
>  	unsigned long flags;
>  };
>  
> +#ifdef CONFIG_COMPAT
> +#define SPIOCSTYPE32 (_IOW('q', 0x01, compat_ulong_t))
> +#endif
> +
> +/*
> + * serport_set_type() is called by serport_ldisc_ioctl() and
> + * serport_ldisc_compat_ioctl() to set up the serio_device_id values
> + */
> +
> +static int serport_set_type(struct tty_struct *tty, unsigned long arg)
> +{
> +	struct serport *serport = (struct serport *) tty->disc_data;
> +	unsigned long type;
> +
> +	if (get_user(type, (unsigned long __user *) arg))
> +		return -EFAULT;

I of not think we can do this. Kernel with try to fetch 64 bits from
where userland put only 32. It probbaly works on little-endian
architectures by chance, but won't on big endian.

Does the version of the patch below work for you?

Thanks.

-- 
Dmitry


Input: serport - add compat handling for SPIOCSTYPE ioctl

From: John Sung <penmount.touch@gmail.com>

When running a 32-bit inputattach utility in a 64-bit system, there will be
error code "inputattach: can't set device type". This is caused by the
serport device driver not supporting compat_ioctl, so that SPIOCSTYPE ioctl
fails.

Signed-off-by: John Sung <penmount.touch@gmail.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/input/serio/serport.c |   45 +++++++++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/drivers/input/serio/serport.c b/drivers/input/serio/serport.c
index 0cb7ef5..69175b8 100644
--- a/drivers/input/serio/serport.c
+++ b/drivers/input/serio/serport.c
@@ -21,6 +21,7 @@
 #include <linux/init.h>
 #include <linux/serio.h>
 #include <linux/tty.h>
+#include <linux/compat.h>
 
 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION("Input device TTY line discipline");
@@ -198,28 +199,55 @@ static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, u
 	return 0;
 }
 
+static void serport_set_type(struct tty_struct *tty, unsigned long type)
+{
+	struct serport *serport = tty->disc_data;
+
+	serport->id.proto = type & 0x000000ff;
+	serport->id.id    = (type & 0x0000ff00) >> 8;
+	serport->id.extra = (type & 0x00ff0000) >> 16;
+}
+
 /*
  * serport_ldisc_ioctl() allows to set the port protocol, and device ID
  */
 
-static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
+static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
+			       unsigned int cmd, unsigned long arg)
 {
-	struct serport *serport = (struct serport*) tty->disc_data;
-	unsigned long type;
-
 	if (cmd == SPIOCSTYPE) {
+		unsigned long type;
+
 		if (get_user(type, (unsigned long __user *) arg))
 			return -EFAULT;
 
-		serport->id.proto = type & 0x000000ff;
-		serport->id.id	  = (type & 0x0000ff00) >> 8;
-		serport->id.extra = (type & 0x00ff0000) >> 16;
+		serport_set_type(tty, type);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+#ifdef CONFIG_COMPAT
+#define COMPAT_SPIOCSTYPE	_IOW('q', 0x01, compat_ulong_t)
+static long serport_ldisc_compat_ioctl(struct tty_struct *tty,
+				       struct file *file,
+				       unsigned int cmd, unsigned long arg)
+{
+	if (cmd == COMPAT_SPIOCSTYPE) {
+		void __user *uarg = compat_ptr(arg);
+		compat_ulong_t compat_type;
+
+		if (get_user(compat_type, (compat_ulong_t __user *)uarg))
+			return -EFAULT;
 
+		serport_set_type(tty, compat_type);
 		return 0;
 	}
 
 	return -EINVAL;
 }
+#endif
 
 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
 {
@@ -243,6 +271,9 @@ static struct tty_ldisc_ops serport_ldisc = {
 	.close =	serport_ldisc_close,
 	.read =		serport_ldisc_read,
 	.ioctl =	serport_ldisc_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl =	serport_ldisc_compat_ioctl,
+#endif
 	.receive_buf =	serport_ldisc_receive,
 	.write_wakeup =	serport_ldisc_write_wakeup
 };

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] Input: serport: Add compat_ioctl routine to support 32bit inputattach in 64bit systems
       [not found]   ` <CAKw_OUUdUA8v=e6=A4EEDpesskbOsZuQrO1Qqm87W6ayurz4Fg@mail.gmail.com>
@ 2014-09-10 18:08     ` Dmitry Torokhov
  0 siblings, 0 replies; 3+ messages in thread
From: Dmitry Torokhov @ 2014-09-10 18:08 UTC (permalink / raw)
  To: John Sung; +Cc: linux-input, linux-kernel

On Wed, Sep 10, 2014 at 10:18:14AM +0800, John Sung wrote:
> Hi Dmitry,
> 
> I tried the patch, and it works with my 32-bit inputattach.

Excellent, I queued it up for mainline.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-09-10 18:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-09  5:20 [PATCH v2] Input: serport: Add compat_ioctl routine to support 32bit inputattach in 64bit systems John Sung
2014-09-09 18:14 ` Dmitry Torokhov
     [not found]   ` <CAKw_OUUdUA8v=e6=A4EEDpesskbOsZuQrO1Qqm87W6ayurz4Fg@mail.gmail.com>
2014-09-10 18:08     ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).