Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20220512130008.GI7074@brightrain.aerifal.cx>
Date: Thu, 12 May 2022 09:00:08 -0400
From: Rich Felker <dalias@...c.org>
To: Ismael Luceno <ismael@...ev.co.uk>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH v3 1/2] nftw: implement FTW_CHDIR

On Sun, Jan 23, 2022 at 04:59:54PM +0100, Ismael Luceno wrote:
> Signed-off-by: Ismael Luceno <ismael@...ev.co.uk>
> ---
>  src/misc/nftw.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/src/misc/nftw.c b/src/misc/nftw.c
> index 5b233b2b8e77..7569a657e54e 100644
> --- a/src/misc/nftw.c
> +++ b/src/misc/nftw.c
> @@ -87,6 +87,14 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
>  		DIR *d = fdopendir(dfd);
>  		if (d) {
>  			struct dirent *de;
> +			if (flags & FTW_CHDIR) {
> +				if (!fchdir(dfd)) {
> +					err = errno;
> +					closedir(d);
> +					errno = err;
> +					return -1;
> +				}
> +			}
>  			while ((de = readdir(d))) {
>  				if (de->d_name[0] == '.'
>  				 && (!de->d_name[1]

I'm not sure how this works -- after the fchdir, subsequent open at
line 69 will fail because the pathname is no longer valid relative to
the new working directory. I think different logic is needed at that
point too in order to adjust the argument to open depending on
FTW_CHDIR (or the whole thing could be converted to using at
functions, but that's a much bigger change I'd really only want to do
in a full overhaul of this function). I'm guessing you only tested
with absolute pathnames.

> @@ -123,6 +131,7 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
>  	int r, cs;
>  	size_t l;
>  	char pathbuf[PATH_MAX+1];
> +	int orig_dfd;
>  
>  	if (fd_limit <= 0) return 0;
>  
> @@ -133,9 +142,22 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
>  	}
>  	memcpy(pathbuf, path, l+1);
>  	
> +	if (flags & FTW_CHDIR) {
> +		orig_dfd = open(".", O_CLOEXEC | O_PATH);
> +		if (orig_dfd < 0)
> +			return -1;
> +	}
> +
>  	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
>  	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
>  	pthread_setcancelstate(cs, 0);
> +	if (flags & FTW_CHDIR) {
> +		if (!fchdir(orig_dfd))
> +			r = -1;
> +		int err = errno;
> +		close(orig_dfd);
> +		errno = err;
> +	}
>  	return r;
>  }
>  
> -- 
> 2.33.0

The added open and close calls need to be inside the range where
cancellation is blocked. Otherwise they will cause nftw to act on
cancellation.

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.