Hello,

The following two patches (1 for tar, 1 for gnulib) fix the bug I
reported earlier [1]. I ran 'make check' successfully on Linux as
well.

[1] http://lists.gnu.org/archive/html/bug-tar/2009-03/msg00005.html

-- David
From 5ae2d3ef36c367d4c65a38792247975f580c8e44 Mon Sep 17 00:00:00 2001
From: David Bartley <dtbar...@csclub.uwaterloo.ca>
Date: Sat, 7 Mar 2009 13:10:17 -0500
Subject: [PATCH] Add restore_unlink_dir function for restoring PRIV_SYS_LINKDIR on Solaris.

---
 ChangeLog       |    7 ++++++
 lib/unlinkdir.c |   56 ++++++++++++++++++++++++++++++++++++++++++++++++------
 lib/unlinkdir.h |    6 +++-
 3 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4f87a95..acb1dc8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2009-03-07  David Bartley  <dtbar...@csclub.uwaterloo.ca>
+
+	Add restore_unlink_dir function for restoring PRIV_SYS_LINKDIR on
+	Solaris.
+	* lib/unlinkdir.c: Add restore_unlink_dir.
+	* lib/unlinkdir.h: Likewise.
+
 2009-03-07  Bruno Haible  <br...@clisp.org>
 
 	Tests for module 'uninorm/u32-normcoll'.
diff --git a/lib/unlinkdir.c b/lib/unlinkdir.c
index 9c3ff00..1e8ff69 100644
--- a/lib/unlinkdir.c
+++ b/lib/unlinkdir.c
@@ -15,7 +15,7 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-/* Written by Paul Eggert and Jim Meyering.  */
+/* Written by Paul Eggert, Jim Meyering, and David Bartley.  */
 
 #include <config.h>
 
@@ -28,6 +28,11 @@
 
 #if ! UNLINK_CANNOT_UNLINK_DIR
 
+static bool initialized;
+# if defined PRIV_EFFECTIVE && defined PRIV_SYS_LINKDIR
+static bool had_sys_linkdir;
+#endif
+
 /* Return true if we cannot unlink directories, false if we might be
    able to unlink directories.  If possible, tell the kernel we don't
    want to be able to unlink directories, so that we can return true.
@@ -39,7 +44,6 @@
 bool
 cannot_unlink_dir (void)
 {
-  static bool initialized;
   static bool cannot;
 
   if (! initialized)
@@ -51,11 +55,15 @@ cannot_unlink_dir (void)
       priv_set_t *pset = priv_allocset ();
       if (pset)
 	{
-	  cannot =
-	    (getppriv (PRIV_EFFECTIVE, pset) == 0
-	     && (! priv_ismember (pset, PRIV_SYS_LINKDIR)
-		 || (priv_delset (pset, PRIV_SYS_LINKDIR) == 0
-		     && setppriv (PRIV_SET, PRIV_EFFECTIVE, pset) == 0)));
+	  if (getppriv (PRIV_EFFECTIVE, pset) == 0)
+	    {
+	      if (priv_ismember (pset, PRIV_SYS_LINKDIR))
+		cannot = had_sys_linkdir =
+		  (priv_delset (pset, PRIV_SYS_LINKDIR) == 0
+		  && setppriv (PRIV_SET, PRIV_EFFECTIVE, pset) == 0);
+	      else
+		cannot = true;
+	    }
 	  priv_freeset (pset);
 	}
 # else
@@ -68,4 +76,38 @@ cannot_unlink_dir (void)
   return cannot;
 }
 
+
+/* Return true if the kernel restored the ability of unlink to remove
+   directories.  */
+
+bool
+restore_unlink_dir (void)
+{
+  bool restored = false;
+
+# if defined PRIV_EFFECTIVE && defined PRIV_SYS_LINKDIR
+  if (initialized)
+    {
+      /* Try to re-add the PRIV_SYS_LINKDIR privilege, but only if we
+	 previously removed it.  */
+      if (had_sys_linkdir)
+        {
+	  priv_set_t *pset = priv_allocset ();
+	  if (pset)
+	    {
+	      if (getppriv (PRIV_EFFECTIVE, pset) == 0)
+		{
+		  initialized = ! (priv_addset (pset, PRIV_SYS_LINKDIR) == 0
+		    && setppriv (PRIV_SET, PRIV_EFFECTIVE, pset) == 0);
+		}
+	    }
+	  priv_freeset (pset);
+	  restored = ! initialized;
+	}
+    }
+# endif
+
+  return restored;
+}
+
 #endif
diff --git a/lib/unlinkdir.h b/lib/unlinkdir.h
index 10c4e5f..86cbd68 100644
--- a/lib/unlinkdir.h
+++ b/lib/unlinkdir.h
@@ -1,6 +1,6 @@
 /* unlinkdir.h - determine (and maybe change) whether we can unlink directories
 
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -15,12 +15,14 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-/* Written by Paul Eggert and Jim Meyering.  */
+/* Written by Paul Eggert, Jim Meyering, and David Bartley.  */
 
 #include <stdbool.h>
 
 #if UNLINK_CANNOT_UNLINK_DIR
 # define cannot_unlink_dir() true
+# define restore_unlink_dir() false
 #else
 bool cannot_unlink_dir (void);
+bool restore_unlink_dir (void);
 #endif
-- 
1.5.6.5

From 739297e42dc4618d585c658c60859248ad112d62 Mon Sep 17 00:00:00 2001
From: David Bartley <dtbar...@csclub.uwaterloo.ca>
Date: Sat, 7 Mar 2009 13:16:20 -0500
Subject: [PATCH] Fix bug on Solaris that prevents setuid root files from being created.

---
 ChangeLog.CVS |    6 ++++++
 src/extract.c |   13 +++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/ChangeLog.CVS b/ChangeLog.CVS
index 41227f5..f0c53ed 100644
--- a/ChangeLog.CVS
+++ b/ChangeLog.CVS
@@ -1,3 +1,9 @@
+2009-03-07  David Bartley  <dtbar...@csclub.uwaterloo.ca>
+
+	Fix bug on Solaris that prevents setuid root files from being created.
+	
+	* src/extract.c: Call restore_unlink_dir if chmod fails with EPERM.
+
 2009-03-05  Sergey Poznyakoff  <g...@gnu.org.ua>
 
 	* src/incremen.c: --no-recursive works with --incremental.
diff --git a/src/extract.c b/src/extract.c
index 6d70398..fa8ba4b 100644
--- a/src/extract.c
+++ b/src/extract.c
@@ -1,7 +1,7 @@
 /* Extract files from a tar archive.
 
    Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
-   2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   2001, 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
 
    Written by John Gilmore, on 1985-11-19.
 
@@ -24,6 +24,7 @@
 #include <utimens.h>
 #include <errno.h>
 #include <xgetcwd.h>
+#include <unlinkdir.h>
 
 #include "common.h"
 
@@ -186,7 +187,15 @@ set_mode (char const *file_name,
       mode = cur_info->st_mode ^ invert_permissions;
     }
 
-  if (chmod (file_name, mode) != 0)
+  bool failed = (chmod (file_name, mode) != 0);
+  if (failed && errno == EPERM)
+    {
+      /* On Solaris, chmod may fail if we dropped a privilege (this happens
+         when we call cannot_unlink_dir).  */
+      if (restore_unlink_dir ())
+        failed = (chmod (file_name, mode) != 0);
+    }
+  if (failed)
     chmod_error_details (file_name, mode);
 }
 
-- 
1.5.6.5

Reply via email to