de36f7
diff -up patch-2.7.1/src/common.h.selinux patch-2.7.1/src/common.h
de36f7
--- patch-2.7.1/src/common.h.selinux	2012-09-28 16:00:04.000000000 +0200
de36f7
+++ patch-2.7.1/src/common.h	2018-11-26 15:41:05.747151852 +0100
de36f7
@@ -30,6 +30,8 @@
de36f7
 #include <sys/types.h>
de36f7
 #include <time.h>
de36f7
 
de36f7
+#include <selinux/selinux.h>
de36f7
+
de36f7
 #include <sys/stat.h>
de36f7
 
de36f7
 #include <limits.h>
de36f7
@@ -80,6 +82,7 @@ XTERN char *outfile;
de36f7
 XTERN int inerrno;
de36f7
 XTERN int invc;
de36f7
 XTERN struct stat instat;
de36f7
+XTERN security_context_t incontext;
de36f7
 XTERN bool dry_run;
de36f7
 XTERN bool posixly_correct;
de36f7
 
de36f7
diff -up patch-2.7.1/src/inp.c.selinux patch-2.7.1/src/inp.c
de36f7
--- patch-2.7.1/src/inp.c.selinux	2012-09-19 03:07:31.000000000 +0200
de36f7
+++ patch-2.7.1/src/inp.c	2018-11-26 15:41:05.747151852 +0100
de36f7
@@ -138,7 +138,7 @@ get_input_file (char const *filename, ch
de36f7
     char *getbuf;
de36f7
 
de36f7
     if (inerrno == -1)
de36f7
-      inerrno = stat_file (filename, &instat);
de36f7
+	inerrno = stat_file (filename, &instat, &incontext);
de36f7
 
de36f7
     /* Perhaps look for RCS or SCCS versions.  */
de36f7
     if (S_ISREG (file_type)
de36f7
@@ -183,7 +183,7 @@ get_input_file (char const *filename, ch
de36f7
 	    }
de36f7
 
de36f7
 	    if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
de36f7
-				   &instat))
de36f7
+				   &instat, &incontext))
de36f7
 	      inerrno = 0;
de36f7
 
de36f7
 	    free (getbuf);
