Skip to content

Commit b89c447

Browse files
author
Marc Stern
authored
Merge pull request #3149 from fzipi/fix-tmpnam
fix: remove usage of insecure tmpnam
2 parents 3f4c02f + 93aa06b commit b89c447

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

apache2/modsecurity.c

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,49 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
122122
return msce;
123123
}
124124

125+
int acquire_global_lock(apr_global_mutex_t *lock, apr_pool_t *mp) {
126+
apr_status_t rc;
127+
apr_file_t *lock_name;
128+
const char *temp_dir;
129+
const char *filename;
130+
131+
// get platform temp dir
132+
rc = apr_temp_dir_get(&temp_dir, mp);
133+
if (rc != APR_SUCCESS) {
134+
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, NULL, "ModSecurity: Could not get temp dir");
135+
return -1;
136+
}
137+
138+
// use temp path template for lock files
139+
char *path = apr_pstrcat(mp, temp_dir, GLOBAL_LOCK_TEMPLATE, NULL);
140+
141+
rc = apr_file_mktemp(&lock_name, path, 0, mp);
142+
if (rc != APR_SUCCESS) {
143+
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Could not create temporary file for global lock");
144+
return -1;
145+
}
146+
// below func always return APR_SUCCESS
147+
apr_file_name_get(&filename, lock_name);
148+
149+
rc = apr_global_mutex_create(&lock, filename, APR_LOCK_DEFAULT, mp);
150+
if (rc != APR_SUCCESS) {
151+
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Could not create global mutex");
152+
return -1;
153+
}
154+
#if !defined(MSC_TEST)
155+
#ifdef __SET_MUTEX_PERMS
156+
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
157+
rc = ap_unixd_set_global_mutex_perms(lock);
158+
#else
159+
rc = unixd_set_global_mutex_perms(lock);
160+
#endif
161+
if (rc != APR_SUCCESS) {
162+
return -1;
163+
}
164+
#endif /* SET_MUTEX_PERMS */
165+
#endif /* MSC_TEST */
166+
return APR_SUCCESS;
167+
}
125168
/**
126169
* Initialise the modsecurity engine. This function must be invoked
127170
* after configuration processing is complete as Apache needs to know the
@@ -132,7 +175,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
132175

133176
msce->auditlog_lock = msce->geo_lock = NULL;
134177
#ifdef GLOBAL_COLLECTION_LOCK
135-
msce->geo_lock = NULL;
178+
msce->dbm_lock = NULL;
136179
#endif
137180

138181
/**
@@ -145,65 +188,23 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
145188
#ifdef WITH_CURL
146189
curl_global_init(CURL_GLOBAL_ALL);
147190
#endif
148-
/* Serial audit log mutext */
149-
tmpnam(auditlog_lock_name);
150-
rc = apr_global_mutex_create(&msce->auditlog_lock, auditlog_lock_name, APR_LOCK_DEFAULT, mp);
191+
/* Serial audit log mutex */
192+
rc = acquire_global_lock(msce->auditlog_lock, mp);
151193
if (rc != APR_SUCCESS) {
152-
//ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, "mod_security: Could not create modsec_auditlog_lock");
153-
//return HTTP_INTERNAL_SERVER_ERROR;
154194
return -1;
155195
}
156196

157-
#if !defined(MSC_TEST)
158-
#ifdef __SET_MUTEX_PERMS
159-
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
160-
rc = ap_unixd_set_global_mutex_perms(msce->auditlog_lock);
161-
#else
162-
rc = unixd_set_global_mutex_perms(msce->auditlog_lock);
163-
#endif
164-
if (rc != APR_SUCCESS) {
165-
// ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, "mod_security: Could not set permissions on modsec_auditlog_lock; check User and Group directives");
166-
// return HTTP_INTERNAL_SERVER_ERROR;
167-
return -1;
168-
}
169-
#endif /* SET_MUTEX_PERMS */
170-
171-
tmpnam(geo_lock_name);
172-
rc = apr_global_mutex_create(&msce->geo_lock, geo_lock_name, APR_LOCK_DEFAULT, mp);
197+
rc = acquire_global_lock(msce->geo_lock, mp);
173198
if (rc != APR_SUCCESS) {
174199
return -1;
175200
}
176201

177-
#ifdef __SET_MUTEX_PERMS
178-
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
179-
rc = ap_unixd_set_global_mutex_perms(msce->geo_lock);
180-
#else
181-
rc = unixd_set_global_mutex_perms(msce->geo_lock);
182-
#endif
183-
if (rc != APR_SUCCESS) {
184-
return -1;
185-
}
186-
#endif /* SET_MUTEX_PERMS */
187-
188202
#ifdef GLOBAL_COLLECTION_LOCK
189-
tmpnam(dbm_lock_name);
190-
rc = apr_global_mutex_create(&msce->dbm_lock, dbm_lock_name, APR_LOCK_DEFAULT, mp);
203+
rc = acquire_global_lock(&msce->dbm_lock, mp);
191204
if (rc != APR_SUCCESS) {
192205
return -1;
193206
}
194-
195-
#ifdef __SET_MUTEX_PERMS
196-
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
197-
rc = ap_unixd_set_global_mutex_perms(msce->dbm_lock);
198-
#else
199-
rc = unixd_set_global_mutex_perms(msce->dbm_lock);
200-
#endif
201-
if (rc != APR_SUCCESS) {
202-
return -1;
203-
}
204-
#endif /* SET_MUTEX_PERMS */
205-
#endif
206-
#endif
207+
#endif /* GLOBAL_COLLECTION_LOCK */
207208

208209
return 1;
209210
}

apache2/modsecurity.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ typedef struct msc_parm msc_parm;
135135

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

138-
static char auditlog_lock_name[L_tmpnam];
139-
static char geo_lock_name[L_tmpnam];
140-
#ifdef GLOBAL_COLLECTION_LOCK
141-
static char dbm_lock_name[L_tmpnam];
142-
#endif
138+
#define GLOBAL_LOCK_TEMPLATE "/modsec-lock-tmp.XXXXXX"
143139

144140
extern DSOLOCAL char *new_server_signature;
145141
extern DSOLOCAL char *real_server_signature;
@@ -709,6 +705,9 @@ struct msc_parm {
709705
int pad_2;
710706
};
711707

708+
/* Reusable functions */
709+
int acquire_global_lock(apr_global_mutex_t *lock, apr_pool_t *mp);
710+
712711
/* Engine functions */
713712

714713
msc_engine DSOLOCAL *modsecurity_create(apr_pool_t *mp, int processing_mode);

0 commit comments

Comments
 (0)