Skip to content

Commit ea33cb6

Browse files
Dan Carpenterquic-jhugo
authored andcommitted
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: 129776a ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Reviewed-by: Pranjal Ramajor Asha Kanojiya <quic_pkanojiy@quicinc.com> Reviewed-by: Jeffrey Hugo <quic_jhugo@quicinc.com> Cc: stable@vger.kernel.org # 6.4.x Signed-off-by: Jeffrey Hugo <quic_jhugo@quicinc.com> Link: https://patchwork.freedesktop.org/patch/msgid/9a0cb0c1-a974-4f10-bc8d-94437983639a@moroto.mountain
1 parent 2329cc7 commit ea33cb6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

drivers/accel/qaic/qaic_control.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/mm.h>
1515
#include <linux/moduleparam.h>
1616
#include <linux/mutex.h>
17+
#include <linux/overflow.h>
1718
#include <linux/pci.h>
1819
#include <linux/scatterlist.h>
1920
#include <linux/types.h>
@@ -748,7 +749,8 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg,
748749
int ret;
749750
int i;
750751

751-
if (!user_msg->count) {
752+
if (!user_msg->count ||
753+
user_msg->len < sizeof(*trans_hdr)) {
752754
ret = -EINVAL;
753755
goto out;
754756
}
@@ -765,12 +767,13 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg,
765767
}
766768

767769
for (i = 0; i < user_msg->count; ++i) {
768-
if (user_len >= user_msg->len) {
770+
if (user_len > user_msg->len - sizeof(*trans_hdr)) {
769771
ret = -EINVAL;
770772
break;
771773
}
772774
trans_hdr = (struct qaic_manage_trans_hdr *)(user_msg->data + user_len);
773-
if (user_len + trans_hdr->len > user_msg->len) {
775+
if (trans_hdr->len < sizeof(trans_hdr) ||
776+
size_add(user_len, trans_hdr->len) > user_msg->len) {
774777
ret = -EINVAL;
775778
break;
776779
}

0 commit comments

Comments
 (0)