Skip to content

Commit

Permalink
use secure_getenv instead of getenv
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
XinfengZhang committed Mar 28, 2023
1 parent b4b643b commit 3aadcf0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ PKG_PROG_PKG_CONFIG
AC_HEADER_STDC
AC_SYS_LARGEFILE

#check for secure_getenv
AC_CHECK_FUNCS(secure_getenv)

# Check for Doxygen
if test "$enable_docs" = "yes"; then
AC_CHECK_TOOL([DOXYGEN], [doxygen], [no])
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ elif get_option('with_legacy').contains('fglrx')
c_args += ['-DHAVE_FGLRX']
endif

if cc.has_function('secure_getenv')
c_args += ['-DHAVE_SECURE_GETENV']
endif

add_project_arguments(c_args, language: ['c'])

subdir('va')
Expand Down
2 changes: 2 additions & 0 deletions va/compat_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef unsigned int __uid_t;

#if _MSC_VER
#define getenv _getenv
#define secure_getenv _getenv
#define HAVE_SECURE_GETENV
inline char* _getenv(const char *varname)
{
static char _getenv_buf[32767];
Expand Down
22 changes: 18 additions & 4 deletions va/va.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@
#define CHECK_MAXIMUM(s, ctx, var) if (!va_checkMaximum(dpy, ctx->max_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
#define CHECK_STRING(s, ctx, var) if (!va_checkString(dpy, ctx->str_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;

#ifndef HAVE_SECURE_GETENV
static char * secure_getenv(const char *name)
{
#if defined(__MINGW32__) || defined(__MINGW64__)
if (getuid() == geteuid())
#else
if (getuid() == geteuid() && getgid() == getegid())
#endif
return getenv(name);
else
return NULL;
}
#endif

/*
* read a config "env" for libva.conf or from environment setting
* libva.conf has higher priority
Expand Down Expand Up @@ -97,7 +111,7 @@ int va_parseConfig(char *env, char *env_value)
fclose(fp);

/* no setting in config file, use env setting */
value = getenv(env);
value = secure_getenv(env);
if (value) {
if (env_value) {
strncpy(env_value, value, 1024);
Expand Down Expand Up @@ -348,7 +362,7 @@ static VAStatus va_getDriverNumCandidates(VADisplay dpy, int *num_candidates)
VADriverContextP ctx;

ctx = CTX(dpy);
driver_name_env = getenv("LIBVA_DRIVER_NAME");
driver_name_env = secure_getenv("LIBVA_DRIVER_NAME");

if (pDisplayContext->vaGetNumCandidates)
vaStatus = pDisplayContext->vaGetNumCandidates(pDisplayContext, num_candidates);
Expand All @@ -374,7 +388,7 @@ static VAStatus va_getDriverNameByIndex(VADisplay dpy, char **driver_name, int c
else
status = VA_STATUS_ERROR_INVALID_PARAMETER;
}
driver_name_env = getenv("LIBVA_DRIVER_NAME");
driver_name_env = secure_getenv("LIBVA_DRIVER_NAME");
/*if user set driver name by vaSetDriverName */
if (ctx->override_driver_name) {
if (*driver_name)
Expand Down Expand Up @@ -424,7 +438,7 @@ static VAStatus va_openDriver(VADisplay dpy, char *driver_name)

if (geteuid() == getuid())
/* don't allow setuid apps to use LIBVA_DRIVERS_PATH */
search_path = getenv("LIBVA_DRIVERS_PATH");
search_path = secure_getenv("LIBVA_DRIVERS_PATH");
if (!search_path)
search_path = VA_DRIVERS_PATH;

Expand Down

0 comments on commit 3aadcf0

Please sign in to comment.