Skip to contentSkip to conversation

Add support compat in dvb_frontend.c

PATCH In review Jaedon Shin 5 participants 10 messages Dec 1, 2017 – Dec 13, 2017

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

cover Add support compat in dvb_frontend.c

This patch series supports compat ioctl for 32-bit user space applications in 64-bit system.

Jaedon Shin (3):
media: dvb_frontend: Add unlocked_ioctl in dvb_frontend.c
media: dvb_frontend: Add compat_ioctl callback
media: dvb_frontend: Add commands implementation for compat ioct

 drivers/media/dvb-core/dvb_frontend.c | 159 +++++++++++++++++++++++++++++++++-
 fs/compat_ioctl.c                     |  17 ----
 2 files changed, 156 insertions(+), 20 deletions(-)
Signature · 1 line
2.15.0
#1 lore raw

PATCH 1/3 media: dvb_frontend: Add unlocked_ioctl in dvb_frontend.c

Adds unlocked ioctl function directly in dvb_frontend.c instead of using dvb_generic_ioctl().

---
 drivers/media/dvb-core/dvb_frontend.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/media/dvb-core/dvb_frontend.c +14 -3
index 3ad83359098b..6d8f4dd39c0c 100644
@@ -1920,7 +1920,8 @@ static int dtv_property_process_set(struct dvb_frontend *fe,
 	return r;
 }
 
-static int dvb_frontend_ioctl(struct file *file, unsigned int cmd, void *parg)
+static int dvb_frontend_do_ioctl(struct file *file, unsigned int cmd,
+				 void *parg)
 {
 	struct dvb_device *dvbdev = file->private_data;
 	struct dvb_frontend *fe = dvbdev->priv;
@@ -1963,6 +1964,17 @@ static int dvb_frontend_ioctl(struct file *file, unsigned int cmd, void *parg)
 	return err;
 }
 
