930323f
diff -up patch-2.7.6/src/common.h.selinux patch-2.7.6/src/common.h
930323f
--- patch-2.7.6/src/common.h.selinux	2018-02-03 12:41:49.000000000 +0000
930323f
+++ patch-2.7.6/src/common.h	2018-02-12 12:29:44.415225377 +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>
5587b74
@@ -84,6 +86,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
 
930323f
diff -up patch-2.7.6/src/inp.c.selinux patch-2.7.6/src/inp.c
930323f
--- patch-2.7.6/src/inp.c.selinux	2017-09-04 12:34:16.000000000 +0100
930323f
+++ patch-2.7.6/src/inp.c	2018-02-12 12:29:44.415225377 +0000
158e6d1
@@ -145,7 +145,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)
158e6d1
@@ -190,7 +190,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);
158e6d1
@@ -201,6 +201,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)))
930323f
diff -up patch-2.7.6/src/Makefile.am.selinux patch-2.7.6/src/Makefile.am
930323f
--- patch-2.7.6/src/Makefile.am.selinux	2017-09-04 12:34:16.000000000 +0100
930323f
+++ patch-2.7.6/src/Makefile.am	2018-02-12 12:29:44.415225377 +0000
ab01eb7
@@ -37,7 +37,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) \
ab01eb7
-	      $(LIB_XATTR) $(LIB_EACCESS)
ab01eb7
+	      $(LIB_XATTR) $(LIB_EACCESS) -lselinux
274dc2d
 
274dc2d
 if ENABLE_MERGE
274dc2d
   patch_SOURCES += merge.c
930323f
diff -up patch-2.7.6/src/Makefile.in.selinux patch-2.7.6/src/Makefile.in
930323f
--- patch-2.7.6/src/Makefile.in.selinux	2018-02-03 13:33:56.000000000 +0000
930323f
+++ patch-2.7.6/src/Makefile.in	2018-02-12 12:29:44.415225377 +0000
930323f
@@ -1147,7 +1147,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) \
ab01eb7
-	      $(LIB_XATTR) $(LIB_EACCESS)
ab01eb7
+	      $(LIB_XATTR) $(LIB_EACCESS) -lselinux
274dc2d
 
274dc2d
 all: all-am
274dc2d
 
930323f
diff -up patch-2.7.6/src/patch.c.selinux patch-2.7.6/src/patch.c
930323f
--- patch-2.7.6/src/patch.c.selinux	2018-02-03 12:41:49.000000000 +0000
930323f
+++ patch-2.7.6/src/patch.c	2018-02-12 12:30:27.315164138 +0000
930323f
@@ -269,19 +269,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)
930323f
@@ -598,7 +598,7 @@ main (int argc, char **argv)
930323f
 			}
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
 			}
930323f
@@ -658,7 +658,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)
930323f
@@ -1790,7 +1790,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
     }
930323f
diff -up patch-2.7.6/src/pch.c.selinux patch-2.7.6/src/pch.c
930323f
--- patch-2.7.6/src/pch.c.selinux	2018-02-03 12:41:49.000000000 +0000
930323f
+++ patch-2.7.6/src/pch.c	2018-02-12 12:29:44.416225375 +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.
ab01eb7
@@ -296,7 +296,7 @@ there_is_another_patch (bool need_header
5587b74
 	if (t > buf + 1 && *(t - 1) == '\n')
972b7e7
 	  {
5587b74
 	    inname = xmemdup0 (buf, t - buf - 1);
972b7e7
-	    inerrno = stat_file (inname, &instat);
972b7e7
+	    inerrno = stat_file (inname, &instat, &incontext);
972b7e7
 	    if (inerrno)
3db4109
 	      {
972b7e7
 		perror (inname);
ab01eb7
@@ -433,6 +433,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;
ab01eb7
@@ -473,6 +474,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;
930323f
@@ -883,7 +885,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)
930323f
@@ -922,7 +924,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;
930323f
@@ -985,7 +987,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
 	  }
930323f
@@ -998,8 +1000,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
 
930323f
diff -up patch-2.7.6/src/util.c.selinux patch-2.7.6/src/util.c
930323f
--- patch-2.7.6/src/util.c.selinux	2018-02-03 12:41:49.000000000 +0000
930323f
+++ patch-2.7.6/src/util.c	2018-02-12 12:29:44.417225374 +0000
058b322
@@ -300,6 +300,23 @@ 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;
058b322
+      if (incontext && getfilecon (to, &outcontext) != -1 && outcontext)
274dc2d
+	{
058b322
+	  if (strcmp (outcontext, incontext) &&
058b322
+	      setfilecon (to, incontext) != 0)
058b322
+	    {
058b322
+	      freecon (outcontext);
058b322
+	      if (errno != ENOTSUP && errno != EPERM)
058b322
+		pfatal ("Can't set security context on file %s",
058b322
+			quotearg (to));
058b322
+	    }
058b322
+	  else
058b322
+	    freecon (outcontext);
274dc2d
+	}
274dc2d
+    }
274dc2d
 }
274dc2d
 
274dc2d
 static void
058b322
@@ -446,7 +463,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)
930323f
@@ -818,7 +835,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
     {
930323f
@@ -843,6 +861,13 @@ version_get (char const *filename, char
ffc4fff
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
158e6d1
       if (safe_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;
930323f
@@ -1670,12 +1695,28 @@ make_tempfile (char const **name, char l
ab01eb7
   return fd;
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 *) =
158e6d1
     follow_symlinks ? safe_stat : safe_lstat;
972b7e7
+  int (*xgetfilecon)(char const *, security_context_t *) =
972b7e7
+    follow_symlinks ? getfilecon : lgetfilecon;
058b322
+
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;
058b322
 
058b322
-  return xstat (filename, st) == 0 ? 0 : errno;
972b7e7
+	  *con = NULL;
972b7e7
+	}
972b7e7
+      else
972b7e7
+	return 0;
972b7e7
+    }
ab01eb7
+ 
972b7e7
+  return errno;
972b7e7
 }
0885656
 
fe471a9
 /* Check if a filename is relative and free of ".." components.
930323f
diff -up patch-2.7.6/src/util.h.selinux patch-2.7.6/src/util.h
930323f
--- patch-2.7.6/src/util.h.selinux	2018-02-03 12:41:49.000000000 +0000
930323f
+++ patch-2.7.6/src/util.h	2018-02-12 12:30:08.533190949 +0000
158e6d1
@@ -44,7 +44,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);
158e6d1
@@ -67,7 +67,7 @@ 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 *);
930323f
 bool filename_is_safe (char const *) _GL_ATTRIBUTE_PURE;
0885656
 bool cwd_is_root (char const *);
972b7e7
 
158e6d1
@@ -75,7 +75,8 @@ 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 *,