de36f7
@@ -194,6 +194,7 @@ get_input_file (char const *filename, ch
de36f7
       {
de36f7
 	instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
de36f7
 	instat.st_size = 0;
de36f7
+	incontext = NULL;
de36f7
       }
de36f7
     else if (! ((S_ISREG (file_type) || S_ISLNK (file_type))
de36f7
 	        && (file_type & S_IFMT) == (instat.st_mode & S_IFMT)))
de36f7
diff -up patch-2.7.1/src/Makefile.am.selinux patch-2.7.1/src/Makefile.am
de36f7
--- patch-2.7.1/src/Makefile.am.selinux	2012-09-14 11:15:41.000000000 +0200
de36f7
+++ patch-2.7.1/src/Makefile.am	2018-11-26 15:41:05.747151852 +0100
de36f7
@@ -34,7 +34,7 @@ patch_SOURCES = \
de36f7
 
de36f7
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
de36f7
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
de36f7
-	      $(LIB_XATTR)
de36f7
+	      $(LIB_XATTR) -lselinux
de36f7
 
de36f7
 if ENABLE_MERGE
de36f7
   patch_SOURCES += merge.c
de36f7
diff -up patch-2.7.1/src/Makefile.in.selinux patch-2.7.1/src/Makefile.in
de36f7
--- patch-2.7.1/src/Makefile.in.selinux	2012-09-28 18:41:31.000000000 +0200
de36f7
+++ patch-2.7.1/src/Makefile.in	2018-11-26 15:41:05.748151855 +0100
de36f7
@@ -981,7 +981,7 @@ patch_SOURCES = bestmatch.h common.h inp
de36f7
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib \
de36f7
 	$(am__append_2)
de36f7
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
de36f7
-	      $(LIB_XATTR)
de36f7
+	      $(LIB_XATTR) -lselinux
de36f7
 
de36f7
 all: all-am
de36f7
 
de36f7
diff -up patch-2.7.1/src/patch.c.selinux patch-2.7.1/src/patch.c
de36f7
--- patch-2.7.1/src/patch.c.selinux	2018-11-26 15:41:05.746151849 +0100
de36f7
+++ patch-2.7.1/src/patch.c	2018-11-26 15:42:11.784367497 +0100
de36f7
@@ -257,19 +257,19 @@ main (int argc, char **argv)
de36f7
 	  if (! strcmp (inname, outname))
de36f7
 	    {
de36f7
 	      if (inerrno == -1)
de36f7
-		inerrno = stat_file (inname, &instat);
de36f7
+		inerrno = stat_file (inname, &instat, NULL);
de36f7
 	      outstat = instat;
de36f7
 	      outerrno = inerrno;
de36f7
 	    }
de36f7
 	  else
de36f7
-	    outerrno = stat_file (outname, &outstat);
de36f7
+	    outerrno = stat_file (outname, &outstat, NULL);
de36f7
 
de36f7
 	  if (! outerrno)
de36f7
 	    {
de36f7
 	      if (has_queued_output (&outstat))
de36f7
 		{
de36f7
 		  output_files (&outstat);
de36f7
-		  outerrno = stat_file (outname, &outstat);
de36f7
+		  outerrno = stat_file (outname, &outstat, NULL);
de36f7
 		  inerrno = -1;
de36f7
 		}
de36f7
 	      if (! outerrno)
de36f7
@@ -567,7 +567,7 @@ main (int argc, char **argv)
de36f7
 			}
de36f7
 		      else
de36f7
 			{
de36f7
-			  attr |= FA_IDS | FA_MODE | FA_XATTRS;
de36f7
+			  attr |= FA_IDS | FA_MODE | FA_XATTRS | FA_SECCONTEXT;
de36f7
 			  set_file_attributes (TMPOUTNAME, attr, inname, &instat,
de36f7
 					       mode, &new_time);
de36f7
 			}
de36f7
@@ -627,7 +627,7 @@ main (int argc, char **argv)
de36f7
 			struct stat oldst;
de36f7
 			int olderrno;
de36f7
 
de36f7
-			olderrno = stat_file (rej, &oldst);
de36f7
+			olderrno = stat_file (rej, &oldst, NULL);
de36f7
 			if (olderrno && olderrno != ENOENT)
de36f7
 			  write_fatal ();
de36f7
 		        if (! olderrno && lookup_file_id (&oldst) == CREATED)
de36f7
@@ -1754,7 +1754,7 @@ delete_file_later (const char *name, con
de36f7
 
de36f7
   if (! st)
de36f7
     {
de36f7
-      if (stat_file (name, &st_tmp) != 0)
de36f7
+      if (stat_file (name, &st_tmp, NULL) != 0)
de36f7
 	pfatal ("Can't get file attributes of %s %s", "file", name);
de36f7
       st = &st_tmp;
de36f7
     }
de36f7
diff -up patch-2.7.1/src/pch.c.selinux patch-2.7.1/src/pch.c
de36f7
--- patch-2.7.1/src/pch.c.selinux	2018-11-26 15:41:05.744151842 +0100
de36f7
+++ patch-2.7.1/src/pch.c	2018-11-26 15:41:05.749151858 +0100
de36f7
@@ -1,6 +1,6 @@
de36f7
 /* reading patches */
de36f7
 
de36f7
-/* Copyright (C) 1986, 1987, 1988 Larry Wall
de36f7
+/* Copyright (C) 1986, 1987, 1988, 2012 Larry Wall
de36f7
 
de36f7
    Copyright (C) 1990-1993, 1997-2003, 2006, 2009-2012 Free Software
de36f7
    Foundation, Inc.
de36f7
@@ -294,7 +294,7 @@ there_is_another_patch (bool need_header
de36f7
 	  {
de36f7
 	    inname = savebuf (buf, t - buf);
de36f7
 	    inname[t - buf - 1] = 0;
de36f7
-	    inerrno = stat_file (inname, &instat);
de36f7
+	    inerrno = stat_file (inname, &instat, &incontext);
de36f7
 	    if (inerrno)
de36f7
 	      {
de36f7
 		perror (inname);
de36f7
@@ -469,6 +469,7 @@ intuit_diff_type (bool need_header, mode
de36f7
     bool extended_headers = false;
de36f7
     enum nametype i;
de36f7
     struct stat st[3];
de36f7
+    security_context_t con[3];
de36f7
     int stat_errno[3];
de36f7
     int version_controlled[3];
de36f7
     enum diff retval;
de36f7
@@ -508,6 +509,7 @@ intuit_diff_type (bool need_header, mode
de36f7
     version_controlled[OLD] = -1;
de36f7
     version_controlled[NEW] = -1;
de36f7
     version_controlled[INDEX] = -1;
de36f7
+    con[OLD] = con[NEW] = con[INDEX] = NULL;
de36f7
     p_rfc934_nesting = 0;
de36f7
     p_timestamp[OLD].tv_sec = p_timestamp[NEW].tv_sec = -1;
de36f7
     p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
de36f7
@@ -915,7 +917,7 @@ intuit_diff_type (bool need_header, mode
de36f7
 		}
de36f7
 	      else
de36f7
 		{
de36f7
-		  stat_errno[i] = stat_file (p_name[i], &st[i]);
de36f7
+		  stat_errno[i] = stat_file (p_name[i], &st[i], &con[i]);
de36f7
 		  if (! stat_errno[i])
de36f7
 		    {
de36f7
 		      if (lookup_file_id (&st[i]) == DELETE_LATER)
de36f7
@@ -954,7 +956,7 @@ intuit_diff_type (bool need_header, mode
de36f7
 			  if (cs)
de36f7
 			    {
de36f7
 			      if (version_get (p_name[i], cs, false, readonly,
de36f7
-					       getbuf, &st[i]))
de36f7
+					       getbuf, &st[i], &con[i]))
de36f7
 				stat_errno[i] = 0;
de36f7
 			      else
de36f7
 				version_controlled[i] = 0;
de36f7
@@ -1007,7 +1009,7 @@ intuit_diff_type (bool need_header, mode
de36f7
       {
de36f7
 	if (inname)
de36f7
 	  {
de36f7
-	    inerrno = stat_file (inname, &instat);
de36f7
+	    inerrno = stat_file (inname, &instat, &incontext);
de36f7
 	    if (inerrno || (instat.st_mode & S_IFMT) == file_type)
de36f7
 	      maybe_reverse (inname, inerrno, inerrno || instat.st_size == 0);
de36f7
 	  }
de36f7
@@ -1020,8 +1022,14 @@ intuit_diff_type (bool need_header, mode
de36f7
 	inerrno = stat_errno[i];
de36f7
 	invc = version_controlled[i];
de36f7
 	instat = st[i];
de36f7
+	incontext = con[i];
de36f7
+	con[i] = NULL;
de36f7
       }
de36f7
 
de36f7
+    for (i = OLD; i <= INDEX; i++)
de36f7
+      if (con[i])
de36f7
+	freecon (con[i]);
de36f7
+
de36f7
     return retval;
de36f7
 }
de36f7
 
de36f7
diff -up patch-2.7.1/src/util.c.selinux patch-2.7.1/src/util.c
de36f7
--- patch-2.7.1/src/util.c.selinux	2012-09-22 22:09:10.000000000 +0200
de36f7
+++ patch-2.7.1/src/util.c	2018-11-26 15:41:05.749151858 +0100
de36f7
@@ -294,6 +294,19 @@ set_file_attributes (char const *to, enu
de36f7
 		S_ISLNK (mode) ? "symbolic link" : "file",
de36f7
 		quotearg (to));
de36f7
     }
de36f7
+  if (attr & FA_SECCONTEXT)
de36f7
+    {
de36f7
+      security_context_t outcontext;
de36f7
+      if (incontext && getfilecon (to, &outcontext) != -1 &&
de36f7
+	  outcontext &&
de36f7
+	  strcmp (outcontext, incontext) &&
de36f7
+	  setfilecon (to, incontext) != 0)
de36f7
+	{
de36f7
+	  if (errno != ENOTSUP && errno != EPERM)
de36f7
+	    pfatal ("Can't set security context on file %s",
de36f7
+		    quotearg (to));
de36f7
+	}
de36f7
+    }
de36f7
 }
de36f7
 
de36f7
 static void
de36f7
@@ -440,7 +453,7 @@ move_file (char const *from, bool *from_
de36f7
   struct stat to_st;
de36f7
   int to_errno;
de36f7
 
de36f7
-  to_errno = stat_file (to, &to_st);
de36f7
+  to_errno = stat_file (to, &to_st, NULL);
de36f7
   if (backup)
de36f7
     create_backup (to, to_errno ? NULL : &to_st, false);
de36f7
   if (! to_errno)
de36f7
@@ -810,7 +823,8 @@ version_controller (char const *filename
de36f7
    Return true if successful.  */
de36f7
 bool
de36f7
 version_get (char const *filename, char const *cs, bool exists, bool readonly,
de36f7
-	     char const *getbuf, struct stat *filestat)
de36f7
+	     char const *getbuf, struct stat *filestat,
de36f7
+	     security_context_t *filecontext)
de36f7
 {
de36f7
   if (patch_get < 0)
de36f7
     {
de36f7
@@ -835,6 +849,13 @@ version_get (char const *filename, char
de36f7
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
de36f7
       if (stat (filename, filestat) != 0)
de36f7
 	pfatal ("%s", quotearg (filename));
de36f7
+      if (filecontext && getfilecon (filename, filecontext) == -1)
de36f7
+	{
de36f7
+	  if (errno == ENODATA || errno == ENOTSUP)
de36f7
+	    *filecontext = NULL;
de36f7
+	  else
de36f7
+	    pfatal ("%s", quotearg (filename));
de36f7
+	}
de36f7
     }
de36f7
 
de36f7
   return 1;
de36f7
@@ -1653,10 +1674,26 @@ make_tempfile (char const **name, char l
de36f7
     }
de36f7
 }
de36f7
 
de36f7
-int stat_file (char const *filename, struct stat *st)
de36f7
+int stat_file (char const *filename, struct stat *st, security_context_t *con)
de36f7
 {
de36f7
   int (*xstat)(char const *, struct stat *) =
de36f7
     follow_symlinks ? stat : lstat;
de36f7
+  int (*xgetfilecon)(char const *, security_context_t *) =
de36f7
+    follow_symlinks ? getfilecon : lgetfilecon;
de36f7
+
de36f7
+  if (xstat (filename, st) == 0)
de36f7
+    {
de36f7
+      if (con)
de36f7
+	{
de36f7
+	  if (xgetfilecon (filename, con) != -1 ||
de36f7
+	      errno == ENODATA || errno == ENOTSUP)
de36f7
+	    return 0;
de36f7
+
de36f7
+	  *con = NULL;
de36f7
+	}
de36f7
+      else
de36f7
+	return 0;
de36f7
+    }
de36f7
 
de36f7
-  return xstat (filename, st) == 0 ? 0 : errno;
de36f7
+  return errno;
de36f7
 }
de36f7
diff -up patch-2.7.1/src/util.h.selinux patch-2.7.1/src/util.h
de36f7
--- patch-2.7.1/src/util.h.selinux	2012-09-21 22:21:16.000000000 +0200
de36f7
+++ patch-2.7.1/src/util.h	2018-11-26 15:41:05.749151858 +0100
de36f7
@@ -45,7 +45,7 @@ char *parse_name (char const *, int, cha
de36f7
 char *savebuf (char const *, size_t);
de36f7
 char *savestr (char const *);
de36f7
 char const *version_controller (char const *, bool, struct stat const *, char **, char **);
de36f7
-bool version_get (char const *, char const *, bool, bool, char const *, struct stat *);
de36f7
+bool version_get (char const *, char const *, bool, bool, char const *, struct stat *, security_context_t *);
de36f7
 int create_file (char const *, int, mode_t, bool);
de36f7
 int systemic (char const *);
de36f7
 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], lin);
de36f7
@@ -68,13 +68,14 @@ void insert_file_id (struct stat const *
de36f7
 enum file_id_type lookup_file_id (struct stat const *);
de36f7
 void set_queued_output (struct stat const *, bool);
de36f7
 bool has_queued_output (struct stat const *);
de36f7
-int stat_file (char const *, struct stat *);
de36f7
+int stat_file (char const *, struct stat *, security_context_t *);
de36f7
 
de36f7
 enum file_attributes {
de36f7
   FA_TIMES = 1,
de36f7
   FA_IDS = 2,
de36f7
   FA_MODE = 4,
de36f7
-  FA_XATTRS = 8
de36f7
+  FA_XATTRS = 8,
de36f7
+  FA_SECCONTEXT = 16
de36f7
 };
de36f7
 
de36f7
 void set_file_attributes (char const *, enum file_attributes, char const *,