+static long dvb_frontend_ioctl(struct file *file, unsigned int cmd,
+			       unsigned long arg)
+{
+	struct dvb_device *dvbdev = file->private_data;
+
+	if (!dvbdev)
+		return -ENODEV;
+
+	return dvb_usercopy(file, cmd, arg, dvb_frontend_do_ioctl);
+}
+
 static int dtv_set_frontend(struct dvb_frontend *fe)
 {
 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
@@ -2644,7 +2656,7 @@ static int dvb_frontend_release(struct inode *inode, struct file *file)
 
 static const struct file_operations dvb_frontend_fops = {
 	.owner		= THIS_MODULE,
-	.unlocked_ioctl	= dvb_generic_ioctl,
+	.unlocked_ioctl	= dvb_frontend_ioctl,
 	.poll		= dvb_frontend_poll,
 	.open		= dvb_frontend_open,
 	.release	= dvb_frontend_release,
@@ -2712,7 +2724,6 @@ int dvb_register_frontend(struct dvb_adapter* dvb,
 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 		.name = fe->ops.info.name,
 #endif
-		.kernel_ioctl = dvb_frontend_ioctl
 	};
 
 	dev_dbg(dvb->device, "%s:\n", __func__);
Signature · 1 line
2.15.0
#2 lore raw

PATCH 2/3 media: dvb_frontend: Add compat_ioctl callback

In reply to Jaedon Shin

Adds compat_ioctl for 32-bit user space applications on a 64-bit system.

---
 drivers/media/dvb-core/dvb_frontend.c | 11 +++++++++++
 fs/compat_ioctl.c                     | 17 -----------------
 2 files changed, 11 insertions(+), 17 deletions(-)
drivers/media/dvb-core/dvb_frontend.c +11 -0
index 6d8f4dd39c0c..1ae23403a0ab 100644
@@ -1975,6 +1975,14 @@ static long dvb_frontend_ioctl(struct file *file, unsigned int cmd,
 	return dvb_usercopy(file, cmd, arg, dvb_frontend_do_ioctl);
 }
 
+#ifdef CONFIG_COMPAT
+static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd,
+				      unsigned long arg)
+{
+	return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
+}
+#endif
+
 static int dtv_set_frontend(struct dvb_frontend *fe)
 {
 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
@@ -2657,6 +2665,9 @@ static int dvb_frontend_release(struct inode *inode, struct file *file)
 static const struct file_operations dvb_frontend_fops = {
 	.owner		= THIS_MODULE,
 	.unlocked_ioctl	= dvb_frontend_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl	= dvb_frontend_compat_ioctl,
+#endif
 	.poll		= dvb_frontend_poll,
 	.open		= dvb_frontend_open,
 	.release	= dvb_frontend_release,
fs/compat_ioctl.c +0 -17
index 5fc5dc660600..9a1fe60cce9a 100644
@@ -1218,23 +1218,6 @@ COMPATIBLE_IOCTL(DMX_SET_PES_FILTER)
 COMPATIBLE_IOCTL(DMX_SET_BUFFER_SIZE)
 COMPATIBLE_IOCTL(DMX_GET_PES_PIDS)
 COMPATIBLE_IOCTL(DMX_GET_STC)
-COMPATIBLE_IOCTL(FE_GET_INFO)
-COMPATIBLE_IOCTL(FE_DISEQC_RESET_OVERLOAD)
-COMPATIBLE_IOCTL(FE_DISEQC_SEND_MASTER_CMD)
-COMPATIBLE_IOCTL(FE_DISEQC_RECV_SLAVE_REPLY)
-COMPATIBLE_IOCTL(FE_DISEQC_SEND_BURST)
-COMPATIBLE_IOCTL(FE_SET_TONE)
-COMPATIBLE_IOCTL(FE_SET_VOLTAGE)
-COMPATIBLE_IOCTL(FE_ENABLE_HIGH_LNB_VOLTAGE)
-COMPATIBLE_IOCTL(FE_READ_STATUS)
-COMPATIBLE_IOCTL(FE_READ_BER)
-COMPATIBLE_IOCTL(FE_READ_SIGNAL_STRENGTH)
-COMPATIBLE_IOCTL(FE_READ_SNR)
-COMPATIBLE_IOCTL(FE_READ_UNCORRECTED_BLOCKS)
-COMPATIBLE_IOCTL(FE_SET_FRONTEND)
-COMPATIBLE_IOCTL(FE_GET_FRONTEND)
-COMPATIBLE_IOCTL(FE_GET_EVENT)
-COMPATIBLE_IOCTL(FE_DISHNETWORK_SEND_LEGACY_CMD)
 COMPATIBLE_IOCTL(VIDEO_STOP)
 COMPATIBLE_IOCTL(VIDEO_PLAY)
 COMPATIBLE_IOCTL(VIDEO_FREEZE)
Signature · 1 line
2.15.0
#4 lore raw

PATCH 3/3 media: dvb_frontend: Add commands implementation for compat ioct

In reply to Jaedon Shin

The dtv_properties structure and the dtv_property structure are different sizes in 32-bit and 64-bit system. This patch provides FE_SET_PROPERTY and FE_GET_PROPERTY ioctl commands implementation for 32-bit user space applications.

---
 drivers/media/dvb-core/dvb_frontend.c | 131 ++++++++++++++++++++++++++++++++++
 1 file changed, 131 insertions(+)
drivers/media/dvb-core/dvb_frontend.c +131 -0
index 1ae23403a0ab..f3751a573dfe 100644
@@ -1976,9 +1976,140 @@ static long dvb_frontend_ioctl(struct file *file, unsigned int cmd,
 }
 
 #ifdef CONFIG_COMPAT
+struct compat_dtv_property {
+	__u32 cmd;
+	__u32 reserved[3];
+	union {
+		__u32 data;
+		struct dtv_fe_stats st;
+		struct {
+			__u8 data[32];
+			__u32 len;
+			__u32 reserved1[3];
+			compat_uptr_t reserved2;
+		} buffer;
+	} u;
+	int result;
+} __attribute__ ((packed));
+
+struct compat_dtv_properties {
+	__u32 num;
+	compat_uptr_t props;
+};
+
+#define COMPAT_FE_SET_PROPERTY	   _IOW('o', 82, struct compat_dtv_properties)
+#define COMPAT_FE_GET_PROPERTY	   _IOR('o', 83, struct compat_dtv_properties)
+
+static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd,
+					    unsigned long arg)
+{
+	struct dvb_device *dvbdev = file->private_data;
+	struct dvb_frontend *fe = dvbdev->priv;
+	struct dvb_frontend_private *fepriv = fe->frontend_priv;
+	int i, err = 0;
+
+	if (cmd == COMPAT_FE_SET_PROPERTY) {
+		struct compat_dtv_properties prop, *tvps = NULL;
+		struct compat_dtv_property *tvp = NULL;
+
+		if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
+			return -EFAULT;
+
+		tvps = ∝
+
+		/*
+		 * Put an arbitrary limit on the number of messages that can
+		 * be sent at once
+		 */
+		if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
+			return -EINVAL;
+
+		tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
+		if (IS_ERR(tvp))
+			return PTR_ERR(tvp);
+
+		for (i = 0; i < tvps->num; i++) {
+			err = dtv_property_process_set(fe, file,
+							(tvp + i)->cmd,
+							(tvp + i)->u.data);
+			if (err < 0) {
+				kfree(tvp);
+				return err;
+			}
+		}
+		kfree(tvp);
+	} else if (cmd == COMPAT_FE_GET_PROPERTY) {
+		struct compat_dtv_properties prop, *tvps = NULL;
+		struct compat_dtv_property *tvp = NULL;
+		struct dtv_frontend_properties getp = fe->dtv_property_cache;
+
+		if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
+			return -EFAULT;
+
+		tvps = &prop;
+
+		/*
+		 * Put an arbitrary limit on the number of messages that can
+		 * be sent at once
+		 */
+		if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
+			return -EINVAL;
+
+		tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
+		if (IS_ERR(tvp))
+			return PTR_ERR(tvp);
+
+		/*
+		 * Let's use our own copy of property cache, in order to
+		 * avoid mangling with DTV zigzag logic, as drivers might
+		 * return crap, if they don't check if the data is available
+		 * before updating the properties cache.
+		 */
+		if (fepriv->state != FESTATE_IDLE) {
+			err = dtv_get_frontend(fe, &getp, NULL);
+			if (err < 0) {
+				kfree(tvp);
+				return err;
+			}
+		}
+		for (i = 0; i < tvps->num; i++) {
+			err = dtv_property_process_get(
+			    fe, &getp, (struct dtv_property *)tvp + i, file);
+			if (err < 0) {
+				kfree(tvp);
+				return err;
+			}
+		}
+
+		if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
+				 tvps->num * sizeof(struct compat_dtv_property))) {
+			kfree(tvp);
+			return -EFAULT;
+		}
+		kfree(tvp);
+	}
+
+	return err;
+}
+
 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd,
 				      unsigned long arg)
 {
+	struct dvb_device *dvbdev = file->private_data;
+	struct dvb_frontend *fe = dvbdev->priv;
+	struct dvb_frontend_private *fepriv = fe->frontend_priv;
+	int err;
+
+	if (cmd == COMPAT_FE_SET_PROPERTY || cmd == COMPAT_FE_GET_PROPERTY) {
+		if (down_interruptible(&fepriv->sem))
+			return -ERESTARTSYS;
+
+		err = dvb_frontend_handle_compat_ioctl(file, cmd, arg);
+
+		up(&fepriv->sem);
+		return err;
+	}
+
 	return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
 }
 #endif
