Whamcloud - gitweb
libext2fs: add new getenv.c file
authorTheodore Ts'o <tytso@mit.edu>
Thu, 25 Apr 2024 16:41:48 +0000 (12:41 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 25 Apr 2024 16:41:48 +0000 (12:41 -0400)
Fixes: eefbea0da810 ("libext2fs: use a safe_getenv() function everywhere")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/getenv.c [new file with mode: 0644]

diff --git a/lib/ext2fs/getenv.c b/lib/ext2fs/getenv.c
new file mode 100644 (file)
index 0000000..f279f7b
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * getenv.c --- implement a safe getenv for use by the ext2fs library
+ *
+ * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ *     2002 by Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
+ * %End-Header%
+ */
+#if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
+#define _XOPEN_SOURCE 600
+#define _DARWIN_C_SOURCE
+#define _FILE_OFFSET_BITS 64
+#ifndef _LARGEFILE_SOURCE
+#define _LARGEFILE_SOURCE
+#endif
+#ifndef _LARGEFILE64_SOURCE
+#define _LARGEFILE64_SOURCE
+#endif
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#endif
+
+#include "config.h"
+#include <stdlib.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#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 "ext2fs.h"
+
+char *ext2fs_safe_getenv(const char *arg)
+{
+       if ((getuid() != geteuid()) || (getgid() != getegid()))
+               return NULL;
+#ifdef 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
+
+#if defined(HAVE_SECURE_GETENV)
+       return secure_getenv(arg);
+#elif defined(HAVE___SECURE_GETENV)
+       return __secure_getenv(arg);
+#else
+       return getenv(arg);
+#endif
+}