
From: Su Hui <suhui@nfschina.com> mainline inclusion from mainline-v6.8-rc3 commit a3bdcdd022c68942a774e8e63424cc11c85aab78 category: bugfix bugzilla: 190676 CVE: NA Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?i... -------------------------------- 'struct hidraw_list' is a circular queue whose head can be smaller than tail. Using 'list->tail != list->head' to release all memory that should be released. Fixes: a5623a203cff ("HID: hidraw: fix memory leak in hidraw_release()") Signed-off-by: Su Hui <suhui@nfschina.com> Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org> Signed-off-by: Jiri Kosina <jkosina@suse.com> Conflicts: drivers/hid/hidraw.c [context conflict] Signed-off-by: Bowen You <youbowen2@huawei.com> --- drivers/hid/hidraw.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hidraw.c b/drivers/hid/hidraw.c index 788f1fecd626..723069a41f5a 100644 --- a/drivers/hid/hidraw.c +++ b/drivers/hid/hidraw.c @@ -353,13 +353,15 @@ static int hidraw_release(struct inode * inode, struct file * file) unsigned int minor = iminor(inode); struct hidraw_list *list = file->private_data; unsigned long flags; - int i; mutex_lock(&minors_lock); spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags); - for (i = list->tail; i < list->head; i++) - kfree(list->buffer[i].value); + while (list->tail != list->head) { + kfree(list->buffer[list->tail].value); + list->buffer[list->tail].value = NULL; + list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1); + } list_del(&list->node); spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags); kfree(list); -- 2.34.1