Fix segfault if config file is not present or unparseable
Make parse_ini_file return a failure if the configuration could not be
extracted and make C_Initialize return and error for this case. This
prevents any correct PKCS11 token user from doing anything else with
the token.
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
diff --git a/ini.c b/ini.c
index 17c8e4c..3b8b89d 100644
--- a/ini.c
+++ b/ini.c
@@ -95,7 +95,7 @@
}
}
-void parse_ini_file(void)
+int parse_ini_file(void)
{
char *home = getenv("HOME");
char *conf = getenv(ENV_CONFIG);
@@ -118,37 +118,37 @@
if (fd < 0) {
fprintf(stderr, "failed to open config file %s: %s\n",
config, strerror(errno));
- return;
+ return 0;
}
if (fstat(fd, &st)) {
fprintf(stderr, "failed to stat config file %s: %s\n",
config, strerror(errno));
- return;
+ return 0;
}
conf_size = st.st_size;
if (conf_size == 0) {
fprintf(stderr, "conf file %s is empty\n", config);
- return;
+ return 0;
}
conf_buf = malloc(conf_size + 2);
if (!conf_buf) {
fprintf(stderr, "failed to allocate %d bytes for con file %s\n",
(int)conf_size, config);
- return;
+ return 0;
}
len = read(fd, conf_buf, conf_size);
if (len < 0) {
fprintf(stderr, "failed to read config file %s: %s\n",
config, strerror(errno));
- return;
+ return 0;
}
if (len != conf_size) {
fprintf(stderr, "Config file %s changed while being read\n",
config);
free(conf_buf);
- return;
+ return 0;
}
close(fd);
@@ -160,6 +160,7 @@
*end = '\0';
parse_mem();
cache_load_crypto_keys();
+ return 1;
}
void free_ini_file(void)
diff --git a/openssl-pkcs11.h b/openssl-pkcs11.h
index a1d5e12..1ab72f9 100644
--- a/openssl-pkcs11.h
+++ b/openssl-pkcs11.h
@@ -43,7 +43,7 @@
CK_MECHANISM_INFO_PTR info);
/* ini.c exported functions */
-void parse_ini_file(void);
+int parse_ini_file(void);
void free_ini_file(void);
/* cache.c exported functions */
diff --git a/pkcs11.c b/pkcs11.c
index 21b336d..47a826a 100644
--- a/pkcs11.c
+++ b/pkcs11.c
@@ -94,9 +94,10 @@
* can't do threading */
return CKR_CANT_LOCK;
}
- parse_ini_file();
-
- return CKR_OK;
+ if (parse_ini_file())
+ return CKR_OK;
+ else
+ return CKR_GENERAL_ERROR;
}
CK_RV