Blame SOURCES/krb5-1.15-beta1-selinux-label.patch

963210
From 71fe21b5aeac1834df824ff5c6475e653ceb8b6b Mon Sep 17 00:00:00 2001
963210
From: Robbie Harwood <rharwood@redhat.com>
963210
Date: Wed, 4 Jan 2017 13:17:28 -0500
963210
Subject: [PATCH] krb5-1.15-beta1-selinux-label.patch
963210
963210
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
963210
---
963210
 src/aclocal.m4                                     |  49 +++
963210
 src/build-tools/krb5-config.in                     |   3 +-
963210
 src/config/pre.in                                  |   3 +-
963210
 src/configure.in                                   |   2 +
963210
 src/include/k5-int.h                               |   1 +
963210
 src/include/k5-label.h                             |  32 ++
963210
 src/include/krb5/krb5.hin                          |   6 +
963210
 src/kadmin/dbutil/dump.c                           |  11 +-
963210
 src/kdc/main.c                                     |   2 +-
963210
 src/lib/kadm5/logger.c                             |   4 +-
963210
 src/lib/kdb/kdb_log.c                              |   2 +-
963210
 src/lib/krb5/ccache/cc_dir.c                       |  26 +-
963210
 src/lib/krb5/keytab/kt_file.c                      |   4 +-
963210
 src/lib/krb5/os/trace.c                            |   2 +-
963210
 src/lib/krb5/rcache/rc_dfl.c                       |  13 +
963210
 src/plugins/kdb/db2/adb_openclose.c                |   2 +-
963210
 src/plugins/kdb/db2/kdb_db2.c                      |   4 +-
963210
 src/plugins/kdb/db2/libdb2/btree/bt_open.c         |   3 +-
963210
 src/plugins/kdb/db2/libdb2/hash/hash.c             |   3 +-
963210
 src/plugins/kdb/db2/libdb2/recno/rec_open.c        |   4 +-
963210
 .../kdb/ldap/ldap_util/kdb5_ldap_services.c        |  11 +-
963210
 src/slave/kpropd.c                                 |   9 +
963210
 src/util/profile/prof_file.c                       |   3 +-
963210
 src/util/support/Makefile.in                       |   3 +-
963210
 src/util/support/selinux.c                         | 406 +++++++++++++++++++++
963210
 25 files changed, 587 insertions(+), 21 deletions(-)
963210
 create mode 100644 src/include/k5-label.h
963210
 create mode 100644 src/util/support/selinux.c
963210
963210
diff --git a/src/aclocal.m4 b/src/aclocal.m4
963210
index 508e5fe90..607859f17 100644
963210
--- a/src/aclocal.m4
963210
+++ b/src/aclocal.m4
963210
@@ -89,6 +89,7 @@ AC_SUBST_FILE(libnodeps_frag)
963210
 dnl
963210
 KRB5_AC_PRAGMA_WEAK_REF
963210
 WITH_LDAP
963210
+KRB5_WITH_SELINUX
963210
 KRB5_LIB_PARAMS
963210
 KRB5_AC_INITFINI
963210
 KRB5_AC_ENABLE_THREADS
963210
@@ -1742,3 +1743,51 @@ AC_SUBST(PAM_LIBS)
963210
 AC_SUBST(PAM_MAN)
963210
 AC_SUBST(NON_PAM_MAN)
963210
 ])dnl
963210
+dnl
963210
+dnl Use libselinux to set file contexts on newly-created files.
963210
+dnl
963210
+AC_DEFUN(KRB5_WITH_SELINUX,[
963210
+AC_ARG_WITH(selinux,[AC_HELP_STRING(--with-selinux,[compile with SELinux labeling support])],
963210
+           withselinux="$withval",withselinux=auto)
963210
+old_LIBS="$LIBS"
963210
+if test "$withselinux" != no ; then
963210
+       AC_MSG_RESULT([checking for libselinux...])
963210
+       SELINUX_LIBS=
963210
+       AC_CHECK_HEADERS(selinux/selinux.h selinux/label.h)
963210
+       if test "x$ac_cv_header_selinux_selinux_h" != xyes ; then
963210
+               if test "$withselinux" = auto ; then
963210
+                       AC_MSG_RESULT([Unable to locate selinux/selinux.h.])
963210
+                       withselinux=no
963210
+               else
963210
+                       AC_MSG_ERROR([Unable to locate selinux/selinux.h.])
963210
+               fi
963210
+       fi
963210
+
963210
+       LIBS=
963210
+       unset ac_cv_func_setfscreatecon
963210
+       AC_CHECK_FUNCS(setfscreatecon selabel_open)
963210
+       if test "x$ac_cv_func_setfscreatecon" = xno ; then
963210
+               AC_CHECK_LIB(selinux,setfscreatecon)
963210
+               unset ac_cv_func_setfscreatecon
963210
+               AC_CHECK_FUNCS(setfscreatecon selabel_open)
963210
+               if test "x$ac_cv_func_setfscreatecon" = xyes ; then
963210
+                       SELINUX_LIBS="$LIBS"
963210
+               else
963210
+                       if test "$withselinux" = auto ; then
963210
+                               AC_MSG_RESULT([Unable to locate libselinux.])
963210
+                               withselinux=no
963210
+                       else
963210
+                               AC_MSG_ERROR([Unable to locate libselinux.])
963210
+                       fi
963210
+               fi
963210
+       fi
963210
+       if test "$withselinux" != no ; then
963210
+               AC_MSG_NOTICE([building with SELinux labeling support])
963210
+               AC_DEFINE(USE_SELINUX,1,[Define if Kerberos-aware tools should set SELinux file contexts when creating files.])
963210
+               SELINUX_LIBS="$LIBS"
963210
+		EXTRA_SUPPORT_SYMS="$EXTRA_SUPPORT_SYMS krb5int_labeled_open krb5int_labeled_fopen krb5int_push_fscreatecon_for krb5int_pop_fscreatecon"
963210
+       fi
963210
+fi
963210
+LIBS="$old_LIBS"
963210
+AC_SUBST(SELINUX_LIBS)
963210
+])dnl
963210
diff --git a/src/build-tools/krb5-config.in b/src/build-tools/krb5-config.in
963210
index f6184da3f..c17cb5eb5 100755
963210
--- a/src/build-tools/krb5-config.in
963210
+++ b/src/build-tools/krb5-config.in
963210
@@ -41,6 +41,7 @@ DL_LIB='@DL_LIB@'
963210
 DEFCCNAME='@DEFCCNAME@'
