mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alexey Izbyshev <izbyshev@ispras.ru>
To: musl@lists.openwall.com
Subject: Re: [musl] realpath without procfs -- should be ready for inclusion
Date: Tue, 24 Nov 2020 06:39:59 +0300	[thread overview]
Message-ID: <48faf5ab9a1f3c869c85897217db0d75@ispras.ru> (raw)
In-Reply-To: <20201123205259.GZ534@brightrain.aerifal.cx>

On 2020-11-23 23:53, Rich Felker wrote:
> On Mon, Nov 23, 2020 at 01:56:33PM -0500, Rich Felker wrote:
>> On Sun, Nov 22, 2020 at 10:19:33PM -0500, Rich Felker wrote:
>> --- realpath8.c	2020-11-22 17:52:17.586481571 -0500
>> +++ realpath9.c	2020-11-23 13:55:06.808458893 -0500
>> @@ -19,7 +19,7 @@
>>  	char *output = resolved ? resolved : buf;
>>  	size_t p, q, l, cnt=0;
>> 
>> -	l = strnlen(filename, sizeof stack + 1);
>> +	l = strnlen(filename, sizeof stack);
>>  	if (!l) {
>>  		errno = ENOENT;
>>  		return 0;
>> @@ -80,11 +80,16 @@
>>  			return 0;
>>  		}
>>  		if (k==p) goto toolong;
>> +		if (!k) {
>> +			errno = ENOENT;
>> +			return 0;
>> +		}
>>  		if (++cnt == SYMLOOP_MAX) {
>>  			errno = ELOOP;
>>  			return 0;
>>  		}
>>  		p -= k;
>> +		if (stack[k-1]=='/') p++;
>>  		memmove(stack+p, stack, k);
> 
> This is wrong and needs further consideration.
> 
Yes, now memmove() overwrites NUL if p was at the end and stack[k-1] == 
'/'. Is it true per POSIX that "rr/home" must resolve to "//home" if 
"rr" -> "//"? If so, maybe something like the following instead:

+               while (stack[p] == '/') p++;
+               if (stack[p] && stack[k-1] != '/') p--;
                 p -= k;
-               if (stack[k-1]=='/') p++;

>>  	}
>> 
>> @@ -95,7 +100,8 @@
>>  		l = strlen(stack);
>>  		/* Cancel any initial .. components. */
>>  		p = 0;
>> -		while (q-p>=2 && at_dotdot(output+p+2, p+2)) {
>> +		while (output[p]=='.' && output[p+1]=='.'
>> +		  && (!output[p+2] || output[p+2]=='/')) {
>>  			while(l>1 && stack[l-1]!='/') l--;
>>  			if (l>1) l--;
>>  			p += 2;
> 
> OK, I have a better improvement for this: counting the number of
> levels of .. as they're built at the head of output. Then it's just
> while (nup--) here, and the condition for canceling .. in the first
> loop no longer needs any string inspection; it's just (q>3*nup).
> 
Sounds good.

I've missed the last time that the immediately following code is also 
broken:

>               if (q-p && stack[l-1]!='/') output[--p] = '/';

It will underflow the output in case of a simple relative path that 
doesn't start with "..".

I've also noticed other issues to be fixed, per POSIX:

* ENOENT should be returned if filename is NULL

* ENOTDIR should be returned if the last component is not a directory  
and the path has one or more trailing slashes

Alexey

  reply	other threads:[~2020-11-24  3:40 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-22 22:56 Rich Felker
2020-11-23  2:03 ` Alexey Izbyshev
2020-11-23  3:17   ` Érico Nogueira
2020-11-23  3:34     ` Rich Felker
2020-11-23  3:19   ` Rich Felker
2020-11-23 18:56     ` Rich Felker
2020-11-23 20:53       ` Rich Felker
2020-11-24  3:39         ` Alexey Izbyshev [this message]
2020-11-24  4:26           ` Rich Felker
2020-11-24  5:13             ` Alexey Izbyshev
2020-11-24  6:30               ` Rich Felker
2020-11-24  9:21                 ` Alexey Izbyshev
2020-11-24 14:35                   ` Rich Felker
2020-11-24 20:17                     ` Rich Felker
2020-11-25 15:02                   ` Rich Felker
2020-11-25 19:40                     ` Alexey Izbyshev
2020-11-24 20:31             ` Rich Felker
2020-11-25  5:40               ` Alexey Izbyshev
2020-11-25 15:03                 ` Rich Felker
2020-11-24  3:41     ` Alexey Izbyshev

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=48faf5ab9a1f3c869c85897217db0d75@ispras.ru \
    --to=izbyshev@ispras.ru \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).