1151a30
diff -up patch-2.6.1/Makefile.in.selinux patch-2.6.1/Makefile.in
68f90be
--- patch-2.6.1/Makefile.in.selinux	2011-02-08 11:29:34.590271489 +0000
68f90be
+++ patch-2.6.1/Makefile.in	2011-02-08 11:29:34.602271607 +0000
4c70b2a
@@ -40,7 +40,7 @@ EXEEXT = @EXEEXT@
4c70b2a
 LDFLAGS = @LDFLAGS@
4c70b2a
 LIBOBJDIR = gl/lib/
4c70b2a
 LIBOBJS = @LIBOBJS@ ${LIBOBJDIR}full-write$U.o
4c70b2a
-LIBS = @LIBS@
4c70b2a
+LIBS = @LIBS@ -lselinux
4c70b2a
 OBJEXT = @OBJEXT@
4c70b2a
 PACKAGE_NAME = @PACKAGE_NAME@
4c70b2a
 PACKAGE_VERSION = @PACKAGE_VERSION@
1151a30
diff -up patch-2.6.1/src/common.h.selinux patch-2.6.1/src/common.h
1151a30
--- patch-2.6.1/src/common.h.selinux	2009-12-30 12:56:30.000000000 +0000
68f90be
+++ patch-2.6.1/src/common.h	2011-02-08 11:29:34.602271607 +0000
4c70b2a
@@ -32,6 +32,8 @@
ffc4fff
 #include <sys/types.h>
ffc4fff
 #include <time.h>
3db4109
 
ffc4fff
+#include <selinux/selinux.h>
ffc4fff
+
ffc4fff
 #include <sys/stat.h>
ffc4fff
 #if ! defined S_ISDIR && defined S_IFDIR
ffc4fff
 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
4c70b2a
@@ -118,6 +120,7 @@ XTERN char *outfile;
ffc4fff
 XTERN int inerrno;
ffc4fff
 XTERN int invc;
ffc4fff
 XTERN struct stat instat;
ffc4fff
+XTERN security_context_t incontext;
ffc4fff
 XTERN bool dry_run;
ffc4fff
 XTERN bool posixly_correct;
ffc4fff
 
1151a30
diff -up patch-2.6.1/src/inp.c.selinux patch-2.6.1/src/inp.c
68f90be
--- patch-2.6.1/src/inp.c.selinux	2011-02-08 11:29:34.576271352 +0000
68f90be
+++ patch-2.6.1/src/inp.c	2011-02-08 11:29:34.604271627 +0000
1151a30
@@ -152,8 +152,18 @@ get_input_file (char const *filename, ch
1151a30
     char *diffbuf;
638a40a
     char *getbuf;
638a40a
 
1151a30
-    if (inerrno == -1)
4c70b2a
-      inerrno = stat (filename, &instat) == 0 ? 0 : errno;
1151a30
+    inerrno = stat (filename, &instat) == 0 ? 0 : errno;
1151a30
+    if (inerrno == 0)
638a40a
+      {
1151a30
+        inerrno = getfilecon (inname, &incontext) == -1 ? errno : 0;
1151a30
+        if (inerrno == ENODATA || inerrno == ENOTSUP)
1151a30
+          {
1151a30
+    	inerrno = 0;
1151a30
+    	incontext = NULL;
1151a30
+          }
638a40a
+      }
1151a30
+    else
1151a30
+      incontext = NULL;
638a40a
 
638a40a
     /* Perhaps look for RCS or SCCS versions.  */
638a40a
     if (patch_get
1151a30
@@ -197,7 +207,7 @@ get_input_file (char const *filename, ch
638a40a
 	    }
638a40a
 
638a40a
 	    if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
638a40a
-				   &instat))
638a40a
+				   &instat, &incontext))
638a40a
 	      inerrno = 0;
638a40a
 
638a40a
 	    free (getbuf);
1151a30
@@ -209,6 +219,7 @@ get_input_file (char const *filename, ch
638a40a
       {
638a40a
 	instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
638a40a
 	instat.st_size = 0;
638a40a
+	incontext = NULL;
638a40a
       }
638a40a
     else if (! S_ISREG (instat.st_mode))
638a40a
       fatal ("File %s is not a regular file -- can't patch",
1151a30
diff -up patch-2.6.1/src/patch.c.selinux patch-2.6.1/src/patch.c
68f90be
--- patch-2.6.1/src/patch.c.selinux	2011-02-08 11:29:34.586271450 +0000
68f90be
+++ patch-2.6.1/src/patch.c	2011-02-08 11:29:34.606271646 +0000
4c70b2a
@@ -421,6 +421,21 @@ main (int argc, char **argv)
4c70b2a
 			      /* Fails if we are not in group instat.st_gid.  */
4c70b2a
 			      chown (outname, -1, instat.st_gid);
4c70b2a
 			    }
4c70b2a
+
4c70b2a
+			  if (! inerrno && incontext)
4c70b2a
+			    {
4c70b2a
+			      security_context_t outcontext;
35f37f0
+			      if (getfilecon (outname, &outcontext) != -1 &&
4c70b2a
+				  outcontext &&
4c70b2a
+				  strcmp(outcontext, incontext) && 
4c70b2a
+				  setfilecon (outname, incontext) != 0)
4c70b2a
+				{
ffc4fff
+				  if (errno != ENOTSUP && errno != EPERM)
4c70b2a
+				    pfatal ("Can't set security context "
4c70b2a
+					    "on file %s", quotearg (outname));
4c70b2a
+				}
4c70b2a
+			    }
4c70b2a
+
4c70b2a
 			  /* FIXME: There may be other attributes to preserve.  */
4c70b2a
 			}
