summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSu Hui <suhui@nfschina.com>2024-01-25 14:32:26 +0800
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2024-02-05 20:17:07 +0000
commitaf6cf5a166641ebc8dbf5fa1697d0cfe327c80d3 (patch)
treeff11caf430723c13c080a6b4dc857762103de0ac
parent07e3ca0f17f579491b5f54e9ed05173d6c1d6fcb (diff)
downloadlinux-stable-af6cf5a166641ebc8dbf5fa1697d0cfe327c80d3.tar.gz
linux-stable-af6cf5a166641ebc8dbf5fa1697d0cfe327c80d3.tar.bz2
linux-stable-af6cf5a166641ebc8dbf5fa1697d0cfe327c80d3.zip
HID: hidraw: fix a problem of memory leak in hidraw_release()
[ Upstream commit a3bdcdd022c68942a774e8e63424cc11c85aab78 ] '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> Signed-off-by: Sasha Levin <sashal@kernel.org>
-rw-r--r--drivers/hid/hidraw.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/drivers/hid/hidraw.c b/drivers/hid/hidraw.c
index 13c8dd8cd350..2bc762d31ac7 100644
--- a/drivers/hid/hidraw.c
+++ b/drivers/hid/hidraw.c
@@ -357,8 +357,11 @@ static int hidraw_release(struct inode * inode, struct file * file)
down_write(&minors_rwsem);
spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
- for (int 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);