usb: gadget: f_uac2: fix memory leak in sample rate store
PATCH 1/1 usb: gadget: f_uac2: fix memory leak in sample rate store
f_uac2_opts_{p,c}_srate_store() duplicate the input page with kstrdup() and then tokenize it with strsep(&split_page, ","). strsep() advances the pointer it is given, so by the time the parsing loop finishes split_page points at the end of the string (or NULL). The subsequent kfree(split_page) therefore frees the wrong pointer (NULL when the whole buffer was consumed), leaking the buffer allocated by kstrdup():
BUG: memory leak
unreferenced object 0xffff888112a01e00 (size 64):
kstrdup
f_uac2_opts_c_srate_store
configfs_write_iter
vfs_write
ksys_writeKeep the original allocation in split_page and hand a separate iterator to strsep(), so the buffer is always freed. While at it, handle a kstrdup() failure instead of dereferencing NULL. Both the p_srate and c_srate attributes use the same macro and are fixed together.
--- drivers/usb/gadget/function/f_uac2.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_uac2.c +7 -1
@@ -2013,6 +2013,7 @@ static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \ { \struct f_uac2_opts *opts =to_f_uac2_opts (item); \char *split_page =NULL ; \ +char *rest; \int ret = -EINVAL ; \char *token; \u32 num; \
@@ -2027,7 +2028,12 @@ static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \ i =0 ; \ memset(opts->name##s, 0x00, sizeof(opts->name##s)); \ split_page = kstrdup(page, GFP_KERNEL); \ - while ((token = strsep(&split_page, ",")) != NULL) { \ + if (!split_page) { \ + ret = -ENOMEM; \ + goto end; \ + } \ + rest = split_page; \ + while ((token = strsep(&rest, ",")) != NULL) { \ ret = kstrtou32(token, 0, &num); \ if (ret) \ goto end; \
--
2.47.3