From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 75d6997a for ; Thu, 25 Apr 2019 13:32:05 +0000 (UTC) Received: (qmail 4554 invoked by alias); 25 Apr 2019 13:31:51 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 44254 Received: (qmail 1469 invoked by uid 1010); 25 Apr 2019 13:31:51 -0000 X-Qmail-Scanner-Diagnostics: from mailout1.w1.samsung.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.1/25426. spamassassin: 3.4.2. Clear:RC:0(210.118.77.11):SA:0(-7.0/5.0):. Processed in 3.308698 secs); 25 Apr 2019 13:31:51 -0000 X-Envelope-From: p.stephenson@samsung.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.samsung.com designates 210.118.77.11 as permitted sender) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20190425133111euoutp010f5b97d362bd2dc28b0a01b6424ad4a9~Yus9nAsxq1591115911euoutp01R DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1556199071; bh=IR6eB4cI63wBhV6orEiVoM0mzqVYFMkOsRPLW8rFlDo=; h=Subject:From:To:Date:In-Reply-To:References:From; b=q1GXISuCXYVum+q+zRReF3gdlof37BRjkFDvnDIJst85KpIlyJxC1DwndbsYGsxoU FB0Z1UpdpIYspUyI2B6nvNO30rj+Xr0KopNdLuijBL1uQIF6uR/kchGRc05aFXNR8/ yYjMhIYjtpETBGr5XjM1YixZ4Ye1swkSZasgxUbM= X-AuditID: cbfec7f4-113ff70000001119-9e-5cc1b69e766d Message-ID: <1556199068.2667.13.camel@samsung.com> Subject: Re: cwd unintentionally changed From: Peter Stephenson To: Date: Thu, 25 Apr 2019 14:31:08 +0100 In-Reply-To: X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrPIsWRmVeSWpSXmKPExsWy7djPc7rzth2MMXhwStLiYPNDJgdGj1UH PzAFMEZx2aSk5mSWpRbp2yVwZRx9f5GxYJdwxYyPu5gaGA/wdzFyckgImEisPvSUtYuRi0NI YAWjxJKv15ggnD4miSM9z6GcXiaJh/MesHQxcoC1/HwpCRFfzihx9usuFriipql9UB1nGCUm fp0BlbnAKPH9xxJGkI28AkYSF8/8YQaxhQU0JLae+c0KYrMJGEpM3TQbrEZEQFLiWvNpMJtF QFXi9KF9YDWcAs4SPxYvZYW4XENiw81jTBAzBSVOznzCAmIzC8hLNG+dzQyyWELgMZvEwXVT WCAaXCQ2dC1mh7CFJV4d3wJly0icntzDAtHQziixZtJrdginh1Fi09E7jBBV1hJ9ty8ygkKA WUBTYv0ufYiwo8T6/TOZIAHDJ3HjrSDEEXwSk7ZNZ4YI80p0tAlBVKtJ7GjayggRlpF4ukZh AqPSLCQfzELywSyEVQsYmVcxiqeWFuempxYb5aWW6xUn5haX5qXrJefnbmIEJoPT/45/2cG4 60/SIUYBDkYlHl6FyAMxQqyJZcWVuYcYJTiYlUR41U0PxgjxpiRWVqUW5ccXleakFh9ilOZg URLnrWZ4EC0kkJ5YkpqdmlqQWgSTZeLglGpgdH8bd1b/Vfj87L8BvWwmj9gTHjgdMzPldKjO 0GG7ylc/e5qGl9reuQfWejr8m/judly909+eSR9O3ZrpvVH30yej1ZrT7i+uPn/0Zvathoof U7/es0zZvj1v/qmj83fc2rshfJci56N3cqq7WbSbk/50cde5GRc/lNKYkPfy+ssDj8RCNkX+ nKDEUpyRaKjFXFScCADckKtkAgMAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpikeLIzCtJLcpLzFFi42I5/e/4Xd252w7GGHxao29xsPkhkwOjx6qD H5gCGKP0bIryS0tSFTLyi0tslaINLYz0DC0t9IxMLPUMjc1jrYxMlfTtbFJSczLLUov07RL0 Mo6+v8hYsEu4YsbHXUwNjAf4uxg5OCQETCR+vpTsYuTiEBJYyigx7esdli5GTqC4jMSnKx/Z IWxhiT/XutggirqZJE7dmsII4ZxhlFhz6RhU5gKjxLbfT5hBWngFjCQunvkDZgsLaEhsPfOb FcRmEzCUmLppNiOILSIgKXGt+TSYzSKgKnH60D6wGk4BZ4kfi5eyQgydzihx5uwCsEHMApoS rdt/Q92kIbHh5jEmiGWCEidnPmGBqJGXaN46m3kCo9AsJC2zkJTNQlK2gJF5FaNIamlxbnpu sZFecWJucWleul5yfu4mRmD4bzv2c8sOxq53wYcYBTgYlXh4V2w8GCPEmlhWXJl7iFGCg1lJ hFfdFCjEm5JYWZValB9fVJqTWnyI0RToo4nMUqLJ+cDYzCuJNzQ1NLewNDQ3Njc2s1AS5+0Q AGoSSE8sSc1OTS1ILYLpY+LglGpg7FmxSnOBR6qTCO95tQ3T3jdopussSAtt/Dbt0Hx7yTLB TaVrfO/Mm+twTbbX5ZL37Yv6fspyd5c2TZmy4teH7JcsiwVbFvsFzpjcWqpicdp4dXP6VP/u NHujq28fOlzQ7975Jj3JuWLJt2N7dN+qps28mn6kmKNWpb94W06wy7mTvWdrWnvMlFiKMxIN tZiLihMBYgcY9ZUCAAA= X-CMS-MailID: 20190425133109eucas1p1cc72f374dfe7ede7ba099f2338bbc9a3 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20190425120502epcas5p21b83b94136e0b2fae1002220c7d52656 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20190425120502epcas5p21b83b94136e0b2fae1002220c7d52656 References: On Thu, 2019-04-25 at 14:03 +0200, Yannic Schröder wrote: > Hi *, >  > I got myself into a situation where zsh changed my working directory to > "/" without further notice. Unfortunately, the next command I issued was > "sudo rm -rf *", which did not end well with cwd being "/" :-( >  > My colleagues and me started to track down the bug. Our efforts are > documented here: > https://github.com/grml/grml-etc-core/issues/76 >  > (We initially though it was a bug with grml zsh config.) >  > A minimal example that triggers the bug looks like this: >  > $ mkdir /tmp/tmp.0HnyRZ1iXv/ > $ cd /tmp/tmp.0HnyRZ1iXv/ > $ mkdir a > $ sudo zsh -f -c 'mount -t tmpfs tmpfs a; cd a; touch x; ls; umount > --lazy ../a; ls; foo=.; echo ${foo:a}; ls; realpath .; echo $PWD;' Thanks, easy to follow. That's certainly not pleasant as it's silent. The source of the problem is in zgetdir().  We attempt to climb the directory hierarchy until we get to /.  In this case we appear to get there immediately.  Then, fatally, we call zchdir() to the place where we think we are, but actually we aren't. The patch below fixes up this case so we make quite sure we're in /. This looks safe, but I'm not sure it's necessarily the limit of what we can do to make things as safe as possible... I'm not quite sure why we need to zchdir().  It seems to be a side effect rather than a basic part of the call.  In particular I don't see why it's a good idea in this case --- maybe we just add an argument saying "not actually changing directory"? Or should we be using getcwd() nowadays?  Presumably this is fine on most modern systems? pws diff --git a/Src/compat.c b/Src/compat.c index 7b5c4411c..7131d91a4 100644 --- a/Src/compat.c +++ b/Src/compat.c @@ -361,8 +361,18 @@ zgetdir(struct dirsav *d)   pino = sbuf.st_ino;   pdev = sbuf.st_dev;   - /* If they're the same, we've reached the root directory. */ + /* If they're the same, we've reached the root directory... */   if (ino == pino && dev == pdev) { +     /* +      * ...well, probably.  If this was an orphaned . after +      * an unmount, or something such, we could be in trouble... +      */ +     if (stat("/", &sbuf) < 0 || + sbuf.st_ino != ino || + sbuf.st_dev != dev) { + zerr("Failed to get current directory: path invalid"); + return NULL; +     }       if (!buf[pos])   buf[--pos] = '/';       if (d) {