2 * libcompat - system compatibility library
4 * Based on code from libselinux, Public Domain.
5 * Copyright © 2014 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 #include <selinux/selinux.h>
27 #include <selinux/context.h>
32 setexecfilecon(const char *filename, const char *fallback)
36 security_context_t curcon = NULL, newcon = NULL, filecon = NULL;
37 security_class_t seclass;
38 context_t tmpcon = NULL;
40 if (is_selinux_enabled() < 1)
47 rc = getfilecon(filename, &filecon);
51 seclass = string_to_security_class("process");
55 rc = security_compute_create(curcon, filecon, seclass, &newcon);
59 if (strcmp(curcon, newcon) == 0) {
60 /* No default transition, use fallback for now. */
62 tmpcon = context_new(curcon);
65 if (context_type_set(tmpcon, fallback))
68 newcon = strdup(context_str(tmpcon));
73 rc = setexeccon(newcon);
76 if (rc < 0 && security_getenforce() == 0)