chiark / gitweb /
unit: When stopping due to BindsTo=, log which unit caused it
[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 <errno.h>
28 #include <selinux/selinux.h>
29 #include <selinux/avc.h>
30 #ifdef HAVE_AUDIT
31 #include <libaudit.h>
32 #endif
33
34 #include "sd-bus.h"
35 #include "bus-util.h"
36 #include "util.h"
37 #include "log.h"
38 #include "selinux-util.h"
39 #include "audit-fd.h"
40 #include "strv.h"
41
42 static bool initialized = false;
43
44 struct audit_info {
45         sd_bus_creds *creds;
46         const char *path;
47         const char *cmdline;
48 };
49
50 /*
51    Any time an access gets denied this callback will be called
52    with the audit data.  We then need to just copy the audit data into the msgbuf.
53 */
54 static int audit_callback(
55                 void *auditdata,
56                 security_class_t cls,
57                 char *msgbuf,
58                 size_t msgbufsize) {
59
60         const struct audit_info *audit = auditdata;
61         uid_t uid = 0, login_uid = 0;
62         gid_t gid = 0;
63         char login_uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
64         char uid_buf[DECIMAL_STR_MAX(uid_t) + 1] = "n/a";
65         char gid_buf[DECIMAL_STR_MAX(gid_t) + 1] = "n/a";
66
67         if (sd_bus_creds_get_audit_login_uid(audit->creds, &login_uid) >= 0)
68                 xsprintf(login_uid_buf, UID_FMT, login_uid);
69         if (sd_bus_creds_get_euid(audit->creds, &uid) >= 0)
70                 xsprintf(uid_buf, UID_FMT, uid);
71         if (sd_bus_creds_get_egid(audit->creds, &gid) >= 0)
72                 xsprintf(gid_buf, GID_FMT, gid);
73
74         snprintf(msgbuf, msgbufsize,
75                  "auid=%s uid=%s gid=%s%s%s%s%s%s%s",
76                  login_uid_buf, uid_buf, gid_buf,
77                  audit->path ? " path=\"" : "", strempty(audit->path), audit->path ? "\"" : "",
78                  audit->cmdline ? " cmdline=\"" : "", strempty(audit->cmdline), audit->cmdline ? "\"" : "");
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_internalv(LOG_AUTH | LOG_INFO, 0, __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                 return log_error_errno(errno, "avc_open() failed: %m");
125
126         selinux_set_callback(SELINUX_CB_AUDIT, (union selinux_callback) audit_callback);
127         selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) log_callback);
128
129         if (security_getenforce() < 0){
130                 r = -errno;
131                 avc_destroy();
132         }
133
134         return r;
135 }
136
137 static int mac_selinux_access_init(sd_bus_error *error) {
138         int r;
139
140         if (initialized)
141                 return 0;
142
143         if (!mac_selinux_use())
144                 return 0;
145
146         r = access_init();
147         if (r < 0)
148                 return sd_bus_error_set(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to initialize SELinux.");
149
150         initialized = true;
151         return 0;
152 }
153 #endif
154
155 void mac_selinux_access_free(void) {
156
157 #ifdef HAVE_SELINUX
158         if (!initialized)
159                 return;
160
161         avc_destroy();
162         initialized = false;
163 #endif
164 }
165
166 /*
167    This function communicates with the kernel to check whether or not it should
168    allow the access.
169    If the machine is in permissive mode it will return ok.  Audit messages will
170    still be generated if the access would be denied in enforcing mode.
171 */
172 int mac_selinux_generic_access_check(
173                 sd_bus_message *message,
174                 const char *path,
175                 const char *permission,
176                 sd_bus_error *error) {
177
178 #ifdef HAVE_SELINUX
179         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
180         const char *tclass = NULL, *scon = NULL;
181         struct audit_info audit_info = {};
182         _cleanup_free_ char *cl = NULL;
183         security_context_t fcon = NULL;
184         char **cmdline = NULL;
185         int r = 0;
186
187         assert(message);
188         assert(permission);
189         assert(error);
190
191         if (!mac_selinux_use())
192                 return 0;
193
194         r = mac_selinux_access_init(error);
195         if (r < 0)
196                 return r;
197
198         r = sd_bus_query_sender_creds(
199                         message,
200                         SD_BUS_CREDS_PID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EGID|
201                         SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_AUDIT_LOGIN_UID|
202                         SD_BUS_CREDS_SELINUX_CONTEXT|
203                         SD_BUS_CREDS_AUGMENT /* get more bits from /proc */,
204                         &creds);
205         if (r < 0)
206                 goto finish;
207
208         r = sd_bus_creds_get_selinux_context(creds, &scon);
209         if (r < 0)
210                 goto finish;
211
212         if (path) {
213                 /* Get the file context of the unit file */
214
215                 r = getfilecon(path, &fcon);
216                 if (r < 0) {
217                         r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get file context on %s.", path);
218                         goto finish;
219                 }
220
221                 tclass = "service";
222         } else {
223                 r = getcon(&fcon);
224                 if (r < 0) {
225                         r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Failed to get current context.");
226                         goto finish;
227                 }
228
229                 tclass = "system";
230         }
231
232         sd_bus_creds_get_cmdline(creds, &cmdline);
233         cl = strv_join(cmdline, " ");
234
235         audit_info.creds = creds;
236         audit_info.path = path;
237         audit_info.cmdline = cl;
238
239         r = selinux_check_access((security_context_t) scon, fcon, tclass, permission, &audit_info);
240         if (r < 0)
241                 r = sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "SELinux policy denies access.");
242
243         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);
244
245 finish:
246         freecon(fcon);
247
248         if (r < 0 && security_getenforce() != 1) {
249                 sd_bus_error_free(error);
250                 r = 0;
251         }
252
253         return r;
254 #else
255         return 0;
256 #endif
257 }
258
259 int mac_selinux_unit_access_check_strv(char **units,
260                                 sd_bus_message *message,
261                                 Manager *m,
262                                 const char *permission,
263                                 sd_bus_error *error) {
264 #ifdef HAVE_SELINUX
265         char **i;
266         Unit *u;
267         int r;
268
269         STRV_FOREACH(i, units) {
270                 u = manager_get_unit(m, *i);
271                 if (u) {
272                         r = mac_selinux_unit_access_check(u, message, permission, error);
273                         if (r < 0)
274                                 return r;
275                 }
276         }
277 #endif
278         return 0;
279 }