963210
 DEFKTNAME='@DEFKTNAME@'
963210
 DEFCKTNAME='@DEFCKTNAME@'
963210
+SELINUX_LIBS='@SELINUX_LIBS@'
963210
 
963210
 LIBS='@LIBS@'
963210
 GEN_LIB=@GEN_LIB@
963210
@@ -255,7 +256,7 @@ if test -n "$do_libs"; then
963210
     fi
963210
 
963210
     # If we ever support a flag to generate output suitable for static
963210
-    # linking, we would output "-lkrb5support $GEN_LIB $LIBS $DL_LIB"
963210
+    # linking, we would output "-lkrb5support $GEN_LIB $LIBS $SELINUX_LIBS $DL_LIB"
963210
     # here.
963210
 
963210
     echo $lib_flags
963210
diff --git a/src/config/pre.in b/src/config/pre.in
963210
index e0626320c..fcea229bd 100644
963210
--- a/src/config/pre.in
963210
+++ b/src/config/pre.in
963210
@@ -177,6 +177,7 @@ LD = $(PURE) @LD@
963210
 KRB_INCLUDES = -I$(BUILDTOP)/include -I$(top_srcdir)/include
963210
 LDFLAGS = @LDFLAGS@
963210
 LIBS = @LIBS@
963210
+SELINUX_LIBS=@SELINUX_LIBS@
963210
 
963210
 INSTALL=@INSTALL@
963210
 INSTALL_STRIP=
963210
@@ -399,7 +400,7 @@ SUPPORT_LIB			= -l$(SUPPORT_LIBNAME)
963210
 # HESIOD_LIBS is -lhesiod...
963210
 HESIOD_LIBS	= @HESIOD_LIBS@
963210
 
963210
-KRB5_BASE_LIBS	= $(KRB5_LIB) $(K5CRYPTO_LIB) $(COM_ERR_LIB) $(SUPPORT_LIB) $(GEN_LIB) $(LIBS) $(DL_LIB)
963210
+KRB5_BASE_LIBS	= $(KRB5_LIB) $(K5CRYPTO_LIB) $(COM_ERR_LIB) $(SUPPORT_LIB) $(GEN_LIB) $(LIBS) $(SELINUX_LIBS) $(DL_LIB)
963210
 KDB5_LIBS	= $(KDB5_LIB) $(GSSRPC_LIBS)
963210
 GSS_LIBS	= $(GSS_KRB5_LIB)
963210
 # needs fixing if ever used on Mac OS X!
963210
diff --git a/src/configure.in b/src/configure.in
963210
index daabd12c8..acf3a458b 100644
963210
--- a/src/configure.in
963210
+++ b/src/configure.in
963210
@@ -1338,6 +1338,8 @@ AC_PATH_PROG(GROFF, groff)
963210
 
963210
 KRB5_WITH_PAM
963210
 
963210
+KRB5_WITH_SELINUX
963210
+
963210
 # Make localedir work in autoconf 2.5x.
963210
 if test "${localedir+set}" != set; then
963210
     localedir='$(datadir)/locale'
963210
diff --git a/src/include/k5-int.h b/src/include/k5-int.h
963210
index 64991738a..173cb0264 100644
963210
--- a/src/include/k5-int.h
963210
+++ b/src/include/k5-int.h
963210
@@ -128,6 +128,7 @@ typedef unsigned char   u_char;
963210
 
963210
 
963210
 #include "k5-platform.h"
963210
+#include "k5-label.h"
963210
 
963210
 #define KRB5_KDB_MAX_LIFE       (60*60*24) /* one day */
963210
 #define KRB5_KDB_MAX_RLIFE      (60*60*24*7) /* one week */
963210
diff --git a/src/include/k5-label.h b/src/include/k5-label.h
963210
new file mode 100644
963210
index 000000000..dfaaa847c
963210
--- /dev/null
963210
+++ b/src/include/k5-label.h
963210
@@ -0,0 +1,32 @@
963210
+#ifndef _KRB5_LABEL_H
963210
+#define _KRB5_LABEL_H
963210
+
963210
+#ifdef THREEPARAMOPEN
963210
+#undef THREEPARAMOPEN
963210
+#endif
963210
+#ifdef WRITABLEFOPEN
963210
+#undef WRITABLEFOPEN
963210
+#endif
963210
+
963210
+/* Wrapper functions which help us create files and directories with the right
963210
+ * context labels. */
963210
+#ifdef USE_SELINUX
963210
+#include <sys/types.h>
963210
+#include <sys/stat.h>
963210
+#include <fcntl.h>
963210
+#include <stdio.h>
963210
+#include <unistd.h>
963210
+FILE *krb5int_labeled_fopen(const char *path, const char *mode);
963210
+int krb5int_labeled_creat(const char *path, mode_t mode);
963210
+int krb5int_labeled_open(const char *path, int flags, ...);
963210
+int krb5int_labeled_mkdir(const char *path, mode_t mode);
963210
+int krb5int_labeled_mknod(const char *path, mode_t mode, dev_t device);
963210
+#define THREEPARAMOPEN(x,y,z) krb5int_labeled_open(x,y,z)
963210
+#define WRITABLEFOPEN(x,y) krb5int_labeled_fopen(x,y)
963210
+void *krb5int_push_fscreatecon_for(const char *pathname);
963210
+void krb5int_pop_fscreatecon(void *previous);
963210
+#else
963210
+#define WRITABLEFOPEN(x,y) fopen(x,y)
963210
+#define THREEPARAMOPEN(x,y,z) open(x,y,z)
963210
+#endif
963210
+#endif
963210
diff --git a/src/include/krb5/krb5.hin b/src/include/krb5/krb5.hin
963210
index ac22f4c55..cf60d6c41 100644
963210
--- a/src/include/krb5/krb5.hin
963210
+++ b/src/include/krb5/krb5.hin
963210
@@ -87,6 +87,12 @@
963210
 #define THREEPARAMOPEN(x,y,z) open(x,y,z)
