972b7e7
diff -up patch-2.7.1/src/common.h.selinux patch-2.7.1/src/common.h
972b7e7
--- patch-2.7.1/src/common.h.selinux	2012-09-28 15:00:04.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/common.h	2015-01-20 12:26:32.914110148 +0000
274dc2d
@@ -30,6 +30,8 @@
ffc4fff
 #include <sys/types.h>
ffc4fff
 #include <time.h>
3db4109
 
ffc4fff
+#include <selinux/selinux.h>
ffc4fff
+
ffc4fff
 #include <sys/stat.h>
274dc2d
 
274dc2d
 #include <limits.h>
274dc2d
@@ -80,6 +82,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
 
972b7e7
diff -up patch-2.7.1/src/inp.c.selinux patch-2.7.1/src/inp.c
972b7e7
--- patch-2.7.1/src/inp.c.selinux	2012-09-19 02:07:31.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/inp.c	2015-01-20 12:26:32.914110148 +0000
972b7e7
@@ -138,7 +138,7 @@ get_input_file (char const *filename, ch
638a40a
     char *getbuf;
638a40a
 
274dc2d
     if (inerrno == -1)
972b7e7
-      inerrno = stat_file (filename, &instat);
972b7e7
+	inerrno = stat_file (filename, &instat, &incontext);
638a40a
 
638a40a
     /* Perhaps look for RCS or SCCS versions.  */
972b7e7
     if (S_ISREG (file_type)
972b7e7
@@ -183,7 +183,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);
972b7e7
@@ -194,6 +194,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
       }
972b7e7
     else if (! ((S_ISREG (file_type) || S_ISLNK (file_type))
972b7e7
 	        && (file_type & S_IFMT) == (instat.st_mode & S_IFMT)))
972b7e7
diff -up patch-2.7.1/src/Makefile.am.selinux patch-2.7.1/src/Makefile.am
972b7e7
--- patch-2.7.1/src/Makefile.am.selinux	2012-09-14 10:15:41.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/Makefile.am	2015-01-20 12:26:32.914110148 +0000
274dc2d
@@ -34,7 +34,7 @@ patch_SOURCES = \
274dc2d
 
274dc2d
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
274dc2d
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
274dc2d
-	      $(LIB_XATTR)
274dc2d
+	      $(LIB_XATTR) -lselinux
274dc2d
 
274dc2d
 if ENABLE_MERGE
274dc2d
   patch_SOURCES += merge.c
972b7e7
diff -up patch-2.7.1/src/Makefile.in.selinux patch-2.7.1/src/Makefile.in
972b7e7
--- patch-2.7.1/src/Makefile.in.selinux	2012-09-28 17:41:31.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/Makefile.in	2015-01-20 12:26:32.915110151 +0000
972b7e7
@@ -981,7 +981,7 @@ patch_SOURCES = bestmatch.h common.h inp
274dc2d
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib \
274dc2d
 	$(am__append_2)
274dc2d
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
274dc2d
-	      $(LIB_XATTR)
274dc2d
+	      $(LIB_XATTR) -lselinux
274dc2d
 
274dc2d
 all: all-am
274dc2d
 
972b7e7
diff -up patch-2.7.1/src/patch.c.selinux patch-2.7.1/src/patch.c
e72bf6d
--- patch-2.7.1/src/patch.c.selinux	2015-01-20 12:26:32.910110134 +0000
e72bf6d
+++ patch-2.7.1/src/patch.c	2015-01-20 12:26:32.915110151 +0000
e72bf6d
@@ -257,19 +257,19 @@ main (int argc, char **argv)
972b7e7
 	  if (! strcmp (inname, outname))
972b7e7
 	    {
972b7e7
 	      if (inerrno == -1)
972b7e7
-		inerrno = stat_file (inname, &instat);
972b7e7
+		inerrno = stat_file (inname, &instat, NULL);
972b7e7
 	      outstat = instat;
972b7e7
 	      outerrno = inerrno;
972b7e7
 	    }
972b7e7
 	  else
972b7e7
-	    outerrno = stat_file (outname, &outstat);
972b7e7
+	    outerrno = stat_file (outname, &outstat, NULL);
972b7e7
 
972b7e7
 	  if (! outerrno)
972b7e7
 	    {
972b7e7
 	      if (has_queued_output (&outstat))
972b7e7
 		{
972b7e7
 		  output_files (&outstat);
972b7e7
-		  outerrno = stat_file (outname, &outstat);
972b7e7
+		  outerrno = stat_file (outname, &outstat, NULL);
972b7e7
 		  inerrno = -1;
972b7e7
 		}
972b7e7
 	      if (! outerrno)
e72bf6d
@@ -564,7 +564,7 @@ main (int argc, char **argv)
274dc2d
 					     mode, &new_time);
274dc2d
 		      else
274dc2d
 			{
274dc2d
-			  attr |= FA_IDS | FA_MODE | FA_XATTRS;
274dc2d
+			  attr |= FA_IDS | FA_MODE | FA_XATTRS | FA_SECCONTEXT;
274dc2d
 			  set_file_attributes (TMPOUTNAME, attr, inname, &instat,
274dc2d
 					       mode, &new_time);
4c70b2a
 			}
