Skip to content

Commit 6944858

Browse files
committed
fix: remove insecure tpmnam usage
Signed-off-by: Felipe Zipitria <[email protected]>
1 parent 10b089a commit 6944858

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

apache2/modsecurity.c

+34-14
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
129129
*/
130130
int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
131131
apr_status_t rc;
132-
apr_file_t *auditlog_lock_name;
133-
apr_file_t *geo_lock_name;
134-
apr_file_t *dbm_lock_name;
135-
132+
apr_file_t *auditlog_lock_file = NULL;
133+
apr_file_t *geo_lock_file = NULL;
134+
apr_file_t *dbm_lock_file = NULL;
135+
const char *temp_dir = NULL;
136+
char *temp_path_template = NULL;
137+
const char *temp_file;
138+
139+
// get temp path suitable for writing
140+
rc = apr_temp_dir_get(&temp_dir, mp);
141+
if (rc != APR_SUCCESS) {
142+
return -1;
143+
}
136144
// use temp path template for lock files
137-
char *path = apr_pstrcat(p, temp_dir, "/modsec-lock-tmp.XXXXXX", NULL);
145+
temp_path_template = apr_pstrcat(mp, temp_dir, "/modsec-lock-tmp.XXXXXX", NULL);
138146

139147
msce->auditlog_lock = msce->geo_lock = NULL;
140148
#ifdef GLOBAL_COLLECTION_LOCK
@@ -152,11 +160,15 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
152160
curl_global_init(CURL_GLOBAL_ALL);
153161
#endif
154162
/* Serial audit log mutext */
155-
rc = apr_file_mktemp(&auditlog_lock_name, path, 0, p)
163+
rc = apr_file_mktemp(&auditlog_lock_file, temp_path_template, 0, mp);
156164
if (rc != APR_SUCCESS) {
157-
return -1
165+
return -1;
166+
}
167+
rc = apr_file_name_get(&temp_file, auditlog_lock_file);
168+
if (rc != APR_SUCCESS) {
169+
return -1;
158170
}
159-
rc = apr_global_mutex_create(&msce->auditlog_lock, auditlog_lock_name, APR_LOCK_DEFAULT, mp);
171+
rc = apr_global_mutex_create(&msce->auditlog_lock, temp_file, APR_LOCK_DEFAULT, mp);
160172
if (rc != APR_SUCCESS) {
161173
return -1;
162174
}
@@ -175,11 +187,15 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
175187
}
176188
#endif /* SET_MUTEX_PERMS */
177189

178-
rc = apr_file_mktemp(&geo_lock_name, path, 0, p)
190+
rc = apr_file_mktemp(&geo_lock_file, temp_path_template, 0, mp);
179191
if (rc != APR_SUCCESS) {
180-
return -1
192+
return -1;
193+
}
194+
rc = apr_file_name_get(&temp_file, geo_lock_file);
195+
if (rc != APR_SUCCESS) {
196+
return -1;
181197
}
182-
rc = apr_global_mutex_create(&msce->geo_lock, geo_lock_name, APR_LOCK_DEFAULT, mp);
198+
rc = apr_global_mutex_create(&msce->geo_lock, temp_file, APR_LOCK_DEFAULT, mp);
183199
if (rc != APR_SUCCESS) {
184200
return -1;
185201
}
@@ -196,11 +212,15 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
196212
#endif /* SET_MUTEX_PERMS */
197213

198214
#ifdef GLOBAL_COLLECTION_LOCK
199-
rc = apr_file_mktemp(&dbm_lock_name, path, 0, p)
215+
rc = apr_file_mktemp(&dbm_lock_file, temp_path_template, 0, mp);
200216
if (rc != APR_SUCCESS) {
201-
return -1
217+
return -1;
218+
}
219+
rc = apr_file_name_get(&temp_file, dbm_lock_file);
220+
if (rc != APR_SUCCESS) {
221+
return -1;
202222
}
203-
rc = apr_global_mutex_create(&msce->dbm_lock, dbm_lock_name, APR_LOCK_DEFAULT, mp);
223+
rc = apr_global_mutex_create(&msce->dbm_lock, temp_file, APR_LOCK_DEFAULT, mp);
204224
if (rc != APR_SUCCESS) {
205225
return -1;
206226
}

apache2/modsecurity.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct msc_parm msc_parm;
5252
#include "apr_md5.h"
5353
#include "apr_strings.h"
5454
#include "apr_hash.h"
55+
#include "apr_file_io.h"
5556
#include "httpd.h"
5657
#include "http_config.h"
5758
#include "http_log.h"
@@ -135,10 +136,10 @@ typedef struct msc_parm msc_parm;
135136

136137
#define FATAL_ERROR "ModSecurity: Fatal error (memory allocation or unexpected internal error)!"
137138

138-
static char auditlog_lock_name[L_tmpnam];
139-
static char geo_lock_name[L_tmpnam];
139+
static char auditlog_lock_name[APR_PATH_MAX];
140+
static char geo_lock_name[APR_PATH_MAX];
140141
#ifdef GLOBAL_COLLECTION_LOCK
141-
static char dbm_lock_name[L_tmpnam];
142+
static char dbm_lock_name[APR_PATH_MAX];
142143
#endif
143144

144145
extern DSOLOCAL char *new_server_signature;

0 commit comments

Comments
 (0)