static void *alloc_wr(size_t wr_size, __u32 num_sge)
 {
+       if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
+                      sizeof (struct ib_sge))
+               return NULL;
+
        return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
                         num_sge * sizeof (struct ib_sge), GFP_KERNEL);
-};
+}
 
 ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
                            struct ib_device *ib_dev,
                        goto err;
                }
 
+               if (user_wr->num_sge >=
+                   (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
+                   sizeof (struct ib_sge)) {
+                       ret = -EINVAL;
+                       goto err;
+               }
+
                next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
                               user_wr->num_sge * sizeof (struct ib_sge),
                               GFP_KERNEL);