Skip to content

Commit 3aadcf0

Browse files
committed
use secure_getenv instead of getenv
replace the getenv with secure_getenv to avoid some potential risk feature test micro for this function is _GNU_SOURCE Signed-off-by: Carl Zhang <[email protected]>
1 parent b4b643b commit 3aadcf0

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

configure.ac

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ PKG_PROG_PKG_CONFIG
183183
AC_HEADER_STDC
184184
AC_SYS_LARGEFILE
185185

186+
#check for secure_getenv
187+
AC_CHECK_FUNCS(secure_getenv)
188+
186189
# Check for Doxygen
187190
if test "$enable_docs" = "yes"; then
188191
AC_CHECK_TOOL([DOXYGEN], [doxygen], [no])

meson.build

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ elif get_option('with_legacy').contains('fglrx')
134134
c_args += ['-DHAVE_FGLRX']
135135
endif
136136

137+
if cc.has_function('secure_getenv')
138+
c_args += ['-DHAVE_SECURE_GETENV']
139+
endif
140+
137141
add_project_arguments(c_args, language: ['c'])
138142

139143
subdir('va')

va/compat_win32.h

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ typedef unsigned int __uid_t;
5353

5454
#if _MSC_VER
5555
#define getenv _getenv
56+
#define secure_getenv _getenv
57+
#define HAVE_SECURE_GETENV
5658
inline char* _getenv(const char *varname)
5759
{
5860
static char _getenv_buf[32767];

va/va.c

+18-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@
5757
#define CHECK_MAXIMUM(s, ctx, var) if (!va_checkMaximum(dpy, ctx->max_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
5858
#define CHECK_STRING(s, ctx, var) if (!va_checkString(dpy, ctx->str_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
5959

60+
#ifndef HAVE_SECURE_GETENV
61+
static char * secure_getenv(const char *name)
62+
{
63+
#if defined(__MINGW32__) || defined(__MINGW64__)
64+
if (getuid() == geteuid())
65+
#else
66+
if (getuid() == geteuid() && getgid() == getegid())
67+
#endif
68+
return getenv(name);
69+
else
70+
return NULL;
71+
}
72+
#endif
73+
6074
/*
6175
* read a config "env" for libva.conf or from environment setting
6276
* libva.conf has higher priority
@@ -97,7 +111,7 @@ int va_parseConfig(char *env, char *env_value)
97111
fclose(fp);
98112

99113
/* no setting in config file, use env setting */
100-
value = getenv(env);
114+
value = secure_getenv(env);
101115
if (value) {
102116
if (env_value) {
103117
strncpy(env_value, value, 1024);
@@ -348,7 +362,7 @@ static VAStatus va_getDriverNumCandidates(VADisplay dpy, int *num_candidates)
348362
VADriverContextP ctx;
349363

350364
ctx = CTX(dpy);
351-
driver_name_env = getenv("LIBVA_DRIVER_NAME");
365+
driver_name_env = secure_getenv("LIBVA_DRIVER_NAME");
352366

353367
if (pDisplayContext->vaGetNumCandidates)
354368
vaStatus = pDisplayContext->vaGetNumCandidates(pDisplayContext, num_candidates);
@@ -374,7 +388,7 @@ static VAStatus va_getDriverNameByIndex(VADisplay dpy, char **driver_name, int c
374388
else
375389
status = VA_STATUS_ERROR_INVALID_PARAMETER;
376390
}
377-
driver_name_env = getenv("LIBVA_DRIVER_NAME");
391+
driver_name_env = secure_getenv("LIBVA_DRIVER_NAME");
378392
/*if user set driver name by vaSetDriverName */
379393
if (ctx->override_driver_name) {
380394
if (*driver_name)
@@ -424,7 +438,7 @@ static VAStatus va_openDriver(VADisplay dpy, char *driver_name)
424438

425439
if (geteuid() == getuid())
426440
/* don't allow setuid apps to use LIBVA_DRIVERS_PATH */
427-
search_path = getenv("LIBVA_DRIVERS_PATH");
441+
search_path = secure_getenv("LIBVA_DRIVERS_PATH");
428442
if (!search_path)
429443
search_path = VA_DRIVERS_PATH;
430444

0 commit comments

Comments
 (0)