Signature · 1 line
2.15.0
#8 lore raw
In reply to Jaedon Shin

Hi Jaedon,

Em Fri, 1 Dec 2017 21:31:27 +0900
Jaedon Shin <[email protected]> escreveu:

Quoted 7 lines

This patch series supports compat ioctl for 32-bit user space applications in 64-bit system.

Jaedon Shin (3):
media: dvb_frontend: Add unlocked_ioctl in dvb_frontend.c
media: dvb_frontend: Add compat_ioctl callback
media: dvb_frontend: Add commands implementation for compat ioct

Thanks for the series. Yeah, indeed we need something like that.

Yet, I suspect that you should also move the logic inside dvb_frontend_handle_ioctl() with copies from/to userspace.

We don't want the logic there to be called when a 32-bit userspace copy happens, as it should now use the new compat32 code.

Thanks,
Mauro

#5 lore raw

Re: [PATCH 3/3] media: dvb_frontend: Add commands implementation for compat ioct

In reply to Jaedon Shin
Thank you for the patch! Yet something to improve:

Hi Jaedon,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on v4.15-rc2 next-20171204]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Jaedon-Shin/Add-support-compat-in-dvb_frontend-c/20171204-201817
base:   git://linuxtv.org/media_tree.git master
config: x86_64-randconfig-x014-201749 (attached as .config)
compiler: gcc-7 (Debian 7.2.0-12) 7.2.1 20171025
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

