Skip to contentSkip to conversation

port of util-linux login patch to shadow-utils login.c

PATCH Colin Walters 1 participant 1 message Jul 10, 2003 SELinux security module

lore
Share & tools
lore
#0 lore raw

Hello,

I've ported the SELinux 2.5 patch against the login in util-linux to the login.c in shadow-utils (which is what Debian uses). This patch could probably just be appended to the SE patch for shadow-utils.

I've tested it and it appears to work.

src/login.c +120 -0
@@ -63,6 +63,17 @@
 #define MAXHOSTNAMELEN 64
 #endif
 
+#ifdef WITH_SELINUX
+#include <selinux/get_context_list.h>
+#include <selinux/flask.h>
+#include <selinux/selinux.h>
+
+security_context_t user_context=NULL;
+security_context_t ttyn_context=NULL;  /* The current context of ttyn device */
+int selinux_enabled=0;
+
+#endif
+
 #ifdef RADIUS
 /*
  * Support for RADIUS authentication based on a hacked util-linux login
@@ -164,6 +175,46 @@
 static struct faillog faillog;
 #endif
 
+#ifdef WITH_SELINUX
+
+static security_context_t security_label_tty(char *tty, security_context_t usercon) {
+  security_context_t newdev_context=NULL; /* The new context of a device */
+  security_context_t prev_context=NULL; /* The new context of a device */
+  if (getfilecon(tty, &prev_context) < 0) 
+    {
+      fprintf(stderr, _("Warning!  Could not get current context for %s, not relabeling.\n"), tty);
+      syslog(LOG_NOTICE, _("Warning!  Could not get current context for %s, not relabeling.\n"), tty);
+      return NULL;
+    }	
+  if( security_compute_relabel(usercon,prev_context,SECCLASS_CHR_FILE,&newdev_context)!=0) {
+    fprintf(stderr, _("Warning!  Could not get new context for %s, not relabeling.\n"), tty);
+    syslog(LOG_NOTICE, _("Warning!  Could not get new context for %s, not relabeling.\n"), tty);
+      return NULL;
+  }
+  if (setfilecon(tty,newdev_context)) 
+    {
+      fprintf(stderr, _("Warning!  Could not relabel %s with %s, not relabeling.\n"), tty,newdev_context);
+      syslog(LOG_NOTICE, _("Warning!  Could not relabel %s with %s, not relabeling.\n"), tty,newdev_context);
+      return NULL;
+    }
+  freecon(newdev_context);
+  return prev_context;
+}
+
+static void security_restorelabel_tty(char *tty,security_context_t context) {
+  if (context==NULL)
+    return;
+
+  if (setfilecon(tty,context)) 
+    {
+      fprintf(stderr, _("Warning!  Could not relabel %s with %s, not relabeling.\n"), tty,context);
+      syslog(LOG_NOTICE, _("Warning!  Could not relabel %s with %s, not relabeling.\n"), tty,context);
+    }
+  freecon(context);
+  return;
+}
+#endif /* WITH_SELINUX */
+
 /* local function prototypes */
 static void usage (void);
 static void setup_tty (void);
@@ -176,6 +227,7 @@
 static void init_env (void);
 static RETSIGTYPE alarm_handler (int);
 
+
 /*
  * usage - print login command usage and exit
  *
@@ -892,6 +944,33 @@
 		else
 		        pwent = *pwd;
 
+#ifdef WITH_SELINUX
+		/* Make sure SELINUX is really running on this system */
+		if ((selinux_enabled = is_selinux_enabled())) {
+			int ret;
+			security_context_t* contextlist;
+			int num_contexts = get_ordered_context_list(pam_user, 0, &contextlist);
+			if (num_contexts > 0) {
+				ret = query_user_context(contextlist, &user_context);
+				freeconary(contextlist);
+				
+				if (ret < 0) {
+					fprintf(stderr, "%s:  query_user_context failed\n", argv[0]);
+					syslog (LOG_ERR,  "%s:  query_user_context failed\n", argv[0]);
+					exit (1);
+				}
+				
+			} else {
+				fprintf(stderr, "login: unable to obtain context for %s.\n", pam_user);
+				ret = manual_user_enter_context(pam_user, &user_context);
+				if (ret < 0) {
+					syslog (LOG_ERR, "UNABLE TO GET VALID CONTEXT FOR %s", username);
+					exit (1);
+				}
+			}
+		}
+#endif /* WITH_SELINUX */
+
 		retcode = pam_setcred (pamh, PAM_ESTABLISH_CRED);
 		PAM_FAIL_CHECK;
 
@@ -943,6 +1022,7 @@
 		} else {
 			pwent = *pwd;
 		}
+
 #ifndef USE_PAM
 #ifdef SHADOWPWD
 		spwd = NULL;
@@ -1241,6 +1321,11 @@
 	setup_limits (&pwent);	/* nice, ulimit etc. */
 #endif				/* ! USE_PAM */
 	chown_tty (tty, &pwent);
+#ifdef WITH_SELINUX
+	if (is_selinux_enabled) {
+		ttyn_context = security_label_tty (tty, user_context);
+	}
+#endif
 
 #ifdef LOGIN_FBTAB
 	/*
@@ -1378,6 +1463,18 @@
 	signal (SIGALRM, SIG_DFL);	/* default alarm signal */
 	signal (SIGHUP, SIG_DFL);	/* added this.  --marekm */
 
+#ifdef WITH_SELINUX
+	if (selinux_enabled) {
+		if (setexeccon(user_context)) {
+			fprintf(stderr, _("Error!  Unable to set executable context %s.\n"), user_context);
+			syslog(LOG_ERR, _("Error!  Unable to set executable context %s.\n"), user_context);
+			exit(3); 
+		}
+		freecon(user_context);
+	}
+#endif
+	
+
 #ifdef USE_PAM
 	/*
 	 * We must fork before setuid() because we need to call
@@ -1407,6 +1504,10 @@
 			 */
 			wait (NULL);
 			PAM_END;
+#ifdef WITH_SELINUX
+			fprintf(stderr, "restoring tty %s with context %s\n", tty, ttyn_context);
+			security_restorelabel_tty (tty, ttyn_context);
+#endif
 			exit (0);
 		}
 		/* child */
@@ -1422,6 +1523,25 @@
 #ifdef	SHADOWGRP
 	endsgent ();	/* stop access to shadow group file */
 #endif
+#ifdef WITH_SELINUX
+	if (selinux_enabled) {
+		if (pwd->pw_uid == 0) {
+			if (hostname)
+				syslog(LOG_NOTICE, _("ROOT LOGIN ON %s FROM %s USING %s"),
+				       tty, fromhost, user_context);
+			else
+				syslog(LOG_NOTICE, _("ROOT LOGIN ON %s USING %s"),
+				       tty, user_context);
+		} else {
+			if (hostname) 
+				syslog(LOG_INFO, _("LOGIN ON %s BY %s FROM %s USING %s"),
+				       tty, pam_user, fromhost, user_context);
+			else 
+				syslog(LOG_INFO, _("LOGIN ON %s BY %s USING %s"),
+				       tty, pam_user, user_context);
+		}
+	} else
+#endif
 	if (pwent.pw_uid == 0)
 		SYSLOG ((LOG_NOTICE, "ROOT LOGIN %s", fromhost));
 	else if (getdef_bool ("LOG_OK_LOGINS"))