Skip to contentSkip to conversation

HID: wacom: handle kzalloc() allocation failure in wacom_wac_queue_flush()

PATCH Applied Qasim Ijaz 4 participants 4 messages Mar 29, 2025 – Apr 24, 2025

Subsystems (2)
lore
Share & tools
lore

Qasim Ijaz

author
#0 lore raw

PATCH 1/1 HID: wacom: handle kzalloc() allocation failure in wacom_wac_queue_flush()

During wacom_wac_queue_flush() the code calls kzalloc() to allocate a zero initialised buffer which it uses as a storage buffer to get data from the fifo via kfifo_out(). However it does not check kzalloc() for allocation failure which returns NULL and could potentially lead to a NULL deref.

Fix this by checking for kzalloc() failure and skipping the current entry if allocation failure occurs.

Fixes 5e013ad20689 ("HID: wacom: Remove static WACOM_PKGLEN_MAX limit")
---
 drivers/hid/wacom_sys.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hid/wacom_sys.c +7 -1
index 97393a3083ca..666b7eb0fdfe 100644
@@ -70,10 +70,16 @@ static void wacom_wac_queue_flush(struct hid_device *hdev,
 {
 	while (!kfifo_is_empty(fifo)) {
 		int size = kfifo_peek_len(fifo);
-		u8 *buf = kzalloc(size, GFP_KERNEL);
+		u8 *buf;
 		unsigned int count;
 		int err;
 
+		buf = kzalloc(size, GFP_KERNEL);
+		if (!buf) {
+			kfifo_skip(fifo);
+			continue;
+		}
+
 		count = kfifo_out(fifo, buf, size);
 		if (count != size) {
 			// Hard to say what is the "right" action in this
Signature · 1 line
2.39.5
#1 lore raw

…

Quoted 3 lines

from the fifo via kfifo_out(). However it does not check kzalloc() for allocation failure which returns NULL and could potentially lead to a NULL deref.

…
pointer dereference?

I imagine that word wrapping can occasionally become a bit nicer for text lines which may be longer than 52 characters.

Regards,
Markus

Reviewed-by on v1 1/1
#2 lore raw
In reply to Qasim Ijaz
Quoted 40 lines · Qasim Ijaz

During wacom_wac_queue_flush() the code calls kzalloc() to allocate a zero initialised buffer which it uses as a storage buffer to get data from the fifo via kfifo_out(). However it does not check kzalloc() for allocation failure which returns NULL and could potentially lead to a NULL deref.

Fix this by checking for kzalloc() failure and skipping the current entry if allocation failure occurs.

Fixes 5e013ad20689 ("HID: wacom: Remove static WACOM_PKGLEN_MAX limit")
---
 drivers/hid/wacom_sys.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hid/wacom_sys.c +7 -2
index 97393a3083ca..666b7eb0fdfe 100644
@@ -70,10 +70,16 @@ static void wacom_wac_queue_flush(struct hid_device *hdev,
 {
        while (!kfifo_is_empty(fifo)) {
                int size = kfifo_peek_len(fifo);
-               u8 *buf = kzalloc(size, GFP_KERNEL);
+               u8 *buf;
                unsigned int count;
                int err;
 
+               buf = kzalloc(size, GFP_KERNEL);
+               if (!buf) {
+                       kfifo_skip(fifo);
+                       continue;
+               }
+
                count = kfifo_out(fifo, buf, size);
                if (count != size) {
                        // Hard to say what is the "right" action in this
--
Quoted 3 lines

2.39.5

Patch looks good to me. With or without Markus' comments addressed,

Jason (she/they)
---
Now instead of four in the eights place /
you’ve got three, ‘Cause you added one /
(That is to say, eight) to the two, /
But you can’t take seven from three, /
So you look at the sixty-fours....

maintainer Applied
#3 lore raw
In reply to Qasim Ijaz
Quoted 28 lines · Qasim Ijaz

During wacom_wac_queue_flush() the code calls kzalloc() to allocate a zero initialised buffer which it uses as a storage buffer to get data from the fifo via kfifo_out(). However it does not check kzalloc() for allocation failure which returns NULL and could potentially lead to a NULL deref.

Fix this by checking for kzalloc() failure and skipping the current entry if allocation failure occurs.

Fixes 5e013ad20689 ("HID: wacom: Remove static WACOM_PKGLEN_MAX limit")
---
 drivers/hid/wacom_sys.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/hid/wacom_sys.c +1 -1
index 97393a3083ca..666b7eb0fdfe 100644
@@ -70,10 +70,16 @@ static void wacom_wac_queue_flush(struct hid_device *hdev,
 {
 	while (!kfifo_is_empty(fifo)) {
 		int size = kfifo_peek_len(fifo);
-		u8 *buf = kzalloc(size, GFP_KERNEL);
+		u8 *buf;
 		unsigned int count;
 		int err;

drivers/hid/wacom_sys.c static void wacom_wac_queue_flush(struct hid_device *hdev, · line 85

Quoted 10 lines
drivers/hid/wacom_sys.c +6 -0
 
+		buf = kzalloc(size, GFP_KERNEL);
+		if (!buf) {
+			kfifo_skip(fifo);
+			continue;
+		}
+
 		count = kfifo_out(fifo, buf, size);
 		if (count != size) {
 			// Hard to say what is the "right" action in this

Applied, thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs