selinux-access.c revision 086891e5c119abb9854237fc32e736fe2d67234c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Dan Walsh
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "selinux-access.h"
#ifdef HAVE_SELINUX
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#ifdef HAVE_AUDIT
#include <libaudit.h>
#endif
#include "sd-bus.h"
#include "bus-util.h"
#include "util.h"
#include "log.h"
#include "audit.h"
#include "selinux-util.h"
#include "audit-fd.h"
#include "strv.h"
static bool initialized = false;
struct audit_info {
const char *path;
const char *cmdline;
};
/*
Any time an access gets denied this callback will be called
with the audit data. We then need to just copy the audit data into the msgbuf.
*/
static int audit_callback(
void *auditdata,
char *msgbuf,
size_t msgbufsize) {
"auid=%s uid=%s gid=%s%s%s%s%s%s%s",
return 0;
}
/*
Any time an access gets denied this callback will be called
code copied from dbus. If audit is turned on the messages will go as
sent to syslog.
*/
#ifdef HAVE_AUDIT
if (get_audit_fd() >= 0) {
int r;
if (r >= 0) {
return 0;
}
}
#endif
return 0;
}
/*
Function must be called once to initialize the SELinux AVC environment.
Sets up callbacks.
If you want to cleanup memory you should need to call selinux_access_finish.
*/
static int access_init(void) {
int r = 0;
log_error("avc_open() failed: %m");
return -errno;
}
if (security_getenforce() < 0){
r = -errno;
avc_destroy();
}
return r;
}
int r;
if (initialized)
return 0;
if (!mac_selinux_use())
return 0;
r = access_init();
if (r < 0)
initialized = true;
return 0;
}
#endif
void mac_selinux_access_free(void) {
#ifdef HAVE_SELINUX
if (!initialized)
return;
avc_destroy();
initialized = false;
#endif
}
/*
This function communicates with the kernel to check whether or not it should
allow the access.
If the machine is in permissive mode it will return ok. Audit messages will
still be generated if the access would be denied in enforcing mode.
*/
const char *path,
const char *permission,
sd_bus_error *error) {
#ifdef HAVE_SELINUX
struct audit_info audit_info = {};
int r = 0;
if (!mac_selinux_use())
return 0;
r = mac_selinux_access_init(error);
if (r < 0)
return r;
SD_BUS_CREDS_AUGMENT /* get more bits from /proc */,
&creds);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (path) {
/* Get the file context of the unit file */
if (r < 0) {
r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get file context on %s.", path);
goto finish;
}
tclass = "service";
} else {
if (r < 0) {
goto finish;
}
tclass = "system";
}
if (r < 0)
log_debug("SELinux access check scon=%s tcon=%s tclass=%s perm=%s path=%s cmdline=%s: %i", scon, fcon, tclass, permission, path, cl, r);
if (r < 0 && security_getenforce() != 1) {
r = 0;
}
return r;
#else
return 0;
#endif
}
int mac_selinux_unit_access_check_strv(char **units,
Manager *m,
const char *permission,
sd_bus_error *error) {
#ifdef HAVE_SELINUX
char **i;
Unit *u;
int r;
STRV_FOREACH(i, units) {
u = manager_get_unit(m, *i);
if (u) {
if (r < 0)
return r;
}
}
#endif
return 0;
}