Input: joydev - prevent potential read overflow in ioctl
Subsystems (3)
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.
--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joydev.c +2 -2
@@ -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
Dmitry Torokhov
maintainerHi 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")Signed-off-by Dan Carpenter <[email protected]>
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@@ -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
-0if (keypam[i] >KEY_MAX || keypam[i] <BTN_MISC ) { retval = -EINVAL ; goto out;Signature · 1 line
2.30.0
Thanks.
Signature · 1 line
Dmitry
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")Signed-off-by Dan Carpenter <[email protected]>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@@ -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
-EINVALif 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
Dmitry Torokhov
maintainerQuoted 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")Signed-off-by Dan Carpenter <[email protected]>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@@ -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
-EINVALif 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
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")Signed-off-by Dan Carpenter <[email protected]>--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/input/joydev.c +0
-0
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
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")Signed-off-by Dan Carpenter <[email protected]>--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/input/joydev.c +0
-0drivers/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 alen = min(len, sizeof(joydev->abspam)); abspam = memdup_user(argp, len);Maybe we can call
min(len, joydev->nabs)instead or evenmin3()and use only len in the for loops then? Same forjoydev_handle_JSIOCSBTNMAP.Thanks,
Denis
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 carpenterQuoted 53 lines
Quoted 12 lines
Fixes 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")Signed-off-by Dan Carpenter <[email protected]>--- drivers/input/joydev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/input/joydev.c +0
-0drivers/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 alen = min(len, sizeof(joydev->abspam)); abspam = memdup_user(argp, len);Maybe we can call
min(len, joydev->nabs)instead or evenmin3()and use only len in the for loops then? Same forjoydev_handle_JSIOCSBTNMAP.Thanks,
Denis