selinux-access.c revision c3090674833c8bd34fbdb0e743f1c47d85dd14fb
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "job.h"
#include "manager.h"
#include "selinux-access.h"
#ifdef HAVE_SELINUX
#include "dbus.h"
#include "log.h"
#include "dbus-unit.h"
#include "bus-errors.h"
#include "dbus-common.h"
#include "audit.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_AUDIT
#include <libaudit.h>
#endif
#include <limits.h>
/* FD to send audit messages to */
static int audit_fd = -1;
static int selinux_enabled = -1;
static int first_time = 1;
static int selinux_enforcing = 0;
struct auditstruct {
const char *path;
char *cmdline;
};
/*
Define a mapping between the systemd method calls and the SELinux access to check.
We define two tables, one for access checks on unit files, and one for
access checks for the system in general.
If we do not find a match in either table, then the "undefined" system
check will be called.
*/
{ "EnableUnitFiles", "enable" },
{ "GetUnit", "status" },
{ "GetUnitFileState", "status" },
{ "Kill", "stop" },
{ "KillUnit", "stop" },
{ "LinkUnitFiles", "enable" },
{ "MaskUnitFiles", "disable" },
{ "PresetUnitFiles", "enable" },
{ "ReenableUnitFiles", "enable" },
{ "Reexecute", "start" },
{ "ReloadOrRestart", "start" },
{ "ReloadOrRestartUnit", "start" },
{ "ReloadOrTryRestart", "start" },
{ "ReloadOrTryRestartUnit", "start" },
{ "ReloadUnit", "reload" },
{ "ResetFailedUnit", "stop" },
{ "Restart", "start" },
{ "RestartUnit", "start" },
{ "Start", "start" },
{ "StartUnit", "start" },
{ "StartUnitReplace", "start" },
{ "Stop", "stop" },
{ "StopUnit", "stop" },
{ "TryRestart", "start" },
{ "TryRestartUnit", "start" },
{ "UnmaskUnitFiles", "enable" },
};
{ "CreateSnapshot", "status" },
{ "Dump", "status" },
{ "Exit", "halt" },
{ "FlushDevices", "halt" },
{ "Get", "status" },
{ "GetAll", "status" },
{ "GetJob", "status" },
{ "GetSeat", "status" },
{ "GetSession", "status" },
{ "GetSessionByPID", "status" },
{ "GetUnitByPID", "status" },
{ "GetUser", "status" },
{ "Halt", "halt" },
{ "Introspect", "status" },
{ "KExec", "reboot" },
{ "KillSession", "halt" },
{ "KillUser", "halt" },
{ "LoadUnit", "reload" },
{ "ListJobs", "status" },
{ "ListSeats", "status" },
{ "ListSessions", "status" },
{ "ListUnits", "status" },
{ "ListUnitFiles", "status" },
{ "ListUsers", "status" },
{ "LockSession", "halt" },
{ "PowerOff", "halt" },
{ "Reboot", "reboot" },
{ "Reload", "reload" },
{ "Reexecute", "reload" },
{ "ResetFailed", "reload" },
{ "Subscribe", "status" },
{ "SwithcRoot", "reboot" },
{ "SetEnvironment", "status" },
{ "SetUserLinger", "halt" },
{ "TerminateSeat", "halt" },
{ "TerminateSession", "halt" },
{ "TerminateUser", "halt" },
{ "Unsubscribe", "status" },
{ "UnsetEnvironment", "status" },
{ "UnsetAndSetEnvironment", "status" },
};
/*
If the admin toggles the selinux enforcment mode this callback
will get called before the next access check
*/
static int setenforce_callback(int enforcing)
{
return 0;
}
/* This mimics dbus_bus_get_unix_user() */
static int bus_get_selinux_security_context(
const char *name,
char **scon,
int r;
"GetConnectionSELinuxSecurityContext");
if (!m) {
r = -errno;
goto finish;
}
m,
if (!r) {
r = -errno;
goto finish;
}
if (!reply) {
r = -errno;
goto finish;
}
if (!r) {
r = -errno;
goto finish;
}
if (!r) {
r = -errno;
goto finish;
}
r = 0;
if (m)
if (reply)
return r;
}
/* This mimics dbus_bus_get_unix_user() */
static int bus_get_audit_data(
const char *name,
struct auditstruct *audit,
int r;
if (pid <= 0)
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
/*
Any time an access gets denied this callback will be called
with the aduit data. We then need to just copy the audit data into the msgbuf.
*/
{
"name=\"%s\" cmdline=\"%s\" auid=%d uid=%d gid=%d",
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 (audit_fd >= 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 = -1;
return -errno;
}
if ((r = security_getenforce()) >= 0) {
return 0;
}
r = -errno;
avc_destroy();
return r;
}
int r;
#ifdef HAVE_AUDIT
#endif
if (!first_time)
return 0;
if (selinux_enabled < 0)
if (selinux_enabled) {
/* if not first time is not set, then initialize access */
r = access_init();
if (r < 0) {
return r;
}
first_time = 0;
}
return 0;
}
static int get_audit_data(
struct auditstruct *audit,
const char *sender;
int r;
if (sender)
else {
int fd;
return -EINVAL;
if (r < 0) {
log_error("Failed to determine peer credentials: %m");
return -errno;
}
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
}
/*
This function returns the security context of the remote end of the dbus
connections. Whether it is on the bus or a local connection.
*/
static int get_calling_context(
const char *sender;
int r;
/*
If sender exists then
if sender is NULL this indicates a local connection. Grab the fd
from dbus and do an getpeercon to peers process context
*/
if (sender) {
if (r < 0)
return -EINVAL;
} else {
int fd;
if (! r)
return -EINVAL;
if (r < 0)
return -errno;
}
return 0;
}
/*
This function returns the SELinux permission to check and whether or not the
check requires a unit file.
*/
{
int i;
*require_unit = -1;
for (i = 0; unit_methods[i][0]; i++) {
*require_unit = 1;
break;
}
}
if (*require_unit < 0) {
for (i = 0; system_methods[i][0]; i++) {
*require_unit = 0;
break;
}
}
}
if (*require_unit < 0) {
*require_unit = 0;
*perm = "undefined";
}
}
/*
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.
*/
static int selinux_access_check(DBusConnection *connection, DBusMessage *message, Manager *m, DBusError *error, const char *perm, const char *path) {
int r = 0;
struct auditstruct audit;
if (r != 0)
goto finish;
if (path) {
tclass = "service";
/* get the file context of the unit file */
if (r < 0) {
goto finish;
}
} else {
tclass = "system";
if (r < 0) {
dbus_set_error(error, BUS_ERROR_ACCESS_DENIED, "Unable to get current context, SELinux policy denies access.");
goto finish;
}
}
errno= 0;
if (r < 0) {
r = -errno;
}
log_debug("SELinux checkaccess scon %s tcon %s tclass %s perm %s path %s: %d", scon, fcon, tclass, perm, path, r);
if (r)
r = -errno;
return r;
}
/*
Clean up memory allocated in selinux_avc_init
*/
void selinux_access_finish(void) {
if (!first_time)
avc_destroy();
first_time = 1;
}
int selinux_unit_access_check(DBusConnection *connection, DBusMessage *message, Manager *m, const char *path, DBusError *error) {
const char *perm;
int require_unit;
const char *member;
int r;
r = selinux_init(m, error);
if (r < 0)
return r;
if (! selinux_enabled)
return 0;
if (r < 0 && !selinux_enforcing) {
r = 0;
}
return r;
}
int selinux_manager_access_check(DBusConnection *connection, DBusMessage *message, Manager *m, DBusError *error) {
int r = -1;
const char *member;
int require_unit;
const char *perm;
r = selinux_init(m, error);
if (r < 0)
return r;
if (! selinux_enabled)
return 0;
if (require_unit) {
const char *name;
Unit *u;
if (!dbus_message_get_args(
r = -EINVAL;
goto finish;
}
if (r < 0) {
goto finish;
}
}
/* if SELinux is in permissive mode return 0 */
if (r && (!selinux_enforcing)) {
r = 0;
}
return r;
}
#else
int selinux_unit_access_check(DBusConnection *connection, DBusMessage *message, Manager *m, const char *path, DBusError *error) {
return 0;
}
int selinux_manager_access_check(DBusConnection *connection, DBusMessage *message, Manager *m, DBusError *error) {
return 0;
}
void selinux_access_finish(void) {
}
#endif