Blob Blame History Raw
diff -up patch-2.7/src/common.h.selinux patch-2.7/src/common.h
--- patch-2.7/src/common.h.selinux	2012-04-17 21:13:36.000000000 +0100
+++ patch-2.7/src/common.h	2012-10-18 17:04:21.215420195 +0100
@@ -30,6 +30,8 @@
 #include <sys/types.h>
 #include <time.h>
 
+#include <selinux/selinux.h>
+
 #include <sys/stat.h>
 
 #include <limits.h>
@@ -80,6 +82,7 @@ XTERN char *outfile;
 XTERN int inerrno;
 XTERN int invc;
 XTERN struct stat instat;
+XTERN security_context_t incontext;
 XTERN bool dry_run;
 XTERN bool posixly_correct;
 
diff -up patch-2.7/src/inp.c.selinux patch-2.7/src/inp.c
--- patch-2.7/src/inp.c.selinux	2012-08-07 20:46:33.000000000 +0100
+++ patch-2.7/src/inp.c	2012-10-18 17:18:31.335799300 +0100
@@ -138,7 +138,22 @@ get_input_file (char const *filename, ch
     char *getbuf;
 
     if (inerrno == -1)
-      inerrno = lstat (filename, &instat) == 0 ? 0 : errno;
+      {
+	inerrno = lstat (filename, &instat) == 0 ? 0 : errno;
+	if (inerrno == 0)
+	  {
+	    inerrno = getfilecon (inname, &incontext) == -1 ? errno : 0;
+	    if (inerrno == ENODATA || inerrno == ENOTSUP)
+	      {
+		inerrno = 0;
+		incontext = NULL;
+	      }
+	  }
+	else
+	  incontext = NULL;
+      }
+    else
+      incontext = NULL;
 
     /* Perhaps look for RCS or SCCS versions.  */
     if (S_ISREG (mode)
@@ -183,7 +198,7 @@ get_input_file (char const *filename, ch
 	    }
 
 	    if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
-				   &instat))
+				   &instat, &incontext))
 	      inerrno = 0;
 
 	    free (getbuf);
@@ -194,6 +209,7 @@ get_input_file (char const *filename, ch
       {
 	instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
 	instat.st_size = 0;
+	incontext = NULL;
       }
     else if (! ((S_ISREG (mode) || S_ISLNK (mode))
 	        && (mode & S_IFMT) == (instat.st_mode & S_IFMT)))
diff -up patch-2.7/src/Makefile.am.selinux patch-2.7/src/Makefile.am
--- patch-2.7/src/Makefile.am.selinux	2012-04-17 08:59:41.000000000 +0100
+++ patch-2.7/src/Makefile.am	2012-10-18 17:04:21.216420199 +0100
@@ -34,7 +34,7 @@ patch_SOURCES = \
 
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
-	      $(LIB_XATTR)
+	      $(LIB_XATTR) -lselinux
 
 if ENABLE_MERGE
   patch_SOURCES += merge.c
diff -up patch-2.7/src/Makefile.in.selinux patch-2.7/src/Makefile.in
--- patch-2.7/src/Makefile.in.selinux	2012-09-12 02:40:57.000000000 +0100
+++ patch-2.7/src/Makefile.in	2012-10-18 17:04:21.217420203 +0100
@@ -989,7 +989,7 @@ patch_SOURCES = bestmatch.h common.h inp
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib \
 	$(am__append_2)
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
-	      $(LIB_XATTR)
+	      $(LIB_XATTR) -lselinux
 
 all: all-am
 
diff -up patch-2.7/src/patch.c.selinux patch-2.7/src/patch.c
--- patch-2.7/src/patch.c.selinux	2012-08-08 12:24:46.000000000 +0100
+++ patch-2.7/src/patch.c	2012-10-18 17:04:21.219420213 +0100
@@ -552,7 +552,7 @@ main (int argc, char **argv)
 					     mode, &new_time);
 		      else
 			{
-			  attr |= FA_IDS | FA_MODE | FA_XATTRS;
+			  attr |= FA_IDS | FA_MODE | FA_XATTRS | FA_SECCONTEXT;
 			  set_file_attributes (TMPOUTNAME, attr, inname, &instat,
 					       mode, &new_time);
 			}
