selinux-access.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
/*-*- 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 <errno.h>
#ifdef HAVE_AUDIT
#include <libaudit.h>
#endif
#include "sd-bus.h"
#include "alloc-util.h"
#include "audit-fd.h"
#include "bus-util.h"
#include "log.h"
#include "path-util.h"
#include "selinux-util.h"
#include "stdio-util.h"
#include "strv.h"
#include "util.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;
}
static int callback_type_to_priority(int type) {
switch(type) {
case SELINUX_ERROR:
return LOG_ERR;
case SELINUX_WARNING:
return LOG_WARNING;
case SELINUX_INFO:
return LOG_INFO;
case SELINUX_AVC:
default:
return LOG_NOTICE;
}
}
/*
libselinux uses this callback when access gets denied or other
events happen. If audit is turned on, messages will be reported
using audit netlink, otherwise they will be logged using the usual
channels.
Code copied from dbus and modified.
*/
#ifdef HAVE_AUDIT
int fd;
fd = get_audit_fd();
if (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;
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
/*
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;
/* The SELinux context is something we really should have
* gotten directly from the message or sender, and not be an
* augmented field. If it was augmented we cannot use it for
* authorization, since this is racy and vulnerable. Let's add
* an extra check, just in case, even though this really
* shouldn't be possible. */
assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_SELINUX_CONTEXT) == 0, -EPERM);
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 {
r = getcon_raw(&fcon);
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
}