1bbc12
diff -up patch-2.7.6/src/common.h.selinux patch-2.7.6/src/common.h
1bbc12
--- patch-2.7.6/src/common.h.selinux	2018-02-03 12:41:49.000000000 +0000
1bbc12
+++ patch-2.7.6/src/common.h	2018-02-12 12:29:44.415225377 +0000
1bbc12
@@ -30,6 +30,8 @@
1bbc12
 #include <sys/types.h>
1bbc12
 #include <time.h>
1bbc12
 
1bbc12
+#include <selinux/selinux.h>
1bbc12
+
1bbc12
 #include <sys/stat.h>
1bbc12
 
1bbc12
 #include <limits.h>
1bbc12
@@ -84,6 +86,7 @@ XTERN char *outfile;
1bbc12
 XTERN int inerrno;
1bbc12
 XTERN int invc;
1bbc12
 XTERN struct stat instat;
1bbc12
+XTERN security_context_t incontext;
1bbc12
 XTERN bool dry_run;
1bbc12
 XTERN bool posixly_correct;
1bbc12
 
1bbc12
diff -up patch-2.7.6/src/inp.c.selinux patch-2.7.6/src/inp.c
1bbc12
--- patch-2.7.6/src/inp.c.selinux	2017-09-04 12:34:16.000000000 +0100
1bbc12
+++ patch-2.7.6/src/inp.c	2018-02-12 12:29:44.415225377 +0000
1bbc12
@@ -145,7 +145,7 @@ get_input_file (char const *filename, ch
1bbc12
     char *getbuf;
1bbc12
 
1bbc12
     if (inerrno == -1)
1bbc12
-      inerrno = stat_file (filename, &instat);
1bbc12
+	inerrno = stat_file (filename, &instat, &incontext);
1bbc12
 
1bbc12
     /* Perhaps look for RCS or SCCS versions.  */
1bbc12
     if (S_ISREG (file_type)
1bbc12
@@ -190,7 +190,7 @@ get_input_file (char const *filename, ch
1bbc12
 	    }
1bbc12
 
1bbc12
 	    if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
1bbc12
-				   &instat))
1bbc12
+				   &instat, &incontext))
1bbc12
 	      inerrno = 0;
1bbc12
 
1bbc12
 	    free (getbuf);
