Skip to contentSkip to conversation

usb: gadget: f_uac2: fix memory leak in sample rate store

PATCH New Rituparna Warwatkar 1 participant 1 message Aug 2 USB

lore
Share & tools
lore
#0 lore raw

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_write

Keep 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.

Fixes a7339e4f5788 ("usb: gadget: f_uac2: Support multiple sampling rates")
Reported-by [email protected]
---
 drivers/usb/gadget/function/f_uac2.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/usb/gadget/function/f_uac2.c +7 -1
index 897787d0803..8facf289710 100644
@@ -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