linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_fs: report error if excess data received
@ 2016-05-11 10:19 changbin.du
  2016-05-11 10:59 ` Felipe Balbi
  2016-05-16 16:05 ` Michal Nazarewicz
  0 siblings, 2 replies; 38+ messages in thread
From: changbin.du @ 2016-05-11 10:19 UTC (permalink / raw)
  To: balbi
  Cc: gregkh, mina86, rui.silva, k.opasiak, lars, linux-usb,
	linux-kernel, Du, Changbin

From: "Du, Changbin" <changbin.du@intel.com>

Since the buffer size for req is rounded up to maxpacketsize,
then we may end up with more data then user space has space
for.

If it happen, we can keep the excess data for next i/o, or
report an error. But we cannot silently drop data, because
USB layer should ensure the data integrality it has transferred,
otherwise applications may get corrupt data if it doesn't
detect this case.

Here, we simply report an error to userspace to let userspace
proccess. Actually, userspace applications should negotiate
with host side for how many bytes it should receive.

Signed-off-by: Du, Changbin <changbin.du@intel.com>
---
 drivers/usb/gadget/function/f_fs.c | 48 +++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 15b648c..411ed2d 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -640,6 +640,36 @@ static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
 	}
 }
 
+static size_t ffs_copy_to_user(const void *buf, size_t bytes,
+				struct ffs_io_data *io_data)
+{
+	size_t count = iov_iter_count(&io_data->data);
+	int ret;
+
+	/**
+	 * Since the buffer size for req is rounded up to maxpacketsize,
+	 * then we may end up with more data then user space has space for.
+	 * We can keep the excess data for next i/o, or report an error.
+	 * But we cannot silently drop data, because USB layer should ensure
+	 * the data integrality it has transferred.
+	 *
+	 * Here, we simply report an error to userspace to let userspace
+	 * proccess. Actually, userspace applications should negotiate with
+	 * each other for how many bytes host send.
+	 */
+	if (bytes > count) {
+		pr_err("ffs read size %zu bigger than requested size %zu\n",
+			bytes, count);
+		return -EOVERFLOW;
+	}
+
+	ret = copy_to_iter(buf, bytes, &io_data->data);
+	if (ret != bytes)
+		return -EFAULT;
+
+	return ret;
+}
+
 static void ffs_user_copy_worker(struct work_struct *work)
 {
 	struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
@@ -650,9 +680,7 @@ static void ffs_user_copy_worker(struct work_struct *work)
 
 	if (io_data->read && ret > 0) {
 		use_mm(io_data->mm);
-		ret = copy_to_iter(io_data->buf, ret, &io_data->data);
-		if (iov_iter_count(&io_data->data))
-			ret = -EFAULT;
+		ret = ffs_copy_to_user(io_data->buf, ret, io_data);
 		unuse_mm(io_data->mm);
 	}
 
@@ -803,18 +831,10 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
 			interrupted = ep->status < 0;
 		}
 
-		/*
-		 * XXX We may end up silently droping data here.  Since data_len
-		 * (i.e. req->length) may be bigger than len (after being
-		 * rounded up to maxpacketsize), we may end up with more data
-		 * then user space has space for.
-		 */
 		ret = interrupted ? -EINTR : ep->status;
-		if (io_data->read && ret > 0) {
-			ret = copy_to_iter(data, ret, &io_data->data);
-			if (!ret)
-				ret = -EFAULT;
-		}
+		if (io_data->read && ret > 0)
+			ret = ffs_copy_to_user(data, ret, io_data);
+
 		goto error_mutex;
 	} else if (!(req = usb_ep_alloc_request(ep->ep, GFP_KERNEL))) {
 		ret = -ENOMEM;
-- 
2.7.4

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

end of thread, other threads:[~2016-05-19  8:50 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-11 10:19 [PATCH] usb: gadget: f_fs: report error if excess data received changbin.du
2016-05-11 10:59 ` Felipe Balbi
2016-05-11 12:30   ` Michal Nazarewicz
2016-05-12  4:25     ` Du, Changbin
2016-05-12  4:21   ` Du, Changbin
2016-05-12  6:52     ` Felipe Balbi
2016-05-12  7:30       ` Du, Changbin
2016-05-12  7:46         ` Felipe Balbi
2016-05-12  8:16           ` Du, Changbin
2016-05-12  9:15             ` Felipe Balbi
2016-05-12  9:22               ` Felipe Balbi
2016-05-12  9:51                 ` Du, Changbin
2016-05-12  9:39               ` Du, Changbin
2016-05-12 10:13                 ` Felipe Balbi
2016-05-12 10:14                 ` Felipe Balbi
2016-05-12 10:45                   ` Du, Changbin
2016-05-12 11:22                     ` Felipe Balbi
2016-05-13  5:52                       ` Du, Changbin
2016-05-13  6:36                         ` Felipe Balbi
2016-05-13 10:32                           ` Du, Changbin
2016-05-13 14:29                           ` Alan Stern
2016-05-14 20:39                             ` Michal Nazarewicz
2016-05-16 12:57                             ` Felipe Balbi
2016-05-16 13:08                               ` Michal Nazarewicz
2016-05-16 13:16                                 ` Felipe Balbi
2016-05-16 19:09                                   ` Michal Nazarewicz
2016-05-17  2:53                                     ` Du, Changbin
2016-05-18  9:45                                       ` Michal Nazarewicz
2016-05-18 10:15                                         ` Felipe Balbi
2016-05-18 13:39                                           ` Michal Nazarewicz
2016-05-19  2:54                                             ` Du, Changbin
2016-05-19  7:34                                               ` Michal Nazarewicz
2016-05-19  8:49                                                 ` Du, Changbin
2016-05-19  2:31                                           ` Du, Changbin
2016-05-16 16:05 ` Michal Nazarewicz
2016-05-16 16:27   ` Lars-Peter Clausen
2016-05-16 16:48     ` Michal Nazarewicz
2016-05-16 16:35   ` Krzysztof Opasiak

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).