>> drivers/media//dvb-core/dvb_frontend.c:1992:4: error: unknown type name 'compat_uptr_t'
       compat_uptr_t reserved2;
       ^~~~~~~~~~~~~
   drivers/media//dvb-core/dvb_frontend.c:2000:2: error: unknown type name 'compat_uptr_t'
     compat_uptr_t props;
     ^~~~~~~~~~~~~
   drivers/media//dvb-core/dvb_frontend.c: In function 'dvb_frontend_handle_compat_ioctl':
   drivers/media//dvb-core/dvb_frontend.c:2018:29: error: implicit declaration of function 'compat_ptr'; did you mean 'complete'? [-Werror=implicit-function-declaration]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^~~~~~~~~~
                                complete
>> drivers/media//dvb-core/dvb_frontend.c:2018:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
   In file included from include/linux/poll.h:12:0,
                    from drivers/media//dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
>> drivers/media//dvb-core/dvb_frontend.c:2030:21: warning: passing argument 1 of 'memdup_user' makes pointer from integer without a cast [-Wint-conversion]
      tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
                        ^~~~~~~~~~
   In file included from drivers/media//dvb-core/dvb_frontend.c:30:0:
   include/linux/string.h:13:14: note: expected 'const void *' but argument is of type 'int'
    extern void *memdup_user(const void __user *, size_t);
                 ^~~~~~~~~~~
   drivers/media//dvb-core/dvb_frontend.c:2049:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^~~~~~~~~~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media//dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   drivers/media//dvb-core/dvb_frontend.c:2061:21: warning: passing argument 1 of 'memdup_user' makes pointer from integer without a cast [-Wint-conversion]
      tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
                        ^~~~~~~~~~
   In file included from drivers/media//dvb-core/dvb_frontend.c:30:0:
   include/linux/string.h:13:14: note: expected 'const void *' but argument is of type 'int'
    extern void *memdup_user(const void __user *, size_t);
                 ^~~~~~~~~~~
>> drivers/media//dvb-core/dvb_frontend.c:2087:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
                       ^

cc1: some warnings being treated as errors

vim +/compat_uptr_t +1992 drivers/media//dvb-core/dvb_frontend.c

