Blame SOURCES/shadow-4.3.1-selinux-perms.patch

4fb1b2
Index: shadow-4.5/src/chgpasswd.c
4fb1b2
===================================================================
4fb1b2
--- shadow-4.5.orig/src/chgpasswd.c
4fb1b2
+++ shadow-4.5/src/chgpasswd.c
4fb1b2
@@ -39,6 +39,13 @@
4fb1b2
 #include <pwd.h>
4fb1b2
 #include <stdio.h>
4fb1b2
 #include <stdlib.h>
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+#include <selinux/selinux.h>
4fb1b2
+#include <selinux/avc.h>
4fb1b2
+#endif
4fb1b2
+#ifdef WITH_LIBAUDIT
4fb1b2
+#include <libaudit.h>
4fb1b2
+#endif
4fb1b2
 #ifdef ACCT_TOOLS_SETUID
4fb1b2
 #ifdef USE_PAM
4fb1b2
 #include "pam_defs.h"
4fb1b2
@@ -76,6 +83,9 @@ static bool sgr_locked = false;
4fb1b2
 #endif
4fb1b2
 static bool gr_locked = false;
4fb1b2
 
4fb1b2
+/* The name of the caller */
4fb1b2
+static char *myname = NULL;
4fb1b2
+
4fb1b2
 /* local function prototypes */
4fb1b2
 static void fail_exit (int code);
4fb1b2
 static /*@noreturn@*/void usage (int status);
4fb1b2
@@ -300,6 +310,63 @@ static void check_perms (void)
4fb1b2
 #endif				/* ACCT_TOOLS_SETUID */
4fb1b2
 }
4fb1b2
 
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+static int
4fb1b2
+log_callback (int type, const char *fmt, ...)
4fb1b2
+{
4fb1b2
+    int audit_fd;
4fb1b2
+    va_list ap;
4fb1b2
+
4fb1b2
+    va_start(ap, fmt);
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+    audit_fd = audit_open();
4fb1b2
+
4fb1b2
+    if (audit_fd >= 0) {
4fb1b2
+	char *buf;
4fb1b2
+
4fb1b2
+	if (vasprintf (&buf, fmt, ap) < 0)
4fb1b2
+		goto ret;
4fb1b2
+	audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
4fb1b2
+				   NULL, 0);
4fb1b2
+	audit_close(audit_fd);
4fb1b2
+	free(buf);
4fb1b2
+	goto ret;
4fb1b2
+    }
4fb1b2
+
4fb1b2
+#endif
4fb1b2
+    vsyslog (LOG_USER | LOG_INFO, fmt, ap);
4fb1b2
+ret:
4fb1b2
+    va_end(ap);
4fb1b2
+    return 0;
4fb1b2
+}
4fb1b2
+
4fb1b2
+static void
4fb1b2
+selinux_check_root (void)
4fb1b2
+{
4fb1b2
+    int status = -1;
4fb1b2
+    security_context_t user_context;
4fb1b2
+    union selinux_callback old_callback;
4fb1b2
+
4fb1b2
+    if (is_selinux_enabled() < 1)
4fb1b2
+	return;
4fb1b2
+
4fb1b2
+    old_callback = selinux_get_callback(SELINUX_CB_LOG);
4fb1b2
+    /* setup callbacks */
4fb1b2
+    selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
4fb1b2
+    if ((status = getprevcon(&user_context)) < 0) {
4fb1b2
+	selinux_set_callback(SELINUX_CB_LOG, old_callback);
4fb1b2
+	exit(1);
4fb1b2
+    }
4fb1b2
+
4fb1b2
+    status = selinux_check_access(user_context, user_context, "passwd", "passwd", NULL);
4fb1b2
+
4fb1b2
+    selinux_set_callback(SELINUX_CB_LOG, old_callback);
4fb1b2
+    freecon(user_context);
4fb1b2
+    if (status != 0 && security_getenforce() != 0)
4fb1b2
+	exit(1);
4fb1b2
+}
4fb1b2
+#endif
4fb1b2
+
4fb1b2
 /*
4fb1b2
  * open_files - lock and open the group databases
4fb1b2
  */
4fb1b2
@@ -393,6 +460,7 @@ int main (int argc, char **argv)
4fb1b2
 
4fb1b2
 	const struct group *gr;
4fb1b2
 	struct group newgr;