963210
 #endif
963210
 
963210
+#if KRB5_PRIVATE
963210
+#ifndef WRITABLEFOPEN
963210
+#define WRITABLEFOPEN(x,y) fopen(x,y)
963210
+#endif
963210
+#endif
963210
+
963210
 #define KRB5_OLD_CRYPTO
963210
 
963210
 #include <stdlib.h>
963210
diff --git a/src/kadmin/dbutil/dump.c b/src/kadmin/dbutil/dump.c
963210
index f7889bd23..cad53cfbf 100644
963210
--- a/src/kadmin/dbutil/dump.c
963210
+++ b/src/kadmin/dbutil/dump.c
963210
@@ -148,12 +148,21 @@ create_ofile(char *ofile, char **tmpname)
963210
 {
963210
     int fd = -1;
963210
     FILE *f;
963210
+#ifdef USE_SELINUX
963210
+    void *selabel;
963210
+#endif
963210
 
963210
     *tmpname = NULL;
963210
     if (asprintf(tmpname, "%s-XXXXXX", ofile) < 0)
963210
         goto error;
963210
 
963210
+#ifdef USE_SELINUX
963210
+    selabel = krb5int_push_fscreatecon_for(ofile);
963210
+#endif
963210
     fd = mkstemp(*tmpname);
963210
+#ifdef USE_SELINUX
963210
+    krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
     if (fd == -1)
963210
         goto error;
963210
 
963210
@@ -194,7 +203,7 @@ prep_ok_file(krb5_context context, char *file_name, int *fd)
963210
         return 0;
963210
     }
963210
 