1980
1981 #ifdef CONFIG_COMPAT
1982 struct compat_dtv_property {
1983 __u32 cmd;
1984 __u32 reserved[3];
1985 union {
1986 __u32 data;
1987 struct dtv_fe_stats st;
1988 struct {
1989 __u8 data[32];
1990 __u32 len;
1991 __u32 reserved1[3];

> 1992				compat_uptr_t reserved2;
  1993			} buffer;
  1994		} u;
  1995		int result;
  1996	} __attribute__ ((packed));
  1997	
  1998	struct compat_dtv_properties {
  1999		__u32 num;
> 2000		compat_uptr_t props;
  2001	};
  2002	
  2003	#define COMPAT_FE_SET_PROPERTY	   _IOW('o', 82, struct compat_dtv_properties)
  2004	#define COMPAT_FE_GET_PROPERTY	   _IOR('o', 83, struct compat_dtv_properties)
  2005	
  2006	static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd,
  2007						    unsigned long arg)
  2008	{
  2009		struct dvb_device *dvbdev = file->private_data;
  2010		struct dvb_frontend *fe = dvbdev->priv;
  2011		struct dvb_frontend_private *fepriv = fe->frontend_priv;
  2012		int i, err = 0;
  2013	
  2014		if (cmd == COMPAT_FE_SET_PROPERTY) {
  2015			struct compat_dtv_properties prop, *tvps = NULL;
  2016			struct compat_dtv_property *tvp = NULL;
  2017	
> 2018			if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
  2019				return -EFAULT;
  2020	
  2021			tvps = &prop;
  2022	
  2023			/*
  2024			 * Put an arbitrary limit on the number of messages that can
  2025			 * be sent at once
  2026			 */
  2027			if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
  2028				return -EINVAL;
  2029	
> 2030			tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
  2031			if (IS_ERR(tvp))
  2032				return PTR_ERR(tvp);
  2033	
  2034			for (i = 0; i < tvps->num; i++) {
  2035				err = dtv_property_process_set(fe, file,
  2036								(tvp + i)->cmd,
  2037								(tvp + i)->u.data);
  2038				if (err < 0) {
  2039					kfree(tvp);
  2040					return err;
  2041				}
  2042			}
  2043			kfree(tvp);
  2044		} else if (cmd == COMPAT_FE_GET_PROPERTY) {
  2045			struct compat_dtv_properties prop, *tvps = NULL;
  2046			struct compat_dtv_property *tvp = NULL;
  2047			struct dtv_frontend_properties getp = fe->dtv_property_cache;
  2048	
  2049			if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
  2050				return -EFAULT;
  2051	
  2052			tvps = &prop;
  2053	
  2054			/*
  2055			 * Put an arbitrary limit on the number of messages that can
  2056			 * be sent at once
  2057			 */
  2058			if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
  2059				return -EINVAL;
  2060	
> 2061			tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
  2062			if (IS_ERR(tvp))
  2063				return PTR_ERR(tvp);
  2064	
  2065			/*
  2066			 * Let's use our own copy of property cache, in order to
  2067			 * avoid mangling with DTV zigzag logic, as drivers might
  2068			 * return crap, if they don't check if the data is available
  2069			 * before updating the properties cache.
  2070			 */
  2071			if (fepriv->state != FESTATE_IDLE) {
  2072				err = dtv_get_frontend(fe, &getp, NULL);
  2073				if (err < 0) {
  2074					kfree(tvp);
  2075					return err;
  2076				}
  2077			}
  2078			for (i = 0; i < tvps->num; i++) {
  2079				err = dtv_property_process_get(
  2080				    fe, &getp, (struct dtv_property *)tvp + i, file);
  2081				if (err < 0) {
  2082					kfree(tvp);
  2083					return err;
  2084				}
  2085			}
  2086	
> 2087			if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,

2088 tvps->num * sizeof(struct compat_dtv_property))) {
2089 kfree(tvp);
2090 return -EFAULT;
2091 }
2092 kfree(tvp);
2093 }
2094
2095 return err;
2096 }
2097

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#6 lore raw

Re: [PATCH 3/3] media: dvb_frontend: Add commands implementation for compat ioct

In reply to Jaedon Shin

Hello
Sorry if I say something completely wrong here, I was thinking to implement the same on my own
As far as I understand from the patch, you have added two "compact" specific ioctl commands
As far as I know, the compact_ioctl is called automatically when the userland is 32 bit and kernel is 64 bit and it must be transparent With compact specific ioctl command, the application should know if it is running on top of 64 bit kernel and use them in case
I am not sure if this is the correct approach (also the userland application should be all upgraded to make use of these new commands) As far as I have understood, the compatc_ioctl body should adapt the structures in and out, using the compact_ types
Bye

2017-12-01 13:31 GMT+01:00 Jaedon Shin <[email protected]>:

Quoted 158 lines

The dtv_properties structure and the dtv_property structure are different sizes in 32-bit and 64-bit system. This patch provides FE_SET_PROPERTY and FE_GET_PROPERTY ioctl commands implementation for 32-bit user space applications.

---
 drivers/media/dvb-core/dvb_frontend.c | 131 ++++++++++++++++++++++++++++++++++
 1 file changed, 131 insertions(+)
drivers/media/dvb-core/dvb_frontend.c +131 -1
index 1ae23403a0ab..f3751a573dfe 100644
@@ -1976,9 +1976,140 @@ static long dvb_frontend_ioctl(struct file *file, unsigned int cmd,
 }
 
 #ifdef CONFIG_COMPAT
