From 396cfdfee8b3c5f5207b488d159b8677605a453f Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 1 May 2012 01:50:03 +0200 Subject: [PATCH] When running on valgrind, don't chroot or drop privileges Valgrind documentation says: You are encouraged to copy the valgrind/*.h headers into your project's include directory, so your program doesn't have a compile-time dependency on Valgrind being installed. The Valgrind headers, unlike most of the rest of the code, are under a BSD-style license so you may include them without worrying about license incompatibility. I don't like copying headers around. Therefore, there is an autoconf check for valgrind headers. --- configure.ac | 1 + src/lldpd.h | 6 ++++++ src/priv.c | 26 +++++++++++++++----------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/configure.ac b/configure.ac index 32ca90b4..3c85065b 100644 --- a/configure.ac +++ b/configure.ac @@ -102,6 +102,7 @@ AC_CHECK_HEADERS([ \ @%:@include @%:@include ]) +AC_CHECK_HEADERS([valgrind/valgrind.h]) AC_CACHE_SAVE diff --git a/src/lldpd.h b/src/lldpd.h index 97c8531b..0ecf4e93 100644 --- a/src/lldpd.h +++ b/src/lldpd.h @@ -21,6 +21,12 @@ # include #endif +#ifdef HAVE_VALGRIND_VALGRIND_H +# include +#else +# define RUNNING_ON_VALGRIND 0 +#endif + #define _GNU_SOURCE 1 #include #include diff --git a/src/priv.c b/src/priv.c index 217cac9e..9e57532f 100644 --- a/src/priv.c +++ b/src/priv.c @@ -531,17 +531,21 @@ priv_init(char *chrootdir) switch (monitored) { case 0: /* We are in the children, drop privileges */ - if (chroot(chrootdir) == -1) - fatal("[priv]: unable to chroot"); - if (chdir("/") != 0) - fatal("[priv]: unable to chdir"); - gidset[0] = gid; - if (setresgid(gid, gid, gid) == -1) - fatal("[priv]: setresgid() failed"); - if (setgroups(1, gidset) == -1) - fatal("[priv]: setgroups() failed"); - if (setresuid(uid, uid, uid) == -1) - fatal("[priv]: setresuid() failed"); + if (RUNNING_ON_VALGRIND) + LLOG_WARNX("[priv]: running on valgrind, keep privileges"); + else { + if (chroot(chrootdir) == -1) + fatal("[priv]: unable to chroot"); + if (chdir("/") != 0) + fatal("[priv]: unable to chdir"); + gidset[0] = gid; + if (setresgid(gid, gid, gid) == -1) + fatal("[priv]: setresgid() failed"); + if (setgroups(1, gidset) == -1) + fatal("[priv]: setgroups() failed"); + if (setresuid(uid, uid, uid) == -1) + fatal("[priv]: setresuid() failed"); + } remote = pair[0]; close(pair[1]); priv_ping(); -- 2.39.5