blob: e6d76b600b0d4122f223bfb01153500e93f6fff7 [file] [log] [blame]
paul01245822003-05-20 01:22:17 +00001/*
2 * Zebra privileges.
3 *
4 * Copyright (C) 2003 Paul Jakma.
Brian Bennett5cd0e5c2015-02-17 23:24:15 +00005 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
paul01245822003-05-20 01:22:17 +00006 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
paul01245822003-05-20 01:22:17 +000024#include <zebra.h>
25#include "log.h"
26#include "privs.h"
27#include "memory.h"
paul01245822003-05-20 01:22:17 +000028
paulceacedb2005-09-29 14:39:32 +000029#ifdef HAVE_CAPABILITIES
30/* sort out some generic internal types for:
31 *
32 * privilege values (cap_value_t, priv_t) -> pvalue_t
33 * privilege set (..., priv_set_t) -> pset_t
34 * privilege working storage (cap_t, ...) -> pstorage_t
35 *
36 * values we think of as numeric (they're ints really, but we dont know)
37 * sets are mostly opaque, to hold a set of privileges, related in some way.
38 * storage binds together a set of sets we're interested in.
39 * (in reality: cap_value_t and priv_t are ints)
40 */
41#ifdef HAVE_LCAPS
42/* Linux doesn't have a 'set' type: a set of related privileges */
43struct _pset {
44 int num;
45 cap_value_t *caps;
46};
47typedef cap_value_t pvalue_t;
48typedef struct _pset pset_t;
49typedef cap_t pstorage_t;
David Lamparter6b0655a2014-06-04 06:53:35 +020050
paulceacedb2005-09-29 14:39:32 +000051#elif defined (HAVE_SOLARIS_CAPABILITIES)
52typedef priv_t pvalue_t;
53typedef priv_set_t pset_t;
54typedef priv_set_t *pstorage_t;
55#else /* neither LCAPS nor SOLARIS_CAPABILITIES */
56#error "HAVE_CAPABILITIES defined, but neither LCAPS nor Solaris Capabilties!"
57#endif /* HAVE_LCAPS */
58#endif /* HAVE_CAPABILITIES */
David Lamparter6b0655a2014-06-04 06:53:35 +020059
paulceacedb2005-09-29 14:39:32 +000060/* the default NULL state we report is RAISED, but could be LOWERED if
61 * zprivs_terminate is called and the NULL handler is installed.
62 */
63static zebra_privs_current_t zprivs_null_state = ZPRIVS_RAISED;
64
paul01245822003-05-20 01:22:17 +000065/* internal privileges state */
66static struct _zprivs_t
67{
paulceacedb2005-09-29 14:39:32 +000068#ifdef HAVE_CAPABILITIES
69 pstorage_t caps; /* working storage */
70 pset_t *syscaps_p; /* system-type requested permitted caps */
71 pset_t *syscaps_i; /* system-type requested inheritable caps */
72#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +000073 uid_t zuid, /* uid to run as */
74 zsuid; /* saved uid */
75 gid_t zgid; /* gid to run as */
hassoba3a0bc2003-06-04 17:41:54 +000076 gid_t vtygrp; /* gid for vty sockets */
paul01245822003-05-20 01:22:17 +000077} zprivs_state;
78
79/* externally exported but not directly accessed functions */
paulceacedb2005-09-29 14:39:32 +000080#ifdef HAVE_CAPABILITIES
paul01245822003-05-20 01:22:17 +000081int zprivs_change_caps (zebra_privs_ops_t);
82zebra_privs_current_t zprivs_state_caps (void);
paulceacedb2005-09-29 14:39:32 +000083#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +000084int zprivs_change_uid (zebra_privs_ops_t);
85zebra_privs_current_t zprivs_state_uid (void);
86int zprivs_change_null (zebra_privs_ops_t);
87zebra_privs_current_t zprivs_state_null (void);
paul01245822003-05-20 01:22:17 +000088
paulceacedb2005-09-29 14:39:32 +000089#ifdef HAVE_CAPABILITIES
90/* internal capability API */
91static pset_t *zcaps2sys (zebra_capabilities_t *, int);
92static void zprivs_caps_init (struct zebra_privs_t *);
93static void zprivs_caps_terminate (void);
94
95/* Map of Quagga abstract capabilities to system capabilities */
96static struct
paul01245822003-05-20 01:22:17 +000097{
paulceacedb2005-09-29 14:39:32 +000098 int num;
99 pvalue_t *system_caps;
100} cap_map [ZCAP_MAX] =
101{
102#ifdef HAVE_LCAPS /* Quagga -> Linux capabilities mappings */
103 [ZCAP_SETID] = { 2, (pvalue_t []) { CAP_SETGID,
104 CAP_SETUID }, },
Christian Frankea5d589d2015-05-13 13:59:18 +0200105 [ZCAP_BIND] = { 2, (pvalue_t []) { CAP_NET_BIND_SERVICE }, },
paulceacedb2005-09-29 14:39:32 +0000106 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { CAP_NET_ADMIN }, },
107 [ZCAP_NET_RAW] = { 1, (pvalue_t []) { CAP_NET_RAW }, },
108 [ZCAP_CHROOT] = { 1, (pvalue_t []) { CAP_SYS_CHROOT, }, },
109 [ZCAP_NICE] = { 1, (pvalue_t []) { CAP_SYS_NICE }, },
110 [ZCAP_PTRACE] = { 1, (pvalue_t []) { CAP_SYS_PTRACE }, },
111 [ZCAP_DAC_OVERRIDE] = { 1, (pvalue_t []) { CAP_DAC_OVERRIDE }, },
112 [ZCAP_READ_SEARCH] = { 1, (pvalue_t []) { CAP_DAC_READ_SEARCH }, },
113 [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { CAP_SYS_ADMIN }, },
114 [ZCAP_FOWNER] = { 1, (pvalue_t []) { CAP_FOWNER }, },
115#elif defined(HAVE_SOLARIS_CAPABILITIES) /* HAVE_LCAPS */
116 /* Quagga -> Solaris privilege mappings */
117 [ZCAP_SETID] = { 1, (pvalue_t []) { PRIV_PROC_SETID }, },
118 [ZCAP_BIND] = { 1, (pvalue_t []) { PRIV_NET_PRIVADDR }, },
Paul Jakma6b148fa2007-09-18 18:07:18 +0000119 /* IP_CONFIG is a subset of NET_CONFIG and is allowed in zones */
120#ifdef PRIV_SYS_IP_CONFIG
121 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_IP_CONFIG }, },
122#else
paulceacedb2005-09-29 14:39:32 +0000123 [ZCAP_NET_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_NET_CONFIG }, },
Paul Jakma6b148fa2007-09-18 18:07:18 +0000124#endif
paulceacedb2005-09-29 14:39:32 +0000125 [ZCAP_NET_RAW] = { 2, (pvalue_t []) { PRIV_NET_RAWACCESS,
126 PRIV_NET_ICMPACCESS }, },
127 [ZCAP_CHROOT] = { 1, (pvalue_t []) { PRIV_PROC_CHROOT }, },
128 [ZCAP_NICE] = { 1, (pvalue_t []) { PRIV_PROC_PRIOCNTL }, },
129 [ZCAP_PTRACE] = { 1, (pvalue_t []) { PRIV_PROC_SESSION }, },
130 [ZCAP_DAC_OVERRIDE] = { 2, (pvalue_t []) { PRIV_FILE_DAC_EXECUTE,
131 PRIV_FILE_DAC_READ,
132 PRIV_FILE_DAC_SEARCH,
133 PRIV_FILE_DAC_WRITE,
134 PRIV_FILE_DAC_SEARCH }, },
135 [ZCAP_READ_SEARCH] = { 2, (pvalue_t []) { PRIV_FILE_DAC_SEARCH,
136 PRIV_FILE_DAC_READ }, },
137 [ZCAP_SYS_ADMIN] = { 1, (pvalue_t []) { PRIV_SYS_ADMIN }, },
138 [ZCAP_FOWNER] = { 1, (pvalue_t []) { PRIV_FILE_OWNER }, },
139#endif /* HAVE_SOLARIS_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000140};
David Lamparter6b0655a2014-06-04 06:53:35 +0200141
paulceacedb2005-09-29 14:39:32 +0000142#ifdef HAVE_LCAPS
143/* Linux forms of capabilities methods */
paul01245822003-05-20 01:22:17 +0000144/* convert zebras privileges to system capabilities */
paulceacedb2005-09-29 14:39:32 +0000145static pset_t *
paul01245822003-05-20 01:22:17 +0000146zcaps2sys (zebra_capabilities_t *zcaps, int num)
147{
paulceacedb2005-09-29 14:39:32 +0000148 pset_t *syscaps;
149 int i, j = 0, count = 0;
paul01245822003-05-20 01:22:17 +0000150
151 if (!num)
152 return NULL;
paulceacedb2005-09-29 14:39:32 +0000153
154 /* first count up how many system caps we have */
155 for (i= 0; i < num; i++)
156 count += cap_map[zcaps[i]].num;
157
158 if ( (syscaps = XCALLOC (MTYPE_PRIVS, (sizeof(pset_t) * num))) == NULL)
paul01245822003-05-20 01:22:17 +0000159 {
paulceacedb2005-09-29 14:39:32 +0000160 fprintf (stderr, "%s: could not allocate syscaps!", __func__);
paul01245822003-05-20 01:22:17 +0000161 return NULL;
162 }
163
paulceacedb2005-09-29 14:39:32 +0000164 syscaps->caps = XCALLOC (MTYPE_PRIVS, (sizeof (pvalue_t) * count));
165
166 if (!syscaps->caps)
paul01245822003-05-20 01:22:17 +0000167 {
paulceacedb2005-09-29 14:39:32 +0000168 fprintf (stderr, "%s: could not XCALLOC caps!", __func__);
169 return NULL;
paul01245822003-05-20 01:22:17 +0000170 }
paulceacedb2005-09-29 14:39:32 +0000171
172 /* copy the capabilities over */
173 count = 0;
174 for (i=0; i < num; i++)
175 for (j = 0; j < cap_map[zcaps[i]].num; j++)
176 syscaps->caps[count++] = cap_map[zcaps[i]].system_caps[j];
177
178 /* iterations above should be exact same as previous count, obviously.. */
179 syscaps->num = count;
180
paul01245822003-05-20 01:22:17 +0000181 return syscaps;
182}
183
184/* set or clear the effective capabilities to/from permitted */
185int
186zprivs_change_caps (zebra_privs_ops_t op)
187{
188 cap_flag_value_t cflag;
189
paulceacedb2005-09-29 14:39:32 +0000190 /* should be no possibility of being called without valid caps */
191 assert (zprivs_state.syscaps_p && zprivs_state.caps);
192 if (! (zprivs_state.syscaps_p && zprivs_state.caps))
193 exit (1);
194
paul01245822003-05-20 01:22:17 +0000195 if (op == ZPRIVS_RAISE)
196 cflag = CAP_SET;
197 else if (op == ZPRIVS_LOWER)
198 cflag = CAP_CLEAR;
199 else
200 return -1;
201
202 if ( !cap_set_flag (zprivs_state.caps, CAP_EFFECTIVE,
paulceacedb2005-09-29 14:39:32 +0000203 zprivs_state.syscaps_p->num,
204 zprivs_state.syscaps_p->caps,
205 cflag))
paul01245822003-05-20 01:22:17 +0000206 return cap_set_proc (zprivs_state.caps);
207 return -1;
208}
209
210zebra_privs_current_t
211zprivs_state_caps (void)
212{
213 int i;
paul01245822003-05-20 01:22:17 +0000214 cap_flag_value_t val;
paulceacedb2005-09-29 14:39:32 +0000215
216 /* should be no possibility of being called without valid caps */
217 assert (zprivs_state.syscaps_p && zprivs_state.caps);
218 if (! (zprivs_state.syscaps_p && zprivs_state.caps))
219 exit (1);
paul01245822003-05-20 01:22:17 +0000220
paulceacedb2005-09-29 14:39:32 +0000221 for (i=0; i < zprivs_state.syscaps_p->num; i++)
paul01245822003-05-20 01:22:17 +0000222 {
paulceacedb2005-09-29 14:39:32 +0000223 if ( cap_get_flag (zprivs_state.caps, zprivs_state.syscaps_p->caps[i],
paul01245822003-05-20 01:22:17 +0000224 CAP_EFFECTIVE, &val) )
paulceacedb2005-09-29 14:39:32 +0000225 {
226 zlog_warn ("zprivs_state_caps: could not cap_get_flag, %s",
227 safe_strerror (errno) );
228 return ZPRIVS_UNKNOWN;
229 }
paul01245822003-05-20 01:22:17 +0000230 if (val == CAP_SET)
paul33b72942003-05-20 02:22:42 +0000231 return ZPRIVS_RAISED;
paul01245822003-05-20 01:22:17 +0000232 }
233 return ZPRIVS_LOWERED;
234}
235
paulceacedb2005-09-29 14:39:32 +0000236static void
237zprivs_caps_init (struct zebra_privs_t *zprivs)
238{
239 zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
240 zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
paul01245822003-05-20 01:22:17 +0000241
paulceacedb2005-09-29 14:39:32 +0000242 /* Tell kernel we want caps maintained across uid changes */
243 if ( prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1 )
244 {
245 fprintf (stderr, "privs_init: could not set PR_SET_KEEPCAPS, %s\n",
246 safe_strerror (errno) );
247 exit(1);
248 }
249
250 if ( !zprivs_state.syscaps_p )
251 {
252 fprintf (stderr, "privs_init: capabilities enabled, "
253 "but no capabilities supplied\n");
254 }
255
paulceacedb2005-09-29 14:39:32 +0000256 /* we have caps, we have no need to ever change back the original user */
257 if (zprivs_state.zuid)
258 {
259 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
260 {
261 fprintf (stderr, "zprivs_init (cap): could not setreuid, %s\n",
262 safe_strerror (errno));
263 exit (1);
264 }
265 }
266
Paul Jakma924f9d32006-03-30 13:25:52 +0000267 if ( !(zprivs_state.caps = cap_init()) )
268 {
269 fprintf (stderr, "privs_init: failed to cap_init, %s\n",
270 safe_strerror (errno));
271 exit (1);
272 }
273
paulceacedb2005-09-29 14:39:32 +0000274 if ( cap_clear (zprivs_state.caps) )
275 {
276 fprintf (stderr, "privs_init: failed to cap_clear, %s\n",
277 safe_strerror (errno));
278 exit (1);
279 }
280
281 /* set permitted caps */
282 cap_set_flag(zprivs_state.caps, CAP_PERMITTED,
283 zprivs_state.syscaps_p->num,
284 zprivs_state.syscaps_p->caps,
285 CAP_SET);
286
287 /* set inheritable caps, if any */
288 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
289 {
290 cap_set_flag(zprivs_state.caps, CAP_INHERITABLE,
291 zprivs_state.syscaps_i->num,
292 zprivs_state.syscaps_i->caps,
293 CAP_SET);
294 }
295
296 /* apply caps. CAP_EFFECTIVE is cleared. we'll raise the caps as
297 * and when, and only when, they are needed.
298 */
299 if ( cap_set_proc (zprivs_state.caps) )
300 {
Christian Franke30657772015-05-13 13:59:17 +0200301 cap_t current_caps;
302 char *current_caps_text = NULL;
303 char *wanted_caps_text = NULL;
304
305 fprintf(stderr, "privs_init: initial cap_set_proc failed: %s\n",
306 safe_strerror(errno));
307
308 current_caps = cap_get_proc();
309 if (current_caps)
310 current_caps_text = cap_to_text(current_caps, NULL);
311
312 wanted_caps_text = cap_to_text(zprivs_state.caps, NULL);
313 fprintf(stderr, "Wanted caps: %s\n", wanted_caps_text ? wanted_caps_text : "???");
314 fprintf(stderr, "Have caps: %s\n", current_caps_text ? current_caps_text : "???");
315
paulceacedb2005-09-29 14:39:32 +0000316 exit (1);
317 }
318
319 /* set methods for the caller to use */
320 zprivs->change = zprivs_change_caps;
321 zprivs->current_state = zprivs_state_caps;
322}
323
324static void
325zprivs_caps_terminate (void)
326{
327 /* clear all capabilities */
328 if (zprivs_state.caps)
329 cap_clear (zprivs_state.caps);
330
331 /* and boom, capabilities are gone forever */
332 if ( cap_set_proc (zprivs_state.caps) )
333 {
334 fprintf (stderr, "privs_terminate: cap_set_proc failed, %s",
335 safe_strerror (errno) );
336 exit (1);
337 }
338
339 /* free up private state */
340 if (zprivs_state.syscaps_p->num)
341 {
342 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p->caps);
343 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_p);
344 }
345
346 if (zprivs_state.syscaps_i && zprivs_state.syscaps_i->num)
347 {
348 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i->caps);
349 XFREE (MTYPE_PRIVS, zprivs_state.syscaps_i);
350 }
351
352 cap_free (zprivs_state.caps);
353}
354#elif defined (HAVE_SOLARIS_CAPABILITIES) /* !HAVE_LCAPS */
David Lamparter6b0655a2014-06-04 06:53:35 +0200355
paulceacedb2005-09-29 14:39:32 +0000356/* Solaris specific capability/privilege methods
357 *
358 * Resources:
359 * - the 'privileges' man page
360 * - http://cvs.opensolaris.org
361 * - http://blogs.sun.com/roller/page/gbrunett?entry=privilege_enabling_set_id_programs1
362 */
363
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000364static pset_t *
365zprivs_caps_minimal ()
366{
367 pset_t *minimal;
368
369 if ((minimal = priv_str_to_set("basic", ",", NULL)) == NULL)
370 {
371 fprintf (stderr, "%s: couldn't get basic set!\n", __func__);
372 exit (1);
373 }
374
375 /* create a minimal privilege set from the basic set */
376 (void) priv_delset(minimal, PRIV_PROC_EXEC);
377 (void) priv_delset(minimal, PRIV_PROC_INFO);
378 (void) priv_delset(minimal, PRIV_PROC_SESSION);
379 (void) priv_delset(minimal, PRIV_FILE_LINK_ANY);
380
381 return minimal;
382}
383
paulceacedb2005-09-29 14:39:32 +0000384/* convert zebras privileges to system capabilities */
385static pset_t *
386zcaps2sys (zebra_capabilities_t *zcaps, int num)
387{
388 pset_t *syscaps;
paul6e0f1b92005-11-24 12:47:17 +0000389 int i, j = 0;
paulceacedb2005-09-29 14:39:32 +0000390
391 if ((syscaps = priv_allocset()) == NULL)
392 {
393 fprintf (stderr, "%s: could not allocate syscaps!\n", __func__);
394 exit (1);
395 }
396
397 priv_emptyset (syscaps);
398
399 for (i=0; i < num; i++)
400 for (j = 0; j < cap_map[zcaps[i]].num; j++)
401 priv_addset (syscaps, cap_map[zcaps[i]].system_caps[j]);
402
403 return syscaps;
404}
405
406/* callback exported to users to RAISE and LOWER effective privileges
407 * from nothing to the given permitted set and back down
408 */
409int
410zprivs_change_caps (zebra_privs_ops_t op)
411{
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000412 pset_t *privset;
paulceacedb2005-09-29 14:39:32 +0000413
414 /* should be no possibility of being called without valid caps */
415 assert (zprivs_state.syscaps_p);
416 if (!zprivs_state.syscaps_p)
417 {
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000418 fprintf (stderr, "%s: Eek, missing privileged caps!", __func__);
419 exit (1);
420 }
421
422 assert (zprivs_state.caps);
423 if (!zprivs_state.caps)
424 {
paulceacedb2005-09-29 14:39:32 +0000425 fprintf (stderr, "%s: Eek, missing caps!", __func__);
426 exit (1);
427 }
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000428
429 /* to raise: copy original permitted as our working effective set
430 * to lower: copy regular effective set stored in zprivs_state.caps
paulceacedb2005-09-29 14:39:32 +0000431 */
432 if (op == ZPRIVS_RAISE)
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000433 privset = zprivs_state.syscaps_p;
paulceacedb2005-09-29 14:39:32 +0000434 else if (op == ZPRIVS_LOWER)
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000435 privset = zprivs_state.caps;
paulceacedb2005-09-29 14:39:32 +0000436 else
437 return -1;
438
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000439 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, privset) != 0)
paulceacedb2005-09-29 14:39:32 +0000440 return -1;
441
442 return 0;
443}
444
445/* Retrieve current privilege state, is it RAISED or LOWERED? */
446zebra_privs_current_t
447zprivs_state_caps (void)
448{
449 zebra_privs_current_t result;
450 pset_t *effective;
451
452 if ( (effective = priv_allocset()) == NULL)
453 {
paul6e0f1b92005-11-24 12:47:17 +0000454 fprintf (stderr, "%s: failed to get priv_allocset! %s\n", __func__,
paulceacedb2005-09-29 14:39:32 +0000455 safe_strerror (errno));
456 return ZPRIVS_UNKNOWN;
457 }
458
459 if (getppriv (PRIV_EFFECTIVE, effective))
460 {
paul6e0f1b92005-11-24 12:47:17 +0000461 fprintf (stderr, "%s: failed to get state! %s\n", __func__,
paulceacedb2005-09-29 14:39:32 +0000462 safe_strerror (errno));
463 result = ZPRIVS_UNKNOWN;
464 }
465 else
466 {
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000467 if (priv_isequalset (effective, zprivs_state.syscaps_p))
468 result = ZPRIVS_RAISED;
469 else if (priv_isequalset (effective, zprivs_state.caps))
paulceacedb2005-09-29 14:39:32 +0000470 result = ZPRIVS_LOWERED;
471 else
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000472 result = ZPRIVS_UNKNOWN;
paulceacedb2005-09-29 14:39:32 +0000473 }
474
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000475 priv_freeset (effective);
paulceacedb2005-09-29 14:39:32 +0000476 return result;
477}
478
479static void
480zprivs_caps_init (struct zebra_privs_t *zprivs)
481{
482 pset_t *basic;
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000483 pset_t *minimal;
paulceacedb2005-09-29 14:39:32 +0000484
485 /* the specified sets */
486 zprivs_state.syscaps_p = zcaps2sys (zprivs->caps_p, zprivs->cap_num_p);
487 zprivs_state.syscaps_i = zcaps2sys (zprivs->caps_i, zprivs->cap_num_i);
488
489 /* nonsensical to have gotten here but not have capabilities */
490 if (!zprivs_state.syscaps_p)
491 {
492 fprintf (stderr, "%s: capabilities enabled, "
493 "but no valid capabilities supplied\n",
494 __func__);
495 }
496
497 /* We retain the basic set in our permitted set, as Linux has no
498 * equivalent. The basic set on Linux hence is implicit, always
499 * there.
500 */
501 if ((basic = priv_str_to_set("basic", ",", NULL)) == NULL)
502 {
503 fprintf (stderr, "%s: couldn't get basic set!\n", __func__);
504 exit (1);
505 }
506
507 /* Add the basic set to the permitted set */
508 priv_union (basic, zprivs_state.syscaps_p);
509 priv_freeset (basic);
510
paulceacedb2005-09-29 14:39:32 +0000511 /* Hey kernel, we know about privileges!
512 * this isn't strictly required, use of setppriv should have same effect
513 */
514 if (setpflags (PRIV_AWARE, 1))
515 {
516 fprintf (stderr, "%s: error setting PRIV_AWARE!, %s\n", __func__,
517 safe_strerror (errno) );
518 exit (1);
519 }
520
521 /* need either valid or empty sets for both p and i.. */
522 assert (zprivs_state.syscaps_i && zprivs_state.syscaps_p);
523
Paul Jakma924f9d32006-03-30 13:25:52 +0000524 /* we have caps, we have no need to ever change back the original user
525 * change real, effective and saved to the specified user.
526 */
527 if (zprivs_state.zuid)
528 {
529 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
530 {
531 fprintf (stderr, "%s: could not setreuid, %s\n",
532 __func__, safe_strerror (errno));
533 exit (1);
534 }
535 }
536
paulceacedb2005-09-29 14:39:32 +0000537 /* set the permitted set */
538 if (setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.syscaps_p))
539 {
540 fprintf (stderr, "%s: error setting permitted set!, %s\n", __func__,
541 safe_strerror (errno) );
542 exit (1);
543 }
544
545 /* set the inheritable set */
546 if (setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.syscaps_i))
547 {
548 fprintf (stderr, "%s: error setting inheritable set!, %s\n", __func__,
549 safe_strerror (errno) );
550 exit (1);
551 }
552
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000553 /* we need a minimal basic set for 'effective', potentially for inheritable too */
554 minimal = zprivs_caps_minimal();
555
556 /* now set the effective set with a subset of basic privileges */
557 if (setppriv (PRIV_SET, PRIV_EFFECTIVE, minimal))
paulceacedb2005-09-29 14:39:32 +0000558 {
559 fprintf (stderr, "%s: error setting effective set!, %s\n", __func__,
560 safe_strerror (errno) );
561 exit (1);
562 }
563
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000564 /* we'll use the minimal set as our working-storage privset */
565 zprivs_state.caps = minimal;
paulceacedb2005-09-29 14:39:32 +0000566
567 /* set methods for the caller to use */
568 zprivs->change = zprivs_change_caps;
569 zprivs->current_state = zprivs_state_caps;
570}
571
572static void
573zprivs_caps_terminate (void)
574{
575 assert (zprivs_state.caps);
576
Brian Bennett5cd0e5c2015-02-17 23:24:15 +0000577 /* clear all capabilities by using working-storage privset */
paulceacedb2005-09-29 14:39:32 +0000578 setppriv (PRIV_SET, PRIV_EFFECTIVE, zprivs_state.caps);
579 setppriv (PRIV_SET, PRIV_PERMITTED, zprivs_state.caps);
580 setppriv (PRIV_SET, PRIV_INHERITABLE, zprivs_state.caps);
581
582 /* free up private state */
583 if (zprivs_state.syscaps_p)
584 priv_freeset (zprivs_state.syscaps_p);
585 if (zprivs_state.syscaps_i)
586 priv_freeset (zprivs_state.syscaps_i);
587
588 priv_freeset (zprivs_state.caps);
589}
590#else /* !HAVE_LCAPS && ! HAVE_SOLARIS_CAPABILITIES */
591#error "Neither Solaris nor Linux capabilities, dazed and confused..."
592#endif /* HAVE_LCAPS */
593#endif /* HAVE_CAPABILITIES */
David Lamparter6b0655a2014-06-04 06:53:35 +0200594
paul01245822003-05-20 01:22:17 +0000595int
596zprivs_change_uid (zebra_privs_ops_t op)
597{
paul28efaa32003-05-20 03:49:43 +0000598
paul01245822003-05-20 01:22:17 +0000599 if (op == ZPRIVS_RAISE)
600 return seteuid (zprivs_state.zsuid);
601 else if (op == ZPRIVS_LOWER)
602 return seteuid (zprivs_state.zuid);
603 else
604 return -1;
605}
606
607zebra_privs_current_t
608zprivs_state_uid (void)
609{
610 return ( (zprivs_state.zuid == geteuid()) ? ZPRIVS_LOWERED : ZPRIVS_RAISED);
611}
612
613int
614zprivs_change_null (zebra_privs_ops_t op)
615{
616 return 0;
617}
618
619zebra_privs_current_t
620zprivs_state_null (void)
621{
paulceacedb2005-09-29 14:39:32 +0000622 return zprivs_null_state;
paul01245822003-05-20 01:22:17 +0000623}
624
David Lamparter12bbd622015-09-15 02:26:44 -0700625#ifndef HAVE_GETGROUPLIST
626/* Solaris 11 has no getgrouplist() */
627static int
628getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups)
629{
630 struct group *grp;
631 size_t usridx;
632 int pos = 0, ret;
633
634 if (pos < *ngroups)
635 groups[pos] = group;
636 pos++;
637
638 setgrent();
639 while ((grp = getgrent()))
640 {
641 if (grp->gr_gid == group)
642 continue;
643 for (usridx = 0; grp->gr_mem[usridx] != NULL; usridx++)
644 if (!strcmp (grp->gr_mem[usridx], user))
645 {
646 if (pos < *ngroups)
647 groups[pos] = grp->gr_gid;
648 pos++;
649 break;
650 }
651 }
652 endgrent();
653
654 ret = (pos <= *ngroups) ? pos : -1;
655 *ngroups = pos;
656 return ret;
657}
658#endif /* HAVE_GETGROUPLIST */
659
paul01245822003-05-20 01:22:17 +0000660void
661zprivs_init(struct zebra_privs_t *zprivs)
662{
663 struct passwd *pwentry = NULL;
664 struct group *grentry = NULL;
Timo Teräs80c93542015-05-22 13:40:56 +0300665 gid_t groups[NGROUPS_MAX];
666 int i, ngroups = 0;
Donald Sharpe6ec2d62016-04-08 19:30:51 -0400667 int found = 0;
paul01245822003-05-20 01:22:17 +0000668
hassoba3a0bc2003-06-04 17:41:54 +0000669 if (!zprivs)
670 {
paul58a9d812003-06-11 05:12:40 +0000671 fprintf (stderr, "zprivs_init: called with NULL arg!\n");
hassoba3a0bc2003-06-04 17:41:54 +0000672 exit (1);
673 }
674
paul01245822003-05-20 01:22:17 +0000675 /* NULL privs */
676 if (! (zprivs->user || zprivs->group
677 || zprivs->cap_num_p || zprivs->cap_num_i) )
678 {
679 zprivs->change = zprivs_change_null;
680 zprivs->current_state = zprivs_state_null;
681 return;
682 }
683
684 if (zprivs->user)
685 {
Timo Teräs80c93542015-05-22 13:40:56 +0300686 if ( (pwentry = getpwnam (zprivs->user)) == NULL )
hassoba3a0bc2003-06-04 17:41:54 +0000687 {
paul58a9d812003-06-11 05:12:40 +0000688 /* cant use log.h here as it depends on vty */
689 fprintf (stderr, "privs_init: could not lookup user %s\n",
690 zprivs->user);
hassoba3a0bc2003-06-04 17:41:54 +0000691 exit (1);
692 }
Timo Teräs80c93542015-05-22 13:40:56 +0300693
694 zprivs_state.zuid = pwentry->pw_uid;
695 zprivs_state.zgid = pwentry->pw_gid;
hassoba3a0bc2003-06-04 17:41:54 +0000696 }
697
698 grentry = NULL;
699
Timo Teräs80c93542015-05-22 13:40:56 +0300700 if (zprivs->group)
701 {
702 if ( (grentry = getgrnam (zprivs->group)) == NULL )
703 {
704 fprintf (stderr, "privs_init: could not lookup group %s\n",
705 zprivs->group);
706 exit (1);
707 }
708
709 zprivs_state.zgid = grentry->gr_gid;
710 }
711
712 if (zprivs->user)
713 {
714 ngroups = sizeof(groups);
715 if ( (ngroups = getgrouplist (zprivs->user, zprivs_state.zgid, groups, &ngroups )) < 0 )
716 {
717 /* cant use log.h here as it depends on vty */
718 fprintf (stderr, "privs_init: could not getgrouplist for user %s\n",
719 zprivs->user);
720 exit (1);
721 }
722 }
723
hassoba3a0bc2003-06-04 17:41:54 +0000724 if (zprivs->vty_group)
725 /* Add the vty_group to the supplementary groups so it can be chowned to */
726 {
727 if ( (grentry = getgrnam (zprivs->vty_group)) )
728 {
729 zprivs_state.vtygrp = grentry->gr_gid;
Timo Teräs80c93542015-05-22 13:40:56 +0300730
731 for ( i = 0; i < ngroups; i++ )
732 if ( groups[i] == zprivs_state.vtygrp )
Donald Sharpe6ec2d62016-04-08 19:30:51 -0400733 {
734 found++;
735 break;
736 }
Timo Teräs80c93542015-05-22 13:40:56 +0300737
Donald Sharpe6ec2d62016-04-08 19:30:51 -0400738 if (!found)
739 {
740 fprintf (stderr, "privs_init: user(%s) is not part of vty group specified(%s)\n",
741 zprivs->user, zprivs->vty_group);
742 exit (1);
743 }
Timo Teräs80c93542015-05-22 13:40:56 +0300744 if ( i >= ngroups && ngroups < (int) ZEBRA_NUM_OF(groups) )
hassoba3a0bc2003-06-04 17:41:54 +0000745 {
Timo Teräs80c93542015-05-22 13:40:56 +0300746 groups[i] = zprivs_state.vtygrp;
747 }
hassoba3a0bc2003-06-04 17:41:54 +0000748 }
paul01245822003-05-20 01:22:17 +0000749 else
750 {
paul58a9d812003-06-11 05:12:40 +0000751 fprintf (stderr, "privs_init: could not lookup vty group %s\n",
752 zprivs->vty_group);
paul01245822003-05-20 01:22:17 +0000753 exit (1);
754 }
755 }
Timo Teräs80c93542015-05-22 13:40:56 +0300756
757 if (ngroups)
paul01245822003-05-20 01:22:17 +0000758 {
Timo Teräs80c93542015-05-22 13:40:56 +0300759 if ( setgroups (ngroups, groups) )
hassoba3a0bc2003-06-04 17:41:54 +0000760 {
Timo Teräs80c93542015-05-22 13:40:56 +0300761 fprintf (stderr, "privs_init: could not setgroups, %s\n",
762 safe_strerror (errno) );
paul01245822003-05-20 01:22:17 +0000763 exit (1);
764 }
Timo Teräs80c93542015-05-22 13:40:56 +0300765 }
766
767 if (zprivs_state.zgid)
768 {
paul01245822003-05-20 01:22:17 +0000769 /* change group now, forever. uid we do later */
770 if ( setregid (zprivs_state.zgid, zprivs_state.zgid) )
771 {
paul58a9d812003-06-11 05:12:40 +0000772 fprintf (stderr, "zprivs_init: could not setregid, %s\n",
ajs6099b3b2004-11-20 02:06:59 +0000773 safe_strerror (errno) );
paul01245822003-05-20 01:22:17 +0000774 exit (1);
775 }
776 }
777
paulceacedb2005-09-29 14:39:32 +0000778#ifdef HAVE_CAPABILITIES
779 zprivs_caps_init (zprivs);
780#else /* !HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000781 /* we dont have caps. we'll need to maintain rid and saved uid
782 * and change euid back to saved uid (who we presume has all neccessary
783 * privileges) whenever we are asked to raise our privileges.
paulceacedb2005-09-29 14:39:32 +0000784 *
785 * This is not worth that much security wise, but all we can do.
paul01245822003-05-20 01:22:17 +0000786 */
787 zprivs_state.zsuid = geteuid();
788 if ( zprivs_state.zuid )
789 {
790 if ( setreuid (-1, zprivs_state.zuid) )
791 {
paul58a9d812003-06-11 05:12:40 +0000792 fprintf (stderr, "privs_init (uid): could not setreuid, %s\n",
ajs6099b3b2004-11-20 02:06:59 +0000793 safe_strerror (errno));
paul01245822003-05-20 01:22:17 +0000794 exit (1);
795 }
796 }
797
798 zprivs->change = zprivs_change_uid;
799 zprivs->current_state = zprivs_state_uid;
paulceacedb2005-09-29 14:39:32 +0000800#endif /* HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000801}
802
803void
paulceacedb2005-09-29 14:39:32 +0000804zprivs_terminate (struct zebra_privs_t *zprivs)
paul01245822003-05-20 01:22:17 +0000805{
paulceacedb2005-09-29 14:39:32 +0000806 if (!zprivs)
paul01245822003-05-20 01:22:17 +0000807 {
paulceacedb2005-09-29 14:39:32 +0000808 fprintf (stderr, "%s: no privs struct given, terminating", __func__);
809 exit (0);
810 }
paul01245822003-05-20 01:22:17 +0000811
paulceacedb2005-09-29 14:39:32 +0000812#ifdef HAVE_CAPABILITIES
813 zprivs_caps_terminate();
814#else /* !HAVE_CAPABILITIES */
paul01245822003-05-20 01:22:17 +0000815 if (zprivs_state.zuid)
816 {
817 if ( setreuid (zprivs_state.zuid, zprivs_state.zuid) )
818 {
paulceacedb2005-09-29 14:39:32 +0000819 fprintf (stderr, "privs_terminate: could not setreuid, %s",
ajs6099b3b2004-11-20 02:06:59 +0000820 safe_strerror (errno) );
paul01245822003-05-20 01:22:17 +0000821 exit (1);
822 }
823 }
824#endif /* HAVE_LCAPS */
paulceacedb2005-09-29 14:39:32 +0000825
826 zprivs->change = zprivs_change_null;
827 zprivs->current_state = zprivs_state_null;
828 zprivs_null_state = ZPRIVS_LOWERED;
paul01245822003-05-20 01:22:17 +0000829 return;
830}
hassoba3a0bc2003-06-04 17:41:54 +0000831
832void
833zprivs_get_ids(struct zprivs_ids_t *ids)
834{
835
836 ids->uid_priv = getuid();
837 (zprivs_state.zuid) ? (ids->uid_normal = zprivs_state.zuid)
838 : (ids->uid_normal = -1);
839 (zprivs_state.zgid) ? (ids->gid_normal = zprivs_state.zgid)
840 : (ids->gid_normal = -1);
841 (zprivs_state.vtygrp) ? (ids->gid_vty = zprivs_state.vtygrp)
842 : (ids->gid_vty = -1);
843
844 return;
845}