From 061fa6cc1bc8603c12fc810f839b123e1bed1544 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 5 Mar 2013 10:24:34 +0100 Subject: [PATCH] misc: Correctly handle getenv(). The returned string of getenv() has an unknown size. You need to store the result always in a char array with a certain size to make sure we don't feed tainted data to the next function call. --- src/csync_misc.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/csync_misc.c b/src/csync_misc.c index 2b9a7edb03..2ac60d09c6 100644 --- a/src/csync_misc.c +++ b/src/csync_misc.c @@ -87,24 +87,28 @@ char *csync_get_local_username(void) { #endif /* NSS_BUFLEN_PASSWD */ char *csync_get_user_home_dir(void) { - char *szPath = NULL; + char home[PATH_MAX] = {0}; + const char *envp; struct passwd pwd; struct passwd *pwdbuf; char buf[NSS_BUFLEN_PASSWD]; int rc; - szPath = getenv("HOME"); - if( szPath ) { - return c_strdup(szPath); + envp = getenv("HOME"); + if (envp != NULL) { + snprintf(home, sizeof(home), "%s", envp); + if (home[0] != '\0') { + return c_strdup(home); + } } /* Still nothing found, read the password file */ rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); if (rc != 0) { - szPath = c_strdup(pwd.pw_dir); + return c_strdup(pwd.pw_dir); } - return szPath; + return NULL; } char *csync_get_local_username(void) {