diff -up patch-2.7/src/pch.c.selinux patch-2.7/src/pch.c
--- patch-2.7/src/pch.c.selinux	2012-08-11 09:19:49.000000000 +0100
+++ patch-2.7/src/pch.c	2012-10-18 17:04:21.220420217 +0100
@@ -295,7 +295,12 @@ there_is_another_patch (bool need_header
 	    inname[t - buf - 1] = 0;
 	    if (lstat (inname, &instat) == 0)
 	      {
-		inerrno = 0;
+		inerrno = getfilecon (inname, &incontext) == -1 ? errno : 0;
+		if (errno == ENODATA || inerrno == ENOTSUP)
+		  {
+		    inerrno = 0;
+		    incontext = NULL;
+		  }
 		invc = -1;
 	      }
 	    else
@@ -927,7 +932,7 @@ intuit_diff_type (bool need_header, mode
 			  if (cs)
 			    {
 			      if (version_get (p_name[i], cs, false, readonly,
-					       getbuf, &st[i]))
+					       getbuf, &st[i], NULL))
 				stat_errno[i] = 0;
 			      else
 				version_controlled[i] = 0;
@@ -993,6 +998,9 @@ intuit_diff_type (bool need_header, mode
 	inerrno = stat_errno[i];
 	invc = version_controlled[i];
 	instat = st[i];
+	if (getfilecon (inname, &incontext) == -1)
+	  if (errno == ENODATA || errno == ENOTSUP)
+	    incontext = NULL;
       }
 
     return retval;
diff -up patch-2.7/src/util.c.selinux patch-2.7/src/util.c
--- patch-2.7/src/util.c.selinux	2012-08-11 09:20:49.000000000 +0100
+++ patch-2.7/src/util.c	2012-10-18 17:20:25.397226771 +0100
@@ -291,6 +291,19 @@ set_file_attributes (char const *to, enu
 		S_ISLNK (mode) ? "symbolic link" : "file",
 		quotearg (to));
     }
+  if (attr & FA_SECCONTEXT)
+    {
+      security_context_t outcontext;
+      if (incontext && getfilecon (to, &outcontext) != -1 &&
+	  outcontext &&
+	  strcmp (outcontext, incontext) &&
+	  setfilecon (to, incontext) != 0)
+	{
+	  if (errno != ENOTSUP && errno != EPERM)
+	    pfatal ("Can't set security context on file %s",
+		    quotearg (to));
+	}
+    }
 }
 
 static void
@@ -807,7 +820,8 @@ version_controller (char const *filename
    Return true if successful.  */
 bool
 version_get (char const *filename, char const *cs, bool exists, bool readonly,
-	     char const *getbuf, struct stat *filestat)
+	     char const *getbuf, struct stat *filestat,
+	     security_context_t *filecontext)
 {
   if (patch_get < 0)
     {
@@ -832,6 +846,13 @@ version_get (char const *filename, char
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
       if (stat (filename, filestat) != 0)
 	pfatal ("%s", quotearg (filename));
+      if (filecontext && getfilecon (filename, filecontext) == -1)
+	{
+	  if (errno == ENODATA || errno == ENOTSUP)
+	    *filecontext = NULL;
+	  else
+	    pfatal ("%s", quotearg (filename));
+	}
     }
 
   return 1;
diff -up patch-2.7/src/util.h.selinux patch-2.7/src/util.h
--- patch-2.7/src/util.h.selinux	2012-08-11 09:20:01.000000000 +0100
+++ patch-2.7/src/util.h	2012-10-18 17:04:21.220420217 +0100
@@ -45,7 +45,7 @@ char *parse_name (char const *, int, cha
 char *savebuf (char const *, size_t);
 char *savestr (char const *);
 char const *version_controller (char const *, bool, struct stat const *, char **, char **);
-bool version_get (char const *, char const *, bool, bool, char const *, struct stat *);
+bool version_get (char const *, char const *, bool, bool, char const *, struct stat *, security_context_t *);
 int create_file (char const *, int, mode_t, bool);
 int systemic (char const *);
 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], lin);
@@ -73,7 +73,8 @@ enum file_attributes {
   FA_TIMES = 1,
   FA_IDS = 2,
   FA_MODE = 4,
-  FA_XATTRS = 8
+  FA_XATTRS = 8,
+  FA_SECCONTEXT = 16
 };
 
 void set_file_attributes (char const *, enum file_attributes, char const *,