Skip to contentSkip to conversation

accel/qaic: Improve bounds checking in encode/decode

PATCH Applied Dan Carpenter 3 participants 19 messages Jul 11, 2023 – Aug 4, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v4 cover accel/qaic: Improve bounds checking in encode/decode

Fixed in v4: Send the correct [PATCH 1/5] patch.

Fixed in v3: Redo messed up threading

Fixed two things in v2: Include the <linux/overflow.h> file. Change the >= in encode and decode to >.

regards,
dan carpenter

#1 lore raw

PATCH v4 1/5 accel/qaic: tighten bounds checking in encode_message()

There are several issues in this code. The check at the start of the loop:

	if (user_len >= user_msg->len) {

This check does not ensure that we have enough space for the trans_hdr (8 bytes). Instead the check needs to be:

	if (user_len > user_msg->len - sizeof(*trans_hdr)) {

That subtraction is done as an unsigned long we want to avoid negatives. Add a lower bound to the start of the function.

	if (user_msg->len < sizeof(*trans_hdr))

There is a second integer underflow which can happen if trans_hdr->len is zero inside the encode_passthrough() function.

	memcpy(out_trans->data, in_trans->data, in_trans->hdr.len - sizeof(in_trans->hdr));

Instead of adding a check to encode_passthrough() it's better to check in this central place. Add that check:

	if (trans_hdr->len < sizeof(trans_hdr)

The final concern is that the "user_len + trans_hdr->len" might have an integer overflow bug. Use size_add() to prevent that.

- if (user_len + trans_hdr->len > user_msg->len) {
+ if (size_add(user_len, trans_hdr->len) > user_msg->len) {

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
v4: send this patch.
v2: * Fix the >= vs > bug in:

	if (user_len > user_msg->len - sizeof(*trans_hdr)) {

    * include overflow.h
---
 drivers/accel/qaic/qaic_control.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/accel/qaic/qaic_control.c +6 -3
index 5c57f7b4494e..2fdd5959c52f 100644
@@ -14,6 +14,7 @@
 #include <linux/mm.h>
 #include <linux/moduleparam.h>
 #include <linux/mutex.h>
+#include <linux/overflow.h>
 #include <linux/pci.h>
 #include <linux/scatterlist.h>
 #include <linux/types.h>
@@ -748,7 +749,8 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg,
 	int ret;
 	int i;
 
-	if (!user_msg->count) {
+	if (!user_msg->count ||
+	    user_msg->len < sizeof(*trans_hdr)) {
 		ret = -EINVAL;
 		goto out;
 	}
@@ -765,12 +767,13 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg,
 	}
 
 	for (i = 0; i < user_msg->count; ++i) {
-		if (user_len >= user_msg->len) {
+		if (user_len > user_msg->len - sizeof(*trans_hdr)) {
 			ret = -EINVAL;
 			break;
 		}
 		trans_hdr = (struct qaic_manage_trans_hdr *)(user_msg->data + user_len);
-		if (user_len + trans_hdr->len > user_msg->len) {
+		if (trans_hdr->len < sizeof(trans_hdr) ||
+		    size_add(user_len, trans_hdr->len) > user_msg->len) {
 			ret = -EINVAL;
 			break;
 		}
Signature · 1 line
2.39.2
Reviewed-by on v4 1/5
#2 lore raw

Re: [PATCH 1/5 v4] accel/qaic: tighten bounds checking in encode_message()

Quoted 31 lines · Dan Carpenter

There are several issues in this code. The check at the start of the loop:

	if (user_len >= user_msg->len) {

This check does not ensure that we have enough space for the trans_hdr (8 bytes). Instead the check needs to be:

	if (user_len > user_msg->len - sizeof(*trans_hdr)) {

That subtraction is done as an unsigned long we want to avoid negatives. Add a lower bound to the start of the function.

	if (user_msg->len < sizeof(*trans_hdr))

There is a second integer underflow which can happen if trans_hdr->len is zero inside the encode_passthrough() function.

	memcpy(out_trans->data, in_trans->data, in_trans->hdr.len - sizeof(in_trans->hdr));

Instead of adding a check to encode_passthrough() it's better to check in this central place. Add that check:

	if (trans_hdr->len < sizeof(trans_hdr)

The final concern is that the "user_len + trans_hdr->len" might have an integer overflow bug. Use size_add() to prevent that.

(quoted diff) +1 -1
-	if (user_len + trans_hdr->len > user_msg->len) {
+	if (size_add(user_len, trans_hdr->len) > user_msg->len) {
Quoted 2 lines
Fixes 129776ac2e38 ("accel/qaic: Add control path")
Applied
#3 lore raw

Re: [PATCH 1/5 v4] accel/qaic: tighten bounds checking in encode_message()

Quoted 31 lines · Pranjal Ramajor Asha Kanojiya
Quoted 28 lines · Dan Carpenter

There are several issues in this code.  The check at the start of the loop:

    if (user_len >= user_msg->len) {

This check does not ensure that we have enough space for the trans_hdr (8 bytes).  Instead the check needs to be:

    if (user_len > user_msg->len - sizeof(*trans_hdr)) {

That subtraction is done as an unsigned long we want to avoid negatives.  Add a lower bound to the start of the function.

    if (user_msg->len < sizeof(*trans_hdr))

There is a second integer underflow which can happen if trans_hdr->len is zero inside the encode_passthrough() function.

    memcpy(out_trans->data, in_trans->data, in_trans->hdr.len - sizeof(in_trans->hdr));

Instead of adding a check to encode_passthrough() it's better to check in this central place.  Add that check:

    if (trans_hdr->len < sizeof(trans_hdr)

The final concern is that the "user_len + trans_hdr->len" might have an integer overflow bug.  Use size_add() to prevent that.

Quoted 8 lines
Quoted 5 lines

-    if (user_len + trans_hdr->len > user_msg->len) { +    if (size_add(user_len, trans_hdr->len) > user_msg->len) {

Fixes 129776ac2e38 ("accel/qaic: Add control path")

Applied to drm-misc-fixes

-Jeff

#4 lore raw

PATCH v4 2/5 accel/qaic: tighten bounds checking in decode_message()

In reply to Dan Carpenter

Copy the bounds checking from encode_message() to decode_message().

This patch addresses the following concerns. Ensure that there is enough space for at least one header so that we don't have a negative size later.

if (msg_hdr_len < sizeof(*trans_hdr))

Ensure that we have enough space to read the next header from the msg->data.

	if (msg_len > msg_hdr_len - sizeof(*trans_hdr))
		return -EINVAL;

Check that the trans_hdr->len is not below the minimum size:

if (hdr_len < sizeof(*trans_hdr))

This minimum check ensures that we don't corrupt memory in decode_passthrough() when we do.

	memcpy(out_trans->data, in_trans->data, len - sizeof(in_trans->hdr));

And finally, use size_add() to prevent an integer overflow:

if (size_add(msg_len, hdr_len) > msg_hdr_len)

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
v2: Fix the >= vs > bug in "if (msg_len > msg_hdr_len - sizeof(*trans_hdr))"

---
 drivers/accel/qaic/qaic_control.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/accel/qaic/qaic_control.c +10 -2
index 2fdd5959c52f..752b67aff777 100644
@@ -956,15 +956,23 @@ static int decode_message(struct qaic_device *qdev, struct manage_msg *user_msg,
 	int ret;
 	int i;
 
-	if (msg_hdr_len > QAIC_MANAGE_MAX_MSG_LENGTH)
+	if (msg_hdr_len < sizeof(*trans_hdr) ||
+	    msg_hdr_len > QAIC_MANAGE_MAX_MSG_LENGTH)
 		return -EINVAL;
 
 	user_msg->len = 0;
 	user_msg->count = le32_to_cpu(msg->hdr.count);
 
 	for (i = 0; i < user_msg->count; ++i) {
+		u32 hdr_len;
+
+		if (msg_len > msg_hdr_len - sizeof(*trans_hdr))
+			return -EINVAL;
+
 		trans_hdr = (struct wire_trans_hdr *)(msg->data + msg_len);
-		if (msg_len + le32_to_cpu(trans_hdr->len) > msg_hdr_len)
+		hdr_len = le32_to_cpu(trans_hdr->len);
+		if (hdr_len < sizeof(*trans_hdr) ||
+		    size_add(msg_len, hdr_len) > msg_hdr_len)
 			return -EINVAL;
 
 		switch (le32_to_cpu(trans_hdr->type)) {
Signature · 1 line
2.39.2
Reviewed-by on v4 2/5
#5 lore raw

Re: [PATCH 2/5 v4] accel/qaic: tighten bounds checking in decode_message()

Quoted 21 lines · Dan Carpenter

Copy the bounds checking from encode_message() to decode_message().

This patch addresses the following concerns. Ensure that there is enough space for at least one header so that we don't have a negative size later.

if (msg_hdr_len < sizeof(*trans_hdr))

Ensure that we have enough space to read the next header from the msg->data.

	if (msg_len > msg_hdr_len - sizeof(*trans_hdr))
		return -EINVAL;

Check that the trans_hdr->len is not below the minimum size:

if (hdr_len < sizeof(*trans_hdr))

This minimum check ensures that we don't corrupt memory in decode_passthrough() when we do.

Quoted 8 lines
	memcpy(out_trans->data, in_trans->data, len - sizeof(in_trans->hdr));

And finally, use size_add() to prevent an integer overflow:

if (size_add(msg_len, hdr_len) > msg_hdr_len)

Fixes 129776ac2e38 ("accel/qaic: Add control path")
Applied
#6 lore raw

Re: [PATCH 2/5 v4] accel/qaic: tighten bounds checking in decode_message()

Quoted 26 lines · Pranjal Ramajor Asha Kanojiya
Quoted 23 lines · Dan Carpenter

Copy the bounds checking from encode_message() to decode_message().

This patch addresses the following concerns.  Ensure that there is enough space for at least one header so that we don't have a negative size later.

    if (msg_hdr_len < sizeof(*trans_hdr))

Ensure that we have enough space to read the next header from the msg->data.

    if (msg_len > msg_hdr_len - sizeof(*trans_hdr)) return -EINVAL;

Check that the trans_hdr->len is not below the minimum size:

    if (hdr_len < sizeof(*trans_hdr))

This minimum check ensures that we don't corrupt memory in decode_passthrough() when we do.

    memcpy(out_trans->data, in_trans->data, len - sizeof(in_trans->hdr));

Quoted 8 lines
Quoted 6 lines

And finally, use size_add() to prevent an integer overflow:

    if (size_add(msg_len, hdr_len) > msg_hdr_len)

Fixes 129776ac2e38 ("accel/qaic: Add control path")

Pushed to drm-misc-fixes

-Jeff

#7 lore raw

PATCH v4 3/5 accel/qaic: Add consistent integer overflow checks

In reply to Dan Carpenter

The encode_dma() function has integer overflow checks. The encode_passthrough(), encode_activate() and encode_status() functions did not. I added integer overflow checking everywhere. I also updated the integer overflow checking in encode_dma() to use size_add() so everything is consistent.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
v2: no change

 drivers/accel/qaic/qaic_control.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/accel/qaic/qaic_control.c +6 -8
index 752b67aff777..23680f5f1902 100644
@@ -367,7 +367,7 @@ static int encode_passthrough(struct qaic_device *qdev, void *trans, struct wrap
 	if (in_trans->hdr.len % 8 != 0)
 		return -EINVAL;
 
-	if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_EXT_MSG_LENGTH)
+	if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_EXT_MSG_LENGTH)
 		return -ENOSPC;
 
 	trans_wrapper = add_wrapper(wrappers,
@@ -558,12 +558,10 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list
 	msg = &wrapper->msg;
 	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len > (UINT_MAX - QAIC_MANAGE_EXT_MSG_LENGTH))
-		return -EINVAL;
-
 	/* There should be enough space to hold at least one ASP entry. */
-	if (msg_hdr_len + sizeof(*out_trans) + sizeof(struct wire_addr_size_pair) >
-	    QAIC_MANAGE_EXT_MSG_LENGTH)
+	if (size_add(msg_hdr_len,
+		     sizeof(*out_trans) + sizeof(struct wire_addr_size_pair)) >
+		     QAIC_MANAGE_EXT_MSG_LENGTH)
 		return -ENOMEM;
 
 	if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size)
@@ -635,7 +633,7 @@ static int encode_activate(struct qaic_device *qdev, void *trans, struct wrapper
 	msg = &wrapper->msg;
 	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len + sizeof(*out_trans) > QAIC_MANAGE_MAX_MSG_LENGTH)
+	if (size_add(msg_hdr_len, sizeof(*out_trans)) > QAIC_MANAGE_MAX_MSG_LENGTH)
 		return -ENOSPC;
 
 	if (!in_trans->queue_size)
@@ -719,7 +717,7 @@ static int encode_status(struct qaic_device *qdev, void *trans, struct wrapper_l
 	msg = &wrapper->msg;
 	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_MAX_MSG_LENGTH)
+	if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_MAX_MSG_LENGTH)
 		return -ENOSPC;
 
 	trans_wrapper = add_wrapper(wrappers, sizeof(*trans_wrapper));
Signature · 1 line
2.39.2
Reviewed-by on v4 3/5
#8 lore raw

Re: [PATCH 3/5 v4] accel/qaic: Add consistent integer overflow checks

Quoted 29 lines · Dan Carpenter

The encode_dma() function has integer overflow checks. The encode_passthrough(), encode_activate() and encode_status() functions did not. I added integer overflow checking everywhere. I also updated the integer overflow checking in encode_dma() to use size_add() so everything is consistent.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
v2: no change

  drivers/accel/qaic/qaic_control.c | 14 ++++++--------
  1 file changed, 6 insertions(+), 8 deletions(-)
drivers/accel/qaic/qaic_control.c +1 -1
index 752b67aff777..23680f5f1902 100644
@@ -367,7 +367,7 @@ static int encode_passthrough(struct qaic_device *qdev, void *trans, struct wrap
  	if (in_trans->hdr.len % 8 != 0)
  		return -EINVAL;
 
-	if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_EXT_MSG_LENGTH)
+	if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_EXT_MSG_LENGTH)
  		return -ENOSPC;
 
  	trans_wrapper = add_wrapper(wrappers,
@@ -558,12 +558,10 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list
  	msg = &wrapper->msg;

drivers/accel/qaic/qaic_control.c static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list · line 563

Quoted 10 lines
drivers/accel/qaic/qaic_control.c +2 -5
  	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len > (UINT_MAX - QAIC_MANAGE_EXT_MSG_LENGTH))
-		return -EINVAL;
-
  	/* There should be enough space to hold at least one ASP entry. */
-	if (msg_hdr_len + sizeof(*out_trans) + sizeof(struct wire_addr_size_pair) >
-	    QAIC_MANAGE_EXT_MSG_LENGTH)
+	if (size_add(msg_hdr_len,
+		     sizeof(*out_trans) + sizeof(struct wire_addr_size_pair)) >

Can we have the entire size_add() on same line?

Quoted 14 lines
drivers/accel/qaic/qaic_control.c +2 -1
+		     QAIC_MANAGE_EXT_MSG_LENGTH)
  		return -ENOMEM;
 
  	if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size)
@@ -635,7 +633,7 @@ static int encode_activate(struct qaic_device *qdev, void *trans, struct wrapper
  	msg = &wrapper->msg;
  	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len + sizeof(*out_trans) > QAIC_MANAGE_MAX_MSG_LENGTH)
+	if (size_add(msg_hdr_len, sizeof(*out_trans)) > QAIC_MANAGE_MAX_MSG_LENGTH)
  		return -ENOSPC;
 
  	if (!in_trans->queue_size)

drivers/accel/qaic/qaic_control.c static int encode_status(struct qaic_device *qdev, void *trans, struct wrapper_l · line 723

Quoted 8 lines
drivers/accel/qaic/qaic_control.c +1 -1
@@ -719,7 +717,7 @@ static int encode_status(struct qaic_device *qdev, void *trans, struct wrapper_l
  	msg = &wrapper->msg;
  	msg_hdr_len = le32_to_cpu(msg->hdr.len);
 
-	if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_MAX_MSG_LENGTH)
+	if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_MAX_MSG_LENGTH)
  		return -ENOSPC;
 
  	trans_wrapper = add_wrapper(wrappers, sizeof(*trans_wrapper));
Applied
#9 lore raw

Re: [PATCH 3/5 v4] accel/qaic: Add consistent integer overflow checks

Quoted 61 lines · Pranjal Ramajor Asha Kanojiya
Quoted 32 lines · Dan Carpenter

The encode_dma() function has integer overflow checks.  The encode_passthrough(), encode_activate() and encode_status() functions did not.  I added integer overflow checking everywhere.  I also updated the integer overflow checking in encode_dma() to use size_add() so everything is consistent.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
v2: no change

  drivers/accel/qaic/qaic_control.c | 14 ++++++--------

  1 file changed, 6 insertions(+), 8 deletions(-)
drivers/accel/qaic/qaic_control.c +1 -1
index 752b67aff777..23680f5f1902 100644
@@ -367,7 +367,7 @@ static int encode_passthrough(struct qaic_device 
 *qdev, void *trans, struct wrap
       if (in_trans->hdr.len % 8 != 0)
           return -EINVAL;
-    if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_EXT_MSG_LENGTH)
+    if (size_add(msg_hdr_len, in_trans->hdr.len) > 
 QAIC_MANAGE_EXT_MSG_LENGTH)
           return -ENOSPC;
       trans_wrapper = add_wrapper(wrappers,
@@ -558,12 +558,10 @@ static int encode_dma(struct qaic_device *qdev, 

void *trans, struct wrapper_list
      msg = &wrapper->msg;
      msg_hdr_len = le32_to_cpu(msg->hdr.len);
-    if (msg_hdr_len > (UINT_MAX - QAIC_MANAGE_EXT_MSG_LENGTH))

Quoted 8 lines

-        return -EINVAL;
-
      /* There should be enough space to hold at least one ASP entry. */ -    if (msg_hdr_len + sizeof(*out_trans) + sizeof(struct wire_addr_size_pair) >
-        QAIC_MANAGE_EXT_MSG_LENGTH)
+    if (size_add(msg_hdr_len,
+             sizeof(*out_trans) + sizeof(struct wire_addr_size_pair)) >

Can we have the entire size_add() on same line?

Quoted 15 lines

+             QAIC_MANAGE_EXT_MSG_LENGTH)
          return -ENOMEM;
      if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size)

drivers/accel/qaic/qaic_control.c +1 -1
@@ -635,7 +633,7 @@ static int encode_activate(struct qaic_device 
 *qdev, void *trans, struct wrapper
       msg = &wrapper->msg;
       msg_hdr_len = le32_to_cpu(msg->hdr.len);
-    if (msg_hdr_len + sizeof(*out_trans) > QAIC_MANAGE_MAX_MSG_LENGTH)
+    if (size_add(msg_hdr_len, sizeof(*out_trans)) > 
 QAIC_MANAGE_MAX_MSG_LENGTH)
           return -ENOSPC;
       if (!in_trans->queue_size)
@@ -719,7 +717,7 @@ static int encode_status(struct qaic_device *qdev, 

void *trans, struct wrapper_l
      msg = &wrapper->msg;

Quoted 8 lines
Quoted 6 lines

      msg_hdr_len = le32_to_cpu(msg->hdr.len);
-    if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_MAX_MSG_LENGTH) +    if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_MAX_MSG_LENGTH)
          return -ENOSPC;
      trans_wrapper = add_wrapper(wrappers, sizeof(*trans_wrapper));

Pushed to drm-misc-fixes

-Jeff

#10 lore raw

PATCH v4 4/5 accel/qaic: move and expand integer overflow checks for map_user_pages()

In reply to Dan Carpenter

The integer overflow checking for find_and_map_user_pages() was done in encode_dma(). Presumably this was to do it before the allocation. But it's not super important that the failure path is a fast path and it hurts readability to put the check so far from the where the variable is used.

Move the check to find_and_map_user_pages() instead and add some more additional potential integer overflow checks.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
no change

 drivers/accel/qaic/qaic_control.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/accel/qaic/qaic_control.c +6 -3
index 23680f5f1902..d5ce36cb351f 100644
@@ -402,6 +402,12 @@ static int find_and_map_user_pages(struct qaic_device *qdev,
 
 	xfer_start_addr = in_trans->addr + resources->xferred_dma_size;
 
+	if (in_trans->size == 0 ||
+	    in_trans->addr + in_trans->size < in_trans->addr ||
+	    in_trans->addr + resources->xferred_dma_size < in_trans->addr ||
+	    in_trans->size + offset_in_page(xfer_start_addr) < resources->xferred_dma_size)
+		return -EINVAL;
+
 	need_pages = DIV_ROUND_UP(in_trans->size + offset_in_page(xfer_start_addr) -
 				  resources->xferred_dma_size, PAGE_SIZE);
 
@@ -564,9 +570,6 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list
 		     QAIC_MANAGE_EXT_MSG_LENGTH)
 		return -ENOMEM;
 
-	if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size)
-		return -EINVAL;
-
 	xfer = kmalloc(sizeof(*xfer), GFP_KERNEL);
 	if (!xfer)
 		return -ENOMEM;
Signature · 1 line
2.39.2
#11 lore raw

Re: [PATCH 4/5 v4] accel/qaic: move and expand integer overflow checks for map_user_pages()

Quoted 22 lines · Dan Carpenter

The integer overflow checking for find_and_map_user_pages() was done in encode_dma(). Presumably this was to do it before the allocation. But it's not super important that the failure path is a fast path and it hurts readability to put the check so far from the where the variable is used.

Move the check to find_and_map_user_pages() instead and add some more additional potential integer overflow checks.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
no change

  drivers/accel/qaic/qaic_control.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/accel/qaic/qaic_control.c +0 -0
index 23680f5f1902..d5ce36cb351f 100644

drivers/accel/qaic/qaic_control.c static int find_and_map_user_pages(struct qaic_device *qdev, · line 407

Quoted 6 lines
drivers/accel/qaic/qaic_control.c +3 -0
@@ -402,6 +402,12 @@ static int find_and_map_user_pages(struct qaic_device *qdev,
 
  	xfer_start_addr = in_trans->addr + resources->xferred_dma_size;
 
+	if (in_trans->size == 0 ||
+	    in_trans->addr + in_trans->size < in_trans->addr ||
+	    in_trans->addr + resources->xferred_dma_size < in_trans->addr ||

Why not use xfer_start_addr ?

Quoted 16 lines
drivers/accel/qaic/qaic_control.c +3 -3
+	    in_trans->size + offset_in_page(xfer_start_addr) < resources->xferred_dma_size)
+		return -EINVAL;
+
  	need_pages = DIV_ROUND_UP(in_trans->size + offset_in_page(xfer_start_addr) -
  				  resources->xferred_dma_size, PAGE_SIZE);
 
@@ -564,9 +570,6 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list
  		     QAIC_MANAGE_EXT_MSG_LENGTH)
  		return -ENOMEM;
 
-	if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size)
-		return -EINVAL;
-
  	xfer = kmalloc(sizeof(*xfer), GFP_KERNEL);
  	if (!xfer)
  		return -ENOMEM;
#12 lore raw

PATCH v4 5/5 accel/qaic: Fix a leak in map_user_pages()

In reply to Dan Carpenter

If get_user_pages_fast() allocates some pages but not as many as we wanted, then the current code leaks those pages. Call put_page() on the pages before returning.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

---
no change

 drivers/accel/qaic/qaic_control.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/accel/qaic/qaic_control.c +5 -2
index d5ce36cb351f..9a6f80f31c65 100644
@@ -425,9 +425,12 @@ static int find_and_map_user_pages(struct qaic_device *qdev,
 	}
 
 	ret = get_user_pages_fast(xfer_start_addr, nr_pages, 0, page_list);
-	if (ret < 0 || ret != nr_pages) {
-		ret = -EFAULT;
+	if (ret < 0)
 		goto free_page_list;
+	if (ret != nr_pages) {
+		nr_pages = ret;
+		ret = -EFAULT;
+		goto put_pages;
 	}
 
 	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
Signature · 1 line
2.39.2
Reviewed-by on v4 5/5
#13 lore raw

Re: [PATCH 5/5 v4] accel/qaic: Fix a leak in map_user_pages()

Quoted 6 lines · Dan Carpenter

If get_user_pages_fast() allocates some pages but not as many as we wanted, then the current code leaks those pages. Call put_page() on the pages before returning.

Fixes 129776ac2e38 ("accel/qaic: Add control path")
Applied
#14 lore raw

Re: [PATCH 5/5 v4] accel/qaic: Fix a leak in map_user_pages()

Quoted 11 lines · Pranjal Ramajor Asha Kanojiya
Quoted 6 lines · Dan Carpenter

If get_user_pages_fast() allocates some pages but not as many as we wanted, then the current code leaks those pages.  Call put_page() on the pages before returning.

Fixes 129776ac2e38 ("accel/qaic: Add control path")

Pushed to drm-misc-fixes

-Jeff

Reviewed-by on the v4 series
#15 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter

Fixed in v4: Send the correct [PATCH 1/5] patch.

Fixed in v3: Redo messed up threading

Fixed two things in v2: Include the <linux/overflow.h> file. Change the >= in encode and decode to >.

regards,
dan carpenter

Did you intentionally drop tags from previous versions?

For 1-3, 5

Looks like 3,5 are reviewed by Pranjal and also good. I see 5 is also reviewed by Dafna. Expect those to be merged. 1,2 need a review from Pranjal, but I expect all is good and will be merged.

I did not see feedback on my question for 4. Would like your feedback before queuing that one up.

Overall, thanks for your work. I think we are pretty close to wrapping this up.

-Jeff

#16 lore raw
Quoted 5 lines · Jeffrey Hugo
Quoted 4 lines · Dan Carpenter

Fixed in v4: Send the correct [PATCH 1/5] patch.

Fixed in v3: Redo messed up threading

Quoted 7 lines
Quoted 5 lines

Fixed two things in v2: Include the <linux/overflow.h> file. Change the >= in encode and decode to >.

regards,
dan carpenter

Did you intentionally drop tags from previous versions?

Sorry, I kept messing up the resends.

Quoted 11 lines

For 1-3, 5

Looks like 3,5 are reviewed by Pranjal and also good. I see 5 is also reviewed by Dafna. Expect those to be merged. 1,2 need a review from Pranjal, but I expect all is good and will be merged.

I did not see feedback on my question for 4. Would like your feedback before queuing that one up.

Sorry, again. Yeah. I think you're right. Could we queue the rest and I will resend 4 separately? I know it's a headache. If not it's fine, I can resend them all.

regards,
dan carpenter

#17 lore raw
Quoted 24 lines · Dan Carpenter
Quoted 5 lines · Jeffrey Hugo
Quoted 4 lines · Dan Carpenter

Fixed in v4: Send the correct [PATCH 1/5] patch.

Fixed in v3: Redo messed up threading

Quoted 7 lines
Quoted 5 lines

Fixed two things in v2: Include the <linux/overflow.h> file. Change the >= in encode and decode to >.

regards,
dan carpenter

Did you intentionally drop tags from previous versions?

Sorry, I kept messing up the resends.

Quoted 8 lines

For 1-3, 5

Looks like 3,5 are reviewed by Pranjal and also good. I see 5 is also reviewed by Dafna. Expect those to be merged. 1,2 need a review from Pranjal, but I expect all is good and will be merged.

Quoted 7 lines
Quoted 3 lines

I did not see feedback on my question for 4. Would like your feedback before queuing that one up.

Sorry, again. Yeah. I think you're right. Could we queue the rest and I will resend 4 separately? I know it's a headache. If not it's fine, I can resend them all.

These all seem independent enough that I don't see splitting out 4 as a problem.

-Jeff

#18 lore raw
In reply to Dan Carpenter
Quoted 24 lines · Dan Carpenter
Quoted 5 lines · Jeffrey Hugo
Quoted 4 lines · Dan Carpenter

Fixed in v4: Send the correct [PATCH 1/5] patch.

Fixed in v3: Redo messed up threading

Quoted 7 lines
Quoted 5 lines

Fixed two things in v2: Include the <linux/overflow.h> file. Change the >= in encode and decode to >.

regards,
dan carpenter

Did you intentionally drop tags from previous versions?

Sorry, I kept messing up the resends.

Quoted 8 lines

For 1-3, 5

Looks like 3,5 are reviewed by Pranjal and also good. I see 5 is also reviewed by Dafna. Expect those to be merged. 1,2 need a review from Pranjal, but I expect all is good and will be merged.

Quoted 7 lines
Quoted 3 lines

I did not see feedback on my question for 4. Would like your feedback before queuing that one up.

Sorry, again. Yeah. I think you're right. Could we queue the rest and I will resend 4 separately? I know it's a headache. If not it's fine, I can resend them all.

Resend for #4 is still coming, right? I just don't want to forget about it.

-Jeff