se_procmail
I have completed initial work on a wrapper for procmail to run it in a new security context. I've tested it along with spamassassin in some simple configurations. I've attached both se_procmail.c and a README file.
A number of modifications to the policy were needed. If you're tracking my arch tree, the required modifications are in patch-63 and patch-64. I've attached the diff associated with those patches, for convenience.
Now, I'm not quite sure where to put se_procmail. It doesn't seem quite right to put it in policycoreutils. But it also feels like overkill to put it in a separate package. Ideas?
se_procmail 0.1 - a SELinux procmail wrapper
* What is se_procmail?
It is a simple wrapper for the "procmail" program to execute it in a new security context.
* Why do I want it?
The modified policy files that go along with se_procmail run procmail in its own user-specialized domain, e.g. user_procmail_t. This allows for more security, and it allows us to cleanly define transitions such as from user_procmail_t to user_spamassassin_t.
* How do I install it?
First, you will need at least GCC 2.95 and glibc. You almost certainly have those already. You will also need the SELinux 2.5 development headers installed.
Important: se_procmail is NOT compatible with the old SELinux 2.4 API.
Compile it thus:
$ gcc -Wall -lselinux -o se_procmail se_procmail.c
Then, to install:
$ cp se_procmail /usr/bin $ chcon system_u:object_r:se_procmail_exec_t /usr/bin/se_procmail
* Setup
Next, you need to create a file /etc/security/mail_delivery_contexts. This file is a mapping from usernames to security contexts. Here's an example:
default system_u:system_r:user_procmail_t
jdoe system_u:system_r:jdoe_procmail_t
root system_u:system_r:staff_procmail_t
Note that the first line is special; it is the default value that will be used if no username matches.
* Future Ideas
If no username matches in mail_delivery_contexts, we might want an option to try computing a security context based simply on the username, e.g. $LOGNAME_procmail_t. This is so sites with a large number of users with individual procmail domains wouldn't have to list them all.
* Contact
se_procmail was written by Colin Walters <[email protected]>.
Local Variables:
mode: outline
End:
/* -*- mode: c; c-file-style: "gnu"; coding: utf-8 -*-
* Wrapper program to run procmail in a new security context *
* Copyright © 2003 Colin Walters <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*/
#define PROCMAIL_PATH "/usr/bin/procmail"
#define SE_PROCMAIL_CONFIG_PATH "/etc/security/mail_delivery_contexts"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <pwd.h>
#include <ctype.h>
#include <selinux/selinux.h>
#include <syslog.h>
void lose(const char *fmt, ...) __attribute__ ((noreturn, format(printf, 1, 2))); void xfree(void *p);
void *xmalloc(size_t size) __attribute__((malloc));
void *xrealloc(void *p, size_t size) __attribute__((malloc));
char *xstrdup(const char *s);
void run_procmail(security_context_t context, int argc, char *const argv[]) __attribute__ ((noreturn));
/*****************************
* Standard utility functions
*/
voidlose(const char *fmt, ...)
{
va_list args;
va_start(args, fmt); vsyslog(LOG_MAKEPRI(LOG_MAIL, LOG_CRIT), fmt, args); va_end(args);
exit(1);
}
/* Various random memory wrappers */
voidxfree(void *p)
{
if (p)
free(p); }
void *xmalloc(size_t size)
{
void *ret = malloc(size);
if (!ret)
lose("Out of memory!\n"); return ret;
}
void *xrealloc(void *p, size_t size)
{
void *ret = realloc(p, size);
if (!ret)
lose("Out of memory!\n"); return ret;
}
char *xstrdup(const char *s)
{
size_t count = strlen(s)+1;
char *ret = xmalloc(count);
return (char *) memcpy(ret, s, count);
}
/*****************************
* Main code
*/
voidrun_procmail(security_context_t context, int argc, char *const argv[]) {
char **newargv;
if (setexeccon(context) < 0)
lose("unable to set exec security context to \"%s\": %s",
context, strerror(errno));
newargv = xmalloc((argc+2) * sizeof(char *));
newargv[0] = PROCMAIL_PATH;
newargv[argc+1] = NULL;
memcpy(newargv+1,argv,argc * sizeof(char *)); syslog(LOG_MAKEPRI(LOG_MAIL,LOG_DEBUG), "execing \"%s\" in context \"%s\"", PROCMAIL_PATH, context); execv(PROCMAIL_PATH, newargv);
lose("unable to exec procmail: %s", strerror(errno));
}
intmain(int argc, char **argv)
{
struct passwd *pwent;
uid_t uid;
const char *logname;
char *defcontext, *p;
FILE *f;
char *line = NULL;
ssize_t len = 0;
ssize_t bytesread = 0;
openlog("se_procmail", 0, LOG_MAIL);
logname = getenv("LOGNAME");
if (!logname) {
uid = getuid();
pwent = getpwuid(uid);
if (!pwent)
lose("unable to determine login name");
logname = pwent->pw_name;
}
if ((f = fopen(SE_PROCMAIL_CONFIG_PATH, "r")) == NULL)
lose("unable to open %s: %s", SE_PROCMAIL_CONFIG_PATH, strerror(errno)); if (getline(&line, &len, f) < 0)
lose("unable to determine mail delivery security context for \"%s\"\n", logname); p = line;
while (*p && !isspace (*p))
p++;
if (!*p)
goto syntaxerr;
while (*p && isspace (*p))
p++;
if (!*p)
goto syntaxerr;
defcontext = xstrdup (p);
defcontext[strlen(defcontext)-1] = '\0'; /* trim newline */
xfree(line); line = NULL;
len = 0;
while ((bytesread = getline(&line, &len, f)) > 0) {
char *user, *context;
p = line;
while (*p && !isspace (*p))
p++;
if (!*p)
goto syntaxerr;
*p = '\0';
user = line;
if (!strcmp(logname, user)) {
p++;
while (*p && isspace (*p))
p++;
if (!*p)
goto syntaxerr;
context = p;
context[strlen(context)-1] = '\0'; /* trim newline */
fclose(f);
run_procmail (context, argc-1, argv+1);
}
xfree(line);
line = NULL;
len = 0; }
fclose(f);
run_procmail (defcontext, argc-1, argv+1);
syntaxerr:
lose("syntax error while parsing %s\n", SE_PROCMAIL_CONFIG_PATH);
}
domains/program/unused/postfix.te +0 -8
@@ -138,11 +138,6 @@ allow { postfix_smtp_t postfix_smtpd_t } postfix_prng_t:file rw_file_perms; postfix_server_domain(local, `, mta_delivery_agent') -ifdef(`procmail.te', ` -domain_auto_trans(postfix_local_t, procmail_exec_t, procmail_t) -# for a bug in the postfix local program -dontaudit procmail_t postfix_local_t:tcp_socket { read write }; -') allow postfix_local_t etc_aliases_t:file r_file_perms; allow postfix_local_t self:fifo_file rw_file_perms; allow postfix_local_t postfix_spool_t:file rw_file_perms;
@@ -264,6 +259,3 @@ allow postfix_pipe_t postfix_pipe_t:fifo_file { read write }; allow postfix_pipe_t postfix_private_t:dir search; allow postfix_pipe_t postfix_private_t:sock_file write; -ifdef(`procmail.te', ` -domain_auto_trans(postfix_pipe_t, procmail_exec_t, procmail_t) -')
domains/program/unused/procmail.te +17 -47
73 lines not on this page · raw message
domains/program/unused/sendmail.te +0 -6
@@ -56,12 +56,6 @@ dontaudit sendmail_t sysadm_home_dir_t:dir { getattr search }; dontaudit system_mail_t sysadm_home_dir_t:dir { getattr search }; -# Run procmail in its own domain, if defined. -ifdef(`procmail.te',` -domain_auto_trans(sendmail_t, procmail_exec_t, procmail_t) -domain_auto_trans(system_mail_t, procmail_exec_t, procmail_t) -') - # sendmail -q allow system_mail_t mqueue_spool_t:dir rw_dir_perms; allow system_mail_t mqueue_spool_t:file create_file_perms;
file_contexts/program/procmail.fc +1 -0
@@ -1,2 +1,3 @@ # procmail /usr/bin/procmail system_u:object_r:procmail_exec_t +/usr/bin/se_procmail system_u:object_r:se_procmail_exec_t
macros/program/mta_macros.te +0 -2
@@ -57,8 +57,6 @@ # Execute procmail. can_exec($1_mail_t, bin_t) -ifdef(`procmail.te',` -can_exec($1_mail_t, procmail_exec_t)') ifelse(`$1', `system', ` # Read cron temporary files.
macros/program/procmail_macros.te +84 -0
84 lines not on this page · raw message
macros/program/spamassassin_macros.te +7 -0
@@ -33,6 +33,8 @@ domain_auto_trans($1_t, $2_exec_t, $1_$2_t) role $1_r types $1_$2_t; +# Spamassassin can be executed by procmail, which is in system_r +role system_r types $1_$2_t; general_domain_access($1_$2_t) base_file_read_access($1_$2_t)
@@ -42,6 +44,11 @@ dontaudit $1_$2_t var_t:dir search; allow $1_$2_t privfd:fd use; allow $1_$2_t { $1_devpts_t $1_tty_device_t }:chr_file rw_file_perms; + +ifdef(`procmail.te',` +domain_auto_trans($1_procmail_t, $2_exec_t, $1_$2_t) +') + ') dnl end spamassassin_program_domain ###
macros/user_macros.te +1 -0
@@ -167,6 +167,7 @@ ifdef(`sendmail.te', `sendmail_user_domain($1)') ifdef(`crontab.te', `crontab_domain($1)') ifdef(`ssh.te', `ssh_domain($1)') +ifdef(`procmail.te', `procmail_domain($1)') ifdef(`irc.te', `irc_domain($1)') ifdef(`using_spamassassin', `spamassassin_domain($1)') ifdef(`uml.te', `uml_domain($1)')