/src/e2fsprogs/lib/ext2fs/getenv.c
Line | Count | Source |
1 | | /* |
2 | | * getenv.c --- implement a safe getenv for use by the ext2fs library |
3 | | * |
4 | | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
5 | | * 2002 by Theodore Ts'o. |
6 | | * |
7 | | * %Begin-Header% |
8 | | * This file may be redistributed under the terms of the GNU Library |
9 | | * General Public License, version 2. |
10 | | * %End-Header% |
11 | | */ |
12 | | #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) |
13 | | #define _XOPEN_SOURCE 600 |
14 | | #define _DARWIN_C_SOURCE |
15 | | #ifndef _GNU_SOURCE |
16 | | #define _GNU_SOURCE |
17 | | #endif |
18 | | #endif |
19 | | |
20 | | #include "config.h" |
21 | | #include <stdlib.h> |
22 | | #if HAVE_UNISTD_H |
23 | | #include <unistd.h> |
24 | | #endif |
25 | | #ifdef HAVE_SYS_PRCTL_H |
26 | | #include <sys/prctl.h> |
27 | | #else |
28 | | #define PR_GET_DUMPABLE 3 |
29 | | #endif |
30 | | #if (!defined(HAVE_PRCTL) && defined(linux)) |
31 | | #include <sys/syscall.h> |
32 | | #endif |
33 | | |
34 | | #include "ext2fs.h" |
35 | | |
36 | | char *ext2fs_safe_getenv(const char *arg) |
37 | 1.28k | { |
38 | 1.28k | if ((getuid() != geteuid()) || (getgid() != getegid())) |
39 | 0 | return NULL; |
40 | 1.28k | #ifdef HAVE_PRCTL |
41 | 1.28k | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
42 | 0 | return NULL; |
43 | | #else |
44 | | #if (defined(linux) && defined(SYS_prctl)) |
45 | | if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
46 | | return NULL; |
47 | | #endif |
48 | | #endif |
49 | | |
50 | 1.28k | #if defined(HAVE_SECURE_GETENV) |
51 | 1.28k | return secure_getenv(arg); |
52 | | #elif defined(HAVE___SECURE_GETENV) |
53 | | return __secure_getenv(arg); |
54 | | #else |
55 | | return getenv(arg); |
56 | | #endif |
57 | 1.28k | } |