Skip to content

Commit 1762183

Browse files
committed
bugfix: semaphore: memory invalid reads might happen when using ngx.semaphore in init_by_lua*.
1 parent 8ac6fe7 commit 1762183

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

src/ngx_http_lua_directive.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ ngx_http_lua_shared_dict(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
120120
ctx->name = name;
121121
ctx->main_conf = lmcf;
122122
ctx->log = &cf->cycle->new_log;
123+
ctx->cycle = cf->cycle;
123124

124125
zone = ngx_shared_memory_add(cf, &name, (size_t) size,
125126
&ngx_http_lua_module);

src/ngx_http_lua_semaphore.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,9 @@ ngx_http_lua_ffi_semaphore_new(ngx_http_lua_semaphore_t **psem,
293293

294294
sem->resource_count = n;
295295
sem->wait_count = 0;
296-
sem->log = ngx_cycle->log;
297296
*psem = sem;
298297

299-
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sem->log, 0,
298+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
300299
"http lua semaphore new: %p, resources: %d",
301300
sem, sem->resource_count);
302301

@@ -307,7 +306,7 @@ ngx_http_lua_ffi_semaphore_new(ngx_http_lua_semaphore_t **psem,
307306
int
308307
ngx_http_lua_ffi_semaphore_post(ngx_http_lua_semaphore_t *sem, int n)
309308
{
310-
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, sem->log, 0,
309+
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
311310
"http lua semaphore post: %p, n: %d, resources: %d",
312311
sem, n, sem->resource_count);
313312

@@ -333,7 +332,7 @@ ngx_http_lua_ffi_semaphore_wait(ngx_http_request_t *r,
333332
ngx_http_lua_co_ctx_t *wait_co_ctx;
334333
ngx_int_t rc;
335334

336-
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, sem->log, 0,
335+
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
337336
"http lua semaphore wait: %p, timeout: %d, "
338337
"resources: %d, event posted: %d",
339338
sem, wait_ms, sem->resource_count,
@@ -391,7 +390,7 @@ ngx_http_lua_ffi_semaphore_wait(ngx_http_request_t *r,
391390
wait_co_ctx->data = sem;
392391
wait_co_ctx->cleanup = ngx_http_lua_semaphore_cleanup;
393392

394-
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, sem->log, 0,
393+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
395394
"http lua semaphore wait yielding");
396395

397396
return NGX_AGAIN;
@@ -414,7 +413,7 @@ ngx_http_lua_semaphore_cleanup(void *data)
414413

415414
sem = coctx->data;
416415

417-
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, sem->log, 0,
416+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
418417
"http lua semaphore cleanup");
419418

420419
if (coctx->sleep.timer_set) {

src/ngx_http_lua_semaphore.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ typedef struct ngx_http_lua_semaphore_s {
3535
ngx_queue_t wait_queue;
3636
ngx_queue_t chain;
3737
ngx_event_t sem_event;
38-
ngx_log_t *log;
3938
ngx_http_lua_semaphore_mm_block_t *block;
4039
int resource_count;
4140
unsigned wait_count;

src/ngx_http_lua_shdict.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data)
5656
ngx_http_lua_shdict_ctx_t *octx = data;
5757

5858
size_t len;
59+
ngx_int_t rc;
60+
volatile ngx_cycle_t *saved_cycle;
5961
ngx_http_lua_shdict_ctx_t *ctx;
6062
ngx_http_lua_main_conf_t *lmcf;
6163

@@ -117,7 +119,14 @@ ngx_http_lua_shdict_init_zone(ngx_shm_zone_t *shm_zone, void *data)
117119
if (lmcf->shm_zones_inited == lmcf->shm_zones->nelts
118120
&& lmcf->init_handler)
119121
{
120-
if (lmcf->init_handler(ctx->log, lmcf, lmcf->lua) != NGX_OK) {
122+
saved_cycle = ngx_cycle;
123+
ngx_cycle = ctx->cycle;
124+
125+
rc = lmcf->init_handler(ctx->log, lmcf, lmcf->lua);
126+
127+
ngx_cycle = saved_cycle;
128+
129+
if (rc != NGX_OK) {
121130
/* an error happened */
122131
return NGX_ERROR;
123132
}

src/ngx_http_lua_shdict.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct {
3737
ngx_str_t name;
3838
ngx_http_lua_main_conf_t *main_conf;
3939
ngx_log_t *log;
40+
ngx_cycle_t *cycle;
4041
} ngx_http_lua_shdict_ctx_t;
4142

4243

0 commit comments

Comments
 (0)