thunderbolt: Check return value of kmemdup() in icm_handle_event()
authorXiaoke Wang <xkernel.wang@foxmail.com>
Mon, 13 Dec 2021 08:27:15 +0000 (16:27 +0800)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Wed, 15 Dec 2021 10:39:08 +0000 (13:39 +0300)
kmemdup() may return NULL if there is not enough memory available. Check
this and bail out early in this case. While there move INIT_WORK() to
happen after we have allocated all the memory needed for the event
handling to avoid doing unnecessary work.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/icm.c

index 6255f1e..fff0c74 100644 (file)
@@ -1741,8 +1741,13 @@ static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
        if (!n)
                return;
 
-       INIT_WORK(&n->work, icm_handle_notification);
        n->pkg = kmemdup(buf, size, GFP_KERNEL);
+       if (!n->pkg) {
+               kfree(n);
+               return;
+       }
+
+       INIT_WORK(&n->work, icm_handle_notification);
        n->tb = tb;
 
        queue_work(tb->wq, &n->work);