4fb1b2
+	struct passwd *pw = NULL;
4fb1b2
 	int errors = 0;
4fb1b2
 	int line = 0;
4fb1b2
 
4fb1b2
@@ -408,8 +476,33 @@ int main (int argc, char **argv)
4fb1b2
 
4fb1b2
 	OPENLOG ("chgpasswd");
4fb1b2
 
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+	audit_help_open ();
4fb1b2
+#endif
4fb1b2
+
4fb1b2
+	/*
4fb1b2
+	 * Determine the name of the user that invoked this command. This
4fb1b2
+	 * is really hit or miss because there are so many ways that command
4fb1b2
+	 * can be executed and so many ways to trip up the routines that
4fb1b2
+	 * report the user name.
4fb1b2
+	 */
4fb1b2
+	pw = get_my_pwent ();
4fb1b2
+	if (NULL == pw) {
4fb1b2
+		fprintf (stderr, _("%s: Cannot determine your user name.\n"),
4fb1b2
+		         Prog);
4fb1b2
+		SYSLOG ((LOG_WARN,
4fb1b2
+		         "Cannot determine the user name of the caller (UID %lu)",
4fb1b2
+		         (unsigned long) getuid ()));
4fb1b2
+		exit (E_NOPERM);
4fb1b2
+	}
4fb1b2
+	myname = xstrdup (pw->pw_name);
4fb1b2
+
4fb1b2
 	check_perms ();
4fb1b2
 
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+	selinux_check_root ();
4fb1b2
+#endif
4fb1b2
+
4fb1b2
 #ifdef SHADOWGRP
4fb1b2
 	is_shadow_grp = sgr_file_present ();
4fb1b2
 #endif
4fb1b2
@@ -536,6 +629,15 @@ int main (int argc, char **argv)
4fb1b2
 			newgr.gr_passwd = cp;
4fb1b2
 		}
4fb1b2
 
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+		{
4fb1b2
+
4fb1b2
+			audit_logger_with_group (AUDIT_GRP_CHAUTHTOK, Prog,
4fb1b2
+		              "change-password",
4fb1b2
+		              myname, AUDIT_NO_ID, gr->gr_name,
4fb1b2
+		              SHADOW_AUDIT_SUCCESS);
4fb1b2
+		}
4fb1b2
+#endif
4fb1b2
 		/* 
4fb1b2
 		 * The updated group file entry is then put back and will
4fb1b2
 		 * be written to the group file later, after all the
4fb1b2
Index: shadow-4.5/src/chpasswd.c
4fb1b2
===================================================================
4fb1b2
--- shadow-4.5.orig/src/chpasswd.c
4fb1b2
+++ shadow-4.5/src/chpasswd.c
4fb1b2
@@ -39,6 +39,13 @@
4fb1b2
 #include <pwd.h>
4fb1b2
 #include <stdio.h>
4fb1b2
 #include <stdlib.h>
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+#include <selinux/selinux.h>
4fb1b2
+#include <selinux/avc.h>
4fb1b2
+#endif
4fb1b2
+#ifdef WITH_LIBAUDIT
4fb1b2
+#include <libaudit.h>
4fb1b2
+#endif
4fb1b2
 #ifdef USE_PAM
4fb1b2
 #include "pam_defs.h"
4fb1b2
 #endif				/* USE_PAM */
4fb1b2
@@ -297,6 +304,63 @@ static void check_perms (void)
4fb1b2
 #endif				/* USE_PAM */
4fb1b2
 }