+struct compat_dtv_property {
+       __u32 cmd;
+       __u32 reserved[3];
+       union {
+               __u32 data;
+               struct dtv_fe_stats st;
+               struct {
+                       __u8 data[32];
+                       __u32 len;
+                       __u32 reserved1[3];
+                       compat_uptr_t reserved2;
+               } buffer;
+       } u;
+       int result;
+} __attribute__ ((packed));
+
+struct compat_dtv_properties {
+       __u32 num;
+       compat_uptr_t props;
+};
+
+#define COMPAT_FE_SET_PROPERTY    _IOW('o', 82, struct compat_dtv_properties)
+#define COMPAT_FE_GET_PROPERTY    _IOR('o', 83, struct compat_dtv_properties)
+
+static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd,
+                                           unsigned long arg)
+{
+       struct dvb_device *dvbdev = file->private_data;
+       struct dvb_frontend *fe = dvbdev->priv;
+       struct dvb_frontend_private *fepriv = fe->frontend_priv;
+       int i, err = 0;
+
+       if (cmd == COMPAT_FE_SET_PROPERTY) {
+               struct compat_dtv_properties prop, *tvps = NULL;
+               struct compat_dtv_property *tvp = NULL;
+
+               if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
+                       return -EFAULT;
+
+               tvps = &prop;
+
+               /*
+                * Put an arbitrary limit on the number of messages that can
+                * be sent at once
+                */
+               if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
+                       return -EINVAL;
+
+               tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
+               if (IS_ERR(tvp))
+                       return PTR_ERR(tvp);
+
+               for (i = 0; i < tvps->num; i++) {
+                       err = dtv_property_process_set(fe, file,
+                                                       (tvp + i)->cmd,
+                                                       (tvp + i)->u.data);
+                       if (err < 0) {
+                               kfree(tvp);
+                               return err;
+                       }
+               }
+               kfree(tvp);
+       } else if (cmd == COMPAT_FE_GET_PROPERTY) {
+               struct compat_dtv_properties prop, *tvps = NULL;
+               struct compat_dtv_property *tvp = NULL;
+               struct dtv_frontend_properties getp = fe->dtv_property_cache;
+
+               if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
+                       return -EFAULT;
+
+               tvps = &prop;
+
+               /*
+                * Put an arbitrary limit on the number of messages that can
+                * be sent at once
+                */
+               if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
+                       return -EINVAL;
+
+               tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
+               if (IS_ERR(tvp))
+                       return PTR_ERR(tvp);
+
+               /*
+                * Let's use our own copy of property cache, in order to
+                * avoid mangling with DTV zigzag logic, as drivers might
+                * return crap, if they don't check if the data is available
+                * before updating the properties cache.
+                */
+               if (fepriv->state != FESTATE_IDLE) {
+                       err = dtv_get_frontend(fe, &getp, NULL);
+                       if (err < 0) {
+                               kfree(tvp);
+                               return err;
+                       }
+               }
+               for (i = 0; i < tvps->num; i++) {
+                       err = dtv_property_process_get(
+                           fe, &getp, (struct dtv_property *)tvp + i, file);
+                       if (err < 0) {
+                               kfree(tvp);
+                               return err;
+                       }
+               }
+
+               if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
+                                tvps->num * sizeof(struct compat_dtv_property))) {
+                       kfree(tvp);
+                       return -EFAULT;
+               }
+               kfree(tvp);
+       }
+
+       return err;
+}
+
 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd,
                                      unsigned long arg)
 {
+       struct dvb_device *dvbdev = file->private_data;
+       struct dvb_frontend *fe = dvbdev->priv;
+       struct dvb_frontend_private *fepriv = fe->frontend_priv;
+       int err;
+
+       if (cmd == COMPAT_FE_SET_PROPERTY || cmd == COMPAT_FE_GET_PROPERTY) {
+               if (down_interruptible(&fepriv->sem))
+                       return -ERESTARTSYS;
+
+               err = dvb_frontend_handle_compat_ioctl(file, cmd, arg);
+
+               up(&fepriv->sem);
+               return err;
+       }
+
        return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
 }
 #endif
--

2.15.0

#3 lore raw

Re: [PATCH 2/3] media: dvb_frontend: Add compat_ioctl callback

In reply to Jaedon Shin
Thank you for the patch! Yet something to improve:

Hi Jaedon,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on v4.15-rc2 next-20171204]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Jaedon-Shin/Add-support-compat-in-dvb_frontend-c/20171204-201817
base:   git://linuxtv.org/media_tree.git master
config: x86_64-randconfig-x014-201749 (attached as .config)
compiler: gcc-7 (Debian 7.2.0-12) 7.2.1 20171025
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

drivers/media/dvb-core/dvb_frontend.c: In function 'dvb_frontend_compat_ioctl':

>> drivers/media/dvb-core/dvb_frontend.c:1985:54: error: implicit declaration of function 'compat_ptr'; did you mean 'complete'? [-Werror=implicit-function-declaration]
     return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
                                                         ^~~~~~~~~~
                                                         complete

