From dima@KOT.SPb.Ru  Thu Nov 20 14:28:06 2003
Return-Path: <dima@KOT.SPb.Ru>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 3AFAC16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 20 Nov 2003 14:28:06 -0800 (PST)
Received: from orion.ifmo.ru (kot.spb.ru [194.85.164.6])
	by mx1.FreeBSD.org (Postfix) with ESMTP id F22574401E
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 20 Nov 2003 14:28:04 -0800 (PST)
	(envelope-from dima@KOT.SPb.Ru)
Received: from orion.ifmo.ru (dima@localhost [127.0.0.1])
	by orion.ifmo.ru (8.12.9p2/8.12.9) with ESMTP id hAKMS3Ti086238
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 21 Nov 2003 01:28:03 +0300 (MSK)
	(envelope-from dima@orion.ifmo.ru)
Received: (from dima@localhost)
	by orion.ifmo.ru (8.12.9p2/8.12.9/Submit) id hAKMS3JB086237;
	Fri, 21 Nov 2003 01:28:03 +0300 (MSK)
	(envelope-from dima)
Message-Id: <200311202228.hAKMS3JB086237@orion.ifmo.ru>
Date: Fri, 21 Nov 2003 01:28:03 +0300 (MSK)
From: Dmitry Afanasiev <dima@KOT.SPb.Ru>
Reply-To: Dmitry Afanasiev <dima@KOT.SPb.Ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: strange bug in /bin/sh
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         59530
>Category:       bin
>Synopsis:       strange bug in /bin/sh
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Nov 20 14:30:20 PST 2003
>Closed-Date:    Tue Feb 26 11:05:16 UTC 2008
>Last-Modified:  Tue Feb 26 11:05:16 UTC 2008
>Originator:     Dmitry Afanasiev
>Release:        FreeBSD 4.9-RC i386
>Organization:
>Environment:
System: FreeBSD orion 4.9-RC FreeBSD 4.9-RC #31: Mon Oct 6 16:12:22 MSD 2003 root@orion:/usr/obj/usr/src/sys/MATPOCKuH i386


>Description:
	If cd builted in /bin/sh called to directory with no permissions, current directory after this is is not valid.
>How-To-Repeat:
	(!) Not from root:
$ mkdir test
$ cd test
$ mkdir noperm
$ chmod 0 noperm
$ cd noperm
cd: can't cd to noperm
$ pwd
/tmp/test/noperm
$ cd noperm
cd: can't cd to noperm
$ cd noperm
cd: can't cd to noperm
$ pwd
/tmp/test/noperm/noperm/noperm
$ ls
noperm


>Fix:
--- cd.c.orig   Fri Nov 21 01:10:44 2003
+++ cd.c        Fri Nov 21 01:10:02 2003
@@ -204,7 +204,7 @@
        }
 
        INTOFF;
-       if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
+       if (chdir(curdir) < 0 || updatepwd(badstat ? NULL : dest) < 0) {
                INTON;
                return (-1);
        }

But this path may be incorrect. :)
>Release-Note:
>Audit-Trail:

