From 4c2d1c67cea075107aadaa6d81fe456687c69e67 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Fri, 17 Jun 2005 12:44:30 +0000 Subject: Manoj Srivastava: - Added SELinux capability, and turned it on be default. Added restorecon calls in preinst and postinst (should not matter if the machine is not SELinux aware). By and large, the changes made should have no effect unless the rules file calls --with-selinux; and even then there should be no performance hit for machines not actively running SELinux. - Modified the preinst and postinst to call restorecon to set the security context for the generated public key files. - Added a comment to /etc/pam.d/ssh to indicate that an SELinux system may want to also include pam_selinux.so. --- selinux.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 selinux.c (limited to 'selinux.c') diff --git a/selinux.c b/selinux.c new file mode 100644 index 000000000..697c2910a --- /dev/null +++ b/selinux.c @@ -0,0 +1,111 @@ +#include "includes.h" + +#include "auth.h" +#include "log.h" + +#ifdef WITH_SELINUX + +#include +#include +#include +#include +#include + +extern Authctxt *the_authctxt; + +static const security_context_t +selinux_get_user_context(const char *name) +{ + security_context_t user_context = NULL; + char *role = NULL; + int ret = 0; + + if (the_authctxt) + role = the_authctxt->role; + if (role != NULL && role[0]) + ret = get_default_context_with_role(name, role, NULL, + &user_context); + else + ret = get_default_context(name, NULL, &user_context); + if (ret < 0) { + if (security_getenforce() > 0) + fatal("Failed to get default security context for %s.", + name); + else + error("Failed to get default security context for %s. " + "Continuing in permissive mode", + name); + } + return user_context; +} + +void +setup_selinux_pty(const char *name, const char *tty) +{ + security_context_t new_tty_context, user_context, old_tty_context; + + if (is_selinux_enabled() <= 0) + return; + + new_tty_context = old_tty_context = NULL; + user_context = selinux_get_user_context(name); + + if (getfilecon(tty, &old_tty_context) < 0) { + error("getfilecon(%.100s) failed: %.100s", + tty, strerror(errno)); + } else { + if (security_compute_relabel(user_context, old_tty_context, + SECCLASS_CHR_FILE, &new_tty_context) != 0) { + error("security_compute_relabel(%.100s) failed: " + "%.100s", tty, strerror(errno)); + } else { + if (setfilecon(tty, new_tty_context) != 0) + error("setfilecon(%.100s, %s) failed: %.100s", + tty, new_tty_context, strerror(errno)); + freecon(new_tty_context); + } + freecon(old_tty_context); + } + if (user_context) + freecon(user_context); +} + +void +setup_selinux_exec_context(const char *name) +{ + security_context_t user_context; + + if (is_selinux_enabled() <= 0) + return; + + user_context = selinux_get_user_context(name); + + if (setexeccon(user_context)) { + if (security_getenforce() > 0) + fatal("Failed to set exec security context %s for %s.", + user_context, name); + else + error("Failed to set exec security context %s for %s. " + "Continuing in permissive mode", + user_context, name); + } + if (user_context) + freecon(user_context); +} + +#else /* WITH_SELINUX */ + +void +setup_selinux_pty(const char *name, const char *tty) +{ + (void) name; + (void) tty; +} + +void +setup_selinux_exec_context(const char *name) +{ + (void) name; +} + +#endif /* WITH_SELINUX */ -- cgit v1.2.3