@@ -111,6 +111,7 @@ static int load_mmap(struct selabel_handle *rec, const char *path,
struct mmap_area *mmap_area;
uint32_t i, magic, version;
uint32_t entry_len, stem_map_len, regex_array_len;
+ const char *reg_version;
if (isbinary) {
len = strlen(path);
@@ -174,11 +175,13 @@ static int load_mmap(struct selabel_handle *rec, const char *path,
if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
return -1;
+ reg_version = regex_version();
+ if (!reg_version)
+ return -1;
+
if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
- if (!regex_version()) {
- return -1;
- }
- len = strlen(regex_version());
+
+ len = strlen(reg_version);
rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t));
if (rc < 0)
@@ -200,7 +203,7 @@ static int load_mmap(struct selabel_handle *rec, const char *path,
}
str_buf[entry_len] = '\0';
- if ((strcmp(str_buf, regex_version()) != 0)) {
+ if ((strcmp(str_buf, reg_version) != 0)) {
free(str_buf);
return -1;
}
@@ -49,19 +49,13 @@ err: regex_data_free(*regex);
char const * regex_version(void) {
#ifdef USE_PCRE2
- static int initialized = 0;
- static char * version_string = NULL;
- size_t version_string_len;
- if (!initialized) {
- version_string_len = pcre2_config(PCRE2_CONFIG_VERSION, NULL);
- version_string = (char*) malloc(version_string_len);
- if (!version_string) {
- return NULL;
- }
- pcre2_config(PCRE2_CONFIG_VERSION, version_string);
- initialized = 1;
- }
- return version_string;
+ static char version_buf[256];
+ size_t len = pcre2_config(PCRE2_CONFIG_VERSION, NULL);
+ if (len <= 0 || len > sizeof(version_buf))
+ return NULL;
+
+ pcre2_config(PCRE2_CONFIG_VERSION, version_buf);
+ return version_buf;
#else
return pcre_version();
#endif
@@ -101,6 +101,7 @@ static int write_binary_file(struct saved_data *data, int fd)
uint32_t section_len;
uint32_t i;
int rc;
+ const char *reg_version;
bin_file = fdopen(fd, "w");
if (!bin_file) {
@@ -120,13 +121,14 @@ static int write_binary_file(struct saved_data *data, int fd)
goto err;
/* write version of the regex back-end */
- if (!regex_version())
+ reg_version = regex_version();
+ if (!reg_version)
goto err;
- section_len = strlen(regex_version());
+ section_len = strlen(reg_version);
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
if (len != 1)
goto err;
- len = fwrite(regex_version(), sizeof(char), section_len, bin_file);
+ len = fwrite(reg_version, sizeof(char), section_len, bin_file);
if (len != section_len)
goto err;