1bbc12
@@ -201,6 +201,7 @@ get_input_file (char const *filename, ch
1bbc12
       {
1bbc12
 	instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
1bbc12
 	instat.st_size = 0;
1bbc12
+	incontext = NULL;
1bbc12
       }
1bbc12
     else if (! ((S_ISREG (file_type) || S_ISLNK (file_type))
1bbc12
 	        && (file_type & S_IFMT) == (instat.st_mode & S_IFMT)))
1bbc12
diff -up patch-2.7.6/src/Makefile.am.selinux patch-2.7.6/src/Makefile.am
1bbc12
--- patch-2.7.6/src/Makefile.am.selinux	2017-09-04 12:34:16.000000000 +0100
1bbc12
+++ patch-2.7.6/src/Makefile.am	2018-02-12 12:29:44.415225377 +0000
1bbc12
@@ -37,7 +37,7 @@ patch_SOURCES = \
1bbc12
 
1bbc12
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
1bbc12
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
1bbc12
-	      $(LIB_XATTR) $(LIB_EACCESS)
1bbc12
+	      $(LIB_XATTR) $(LIB_EACCESS) -lselinux
1bbc12
 
1bbc12
 if ENABLE_MERGE
1bbc12
   patch_SOURCES += merge.c
1bbc12
diff -up patch-2.7.6/src/Makefile.in.selinux patch-2.7.6/src/Makefile.in
1bbc12
--- patch-2.7.6/src/Makefile.in.selinux	2018-02-03 13:33:56.000000000 +0000
1bbc12
+++ patch-2.7.6/src/Makefile.in	2018-02-12 12:29:44.415225377 +0000
1bbc12
@@ -1147,7 +1147,7 @@ patch_SOURCES = bestmatch.h common.h inp
1bbc12
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib \
1bbc12
 	$(am__append_2)
1bbc12
 patch_LDADD = $(LDADD) $(top_builddir)/lib/libpatch.a $(LIB_CLOCK_GETTIME) \
1bbc12
-	      $(LIB_XATTR) $(LIB_EACCESS)
1bbc12
+	      $(LIB_XATTR) $(LIB_EACCESS) -lselinux
1bbc12
 
1bbc12
 all: all-am
1bbc12
 
1bbc12
diff -up patch-2.7.6/src/patch.c.selinux patch-2.7.6/src/patch.c
1bbc12
--- patch-2.7.6/src/patch.c.selinux	2018-02-03 12:41:49.000000000 +0000
1bbc12
+++ patch-2.7.6/src/patch.c	2018-02-12 12:30:27.315164138 +0000
1bbc12
@@ -269,19 +269,19 @@ main (int argc, char **argv)
1bbc12
 	  if (! strcmp (inname, outname))
1bbc12
 	    {
1bbc12
 	      if (inerrno == -1)
1bbc12
-		inerrno = stat_file (inname, &instat);
1bbc12
+		inerrno = stat_file (inname, &instat, NULL);
1bbc12
 	      outstat = instat;
1bbc12
 	      outerrno = inerrno;
1bbc12
 	    }
1bbc12
 	  else
1bbc12
-	    outerrno = stat_file (outname, &outstat);
1bbc12
+	    outerrno = stat_file (outname, &outstat, NULL);
1bbc12
 
1bbc12
 	  if (! outerrno)
1bbc12
 	    {
1bbc12
 	      if (has_queued_output (&outstat))
1bbc12
 		{
1bbc12
 		  output_files (&outstat);
1bbc12
-		  outerrno = stat_file (outname, &outstat);
1bbc12
+		  outerrno = stat_file (outname, &outstat, NULL);
1bbc12
 		  inerrno = -1;
1bbc12
 		}
1bbc12
 	      if (! outerrno)
1bbc12
@@ -598,7 +598,7 @@ main (int argc, char **argv)
1bbc12
 			}
1bbc12
 		      else
1bbc12
 			{
1bbc12
-			  attr |= FA_IDS | FA_MODE | FA_XATTRS;
1bbc12
+			  attr |= FA_IDS | FA_MODE | FA_XATTRS | FA_SECCONTEXT;
1bbc12
 			  set_file_attributes (TMPOUTNAME, attr, inname, &instat,
1bbc12
 					       mode, &new_time);
1bbc12
 			}
1bbc12
@@ -658,7 +658,7 @@ main (int argc, char **argv)
1bbc12
 			struct stat oldst;
1bbc12
 			int olderrno;
1bbc12
 
1bbc12
-			olderrno = stat_file (rej, &oldst);
1bbc12
+			olderrno = stat_file (rej, &oldst, NULL);
1bbc12
 			if (olderrno && olderrno != ENOENT)
1bbc12
 			  write_fatal ();
1bbc12
 		        if (! olderrno && lookup_file_id (&oldst) == CREATED)
1bbc12
@@ -1790,7 +1790,7 @@ delete_file_later (const char *name, con
1bbc12
 
1bbc12
   if (! st)
1bbc12
     {
1bbc12
-      if (stat_file (name, &st_tmp) != 0)
1bbc12
+      if (stat_file (name, &st_tmp, NULL) != 0)
1bbc12
 	pfatal ("Can't get file attributes of %s %s", "file", name);
1bbc12
       st = &st_tmp;
1bbc12
     }
1bbc12
diff -up patch-2.7.6/src/pch.c.selinux patch-2.7.6/src/pch.c
1bbc12
--- patch-2.7.6/src/pch.c.selinux	2018-02-03 12:41:49.000000000 +0000
1bbc12
+++ patch-2.7.6/src/pch.c	2018-02-12 12:29:44.416225375 +0000
1bbc12
@@ -1,6 +1,6 @@
1bbc12
 /* reading patches */
1bbc12
 
1bbc12
-/* Copyright (C) 1986, 1987, 1988 Larry Wall
1bbc12
+/* Copyright (C) 1986, 1987, 1988, 2012 Larry Wall
1bbc12
 
1bbc12
    Copyright (C) 1990-1993, 1997-2003, 2006, 2009-2012 Free Software
1bbc12
    Foundation, Inc.
1bbc12
@@ -296,7 +296,7 @@ there_is_another_patch (bool need_header
1bbc12
 	if (t > buf + 1 && *(t - 1) == '\n')
1bbc12
 	  {
1bbc12
 	    inname = xmemdup0 (buf, t - buf - 1);
1bbc12
-	    inerrno = stat_file (inname, &instat);
1bbc12
+	    inerrno = stat_file (inname, &instat, &incontext);
1bbc12
 	    if (inerrno)
1bbc12
 	      {
1bbc12
 		perror (inname);
1bbc12
@@ -433,6 +433,7 @@ intuit_diff_type (bool need_header, mode
1bbc12
     bool extended_headers = false;
1bbc12
     enum nametype i;
1bbc12
     struct stat st[3];
1bbc12
+    security_context_t con[3];
1bbc12
     int stat_errno[3];
1bbc12
     int version_controlled[3];
1bbc12
     enum diff retval;
1bbc12
@@ -473,6 +474,7 @@ intuit_diff_type (bool need_header, mode
1bbc12
     version_controlled[OLD] = -1;
1bbc12
     version_controlled[NEW] = -1;
1bbc12
     version_controlled[INDEX] = -1;
1bbc12
+    con[OLD] = con[NEW] = con[INDEX] = NULL;
1bbc12
     p_rfc934_nesting = 0;
1bbc12
     p_timestamp[OLD].tv_sec = p_timestamp[NEW].tv_sec = -1;
1bbc12
     p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
1bbc12
@@ -883,7 +885,7 @@ intuit_diff_type (bool need_header, mode
1bbc12
 		}
1bbc12
 	      else
1bbc12
 		{
1bbc12
-		  stat_errno[i] = stat_file (p_name[i], &st[i]);
1bbc12
+		  stat_errno[i] = stat_file (p_name[i], &st[i], &con[i]);
1bbc12
 		  if (! stat_errno[i])
1bbc12
 		    {
1bbc12
 		      if (lookup_file_id (&st[i]) == DELETE_LATER)
1bbc12
@@ -922,7 +924,7 @@ intuit_diff_type (bool need_header, mode
1bbc12
 			  if (cs)
1bbc12
 			    {
1bbc12
 			      if (version_get (p_name[i], cs, false, readonly,
1bbc12
-					       getbuf, &st[i]))
1bbc12
+					       getbuf, &st[i], &con[i]))
1bbc12
 				stat_errno[i] = 0;
1bbc12
 			      else
1bbc12
 				version_controlled[i] = 0;
1bbc12
@@ -985,7 +987,7 @@ intuit_diff_type (bool need_header, mode
1bbc12
       {
1bbc12
 	if (inname)
1bbc12
 	  {
1bbc12
-	    inerrno = stat_file (inname, &instat);
1bbc12
+	    inerrno = stat_file (inname, &instat, &incontext);
1bbc12
 	    if (inerrno || (instat.st_mode & S_IFMT) == file_type)
1bbc12
 	      maybe_reverse (inname, inerrno, inerrno || instat.st_size == 0);
1bbc12
 	  }
1bbc12
@@ -998,8 +1000,14 @@ intuit_diff_type (bool need_header, mode
1bbc12
 	inerrno = stat_errno[i];
1bbc12
 	invc = version_controlled[i];
1bbc12
 	instat = st[i];
1bbc12
+	incontext = con[i];
1bbc12
+	con[i] = NULL;
1bbc12
       }
1bbc12
 
1bbc12
+    for (i = OLD; i <= INDEX; i++)
1bbc12
+      if (con[i])
1bbc12
+	freecon (con[i]);
1bbc12
+
1bbc12
     return retval;
1bbc12
 }
1bbc12
 
1bbc12
diff -up patch-2.7.6/src/util.c.selinux patch-2.7.6/src/util.c
1bbc12
--- patch-2.7.6/src/util.c.selinux	2018-02-03 12:41:49.000000000 +0000
1bbc12
+++ patch-2.7.6/src/util.c	2018-02-12 12:29:44.417225374 +0000
1bbc12
@@ -300,6 +300,23 @@ set_file_attributes (char const *to, enu
1bbc12
 		S_ISLNK (mode) ? "symbolic link" : "file",
1bbc12
 		quotearg (to));
1bbc12
     }
1bbc12
+  if (attr & FA_SECCONTEXT)
1bbc12
+    {
1bbc12
+      security_context_t outcontext;
1bbc12
+      if (incontext && getfilecon (to, &outcontext) != -1 && outcontext)
1bbc12
+	{
1bbc12
+	  if (strcmp (outcontext, incontext) &&
1bbc12
+	      setfilecon (to, incontext) != 0)
1bbc12
+	    {
1bbc12
+	      freecon (outcontext);
1bbc12
+	      if (errno != ENOTSUP && errno != EPERM)
1bbc12
+		pfatal ("Can't set security context on file %s",
1bbc12
+			quotearg (to));
1bbc12
+	    }
1bbc12
+	  else
1bbc12
+	    freecon (outcontext);
1bbc12
+	}
1bbc12
+    }
1bbc12
 }
1bbc12
 
1bbc12
 static void
1bbc12
@@ -446,7 +463,7 @@ move_file (char const *from, bool *from_
1bbc12
   struct stat to_st;
1bbc12
   int to_errno;
1bbc12
 
1bbc12
-  to_errno = stat_file (to, &to_st);
1bbc12
+  to_errno = stat_file (to, &to_st, NULL);
1bbc12
   if (backup)
1bbc12
     create_backup (to, to_errno ? NULL : &to_st, false);
1bbc12
   if (! to_errno)
1bbc12
@@ -818,7 +835,8 @@ version_controller (char const *filename
1bbc12
    Return true if successful.  */
1bbc12
 bool
1bbc12
 version_get (char const *filename, char const *cs, bool exists, bool readonly,
1bbc12
-	     char const *getbuf, struct stat *filestat)
1bbc12
+	     char const *getbuf, struct stat *filestat,
1bbc12
+	     security_context_t *filecontext)
1bbc12
 {
1bbc12
   if (patch_get < 0)
1bbc12
     {
1bbc12
@@ -843,6 +861,13 @@ version_get (char const *filename, char
1bbc12
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
1bbc12
       if (safe_stat (filename, filestat) != 0)
1bbc12
 	pfatal ("%s", quotearg (filename));
1bbc12
+      if (filecontext && getfilecon (filename, filecontext) == -1)
1bbc12
+	{
1bbc12
+	  if (errno == ENODATA || errno == ENOTSUP)
1bbc12
+	    *filecontext = NULL;
1bbc12
+	  else
1bbc12
+	    pfatal ("%s", quotearg (filename));
1bbc12
+	}
1bbc12
     }
1bbc12
 
1bbc12
   return 1;
1bbc12
@@ -1670,12 +1695,28 @@ make_tempfile (char const **name, char l
1bbc12
   return fd;
1bbc12
 }
1bbc12
 
1bbc12
-int stat_file (char const *filename, struct stat *st)
1bbc12
+int stat_file (char const *filename, struct stat *st, security_context_t *con)
1bbc12
 {
1bbc12
   int (*xstat)(char const *, struct stat *) =
1bbc12
     follow_symlinks ? safe_stat : safe_lstat;
1bbc12
+  int (*xgetfilecon)(char const *, security_context_t *) =
1bbc12
+    follow_symlinks ? getfilecon : lgetfilecon;
1bbc12
+
1bbc12
+  if (xstat (filename, st) == 0)
1bbc12
+    {
1bbc12
+      if (con)
1bbc12
+	{
1bbc12
+	  if (xgetfilecon (filename, con) != -1 ||
1bbc12
+	      errno == ENODATA || errno == ENOTSUP)
1bbc12
+	    return 0;
1bbc12
 
1bbc12
-  return xstat (filename, st) == 0 ? 0 : errno;
1bbc12
+	  *con = NULL;
1bbc12
+	}
1bbc12
+      else
1bbc12
+	return 0;
1bbc12
+    }
1bbc12
+ 
1bbc12
+  return errno;
1bbc12
 }
1bbc12
 
1bbc12
 /* Check if a filename is relative and free of ".." components.
1bbc12
diff -up patch-2.7.6/src/util.h.selinux patch-2.7.6/src/util.h
1bbc12
--- patch-2.7.6/src/util.h.selinux	2018-02-03 12:41:49.000000000 +0000
1bbc12
+++ patch-2.7.6/src/util.h	2018-02-12 12:30:08.533190949 +0000
1bbc12
@@ -44,7 +44,7 @@ char *parse_name (char const *, int, cha
1bbc12
 char *savebuf (char const *, size_t);
1bbc12
 char *savestr (char const *);
1bbc12
 char const *version_controller (char const *, bool, struct stat const *, char **, char **);
1bbc12
-bool version_get (char const *, char const *, bool, bool, char const *, struct stat *);
1bbc12
+bool version_get (char const *, char const *, bool, bool, char const *, struct stat *, security_context_t *);
1bbc12
 int create_file (char const *, int, mode_t, bool);
1bbc12
 int systemic (char const *);
1bbc12
 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], lin);
1bbc12
@@ -67,7 +67,7 @@ void insert_file_id (struct stat const *
1bbc12
 enum file_id_type lookup_file_id (struct stat const *);
1bbc12
 void set_queued_output (struct stat const *, bool);
1bbc12
 bool has_queued_output (struct stat const *);
1bbc12
-int stat_file (char const *, struct stat *);
1bbc12
+int stat_file (char const *, struct stat *, security_context_t *);
1bbc12
 bool filename_is_safe (char const *) _GL_ATTRIBUTE_PURE;
1bbc12
 bool cwd_is_root (char const *);
1bbc12
 
1bbc12
@@ -75,7 +75,8 @@ enum file_attributes {
1bbc12
   FA_TIMES = 1,
1bbc12
   FA_IDS = 2,
1bbc12
   FA_MODE = 4,
1bbc12
-  FA_XATTRS = 8
1bbc12
+  FA_XATTRS = 8,
1bbc12
+  FA_SECCONTEXT = 16
1bbc12
 };
1bbc12
 
1bbc12
 void set_file_attributes (char const *, enum file_attributes, char const *,