2011-12-10 03:43:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2011 , Peter Vrabec <pvrabec@redhat.com>
|
2011-12-10 03:43:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2011-12-10 03:43:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
|
2019-12-17 18:10:47 +05:30
|
|
|
#include <stdio.h>
|
2011-12-10 03:43:02 +05:30
|
|
|
#include "defines.h"
|
|
|
|
|
|
|
|
#include <selinux/selinux.h>
|
2021-04-09 21:50:55 +05:30
|
|
|
#include <selinux/label.h>
|
2012-01-08 21:36:57 +05:30
|
|
|
#include "prototypes.h"
|
2011-12-10 03:43:02 +05:30
|
|
|
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog_internal.h"
|
|
|
|
|
2011-12-10 03:43:02 +05:30
|
|
|
static bool selinux_checked = false;
|
|
|
|
static bool selinux_enabled;
|
2021-04-13 17:43:11 +05:30
|
|
|
static /*@null@*/struct selabel_handle *selabel_hnd = NULL;
|
|
|
|
|
|
|
|
static void cleanup(void)
|
|
|
|
{
|
|
|
|
if (selabel_hnd) {
|
|
|
|
selabel_close(selabel_hnd);
|
|
|
|
selabel_hnd = NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 03:43:02 +05:30
|
|
|
|
2021-10-08 16:43:13 +05:30
|
|
|
void reset_selinux_handle (void)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2011-12-10 03:43:02 +05:30
|
|
|
/*
|
|
|
|
* set_selinux_file_context - Set the security context before any file or
|
|
|
|
* directory creation.
|
|
|
|
*
|
|
|
|
* set_selinux_file_context () should be called before any creation
|
|
|
|
* of file, symlink, directory, ...
|
|
|
|
*
|
|
|
|
* Callers may have to Reset SELinux to create files with default
|
|
|
|
* contexts with reset_selinux_file_context
|
|
|
|
*/
|
2021-04-09 21:51:00 +05:30
|
|
|
int set_selinux_file_context (const char *dst_name, mode_t mode)
|
2011-12-10 03:43:02 +05:30
|
|
|
{
|
|
|
|
if (!selinux_checked) {
|
|
|
|
selinux_enabled = is_selinux_enabled () > 0;
|
|
|
|
selinux_checked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selinux_enabled) {
|
|
|
|
/* Get the default security context for this file */
|
2021-04-09 21:50:55 +05:30
|
|
|
|
|
|
|
/*@null@*/char *fcontext_raw = NULL;
|
|
|
|
int r;
|
|
|
|
|
2021-04-13 17:43:11 +05:30
|
|
|
if (selabel_hnd == NULL) {
|
|
|
|
selabel_hnd = selabel_open(SELABEL_CTX_FILE, NULL, 0);
|
|
|
|
if (selabel_hnd == NULL) {
|
|
|
|
return security_getenforce () != 0;
|
|
|
|
}
|
|
|
|
(void) atexit(cleanup);
|
2021-04-09 21:50:55 +05:30
|
|
|
}
|
|
|
|
|
2021-04-13 17:43:11 +05:30
|
|
|
r = selabel_lookup_raw(selabel_hnd, &fcontext_raw, dst_name, mode);
|
2021-04-09 21:50:55 +05:30
|
|
|
if (r < 0) {
|
|
|
|
/* No context specified for the searched path */
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
return 0;
|
2011-12-10 03:43:02 +05:30
|
|
|
}
|
2021-04-09 21:50:55 +05:30
|
|
|
|
|
|
|
return security_getenforce () != 0;
|
2011-12-10 03:43:02 +05:30
|
|
|
}
|
2021-04-09 21:50:55 +05:30
|
|
|
|
2011-12-10 03:43:02 +05:30
|
|
|
/* Set the security context for the next created file */
|
2021-04-09 21:50:55 +05:30
|
|
|
r = setfscreatecon_raw (fcontext_raw);
|
|
|
|
freecon (fcontext_raw);
|
|
|
|
if (r < 0) {
|
|
|
|
return security_getenforce () != 0;
|
2011-12-10 03:43:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reset_selinux_file_context - Reset the security context to the default
|
|
|
|
* policy behavior
|
|
|
|
*
|
|
|
|
* reset_selinux_file_context () should be called after the context
|
|
|
|
* was changed with set_selinux_file_context ()
|
|
|
|
*/
|
|
|
|
int reset_selinux_file_context (void)
|
|
|
|
{
|
|
|
|
if (!selinux_checked) {
|
|
|
|
selinux_enabled = is_selinux_enabled () > 0;
|
|
|
|
selinux_checked = true;
|
|
|
|
}
|
|
|
|
if (selinux_enabled) {
|
2021-04-09 21:50:51 +05:30
|
|
|
if (setfscreatecon_raw (NULL) != 0) {
|
2021-04-09 21:50:53 +05:30
|
|
|
return security_getenforce () != 0;
|
2011-12-10 03:43:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-16 03:03:54 +05:30
|
|
|
/*
|
|
|
|
* Log callback for libselinux internal error reporting.
|
|
|
|
*/
|
|
|
|
__attribute__((__format__ (printf, 2, 3)))
|
|
|
|
static int selinux_log_cb (int type, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char *buf;
|
|
|
|
int r;
|
|
|
|
#ifdef WITH_AUDIT
|
|
|
|
static int selinux_audit_fd = -2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
r = vasprintf (&buf, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_AUDIT
|
|
|
|
if (-2 == selinux_audit_fd) {
|
|
|
|
selinux_audit_fd = audit_open ();
|
|
|
|
|
|
|
|
if (-1 == selinux_audit_fd) {
|
|
|
|
/* You get these only when the kernel doesn't have
|
|
|
|
* audit compiled in. */
|
|
|
|
if ( (errno != EINVAL)
|
|
|
|
&& (errno != EPROTONOSUPPORT)
|
|
|
|
&& (errno != EAFNOSUPPORT)) {
|
|
|
|
|
|
|
|
(void) fputs (_("Cannot open audit interface.\n"),
|
2021-05-09 04:12:14 +05:30
|
|
|
shadow_logfd);
|
2019-10-16 03:03:54 +05:30
|
|
|
SYSLOG ((LOG_WARN, "Cannot open audit interface."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-1 != selinux_audit_fd) {
|
|
|
|
if (SELINUX_AVC == type) {
|
|
|
|
if (audit_log_user_avc_message (selinux_audit_fd,
|
|
|
|
AUDIT_USER_AVC, buf, NULL, NULL,
|
|
|
|
NULL, 0) > 0) {
|
|
|
|
goto skip_syslog;
|
|
|
|
}
|
|
|
|
} else if (SELINUX_ERROR == type) {
|
|
|
|
if (audit_log_user_avc_message (selinux_audit_fd,
|
|
|
|
AUDIT_USER_SELINUX_ERR, buf, NULL, NULL,
|
|
|
|
NULL, 0) > 0) {
|
|
|
|
goto skip_syslog;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SYSLOG ((LOG_WARN, "libselinux: %s", buf));
|
|
|
|
|
|
|
|
skip_syslog:
|
|
|
|
free (buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_selinux_permit - Check whether SELinux grants the given
|
|
|
|
* operation
|
|
|
|
*
|
|
|
|
* Parameter is the SELinux permission name, e.g. rootok
|
|
|
|
*
|
|
|
|
* Returns 0 when permission is granted
|
|
|
|
* or something failed but running in
|
|
|
|
* permissive mode
|
|
|
|
*/
|
|
|
|
int check_selinux_permit (const char *perm_name)
|
|
|
|
{
|
2021-04-09 21:50:51 +05:30
|
|
|
char *user_context_raw;
|
2019-10-16 03:03:54 +05:30
|
|
|
int r;
|
|
|
|
|
|
|
|
if (0 == is_selinux_enabled ()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
selinux_set_callback (SELINUX_CB_LOG, (union selinux_callback) selinux_log_cb);
|
|
|
|
|
2021-04-09 21:50:51 +05:30
|
|
|
if (getprevcon_raw (&user_context_raw) != 0) {
|
2021-05-09 04:12:14 +05:30
|
|
|
fprintf (shadow_logfd,
|
2019-10-16 03:03:54 +05:30
|
|
|
_("%s: can not get previous SELinux process context: %s\n"),
|
2021-12-26 04:11:58 +05:30
|
|
|
shadow_progname, strerror (errno));
|
2019-10-16 03:03:54 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"can not get previous SELinux process context: %s",
|
|
|
|
strerror (errno)));
|
|
|
|
return (security_getenforce () != 0);
|
|
|
|
}
|
|
|
|
|
2021-04-09 21:50:51 +05:30
|
|
|
r = selinux_check_access (user_context_raw, user_context_raw, "passwd", perm_name, NULL);
|
|
|
|
freecon (user_context_raw);
|
2019-10-16 03:03:54 +05:30
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-12-10 03:43:02 +05:30
|
|
|
#else /* !WITH_SELINUX */
|
|
|
|
extern int errno; /* warning: ANSI C forbids an empty source file */
|
|
|
|
#endif /* !WITH_SELINUX */
|