Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [patch] Avoid race in zf_mkdir
- X-seq: zsh-workers 47443
- From: Matthew Martin <phy1729@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: [patch] Avoid race in zf_mkdir
- Date: Fri, 9 Oct 2020 16:40:18 -0500
- Archived-at: <https://zsh.org/workers/47443>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-10/20201009214018.GB6449%40CptOrmolo.darkstar>
- Authentication-results: zsh.org; iprev=pass (mail-ot1-f68.google.com) smtp.remote-ip=209.85.210.68; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=N0bGAjNVUEsNCq3ay2nK2ZD6E+XoOonjdBePgDSBAuQ=; b=VDbKjYPxq0V7pWOMof5jg2WuLS4VzrxGO+sE3kHOz4QWBrXs3pcb6mbTai+FbKPZ2F 62Y/CntnEm8NnKkq3j0flsOMY+NNadBn0fF2/jXE+u3Ocou2R2sdBpH4VAYCYDL0dFW+ 7qYiGVcGHXUKSeNlqDQYyNAlgmLPHOCPc+Ez23DVEDKXWdbfa+oKkpWRp9rDRizyxeMv vnZnq64/PV/vRr3MHYb0tUWuhd0v3aF3a+as60GMrl7veoJtEa1jFrvGeFGmw8Dj+HQ+ r1aTHHK4buB0A0ipDMUT/PkqbGoQMCIrvx9PczdCnVtcgkj4Fdk6umLXYwwbGPSFrJ+z EQnQ==
- In-reply-to: <CAN=4vMqJ8SXkz_EVqwL7-9gCQYg9GMczOztAtsErirXGEDRq8Q@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-workers.zsh.org>
- List-owner: <mailto:zsh-workers-request@zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
- Mail-followup-to: zsh-workers@xxxxxxx
- References: <20201009200737.GA78914@CptOrmolo.darkstar> <CAH+w=7anY9r+-MYki4cycVuaP2+d9L3W5u8k3sHyZx4R5kvafg@mail.gmail.com> <CAN=4vMoJrD+g4Go37yT6gF-Lx61vHm45GvmG1Fx6-+3yyb1hAQ@mail.gmail.com> <CAH+w=7ZRvW3Yq3XO6TwbK1hgSDNE8ynX0eQOU2dTuLcAELNUDg@mail.gmail.com> <20201009205357.GA6449@CptOrmolo.darkstar> <CAN=4vMqJ8SXkz_EVqwL7-9gCQYg9GMczOztAtsErirXGEDRq8Q@mail.gmail.com>
- Sender: zsh-workers-request@xxxxxxx
On Fri, Oct 09, 2020 at 11:22:00PM +0200, Roman Perepelitsa wrote:
> Perhaps something like this? This should provide the following
> guarantees for zf_mkdir -p:
>
> - If it succeeds, the directory must have existed at some point during
> the execution of the function (either created by zf_mkdir itself or by
> some other concurrent process).
> - If it fails, there must have been a point in time during the
> execution of the function where the target directory or one of its
> parents didn't exist and it was impossible to create it.
>
> `zf_mkdir -p foo` It should work as expected in the face of concurrent
> `mkdir foo && rmdir foo` or `touch foo && rm foo`.
>
> I confess that I haven't tested it.
>
> Roman.
> diff --git a/Src/Modules/files.c b/Src/Modules/files.c
> index 6d20e38a8..a9ccccb8b 100644
> --- a/Src/Modules/files.c
> +++ b/Src/Modules/files.c
> @@ -122,19 +122,28 @@ domkdir(char *nam, char *path, mode_t mode, int p)
> {
> int err;
> mode_t oumask;
> + struct stat st;
> char const *rpath = unmeta(path);
>
> - if(p) {
> - struct stat st;
> -
> - if(!stat(rpath, &st) && S_ISDIR(st.st_mode))
> + while(1) {
> + oumask = umask(0);
> + err = mkdir(rpath, mode) ? errno : 0;
> + umask(oumask);
> + if (!err)
> + return 0;
> + if(!p || err != EEXIST)
> + break;
> + if(!stat(rpath, &st)) {
> + if(errno == ENOENT)
> + continue;
For a sufficiently well timed attacker, the target could be created and
deleted so that this loop never exits. Even if pathological, I don't
think it should be possible for mkdir to loop forever.
> + err = errno;
> + break;
> + }
> + if(S_ISDIR(st.st_mode))
> return 0;
> + break;
> }
> - oumask = umask(0);
> - err = mkdir(rpath, mode) ? errno : 0;
> - umask(oumask);
> - if(!err)
> - return 0;
> +
> zwarnnam(nam, "cannot make directory `%s': %e", path, err);
> return 1;
> }
Messages sorted by:
Reverse Date,
Date,
Thread,
Author