2 * dpkg - main program for package management
3 * selinux.c - SELinux 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>
41 #include "security-mac.h"
43 #ifdef WITH_LIBSELINUX
44 static struct selabel_handle
*sehandle
;
47 #ifdef WITH_LIBSELINUX
48 static int DPKG_ATTR_PRINTF(2)
49 log_callback(int type
, const char *fmt
, ...)
64 m_vasprintf(&msg
, fmt
, ap
);
67 warning("selinux: %s", msg
);
75 dpkg_selabel_load(void)
77 #ifdef WITH_LIBSELINUX
78 static int selinux_enabled
= -1;
80 if (selinux_enabled
< 0) {
83 /* Set selinux_enabled if it is not already set (singleton). */
84 selinux_enabled
= (in_force(FORCE_SECURITY_MAC
) &&
85 is_selinux_enabled() > 0);
89 /* Open the SELinux status notification channel, with fallback
90 * enabled for older kernels. */
91 rc
= selinux_status_open(1);
93 ohshit(_("cannot open security status notification channel"));
95 selinux_set_callback(SELINUX_CB_LOG
, (union selinux_callback
) {
96 .func_log
= log_callback
,
98 } else if (selinux_enabled
&& selinux_status_updated()) {
99 /* The SELinux policy got updated in the kernel, usually after
100 * upgrading the package shipping it, we need to reload. */
101 selabel_close(sehandle
);
103 /* SELinux is either disabled or it does not need a reload. */
107 sehandle
= selabel_open(SELABEL_CTX_FILE
, NULL
, 0);
108 if (sehandle
== NULL
&& security_getenforce() == 1)
109 ohshite(_("cannot get security labeling handle"));
114 dpkg_selabel_set_context(const char *matchpath
, const char *path
, mode_t mode
)
116 #ifdef WITH_LIBSELINUX
117 char *scontext
= NULL
;
120 /* If SELinux is not enabled just do nothing. */
121 if (sehandle
== NULL
)
125 * We use the _raw function variants here so that no translation
126 * happens from computer to human readable forms, to avoid issues
127 * when mcstransd has disappeared during the unpack process.
130 /* Do nothing if we can't figure out what the context is, or if it has
131 * no context; in which case the default context shall be applied. */
132 ret
= selabel_lookup_raw(sehandle
, &scontext
, matchpath
, mode
& S_IFMT
);
133 if (ret
== -1 || (ret
== 0 && scontext
== NULL
))
136 ret
= lsetfilecon_raw(path
, scontext
);
137 if (ret
< 0 && errno
!= ENOTSUP
)
138 ohshite(_("cannot set security context for file object '%s'"),
142 #endif /* WITH_LIBSELINUX */
146 dpkg_selabel_close(void)
148 #ifdef WITH_LIBSELINUX
149 if (sehandle
== NULL
)
152 selinux_status_close();
153 selabel_close(sehandle
);