2013-03-21 19:07:10 +08:00
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/context.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/avc.h>
|
|
|
|
#include <rpm/rpmlog.h>
|
|
|
|
#include <rpm/rpmts.h>
|
2013-04-03 16:36:09 +08:00
|
|
|
#include "lib/rpmplugin.h"
|
2013-03-21 19:07:10 +08:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
static struct selabel_handle * sehandle = NULL;
|
|
|
|
|
2019-02-14 19:12:49 +08:00
|
|
|
static inline rpmlogLvl loglvl(int iserror)
|
|
|
|
{
|
|
|
|
return iserror ? RPMLOG_ERR : RPMLOG_DEBUG;
|
|
|
|
}
|
|
|
|
|
2013-03-21 19:07:10 +08:00
|
|
|
static void sehandle_fini(int close_status)
|
|
|
|
{
|
|
|
|
if (sehandle) {
|
|
|
|
selabel_close(sehandle);
|
|
|
|
sehandle = NULL;
|
|
|
|
}
|
|
|
|
if (close_status) {
|
|
|
|
selinux_status_close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static rpmRC sehandle_init(int open_status)
|
|
|
|
{
|
|
|
|
const char * path = selinux_file_context_path();
|
|
|
|
struct selinux_opt opts[] = {
|
|
|
|
{ .type = SELABEL_OPT_PATH, .value = path }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return RPMRC_FAIL;
|
|
|
|
|
|
|
|
if (open_status) {
|
|
|
|
selinux_status_close();
|
|
|
|
if (selinux_status_open(0) < 0) {
|
|
|
|
return RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
} else if (!selinux_status_updated() && sehandle) {
|
|
|
|
return RPMRC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sehandle)
|
|
|
|
sehandle_fini(0);
|
|
|
|
|
|
|
|
sehandle = selabel_open(SELABEL_CTX_FILE, opts, 1);
|
|
|
|
|
2019-02-14 19:12:49 +08:00
|
|
|
rpmlog(loglvl(sehandle == NULL), "selabel_open: (%s) %s\n",
|
2016-03-04 20:51:07 +08:00
|
|
|
path, (sehandle == NULL ? strerror(errno) : ""));
|
|
|
|
|
2013-03-21 19:07:10 +08:00
|
|
|
return (sehandle != NULL) ? RPMRC_OK : RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:06:49 +08:00
|
|
|
static rpmRC selinux_tsm_pre(rpmPlugin plugin, rpmts ts)
|
2013-03-21 19:07:10 +08:00
|
|
|
{
|
|
|
|
rpmRC rc = RPMRC_OK;
|
|
|
|
|
|
|
|
/* If SELinux isn't enabled on the system, dont mess with it */
|
|
|
|
if (!is_selinux_enabled()) {
|
|
|
|
rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_NOCONTEXTS));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If not enabled or a test-transaction, dont bother with labels */
|
|
|
|
if (!(rpmtsFlags(ts) & (RPMTRANS_FLAG_NOCONTEXTS|RPMTRANS_FLAG_TEST))) {
|
|
|
|
rc = sehandle_init(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:06:49 +08:00
|
|
|
static rpmRC selinux_tsm_post(rpmPlugin plugin, rpmts ts, int rc)
|
2013-03-21 19:07:10 +08:00
|
|
|
{
|
|
|
|
if (sehandle) {
|
|
|
|
sehandle_fini(1);
|
|
|
|
}
|
|
|
|
return RPMRC_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-05 14:06:49 +08:00
|
|
|
static rpmRC selinux_psm_pre(rpmPlugin plugin, rpmte te)
|
2013-03-21 19:07:10 +08:00
|
|
|
{
|
|
|
|
rpmRC rc = RPMRC_OK;
|
|
|
|
|
|
|
|
if (sehandle) {
|
|
|
|
/* reload the labels if policy changed underneath */
|
|
|
|
rc = sehandle_init(0);
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:01:48 +08:00
|
|
|
#ifndef HAVE_SETEXECFILECON
|
2020-02-18 21:50:40 +08:00
|
|
|
static int setexecfilecon(const char *path, const char *fallback_type)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
2020-10-28 14:58:39 +08:00
|
|
|
char *mycon = NULL, fcon = NULL, newcon = NULL;
|
2013-03-21 19:07:10 +08:00
|
|
|
context_t con = NULL;
|
|
|
|
|
|
|
|
/* Figure the context to for next exec() */
|
|
|
|
if (getcon(&mycon) < 0)
|
|
|
|
goto exit;
|
|
|
|
if (getfilecon(path, &fcon) < 0)
|
|
|
|
goto exit;
|
2020-02-18 21:50:40 +08:00
|
|
|
if (security_compute_create(mycon, fcon,
|
|
|
|
string_to_security_class("process"), &newcon) < 0)
|
2013-03-21 19:07:10 +08:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (rstreq(mycon, newcon)) {
|
|
|
|
con = context_new(mycon);
|
|
|
|
if (!con)
|
|
|
|
goto exit;
|
2020-02-18 21:50:40 +08:00
|
|
|
if (context_type_set(con, fallback_type))
|
2013-03-21 19:07:10 +08:00
|
|
|
goto exit;
|
|
|
|
freecon(newcon);
|
|
|
|
newcon = xstrdup(context_str(con));
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:50:40 +08:00
|
|
|
rc = setexeccon(newcon);
|
2013-03-21 19:07:10 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
context_free(con);
|
|
|
|
freecon(newcon);
|
|
|
|
freecon(fcon);
|
|
|
|
freecon(mycon);
|
2020-02-18 21:50:40 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static rpmRC selinux_scriptlet_fork_post(rpmPlugin plugin,
|
|
|
|
const char *path, int type)
|
|
|
|
{
|
|
|
|
/* No default transition, use rpm_script_t for now. */
|
|
|
|
const char *script_type = "rpm_script_t";
|
|
|
|
rpmRC rc = RPMRC_FAIL;
|
2013-03-21 19:07:10 +08:00
|
|
|
|
2015-01-16 00:01:48 +08:00
|
|
|
if (sehandle == NULL)
|
|
|
|
return RPMRC_OK;
|
|
|
|
|
2020-02-18 21:50:40 +08:00
|
|
|
if (setexecfilecon(path, script_type) == 0)
|
2015-01-16 00:01:48 +08:00
|
|
|
rc = RPMRC_OK;
|
|
|
|
|
2015-10-12 18:47:45 +08:00
|
|
|
/* If selinux is not enforcing, we don't care either */
|
|
|
|
if (rc && security_getenforce() < 1)
|
|
|
|
rc = RPMRC_OK;
|
2013-04-05 15:49:24 +08:00
|
|
|
|
2020-02-18 21:50:40 +08:00
|
|
|
rpmlog(loglvl(rc), "setexecfilecon: (%s, %s) %s\n",
|
|
|
|
path, script_type, rc ? strerror(errno) : "");
|
|
|
|
|
2013-03-21 19:07:10 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-03-05 20:30:59 +08:00
|
|
|
static rpmRC selinux_fsm_file_prepare(rpmPlugin plugin, rpmfi fi,
|
2013-04-03 16:57:53 +08:00
|
|
|
const char *path, const char *dest,
|
|
|
|
mode_t file_mode, rpmFsmOp op)
|
2013-03-21 19:07:10 +08:00
|
|
|
{
|
|
|
|
rpmRC rc = RPMRC_FAIL; /* assume failure */
|
|
|
|
rpmFileAction action = XFO_ACTION(op);
|
|
|
|
|
2013-03-27 11:52:20 +08:00
|
|
|
if (sehandle && !XFA_SKIPPING(action)) {
|
2020-10-28 14:58:39 +08:00
|
|
|
char *scon = NULL;
|
2013-03-21 19:07:10 +08:00
|
|
|
if (selabel_lookup_raw(sehandle, &scon, dest, file_mode) == 0) {
|
|
|
|
int conrc = lsetfilecon(path, scon);
|
|
|
|
|
|
|
|
if (conrc == 0 || (conrc < 0 && errno == EOPNOTSUPP))
|
|
|
|
rc = RPMRC_OK;
|
2019-12-13 20:14:10 +08:00
|
|
|
|
|
|
|
rpmlog(loglvl(rc != RPMRC_OK), "lsetfilecon: (%s, %s) %s\n",
|
|
|
|
path, scon, (conrc < 0 ? strerror(errno) : ""));
|
|
|
|
|
2013-04-02 16:20:01 +08:00
|
|
|
freecon(scon);
|
|
|
|
} else {
|
|
|
|
/* No context for dest is not our headache */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
rc = RPMRC_OK;
|
2013-03-21 19:07:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = RPMRC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2013-03-31 17:51:31 +08:00
|
|
|
|
|
|
|
struct rpmPluginHooks_s selinux_hooks = {
|
2013-04-05 14:06:49 +08:00
|
|
|
.tsm_pre = selinux_tsm_pre,
|
|
|
|
.tsm_post = selinux_tsm_post,
|
|
|
|
.psm_pre = selinux_psm_pre,
|
|
|
|
.scriptlet_fork_post = selinux_scriptlet_fork_post,
|
|
|
|
.fsm_file_prepare = selinux_fsm_file_prepare,
|
2013-03-31 17:51:31 +08:00
|
|
|
};
|