Re: [PATCH] fix buffer overflow in udev_util_replace_whitespace()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

> > Indeed, excuse my brainfart.  Looking at the rest of the function I
> > agree it needs fixing,  Unless Andrey can correct us.
> > 
> 
> This change breaks udev_util_replace_whitespace().
> 
> #include <stdio.h>
> #include <stddef.h>
> 
> main()
> {
>         int len = 3, i, j;
>         char *str = "a b";
>         char to[10];
> 
>         len = strnlen(str, len - 1);
>         while (len && isspace(str[len-1]))
>                 len--;
> 
>         j = i = 0;
>         while (i < len) {
>                 /* substitute multiple whitespace with a single '_' */
>                 if (isspace(str[i])) {
>                         while (isspace(str[i]))
>                                 i++;
>                         to[j++] = '_';
>                         }
>                 to[j++] = str[i++];
>         }
>         to[j] = '\0';
> 
>         printf("'%s'\n", to);
> }
> 
> {pts/1}% gcc -O0 foo.c
> {pts/1}% ./a.out
> 'a'
> 
> instead of expected a_b

well, I obviously don't know what the expected behaviour is, unless
the expexted behaviour is what the code says, which would imply
potentially undefined behaviour and as such seemed rather unlikely to
me to be the expected behaviour.

So, if you expect me to say anything with regard to the expected
behaviour, other than that it probably ought to not be undefined,
could you provide a description of what the expected behaviour is?

> What is exact problem with original code? 

Assuming that len is the amount of memory available at to, it could
cause undefined behaviour by the means of a buffer overflow with
appropriate input (the code does not necessarily limit itself to writing
to the first len bytes at to, even for cases with len!=0).

Florian
--
To unsubscribe from this list: send the line "unsubscribe linux-hotplug" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel]     [Linux DVB]     [Asterisk Internet PBX]     [DCCP]     [Netdev]     [X.org]     [Util Linux NG]     [Fedora Women]     [ALSA Devel]     [Linux USB]

  Powered by Linux