1 /*
2 * Copyright (c) 2016-2019 The Khronos Group Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * OpenCL is a trademark of Apple Inc. used under license by Khronos.
17 */
18
19 // for secure_getenv():
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE
22 #endif
23
24 #include "icd_cmake_config.h"
25
26 #include <stdlib.h>
27 #include <unistd.h>
28
khrIcd_getenv(const char * name)29 char *khrIcd_getenv(const char *name) {
30 // No allocation of memory necessary for Linux.
31 return getenv(name);
32 }
33
khrIcd_secure_getenv(const char * name)34 char *khrIcd_secure_getenv(const char *name) {
35 #if defined(__APPLE__)
36 // Apple does not appear to have a secure getenv implementation.
37 // The main difference between secure getenv and getenv is that secure getenv
38 // returns NULL if the process is being run with elevated privileges by a normal user.
39 // The idea is to prevent the reading of malicious environment variables by a process
40 // that can do damage.
41 // This algorithm is derived from glibc code that sets an internal
42 // variable (__libc_enable_secure) if the process is running under setuid or setgid.
43 return geteuid() != getuid() || getegid() != getgid() ? NULL : khrIcd_getenv(name);
44 #else
45 // Linux
46 #ifdef HAVE_SECURE_GETENV
47 return secure_getenv(name);
48 #elif defined(HAVE___SECURE_GETENV)
49 return __secure_getenv(name);
50 #else
51 #pragma message( \
52 "Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
53 " updating to a different libc.")
54 return khrIcd_getenv(name);
55 #endif
56 #endif
57 }
58
khrIcd_free_getenv(char * val)59 void khrIcd_free_getenv(char *val) {
60 // No freeing of memory necessary for Linux, but we should at least touch
61 // val to get rid of compiler warnings.
62 (void)val;
63 }
64