cc1: some warnings being treated as errors

vim +1985 drivers/media/dvb-core/dvb_frontend.c

1980
1981 #ifdef CONFIG_COMPAT
1982 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd,
1983 unsigned long arg)
1984 {

> 1985		return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg));

1986 }
1987 #endif
1988

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw

Re: [PATCH 3/3] media: dvb_frontend: Add commands implementation for compat ioct

In reply to Jaedon Shin
Thank you for the patch! Perhaps something to improve:

Hi Jaedon,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linuxtv-media/master]
[also build test WARNING on v4.15-rc2 next-20171204]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Jaedon-Shin/Add-support-compat-in-dvb_frontend-c/20171204-201817
base:   git://linuxtv.org/media_tree.git master
config: x86_64-randconfig-ne0-12042359 (attached as .config)
compiler: gcc-6 (Debian 6.4.0-9) 6.4.0 20171026
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

drivers/media/dvb-core/dvb_frontend.c:1992:4: error: unknown type name 'compat_uptr_t'

       compat_uptr_t reserved2;
       ^~~~~~~~~~~~~
   drivers/media/dvb-core/dvb_frontend.c:2000:2: error: unknown type name 'compat_uptr_t'
     compat_uptr_t props;
     ^~~~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c: In function 'dvb_frontend_handle_compat_ioctl':
   drivers/media/dvb-core/dvb_frontend.c:2018:29: error: implicit declaration of function 'compat_ptr' [-Werror=implicit-function-declaration]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/media/dvb-core/dvb_frontend.c:2018:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   drivers/media/dvb-core/dvb_frontend.c:2018:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
>> drivers/media/dvb-core/dvb_frontend.c:2018:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2018:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
>> drivers/media/dvb-core/dvb_frontend.c:2018:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2018:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
>> drivers/media/dvb-core/dvb_frontend.c:2018:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   drivers/media/dvb-core/dvb_frontend.c:2030:21: warning: passing argument 1 of 'memdup_user' makes pointer from integer without a cast [-Wint-conversion]
      tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
                        ^~~~~~~~~~
   In file included from drivers/media/dvb-core/dvb_frontend.c:30:0:
   include/linux/string.h:13:14: note: expected 'const void *' but argument is of type 'int'
    extern void *memdup_user(const void __user *, size_t);
                 ^~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2049:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2049:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2049:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2049:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2049:29: warning: passing argument 2 of 'copy_from_user' makes pointer from integer without a cast [-Wint-conversion]
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
                                ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2049:3: note: in expansion of macro 'if'
      if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
      ^~
   In file included from include/linux/poll.h:12:0,
                    from drivers/media/dvb-core/dvb_frontend.c:35:
   include/linux/uaccess.h:144:1: note: expected 'const void *' but argument is of type 'int'
    copy_from_user(void *to, const void __user *from, unsigned long n)
    ^~~~~~~~~~~~~~
   drivers/media/dvb-core/dvb_frontend.c:2061:21: warning: passing argument 1 of 'memdup_user' makes pointer from integer without a cast [-Wint-conversion]
      tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
                        ^~~~~~~~~~
   In file included from drivers/media/dvb-core/dvb_frontend.c:30:0:
   include/linux/string.h:13:14: note: expected 'const void *' but argument is of type 'int'
    extern void *memdup_user(const void __user *, size_t);
                 ^~~~~~~~~~~
   In file included from include/linux/string.h:6:0,
                    from drivers/media/dvb-core/dvb_frontend.c:30:
   drivers/media/dvb-core/dvb_frontend.c:2087:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
                       ^
   include/linux/compiler.h:58:30: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                 ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2087:3: note: in expansion of macro 'if'
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
      ^~
   drivers/media/dvb-core/dvb_frontend.c:2087:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
                       ^
   include/linux/compiler.h:58:42: note: in definition of macro '__trace_if'
     if (__builtin_constant_p(!!(cond)) ? !!(cond) :   \
                                             ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2087:3: note: in expansion of macro 'if'
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
      ^~
   drivers/media/dvb-core/dvb_frontend.c:2087:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
                       ^
   include/linux/compiler.h:69:16: note: in definition of macro '__trace_if'
      ______r = !!(cond);     \
                   ^~~~
   drivers/media/dvb-core/dvb_frontend.c:2087:3: note: in expansion of macro 'if'
      if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
      ^~

drivers/media/dvb-core/dvb_frontend.c: At top level:
include/linux/compiler.h:64:4: warning: '______f' is static but declared in inline function 'strcpy' which is not static

vim +/if +2018 drivers/media/dvb-core/dvb_frontend.c

2005
2006 static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd,
2007 unsigned long arg)
2008 {
2009 struct dvb_device *dvbdev = file->private_data;
2010 struct dvb_frontend *fe = dvbdev->priv;
2011 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2012 int i, err = 0;
2013
2014 if (cmd == COMPAT_FE_SET_PROPERTY) {
2015 struct compat_dtv_properties prop, *tvps = NULL;
2016 struct compat_dtv_property *tvp = NULL;
2017

> 2018			if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))

