All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrey Borzenkov <arvidjaar@mail.ru>
To: linux-hotplug@vger.kernel.org
Subject: Re: [PATCH] fix buffer overflow in udev_util_replace_whitespace()
Date: Sat, 05 Sep 2009 18:51:49 +0000	[thread overview]
Message-ID: <200909052251.52880.arvidjaar@mail.ru> (raw)
In-Reply-To: <20090831173335.GL4363@florz.florz.dyndns.org>

[-- Attachment #1: Type: Text/Plain, Size: 1433 bytes --]

On Saturday 05 of September 2009 22:17:52 Alan Jenkins wrote:
> On 9/5/09, Florian Zumbiehl <florz@florz.de> wrote:
> > Hi,
> >
> > [...]
> >
> >> after
> >>
> >> strnlen("a", 2-1) = 0
> >>
> > | $ cat foo.c
> > |
> > | #include <string.h>
> > | #include <stdio.h>
> > |
> > | int main(){
> > | 	printf("%u\n",strnlen("a",2-1));
> > | 	return 0;
> > | }
> > |
> > | $ gcc -o foo foo.c
> > | $ ./foo
> > | 1
> > | $
> >
> > Florian
> 
> 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

What is exact problem with original code? 

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

  parent reply	other threads:[~2009-09-05 18:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-31 17:33 [PATCH] fix buffer overflow in udev_util_encode_string() Florian Zumbiehl
2009-09-01 10:56 ` Kay Sievers
2009-09-02 20:55 ` Karel Zak
2009-09-04 19:34 ` [PATCH] fix buffer overflow in udev_util_replace_whitespace() Florian Zumbiehl
2009-09-05  3:19 ` Andrey Borzenkov
2009-09-05  4:21 ` Florian Zumbiehl
2009-09-05 10:44 ` Alan Jenkins
2009-09-05 17:01 ` Florian Zumbiehl
2009-09-05 18:17 ` Alan Jenkins
2009-09-05 18:51 ` Andrey Borzenkov [this message]
2009-09-05 20:21 ` Florian Zumbiehl
2009-09-05 20:57 ` Florian Zumbiehl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200909052251.52880.arvidjaar@mail.ru \
    --to=arvidjaar@mail.ru \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.