Skip to content

Commit dca70ed

Browse files
Jim Linwuliangfeng
authored andcommitted
CHROMIUM: usb: gadget: configfs: Fix KASAN use-after-free
When gadget is disconnected, running sequence is like this. . composite_disconnect . Call trace: usb_string_copy+0xd0/0x128 gadget_config_name_configuration_store+0x4 gadget_config_name_attr_store+0x40/0x50 configfs_write_file+0x198/0x1f4 vfs_write+0x100/0x220 SyS_write+0x58/0xa8 . configfs_composite_unbind . configfs_composite_bind In configfs_composite_bind, it has "cn->strings.s = cn->configuration;" When usb_string_copy is invoked. it would allocate memory, copy input string, release previous pointed memory space, and use new allocated memory. When gadget is connected, host sends down request to get information. Call trace: usb_gadget_get_string+0xec/0x168 lookup_string+0x64/0x98 composite_setup+0xa34/0x1ee8 If gadget is disconnected and connected quickly, in the failed case, cn->configuration memory has been released by usb_string_copy kfree but configfs_composite_bind hasn't been run in time to assign new allocated "cn->configuration" pointer to "cn->strings.s". When "strlen(s->s) of usb_gadget_get_string is being executed, the dangling memory is accessed, "BUG: KASAN: use-after-free" error occurs. BUG=chrome-os-partner:58412 TEST=After smaug device was connected to ubuntu PC host, detached and attached type-C cable quickly several times without seeing "BUG: KASAN: use-after-free in usb_gadget_get_string". Change-Id: I58240ee7c55ae8f8fb8597d14f09c5ac07abb032 Signed-off-by: Jim Lin <jilin@nvidia.com> Reviewed-on: https://chromium-review.googlesource.com/428059 Commit-Ready: Jim Lin <jilin%nvidia.com@gtempaccount.com> Tested-by: Jim Lin <jilin%nvidia.com@gtempaccount.com> Reviewed-by: Adrian Salido <salidoa@google.com> Reviewed-by: Benson Leung <bleung@chromium.org> Git-repo: https://chromium.googlesource.com/chromiumos/third_party/kernel Git-commit: a7b597d255d70f6f0c6bfdfb7e4e04f67fcebf9d Signed-off-by: Dennis Cagle <d-cagle@codeaurora.org> Signed-off-by: Tao Huang <huangtao@rock-chips.com> (cherry picked from https://android.googlesource.com/kernel/msm commit fcf0e822fce098b27b6735d96e5414d6ff8de762)
1 parent 8d1ab20 commit dca70ed

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

drivers/usb/gadget/configfs.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,28 @@ struct gadget_config_name {
138138
struct list_head list;
139139
};
140140

141+
#define MAX_USB_STRING_LEN 126
142+
#define MAX_USB_STRING_WITH_NULL_LEN (MAX_USB_STRING_LEN+1)
143+
141144
static int usb_string_copy(const char *s, char **s_copy)
142145
{
143146
int ret;
144147
char *str;
145148
char *copy = *s_copy;
146149
ret = strlen(s);
147-
if (ret > 126)
150+
if (ret > MAX_USB_STRING_LEN)
148151
return -EOVERFLOW;
149152

150-
str = kstrdup(s, GFP_KERNEL);
151-
if (!str)
152-
return -ENOMEM;
153+
if (copy) {
154+
str = copy;
155+
} else {
156+
str = kmalloc(MAX_USB_STRING_WITH_NULL_LEN, GFP_KERNEL);
157+
if (!str)
158+
return -ENOMEM;
159+
}
160+
strncpy(str, s, MAX_USB_STRING_WITH_NULL_LEN);
153161
if (str[ret - 1] == '\n')
154162
str[ret - 1] = '\0';
155-
kfree(copy);
156163
*s_copy = str;
157164
return 0;
158165
}

0 commit comments

Comments
 (0)