LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert@linux-m68k.org>
To: Richard Weinberger <richard@nod.at>
Cc: uml-devel <user-mode-linux-devel@lists.sourceforge.net>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [uml-devel] [PATCH 07/15] hostfs: Remove open coded strcpy()
Date: Mon, 16 Mar 2015 13:44:48 +0100	[thread overview]
Message-ID: <CAMuHMdVjPV27+E9WQp18oZiaUWX2XS=_mTtaZOVN0o4wfjCq1g@mail.gmail.com> (raw)
In-Reply-To: <5506CA23.8090106@nod.at>

Hi Richard,

On Mon, Mar 16, 2015 at 1:18 PM, Richard Weinberger <richard@nod.at> wrote:
> Am 16.03.2015 um 13:03 schrieb Geert Uytterhoeven:
>> On Mon, Mar 16, 2015 at 12:41 PM, Richard Weinberger <richard@nod.at> wrote:
>>> --- a/fs/hostfs/hostfs_kern.c
>>> +++ b/fs/hostfs/hostfs_kern.c
>>> @@ -105,11 +105,10 @@ static char *__dentry_name(struct dentry *dentry, char *name)
>>
>> This code looks fishy to me...
>>
>> First we have:
>>
>>     len = strlen(root);
>>     strlcpy(name, root, PATH_MAX);
>>
>> (I notice the code used strncpy() before. One difference with strlcpy()
>>  is that strncpy() fills the remaining of the destination buffer with zeroes.)
>>
>> Then:
>>
>>>                 __putname(name);
>>>                 return NULL;
>>>         }
>>> -       if (p > name + len) {
>>> -               char *s = name + len;
>>
>> Unless strlcpy() truncated the string (which is unlikely, as root
>> cannot be longer
>> than PATH_MAX?), s = name + len now points to the zero terminator.
>> So the below would copy just one single byte:

Oops, that's of course not true, as s is the destination, not the source,
of the copy operation.

>>> -               while ((*s++ = *p++) != '\0')
>>> -                       ;
>>> -       }
>>> +
>>> +       if (p > name + len)
>>> +               strcpy(name + len, p);
>>> +
>>
>> What is this code really supposed to do?
>
> Hostfs' __dentry_name() builds the real path. i.e, the prefix on the host side
> plus the requested path in UML.
>
> "strlcpy(name, root, PATH_MAX);" copies the host prefix into name and then
> the "strcpy(name + len, p);" copies the requested path into it.
>
> The trick is that both share the same buffer, allocated by dentry_path_raw().

Ah, so the path is stored in the end of the buffer...

> Therefore this bounds check works:
>         if (len > p - name) {

... and if this is true, prefix and path would overlap, which means there's\
not enough space in the buffer.

>                 __putname(name);
>                 return NULL;
>         }
>
> Is it now clearer or did I miss something?
> I agree that this code is tricky. :)

Yes, thanks for your explanation!

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

  reply	other threads:[~2015-03-16 12:44 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-16 11:41 Hostfs updates Richard Weinberger
2015-03-16 11:41 ` [PATCH 01/15] hostfs: hostfs_file_open: Switch to data locking model Richard Weinberger
2015-03-16 11:41 ` [PATCH 02/15] hostfs: hostfs_file_open: Fix a fd leak in hostfs_file_open Richard Weinberger
2015-03-16 11:41 ` [PATCH 03/15] hostfs: Allow fsync on directories Richard Weinberger
2015-03-16 11:41 ` [PATCH 04/15] hostfs: Handle bogus st.mode Richard Weinberger
2015-03-16 11:41 ` [PATCH 05/15] hostfs: Make hostfs_readpage more readable Richard Weinberger
2015-03-16 11:41 ` [PATCH 06/15] hostfs: Add a BUG_ON to detect behavior changes of dentry_path_raw() Richard Weinberger
2015-03-16 11:41 ` [PATCH 07/15] hostfs: Remove open coded strcpy() Richard Weinberger
2015-03-16 12:03   ` [uml-devel] " Geert Uytterhoeven
2015-03-16 12:18     ` Richard Weinberger
2015-03-16 12:44       ` Geert Uytterhoeven [this message]
2015-03-16 11:41 ` [PATCH 08/15] hostfs: Use __getname() in follow_link Richard Weinberger
2015-03-16 11:41 ` [PATCH 09/15] hostfs: Report append flag in ->show_options() Richard Weinberger
2015-03-16 11:41 ` [PATCH 10/15] hostfs: Remove superfluous test in hostfs_open() Richard Weinberger
2015-03-16 11:41 ` [PATCH 11/15] hostfs: hostfs_open: Reset open flags upon each retry Richard Weinberger
2015-03-16 11:41 ` [PATCH 12/15] hostfs: Remove superfluous initializations in hostfs_open() Richard Weinberger
2015-03-16 11:41 ` [PATCH 13/15] hostfs: Set page flags in hostfs_readpage() correctly Richard Weinberger
2015-03-16 11:41 ` [PATCH 14/15] hostfs: Use page_offset() Richard Weinberger
2015-03-16 11:41 ` [PATCH 15/15] hostfs: No need to box and later unbox the file mode Richard Weinberger

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='CAMuHMdVjPV27+E9WQp18oZiaUWX2XS=_mTtaZOVN0o4wfjCq1g@mail.gmail.com' \
    --to=geert@linux-m68k.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=richard@nod.at \
    --cc=user-mode-linux-devel@lists.sourceforge.net \
    --subject='Re: [uml-devel] [PATCH 07/15] hostfs: Remove open coded strcpy()' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).