Blame SOURCES/at-3.1.13-selinux.patch

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