fixed requesting DWORD with sc_ctx_win32_get_config_value

the length of the value is not determined by strlen()
This commit is contained in:
Frank Morgner 2018-07-28 16:52:29 +02:00
parent cd557df54d
commit 9294058d5c
4 changed files with 29 additions and 35 deletions

View File

@ -1363,11 +1363,13 @@ sc_card_sm_load(struct sc_card *card, const char *module_path, const char *in_mo
#ifdef _WIN32 #ifdef _WIN32
if (!module_path || strlen(module_path) == 0) { if (!module_path || strlen(module_path) == 0) {
temp_len = PATH_MAX; temp_len = PATH_MAX-1;
rv = sc_ctx_win32_get_config_value(NULL, "SmDir", "Software\\OpenSC Project\\OpenSC", rv = sc_ctx_win32_get_config_value(NULL, "SmDir", "Software\\OpenSC Project\\OpenSC",
temp_path, &temp_len); temp_path, &temp_len);
if (rv == SC_SUCCESS) if (rv == SC_SUCCESS) {
temp_path[temp_len] = '\0';
module_path = temp_path; module_path = temp_path;
}
} }
expanded_len = PATH_MAX; expanded_len = PATH_MAX;
expanded_len = ExpandEnvironmentStringsA(module_path, expanded_val, expanded_len); expanded_len = ExpandEnvironmentStringsA(module_path, expanded_val, expanded_len);

View File

@ -167,12 +167,9 @@ struct _sc_ctx_options {
int int
sc_ctx_win32_get_config_value(const char *name_env, sc_ctx_win32_get_config_value(const char *name_env,
const char *name_reg, const char *name_key, const char *name_reg, const char *name_key,
char *out, size_t *out_len) void *out, size_t *out_len)
{ {
#ifdef _WIN32 #ifdef _WIN32
char temp[PATH_MAX + 1];
char *value = NULL;
DWORD temp_len = PATH_MAX;
long rc; long rc;
HKEY hKey; HKEY hKey;
@ -180,9 +177,12 @@ sc_ctx_win32_get_config_value(const char *name_env,
return SC_ERROR_INVALID_ARGUMENTS; return SC_ERROR_INVALID_ARGUMENTS;
if (name_env) { if (name_env) {
value = getenv(name_env); char *value = value = getenv(name_env);
if (value) if (strlen(value) < *out_len)
goto done; return SC_ERROR_NOT_ENOUGH_MEMORY;
memcpy(out, value, strlen(value));
*out_len = strlen(value);
return SC_SUCCESS;
} }
if (!name_reg) if (!name_reg)
@ -193,36 +193,26 @@ sc_ctx_win32_get_config_value(const char *name_env,
rc = RegOpenKeyExA(HKEY_CURRENT_USER, name_key, 0, KEY_QUERY_VALUE, &hKey); rc = RegOpenKeyExA(HKEY_CURRENT_USER, name_key, 0, KEY_QUERY_VALUE, &hKey);
if (rc == ERROR_SUCCESS) { if (rc == ERROR_SUCCESS) {
temp_len = PATH_MAX; DWORD len = *out_len;
rc = RegQueryValueEx( hKey, name_reg, NULL, NULL, (LPBYTE) temp, &temp_len); rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len);
if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX))
value = temp;
RegCloseKey(hKey); RegCloseKey(hKey);
}
if (!value) {
rc = RegOpenKeyExA( HKEY_LOCAL_MACHINE, name_key, 0, KEY_QUERY_VALUE, &hKey );
if (rc == ERROR_SUCCESS) { if (rc == ERROR_SUCCESS) {
temp_len = PATH_MAX; *out_len = len;
rc = RegQueryValueEx( hKey, name_reg, NULL, NULL, (LPBYTE) temp, &temp_len); return SC_SUCCESS;
if ((rc == ERROR_SUCCESS) && (temp_len < PATH_MAX))
value = temp;
RegCloseKey(hKey);
} }
} }
done: rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, name_key, 0, KEY_QUERY_VALUE, &hKey);
if (value) { if (rc == ERROR_SUCCESS) {
if (strlen(value) >= *out_len) DWORD len = *out_len;
return SC_ERROR_BUFFER_TOO_SMALL; rc = RegQueryValueEx(hKey, name_reg, NULL, NULL, out, &len);
strcpy(out, value); RegCloseKey(hKey);
*out_len = strlen(out); if (rc == ERROR_SUCCESS) {
return SC_SUCCESS; *out_len = len;
return SC_SUCCESS;
}
} }
memset(out, 0, *out_len);
*out_len = 0;
return SC_ERROR_OBJECT_NOT_FOUND; return SC_ERROR_OBJECT_NOT_FOUND;
#else #else
return SC_ERROR_NOT_SUPPORTED; return SC_ERROR_NOT_SUPPORTED;
@ -677,13 +667,14 @@ static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts)
memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks)); memset(ctx->conf_blocks, 0, sizeof(ctx->conf_blocks));
#ifdef _WIN32 #ifdef _WIN32
temp_len = PATH_MAX; temp_len = PATH_MAX-1;
r = sc_ctx_win32_get_config_value("OPENSC_CONF", "ConfigFile", "Software\\OpenSC Project\\OpenSC", r = sc_ctx_win32_get_config_value("OPENSC_CONF", "ConfigFile", "Software\\OpenSC Project\\OpenSC",
temp_path, &temp_len); temp_path, &temp_len);
if (r) { if (r) {
sc_log(ctx, "process_config_file doesn't find opensc config file. Please set the registry key."); sc_log(ctx, "process_config_file doesn't find opensc config file. Please set the registry key.");
return; return;
} }
temp_path[temp_len] = '\0';
conf_path = temp_path; conf_path = temp_path;
#else #else
conf_path = getenv("OPENSC_CONF"); conf_path = getenv("OPENSC_CONF");

View File

@ -840,7 +840,7 @@ int sc_ctx_detect_readers(sc_context_t *ctx);
*/ */
int sc_ctx_win32_get_config_value(const char *env, int sc_ctx_win32_get_config_value(const char *env,
const char *reg, const char *key, const char *reg, const char *key,
char *out, size_t *out_size); void *out, size_t *out_size);
/** /**
* Returns a pointer to the specified sc_reader_t object * Returns a pointer to the specified sc_reader_t object

View File

@ -342,11 +342,12 @@ sc_profile_load(struct sc_profile *profile, const char *filename)
if (!profile_dir) { if (!profile_dir) {
#ifdef _WIN32 #ifdef _WIN32
temp_len = PATH_MAX; temp_len = PATH_MAX - 1;
res = sc_ctx_win32_get_config_value(NULL, "ProfileDir", "Software\\OpenSC Project\\OpenSC", res = sc_ctx_win32_get_config_value(NULL, "ProfileDir", "Software\\OpenSC Project\\OpenSC",
temp_path, &temp_len); temp_path, &temp_len);
if (res) if (res)
LOG_FUNC_RETURN(ctx, res); LOG_FUNC_RETURN(ctx, res);
temp_path[temp_len] = '\0';
profile_dir = temp_path; profile_dir = temp_path;
#else #else
profile_dir = SC_PKCS15_PROFILE_DIRECTORY; profile_dir = SC_PKCS15_PROFILE_DIRECTORY;