zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: [Bug] Strange Globing Behaviour when used with sudo
Date: Fri, 1 Jun 2018 23:08:30 +0900	[thread overview]
Message-ID: <01B9BB58-DDAE-4056-AD9A-2367DC3AC20D@kba.biglobe.ne.jp> (raw)
In-Reply-To: <20180531181546.osffv3ysjzaxjfid@Daniels-MacBook-Air.local>


> 2018/06/01 03:15, Daniel Tameling <tamelingdaniel@gmail.com> wrote:
> 
> As s is NULL and buf is in the example ./file/, statfullpath adds to
> buf a . so that it's "./file/.".
(snip)
>  So one possibility to fix this is to
> call stat before adding the . to ./file/, and the return non-zero if
> it is not a directory.

Yes, but users can add '.' by themselves, for example
sudo zsh -c 'echo */.'. And of course you can not just
"return !S_ISDIR(st->st_mode)", and st may be NULL.

I've also fond that stat("file/..") is similarly broken.

In the patch below, I enclosed everything within "#ifdef __APPLE__"
to minimize the side effects. It is difficult to detect the bug
by configure since it requires sudo.


diff --git a/Src/glob.c b/Src/glob.c
index 66a95329f..95b3c7ca0 100644
--- a/Src/glob.c
+++ b/Src/glob.c
@@ -288,6 +288,26 @@ statfullpath(const char *s, struct stat *st, int l)
     DPUTS(strlen(s) + !*s + pathpos - pathbufcwd >= PATH_MAX,
 	  "BUG: statfullpath(): pathname too long");
     strcpy(buf, pathbuf + pathbufcwd);
+ 
+#ifdef __APPLE__
+    /*
+     * Workaround for broken stat()/lstat()/access() on macOS.
+     * If called by root, these syscalls mistakenly return success for
+     * path like "foo/bar/." or "foo/bar/.." even if "bar" is a file.
+     * We should make sure "bar" is a directory.
+     */
+    if (!*s || !strcmp(s, ".") || !strcmp(s, "..")) {
+	struct stat stbuf;
+	if (stat(unmeta(buf), &stbuf) || !S_ISDIR(stbuf.st_mode))
+		return -1;
+	if (strcmp(s, "..")) {	    /* done if s is "" or "." */
+	    if (st)
+		*st = stbuf;
+	    return 0;
+	}
+    }
+#endif
+
     strcpy(buf + pathpos - pathbufcwd, s);
     if (!*s && *buf) {
 	/*






  reply	other threads:[~2018-06-01 14:48 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-30 17:11 Bengt Brodersen
2018-05-30 18:06 ` Bart Schaefer
2018-05-30 19:15   ` Daniel Shahaf
2018-05-30 20:23     ` Phil Pennock
2018-05-30 20:44       ` Bengt Brodersen
2018-05-30 20:49         ` Bengt Brodersen
2018-05-31  8:44           ` Peter Stephenson
     [not found]           ` <20180531094403.05b62bee@camnpupstephen.cam.scsc.local>
2018-05-31  8:49             ` Peter Stephenson
2018-05-31 11:39               ` Jun T
2018-05-31 15:29                 ` Peter Stephenson
2018-05-31 18:15                   ` Daniel Tameling
2018-06-01 14:08                     ` Jun T. [this message]
2018-06-02 18:17                       ` Daniel Tameling
2018-06-03 16:30                         ` Jun T.
2018-06-03 18:48                           ` Daniel Tameling
2018-06-03 19:17                             ` dana
2018-06-04  1:23                               ` Jun T
2018-06-04 12:48                           ` Jun T
2018-06-14 12:26           ` Bengt Brodersen
2018-05-31  6:37       ` Martijn Dekker

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=01B9BB58-DDAE-4056-AD9A-2367DC3AC20D@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --cc=zsh-workers@zsh.org \
    /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/zsh/

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