Skip to contentSkip to conversation

usb: gadget: Don't allocate zero-length buffers

DISCUSSION Greg KH 1 participant 1 message Mar 20, 2019 USB

lore
Share & tools
lore

Greg KH

author
#0 lore raw
Quoted 24 lines · Andrzej Pietrasiewicz

Don't actually allocate anything if userspace enqueues a zero-length buffer. Otherwise vmalloc of zero-sized area will be attempted in ffs_build_sg_list().

---
 drivers/usb/gadget/function/f_fs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_fs.c +3 -0
index 08357c5dd026..5cecfe3e9395 100644
@@ -797,6 +797,9 @@ static void *ffs_build_sg_list(struct sg_table *sgt, size_t sz)
 static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
 	size_t data_len)
 {
+	if (!data_len)
+		return ZERO_SIZE_PTR;
+
 	if (io_data->use_sg)
 		return ffs_build_sg_list(&io_data->sgt, data_len);
 

drivers/usb/gadget/function/f_fs.c static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data, · line 814

Quoted 8 lines
drivers/usb/gadget/function/f_fs.c +1 -1
@@ -805,7 +808,7 @@ static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
 
 static inline void ffs_free_buffer(struct ffs_io_data *io_data)
 {
-	if (!io_data->buf)
+	if (ZERO_OR_NULL_PTR(io_data->buf))
 		return;
 
 	if (io_data->use_sg) {

Are you sure this is ok? zero length packets mean something in the USB protocol :)

What is the problem of trying to allocate a buffer of 0 bytes?

thanks,

greg k-h