Blame SOURCES/at-3.1.13-selinux.patch

20eab7
diff -up at-3.1.13/atd.c.selinux at-3.1.13/atd.c
20eab7
--- at-3.1.13/atd.c.selinux	2012-11-01 15:11:21.368772308 +0100
20eab7
+++ at-3.1.13/atd.c	2012-11-01 15:13:16.809162818 +0100
20eab7
@@ -83,6 +83,14 @@
20eab7
 #include "getloadavg.h"
20eab7
 #endif
20eab7
 
20eab7
+#ifdef WITH_SELINUX
20eab7
+#include <selinux/selinux.h>
20eab7
+#include <selinux/get_context_list.h>
20eab7
+int selinux_enabled=0;
20eab7
+#include <selinux/flask.h>
20eab7
+#include <selinux/av_permissions.h>
20eab7
+#endif
20eab7
+
20eab7
 #ifndef LOG_ATD
20eab7
 #define LOG_ATD        LOG_DAEMON
20eab7
 #endif
20eab7
@@ -202,6 +210,68 @@ myfork()
20eab7
 #define ATD_MAIL_NAME    "mailx"
20eab7
 #endif
20eab7
 
20eab7
+#ifdef WITH_SELINUX
20eab7
+static int set_selinux_context(const char *name, const char *filename) {
20eab7
+       security_context_t user_context=NULL;
20eab7
+       security_context_t  file_context=NULL;
20eab7
+       struct av_decision avd;
20eab7
+       int retval=-1;
20eab7
+       char *seuser=NULL;
20eab7
+       char *level=NULL;
20eab7
+
20eab7
+       if (getseuserbyname(name, &seuser, &level) == 0) {
20eab7
+               retval=get_default_context_with_level(seuser, level, NULL, &user_context);
20eab7
+               free(seuser);
20eab7
+               free(level);
20eab7
+               if (retval) {
20eab7
+                       if (security_getenforce()==1) {
20eab7
+                               perr("execle: couldn't get security context for user %s\n", name);
20eab7
+                       } else {
20eab7
+                               syslog(LOG_ERR, "execle: couldn't get security context for user %s\n", name);
20eab7
+                               return -1;
20eab7
+                       }
20eab7
+               }
20eab7
+       }
20eab7
+
20eab7
+       /*
20eab7
+       * Since crontab files are not directly executed,
20eab7
+       * crond must ensure that the crontab file has
20eab7
+       * a context that is appropriate for the context of
20eab7
+       * the user cron job.  It performs an entrypoint
20eab7
+       * permission check for this purpose.
20eab7
+       */
20eab7
+       if (fgetfilecon(STDIN_FILENO, &file_context) < 0)
20eab7
+               perr("fgetfilecon FAILED %s", filename);
20eab7
+
20eab7
+       retval = security_compute_av(user_context,
20eab7
+                                    file_context,
20eab7
+                                    SECCLASS_FILE,
20eab7
+                                    FILE__ENTRYPOINT,
20eab7
+                                    &avd);
20eab7
+       freecon(file_context);
20eab7
+       if (retval || ((FILE__ENTRYPOINT & avd.allowed) != FILE__ENTRYPOINT)) {
20eab7
+               if (security_getenforce()==1) {
20eab7
+                       perr("Not allowed to set exec context to %s for user  %s\n", user_context,name);
20eab7
+               } else {
20eab7
+                       syslog(LOG_ERR, "Not allowed to set exec context to %s for user  %s\n", user_context,name);
20eab7
+                       retval = -1;
20eab7
+                       goto err;
20eab7
+               }
20eab7
+       }
20eab7
+       if (setexeccon(user_context) < 0) {
20eab7
+               if (security_getenforce()==1) {
20eab7
+                       perr("Could not set exec context to %s for user  %s\n", user_context,name);
20eab7
+                       retval = -1;
20eab7
+               } else {
20eab7
+                       syslog(LOG_ERR, "Could not set exec context to %s for user  %s\n", user_context,name);
20eab7
+               }
20eab7
+       }
20eab7
+  err:
20eab7
+       freecon(user_context);
20eab7
+       return 0;
20eab7
+}
20eab7
+#endif
20eab7
+
20eab7
 static void
20eab7
 run_file(const char *filename, uid_t uid, gid_t gid)
