Commit f788baad authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'gadget' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs

Pull gadgetfs fixes from Al Viro:
 "Assorted fixes around AIO on gadgetfs: leaks, use-after-free, troubles
  caused by ->f_op flipping"

* 'gadget' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs:
  gadgetfs: really get rid of switching ->f_op
  gadgetfs: get rid of flipping ->f_op in ep_config()
  gadget: switch ep_io_operations to ->read_iter/->write_iter
  gadgetfs: use-after-free in ->aio_read()
  gadget/function/f_fs.c: switch to ->{read,write}_iter()
  gadget/function/f_fs.c: use put iov_iter into io_data
  gadget/function/f_fs.c: close leaks
  move iov_iter.c from mm/ to lib/
  new helper: dup_iter()
parents c202baf0 96b62a57
...@@ -144,10 +144,9 @@ struct ffs_io_data { ...@@ -144,10 +144,9 @@ struct ffs_io_data {
bool read; bool read;
struct kiocb *kiocb; struct kiocb *kiocb;
const struct iovec *iovec; struct iov_iter data;
unsigned long nr_segs; const void *to_free;
char __user *buf; char *buf;
size_t len;
struct mm_struct *mm; struct mm_struct *mm;
struct work_struct work; struct work_struct work;
...@@ -649,29 +648,10 @@ static void ffs_user_copy_worker(struct work_struct *work) ...@@ -649,29 +648,10 @@ static void ffs_user_copy_worker(struct work_struct *work)
io_data->req->actual; io_data->req->actual;
if (io_data->read && ret > 0) { if (io_data->read && ret > 0) {
int i;
size_t pos = 0;
/*
* Since req->length may be bigger than io_data->len (after
* being rounded up to maxpacketsize), we may end up with more
* data then user space has space for.
*/
ret = min_t(int, ret, io_data->len);
use_mm(io_data->mm); use_mm(io_data->mm);
for (i = 0; i < io_data->nr_segs; i++) { ret = copy_to_iter(io_data->buf, ret, &io_data->data);
size_t len = min_t(size_t, ret - pos, if (iov_iter_count(&io_data->data))
io_data->iovec[i].iov_len);
if (!len)
break;
if (unlikely(copy_to_user(io_data->iovec[i].iov_base,
&io_data->buf[pos], len))) {
ret = -EFAULT; ret = -EFAULT;
break;
}
pos += len;
}
unuse_mm(io_data->mm); unuse_mm(io_data->mm);
} }
...@@ -684,7 +664,7 @@ static void ffs_user_copy_worker(struct work_struct *work) ...@@ -684,7 +664,7 @@ static void ffs_user_copy_worker(struct work_struct *work)
io_data->kiocb->private = NULL; io_data->kiocb->private = NULL;
if (io_data->read) if (io_data->read)
kfree(io_data->iovec); kfree(io_data->to_free);
kfree(io_data->buf); kfree(io_data->buf);
kfree(io_data); kfree(io_data);
} }
...@@ -743,6 +723,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) ...@@ -743,6 +723,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
* before the waiting completes, so do not assign to 'gadget' earlier * before the waiting completes, so do not assign to 'gadget' earlier
*/ */
struct usb_gadget *gadget = epfile->ffs->gadget; struct usb_gadget *gadget = epfile->ffs->gadget;
size_t copied;
spin_lock_irq(&epfile->ffs->eps_lock); spin_lock_irq(&epfile->ffs->eps_lock);
/* In the meantime, endpoint got disabled or changed. */ /* In the meantime, endpoint got disabled or changed. */
...@@ -750,34 +731,21 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) ...@@ -750,34 +731,21 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
spin_unlock_irq(&epfile->ffs->eps_lock); spin_unlock_irq(&epfile->ffs->eps_lock);
return -ESHUTDOWN; return -ESHUTDOWN;
} }
data_len = iov_iter_count(&io_data->data);
/* /*
* Controller may require buffer size to be aligned to * Controller may require buffer size to be aligned to
* maxpacketsize of an out endpoint. * maxpacketsize of an out endpoint.
*/ */
data_len = io_data->read ? if (io_data->read)
usb_ep_align_maybe(gadget, ep->ep, io_data->len) : data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
io_data->len;
spin_unlock_irq(&epfile->ffs->eps_lock); spin_unlock_irq(&epfile->ffs->eps_lock);
data = kmalloc(data_len, GFP_KERNEL); data = kmalloc(data_len, GFP_KERNEL);
if (unlikely(!data)) if (unlikely(!data))
return -ENOMEM; return -ENOMEM;
if (io_data->aio && !io_data->read) { if (!io_data->read) {
int i; copied = copy_from_iter(data, data_len, &io_data->data);
size_t pos = 0; if (copied != data_len) {
for (i = 0; i < io_data->nr_segs; i++) {
if (unlikely(copy_from_user(&data[pos],
io_data->iovec[i].iov_base,
io_data->iovec[i].iov_len))) {
ret = -EFAULT;
goto error;
}
pos += io_data->iovec[i].iov_len;
}
} else {
if (!io_data->read &&
unlikely(__copy_from_user(data, io_data->buf,
io_data->len))) {
ret = -EFAULT; ret = -EFAULT;
goto error; goto error;
} }
...@@ -876,10 +844,8 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) ...@@ -876,10 +844,8 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
*/ */
ret = ep->status; ret = ep->status;
if (io_data->read && ret > 0) { if (io_data->read && ret > 0) {
ret = min_t(size_t, ret, io_data->len); ret = copy_to_iter(data, ret, &io_data->data);
if (unlikely(iov_iter_count(&io_data->data)))
if (unlikely(copy_to_user(io_data->buf,
data, ret)))
ret = -EFAULT; ret = -EFAULT;
} }
} }
...@@ -898,37 +864,6 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) ...@@ -898,37 +864,6 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
return ret; return ret;
} }
static ssize_t
ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
loff_t *ptr)
{
struct ffs_io_data io_data;
ENTER();
io_data.aio = false;
io_data.read = false;
io_data.buf = (char * __user)buf;
io_data.len = len;
return ffs_epfile_io(file, &io_data);
}
static ssize_t
ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
{
struct ffs_io_data io_data;
ENTER();
io_data.aio = false;
io_data.read = true;
io_data.buf = buf;
io_data.len = len;
return ffs_epfile_io(file, &io_data);
}
static int static int
ffs_epfile_open(struct inode *inode, struct file *file) ffs_epfile_open(struct inode *inode, struct file *file)
{ {
...@@ -965,67 +900,86 @@ static int ffs_aio_cancel(struct kiocb *kiocb) ...@@ -965,67 +900,86 @@ static int ffs_aio_cancel(struct kiocb *kiocb)
return value; return value;
} }
static ssize_t ffs_epfile_aio_write(struct kiocb *kiocb, static ssize_t ffs_epfile_write_iter(struct kiocb *kiocb, struct iov_iter *from)
const struct iovec *iovec,
unsigned long nr_segs, loff_t loff)
{ {
struct ffs_io_data *io_data; struct ffs_io_data io_data, *p = &io_data;
ssize_t res;
ENTER(); ENTER();
io_data = kmalloc(sizeof(*io_data), GFP_KERNEL); if (!is_sync_kiocb(kiocb)) {
if (unlikely(!io_data)) p = kmalloc(sizeof(io_data), GFP_KERNEL);
if (unlikely(!p))
return -ENOMEM; return -ENOMEM;
p->aio = true;
} else {
p->aio = false;
}
io_data->aio = true; p->read = false;
io_data->read = false; p->kiocb = kiocb;
io_data->kiocb = kiocb; p->data = *from;
io_data->iovec = iovec; p->mm = current->mm;
io_data->nr_segs = nr_segs;
io_data->len = kiocb->ki_nbytes;
io_data->mm = current->mm;
kiocb->private = io_data; kiocb->private = p;
kiocb_set_cancel_fn(kiocb, ffs_aio_cancel); kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
return ffs_epfile_io(kiocb->ki_filp, io_data); res = ffs_epfile_io(kiocb->ki_filp, p);
if (res == -EIOCBQUEUED)
return res;
if (p->aio)
kfree(p);
else
*from = p->data;
return res;
} }
static ssize_t ffs_epfile_aio_read(struct kiocb *kiocb, static ssize_t ffs_epfile_read_iter(struct kiocb *kiocb, struct iov_iter *to)
const struct iovec *iovec,
unsigned long nr_segs, loff_t loff)
{ {
struct ffs_io_data *io_data; struct ffs_io_data io_data, *p = &io_data;
struct iovec *iovec_copy; ssize_t res;
ENTER(); ENTER();
iovec_copy = kmalloc_array(nr_segs, sizeof(*iovec_copy), GFP_KERNEL); if (!is_sync_kiocb(kiocb)) {
if (unlikely(!iovec_copy)) p = kmalloc(sizeof(io_data), GFP_KERNEL);
if (unlikely(!p))
return -ENOMEM; return -ENOMEM;
p->aio = true;
} else {
p->aio = false;
}
memcpy(iovec_copy, iovec, sizeof(struct iovec)*nr_segs); p->read = true;
p->kiocb = kiocb;
io_data = kmalloc(sizeof(*io_data), GFP_KERNEL); if (p->aio) {
if (unlikely(!io_data)) { p->to_free = dup_iter(&p->data, to, GFP_KERNEL);
kfree(iovec_copy); if (!p->to_free) {
kfree(p);
return -ENOMEM; return -ENOMEM;
} }
} else {
p->data = *to;
p->to_free = NULL;
}
p->mm = current->mm;
io_data->aio = true; kiocb->private = p;
io_data->read = true;
io_data->kiocb = kiocb;
io_data->iovec = iovec_copy;
io_data->nr_segs = nr_segs;
io_data->len = kiocb->ki_nbytes;
io_data->mm = current->mm;
kiocb->private = io_data;
kiocb_set_cancel_fn(kiocb, ffs_aio_cancel); kiocb_set_cancel_fn(kiocb, ffs_aio_cancel);
return ffs_epfile_io(kiocb->ki_filp, io_data); res = ffs_epfile_io(kiocb->ki_filp, p);
if (res == -EIOCBQUEUED)
return res;
if (p->aio) {
kfree(p->to_free);
kfree(p);
} else {
*to = p->data;
}
return res;
} }
static int static int
...@@ -1105,10 +1059,10 @@ static const struct file_operations ffs_epfile_operations = { ...@@ -1105,10 +1059,10 @@ static const struct file_operations ffs_epfile_operations = {
.llseek = no_llseek, .llseek = no_llseek,
.open = ffs_epfile_open, .open = ffs_epfile_open,
.write = ffs_epfile_write, .write = new_sync_write,
.read = ffs_epfile_read, .read = new_sync_read,
.aio_write = ffs_epfile_aio_write, .write_iter = ffs_epfile_write_iter,
.aio_read = ffs_epfile_aio_read, .read_iter = ffs_epfile_read_iter,
.release = ffs_epfile_release, .release = ffs_epfile_release,
.unlocked_ioctl = ffs_epfile_ioctl, .unlocked_ioctl = ffs_epfile_ioctl,
}; };
......
This diff is collapsed.
...@@ -98,6 +98,8 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages, ...@@ -98,6 +98,8 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
size_t maxsize, size_t *start); size_t maxsize, size_t *start);
int iov_iter_npages(const struct iov_iter *i, int maxpages); int iov_iter_npages(const struct iov_iter *i, int maxpages);
const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
static inline size_t iov_iter_count(struct iov_iter *i) static inline size_t iov_iter_count(struct iov_iter *i)
{ {
return i->count; return i->count;
......
...@@ -24,7 +24,7 @@ obj-y += lockref.o ...@@ -24,7 +24,7 @@ obj-y += lockref.o
obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
gcd.o lcm.o list_sort.o uuid.o flex_array.o clz_ctz.o \ gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \
bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \ bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \
percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o
obj-y += string_helpers.o obj-y += string_helpers.o
......
...@@ -751,3 +751,18 @@ int iov_iter_npages(const struct iov_iter *i, int maxpages) ...@@ -751,3 +751,18 @@ int iov_iter_npages(const struct iov_iter *i, int maxpages)
return npages; return npages;
} }
EXPORT_SYMBOL(iov_iter_npages); EXPORT_SYMBOL(iov_iter_npages);
const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)
{
*new = *old;
if (new->type & ITER_BVEC)
return new->bvec = kmemdup(new->bvec,
new->nr_segs * sizeof(struct bio_vec),
flags);
else
/* iovec and kvec have identical layout */
return new->iov = kmemdup(new->iov,
new->nr_segs * sizeof(struct iovec),
flags);
}
EXPORT_SYMBOL(dup_iter);
...@@ -21,7 +21,7 @@ obj-y := filemap.o mempool.o oom_kill.o \ ...@@ -21,7 +21,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
mm_init.o mmu_context.o percpu.o slab_common.o \ mm_init.o mmu_context.o percpu.o slab_common.o \
compaction.o vmacache.o \ compaction.o vmacache.o \
interval_tree.o list_lru.o workingset.o \ interval_tree.o list_lru.o workingset.o \
iov_iter.o debug.o $(mmu-y) debug.o $(mmu-y)
obj-y += init-mm.o obj-y += init-mm.o
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment