Skip to content

Commit

Permalink
usb: gadget: f_midi: Fix memleak in f_midi_alloc
Browse files Browse the repository at this point in the history
In the error path, if midi is not null, we should
free the midi->id if necessary to prevent memleak.

Fixes: b85e9de ("usb: gadget: f_midi: convert to new function interface with backward compatibility")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
Link: https://lore.kernel.org/r/20201117021629.1470544-2-zhangqilong3@huawei.com
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Zhang Qilong authored and Greg Kroah-Hartman committed Nov 20, 2020
1 parent 9ca5751 commit e7694cb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions drivers/usb/gadget/function/f_midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
midi->id = kstrdup(opts->id, GFP_KERNEL);
if (opts->id && !midi->id) {
status = -ENOMEM;
goto setup_fail;
goto midi_free;
}
midi->in_ports = opts->in_ports;
midi->out_ports = opts->out_ports;
Expand All @@ -1327,7 +1327,7 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)

status = kfifo_alloc(&midi->in_req_fifo, midi->qlen, GFP_KERNEL);
if (status)
goto setup_fail;
goto midi_free;

spin_lock_init(&midi->transmit_lock);

Expand All @@ -1343,9 +1343,13 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)

return &midi->func;

midi_free:
if (midi)
kfree(midi->id);
kfree(midi);
setup_fail:
mutex_unlock(&opts->lock);
kfree(midi);

return ERR_PTR(status);
}

Expand Down

0 comments on commit e7694cb

Please sign in to comment.