]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/selinux.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / nscd / selinux.c
1 /* SELinux access controls for nscd.
2 Copyright (C) 2004 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 <error.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <pthread.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <syslog.h>
29 #include <selinux/av_permissions.h>
30 #include <selinux/avc.h>
31 #include <selinux/flask.h>
32 #include <selinux/selinux.h>
33
34 #include "dbg_log.h"
35 #include "selinux.h"
36
37
38 #ifdef HAVE_SELINUX
39 /* Global variable to tell if the kernel has SELinux support. */
40 int selinux_enabled;
41
42 /* Define mappings of access vector permissions to request types. */
43 static const int perms[LASTREQ] =
44 {
45 [GETPWBYNAME] = NSCD__GETPWD,
46 [GETPWBYUID] = NSCD__GETPWD,
47 [GETGRBYNAME] = NSCD__GETGRP,
48 [GETGRBYGID] = NSCD__GETGRP,
49 [GETHOSTBYNAME] = NSCD__GETHOST,
50 [GETHOSTBYNAMEv6] = NSCD__GETHOST,
51 [GETHOSTBYADDR] = NSCD__GETHOST,
52 [GETHOSTBYADDRv6] = NSCD__GETHOST,
53 [GETSTAT] = NSCD__GETSTAT,
54 [SHUTDOWN] = NSCD__ADMIN,
55 [INVALIDATE] = NSCD__ADMIN,
56 [GETFDPW] = NSCD__SHMEMPWD,
57 [GETFDGR] = NSCD__SHMEMGRP,
58 [GETFDHST] = NSCD__SHMEMHOST,
59 [GETAI] = NSCD__GETHOST,
60 [INITGROUPS] = NSCD__GETGRP
61 };
62
63 /* Store an entry ref to speed AVC decisions. */
64 static struct avc_entry_ref aeref;
65
66 /* Thread to listen for SELinux status changes via netlink. */
67 static pthread_t avc_notify_thread;
68
69 /* Prototypes for AVC callback functions. */
70 static void *avc_create_thread (void (*run) (void));
71 static void avc_stop_thread (void *thread);
72 static void *avc_alloc_lock (void);
73 static void avc_get_lock (void *lock);
74 static void avc_release_lock (void *lock);
75 static void avc_free_lock (void *lock);
76
77 /* AVC callback structures for use in avc_init. */
78 static const struct avc_log_callback log_cb =
79 {
80 .func_log = dbg_log,
81 .func_audit = NULL
82 };
83 static const struct avc_thread_callback thread_cb =
84 {
85 .func_create_thread = avc_create_thread,
86 .func_stop_thread = avc_stop_thread
87 };
88 static const struct avc_lock_callback lock_cb =
89 {
90 .func_alloc_lock = avc_alloc_lock,
91 .func_get_lock = avc_get_lock,
92 .func_release_lock = avc_release_lock,
93 .func_free_lock = avc_free_lock
94 };
95
96
97 /* Determine if we are running on an SELinux kernel. Set selinux_enabled
98 to the result. */
99 void
100 nscd_selinux_enabled (int *selinux_enabled)
101 {
102 *selinux_enabled = is_selinux_enabled ();
103 if (*selinux_enabled < 0)
104 {
105 dbg_log (_("Failed to determine if kernel supports SELinux"));
106 exit (EXIT_FAILURE);
107 }
108 }
109
110
111 /* Create thread for AVC netlink notification. */
112 static void *
113 avc_create_thread (void (*run) (void))
114 {
115 int rc;
116
117 rc =
118 pthread_create (&avc_notify_thread, NULL, (void *(*) (void *)) run, NULL);
119 if (rc != 0)
120 error (EXIT_FAILURE, rc, _("Failed to start AVC thread"));
121
122 return &avc_notify_thread;
123 }
124
125
126 /* Stop AVC netlink thread. */
127 static void
128 avc_stop_thread (void *thread)
129 {
130 pthread_cancel (*(pthread_t *) thread);
131 }
132
133
134 /* Allocate a new AVC lock. */
135 static void *
136 avc_alloc_lock (void)
137 {
138 pthread_mutex_t *avc_mutex;
139
140 avc_mutex = malloc (sizeof (pthread_mutex_t));
141 if (avc_mutex == NULL)
142 error (EXIT_FAILURE, errno, _("Failed to create AVC lock"));
143 pthread_mutex_init (avc_mutex, NULL);
144
145 return avc_mutex;
146 }
147
148
149 /* Acquire an AVC lock. */
150 static void
151 avc_get_lock (void *lock)
152 {
153 pthread_mutex_lock (lock);
154 }
155
156
157 /* Release an AVC lock. */
158 static void
159 avc_release_lock (void *lock)
160 {
161 pthread_mutex_unlock (lock);
162 }
163
164
165 /* Free an AVC lock. */
166 static void
167 avc_free_lock (void *lock)
168 {
169 pthread_mutex_destroy (lock);
170 free (lock);
171 }
172
173
174 /* Initialize the user space access vector cache (AVC) for NSCD along with
175 log/thread/lock callbacks. */
176 void
177 nscd_avc_init (void)
178 {
179 avc_entry_ref_init (&aeref);
180
181 if (avc_init ("avc", NULL, &log_cb, &thread_cb, &lock_cb) < 0)
182 error (EXIT_FAILURE, errno, _("Failed to start AVC"));
183 else
184 dbg_log (_("Access Vector Cache (AVC) started"));
185 }
186
187
188 /* Check the permission from the caller (via getpeercon) to nscd.
189 Returns 0 if access is allowed, 1 if denied, and -1 on error. */
190 int
191 nscd_request_avc_has_perm (int fd, request_type req)
192 {
193 /* Initialize to NULL so we know what to free in case of failure. */
194 security_context_t scon = NULL;
195 security_context_t tcon = NULL;
196 security_id_t ssid = NULL;
197 security_id_t tsid = NULL;
198 int rc = -1;
199
200 if (getpeercon (fd, &scon) < 0)
201 {
202 dbg_log (_("Error getting context of socket peer"));
203 goto out;
204 }
205 if (getcon (&tcon) < 0)
206 {
207 dbg_log (_("Error getting context of nscd"));
208 goto out;
209 }
210 if (avc_context_to_sid (scon, &ssid) < 0
211 || avc_context_to_sid (tcon, &tsid) < 0)
212 {
213 dbg_log (_("Error getting sid from context"));
214 goto out;
215 }
216
217 rc = avc_has_perm (ssid, tsid, SECCLASS_NSCD, perms[req], &aeref, NULL) < 0;
218
219 out:
220 if (scon)
221 freecon (scon);
222 if (tcon)
223 freecon (tcon);
224 if (ssid)
225 sidput (ssid);
226 if (tsid)
227 sidput (tsid);
228
229 return rc;
230 }
231
232
233 /* Wrapper to get AVC statistics. */
234 void
235 nscd_avc_cache_stats (struct avc_cache_stats *cstats)
236 {
237 avc_cache_stats (cstats);
238 }
239
240
241 /* Print the AVC statistics to stdout. */
242 void
243 nscd_avc_print_stats (struct avc_cache_stats *cstats)
244 {
245 printf (_("\nSELinux AVC Statistics:\n\n"
246 "%15u entry lookups\n"
247 "%15u entry hits\n"
248 "%15u entry misses\n"
249 "%15u entry discards\n"
250 "%15u CAV lookups\n"
251 "%15u CAV hits\n"
252 "%15u CAV probes\n"
253 "%15u CAV misses\n"),
254 cstats->entry_lookups, cstats->entry_hits, cstats->entry_misses,
255 cstats->entry_discards, cstats->cav_lookups, cstats->cav_hits,
256 cstats->cav_probes, cstats->cav_misses);
257 }
258
259
260 /* Clean up the AVC before exiting. */
261 void
262 nscd_avc_destroy (void)
263 {
264 avc_destroy ();
265 }
266
267 #endif /* HAVE_SELINUX */