Skip to content

Commit

Permalink
usb: gadget: configfs: Fix memory leak of interface directory data
Browse files Browse the repository at this point in the history
Kmemleak checking configuration reports a memory leak in
usb_os_desc_prepare_interf_dir function when rndis function
instance is freed and then allocated again. For example, this
happens with FunctionFS driver with RNDIS function enabled
when "ffs-test" test application is run several times in a row.

The data for intermediate "os_desc" group for interface directories
is allocated as a single VLA chunk and (after a change of default
groups handling) is not ever freed and actually not stored anywhere
besides inside a list of default groups of a parent group.

The fix is to make usb_os_desc_prepare_interf_dir function return
a pointer to allocated data (as a pointer to the first VLA item)
instead of (an unused) integer and to make the caller component
(currently the only one is RNDIS function) responsible for storing
the pointer and freeing the memory when appropriate.

Fixes: 1ae1602 ("configfs: switch ->default groups to a linked list")
Cc: stable@vger.kernel.org
Signed-off-by: Andrew Gabbasov <andrew_gabbasov@mentor.com>
Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
  • Loading branch information
agabbasov authored and Felipe Balbi committed Oct 11, 2017
1 parent aec17e1 commit ff74745
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
15 changes: 8 additions & 7 deletions drivers/usb/gadget/configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,11 +1143,12 @@ static struct configfs_attribute *interf_grp_attrs[] = {
NULL
};

int usb_os_desc_prepare_interf_dir(struct config_group *parent,
int n_interf,
struct usb_os_desc **desc,
char **names,
struct module *owner)
struct config_group *usb_os_desc_prepare_interf_dir(
struct config_group *parent,
int n_interf,
struct usb_os_desc **desc,
char **names,
struct module *owner)
{
struct config_group *os_desc_group;
struct config_item_type *os_desc_type, *interface_type;
Expand All @@ -1159,7 +1160,7 @@ int usb_os_desc_prepare_interf_dir(struct config_group *parent,

char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
if (!vlabuf)
return -ENOMEM;
return ERR_PTR(-ENOMEM);

os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
Expand All @@ -1184,7 +1185,7 @@ int usb_os_desc_prepare_interf_dir(struct config_group *parent,
configfs_add_default_group(&d->group, os_desc_group);
}

return 0;
return os_desc_group;
}
EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);

Expand Down
11 changes: 6 additions & 5 deletions drivers/usb/gadget/configfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

void unregister_gadget_item(struct config_item *item);

int usb_os_desc_prepare_interf_dir(struct config_group *parent,
int n_interf,
struct usb_os_desc **desc,
char **names,
struct module *owner);
struct config_group *usb_os_desc_prepare_interf_dir(
struct config_group *parent,
int n_interf,
struct usb_os_desc **desc,
char **names,
struct module *owner);

static inline struct usb_os_desc *to_usb_os_desc(struct config_item *item)
{
Expand Down
12 changes: 10 additions & 2 deletions drivers/usb/gadget/function/f_rndis.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ static void rndis_free_inst(struct usb_function_instance *f)
free_netdev(opts->net);
}

kfree(opts->rndis_interf_group); /* single VLA chunk */
kfree(opts);
}

Expand All @@ -916,6 +917,7 @@ static struct usb_function_instance *rndis_alloc_inst(void)
struct f_rndis_opts *opts;
struct usb_os_desc *descs[1];
char *names[1];
struct config_group *rndis_interf_group;

opts = kzalloc(sizeof(*opts), GFP_KERNEL);
if (!opts)
Expand All @@ -940,8 +942,14 @@ static struct usb_function_instance *rndis_alloc_inst(void)
names[0] = "rndis";
config_group_init_type_name(&opts->func_inst.group, "",
&rndis_func_type);
usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
names, THIS_MODULE);
rndis_interf_group =
usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
names, THIS_MODULE);
if (IS_ERR(rndis_interf_group)) {
rndis_free_inst(&opts->func_inst);
return ERR_CAST(rndis_interf_group);
}
opts->rndis_interf_group = rndis_interf_group;

return &opts->func_inst;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/usb/gadget/function/u_rndis.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct f_rndis_opts {
bool bound;
bool borrowed_net;

struct config_group *rndis_interf_group;
struct usb_os_desc rndis_os_desc;
char rndis_ext_compat_id[16];

Expand Down

0 comments on commit ff74745

Please sign in to comment.