Skip to contentSkip to conversation

Input: joydev - prevent potential read overflow in ioctl

PATCH In review Dan Carpenter 3 participants 7 messages Feb 17, 2021 – Jul 3, 2021

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

PATCH 1/1 Input: joydev - prevent potential read overflow in ioctl

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +2 -2
index a2b5fbba2d3b..750f4513fe20 100644
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {
 		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
 			retval = -EINVAL;
 			goto out;
Signature · 1 line
2.30.0
maintainer
#1 lore raw

Hi Dan,

Quoted 5 lines · Dan Carpenter

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")

Thank you for the patch.

Quoted 18 lines
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +1 -1
index a2b5fbba2d3b..750f4513fe20 100644
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 490

Quoted 5 lines
drivers/input/joydev.c +1 -1
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {

I think we also need to make sure that len is even. Do you mind if I add the check at the top of the function to return -EINVAL if it is odd?

Quoted 6 lines
drivers/input/joydev.c +0 -0
 		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
 			retval = -EINVAL;
 			goto out;
Signature · 1 line
2.30.0

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 32 lines · Dmitry Torokhov

Hi Dan,

Quoted 5 lines · Dan Carpenter

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")

Thank you for the patch.

Quoted 21 lines
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +1 -1
index a2b5fbba2d3b..750f4513fe20 100644
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
Quoted 6 lines

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 490

Quoted 2 lines
drivers/input/joydev.c +1 -1
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {

I think we also need to make sure that len is even. Do you mind if I add the check at the top of the function to return -EINVAL if it is odd?

Yes. You are right.

Btw, thank you for fixing my patches, but feel free to just ask me to resend if that's easier for you.

regards,
dan carpenter

maintainer
#3 lore raw
Quoted 40 lines · Dan Carpenter
Quoted 32 lines · Dmitry Torokhov

Hi Dan,

Quoted 5 lines · Dan Carpenter

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")

Thank you for the patch.

Quoted 21 lines
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +1 -1
index a2b5fbba2d3b..750f4513fe20 100644
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
Quoted 6 lines

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 490

Quoted 2 lines
drivers/input/joydev.c +1 -1
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {

I think we also need to make sure that len is even. Do you mind if I add the check at the top of the function to return -EINVAL if it is odd?

Quoted 4 lines

Yes. You are right.

Btw, thank you for fixing my patches, but feel free to just ask me to resend if that's easier for you.

It is not really fixing your patch, rather addressing another issue while we are there. Some might even say it could have been a separate patch.

Anyway, for small fixups like this I feel it's less noise on the lists without extra resubmissions, that is why I often do them on my side.

Thanks!

Signature · 1 line
Dmitry
#4 lore raw
In reply to Dan Carpenter

Hi,

CVE-2021-3612 was assigned to this patch.

Quoted 2 lines · Dan Carpenter

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

The added check looks insufficient to me. There are second loops in these functions with unpatched "i < joydev->nabs;" and "i < joydev->nkey;" checks.

Quoted 12 lines
Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +0 -0
index a2b5fbba2d3b..750f4513fe20 100644

drivers/input/joydev.c static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev, · line 462

Quoted 8 lines
drivers/input/joydev.c +1 -1
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->abspam, abspam, len);

        for (i = 0; i < joydev->nabs; i++) // <== HERE
                joydev->absmap[joydev->abspam[i]] = i;

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 493

Quoted 10 lines
drivers/input/joydev.c +1 -1
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {
 		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->keypam, keypam, len);

        for (i = 0; i < joydev->nkey; i++) // <== HERE
                joydev->keymap[keypam[i] - BTN_MISC] = i;

Also at the beginning of joydev_handle_JSIOCSAXMAP() there is a

	len = min(len, sizeof(joydev->abspam));
	abspam = memdup_user(argp, len);

Maybe we can call min(len, joydev->nabs) instead or even min3() and use only len in the for loops then? Same for joydev_handle_JSIOCSBTNMAP.

Thanks,
Denis

#5 lore raw
Quoted 5 lines · Denis Efremov

Hi,

CVE-2021-3612 was assigned to this patch.

Quoted 8 lines
Quoted 2 lines · Dan Carpenter

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

The added check looks insufficient to me. There are second loops in these functions with unpatched "i < joydev->nabs;" and "i < joydev->nkey;" checks.

Thanks, Denis.

You're right. The fix isn't complete. We discussed this in a different thread but I sort of assumed it was dealt with and didn't follow up. :/

https://lore.kernel.org/lkml/[email protected]/

regards,
dan carpenter

Quoted 53 lines
Quoted 12 lines
Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +0 -0
index a2b5fbba2d3b..750f4513fe20 100644

drivers/input/joydev.c static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev, · line 462

Quoted 8 lines
drivers/input/joydev.c +1 -1
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->abspam, abspam, len);

        for (i = 0; i < joydev->nabs; i++) // <== HERE
                joydev->absmap[joydev->abspam[i]] = i;

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 493

Quoted 10 lines
drivers/input/joydev.c +1 -1
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {
 		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->keypam, keypam, len);

        for (i = 0; i < joydev->nkey; i++) // <== HERE
                joydev->keymap[keypam[i] - BTN_MISC] = i;

Also at the beginning of joydev_handle_JSIOCSAXMAP() there is a

	len = min(len, sizeof(joydev->abspam));
	abspam = memdup_user(argp, len);

Maybe we can call min(len, joydev->nabs) instead or even min3() and use only len in the for loops then? Same for joydev_handle_JSIOCSBTNMAP.

Thanks,
Denis

#6 lore raw
Quoted 4 lines · Dan Carpenter
Quoted 3 lines · Denis Efremov

Hi,

CVE-2021-3612 was assigned to this patch.

Oh, sorry CVE-2021-3612 assigned not to:
182d679b2298 Input: joydev - prevent potential read overflow in ioctl

But for not yet applied fix
https://access.redhat.com/security/cve/cve-2021-3612:

Quoted 2 lines

An out-of-bounds memory write flaw was found in the Linux kernel’s joystick devices subsystem, in the way the user calls ioctl JSIOCSBTNMAP.

So it appears that CVE is assigned but the fix is not in the mainline yet. Maybe someone could explicitly list the CVE id in a commit message then.

Quoted 74 lines
Quoted 10 lines · Dan Carpenter
Quoted 2 lines

The problem here is that "len" might be less than "joydev->nabs" so the loops which verfy abspam[i] and keypam[] might read beyond the buffer.

The added check looks insufficient to me. There are second loops in these functions with unpatched "i < joydev->nabs;" and "i < joydev->nkey;" checks.

Thanks, Denis.

You're right. The fix isn't complete. We discussed this in a different thread but I sort of assumed it was dealt with and didn't follow up. :/

https://lore.kernel.org/lkml/[email protected]/

regards,
dan carpenter

Quoted 53 lines
Quoted 12 lines
Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +0 -0
index a2b5fbba2d3b..750f4513fe20 100644

drivers/input/joydev.c static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev, · line 462

Quoted 8 lines
drivers/input/joydev.c +1 -1
@@ -456,7 +456,7 @@ static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
 	if (IS_ERR(abspam))
 		return PTR_ERR(abspam);
 
-	for (i = 0; i < joydev->nabs; i++) {
+	for (i = 0; i < len && i < joydev->nabs; i++) {
 		if (abspam[i] > ABS_MAX) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->abspam, abspam, len);

        for (i = 0; i < joydev->nabs; i++) // <== HERE
                joydev->absmap[joydev->abspam[i]] = i;

drivers/input/joydev.c static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev, · line 493

Quoted 10 lines
drivers/input/joydev.c +1 -1
@@ -487,7 +487,7 @@ static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
 	if (IS_ERR(keypam))
 		return PTR_ERR(keypam);
 
-	for (i = 0; i < joydev->nkey; i++) {
+	for (i = 0; i < (len / 2) && i < joydev->nkey; i++) {
 		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
 			retval = -EINVAL;
 			goto out;
        memcpy(joydev->keypam, keypam, len);

        for (i = 0; i < joydev->nkey; i++) // <== HERE
                joydev->keymap[keypam[i] - BTN_MISC] = i;

Also at the beginning of joydev_handle_JSIOCSAXMAP() there is a

	len = min(len, sizeof(joydev->abspam));
	abspam = memdup_user(argp, len);

Maybe we can call min(len, joydev->nabs) instead or even min3() and use only len in the for loops then? Same for joydev_handle_JSIOCSBTNMAP.

Thanks,
Denis