2019 return -EFAULT;
2020
2021 tvps = &prop;
2022
2023 /*
2024 * Put an arbitrary limit on the number of messages that can
2025 * be sent at once
2026 */
2027 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
2028 return -EINVAL;
2029
2030 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
2031 if (IS_ERR(tvp))
2032 return PTR_ERR(tvp);
2033
2034 for (i = 0; i < tvps->num; i++) {
2035 err = dtv_property_process_set(fe, file,
2036 (tvp + i)->cmd,
2037 (tvp + i)->u.data);
2038 if (err < 0) {
2039 kfree(tvp);
2040 return err;
2041 }
2042 }
2043 kfree(tvp);
2044 } else if (cmd == COMPAT_FE_GET_PROPERTY) {
2045 struct compat_dtv_properties prop, *tvps = NULL;
2046 struct compat_dtv_property *tvp = NULL;
2047 struct dtv_frontend_properties getp = fe->dtv_property_cache;
2048
2049 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop)))
2050 return -EFAULT;
2051
2052 tvps = &prop;
2053
2054 /*
2055 * Put an arbitrary limit on the number of messages that can
2056 * be sent at once
2057 */
2058 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS))
2059 return -EINVAL;
2060
2061 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp));
2062 if (IS_ERR(tvp))
2063 return PTR_ERR(tvp);
2064
2065 /*
2066 * Let's use our own copy of property cache, in order to
2067 * avoid mangling with DTV zigzag logic, as drivers might
2068 * return crap, if they don't check if the data is available
2069 * before updating the properties cache.
2070 */
2071 if (fepriv->state != FESTATE_IDLE) {
2072 err = dtv_get_frontend(fe, &getp, NULL);
2073 if (err < 0) {
2074 kfree(tvp);
2075 return err;
2076 }
2077 }
2078 for (i = 0; i < tvps->num; i++) {
2079 err = dtv_property_process_get(
2080 fe, &getp, (struct dtv_property *)tvp + i, file);
2081 if (err < 0) {
2082 kfree(tvp);
2083 return err;
2084 }
2085 }
2086
2087 if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp,
2088 tvps->num * sizeof(struct compat_dtv_property))) {
2089 kfree(tvp);
2090 return -EFAULT;
2091 }
2092 kfree(tvp);
2093 }
2094
2095 return err;
2096 }
2097

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#9 lore raw
In reply to Mauro Carvalho Chehab

Em Sat, 2 Dec 2017 05:50:47 -0200
Mauro Carvalho Chehab <[email protected]> escreveu:

Quoted 13 lines

Hi Jaedon,

Em Fri, 1 Dec 2017 21:31:27 +0900
Jaedon Shin <[email protected]> escreveu:

Quoted 7 lines

This patch series supports compat ioctl for 32-bit user space applications in 64-bit system.

Jaedon Shin (3):
media: dvb_frontend: Add unlocked_ioctl in dvb_frontend.c
media: dvb_frontend: Add compat_ioctl callback
media: dvb_frontend: Add commands implementation for compat ioct

Quoted 7 lines

Thanks for the series. Yeah, indeed we need something like that.

Yet, I suspect that you should also move the logic inside dvb_frontend_handle_ioctl() with copies from/to userspace.

We don't want the logic there to be called when a 32-bit userspace copy happens, as it should now use the new compat32 code.

Nevermind. After reviewing it carefully, the way you handled is OK.

It seems that only thing you missed were to add an include to linux/compat.h.

I'll add it and apply this patchset.

Thanks,
Mauro