From 035507c84b53bceb143d0923e65916cbf90979c7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 24 Dec 2010 01:07:48 +0100 Subject: [PATCH] lib: [env] consolidate safe_getenv() usage Signed-off-by: Karel Zak --- include/env.h | 6 ++++++ lib/env.c | 36 +++++++++++++++++++++++++++++++++- shlibs/blkid/src/Makefile.am | 3 ++- shlibs/blkid/src/cache.c | 32 ++---------------------------- shlibs/blkid/src/config.c | 4 ++-- shlibs/mount/src/Makefile.am | 3 ++- shlibs/mount/src/init.c | 2 +- shlibs/mount/src/utils.c | 38 ++++-------------------------------- 8 files changed, 54 insertions(+), 70 deletions(-) diff --git a/include/env.h b/include/env.h index d69b4f29..bcd0f7ea 100644 --- a/include/env.h +++ b/include/env.h @@ -1,2 +1,8 @@ +#ifndef UTIL_LINUX_ENV_H +#define UTIL_LINUX_ENV_H + extern void sanitize_env (void); +extern char *safe_getenv(const char *arg); + +#endif /* UTIL_LINUX_ENV_H */ diff --git a/lib/env.c b/lib/env.c index 82fadf52..770cadff 100644 --- a/lib/env.c +++ b/lib/env.c @@ -3,11 +3,22 @@ * Added from shadow-utils package * by Arkadiusz Mi¶kiewicz * - */ + */ #include #include #include +#ifdef HAVE_SYS_PRCTL_H +#include +#else +#define PR_GET_DUMPABLE 3 +#endif +#if (!defined(HAVE_PRCTL) && defined(linux)) +#include +#endif +#include +#include + #include "env.h" extern char **environ; @@ -71,3 +82,26 @@ sanitize_env(void) } } + +char *safe_getenv(const char *arg) +{ + uid_t ruid = getuid(); + + if (ruid != 0 || (ruid != geteuid()) || (getgid() != getegid())) + return NULL; +#if HAVE_PRCTL + if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) + return NULL; +#else +#if (defined(linux) && defined(SYS_prctl)) + if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) + return NULL; +#endif +#endif + +#ifdef HAVE___SECURE_GETENV + return __secure_getenv(arg); +#else + return getenv(arg); +#endif +} diff --git a/shlibs/blkid/src/Makefile.am b/shlibs/blkid/src/Makefile.am index 47d2a370..b0701cb7 100644 --- a/shlibs/blkid/src/Makefile.am +++ b/shlibs/blkid/src/Makefile.am @@ -32,7 +32,8 @@ libblkid_la_SOURCES = cache.c dev.c devname.c devno.c getsize.c llseek.c \ $(top_srcdir)/lib/canonicalize.c \ $(top_srcdir)/lib/md5.c \ $(top_srcdir)/lib/crc32.c \ - $(top_srcdir)/include/list.h + $(top_srcdir)/include/list.h \ + $(top_srcdir)/lib/env.c nodist_libblkid_la_SOURCES = blkid.h diff --git a/shlibs/blkid/src/cache.c b/shlibs/blkid/src/cache.c index e71176b5..6f53dc99 100644 --- a/shlibs/blkid/src/cache.c +++ b/shlibs/blkid/src/cache.c @@ -18,18 +18,11 @@ #endif #include #include -#ifdef HAVE_SYS_PRCTL_H -#include -#else -#define PR_GET_DUMPABLE 3 -#endif -#if (!defined(HAVE_PRCTL) && defined(linux)) -#include -#endif #ifdef HAVE_SYS_STAT_H #include #endif #include "blkidP.h" +#include "env.h" int blkid_debug_mask = 0; @@ -58,27 +51,6 @@ int blkid_debug_mask = 0; * the cache file is required in this situation. */ -char *blkid_safe_getenv(const char *arg) -{ - if ((getuid() != geteuid()) || (getgid() != getegid())) - return NULL; -#if HAVE_PRCTL - if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) - return NULL; -#else -#if (defined(linux) && defined(SYS_prctl)) - if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) - return NULL; -#endif -#endif - -#ifdef HAVE___SECURE_GETENV - return __secure_getenv(arg); -#else - return getenv(arg); -#endif -} - #if 0 /* ifdef CONFIG_BLKID_DEBUG */ static blkid_debug_dump_cache(int mask, blkid_cache cache) { @@ -126,7 +98,7 @@ char *blkid_get_cache_filename(struct blkid_config *conf) { char *filename; - filename = blkid_safe_getenv("BLKID_FILE"); + filename = safe_getenv("BLKID_FILE"); if (filename) filename = blkid_strdup(filename); else if (conf) diff --git a/shlibs/blkid/src/config.c b/shlibs/blkid/src/config.c index fcc7b6f1..e4d25ba4 100644 --- a/shlibs/blkid/src/config.c +++ b/shlibs/blkid/src/config.c @@ -28,7 +28,7 @@ #include "blkdev.h" #include "blkidP.h" - +#include "env.h" static int parse_evaluate(struct blkid_config *conf, char *s) { @@ -120,7 +120,7 @@ struct blkid_config *blkid_read_config(const char *filename) FILE *f; if (!filename) - filename = blkid_safe_getenv("BLKID_CONF"); + filename = safe_getenv("BLKID_CONF"); if (!filename) filename = BLKID_CONFIG_FILE; diff --git a/shlibs/mount/src/Makefile.am b/shlibs/mount/src/Makefile.am index 6c241ba2..f552ebee 100644 --- a/shlibs/mount/src/Makefile.am +++ b/shlibs/mount/src/Makefile.am @@ -18,7 +18,8 @@ libmount_la_SOURCES = mountP.h version.c utils.c test.c init.c cache.c \ $(top_srcdir)/include/list.h \ $(top_srcdir)/lib/mangle.c \ $(top_srcdir)/lib/canonicalize.c \ - $(top_srcdir)/lib/strutils.c + $(top_srcdir)/lib/strutils.c \ + $(top_srcdir)/lib/env.c nodist_libmount_la_SOURCES = mountP.h diff --git a/shlibs/mount/src/init.c b/shlibs/mount/src/init.c index 2dfab58f..d80a2d8c 100644 --- a/shlibs/mount/src/init.c +++ b/shlibs/mount/src/init.c @@ -33,7 +33,7 @@ void mnt_init_debug(int mask) if (libmount_debug_mask & MNT_DEBUG_INIT) return; if (!mask) { - char *str = mnt_getenv_safe("LIBMOUNT_DEBUG"); + char *str = getenv("LIBMOUNT_DEBUG"); if (str) libmount_debug_mask = strtoul(str, 0, 0); } else diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index ff3c90cd..c97144dc 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -14,14 +14,6 @@ #include #include #include -#ifdef HAVE_SYS_PRCTL_H -#include -#else -#define PR_GET_DUMPABLE 3 -#endif -#if (!defined(HAVE_PRCTL) && defined(linux)) -#include -#endif #include #include #include @@ -34,29 +26,7 @@ #include "mountP.h" #include "mangle.h" #include "canonicalize.h" - -char *mnt_getenv_safe(const char *arg) -{ - return getenv(arg); - - if ((getuid() != geteuid()) || (getgid() != getegid())) - return NULL; -#if HAVE_PRCTL - if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) - return NULL; -#else -#if (defined(linux) && defined(SYS_prctl)) - if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) - return NULL; -#endif -#endif - -#ifdef HAVE___SECURE_GETENV - return __secure_getenv(arg); -#else - return getenv(arg); -#endif -} +#include "env.h" int endswith(const char *s, const char *sx) { @@ -635,7 +605,7 @@ done: */ const char *mnt_get_fstab_path(void) { - const char *p = mnt_getenv_safe("LIBMOUNT_FSTAB"); + const char *p = safe_getenv("LIBMOUNT_FSTAB"); return p ? : _PATH_MNTTAB; } @@ -649,7 +619,7 @@ const char *mnt_get_fstab_path(void) */ const char *mnt_get_mtab_path(void) { - const char *p = mnt_getenv_safe("LIBMOUNT_MTAB"); + const char *p = safe_getenv("LIBMOUNT_MTAB"); return p ? : _PATH_MOUNTED; } @@ -660,7 +630,7 @@ const char *mnt_get_mtab_path(void) */ const char *mnt_get_utab_path(void) { - const char *p = mnt_getenv_safe("LIBMOUNT_UTAB"); + const char *p = safe_getenv("LIBMOUNT_UTAB"); return p ? : MNT_PATH_UTAB; } -- 2.39.5