20eab7
 {
20eab7
@@ -446,9 +516,23 @@ run_file(const char *filename, uid_t uid
20eab7
 		perr("Cannot reset signal handler to default");
20eab7
 
20eab7
 	    chdir("/");
20eab7
-
20eab7
+#ifdef WITH_SELINUX
20eab7
+            if (selinux_enabled > 0) {
20eab7
+                if (set_selinux_context(pentry->pw_name, filename) < 0)
20eab7
+                       perr("SELinux Failed to set context\n");
20eab7
+            }
20eab7
+#endif
20eab7
 	    if (execle("/bin/sh", "sh", (char *) NULL, nenvp) != 0)
20eab7
 		perr("Exec failed for /bin/sh");
20eab7
+//add for fedora
20eab7
+#ifdef WITH_SELINUX
20eab7
+               if (selinux_enabled>0)
20eab7
+                       if (setexeccon(NULL) < 0)
20eab7
+                               if (security_getenforce()==1)
20eab7
+                                       perr("Could not resset exec context for user %s\n", pentry->pw_name);
20eab7
+#endif
20eab7
+//end
20eab7
+//add for fedora
20eab7
 #ifdef  WITH_PAM
20eab7
 	    if ( ( nenvp != &nul ) && (pam_envp != 0L)  && (*pam_envp != 0L))
20eab7
 	    {
20eab7
@@ -751,6 +835,10 @@ main(int argc, char *argv[])
20eab7
     struct passwd *pwe;
20eab7
     struct group *ge;
20eab7
 
20eab7
+#ifdef WITH_SELINUX
20eab7
+    selinux_enabled=is_selinux_enabled();
20eab7
+#endif
20eab7
+
20eab7
 /* We don't need root privileges all the time; running under uid and gid
20eab7
  * daemon is fine.
20eab7
  */
20eab7
diff -up at-3.1.13/config.h.in.selinux at-3.1.13/config.h.in
20eab7
--- at-3.1.13/config.h.in.selinux	2012-11-01 15:11:21.368772308 +0100
20eab7
+++ at-3.1.13/config.h.in	2012-11-01 15:11:21.371772392 +0100
20eab7
@@ -71,6 +71,9 @@
20eab7
 /* Define if you are building with_pam */
20eab7
 #undef WITH_PAM
20eab7
 
20eab7
+/* Define if you are building with_selinux  */
20eab7
+#undef WITH_SELINUX
20eab7
+
20eab7
 /* Define to 1 if you have the `pstat_getdynamic' function. */
20eab7
 #undef HAVE_PSTAT_GETDYNAMIC
20eab7
 
20eab7
diff -up at-3.1.13/configure.ac.selinux at-3.1.13/configure.ac
20eab7
--- at-3.1.13/configure.ac.selinux	2012-11-01 15:11:21.369772335 +0100
20eab7
+++ at-3.1.13/configure.ac	2012-11-01 15:11:21.372772420 +0100
20eab7
@@ -266,5 +266,13 @@ AC_ARG_WITH(daemon_groupname,
20eab7
 )
20eab7
 AC_SUBST(DAEMON_GROUPNAME)
20eab7
 
20eab7
+AC_ARG_WITH(selinux,
20eab7
+[ --with-selinux       Define to run with selinux],
20eab7
+AC_DEFINE(WITH_SELINUX),
20eab7
+)
20eab7
+AC_CHECK_LIB(selinux, is_selinux_enabled, SELINUXLIB=-lselinux)
20eab7
+AC_SUBST(SELINUXLIB)
20eab7
+AC_SUBST(WITH_SELINUX)
20eab7
+
20eab7
 AC_CONFIG_FILES(Makefile atrun atd.8 atrun.8 at.1 at.allow.5 batch)
20eab7
 AC_OUTPUT
20eab7
diff -up at-3.1.13/Makefile.in.selinux at-3.1.13/Makefile.in
20eab7
--- at-3.1.13/Makefile.in.selinux	2012-11-01 15:11:21.361772115 +0100
20eab7
+++ at-3.1.13/Makefile.in	2012-11-01 15:11:21.372772420 +0100
20eab7
@@ -39,6 +39,8 @@ LIBS		= @LIBS@
20eab7
 LIBOBJS		= @LIBOBJS@
20eab7
 INSTALL		= @INSTALL@
20eab7
 PAMLIB          = @PAMLIB@
20eab7
+SELINUXLIB      = @SELINUXLIB@
20eab7
+
20eab7
 
20eab7
 CLONES		= atq atrm
20eab7
 ATOBJECTS	= at.o panic.o perm.o posixtm.o y.tab.o lex.yy.o