Cache network interface information
[platform/upstream/glibc.git] / nscd / selinux.c
1 /* SELinux access controls for nscd.
2    Copyright (C) 2004,2005,2006,2007,2009,2011 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Matthew Rickard <mjricka@epoch.ncsc.mil>, 2004.
5
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.
10
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.
15
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.  */
20
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
39
40 #include "dbg_log.h"
41 #include "selinux.h"
42
43
44 #ifdef HAVE_SELINUX
45 /* Global variable to tell if the kernel has SELinux support.  */
46 int selinux_enabled;
47
48 /* Define mappings of access vector permissions to request types.  */
49 static const access_vector_t perms[LASTREQ] =
50 {
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,
67 #ifdef NSCD__GETSERV
68   [GETSERVBYNAME] = NSCD__GETSERV,
69   [GETSERVBYPORT] = NSCD__GETSERV,
70   [GETFDSERV] = NSCD__SHMEMSERV,
71 #endif
72 #ifdef NSCD__GETNETGRP
73   [GETNETGRENT] = NSCD__GETNETGRP,
74   [INNETGR] = NSCD__GETNETGRP,
75   [GETFDNETGR] = NSCD__SHMEMNETGRP,
76 #endif
77 };
78
79 /* Store an entry ref to speed AVC decisions.  */
80 static struct avc_entry_ref aeref;
81
82 /* Thread to listen for SELinux status changes via netlink.  */
83 static pthread_t avc_notify_thread;
84
85 #ifdef HAVE_LIBAUDIT
86 /* Prototype for supporting the audit daemon */
87 static void log_callback (const char *fmt, ...);
88 #endif
89
90 /* Prototypes for AVC callback functions.  */
91 static void *avc_create_thread (void (*run) (void));
92 static void avc_stop_thread (void *thread);
93 static void *avc_alloc_lock (void);
94 static void avc_get_lock (void *lock);
95 static void avc_release_lock (void *lock);
96 static void avc_free_lock (void *lock);
97
98 /* AVC callback structures for use in avc_init.  */
99 static const struct avc_log_callback log_cb =
100 {
101 #ifdef HAVE_LIBAUDIT
102   .func_log = log_callback,
103 #else
104   .func_log = dbg_log,
105 #endif
106   .func_audit = NULL
107 };
108 static const struct avc_thread_callback thread_cb =
109 {
110   .func_create_thread = avc_create_thread,
111   .func_stop_thread = avc_stop_thread
112 };
113 static const struct avc_lock_callback lock_cb =
114 {
115   .func_alloc_lock = avc_alloc_lock,
116   .func_get_lock = avc_get_lock,
117   .func_release_lock = avc_release_lock,
118   .func_free_lock = avc_free_lock
119 };
120
121 #ifdef HAVE_LIBAUDIT
122 /* The audit system's netlink socket descriptor */
123 static int audit_fd = -1;
124
125 /* When an avc denial occurs, log it to audit system */
126 static void
127 log_callback (const char *fmt, ...)
128 {
129   if (audit_fd >= 0)
130     {
131       va_list ap;
132       va_start (ap, fmt);
133
134       char *buf;
135       int e = vasprintf (&buf, fmt, ap);
136       if (e < 0)
137         {
138           buf = alloca (BUFSIZ);
139           vsnprintf (buf, BUFSIZ, fmt, ap);
140         }
141
142       /* FIXME: need to attribute this to real user, using getuid for now */
143       audit_log_user_avc_message (audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
144                                   NULL, getuid ());
145
146       if (e >= 0)
147         free (buf);
148
149       va_end (ap);
150     }
151 }
152
153 /* Initialize the connection to the audit system */
154 static void
155 audit_init (void)
156 {
157   audit_fd = audit_open ();
158   if (audit_fd < 0
159       /* If kernel doesn't support audit, bail out */
160       && errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
161     dbg_log (_("Failed opening connection to the audit subsystem: %m"));
162 }
163
164
165 # ifdef HAVE_LIBCAP
166 static const cap_value_t new_cap_list[] =
167   { CAP_AUDIT_WRITE };
168 #  define nnew_cap_list (sizeof (new_cap_list) / sizeof (new_cap_list[0]))
169 static const cap_value_t tmp_cap_list[] =
170   { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
171 #  define ntmp_cap_list (sizeof (tmp_cap_list) / sizeof (tmp_cap_list[0]))
172
173 cap_t
174 preserve_capabilities (void)
175 {
176   if (getuid () != 0)
177     /* Not root, then we cannot preserve anything.  */
178     return NULL;
179
180   if (prctl (PR_SET_KEEPCAPS, 1) == -1)
181     {
182       dbg_log (_("Failed to set keep-capabilities"));
183       error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
184       /* NOTREACHED */
185     }
186
187   cap_t tmp_caps = cap_init ();
188   cap_t new_caps = NULL;
189   if (tmp_caps != NULL)
190     new_caps = cap_init ();
191
192   if (tmp_caps == NULL || new_caps == NULL)
193     {
194       if (tmp_caps != NULL)
195         cap_free (tmp_caps);
196
197       dbg_log (_("Failed to initialize drop of capabilities"));
198       error (EXIT_FAILURE, 0, _("cap_init failed"));
199     }
200
201   /* There is no reason why these should not work.  */
202   cap_set_flag (new_caps, CAP_PERMITTED, nnew_cap_list,
203                 (cap_value_t *) new_cap_list, CAP_SET);
204   cap_set_flag (new_caps, CAP_EFFECTIVE, nnew_cap_list,
205                 (cap_value_t *) new_cap_list, CAP_SET);
206
207   cap_set_flag (tmp_caps, CAP_PERMITTED, ntmp_cap_list,
208                 (cap_value_t *) tmp_cap_list, CAP_SET);
209   cap_set_flag (tmp_caps, CAP_EFFECTIVE, ntmp_cap_list,
210                 (cap_value_t *) tmp_cap_list, CAP_SET);
211
212   int res = cap_set_proc (tmp_caps);
213
214   cap_free (tmp_caps);
215
216   if (__builtin_expect (res != 0, 0))
217     {
218       cap_free (new_caps);
219       dbg_log (_("Failed to drop capabilities"));
220       error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
221     }
222
223   return new_caps;
224 }
225
226 void
227 install_real_capabilities (cap_t new_caps)
228 {
229   /* If we have no capabilities there is nothing to do here.  */
230   if (new_caps == NULL)
231     return;
232
233   if (cap_set_proc (new_caps))
234     {
235       cap_free (new_caps);
236       dbg_log (_("Failed to drop capabilities"));
237       error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
238       /* NOTREACHED */
239     }
240
241   cap_free (new_caps);
242
243   if (prctl (PR_SET_KEEPCAPS, 0) == -1)
244     {
245       dbg_log (_("Failed to unset keep-capabilities"));
246       error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
247       /* NOTREACHED */
248     }
249 }
250 # endif /* HAVE_LIBCAP */
251 #endif /* HAVE_LIBAUDIT */
252
253 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
254    to the result.  */
255 void
256 nscd_selinux_enabled (int *selinux_enabled)
257 {
258   *selinux_enabled = is_selinux_enabled ();
259   if (*selinux_enabled < 0)
260     {
261       dbg_log (_("Failed to determine if kernel supports SELinux"));
262       exit (EXIT_FAILURE);
263     }
264 }
265
266
267 /* Create thread for AVC netlink notification.  */
268 static void *
269 avc_create_thread (void (*run) (void))
270 {
271   int rc;
272
273   rc =
274     pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
275   if (rc != 0)
276     error (EXIT_FAILURE, rc, _("Failed to start AVC thread"));
277
278   return &avc_notify_thread;
279 }
280
281
282 /* Stop AVC netlink thread.  */
283 static void
284 avc_stop_thread (void *thread)
285 {
286   pthread_cancel (*(pthread_t *) thread);
287 }
288
289
290 /* Allocate a new AVC lock.  */
291 static void *
292 avc_alloc_lock (void)
293 {
294   pthread_mutex_t *avc_mutex;
295
296   avc_mutex = malloc (sizeof (pthread_mutex_t));
297   if (avc_mutex == NULL)
298     error (EXIT_FAILURE, errno, _("Failed to create AVC lock"));
299   pthread_mutex_init (avc_mutex, NULL);
300
301   return avc_mutex;
302 }
303
304
305 /* Acquire an AVC lock.  */
306 static void
307 avc_get_lock (void *lock)
308 {
309   pthread_mutex_lock (lock);
310 }
311
312
313 /* Release an AVC lock.  */
314 static void
315 avc_release_lock (void *lock)
316 {
317   pthread_mutex_unlock (lock);
318 }
319
320
321 /* Free an AVC lock.  */
322 static void
323 avc_free_lock (void *lock)
324 {
325   pthread_mutex_destroy (lock);
326   free (lock);
327 }
328
329
330 /* Initialize the user space access vector cache (AVC) for NSCD along with
331    log/thread/lock callbacks.  */
332 void
333 nscd_avc_init (void)
334 {
335   avc_entry_ref_init (&aeref);
336
337   if (avc_init ("avc", NULL, &log_cb, &thread_cb, &lock_cb) < 0)
338     error (EXIT_FAILURE, errno, _("Failed to start AVC"));
339   else
340     dbg_log (_("Access Vector Cache (AVC) started"));
341 #ifdef HAVE_LIBAUDIT
342   audit_init ();
343 #endif
344 }
345
346
347 /* Check the permission from the caller (via getpeercon) to nscd.
348    Returns 0 if access is allowed, 1 if denied, and -1 on error.  */
349 int
350 nscd_request_avc_has_perm (int fd, request_type req)
351 {
352   /* Initialize to NULL so we know what to free in case of failure.  */
353   security_context_t scon = NULL;
354   security_context_t tcon = NULL;
355   security_id_t ssid = NULL;
356   security_id_t tsid = NULL;
357   int rc = -1;
358
359   if (getpeercon (fd, &scon) < 0)
360     {
361       dbg_log (_("Error getting context of socket peer"));
362       goto out;
363     }
364   if (getcon (&tcon) < 0)
365     {
366       dbg_log (_("Error getting context of nscd"));
367       goto out;
368     }
369   if (avc_context_to_sid (scon, &ssid) < 0
370       || avc_context_to_sid (tcon, &tsid) < 0)
371     {
372       dbg_log (_("Error getting sid from context"));
373       goto out;
374     }
375
376 #ifndef NSCD__GETSERV
377   if (perms[req] == 0)
378     {
379       dbg_log (_("compile-time support for database policy missing"));
380       goto out;
381     }
382 #endif
383
384   rc = avc_has_perm (ssid, tsid, SECCLASS_NSCD, perms[req], &aeref, NULL) < 0;
385
386 out:
387   if (scon)
388     freecon (scon);
389   if (tcon)
390     freecon (tcon);
391   if (ssid)
392     sidput (ssid);
393   if (tsid)
394     sidput (tsid);
395
396   return rc;
397 }
398
399
400 /* Wrapper to get AVC statistics.  */
401 void
402 nscd_avc_cache_stats (struct avc_cache_stats *cstats)
403 {
404   avc_cache_stats (cstats);
405 }
406
407
408 /* Print the AVC statistics to stdout.  */
409 void
410 nscd_avc_print_stats (struct avc_cache_stats *cstats)
411 {
412   printf (_("\nSELinux AVC Statistics:\n\n"
413             "%15u  entry lookups\n"
414             "%15u  entry hits\n"
415             "%15u  entry misses\n"
416             "%15u  entry discards\n"
417             "%15u  CAV lookups\n"
418             "%15u  CAV hits\n"
419             "%15u  CAV probes\n"
420             "%15u  CAV misses\n"),
421           cstats->entry_lookups, cstats->entry_hits, cstats->entry_misses,
422           cstats->entry_discards, cstats->cav_lookups, cstats->cav_hits,
423           cstats->cav_probes, cstats->cav_misses);
424 }
425
426 #endif /* HAVE_SELINUX */