Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [patch] Avoid race in zf_mkdir
- X-seq: zsh-workers 47444
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Subject: Re: [patch] Avoid race in zf_mkdir
- Date: Sat, 10 Oct 2020 13:50:20 +0200
- Archived-at: <https://zsh.org/workers/47444>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-10/CAN%3D4vMpuSaF6BDhhUP%3DtnvDDMTN%3DiKExkmtb5QgtoeCqN_9D3g%40mail.gmail.com>
- Authentication-results: zsh.org; iprev=pass (mail-il1-f195.google.com) smtp.remote-ip=209.85.166.195; dkim=pass header.d=gmail.com header.s=20161025 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6/9G7gtMHNSp7iqE/VPmeZLvCNMX7RkfVadY0KqHCY8=; b=Ej4BWvsKracbP/2/m2mqsDgNkFdT4boaw+BJo25xL155y6HdF4acugUOpI/7z9t+dV K8SmbHawGH4rtll6ps7rlUYeIOo1lTUgP3rcQnmdLMLZBRGZx23ZFoKme5ErBMb/YYV3 /uwkdKKE9FX6baOPc6bwoSunwoW/sV92MkrBOPy5RHqsz8o+4YBOUJMIcSAVXMTLD9OP UVGljxb/1iDJHVTeIxWRydCF/YE7w///ziiQmgYRrpq9cNmlO3GyEMjvkIguGXwWB1NW n+djIsuziRdb2g6Al0RHYZqR6RIfscqVoxrNeWUvCloi/JwR2rxL1EY1LPIycErVijAW 7xIg==
- In-reply-to: <CAH+w=7ZfkwJY6a1LzR3n=LsOmB21WKUZMXfNzXN1hNDFpdeG2w@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>
- 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> <CAH+w=7ZfkwJY6a1LzR3n=LsOmB21WKUZMXfNzXN1hNDFpdeG2w@mail.gmail.com>
- Sender: zsh-workers-request@xxxxxxx
On Fri, Oct 9, 2020 at 11:27 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> The document linked by Matthew asserts that "mkdir -m mode" should behave "as if" chmod() is called after creating the directory.
This applies only when mkdir creates a directory but not when the
directory already existed prior to the call. Here's the relevant part:
Each dir operand that names an existing directory shall be ignored
without error.
On Fri, Oct 9, 2020 at 11:40 PM Matthew Martin <phy1729@xxxxxxxxx> wrote:
>
> 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.
Perhaps try N times instead of forever? The patch you've posted uses N
= 1 (which is already better than the existing code) but it can be any
other number.
Roman.
diff --git a/Src/Modules/files.c b/Src/Modules/files.c
index 6d20e38a8..5a58ad600 100644
--- a/Src/Modules/files.c
+++ b/Src/Modules/files.c
@@ -122,19 +122,29 @@ domkdir(char *nam, char *path, mode_t mode, int p)
{
int err;
mode_t oumask;
+ struct stat st;
+ int n = 8;
char const *rpath = unmeta(path);
- if(p) {
- struct stat st;
-
- if(!stat(rpath, &st) && S_ISDIR(st.st_mode))
+ while(n--) {
+ 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;
+ 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