.
[glibc.git] / nscd / selinux.c
blobb826031150e037b0b7d81885fb10f87493db5669
1 /* SELinux access controls for nscd.
2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Matthew Rickard <mjricka@epoch.ncsc.mil>, 2004.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include "config.h"
22 #include <error.h>
23 #include <errno.h>
24 #include <libintl.h>
25 #include <pthread.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <syslog.h>
30 #include <unistd.h>
31 #include <sys/prctl.h>
32 #include <selinux/av_permissions.h>
33 #include <selinux/avc.h>
34 #include <selinux/flask.h>
35 #include <selinux/selinux.h>
36 #ifdef HAVE_LIBAUDIT
37 # include <libaudit.h>
38 #endif
40 #include "dbg_log.h"
41 #include "selinux.h"
44 #ifdef HAVE_SELINUX
45 /* Global variable to tell if the kernel has SELinux support. */
46 int selinux_enabled;
48 /* Define mappings of access vector permissions to request types. */
49 static const int perms[LASTREQ] =
51 [GETPWBYNAME] = NSCD__GETPWD,
52 [GETPWBYUID] = NSCD__GETPWD,
53 [GETGRBYNAME] = NSCD__GETGRP,
54 [GETGRBYGID] = NSCD__GETGRP,
55 [GETHOSTBYNAME] = NSCD__GETHOST,
56 [GETHOSTBYNAMEv6] = NSCD__GETHOST,
57 [GETHOSTBYADDR] = NSCD__GETHOST,
58 [GETHOSTBYADDRv6] = NSCD__GETHOST,
59 [GETSTAT] = NSCD__GETSTAT,
60 [SHUTDOWN] = NSCD__ADMIN,
61 [INVALIDATE] = NSCD__ADMIN,
62 [GETFDPW] = NSCD__SHMEMPWD,
63 [GETFDGR] = NSCD__SHMEMGRP,
64 [GETFDHST] = NSCD__SHMEMHOST,
65 [GETAI] = NSCD__GETHOST,
66 [INITGROUPS] = NSCD__GETGRP
69 /* Store an entry ref to speed AVC decisions. */
70 static struct avc_entry_ref aeref;
72 /* Thread to listen for SELinux status changes via netlink. */
73 static pthread_t avc_notify_thread;
75 #ifdef HAVE_LIBAUDIT
76 /* Prototype for supporting the audit daemon */
77 static void log_callback (const char *fmt, ...);
78 #endif
80 /* Prototypes for AVC callback functions. */
81 static void *avc_create_thread (void (*run) (void));
82 static void avc_stop_thread (void *thread);
83 static void *avc_alloc_lock (void);
84 static void avc_get_lock (void *lock);
85 static void avc_release_lock (void *lock);
86 static void avc_free_lock (void *lock);
88 /* AVC callback structures for use in avc_init. */
89 static const struct avc_log_callback log_cb =
91 #ifdef HAVE_LIBAUDIT
92 .func_log = log_callback,
93 #else
94 .func_log = dbg_log,
95 #endif
96 .func_audit = NULL
98 static const struct avc_thread_callback thread_cb =
100 .func_create_thread = avc_create_thread,
101 .func_stop_thread = avc_stop_thread
103 static const struct avc_lock_callback lock_cb =
105 .func_alloc_lock = avc_alloc_lock,
106 .func_get_lock = avc_get_lock,
107 .func_release_lock = avc_release_lock,
108 .func_free_lock = avc_free_lock
111 #ifdef HAVE_LIBAUDIT
112 /* The audit system's netlink socket descriptor */
113 static int audit_fd = -1;
115 /* When an avc denial occurs, log it to audit system */
116 static void
117 log_callback (const char *fmt, ...)
119 if (audit_fd >= 0)
121 va_list ap;
122 va_start (ap, fmt);
124 char *buf;
125 int e = vasprintf (&buf, fmt, ap);
126 if (e < 0)
128 buf = alloca (BUFSIZ);
129 vsnprintf (buf, BUFSIZ, fmt, ap);
132 /* FIXME: need to attribute this to real user, using getuid for now */
133 audit_log_user_avc_message (audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
134 NULL, getuid ());
136 if (e >= 0)
137 free (buf);
139 va_end (ap);
143 /* Initialize the connection to the audit system */
144 static void
145 audit_init (void)
147 audit_fd = audit_open ();
148 if (audit_fd < 0
149 /* If kernel doesn't support audit, bail out */
150 && errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
151 dbg_log (_("Failed opening connection to the audit subsystem: %m"));
155 # ifdef HAVE_LIBCAP
156 static const cap_value_t new_cap_list[] =
157 { CAP_AUDIT_WRITE };
158 # define nnew_cap_list (sizeof (new_cap_list) / sizeof (new_cap_list[0]))
159 static const cap_value_t tmp_cap_list[] =
160 { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
161 # define ntmp_cap_list (sizeof (tmp_cap_list) / sizeof (tmp_cap_list[0]))
163 cap_t
164 preserve_capabilities (void)
166 if (getuid () != 0)
167 /* Not root, then we cannot preserve anything. */
168 return NULL;
170 if (prctl (PR_SET_KEEPCAPS, 1) == -1)
172 dbg_log (_("Failed to set keep-capabilities"));
173 error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
174 /* NOTREACHED */
177 cap_t tmp_caps = cap_init ();
178 cap_t new_caps;
179 if (tmp_caps != NULL)
180 new_caps = cap_init ();
182 if (tmp_caps == NULL || new_caps == NULL)
184 if (tmp_caps != NULL)
185 cap_free (tmp_caps);
187 dbg_log (_("Failed to initialize drop of capabilities"));
188 error (EXIT_FAILURE, 0, _("cap_init failed"));
191 /* There is no reason why these should not work. */
192 cap_set_flag (new_caps, CAP_PERMITTED, nnew_cap_list,
193 (cap_value_t *) new_cap_list, CAP_SET);
194 cap_set_flag (new_caps, CAP_EFFECTIVE, nnew_cap_list,
195 (cap_value_t *) new_cap_list, CAP_SET);
197 cap_set_flag (tmp_caps, CAP_PERMITTED, ntmp_cap_list,
198 (cap_value_t *) tmp_cap_list, CAP_SET);
199 cap_set_flag (tmp_caps, CAP_EFFECTIVE, ntmp_cap_list,
200 (cap_value_t *) tmp_cap_list, CAP_SET);
202 int res = cap_set_proc (tmp_caps);
204 cap_free (tmp_caps);
206 if (__builtin_expect (res != 0, 0))
208 cap_free (new_caps);
209 dbg_log (_("Failed to drop capabilities\n"));
210 error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
213 return new_caps;
216 void
217 install_real_capabilities (cap_t new_caps)
219 /* If we have no capabilities there is nothing to do here. */
220 if (new_caps == NULL)
221 return;
223 if (cap_set_proc (new_caps))
225 cap_free (new_caps);
226 dbg_log (_("Failed to drop capabilities"));
227 error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
228 /* NOTREACHED */
231 cap_free (new_caps);
233 if (prctl (PR_SET_KEEPCAPS, 0) == -1)
235 dbg_log (_("Failed to unset keep-capabilities"));
236 error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
237 /* NOTREACHED */
240 # endif /* HAVE_LIBCAP */
241 #endif /* HAVE_LIBAUDIT */
243 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
244 to the result. */
245 void
246 nscd_selinux_enabled (int *selinux_enabled)
248 *selinux_enabled = is_selinux_enabled ();
249 if (*selinux_enabled < 0)
251 dbg_log (_("Failed to determine if kernel supports SELinux"));
252 exit (EXIT_FAILURE);
257 /* Create thread for AVC netlink notification. */
258 static void *
259 avc_create_thread (void (*run) (void))
261 int rc;
263 rc =
264 pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
265 if (rc != 0)
266 error (EXIT_FAILURE, rc, _("Failed to start AVC thread"));
268 return &avc_notify_thread;
272 /* Stop AVC netlink thread. */
273 static void
274 avc_stop_thread (void *thread)
276 pthread_cancel (*(pthread_t *) thread);
280 /* Allocate a new AVC lock. */
281 static void *
282 avc_alloc_lock (void)
284 pthread_mutex_t *avc_mutex;
286 avc_mutex = malloc (sizeof (pthread_mutex_t));
287 if (avc_mutex == NULL)
288 error (EXIT_FAILURE, errno, _("Failed to create AVC lock"));
289 pthread_mutex_init (avc_mutex, NULL);
291 return avc_mutex;
295 /* Acquire an AVC lock. */
296 static void
297 avc_get_lock (void *lock)
299 pthread_mutex_lock (lock);
303 /* Release an AVC lock. */
304 static void
305 avc_release_lock (void *lock)
307 pthread_mutex_unlock (lock);
311 /* Free an AVC lock. */
312 static void
313 avc_free_lock (void *lock)
315 pthread_mutex_destroy (lock);
316 free (lock);
320 /* Initialize the user space access vector cache (AVC) for NSCD along with
321 log/thread/lock callbacks. */
322 void
323 nscd_avc_init (void)
325 avc_entry_ref_init (&aeref);
327 if (avc_init ("avc", NULL, &log_cb, &thread_cb, &lock_cb) < 0)
328 error (EXIT_FAILURE, errno, _("Failed to start AVC"));
329 else
330 dbg_log (_("Access Vector Cache (AVC) started"));
331 #ifdef HAVE_LIBAUDIT
332 audit_init ();
333 #endif
337 /* Check the permission from the caller (via getpeercon) to nscd.
338 Returns 0 if access is allowed, 1 if denied, and -1 on error. */
340 nscd_request_avc_has_perm (int fd, request_type req)
342 /* Initialize to NULL so we know what to free in case of failure. */
343 security_context_t scon = NULL;
344 security_context_t tcon = NULL;
345 security_id_t ssid = NULL;
346 security_id_t tsid = NULL;
347 int rc = -1;
349 if (getpeercon (fd, &scon) < 0)
351 dbg_log (_("Error getting context of socket peer"));
352 goto out;
354 if (getcon (&tcon) < 0)
356 dbg_log (_("Error getting context of nscd"));
357 goto out;
359 if (avc_context_to_sid (scon, &ssid) < 0
360 || avc_context_to_sid (tcon, &tsid) < 0)
362 dbg_log (_("Error getting sid from context"));
363 goto out;
366 rc = avc_has_perm (ssid, tsid, SECCLASS_NSCD, perms[req], &aeref, NULL) < 0;
368 out:
369 if (scon)
370 freecon (scon);
371 if (tcon)
372 freecon (tcon);
373 if (ssid)
374 sidput (ssid);
375 if (tsid)
376 sidput (tsid);
378 return rc;
382 /* Wrapper to get AVC statistics. */
383 void
384 nscd_avc_cache_stats (struct avc_cache_stats *cstats)
386 avc_cache_stats (cstats);
390 /* Print the AVC statistics to stdout. */
391 void
392 nscd_avc_print_stats (struct avc_cache_stats *cstats)
394 printf (_("\nSELinux AVC Statistics:\n\n"
395 "%15u entry lookups\n"
396 "%15u entry hits\n"
397 "%15u entry misses\n"
398 "%15u entry discards\n"
399 "%15u CAV lookups\n"
400 "%15u CAV hits\n"
401 "%15u CAV probes\n"
402 "%15u CAV misses\n"),
403 cstats->entry_lookups, cstats->entry_hits, cstats->entry_misses,
404 cstats->entry_discards, cstats->cav_lookups, cstats->cav_hits,
405 cstats->cav_probes, cstats->cav_misses);
409 /* Clean up the AVC before exiting. */
410 void
411 nscd_avc_destroy (void)
413 avc_destroy ();
414 #ifdef HAVE_LIBAUDIT
415 audit_close (audit_fd);
416 #endif
419 #endif /* HAVE_SELINUX */