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
30 #include <selinux/av_permissions.h>
31 #include <selinux/avc.h>
32 #include <selinux/flask.h>
33 #include <selinux/selinux.h>
43 /* Global variable to tell if the kernel has SELinux support. */
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
;
74 /* Prototype for supporting the audit daemon */
75 static void log_callback (const char *fmt
, ...);
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
=
90 .func_log
= log_callback
,
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
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 */
115 log_callback (const char *fmt
, ...)
120 audit_log_avc (audit_fd
, AUDIT_USER_AVC
, fmt
, ap
);
124 /* Initialize the connection to the audit system */
128 audit_fd
= audit_open ();
130 dbg_log (_("Failed opening connection to the audit subsystem: %m"));
132 #endif /* HAVE_LIBAUDIT */
134 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
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"));
148 /* Create thread for AVC netlink notification. */
150 avc_create_thread (void (*run
) (void))
155 pthread_create (&avc_notify_thread
, NULL
, (void *(*) (void *)) run
, NULL
);
157 error (EXIT_FAILURE
, rc
, _("Failed to start AVC thread"));
159 return &avc_notify_thread
;
163 /* Stop AVC netlink thread. */
165 avc_stop_thread (void *thread
)
167 pthread_cancel (*(pthread_t
*) thread
);
171 /* Allocate a new AVC lock. */
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
);
186 /* Acquire an AVC lock. */
188 avc_get_lock (void *lock
)
190 pthread_mutex_lock (lock
);
194 /* Release an AVC lock. */
196 avc_release_lock (void *lock
)
198 pthread_mutex_unlock (lock
);
202 /* Free an AVC lock. */
204 avc_free_lock (void *lock
)
206 pthread_mutex_destroy (lock
);
211 /* Initialize the user space access vector cache (AVC) for NSCD along with
212 log/thread/lock callbacks. */
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"));
221 dbg_log (_("Access Vector Cache (AVC) started"));
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
;
240 if (getpeercon (fd
, &scon
) < 0)
242 dbg_log (_("Error getting context of socket peer"));
245 if (getcon (&tcon
) < 0)
247 dbg_log (_("Error getting context of nscd"));
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"));
257 rc
= avc_has_perm (ssid
, tsid
, SECCLASS_NSCD
, perms
[req
], &aeref
, NULL
) < 0;
273 /* Wrapper to get AVC statistics. */
275 nscd_avc_cache_stats (struct avc_cache_stats
*cstats
)
277 avc_cache_stats (cstats
);
281 /* Print the AVC statistics to stdout. */
283 nscd_avc_print_stats (struct avc_cache_stats
*cstats
)
285 printf (_("\nSELinux AVC Statistics:\n\n"
286 "%15u entry lookups\n"
288 "%15u entry misses\n"
289 "%15u entry discards\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. */
302 nscd_avc_destroy (void)
306 audit_close (audit_fd
);
310 #endif /* HAVE_SELINUX */