chiark / gitweb /
systemctl: add add-wants and add-requires verbs
[elogind.git] / src / core / selinux-access.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Dan Walsh
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "selinux-access.h"
23
24 #ifdef HAVE_SELINUX
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <selinux/selinux.h>
31 #include <selinux/avc.h>
32 #include <sys/socket.h>
33 #ifdef HAVE_AUDIT
34 #include <libaudit.h>
35 #endif
36
37 #include "sd-bus.h"
38 #include "bus-util.h"
39 #include "util.h"
40 #include "log.h"
41 #include "audit.h"
42 #include "selinux-util.h"
43 #include "audit-fd.h"
44 #include "strv.h"
45
46 static bool initialized = false;
47
48 struct audit_info {
49         sd_bus_creds *creds;
50         const char *path;
51         const char *cmdline;
52 };
53
54 /*
55    Any time an access gets denied this callback will be called
56    with the aduit data.  We then need to just copy the audit data into the msgbuf.
57 */
58 static int audit_callback(
59                 void *auditdata,
60                 security_class_t cls,
61                 char *msgbuf,
62                 size_t msgbufsize) {
63
64         const struct audit_info *audit = auditdata;
65         uid_t uid = 0, login_uid = 0;
66         gid_t gid = 0;
67
68         sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid);
69         sd_bus_creds_get_uid(audit->creds, &uid);
70         sd_bus_creds_get_gid(audit->creds, &gid);
71
72         snprintf(msgbuf, msgbufsize,
73                  "auid=%d uid=%d gid=%d%s%s%s%s%s%s",
74                  login_uid, uid, gid,
75                  audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
76                  audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "");
77
78         msgbuf[msgbufsize-1] = 0;
79
80         return 0;
81 }
82
83 /*
84    Any time an access gets denied this callback will be called
85    code copied from dbus. If audit is turned on the messages will go as
86    user_avc's into the /var/log/audit/audit.log, otherwise they will be
87    sent to syslog.
88 */
89 _printf_(2, 3) static int log_callback(int type, const char *fmt, ...) {
90         va_list ap;
91
92 #ifdef HAVE_AUDIT
93         if (get_audit_fd() >= 0) {
94                 _cleanup_free_ char *buf = NULL;
95                 int r;
96
97                 va_start(ap, fmt);
98                 r = vasprintf(&buf, fmt, ap);
99                 va_end(ap);
100
101                 if (r >= 0) {
102                         audit_log_user_avc_message(get_audit_fd(), AUDIT_USER_AVC, buf, NULL, NULL, NULL, 0);
103                         return 0;
104                 }
105         }
106 #endif
107
108         va_start(ap, fmt);
109         log_metav(LOG_USER | LOG_INFO, __FILE__, __LINE__, __FUNCTION__, fmt, ap);
110         va_end(ap);
111
112         return 0;
113 }
114
115 /*
116    Function must be called once to initialize the SELinux AVC environment.
117    Sets up callbacks.
118    If you want to cleanup memory you should need to call selinux_access_finish.
119 */
120 static int access_init(void) {
121         int r = 0;
122
123         if (avc_open(NULL, 0)) {
124                 log_error("avc_open() failed: %m");
125                 return -errno;
126         }
127
128         selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) audit_callback);
129         selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) log_callback);
130
131         if (security_getenforce() < 0){
132                 r = -errno;
133                 avc_destroy();
134         }
135
136         return r;
137 }
138
139 static int selinux_access_init(sd_bus_error *error) {
140         int r;
141
142         if (initialized)
143                 return 0;
144
145         if (!use_selinux())
146                 return 0;
147
148         r = access_init();
149         if (r < 0)
150                 return sd_bus_error_set(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to initialize SELinux.");
151
152         initialized = true;
153         return 0;
154 }
155
156 void selinux_access_free(void) {
157
158         if (!initialized)
159                 return;
160
161         avc_destroy();
162         initialized = false;
163 }
164
165 /*
166    This function communicates with the kernel to check whether or not it should
167    allow the access.
168    If the machine is in permissive mode it will return ok.  Audit messages will
169    still be generated if the access would be denied in enforcing mode.
170 */
171 int selinux_generic_access_check(
172                 sd_bus_message *message,
173                 const char *path,
174                 const char *permission,
175                 sd_bus_error *error) {
176
177         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
178         const char *tclass = NULL, *scon = NULL;
179         struct audit_info audit_info = {};
180         _cleanup_free_ char *cl = NULL;
181         security_context_t fcon = NULL;
182         char **cmdline = NULL;
183         int r = 0;
184
185         assert(message);
186         assert(permission);
187         assert(error);
188
189         if (!use_selinux())
190                 return 0;
191
192         r = selinux_access_init(error);
193         if (r < 0)
194                 return r;
195
196         r = sd_bus_query_sender_creds(
197                         message,
198                         SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|
199                         SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_AUDIT_LOGIN_UID|
200                         SD_BUS_CREDS_SELINUX_CONTEXT,
201                         &creds);
202         if (r < 0)
203                 goto finish;
204
205         r = sd_bus_creds_get_selinux_context(creds, &scon);
206         if (r < 0)
207                 goto finish;
208
209         if (path) {
210                 /* Get the file context of the unit file */
211
212                 r = getfilecon(path, &fcon);
213                 if (r < 0) {
214                         r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get file context on %s.", path);
215                         goto finish;
216                 }
217
218                 tclass = "service";
219         } else {
220                 r = getcon(&fcon);
221                 if (r < 0) {
222                         r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get current context.");
223                         goto finish;
224                 }
225
226                 tclass = "system";
227         }
228
229         sd_bus_creds_get_cmdline(creds, &cmdline);
230         cl = strv_join(cmdline, " ");
231
232         audit_info.creds = creds;
233         audit_info.path = path;
234         audit_info.cmdline = cl;
235
236         r = selinux_check_access((security_context_t) scon, fcon, tclass, permission, &audit_info);
237         if (r < 0)
238                 r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "SELinux policy denies access.");
239
240         log_debug("SELinux access check scon=%s tcon=%s tclass=%s perm=%s path=%s cmdline=%s: %i", scon, fcon, tclass, permission, path, cl, r);
241
242 finish:
243         freecon(fcon);
244
245         if (r < 0 && security_getenforce() != 1) {
246                 sd_bus_error_free(error);
247                 r = 0;
248         }
249
250         return r;
251 }
252
253 int selinux_unit_access_check_strv(char **units,
254                                 sd_bus_message *message,
255                                 Manager *m,
256                                 const char *permission,
257                                 sd_bus_error *error) {
258         char **i;
259         Unit *u;
260         int r;
261
262         STRV_FOREACH(i, units) {
263                 u = manager_get_unit(m, *i);
264                 if (u) {
265                         r = selinux_unit_access_check(u, message, permission, error);
266                         if (r < 0)
267                                 return r;
268                 }
269         }
270
271         return 0;
272 }
273
274 #else
275
276 int selinux_generic_access_check(
277                 sd_bus_message *message,
278                 const char *path,
279                 const char *permission,
280                 sd_bus_error *error) {
281
282         return 0;
283 }
284
285 void selinux_access_free(void) {
286 }
287
288 int selinux_unit_access_check_strv(char **units,
289                                 sd_bus_message *message,
290                                 Manager *m,
291                                 const char *permission,
292                                 sd_bus_error *error) {
293         return 0;
294 }
295
296 #endif