/***
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 <errno.h>
#include <stdio.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;
}
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
log_internalv(LOG_AUTH | callback_type_to_priority(type), 0, __FILE__, __LINE__, __FUNCTION__, fmt, ap);
return 0;
}
if (!mac_selinux_use())
return 0;
if (initialized)
return 1;
log_full_errno(enforce != 0 ? LOG_ERR : LOG_WARNING, saved_errno, "Failed to open the SELinux AVC: %m");
/* If enforcement isn't on, then let's suppress this
* error, and just don't do any AVC checks. The
* warning we printed is hence all the admin will
* see. */
if (enforce == 0)
return 0;
/* Return an access denied error, if we couldn't load
* the AVC but enforcing mode was on, or we couldn't
* determine whether it is one. */
return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to open the SELinux AVC: %s", strerror(saved_errno));
}
initialized = true;
return 1;
}
/*
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) {
int r = 0;
r = 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
const char *path,
const char *permission,
sd_bus_error *error) {
return 0;
}
#endif