if (!privs)
return NULL;
+ /* If we're already elevated, just return */
+ pthread_mutex_lock(&(privs->mutex));
+ if (++privs->refcount > 1) {
+ pthread_mutex_unlock(&(privs->mutex));
+ return privs;
+ }
+ pthread_mutex_unlock(&(privs->mutex));
+
errno = 0;
if (privs->change(ZPRIVS_RAISE)) {
zlog_err("%s: Failed to raise privileges (%s)",
if (!*privs)
return;
+ /* Don't lower privs if there's another caller */
+ pthread_mutex_lock(&(*privs)->mutex);
+ if (--((*privs)->refcount) > 0) {
+ pthread_mutex_unlock(&(*privs)->mutex);
+ return;
+ }
+ pthread_mutex_unlock(&(*privs)->mutex);
+
errno = 0;
if ((*privs)->change(ZPRIVS_LOWER)) {
zlog_err("%s: Failed to lower privileges (%s)",
exit(1);
}
+ pthread_mutex_init(&(zprivs->mutex), NULL);
+ zprivs->refcount = 0;
+
if (zprivs->vty_group) {
/* in a "NULL" setup, this is allowed to fail too, but still
* try. */
#ifndef _ZEBRA_PRIVS_H
#define _ZEBRA_PRIVS_H
+#include <pthread.h>
+
#ifdef __cplusplus
extern "C" {
#endif
zebra_capabilities_t *caps_i; /* caps to allow inheritance of */
int cap_num_p; /* number of caps in arrays */
int cap_num_i;
+
+ /* Mutex and counter used to avoid race conditions in multi-threaded
+ * processes. The privs elevation is process-wide, so we need to
+ * avoid changing the privilege status across threads.
+ */
+ pthread_mutex_t mutex;
+ uint32_t refcount;
+
const char *user; /* user and group to run as */
const char *group;
const char *vty_group; /* group to chown vty socket to */