963210
-    *fd = open(file_ok, O_WRONLY | O_CREAT | O_TRUNC, 0600);
963210
+    *fd = THREEPARAMOPEN(file_ok, O_WRONLY | O_CREAT | O_TRUNC, 0600);
963210
     if (*fd == -1) {
963210
         com_err(progname, errno, _("while creating 'ok' file, '%s'"), file_ok);
963210
         exit_status++;
963210
diff --git a/src/kdc/main.c b/src/kdc/main.c
963210
index ebc852bba..a4dffb29a 100644
963210
--- a/src/kdc/main.c
963210
+++ b/src/kdc/main.c
963210
@@ -872,7 +872,7 @@ write_pid_file(const char *path)
963210
     FILE *file;
963210
     unsigned long pid;
963210
 
963210
-    file = fopen(path, "w");
963210
+    file = WRITABLEFOPEN(path, "w");
963210
     if (file == NULL)
963210
         return errno;
963210
     pid = (unsigned long) getpid();
963210
diff --git a/src/lib/kadm5/logger.c b/src/lib/kadm5/logger.c
963210
index ce79fabf7..c53a5743f 100644
963210
--- a/src/lib/kadm5/logger.c
963210
+++ b/src/lib/kadm5/logger.c
963210
@@ -414,7 +414,7 @@ krb5_klog_init(krb5_context kcontext, char *ename, char *whoami, krb5_boolean do
963210
                      */
963210
                     append = (cp[4] == ':') ? O_APPEND : 0;
963210
                     if (append || cp[4] == '=') {
963210
-                        fd = open(&cp[5], O_CREAT | O_WRONLY | append,
963210
+                        fd = THREEPARAMOPEN(&cp[5], O_CREAT | O_WRONLY | append,
963210
                                   S_IRUSR | S_IWUSR | S_IRGRP);
963210
                         if (fd != -1)
963210
                             f = fdopen(fd, append ? "a" : "w");
963210
@@ -918,7 +918,7 @@ krb5_klog_reopen(krb5_context kcontext)
963210
              * In case the old logfile did not get moved out of the
963210
              * way, open for append to prevent squashing the old logs.
963210
              */
963210
-            f = fopen(log_control.log_entries[lindex].lfu_fname, "a+");
963210
+            f = WRITABLEFOPEN(log_control.log_entries[lindex].lfu_fname, "a+");
963210
             if (f) {
963210
                 set_cloexec_file(f);
963210
                 log_control.log_entries[lindex].lfu_filep = f;
963210
diff --git a/src/lib/kdb/kdb_log.c b/src/lib/kdb/kdb_log.c
963210
index 766d3002a..6466417b7 100644
963210
--- a/src/lib/kdb/kdb_log.c
963210
+++ b/src/lib/kdb/kdb_log.c
963210
@@ -476,7 +476,7 @@ ulog_map(krb5_context context, const char *logname, uint32_t ulogentries)
963210
     int ulogfd = -1;
963210
 
963210
     if (stat(logname, &st) == -1) {
963210
-        ulogfd = open(logname, O_RDWR | O_CREAT, 0600);
963210
+        ulogfd = THREEPARAMOPEN(logname, O_RDWR | O_CREAT, 0600);
963210
         if (ulogfd == -1)
963210
             return errno;
963210
 
963210
diff --git a/src/lib/krb5/ccache/cc_dir.c b/src/lib/krb5/ccache/cc_dir.c
963210
index bba64e516..73f0fe62d 100644
963210
--- a/src/lib/krb5/ccache/cc_dir.c
963210
+++ b/src/lib/krb5/ccache/cc_dir.c
963210
@@ -183,10 +183,19 @@ write_primary_file(const char *primary_path, const char *contents)
963210
     char *newpath = NULL;
963210
     FILE *fp = NULL;
963210
     int fd = -1, status;
963210
+#ifdef USE_SELINUX
963210
+    void *selabel;
963210
+#endif
963210
 
963210
     if (asprintf(&newpath, "%s.XXXXXX", primary_path) < 0)
963210
         return ENOMEM;
963210
+#ifdef USE_SELINUX
963210
+    selabel = krb5int_push_fscreatecon_for(primary_path);
963210
+#endif
963210
     fd = mkstemp(newpath);
963210
+#ifdef USE_SELINUX
963210
+    krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
     if (fd < 0)
963210
         goto cleanup;
963210
 #ifdef HAVE_CHMOD
963210
@@ -221,10 +230,23 @@ static krb5_error_code
963210
 verify_dir(krb5_context context, const char *dirname)
963210
 {
963210
     struct stat st;
963210
+    int status;
963210
+#ifdef USE_SELINUX
963210
+    void *selabel;
963210
+#endif
963210
 
963210
     if (stat(dirname, &st) < 0) {
963210
-        if (errno == ENOENT && mkdir(dirname, S_IRWXU) == 0)
963210
-            return 0;
963210
+        if (errno == ENOENT) {
963210
+#ifdef USE_SELINUX
963210
+            selabel = krb5int_push_fscreatecon_for(dirname);
963210
+#endif
963210
+            status = mkdir(dirname, S_IRWXU);
963210
+#ifdef USE_SELINUX
963210
+            krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
+            if (status == 0)
963210
+                return 0;
963210
+        }
963210
         k5_setmsg(context, KRB5_FCC_NOFILE,
963210
                   _("Credential cache directory %s does not exist"),
963210
                   dirname);
963210
diff --git a/src/lib/krb5/keytab/kt_file.c b/src/lib/krb5/keytab/kt_file.c
963210
index 6a42f267d..674d88bab 100644
963210
--- a/src/lib/krb5/keytab/kt_file.c
963210
+++ b/src/lib/krb5/keytab/kt_file.c
963210
@@ -1022,14 +1022,14 @@ krb5_ktfileint_open(krb5_context context, krb5_keytab id, int mode)
963210
 
963210
     KTCHECKLOCK(id);
963210
     errno = 0;
963210
-    KTFILEP(id) = fopen(KTFILENAME(id),
963210
+    KTFILEP(id) = WRITABLEFOPEN(KTFILENAME(id),
963210
                         (mode == KRB5_LOCKMODE_EXCLUSIVE) ? "rb+" : "rb");
963210
     if (!KTFILEP(id)) {
963210
         if ((mode == KRB5_LOCKMODE_EXCLUSIVE) && (errno == ENOENT)) {
963210
             /* try making it first time around */
963210
             k5_create_secure_file(context, KTFILENAME(id));
963210
             errno = 0;
963210
-            KTFILEP(id) = fopen(KTFILENAME(id), "rb+");
963210
+            KTFILEP(id) = WRITABLEFOPEN(KTFILENAME(id), "rb+");
963210
             if (!KTFILEP(id))
963210
                 goto report_errno;
963210
             writevno = 1;
963210
diff --git a/src/lib/krb5/os/trace.c b/src/lib/krb5/os/trace.c
963210
index 83c8d4db8..a19246128 100644
963210
--- a/src/lib/krb5/os/trace.c
963210
+++ b/src/lib/krb5/os/trace.c
963210
@@ -397,7 +397,7 @@ krb5_set_trace_filename(krb5_context context, const char *filename)
963210
     fd = malloc(sizeof(*fd));
963210
     if (fd == NULL)
963210
         return ENOMEM;
963210
-    *fd = open(filename, O_WRONLY|O_CREAT|O_APPEND, 0600);
963210
+    *fd = THREEPARAMOPEN(filename, O_WRONLY|O_CREAT|O_APPEND, 0600);
963210
     if (*fd == -1) {
963210
         free(fd);
963210
         return errno;
963210
diff --git a/src/lib/krb5/rcache/rc_dfl.c b/src/lib/krb5/rcache/rc_dfl.c
963210
index c4d2c744d..c0f12ed9d 100644
963210
--- a/src/lib/krb5/rcache/rc_dfl.c
963210
+++ b/src/lib/krb5/rcache/rc_dfl.c
963210
@@ -794,6 +794,9 @@ krb5_rc_dfl_expunge_locked(krb5_context context, krb5_rcache id)
963210
     krb5_error_code retval = 0;
963210
     krb5_rcache tmp;
963210
     krb5_deltat lifespan = t->lifespan;  /* save original lifespan */
963210
+#ifdef USE_SELINUX
963210
+    void *selabel;
963210
+#endif
963210
 
963210
     if (! t->recovering) {
963210
         name = t->name;
963210
@@ -815,7 +818,17 @@ krb5_rc_dfl_expunge_locked(krb5_context context, krb5_rcache id)
963210
     retval = krb5_rc_resolve(context, tmp, 0);
963210
     if (retval)
963210
         goto cleanup;
963210
+#ifdef USE_SELINUX
963210
+    if (t->d.fn != NULL)
963210
+        selabel = krb5int_push_fscreatecon_for(t->d.fn);
963210
+    else
963210
+        selabel = NULL;
963210
+#endif
963210
     retval = krb5_rc_initialize(context, tmp, lifespan);
963210
+#ifdef USE_SELINUX
963210
+    if (selabel != NULL)
963210
+        krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
     if (retval)
963210
         goto cleanup;
963210
     for (q = t->a; q; q = q->na) {
963210
diff --git a/src/plugins/kdb/db2/adb_openclose.c b/src/plugins/kdb/db2/adb_openclose.c
963210
index 7db30a33b..2b9d01921 100644
963210
--- a/src/plugins/kdb/db2/adb_openclose.c
963210
+++ b/src/plugins/kdb/db2/adb_openclose.c
963210
@@ -152,7 +152,7 @@ osa_adb_init_db(osa_adb_db_t *dbp, char *filename, char *lockfilename,
963210
          * needs be open read/write so that write locking can work with
963210
          * POSIX systems
963210
          */
963210
-        if ((lockp->lockinfo.lockfile = fopen(lockfilename, "r+")) == NULL) {
963210
+        if ((lockp->lockinfo.lockfile = WRITABLEFOPEN(lockfilename, "r+")) == NULL) {
963210
             /*
963210
              * maybe someone took away write permission so we could only
963210
              * get shared locks?
963210
diff --git a/src/plugins/kdb/db2/kdb_db2.c b/src/plugins/kdb/db2/kdb_db2.c
963210
index 4c4036eb4..d90bdeaba 100644
963210
--- a/src/plugins/kdb/db2/kdb_db2.c
963210
+++ b/src/plugins/kdb/db2/kdb_db2.c
963210
@@ -694,8 +694,8 @@ ctx_create_db(krb5_context context, krb5_db2_context *dbc)
963210
     if (retval)
963210
         return retval;
963210
 
963210
-    dbc->db_lf_file = open(dbc->db_lf_name, O_CREAT | O_RDWR | O_TRUNC,
963210
-                           0600);
963210
+    dbc->db_lf_file = THREEPARAMOPEN(dbc->db_lf_name,
963210
+                                     O_CREAT | O_RDWR | O_TRUNC, 0600);
963210
     if (dbc->db_lf_file < 0) {
963210
         retval = errno;
963210
         goto cleanup;
963210
diff --git a/src/plugins/kdb/db2/libdb2/btree/bt_open.c b/src/plugins/kdb/db2/libdb2/btree/bt_open.c
963210
index 2977b17f3..d5809a5a9 100644
963210
--- a/src/plugins/kdb/db2/libdb2/btree/bt_open.c
963210
+++ b/src/plugins/kdb/db2/libdb2/btree/bt_open.c
963210
@@ -60,6 +60,7 @@ static char sccsid[] = "@(#)bt_open.c	8.11 (Berkeley) 11/2/95";
963210
 #include <string.h>
963210
 #include <unistd.h>
963210
 
963210
+#include "k5-int.h"
963210
 #include "db-int.h"
963210
 #include "btree.h"
963210
 
963210
@@ -203,7 +204,7 @@ __bt_open(fname, flags, mode, openinfo, dflags)
963210
 			goto einval;
963210
 		}
963210
 
963210
-		if ((t->bt_fd = open(fname, flags | O_BINARY, mode)) < 0)
963210
+		if ((t->bt_fd = THREEPARAMOPEN(fname, flags | O_BINARY, mode)) < 0)
963210
 			goto err;
963210
 
963210
 	} else {
963210
diff --git a/src/plugins/kdb/db2/libdb2/hash/hash.c b/src/plugins/kdb/db2/libdb2/hash/hash.c
963210
index 76f5d4709..1fa8b8389 100644
963210
--- a/src/plugins/kdb/db2/libdb2/hash/hash.c
963210
+++ b/src/plugins/kdb/db2/libdb2/hash/hash.c
963210
@@ -51,6 +51,7 @@ static char sccsid[] = "@(#)hash.c	8.12 (Berkeley) 11/7/95";
963210
 #include <assert.h>
963210
 #endif
963210
 
963210
+#include "k5-int.h"
963210
 #include "db-int.h"
963210
 #include "hash.h"
963210
 #include "page.h"
963210
@@ -140,7 +141,7 @@ __kdb2_hash_open(file, flags, mode, info, dflags)
963210
 		new_table = 1;
963210
 	}
963210
 	if (file) {
963210
-		if ((hashp->fp = open(file, flags|O_BINARY, mode)) == -1)
963210
+		if ((hashp->fp = THREEPARAMOPEN(file, flags|O_BINARY, mode)) == -1)
963210
 			RETURN_ERROR(errno, error0);
963210
 		(void)fcntl(hashp->fp, F_SETFD, 1);
963210
 	}
963210
diff --git a/src/plugins/kdb/db2/libdb2/recno/rec_open.c b/src/plugins/kdb/db2/libdb2/recno/rec_open.c
963210
index d8b26e701..b0daa7c02 100644
963210
--- a/src/plugins/kdb/db2/libdb2/recno/rec_open.c
963210
+++ b/src/plugins/kdb/db2/libdb2/recno/rec_open.c
963210
@@ -51,6 +51,7 @@ static char sccsid[] = "@(#)rec_open.c	8.12 (Berkeley) 11/18/94";
963210
 #include <stdio.h>
963210
 #include <unistd.h>
963210
 
963210
+#include "k5-int.h"
963210
 #include "db-int.h"
963210
 #include "recno.h"
963210
 
963210
@@ -68,7 +69,8 @@ __rec_open(fname, flags, mode, openinfo, dflags)
963210
 	int rfd = -1, sverrno;
963210
 
963210
 	/* Open the user's file -- if this fails, we're done. */
963210
-	if (fname != NULL && (rfd = open(fname, flags | O_BINARY, mode)) < 0)
963210
+	if (fname != NULL &&
963210
+            (rfd = THREEPARAMOPEN(fname, flags | O_BINARY, mode)) < 0)
963210
 		return (NULL);
963210
 
963210
 	if (fname != NULL && fcntl(rfd, F_SETFD, 1) == -1) {
963210
diff --git a/src/plugins/kdb/ldap/ldap_util/kdb5_ldap_services.c b/src/plugins/kdb/ldap/ldap_util/kdb5_ldap_services.c
963210
index 022156a5e..3d6994c67 100644
963210
--- a/src/plugins/kdb/ldap/ldap_util/kdb5_ldap_services.c
963210
+++ b/src/plugins/kdb/ldap/ldap_util/kdb5_ldap_services.c
963210
@@ -203,7 +203,7 @@ kdb5_ldap_stash_service_password(int argc, char **argv)
963210
 
963210
     /* set password in the file */
963210
     old_mode = umask(0177);
963210
-    pfile = fopen(file_name, "a+");
963210
+    pfile = WRITABLEFOPEN(file_name, "a+");
963210
     if (pfile == NULL) {
963210
         com_err(me, errno, _("Failed to open file %s: %s"), file_name,
963210
                 strerror (errno));
963210
@@ -244,6 +244,9 @@ kdb5_ldap_stash_service_password(int argc, char **argv)
963210
          * Delete the existing entry and add the new entry
963210
          */
963210
         FILE *newfile;
963210
+#ifdef USE_SELINUX
963210
+        void *selabel;
963210
+#endif
963210
 
963210
         mode_t omask;
963210
 
963210
@@ -255,7 +258,13 @@ kdb5_ldap_stash_service_password(int argc, char **argv)
963210
         }
963210
 
963210
         omask = umask(077);
963210
+#ifdef USE_SELINUX
963210
+        selabel = krb5int_push_fscreatecon_for(file_name);
963210
+#endif
963210
         newfile = fopen(tmp_file, "w");
963210
+#ifdef USE_SELINUX
963210
+        krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
         umask (omask);
963210
         if (newfile == NULL) {
963210
             com_err(me, errno, _("Error creating file %s"), tmp_file);
963210
diff --git a/src/slave/kpropd.c b/src/slave/kpropd.c
963210
index 056c31a42..b78c3d9e5 100644
963210
--- a/src/slave/kpropd.c
963210
+++ b/src/slave/kpropd.c
963210
@@ -464,6 +464,9 @@ doit(int fd)
963210
     krb5_enctype etype;
963210
     int database_fd;
963210
     char host[INET6_ADDRSTRLEN + 1];
963210
+#ifdef USE_SELINUX
963210
+    void *selabel;
963210
+#endif
963210
 
963210
     signal_wrapper(SIGALRM, alarm_handler);
963210
     alarm(params.iprop_resync_timeout);
963210
@@ -520,9 +523,15 @@ doit(int fd)
963210
         free(name);
963210
         exit(1);
963210
     }
963210
+#ifdef USE_SELINUX
963210
+    selabel = krb5int_push_fscreatecon_for(file);
963210
+#endif
963210
     omask = umask(077);
963210
     lock_fd = open(temp_file_name, O_RDWR | O_CREAT, 0600);
963210
     (void)umask(omask);
963210
+#ifdef USE_SELINUX
963210
+    krb5int_pop_fscreatecon(selabel);
963210
+#endif
963210
     retval = krb5_lock_file(kpropd_context, lock_fd,
963210
                             KRB5_LOCKMODE_EXCLUSIVE | KRB5_LOCKMODE_DONTBLOCK);
963210
     if (retval) {
963210
diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c
963210
index 907c119bb..0f5462aea 100644
963210
--- a/src/util/profile/prof_file.c
963210
+++ b/src/util/profile/prof_file.c
963210
@@ -33,6 +33,7 @@
963210
 #endif
963210
 
963210
 #include "k5-platform.h"
963210
+#include "k5-label.h"
963210
 
963210
 struct global_shared_profile_data {
963210
     /* This is the head of the global list of shared trees */
963210
@@ -423,7 +424,7 @@ static errcode_t write_data_to_file(prf_data_t data, const char *outfile,
963210
 
963210
     errno = 0;
963210
 
963210
-    f = fopen(new_file, "w");
963210
+    f = WRITABLEFOPEN(new_file, "w");
963210
     if (!f) {
963210
         retval = errno;
963210
         if (retval == 0)
963210
diff --git a/src/util/support/Makefile.in b/src/util/support/Makefile.in
963210
index 6239e4176..17bcd2a67 100644
963210
--- a/src/util/support/Makefile.in
963210
+++ b/src/util/support/Makefile.in
963210
@@ -69,6 +69,7 @@ IPC_SYMS= \
963210
 
963210
 STLIBOBJS= \
963210
 	threads.o \
963210
+	selinux.o \
963210
 	init-addrinfo.o \
963210
 	plugins.o \
963210
 	errors.o \
963210
@@ -148,7 +149,7 @@ SRCS=\
963210
 
963210
 SHLIB_EXPDEPS =
963210
 # Add -lm if dumping thread stats, for sqrt.
963210
-SHLIB_EXPLIBS= $(LIBS) $(DL_LIB)
963210
+SHLIB_EXPLIBS= $(LIBS) $(SELINUX_LIBS) $(DL_LIB)
963210
 
963210
 DEPLIBS=
963210
 
963210
diff --git a/src/util/support/selinux.c b/src/util/support/selinux.c
963210
new file mode 100644
963210
index 000000000..230263421
963210
--- /dev/null
963210
+++ b/src/util/support/selinux.c
963210
@@ -0,0 +1,406 @@
963210
+/*
963210
+ * Copyright 2007,2008,2009,2011,2012,2013,2016 Red Hat, Inc.  All Rights Reserved.
963210
+ *
963210
+ * Redistribution and use in source and binary forms, with or without
963210
+ * modification, are permitted provided that the following conditions are met:
963210
+ *
963210
+ *  Redistributions of source code must retain the above copyright notice, this
963210
+ *  list of conditions and the following disclaimer.
963210
+ *
963210
+ *  Redistributions in binary form must reproduce the above copyright notice,
963210
+ *  this list of conditions and the following disclaimer in the documentation
963210
+ *  and/or other materials provided with the distribution.
963210
+ *
963210
+ *  Neither the name of Red Hat, Inc. nor the names of its contributors may be
963210
+ *  used to endorse or promote products derived from this software without
963210
+ *  specific prior written permission.
963210
+ *
963210
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
963210
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
963210
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
963210
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
963210
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
963210
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
963210
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
963210
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
963210
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
963210
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
963210
+ * POSSIBILITY OF SUCH DAMAGE.
963210
+ *
963210
+ * File-opening wrappers for creating correctly-labeled files.  So far, we can
963210
+ * assume that this is Linux-specific, so we make many simplifying assumptions.
963210
+ */
963210
+
963210
+#include "../../include/autoconf.h"
963210
+
963210
+#ifdef USE_SELINUX
963210
+
963210
+#include <k5-label.h>
963210
+#include <k5-platform.h>
963210
+
963210
+#include <sys/types.h>
963210
+#include <sys/stat.h>
963210
+
963210
+#include <errno.h>
963210
+#include <fcntl.h>
963210
+#include <limits.h>
963210
+#include <pthread.h>
963210
+#include <stdarg.h>
963210
+#include <stdio.h>
963210
+#include <stdlib.h>
963210
+#include <string.h>
963210
+#include <unistd.h>
963210
+
963210
+#include <selinux/selinux.h>
963210
+#include <selinux/context.h>
963210
+#include <selinux/label.h>
963210
+
963210
+/* #define DEBUG 1 */
963210
+static void
963210
+debug_log(const char *fmt, ...)
963210
+{
963210
+#ifdef DEBUG
963210
+    va_list ap;
963210
+    va_start(ap, str);
963210
+    if (isatty(fileno(stderr))) {
963210
+        vfprintf(stderr, fmt, ap);
963210
+    }
963210
+    va_end(ap);
963210
+#endif
963210
+
963210
+    return;
963210
+}
963210
+
963210
+/* Mutex used to serialize use of the process-global file creation context. */
963210
+k5_mutex_t labeled_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
963210
+
963210
+/* Make sure we finish initializing that mutex before attempting to use it. */
963210
+k5_once_t labeled_once = K5_ONCE_INIT;
963210
+static void
963210
+label_mutex_init(void)
963210
+{
963210
+    k5_mutex_finish_init(&labeled_mutex);
963210
+}
963210
+
963210
+static struct selabel_handle *selabel_ctx;
963210
+static time_t selabel_last_changed;
963210
+
963210
+MAKE_FINI_FUNCTION(cleanup_fscreatecon);
963210
+
963210
+static void
963210
+cleanup_fscreatecon(void)
963210
+{
963210
+    if (selabel_ctx != NULL) {
963210
+        selabel_close(selabel_ctx);
963210
+        selabel_ctx = NULL;
963210
+    }
963210
+}
963210
+
963210
+static security_context_t
963210
+push_fscreatecon(const char *pathname, mode_t mode)
963210
+{
963210
+    security_context_t previous, configuredsc, currentsc, derivedsc;
963210
+    context_t current, derived;
963210
+    const char *fullpath, *currentuser;
963210
+    char *genpath;
963210
+
963210
+    previous = configuredsc = currentsc = derivedsc = NULL;
963210
+    current = derived = NULL;
963210
+    genpath = NULL;
963210
+
963210
+    fullpath = pathname;
963210
+
963210
+    if (!is_selinux_enabled()) {
963210
+        goto fail;
963210
+    }
963210
+
963210
+    if (getfscreatecon(&previous) != 0) {
963210
+        goto fail;
963210
+    }
963210
+
963210
+    /* Canonicalize pathname */
963210
+    if (pathname[0] != '/') {
963210
+        char *wd;
963210
+        size_t len;
963210
+        len = 0;
963210
+
963210
+        wd = getcwd(NULL, len);
963210
+        if (wd == NULL) {
963210
+            goto fail;
963210
+        }
963210
+
963210
+        len = strlen(wd) + 1 + strlen(pathname) + 1;
963210
+        genpath = malloc(len);
963210
+        if (genpath == NULL) {
963210
+            free(wd);
963210
+            goto fail;
963210
+        }
963210
+
963210
+        sprintf(genpath, "%s/%s", wd, pathname);
963210
+        free(wd);
963210
+        fullpath = genpath;
963210
+    }
963210
+
963210
+    debug_log("Looking up context for \"%s\"(%05o).\n", fullpath, mode);
963210
+
963210
+    /* Check whether context file has changed under us */
963210
+    if (selabel_ctx != NULL || selabel_last_changed == 0) {
963210
+        const char *cpath;
963210
+        struct stat st;
963210
+        int i = -1;
963210
+
963210
+        cpath = selinux_file_context_path();
963210
+        if (cpath == NULL || (i = stat(cpath, &st)) != 0 ||
963210
+            st.st_mtime != selabel_last_changed) {
963210
+            cleanup_fscreatecon();
963210
+
963210
+            selabel_last_changed = i ? time(NULL) : st.st_mtime;
963210
+        }
963210
+    }
963210
+
963210
+    if (selabel_ctx == NULL) {
963210
+        selabel_ctx = selabel_open(SELABEL_CTX_FILE, NULL, 0);
963210
+    }
963210
+
963210
+    if (selabel_ctx != NULL &&
963210
+        selabel_lookup(selabel_ctx, &configuredsc, fullpath, mode) != 0) {
963210
+        goto fail;
963210
+    }
963210
+
963210
+    if (genpath != NULL) {
963210
+        free(genpath);
963210
+        genpath = NULL;
963210
+    }
963210
+
963210
+    if (configuredsc == NULL) {
963210
+        goto fail;
963210
+    }
963210
+
963210
+    getcon(&currentsc);
963210
+
963210
+    /* AAAAAAAA */
963210
+    if (currentsc != NULL) {
963210
+        derived = context_new(configuredsc);
963210
+
963210
+        if (derived != NULL) {
963210
+            current = context_new(currentsc);
963210
+
963210
+            if (current != NULL) {
963210
+                currentuser = context_user_get(current);
963210
+
963210
+                if (currentuser != NULL) {
963210
+                    if (context_user_set(derived,
963210
+                                         currentuser) == 0) {
963210
+                        derivedsc = context_str(derived);
963210
+
963210
+                        if (derivedsc != NULL) {
963210
+                            freecon(configuredsc);
963210
+                            configuredsc = strdup(derivedsc);
963210
+                        }
963210
+                    }
963210
+                }
963210
+
963210
+                context_free(current);
963210
+            }
963210
+
963210
+            context_free(derived);
963210
+        }
963210
+
963210
+        freecon(currentsc);
963210
+    }
963210
+
963210
+    debug_log("Setting file creation context to \"%s\".\n", configuredsc);
963210
+    if (setfscreatecon(configuredsc) != 0) {
963210
+        debug_log("Unable to determine current context.\n");
963210
+        goto fail;
963210
+    }
963210
+
963210
+    freecon(configuredsc);
963210
+    return previous;
963210
+
963210
+fail:
963210
+    if (previous != NULL) {
963210
+        freecon(previous);
963210
+    }
963210
+    if (genpath != NULL) {
963210
+        free(genpath);
963210
+    }
963210
+    if (configuredsc != NULL) {
963210
+        freecon(configuredsc);
963210
+    }
963210
+
963210
+    cleanup_fscreatecon();
963210
+    return NULL;
963210
+}
963210
+
963210
+static void
963210
+pop_fscreatecon(security_context_t previous)
963210
+{
963210
+    if (!is_selinux_enabled()) {
963210
+        return;
963210
+    }
963210
+
963210
+    if (previous != NULL) {
963210
+        debug_log("Resetting file creation context to \"%s\".\n", previous);
963210
+    } else {
963210
+        debug_log("Resetting file creation context to default.\n");
963210
+    }
963210
+
963210
+    /* NULL resets to default */
963210
+    setfscreatecon(previous);
963210
+
963210
+    if (previous != NULL) {
963210
+        freecon(previous);
963210
+    }
963210
+
963210
+    /* Need to clean this up here otherwise it leaks */
963210
+    cleanup_fscreatecon();
963210
+}
963210
+
963210
+void *
963210
+krb5int_push_fscreatecon_for(const char *pathname)
963210
+{
963210
+    struct stat st;
963210
+    void *retval;
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+
963210
+    if (stat(pathname, &st) != 0) {
963210
+        st.st_mode = S_IRUSR | S_IWUSR;
963210
+    }
963210
+
963210
+    retval = push_fscreatecon(pathname, st.st_mode);
963210
+    return retval ? retval : (void *) -1;
963210
+}
963210
+
963210
+void
963210
+krb5int_pop_fscreatecon(void *con)
963210
+{
963210
+    if (con != NULL) {
963210
+        pop_fscreatecon((con == (void *) -1) ? NULL : con);
963210
+        k5_mutex_unlock(&labeled_mutex);
963210
+    }
963210
+}
963210
+
963210
+FILE *
963210
+krb5int_labeled_fopen(const char *path, const char *mode)
963210
+{
963210
+    FILE *fp;
963210
+    int errno_save;
963210
+    security_context_t ctx;
963210
+
963210
+    if ((strcmp(mode, "r") == 0) ||
963210
+        (strcmp(mode, "rb") == 0)) {
963210
+        return fopen(path, mode);
963210
+    }
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+    ctx = push_fscreatecon(path, 0);
963210
+
963210
+    fp = fopen(path, mode);
963210
+    errno_save = errno;
963210
+
963210
+    pop_fscreatecon(ctx);
963210
+    k5_mutex_unlock(&labeled_mutex);
963210
+
963210
+    errno = errno_save;
963210
+    return fp;
963210
+}
963210
+
963210
+int
963210
+krb5int_labeled_creat(const char *path, mode_t mode)
963210
+{
963210
+    int fd;
963210
+    int errno_save;
963210
+    security_context_t ctx;
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+    ctx = push_fscreatecon(path, 0);
963210
+
963210
+    fd = creat(path, mode);
963210
+    errno_save = errno;
963210
+
963210
+    pop_fscreatecon(ctx);
963210
+    k5_mutex_unlock(&labeled_mutex);
963210
+
963210
+    errno = errno_save;
963210
+    return fd;
963210
+}
963210
+
963210
+int
963210
+krb5int_labeled_mknod(const char *path, mode_t mode, dev_t dev)
963210
+{
963210
+    int ret;
963210
+    int errno_save;
963210
+    security_context_t ctx;
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+    ctx = push_fscreatecon(path, mode);
963210
+
963210
+    ret = mknod(path, mode, dev);
963210
+    errno_save = errno;
963210
+
963210
+    pop_fscreatecon(ctx);
963210
+    k5_mutex_unlock(&labeled_mutex);
963210
+
963210
+    errno = errno_save;
963210
+    return ret;
963210
+}
963210
+
963210
+int
963210
+krb5int_labeled_mkdir(const char *path, mode_t mode)
963210
+{
963210
+    int ret;
963210
+    int errno_save;
963210
+    security_context_t ctx;
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+    ctx = push_fscreatecon(path, S_IFDIR);
963210
+
963210
+    ret = mkdir(path, mode);
963210
+    errno_save = errno;
963210
+
963210
+    pop_fscreatecon(ctx);
963210
+    k5_mutex_unlock(&labeled_mutex);
963210
+
963210
+    errno = errno_save;
963210
+    return ret;
963210
+}
963210
+
963210
+int
963210
+krb5int_labeled_open(const char *path, int flags, ...)
963210
+{
963210
+    int fd;
963210
+    int errno_save;
963210
+    security_context_t ctx;
963210
+    mode_t mode;
963210
+    va_list ap;
963210
+
963210
+    if ((flags & O_CREAT) == 0) {
963210
+        return open(path, flags);
963210
+    }
963210
+
963210
+    k5_once(&labeled_once, label_mutex_init);
963210
+    k5_mutex_lock(&labeled_mutex);
963210
+    ctx = push_fscreatecon(path, 0);
963210
+
963210
+    va_start(ap, flags);
963210
+    mode = va_arg(ap, mode_t);
963210
+    fd = open(path, flags, mode);
963210
+    va_end(ap);
963210
+
963210
+    errno_save = errno;
963210
+
963210
+    pop_fscreatecon(ctx);
963210
+    k5_mutex_unlock(&labeled_mutex);
963210
+
963210
+    errno = errno_save;
963210
+    return fd;
963210
+}
963210
+
963210
+#endif /* USE_SELINUX */