2 * dpkg - main program for package management
3 * selinux.c - SE Linux support
5 * Copyright © 2007-2015 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/>.
24 #include <sys/types.h>
30 #include <dpkg/i18n.h>
31 #include <dpkg/dpkg.h>
32 #include <dpkg/dpkg-db.h>
34 #ifdef WITH_LIBSELINUX
35 #include <selinux/selinux.h>
36 #include <selinux/avc.h>
37 #include <selinux/label.h>
42 #ifdef WITH_LIBSELINUX
43 static struct selabel_handle *sehandle;
47 dpkg_selabel_load(void)
49 #ifdef WITH_LIBSELINUX
50 static int selinux_enabled = -1;
52 if (selinux_enabled < 0) {
55 /* Set selinux_enabled if it is not already set (singleton). */
56 selinux_enabled = (is_selinux_enabled() > 0);
60 /* Open the SELinux status notification channel, with fallback
61 * enabled for older kernels. */
62 rc = selinux_status_open(1);
64 ohshit(_("cannot open security status notification channel"));
66 /* XXX: We could use selinux_set_callback() to redirect the
67 * errors from the other SELinux calls, but that does not seem
68 * worth it right now. */
69 } else if (selinux_enabled && selinux_status_updated()) {
70 /* The SELinux policy got updated in the kernel, usually after
71 * upgrading the package shipping it, we need to reload. */
72 selabel_close(sehandle);
74 /* SELinux is either disabled or it does not need a reload. */
78 sehandle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
79 if (sehandle == NULL && security_getenforce() == 1)
80 ohshite(_("cannot get security labeling handle"));
85 dpkg_selabel_set_context(const char *matchpath, const char *path, mode_t mode)
87 #ifdef WITH_LIBSELINUX
88 security_context_t scontext = NULL;
91 /* If SELinux is not enabled just do nothing. */
96 * We use the _raw function variants here so that no translation
97 * happens from computer to human readable forms, to avoid issues
98 * when mcstransd has disappeared during the unpack process.
101 /* Do nothing if we can't figure out what the context is, or if it has
102 * no context; in which case the default context shall be applied. */
103 ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
104 if (ret == -1 || (ret == 0 && scontext == NULL))
107 ret = lsetfilecon_raw(path, scontext);
108 if (ret < 0 && errno != ENOTSUP)
109 ohshite(_("cannot set security context for file object '%s'"),
113 #endif /* WITH_LIBSELINUX */
117 dpkg_selabel_close(void)
119 #ifdef WITH_LIBSELINUX
120 if (sehandle == NULL)
123 selinux_status_close();
124 selabel_close(sehandle);