From: "Danny J. Zerkel" <dzerkel@columbus.rr.com>
To: freebsd-gnats-submit@FreeBSD.org, dima@KOT.SPb.Ru
Cc:  
Subject: Re: bin/59530: strange bug in /bin/sh
Date: Fri, 19 Dec 2003 22:45:38 -0500

 Unfortunately, updatepwd() is what changes curdir to make the
 chdir(curdir) attempt to go to the new directory.  Doing the chdir()
 first, means don't go anywhere, just update where we think we are.
 
 I think the real problem is that the chdir() failure fall back is 
 cdphysical(), which if the chdir() fails MUST do updatepwd(NULL).
 
 So this patch appears to work correctly:
 
 *** cd.c.orig   Fri Dec 19 22:01:40 2003
 --- cd.c        Fri Dec 19 22:36:33 2003
 ***************
 *** 217,223 ****
    {
 
          INTOFF;
 !       if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
                  INTON;
                  return (-1);
          }
 --- 217,228 ----
    {
 
          INTOFF;
 !       if (chdir(dest) < 0) {
 !               updatepwd(NULL);
 !               INTON;
 !               return (-1);
 !       }
 !       if (updatepwd(NULL) < 0) {
                  INTON;
                  return (-1);
          }
 
 Danny J. Zerkel -- dzerkel@columbus.rr.com

From: "Danny J. Zerkel" <dzerkel@columbus.rr.com>
To: freebsd-gnats-submit@FreeBSD.org, dima@KOT.SPb.Ru
Cc:  
Subject: Re: bin/59530: strange bug in /bin/sh
Date: Fri, 19 Dec 2003 23:04:16 -0500

 On further review, PWD and OLDPWD are not correctly handled by my
 previous patch.  So I offer the following update, which only sets
 the environment variables only if we succeed in changing directory:
 
 *** cd.c.orig   Fri Dec 19 22:51:26 2003
 --- cd.c        Fri Dec 19 22:57:58 2003
 ***************
 *** 155,160 ****
 --- 155,163 ----
          if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0)
                  return (-1);
 
 +       setvar("PWD", curdir, VEXPORT);
 +       setvar("OLDPWD", prevdir, VEXPORT);
 +
          if (print && iflag && curdir)
                  out1fmt("%s\n", curdir);
 
 ***************
 *** 217,223 ****
    {
 
          INTOFF;
 !       if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
                  INTON;
                  return (-1);
          }
 --- 220,231 ----
    {
 
          INTOFF;
 !       if (chdir(dest) < 0) {
 !               updatepwd(NULL);
 !               INTON;
 !               return (-1);
 !       }
 !       if (updatepwd(NULL) < 0) {
                  INTON;
                  return (-1);
          }
 ***************
 *** 278,285 ****
                          INTON;
                          return (-1);
                  }
 -               setvar("PWD", curdir, VEXPORT);
 -               setvar("OLDPWD", prevdir, VEXPORT);
                  INTON;
                  return (0);
          }
 --- 286,291 ----
 ***************
 *** 310,317 ****
                  ckfree(prevdir);
          prevdir = curdir;
          curdir = savestr(stackblock());
 -       setvar("PWD", curdir, VEXPORT);
 -       setvar("OLDPWD", prevdir, VEXPORT);
          INTON;
 
          return (0);
 --- 316,321 ----
 
 Danny J. Zerkel -- dzerkel@columbus.rr.com

From: swp@swp.pp.ru
To: freebsd-gnats-submit@FreeBSD.org, dima@KOT.SPb.Ru
Cc: swp@swp.pp.ru, dzerkel@columbus.rr.com
Subject: Re: bin/59530: strange bug in /bin/sh
Date: Sun, 18 Jul 2004 16:51:00 +0700 (OMSST)

 this fix incorrect too. please see bin/64990
 
 /swp
 

From: Maxim Konovalov <maxim@macomnet.ru>
To: swp@swp.pp.ru
Cc: bug-followup@freebsd.org
Subject: Re: bin/59530: strange bug in /bin/sh
Date: Sun, 18 Jul 2004 14:03:48 +0400 (MSD)

 On Sun, 18 Jul 2004, 10:00-0000, swp@swp.pp.ru wrote:
 
 > The following reply was made to PR bin/59530; it has been noted by GNATS.
 >
 > From: swp@swp.pp.ru
 > To: freebsd-gnats-submit@FreeBSD.org, dima@KOT.SPb.Ru
 > Cc: swp@swp.pp.ru, dzerkel@columbus.rr.com
 > Subject: Re: bin/59530: strange bug in /bin/sh
 > Date: Sun, 18 Jul 2004 16:51:00 +0700 (OMSST)
 >
 >  this fix incorrect too. please see bin/64990
 
 Have you tried my version I sent you yesterday?
 
 -- 
 Maxim Konovalov
State-Changed-From-To: open->closed 
State-Changed-By: gavin 
State-Changed-When: Tue Feb 26 11:02:07 UTC 2008 
State-Changed-Why:  
Close, this has been fixed in -HEAD.  PRs 64990, 101316, 120571 are 
also duplicates. (spotted by UEm#freebsd-bugbusters) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=59530 
>Unformatted:
