Vulnerability: Use-after-free vulnerability in the ffs_user_copy_worker function in drivers/usb/gadget/function/f_fs.c in the Linux kernel before 4.5.3 allows local users to gain privileges by accessing an I/O data structure after a certain callback call.
Vulnerability ID: CVE-2016-7912
CWE: CWE-416
Severity score: 10
Project: https://github.com/torvalds/linux
Published: 2016-11-16
File: drivers/usb/gadget/function/f_fs.c
Commit message: usb: gadget: f_fs: Fix use-after-free

When using asynchronous read or write operations on the USB endpoints the
issuer of the IO request is notified by calling the ki_complete() callback
of the submitted kiocb when the URB has been completed.

Calling this ki_complete() callback will free kiocb. Make sure that the
structure is no longer accessed beyond that point, otherwise undefined
behaviour might occur.

Fixes: 2e4c7553cd6f ("usb: gadget: f_fs: add aio support")
Cc: <stable@vger.kernel.org> # v3.15+
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Commit author: Lars-Peter Clausen

Vulnerable version (before fix):
 static void ffs_user_copy_worker(struct work_struct *work) 						   work); 	int ret = io_data->req->status ? io_data->req->status : 					 io_data->req->actual;  	if (io_data->read && ret > 0) { 		use_mm(io_data->mm); static void ffs_user_copy_worker(struct work_struct *work)  	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);  	if (io_data->ffs->ffs_eventfd && 	    !(io_data->kiocb->ki_flags & IOCB_EVENTFD)) 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);  	usb_ep_free_request(io_data->ep, io_data->req);  	io_data->kiocb->private = NULL; 	if (io_data->read) 		kfree(io_data->to_free); 	kfree(io_data->buf);

Fixed version (after fix):
 static void ffs_user_copy_worker(struct work_struct *work) 						   work); 	int ret = io_data->req->status ? io_data->req->status : 					 io_data->req->actual; 	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;  	if (io_data->read && ret > 0) { 		use_mm(io_data->mm); static void ffs_user_copy_worker(struct work_struct *work)  	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);  	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd) 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);  	usb_ep_free_request(io_data->ep, io_data->req);  	if (io_data->read) 		kfree(io_data->to_free); 	kfree(io_data->buf);

Patch:
patch: @@ -646,6 +646,7 @@ static void ffs_user_copy_worker(struct work_struct *work)
 						   work);
 	int ret = io_data->req->status ? io_data->req->status :
 					 io_data->req->actual;
+	bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
 
 	if (io_data->read && ret > 0) {
 		use_mm(io_data->mm);
@@ -657,13 +658,11 @@ static void ffs_user_copy_worker(struct work_struct *work)
 
 	io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
 
-	if (io_data->ffs->ffs_eventfd &&
-	    !(io_data->kiocb->ki_flags & IOCB_EVENTFD))
+	if (io_data->ffs->ffs_eventfd && !kiocb_has_eventfd)
 		eventfd_signal(io_data->ffs->ffs_eventfd, 1);
 
 	usb_ep_free_request(io_data->ep, io_data->req);
 
-	io_data->kiocb->private = NULL;
 	if (io_data->read)
 		kfree(io_data->to_free);
 	kfree(io_data->buf);
path: drivers/usb/gadget/function/f_fs.c