4c70b2a
 		    }
1151a30
diff -up patch-2.6.1/src/pch.c.selinux patch-2.6.1/src/pch.c
68f90be
--- patch-2.6.1/src/pch.c.selinux	2011-02-08 11:29:34.591271499 +0000
68f90be
+++ patch-2.6.1/src/pch.c	2011-02-08 11:30:08.810621570 +0000
68f90be
@@ -307,7 +307,12 @@ there_is_another_patch (bool need_header
4c70b2a
 	    inname[t - buf - 1] = 0;
3db4109
 	    if (stat (inname, &instat) == 0)
3db4109
 	      {
3db4109
-		inerrno = 0;
638a40a
+		inerrno = getfilecon (inname, &incontext) == -1 ? errno : 0;
3db4109
+		if (inerrno == ENODATA || inerrno == ENOTSUP)
3db4109
+		  {
3db4109
+		    inerrno = 0;
3db4109
+		    incontext = NULL;
3db4109
+		  }
3db4109
 		invc = -1;
3db4109
 	      }
3db4109
 	    else
68f90be
@@ -681,7 +686,7 @@ intuit_diff_type (bool need_header)
3db4109
 			  if (cs)
3db4109
 			    {
4c70b2a
 			      if (version_get (p_name[i], cs, false, readonly,
3db4109
-					       getbuf, &st[i]))
3db4109
+					       getbuf, &st[i], NULL))
3db4109
 				stat_errno[i] = 0;
3db4109
 			      else
3db4109
 				version_controlled[i] = 0;
68f90be
@@ -746,6 +751,7 @@ intuit_diff_type (bool need_header)
638a40a
 	invc = version_controlled[i];
638a40a
 	instat = st[i];
68f90be
 	validate_target_name (inname);
638a40a
+	getfilecon (inname, &incontext);
638a40a
       }
638a40a
 
4c70b2a
     return retval;
1151a30
diff -up patch-2.6.1/src/util.c.selinux patch-2.6.1/src/util.c
1151a30
--- patch-2.6.1/src/util.c.selinux	2009-11-02 19:09:57.000000000 +0000
68f90be
+++ patch-2.6.1/src/util.c	2011-02-08 11:29:34.613271715 +0000
4c70b2a
@@ -574,7 +574,8 @@ version_controller (char const *filename
4c70b2a
    Return true if successful.  */
4c70b2a
 bool
4c70b2a
 version_get (char const *filename, char const *cs, bool exists, bool readonly,
ffc4fff
-	     char const *getbuf, struct stat *filestat)
ffc4fff
+	     char const *getbuf, struct stat *filestat,
ffc4fff
+	     security_context_t *filecontext)
ffc4fff
 {
ffc4fff
   if (patch_get < 0)
ffc4fff
     {
4c70b2a
@@ -599,6 +600,13 @@ version_get (char const *filename, char 
ffc4fff
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
ffc4fff
       if (stat (filename, filestat) != 0)
ffc4fff
 	pfatal ("%s", quotearg (filename));
ffc4fff
+      if (filecontext && getfilecon (filename, filecontext) == -1)
ffc4fff
+	{
ffc4fff
+	  if (errno == ENODATA || errno == ENOTSUP)
ffc4fff
+	    *filecontext = NULL;
ffc4fff
+	  else
ffc4fff
+	    pfatal ("%s", quotearg (filename));
ffc4fff
+	}
ffc4fff
     }
638a40a
 
ffc4fff
   return 1;
1151a30
diff -up patch-2.6.1/src/util.h.selinux patch-2.6.1/src/util.h
1151a30
--- patch-2.6.1/src/util.h.selinux	2009-11-02 19:09:57.000000000 +0000
68f90be
+++ patch-2.6.1/src/util.h	2011-02-08 11:29:34.614271726 +0000
4c70b2a
@@ -51,7 +51,7 @@ char *fetchname (char *, int, char **, t
4c70b2a
 char *savebuf (char const *, size_t);
4c70b2a
 char *savestr (char const *);
4c70b2a
 char const *version_controller (char const *, bool, struct stat const *, char **, char **);
4c70b2a
-bool version_get (char const *, char const *, bool, bool, char const *, struct stat *);
4c70b2a
+bool version_get (char const *, char const *, bool, bool, char const *, struct stat *, security_context_t *);
4c70b2a
 int create_file (char const *, int, mode_t, bool);
4c70b2a
 int systemic (char const *);
4c70b2a
 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], LINENUM);