Skip to content

Commit 4c6e58a

Browse files
author
Yasuo Ohgaki
committed
Fixed crash when save_path is invalid.
Fixed crash when user save handler is incorrectly used. Fixed crash when session read failed.
1 parent 665d386 commit 4c6e58a

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

ext/session/mod_files.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static void ps_files_close(ps_files *data)
123123
}
124124
}
125125

126-
static void ps_files_open(ps_files *data, const char *key)
126+
static int ps_files_open(ps_files *data, const char *key)
127127
{
128128
char buf[MAXPATHLEN];
129129
TSRMLS_FETCH();
@@ -138,7 +138,7 @@ static void ps_files_open(ps_files *data, const char *key)
138138

139139
if (!ps_files_valid_key(key) ||
140140
!ps_files_path_create(buf, sizeof(buf), data, key))
141-
return;
141+
return FAILURE;
142142

143143
data->lastkey = estrdup(key);
144144

@@ -153,10 +153,13 @@ static void ps_files_open(ps_files *data, const char *key)
153153
if (data->fd != -1)
154154
flock(data->fd, LOCK_EX);
155155

156-
if (data->fd == -1)
156+
if (data->fd == -1) {
157157
php_error(E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf,
158158
strerror(errno), errno);
159+
return FAILURE;
160+
}
159161
}
162+
return SUCCESS;
160163
}
161164

162165
static int ps_files_cleanup_dir(const char *dirname, int maxlifetime)
@@ -254,7 +257,9 @@ PS_READ_FUNC(files)
254257
struct stat sbuf;
255258
PS_FILES_DATA;
256259

257-
ps_files_open(data, key);
260+
if (ps_files_open(data, key) == FAILURE)
261+
return FAILURE;
262+
258263
if (data->fd < 0)
259264
return FAILURE;
260265

@@ -283,7 +288,9 @@ PS_WRITE_FUNC(files)
283288
long n;
284289
PS_FILES_DATA;
285290

286-
ps_files_open(data, key);
291+
if (ps_files_open(data, key) == FAILURE)
292+
return FAILURE;
293+
287294
if (data->fd < 0)
288295
return FAILURE;
289296

ext/session/session.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -543,19 +543,21 @@ static char *_php_create_id(int *newlen TSRMLS_DC)
543543
return estrdup(buf);
544544
}
545545

546-
static void php_session_initialize(TSRMLS_D)
546+
static int php_session_initialize(TSRMLS_D)
547547
{
548548
char *val;
549549
int vallen;
550550

551551
if (PS(mod)->open(&PS(mod_data), PS(save_path), PS(session_name)) == FAILURE) {
552552
php_error(E_ERROR, "Failed to initialize session module");
553-
return;
553+
return FAILURE;
554554
}
555-
if (PS(mod)->read(&PS(mod_data), PS(id), &val, &vallen) == SUCCESS) {
556-
php_session_decode(val, vallen TSRMLS_CC);
557-
efree(val);
555+
if (PS(mod)->read(&PS(mod_data), PS(id), &val, &vallen) == FAILURE) {
556+
return FAILURE;
558557
}
558+
php_session_decode(val, vallen TSRMLS_CC);
559+
efree(val);
560+
return SUCCESS;
559561
}
560562

561563

@@ -946,11 +948,10 @@ static void php_session_start(TSRMLS_D)
946948
}
947949

948950
php_session_cache_limiter(TSRMLS_C);
949-
php_session_initialize(TSRMLS_C);
950-
951-
if (PS(mod_data) && PS(gc_probability) > 0) {
951+
if (php_session_initialize(TSRMLS_C) == SUCCESS &&
952+
PS(mod_data) && PS(gc_probability) > 0) {
952953
int nrdels = -1;
953-
954+
954955
nrand = (int) (100.0*php_combined_lcg(TSRMLS_C));
955956
if (nrand < PS(gc_probability)) {
956957
PS(mod)->gc(&PS(mod_data), PS(gc_maxlifetime), &nrdels);
@@ -962,6 +963,7 @@ static void php_session_start(TSRMLS_D)
962963
}
963964
}
964965

966+
965967
static zend_bool php_session_destroy(TSRMLS_D)
966968
{
967969
zend_bool retval = SUCCESS;

0 commit comments

Comments
 (0)