Back to index

glibc  2.9
selinux.c
Go to the documentation of this file.
00001 /* SELinux access controls for nscd.
00002    Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Matthew Rickard <mjricka@epoch.ncsc.mil>, 2004.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include "config.h"
00022 #include <error.h>
00023 #include <errno.h>
00024 #include <libintl.h>
00025 #include <pthread.h>
00026 #include <stdarg.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <syslog.h>
00030 #include <unistd.h>
00031 #include <sys/prctl.h>
00032 #include <selinux/av_permissions.h>
00033 #include <selinux/avc.h>
00034 #include <selinux/flask.h>
00035 #include <selinux/selinux.h>
00036 #ifdef HAVE_LIBAUDIT
00037 # include <libaudit.h>
00038 #endif
00039 
00040 #include "dbg_log.h"
00041 #include "selinux.h"
00042 
00043 
00044 #ifdef HAVE_SELINUX
00045 /* Global variable to tell if the kernel has SELinux support.  */
00046 int selinux_enabled;
00047 
00048 /* Define mappings of access vector permissions to request types.  */
00049 static const int perms[LASTREQ] =
00050 {
00051   [GETPWBYNAME] = NSCD__GETPWD,
00052   [GETPWBYUID] = NSCD__GETPWD,
00053   [GETGRBYNAME] = NSCD__GETGRP,
00054   [GETGRBYGID] = NSCD__GETGRP,
00055   [GETHOSTBYNAME] = NSCD__GETHOST,
00056   [GETHOSTBYNAMEv6] = NSCD__GETHOST,
00057   [GETHOSTBYADDR] = NSCD__GETHOST,
00058   [GETHOSTBYADDRv6] = NSCD__GETHOST,
00059   [GETSTAT] = NSCD__GETSTAT,
00060   [SHUTDOWN] = NSCD__ADMIN,
00061   [INVALIDATE] = NSCD__ADMIN,
00062   [GETFDPW] = NSCD__SHMEMPWD,
00063   [GETFDGR] = NSCD__SHMEMGRP,
00064   [GETFDHST] = NSCD__SHMEMHOST,
00065   [GETAI] = NSCD__GETHOST,
00066   [INITGROUPS] = NSCD__GETGRP,
00067 #ifdef NSCD__GETSERV
00068   [GETSERVBYNAME] = NSCD__GETSERV,
00069   [GETSERVBYPORT] = NSCD__GETSERV,
00070   [GETFDSERV] = NSCD__SHMEMSERV,
00071 #endif
00072 };
00073 
00074 /* Store an entry ref to speed AVC decisions.  */
00075 static struct avc_entry_ref aeref;
00076 
00077 /* Thread to listen for SELinux status changes via netlink.  */
00078 static pthread_t avc_notify_thread;
00079 
00080 #ifdef HAVE_LIBAUDIT
00081 /* Prototype for supporting the audit daemon */
00082 static void log_callback (const char *fmt, ...);
00083 #endif
00084 
00085 /* Prototypes for AVC callback functions.  */
00086 static void *avc_create_thread (void (*run) (void));
00087 static void avc_stop_thread (void *thread);
00088 static void *avc_alloc_lock (void);
00089 static void avc_get_lock (void *lock);
00090 static void avc_release_lock (void *lock);
00091 static void avc_free_lock (void *lock);
00092 
00093 /* AVC callback structures for use in avc_init.  */
00094 static const struct avc_log_callback log_cb =
00095 {
00096 #ifdef HAVE_LIBAUDIT
00097   .func_log = log_callback,
00098 #else
00099   .func_log = dbg_log,
00100 #endif
00101   .func_audit = NULL
00102 };
00103 static const struct avc_thread_callback thread_cb =
00104 {
00105   .func_create_thread = avc_create_thread,
00106   .func_stop_thread = avc_stop_thread
00107 };
00108 static const struct avc_lock_callback lock_cb =
00109 {
00110   .func_alloc_lock = avc_alloc_lock,
00111   .func_get_lock = avc_get_lock,
00112   .func_release_lock = avc_release_lock,
00113   .func_free_lock = avc_free_lock
00114 };
00115 
00116 #ifdef HAVE_LIBAUDIT
00117 /* The audit system's netlink socket descriptor */
00118 static int audit_fd = -1;
00119 
00120 /* When an avc denial occurs, log it to audit system */
00121 static void
00122 log_callback (const char *fmt, ...)
00123 {
00124   if (audit_fd >= 0)
00125     {
00126       va_list ap;
00127       va_start (ap, fmt);
00128 
00129       char *buf;
00130       int e = vasprintf (&buf, fmt, ap);
00131       if (e < 0)
00132        {
00133          buf = alloca (BUFSIZ);
00134          vsnprintf (buf, BUFSIZ, fmt, ap);
00135        }
00136 
00137       /* FIXME: need to attribute this to real user, using getuid for now */
00138       audit_log_user_avc_message (audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
00139                               NULL, getuid ());
00140 
00141       if (e >= 0)
00142        free (buf);
00143 
00144       va_end (ap);
00145     }
00146 }
00147 
00148 /* Initialize the connection to the audit system */
00149 static void
00150 audit_init (void)
00151 {
00152   audit_fd = audit_open ();
00153   if (audit_fd < 0
00154       /* If kernel doesn't support audit, bail out */
00155       && errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
00156     dbg_log (_("Failed opening connection to the audit subsystem: %m"));
00157 }
00158 
00159 
00160 # ifdef HAVE_LIBCAP
00161 static const cap_value_t new_cap_list[] =
00162   { CAP_AUDIT_WRITE };
00163 #  define nnew_cap_list (sizeof (new_cap_list) / sizeof (new_cap_list[0]))
00164 static const cap_value_t tmp_cap_list[] =
00165   { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
00166 #  define ntmp_cap_list (sizeof (tmp_cap_list) / sizeof (tmp_cap_list[0]))
00167 
00168 cap_t
00169 preserve_capabilities (void)
00170 {
00171   if (getuid () != 0)
00172     /* Not root, then we cannot preserve anything.  */
00173     return NULL;
00174 
00175   if (prctl (PR_SET_KEEPCAPS, 1) == -1)
00176     {
00177       dbg_log (_("Failed to set keep-capabilities"));
00178       error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
00179       /* NOTREACHED */
00180     }
00181 
00182   cap_t tmp_caps = cap_init ();
00183   cap_t new_caps = NULL;
00184   if (tmp_caps != NULL)
00185     new_caps = cap_init ();
00186 
00187   if (tmp_caps == NULL || new_caps == NULL)
00188     {
00189       if (tmp_caps != NULL)
00190        cap_free (tmp_caps);
00191 
00192       dbg_log (_("Failed to initialize drop of capabilities"));
00193       error (EXIT_FAILURE, 0, _("cap_init failed"));
00194     }
00195 
00196   /* There is no reason why these should not work.  */
00197   cap_set_flag (new_caps, CAP_PERMITTED, nnew_cap_list,
00198               (cap_value_t *) new_cap_list, CAP_SET);
00199   cap_set_flag (new_caps, CAP_EFFECTIVE, nnew_cap_list,
00200               (cap_value_t *) new_cap_list, CAP_SET);
00201 
00202   cap_set_flag (tmp_caps, CAP_PERMITTED, ntmp_cap_list,
00203               (cap_value_t *) tmp_cap_list, CAP_SET);
00204   cap_set_flag (tmp_caps, CAP_EFFECTIVE, ntmp_cap_list,
00205               (cap_value_t *) tmp_cap_list, CAP_SET);
00206 
00207   int res = cap_set_proc (tmp_caps);
00208 
00209   cap_free (tmp_caps);
00210 
00211   if (__builtin_expect (res != 0, 0))
00212     {
00213       cap_free (new_caps);
00214       dbg_log (_("Failed to drop capabilities"));
00215       error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
00216     }
00217 
00218   return new_caps;
00219 }
00220 
00221 void
00222 install_real_capabilities (cap_t new_caps)
00223 {
00224   /* If we have no capabilities there is nothing to do here.  */
00225   if (new_caps == NULL)
00226     return;
00227 
00228   if (cap_set_proc (new_caps))
00229     {
00230       cap_free (new_caps);
00231       dbg_log (_("Failed to drop capabilities"));
00232       error (EXIT_FAILURE, 0, _("cap_set_proc failed"));
00233       /* NOTREACHED */
00234     }
00235 
00236   cap_free (new_caps);
00237 
00238   if (prctl (PR_SET_KEEPCAPS, 0) == -1)
00239     {
00240       dbg_log (_("Failed to unset keep-capabilities"));
00241       error (EXIT_FAILURE, errno, _("prctl(KEEPCAPS) failed"));
00242       /* NOTREACHED */
00243     }
00244 }
00245 # endif /* HAVE_LIBCAP */
00246 #endif /* HAVE_LIBAUDIT */
00247 
00248 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
00249    to the result.  */
00250 void
00251 nscd_selinux_enabled (int *selinux_enabled)
00252 {
00253   *selinux_enabled = is_selinux_enabled ();
00254   if (*selinux_enabled < 0)
00255     {
00256       dbg_log (_("Failed to determine if kernel supports SELinux"));
00257       exit (EXIT_FAILURE);
00258     }
00259 }
00260 
00261 
00262 /* Create thread for AVC netlink notification.  */
00263 static void *
00264 avc_create_thread (void (*run) (void))
00265 {
00266   int rc;
00267 
00268   rc =
00269     pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
00270   if (rc != 0)
00271     error (EXIT_FAILURE, rc, _("Failed to start AVC thread"));
00272 
00273   return &avc_notify_thread;
00274 }
00275 
00276 
00277 /* Stop AVC netlink thread.  */
00278 static void
00279 avc_stop_thread (void *thread)
00280 {
00281   pthread_cancel (*(pthread_t *) thread);
00282 }
00283 
00284 
00285 /* Allocate a new AVC lock.  */
00286 static void *
00287 avc_alloc_lock (void)
00288 {
00289   pthread_mutex_t *avc_mutex;
00290 
00291   avc_mutex = malloc (sizeof (pthread_mutex_t));
00292   if (avc_mutex == NULL)
00293     error (EXIT_FAILURE, errno, _("Failed to create AVC lock"));
00294   pthread_mutex_init (avc_mutex, NULL);
00295 
00296   return avc_mutex;
00297 }
00298 
00299 
00300 /* Acquire an AVC lock.  */
00301 static void
00302 avc_get_lock (void *lock)
00303 {
00304   pthread_mutex_lock (lock);
00305 }
00306 
00307 
00308 /* Release an AVC lock.  */
00309 static void
00310 avc_release_lock (void *lock)
00311 {
00312   pthread_mutex_unlock (lock);
00313 }
00314 
00315 
00316 /* Free an AVC lock.  */
00317 static void
00318 avc_free_lock (void *lock)
00319 {
00320   pthread_mutex_destroy (lock);
00321   free (lock);
00322 }
00323 
00324 
00325 /* Initialize the user space access vector cache (AVC) for NSCD along with
00326    log/thread/lock callbacks.  */
00327 void
00328 nscd_avc_init (void)
00329 {
00330   avc_entry_ref_init (&aeref);
00331 
00332   if (avc_init ("avc", NULL, &log_cb, &thread_cb, &lock_cb) < 0)
00333     error (EXIT_FAILURE, errno, _("Failed to start AVC"));
00334   else
00335     dbg_log (_("Access Vector Cache (AVC) started"));
00336 #ifdef HAVE_LIBAUDIT
00337   audit_init ();
00338 #endif
00339 }
00340 
00341 
00342 /* Check the permission from the caller (via getpeercon) to nscd.
00343    Returns 0 if access is allowed, 1 if denied, and -1 on error.  */
00344 int
00345 nscd_request_avc_has_perm (int fd, request_type req)
00346 {
00347   /* Initialize to NULL so we know what to free in case of failure.  */
00348   security_context_t scon = NULL;
00349   security_context_t tcon = NULL;
00350   security_id_t ssid = NULL;
00351   security_id_t tsid = NULL;
00352   int rc = -1;
00353 
00354   if (getpeercon (fd, &scon) < 0)
00355     {
00356       dbg_log (_("Error getting context of socket peer"));
00357       goto out;
00358     }
00359   if (getcon (&tcon) < 0)
00360     {
00361       dbg_log (_("Error getting context of nscd"));
00362       goto out;
00363     }
00364   if (avc_context_to_sid (scon, &ssid) < 0
00365       || avc_context_to_sid (tcon, &tsid) < 0)
00366     {
00367       dbg_log (_("Error getting sid from context"));
00368       goto out;
00369     }
00370 
00371 #ifndef NSCD__GETSERV
00372   if (perms[req] == 0)
00373     {
00374       dbg_log (_("compile-time support for database policy missing"));
00375       goto out;
00376     }
00377 #endif
00378 
00379   rc = avc_has_perm (ssid, tsid, SECCLASS_NSCD, perms[req], &aeref, NULL) < 0;
00380 
00381 out:
00382   if (scon)
00383     freecon (scon);
00384   if (tcon)
00385     freecon (tcon);
00386   if (ssid)
00387     sidput (ssid);
00388   if (tsid)
00389     sidput (tsid);
00390 
00391   return rc;
00392 }
00393 
00394 
00395 /* Wrapper to get AVC statistics.  */
00396 void
00397 nscd_avc_cache_stats (struct avc_cache_stats *cstats)
00398 {
00399   avc_cache_stats (cstats);
00400 }
00401 
00402 
00403 /* Print the AVC statistics to stdout.  */
00404 void
00405 nscd_avc_print_stats (struct avc_cache_stats *cstats)
00406 {
00407   printf (_("\nSELinux AVC Statistics:\n\n"
00408            "%15u  entry lookups\n"
00409            "%15u  entry hits\n"
00410            "%15u  entry misses\n"
00411            "%15u  entry discards\n"
00412            "%15u  CAV lookups\n"
00413            "%15u  CAV hits\n"
00414            "%15u  CAV probes\n"
00415            "%15u  CAV misses\n"),
00416          cstats->entry_lookups, cstats->entry_hits, cstats->entry_misses,
00417          cstats->entry_discards, cstats->cav_lookups, cstats->cav_hits,
00418          cstats->cav_probes, cstats->cav_misses);
00419 }
00420 
00421 
00422 /* Clean up the AVC before exiting.  */
00423 void
00424 nscd_avc_destroy (void)
00425 {
00426   avc_destroy ();
00427 #ifdef HAVE_LIBAUDIT
00428   audit_close (audit_fd);
00429 #endif
00430 }
00431 
00432 #endif /* HAVE_SELINUX */