..
[glibc.git] / nscd / selinux.c
blob91c1442b597d6f49683dcdd3087d095a2ed808ce
1 /* SELinux access controls for nscd.
2 Copyright (C) 2004, 2005 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 <selinux/av_permissions.h>
31 #include <selinux/avc.h>
32 #include <selinux/flask.h>
33 #include <selinux/selinux.h>
34 #ifdef HAVE_LIBAUDIT
35 #include <libaudit.h>
36 #endif
38 #include "dbg_log.h"
39 #include "selinux.h"
42 #ifdef HAVE_SELINUX
43 /* Global variable to tell if the kernel has SELinux support. */
44 int selinux_enabled;
46 /* Define mappings of access vector permissions to request types. */
47 static const int perms[LASTREQ] =
49 [GETPWBYNAME] = NSCD__GETPWD,
50 [GETPWBYUID] = NSCD__GETPWD,
51 [GETGRBYNAME] = NSCD__GETGRP,
52 [GETGRBYGID] = NSCD__GETGRP,
53 [GETHOSTBYNAME] = NSCD__GETHOST,
54 [GETHOSTBYNAMEv6] = NSCD__GETHOST,
55 [GETHOSTBYADDR] = NSCD__GETHOST,
56 [GETHOSTBYADDRv6] = NSCD__GETHOST,
57 [GETSTAT] = NSCD__GETSTAT,
58 [SHUTDOWN] = NSCD__ADMIN,
59 [INVALIDATE] = NSCD__ADMIN,
60 [GETFDPW] = NSCD__SHMEMPWD,
61 [GETFDGR] = NSCD__SHMEMGRP,
62 [GETFDHST] = NSCD__SHMEMHOST,
63 [GETAI] = NSCD__GETHOST,
64 [INITGROUPS] = NSCD__GETGRP
67 /* Store an entry ref to speed AVC decisions. */
68 static struct avc_entry_ref aeref;
70 /* Thread to listen for SELinux status changes via netlink. */
71 static pthread_t avc_notify_thread;
73 #ifdef HAVE_LIBAUDIT
74 /* Prototype for supporting the audit daemon */
75 static void log_callback (const char *fmt, ...);
76 #endif
78 /* Prototypes for AVC callback functions. */
79 static void *avc_create_thread (void (*run) (void));
80 static void avc_stop_thread (void *thread);
81 static void *avc_alloc_lock (void);
82 static void avc_get_lock (void *lock);
83 static void avc_release_lock (void *lock);
84 static void avc_free_lock (void *lock);
86 /* AVC callback structures for use in avc_init. */
87 static const struct avc_log_callback log_cb =
89 #ifdef HAVE_LIBAUDIT
90 .func_log = log_callback,
91 #else
92 .func_log = dbg_log,
93 #endif
94 .func_audit = NULL
96 static const struct avc_thread_callback thread_cb =
98 .func_create_thread = avc_create_thread,
99 .func_stop_thread = avc_stop_thread
101 static const struct avc_lock_callback lock_cb =
103 .func_alloc_lock = avc_alloc_lock,
104 .func_get_lock = avc_get_lock,
105 .func_release_lock = avc_release_lock,
106 .func_free_lock = avc_free_lock
109 #ifdef HAVE_LIBAUDIT
110 /* The audit system's netlink socket descriptor */
111 static int audit_fd = -1;
113 /* When an avc denial occurs, log it to audit system */
114 static void
115 log_callback (const char *fmt, ...)
117 va_list ap;
119 va_start (ap, fmt);
120 audit_log_avc (audit_fd, AUDIT_USER_AVC, fmt, ap);
121 va_end (ap);
124 /* Initialize the connection to the audit system */
125 static void
126 audit_init (void)
128 audit_fd = audit_open ();
129 if (audit_fd < 0)
130 dbg_log (_("Failed opening connection to the audit subsystem"));
132 #endif /* HAVE_LIBAUDIT */
134 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
135 to the result. */
136 void
137 nscd_selinux_enabled (int *selinux_enabled)
139 *selinux_enabled = is_selinux_enabled ();
140 if (*selinux_enabled < 0)
142 dbg_log (_("Failed to determine if kernel supports SELinux"));
143 exit (EXIT_FAILURE);
148 /* Create thread for AVC netlink notification. */
149 static void *
150 avc_create_thread (void (*run) (void))
152 int rc;
154 rc =
155 pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
156 if (rc != 0)
157 error (EXIT_FAILURE, rc, _("Failed to start AVC thread"));
159 return &avc_notify_thread;
163 /* Stop AVC netlink thread. */
164 static void
165 avc_stop_thread (void *thread)
167 pthread_cancel (*(pthread_t *) thread);
171 /* Allocate a new AVC lock. */
172 static void *
173 avc_alloc_lock (void)
175 pthread_mutex_t *avc_mutex;
177 avc_mutex = malloc (sizeof (pthread_mutex_t));
178 if (avc_mutex == NULL)
179 error (EXIT_FAILURE, errno, _("Failed to create AVC lock"));
180 pthread_mutex_init (avc_mutex, NULL);
182 return avc_mutex;
186 /* Acquire an AVC lock. */
187 static void
188 avc_get_lock (void *lock)
190 pthread_mutex_lock (lock);
194 /* Release an AVC lock. */
195 static void
196 avc_release_lock (void *lock)
198 pthread_mutex_unlock (lock);
202 /* Free an AVC lock. */
203 static void
204 avc_free_lock (void *lock)
206 pthread_mutex_destroy (lock);
207 free (lock);
211 /* Initialize the user space access vector cache (AVC) for NSCD along with
212 log/thread/lock callbacks. */
213 void
214 nscd_avc_init (void)
216 avc_entry_ref_init (&aeref);
218 if (avc_init ("avc", NULL, &log_cb, &thread_cb, &lock_cb) < 0)
219 error (EXIT_FAILURE, errno, _("Failed to start AVC"));
220 else
221 dbg_log (_("Access Vector Cache (AVC) started"));
222 #ifdef HAVE_LIBAUDIT
223 audit_init ();
224 #endif
228 /* Check the permission from the caller (via getpeercon) to nscd.
229 Returns 0 if access is allowed, 1 if denied, and -1 on error. */
231 nscd_request_avc_has_perm (int fd, request_type req)
233 /* Initialize to NULL so we know what to free in case of failure. */
234 security_context_t scon = NULL;
235 security_context_t tcon = NULL;
236 security_id_t ssid = NULL;
237 security_id_t tsid = NULL;
238 int rc = -1;
240 if (getpeercon (fd, &scon) < 0)
242 dbg_log (_("Error getting context of socket peer"));
243 goto out;
245 if (getcon (&tcon) < 0)
247 dbg_log (_("Error getting context of nscd"));
248 goto out;
250 if (avc_context_to_sid (scon, &ssid) < 0
251 || avc_context_to_sid (tcon, &tsid) < 0)
253 dbg_log (_("Error getting sid from context"));
254 goto out;
257 rc = avc_has_perm (ssid, tsid, SECCLASS_NSCD, perms[req], &aeref, NULL) < 0;
259 out:
260 if (scon)
261 freecon (scon);
262 if (tcon)
263 freecon (tcon);
264 if (ssid)
265 sidput (ssid);
266 if (tsid)
267 sidput (tsid);
269 return rc;
273 /* Wrapper to get AVC statistics. */
274 void
275 nscd_avc_cache_stats (struct avc_cache_stats *cstats)
277 avc_cache_stats (cstats);
281 /* Print the AVC statistics to stdout. */
282 void
283 nscd_avc_print_stats (struct avc_cache_stats *cstats)
285 printf (_("\nSELinux AVC Statistics:\n\n"
286 "%15u entry lookups\n"
287 "%15u entry hits\n"
288 "%15u entry misses\n"
289 "%15u entry discards\n"
290 "%15u CAV lookups\n"
291 "%15u CAV hits\n"
292 "%15u CAV probes\n"
293 "%15u CAV misses\n"),
294 cstats->entry_lookups, cstats->entry_hits, cstats->entry_misses,
295 cstats->entry_discards, cstats->cav_lookups, cstats->cav_hits,
296 cstats->cav_probes, cstats->cav_misses);
300 /* Clean up the AVC before exiting. */
301 void
302 nscd_avc_destroy (void)
304 avc_destroy ();
305 #ifdef HAVE_LIBAUDIT
306 audit_close (audit_fd);
307 #endif
310 #endif /* HAVE_SELINUX */