4fb1b2
 
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+static int
4fb1b2
+log_callback (int type, const char *fmt, ...)
4fb1b2
+{
4fb1b2
+    int audit_fd;
4fb1b2
+    va_list ap;
4fb1b2
+
4fb1b2
+    va_start(ap, fmt);
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+    audit_fd = audit_open();
4fb1b2
+
4fb1b2
+    if (audit_fd >= 0) {
4fb1b2
+	char *buf;
4fb1b2
+
4fb1b2
+	if (vasprintf (&buf, fmt, ap) < 0)
4fb1b2
+		goto ret;
4fb1b2
+	audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
4fb1b2
+				   NULL, 0);
4fb1b2
+	audit_close(audit_fd);
4fb1b2
+	free(buf);
4fb1b2
+	goto ret;
4fb1b2
+    }
4fb1b2
+
4fb1b2
+#endif
4fb1b2
+    vsyslog (LOG_USER | LOG_INFO, fmt, ap);
4fb1b2
+ret:
4fb1b2
+    va_end(ap);
4fb1b2
+    return 0;
4fb1b2
+}
4fb1b2
+
4fb1b2
+static void
4fb1b2
+selinux_check_root (void)
4fb1b2
+{
4fb1b2
+    int status = -1;
4fb1b2
+    security_context_t user_context;
4fb1b2
+    union selinux_callback old_callback;
4fb1b2
+
4fb1b2
+    if (is_selinux_enabled() < 1)
4fb1b2
+	return;
4fb1b2
+
4fb1b2
+    old_callback = selinux_get_callback(SELINUX_CB_LOG);
4fb1b2
+    /* setup callbacks */
4fb1b2
+    selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
4fb1b2
+    if ((status = getprevcon(&user_context)) < 0) {
4fb1b2
+	selinux_set_callback(SELINUX_CB_LOG, old_callback);
4fb1b2
+	exit(1);
4fb1b2
+    }
4fb1b2
+
4fb1b2
+    status = selinux_check_access(user_context, user_context, "passwd", "passwd", NULL);
4fb1b2
+
4fb1b2
+    selinux_set_callback(SELINUX_CB_LOG, old_callback);
4fb1b2
+    freecon(user_context);
4fb1b2
+    if (status != 0 && security_getenforce() != 0)
4fb1b2
+	exit(1);
4fb1b2
+}
4fb1b2
+#endif
4fb1b2
+
4fb1b2
 /*
4fb1b2
  * open_files - lock and open the password databases
4fb1b2
  */
4fb1b2
@@ -405,8 +469,16 @@ int main (int argc, char **argv)
4fb1b2
 
4fb1b2
 	OPENLOG ("chpasswd");
4fb1b2
 
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+	audit_help_open ();
4fb1b2
+#endif
4fb1b2
+
4fb1b2
 	check_perms ();
4fb1b2
 
4fb1b2
+#ifdef WITH_SELINUX
4fb1b2
+	selinux_check_root ();
4fb1b2
+#endif
4fb1b2
+
4fb1b2
 #ifdef USE_PAM
4fb1b2
 	if (!use_pam)
4fb1b2
 #endif				/* USE_PAM */
4fb1b2
@@ -566,6 +638,11 @@ int main (int argc, char **argv)
4fb1b2
 			newpw.pw_passwd = cp;
4fb1b2
 		}
4fb1b2
 
4fb1b2
+#ifdef WITH_AUDIT
4fb1b2
+		audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
4fb1b2
+		              "updating-password",
4fb1b2
+		              pw->pw_name, (unsigned int) pw->pw_uid, 1);
4fb1b2
+#endif
4fb1b2
 		/* 
4fb1b2
 		 * The updated password file entry is then put back and will
4fb1b2
 		 * be written to the password file later, after all the
4fb1b2
Index: shadow-4.5/src/Makefile.am
4fb1b2
===================================================================
4fb1b2
--- shadow-4.5.orig/src/Makefile.am
4fb1b2
+++ shadow-4.5/src/Makefile.am
4fb1b2
@@ -87,9 +87,9 @@ chage_LDADD    = $(LDADD) $(LIBPAM_SUID)
4fb1b2
 newuidmap_LDADD    = $(LDADD) $(LIBSELINUX)
4fb1b2
 newgidmap_LDADD    = $(LDADD) $(LIBSELINUX)
4fb1b2
 chfn_LDADD     = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT_NOPAM) $(LIBSKEY) $(LIBMD)
4fb1b2
-chgpasswd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBSELINUX) $(LIBCRYPT)
4fb1b2
+chgpasswd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBSELINUX) $(LIBAUDIT) $(LIBCRYPT)
4fb1b2
 chsh_LDADD     = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT_NOPAM) $(LIBSKEY) $(LIBMD)
4fb1b2
-chpasswd_LDADD = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT)
4fb1b2
+chpasswd_LDADD = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBAUDIT) $(LIBCRYPT)
4fb1b2
 gpasswd_LDADD  = $(LDADD) $(LIBAUDIT) $(LIBSELINUX) $(LIBCRYPT)
4fb1b2
 groupadd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBAUDIT) $(LIBSELINUX)
4fb1b2
 groupdel_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBAUDIT) $(LIBSELINUX)