Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220514042409.GO7074@brightrain.aerifal.cx>
Date: Sat, 14 May 2022 00:24:09 -0400
From: Rich Felker <dalias@...c.org>
To: Oliver Ford <ojford@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH musl v2 3/3] mntent: fix parsing lines with
 optional fields

On Fri, May 13, 2022 at 10:39:17PM +0100, Oliver Ford wrote:
> On Thu, May 12, 2022 at 10:10 PM Rich Felker <dalias@...c.org> wrote:
> > This fails to check that the match is at the start of an option
> > (preceded by a ',' or at the beginning of string) and fails to
> > continue if the first match is a false positive (e.g. "ro" in
> > "symlinkroot,ro"). It's possible to solve this still using strstr in a
> > loop, but it might be easier to just iterate delimiters and strcmp.
> >
> > Also, I'm not sure if hasmntopt is supposed to return a match or not
> > for something like "uid" in "uid=1001"; if so, being followed by '='
> > also needs to be considered valid match.
> >
> > Rich
> 
> Comparing glibc and bionic, they both match when followed by an '='.
> So the below function handles that, and replaces strstr with an
> strncmp and a loop. If this version is ok I'll submit a patch?
> 
> char *hasmntopt(const struct mntent *mnt, const char *opt)
> {
>     char *ptr = mnt->mnt_opts;
>     size_t len = strlen(opt);
> 
>     while (ptr) {
>         char *end = ptr + len;

This has UB unless ptr points to at least len bytes. You can only
evaluate ptr+len or ptr[len] after confirming that. The strncmp is the
most natural (and free) way of confirming it. You could assign end
after the strncmp but I think just using ptr[len] instead of *end
makes it easier.

>         if (!strncmp(ptr, opt, len) &&
>                  (*end == '\0' || *end == ',' || *end == '=')) return ptr;
>         ptr = strchr(ptr, ',');
>         if (ptr) ptr++;
>     }
> 
>     return NULL;

Minor style nit: NULL is generally not used in musl anymore; just 0 is
fine here.

BTW you dropped the list from CC; I re-added it.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.