diff mbox series

Input: joydev - prevent potential read overflow in ioctl

Message ID YCyzR8WvFRw4HWw6@mwanda (mailing list archive)
State Accepted
Commit 182d679b2298d62bf42bb14b12a8067b8e17b617
Headers show
Series Input: joydev - prevent potential read overflow in ioctl | expand

Commit Message

Dan Carpenter Feb. 17, 2021, 6:10 a.m. UTC
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 <dan.carpenter@oracle.com>
---
 drivers/input/joydev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Dmitry Torokhov Feb. 19, 2021, 3:17 a.m. UTC | #1
Hi Dan,

On Wed, Feb 17, 2021 at 09:10:15AM +0300, Dan Carpenter wrote:
> 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 <dan.carpenter@oracle.com>

Thank you for the patch.

> ---
>  drivers/input/joydev.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
> index a2b5fbba2d3b..750f4513fe20 100644
> --- a/drivers/input/joydev.c
> +++ b/drivers/input/joydev.c
> @@ -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++) {


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?

>  		if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
>  			retval = -EINVAL;
>  			goto out;
> -- 
> 2.30.0
> 

Thanks.
Dan Carpenter Feb. 19, 2021, 8:32 a.m. UTC | #2
On Thu, Feb 18, 2021 at 07:17:44PM -0800, Dmitry Torokhov wrote:
> Hi Dan,
> 
> On Wed, Feb 17, 2021 at 09:10:15AM +0300, Dan Carpenter wrote:
> > 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 <dan.carpenter@oracle.com>
> 
> Thank you for the patch.
> 
> > ---
> >  drivers/input/joydev.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
> > index a2b5fbba2d3b..750f4513fe20 100644
> > --- a/drivers/input/joydev.c
> > +++ b/drivers/input/joydev.c
> > @@ -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++) {
> 
> 
> 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
Dmitry Torokhov Feb. 19, 2021, 6:54 p.m. UTC | #3
On Fri, Feb 19, 2021 at 11:32:15AM +0300, Dan Carpenter wrote:
> On Thu, Feb 18, 2021 at 07:17:44PM -0800, Dmitry Torokhov wrote:
> > Hi Dan,
> > 
> > On Wed, Feb 17, 2021 at 09:10:15AM +0300, Dan Carpenter wrote:
> > > 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 <dan.carpenter@oracle.com>
> > 
> > Thank you for the patch.
> > 
> > > ---
> > >  drivers/input/joydev.c | 4 ++--
> > >  1 file changed, 2 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
> > > index a2b5fbba2d3b..750f4513fe20 100644
> > > --- a/drivers/input/joydev.c
> > > +++ b/drivers/input/joydev.c
> > > @@ -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++) {
> > 
> > 
> > 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.

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!
Denis Efremov July 3, 2021, 11:20 a.m. UTC | #4
Hi,

CVE-2021-3612 was assigned to this patch.


On 2/17/21 9:10 AM, Dan Carpenter wrote:
> 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.

> 
> Fixes: 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/input/joydev.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
> index a2b5fbba2d3b..750f4513fe20 100644
> --- a/drivers/input/joydev.c
> +++ b/drivers/input/joydev.c
> @@ -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;


> @@ -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
Dan Carpenter July 3, 2021, 12:43 p.m. UTC | #5
On Sat, Jul 03, 2021 at 02:20:13PM +0300, Denis Efremov wrote:
> Hi,
> 
> CVE-2021-3612 was assigned to this patch.
> 
> 
> On 2/17/21 9:10 AM, Dan Carpenter wrote:
> > 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/20210620120030.1513655-1-avlarkin82@gmail.com/

regards,
dan carpenter

> > 
> > Fixes: 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/input/joydev.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
> > index a2b5fbba2d3b..750f4513fe20 100644
> > --- a/drivers/input/joydev.c
> > +++ b/drivers/input/joydev.c
> > @@ -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;
> 
> 
> > @@ -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
Denis Efremov July 3, 2021, 2:34 p.m. UTC | #6
On 7/3/21 3:43 PM, Dan Carpenter wrote:
> On Sat, Jul 03, 2021 at 02:20:13PM +0300, Denis Efremov wrote:
>> 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:
> 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.

>>
>>
>> On 2/17/21 9:10 AM, Dan Carpenter wrote:
>>> 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/20210620120030.1513655-1-avlarkin82@gmail.com/
> 
> regards,
> dan carpenter
> 
>>>
>>> Fixes: 999b874f4aa3 ("Input: joydev - validate axis/button maps before clobbering current ones")
>>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>>> ---
>>>  drivers/input/joydev.c | 4 ++--
>>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
>>> index a2b5fbba2d3b..750f4513fe20 100644
>>> --- a/drivers/input/joydev.c
>>> +++ b/drivers/input/joydev.c
>>> @@ -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;
>>
>>
>>> @@ -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
diff mbox series

Patch

diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
index a2b5fbba2d3b..750f4513fe20 100644
--- a/drivers/input/joydev.c
+++ b/drivers/input/joydev.c
@@ -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;