Skip to content

Commit

Permalink
lib: [env] consolidate safe_getenv() usage
Browse files Browse the repository at this point in the history
Signed-off-by: Karel Zak <kzak@redhat.com>
  • Loading branch information
karelzak committed Jan 3, 2011
1 parent 354815f commit 035507c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 70 deletions.
6 changes: 6 additions & 0 deletions include/env.h
Original file line number Diff line number Diff line change
@@ -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 */

36 changes: 35 additions & 1 deletion lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
* Added from shadow-utils package
* by Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
*
*/
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#else
#define PR_GET_DUMPABLE 3
#endif
#if (!defined(HAVE_PRCTL) && defined(linux))
#include <sys/syscall.h>
#endif
#include <unistd.h>
#include <sys/types.h>

#include "env.h"

extern char **environ;
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion shlibs/blkid/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 2 additions & 30 deletions shlibs/blkid/src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,11 @@
#endif
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#else
#define PR_GET_DUMPABLE 3
#endif
#if (!defined(HAVE_PRCTL) && defined(linux))
#include <sys/syscall.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include "blkidP.h"
#include "env.h"

int blkid_debug_mask = 0;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions shlibs/blkid/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include "blkdev.h"
#include "blkidP.h"

#include "env.h"

static int parse_evaluate(struct blkid_config *conf, char *s)
{
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion shlibs/mount/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion shlibs/mount/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 4 additions & 34 deletions shlibs/mount/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#else
#define PR_GET_DUMPABLE 3
#endif
#if (!defined(HAVE_PRCTL) && defined(linux))
#include <sys/syscall.h>
#endif
#include <sys/stat.h>
#include <ctype.h>
#include <sys/types.h>
Expand All @@ -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)
{
Expand Down Expand Up @@ -635,7 +605,7 @@ int mnt_has_regular_utab(const char **utab, int *writable)
*/
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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 035507c

Please sign in to comment.