e72bf6d
@@ -624,7 +624,7 @@ main (int argc, char **argv)
972b7e7
 			struct stat oldst;
972b7e7
 			int olderrno;
972b7e7
 
972b7e7
-			olderrno = stat_file (rej, &oldst);
972b7e7
+			olderrno = stat_file (rej, &oldst, NULL);
972b7e7
 			if (olderrno && olderrno != ENOENT)
972b7e7
 			  write_fatal ();
972b7e7
 		        if (! olderrno && lookup_file_id (&oldst) == CREATED)
e72bf6d
@@ -1751,7 +1751,7 @@ delete_file_later (const char *name, con
972b7e7
 
972b7e7
   if (! st)
972b7e7
     {
972b7e7
-      if (stat_file (name, &st_tmp) != 0)
972b7e7
+      if (stat_file (name, &st_tmp, NULL) != 0)
972b7e7
 	pfatal ("Can't get file attributes of %s %s", "file", name);
972b7e7
       st = &st_tmp;
972b7e7
     }
972b7e7
diff -up patch-2.7.1/src/pch.c.selinux patch-2.7.1/src/pch.c
e72bf6d
--- patch-2.7.1/src/pch.c.selinux	2015-01-20 12:26:32.911110138 +0000
e72bf6d
+++ patch-2.7.1/src/pch.c	2015-01-20 12:26:32.916110154 +0000
972b7e7
@@ -1,6 +1,6 @@
972b7e7
 /* reading patches */
972b7e7
 
972b7e7
-/* Copyright (C) 1986, 1987, 1988 Larry Wall
972b7e7
+/* Copyright (C) 1986, 1987, 1988, 2012 Larry Wall
972b7e7
 
972b7e7
    Copyright (C) 1990-1993, 1997-2003, 2006, 2009-2012 Free Software
972b7e7
    Foundation, Inc.
972b7e7
@@ -293,7 +293,7 @@ there_is_another_patch (bool need_header
972b7e7
 	  {
972b7e7
 	    inname = savebuf (buf, t - buf);
4c70b2a
 	    inname[t - buf - 1] = 0;
972b7e7
-	    inerrno = stat_file (inname, &instat);
972b7e7
+	    inerrno = stat_file (inname, &instat, &incontext);
972b7e7
 	    if (inerrno)
3db4109
 	      {
972b7e7
 		perror (inname);
e72bf6d
@@ -522,6 +522,7 @@ intuit_diff_type (bool need_header, mode
972b7e7
     bool extended_headers = false;
972b7e7
     enum nametype i;
972b7e7
     struct stat st[3];
972b7e7
+    security_context_t con[3];
972b7e7
     int stat_errno[3];
972b7e7
     int version_controlled[3];
972b7e7
     enum diff retval;
e72bf6d
@@ -561,6 +562,7 @@ intuit_diff_type (bool need_header, mode
972b7e7
     version_controlled[OLD] = -1;
972b7e7
     version_controlled[NEW] = -1;
972b7e7
     version_controlled[INDEX] = -1;
972b7e7
+    con[OLD] = con[NEW] = con[INDEX] = NULL;
972b7e7
     p_rfc934_nesting = 0;
972b7e7
     p_timestamp[OLD].tv_sec = p_timestamp[NEW].tv_sec = -1;
972b7e7
     p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
e72bf6d
@@ -968,7 +970,7 @@ intuit_diff_type (bool need_header, mode
972b7e7
 		}
972b7e7
 	      else
972b7e7
 		{
972b7e7
-		  stat_errno[i] = stat_file (p_name[i], &st[i]);
972b7e7
+		  stat_errno[i] = stat_file (p_name[i], &st[i], &con[i]);
972b7e7
 		  if (! stat_errno[i])
972b7e7
 		    {
972b7e7
 		      if (lookup_file_id (&st[i]) == DELETE_LATER)
e72bf6d
@@ -1007,7 +1009,7 @@ intuit_diff_type (bool need_header, mode
3db4109
 			  if (cs)
3db4109
 			    {
4c70b2a
 			      if (version_get (p_name[i], cs, false, readonly,
3db4109
-					       getbuf, &st[i]))
972b7e7
+					       getbuf, &st[i], &con[i]))
3db4109
 				stat_errno[i] = 0;
3db4109
 			      else
3db4109
 				version_controlled[i] = 0;
e72bf6d
@@ -1060,7 +1062,7 @@ intuit_diff_type (bool need_header, mode
972b7e7
       {
972b7e7
 	if (inname)
972b7e7
 	  {
972b7e7
-	    inerrno = stat_file (inname, &instat);
972b7e7
+	    inerrno = stat_file (inname, &instat, &incontext);
972b7e7
 	    if (inerrno || (instat.st_mode & S_IFMT) == file_type)
972b7e7
 	      maybe_reverse (inname, inerrno, inerrno || instat.st_size == 0);
972b7e7
 	  }
e72bf6d
@@ -1073,8 +1075,14 @@ intuit_diff_type (bool need_header, mode
274dc2d
 	inerrno = stat_errno[i];
638a40a
 	invc = version_controlled[i];
638a40a
 	instat = st[i];
972b7e7
+	incontext = con[i];
972b7e7
+	con[i] = NULL;
638a40a
       }
638a40a
 
972b7e7
+    for (i = OLD; i <= INDEX; i++)
972b7e7
+      if (con[i])
972b7e7
+	freecon (con[i]);
972b7e7
+
4c70b2a
     return retval;
972b7e7
 }
972b7e7
 
972b7e7
diff -up patch-2.7.1/src/util.c.selinux patch-2.7.1/src/util.c
e72bf6d
--- patch-2.7.1/src/util.c.selinux	2015-01-20 12:26:32.912110141 +0000
e72bf6d
+++ patch-2.7.1/src/util.c	2015-01-20 12:26:32.917110158 +0000
972b7e7
@@ -294,6 +294,19 @@ set_file_attributes (char const *to, enu
274dc2d
 		S_ISLNK (mode) ? "symbolic link" : "file",
274dc2d
 		quotearg (to));
274dc2d
     }
274dc2d
+  if (attr & FA_SECCONTEXT)
274dc2d
+    {
274dc2d
+      security_context_t outcontext;
274dc2d
+      if (incontext && getfilecon (to, &outcontext) != -1 &&
274dc2d
+	  outcontext &&
274dc2d
+	  strcmp (outcontext, incontext) &&
274dc2d
+	  setfilecon (to, incontext) != 0)
274dc2d
+	{
274dc2d
+	  if (errno != ENOTSUP && errno != EPERM)
274dc2d
+	    pfatal ("Can't set security context on file %s",
274dc2d
+		    quotearg (to));
274dc2d
+	}
274dc2d
+    }
274dc2d
 }
274dc2d
 
274dc2d
 static void
972b7e7
@@ -440,7 +453,7 @@ move_file (char const *from, bool *from_
972b7e7
   struct stat to_st;
972b7e7
   int to_errno;
972b7e7
 
972b7e7
-  to_errno = stat_file (to, &to_st);
972b7e7
+  to_errno = stat_file (to, &to_st, NULL);
972b7e7
   if (backup)
972b7e7
     create_backup (to, to_errno ? NULL : &to_st, false);
972b7e7
   if (! to_errno)
e72bf6d
@@ -817,7 +830,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
     {
e72bf6d
@@ -842,6 +856,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;
e72bf6d
@@ -1660,10 +1681,26 @@ make_tempfile (char const **name, char l
972b7e7
     }
972b7e7
 }
972b7e7
 
972b7e7
-int stat_file (char const *filename, struct stat *st)
972b7e7
+int stat_file (char const *filename, struct stat *st, security_context_t *con)
972b7e7
 {
972b7e7
   int (*xstat)(char const *, struct stat *) =
972b7e7
     follow_symlinks ? stat : lstat;
972b7e7
+  int (*xgetfilecon)(char const *, security_context_t *) =
972b7e7
+    follow_symlinks ? getfilecon : lgetfilecon;
972b7e7
+
972b7e7
+  if (xstat (filename, st) == 0)
972b7e7
+    {
972b7e7
+      if (con)
972b7e7
+	{
972b7e7
+	  if (xgetfilecon (filename, con) != -1 ||
972b7e7
+	      errno == ENODATA || errno == ENOTSUP)
972b7e7
+	    return 0;
972b7e7
+
972b7e7
+	  *con = NULL;
972b7e7
+	}
972b7e7
+      else
972b7e7
+	return 0;
972b7e7
+    }
972b7e7
 
972b7e7
-  return xstat (filename, st) == 0 ? 0 : errno;
972b7e7
+  return errno;
972b7e7
 }
972b7e7
diff -up patch-2.7.1/src/util.h.selinux patch-2.7.1/src/util.h
972b7e7
--- patch-2.7.1/src/util.h.selinux	2012-09-21 21:21:16.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/util.h	2015-01-20 12:26:32.917110158 +0000
274dc2d
@@ -45,7 +45,7 @@ char *parse_name (char const *, int, cha
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 *);
274dc2d
 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], lin);
972b7e7
@@ -68,13 +68,14 @@ void insert_file_id (struct stat const *
972b7e7
 enum file_id_type lookup_file_id (struct stat const *);
972b7e7
 void set_queued_output (struct stat const *, bool);
972b7e7
 bool has_queued_output (struct stat const *);
972b7e7
-int stat_file (char const *, struct stat *);
972b7e7
+int stat_file (char const *, struct stat *, security_context_t *);
972b7e7
 
972b7e7
 enum file_attributes {
274dc2d
   FA_TIMES = 1,
274dc2d
   FA_IDS = 2,
274dc2d
   FA_MODE = 4,
274dc2d
-  FA_XATTRS = 8
274dc2d
+  FA_XATTRS = 8,
274dc2d
+  FA_SECCONTEXT = 16
274dc2d
 };
274dc2d
 
274dc2d
 void set_file_attributes (char const *, enum file_attributes, char const *,