Skip to content

Commit

Permalink
🐛 Memory leak (#75).
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Jan 5, 2022
1 parent 922599e commit 6b1dceb
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
6 changes: 6 additions & 0 deletions inc/ngx_http_waf_module_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ ngx_int_t ngx_http_waf_rule_deatils_handler(ngx_http_request_t* r, ngx_http_vari
ngx_int_t ngx_http_waf_spend_handler(ngx_http_request_t* r, ngx_http_variable_value_t* v, uintptr_t data);


/**
* @brief 初始化结构体 ngx_http_waf_main_conf_t
*/
void* ngx_http_waf_create_main_conf(ngx_conf_t* cf);


/**
* @brief 初始化结构体 ngx_http_waf_loc_conf_t
*/
Expand Down
3 changes: 3 additions & 0 deletions inc/ngx_http_waf_module_lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ void lru_cache_delete(lru_cache_t* lru, void* key, size_t key_len);
void lru_cache_eliminate(lru_cache_t* lru, size_t count);


void lru_cache_destory(lru_cache_t* lru);


#endif
8 changes: 8 additions & 0 deletions inc/ngx_http_waf_module_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ typedef struct ngx_http_waf_ctx_s {
} ngx_http_waf_ctx_t;


/**
* @struct ngx_http_waf_loc_conf_t
*/
typedef struct ngx_http_waf_main_conf_s {
ngx_array_t *local_caches; /**< 已经启用的所有的缓存管理器数组 */
} ngx_http_waf_main_conf_t;


/**
* @struct ngx_http_waf_loc_conf_t
* @brief 每个 server 块的配置块
Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_waf_module_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ ngx_int_t ngx_http_waf_regex_exec_arrray_sqli_xss(ngx_http_request_t* r,
if (ngx_http_waf_check_flag(loc_conf->waf_mode, NGX_HTTP_WAF_MODE_EXTRA_CACHE) == NGX_HTTP_WAF_TRUE
&& loc_conf->waf_inspection_capacity != NGX_CONF_UNSET
&& cache != NULL) {

lru_cache_find_result_t tmp = lru_cache_find(cache, str->data, sizeof(u_char) * str->len);
if (tmp.status == NGX_HTTP_WAF_KEY_EXISTS) {
cache_hit = NGX_HTTP_WAF_SUCCESS;
Expand Down
81 changes: 67 additions & 14 deletions src/ngx_http_waf_module_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ extern ngx_module_t ngx_http_waf_module;

extern FILE* ngx_http_waf_in;


static void _cleanup_lru_cache(void* data);


char* ngx_http_waf_conf(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
if (ngx_conf_set_flag_slot(cf, cmd, conf) != NGX_CONF_OK) {
return NGX_CONF_ERROR;
Expand Down Expand Up @@ -419,6 +423,27 @@ char* ngx_http_waf_http_status_conf(ngx_conf_t* cf, ngx_command_t* cmd, void* co
}


void* ngx_http_waf_create_main_conf(ngx_conf_t* cf) {
ngx_http_waf_main_conf_t* main_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_waf_main_conf_t));

if (main_conf == NULL) {
return NULL;
}

main_conf->local_caches = ngx_array_create(cf->pool, 20, sizeof(lru_cache_t*));

if (main_conf->local_caches == NULL) {
return NULL;
}

ngx_pool_cleanup_t* cln = ngx_pool_cleanup_add(cf->pool, 0);
cln->data = main_conf->local_caches;
cln->handler = _cleanup_lru_cache;

return main_conf;
}


void* ngx_http_waf_create_loc_conf(ngx_conf_t* cf) {
return ngx_http_waf_init_conf(cf);
}
Expand Down Expand Up @@ -959,34 +984,52 @@ ngx_int_t ngx_http_waf_init_cc_shm(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf


ngx_int_t ngx_http_waf_init_lru_cache(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf) {
conf->black_url_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->black_args_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->black_ua_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->black_referer_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->black_cookie_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->white_url_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
conf->white_referer_inspection_cache = ngx_pcalloc(cf->pool, sizeof(lru_cache_t));
ngx_http_waf_main_conf_t* main_conf = ngx_http_conf_get_module_main_conf(cf, ngx_http_waf_module);

conf->black_url_inspection_cache = NULL;
conf->black_args_inspection_cache = NULL;
conf->black_ua_inspection_cache = NULL;
conf->black_referer_inspection_cache = NULL;
conf->black_cookie_inspection_cache = NULL;
conf->white_url_inspection_cache = NULL;
conf->white_referer_inspection_cache = NULL;

lru_cache_t** p = NULL;

lru_cache_init(&conf->black_url_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->black_url_inspection_cache;

lru_cache_init(&conf->black_args_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->black_args_inspection_cache;

lru_cache_init(&conf->black_ua_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->black_ua_inspection_cache;

lru_cache_init(&conf->black_referer_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->black_referer_inspection_cache;

lru_cache_init(&conf->black_cookie_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->black_cookie_inspection_cache;

lru_cache_init(&conf->white_url_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->white_url_inspection_cache;

lru_cache_init(&conf->white_referer_inspection_cache,
conf->waf_inspection_capacity, gernal_pool, cf->pool);
conf->waf_inspection_capacity, std, NULL);
p = ngx_array_push(main_conf->local_caches);
*p = conf->white_referer_inspection_cache;

return NGX_HTTP_WAF_SUCCESS;
}
Expand Down Expand Up @@ -1131,3 +1174,13 @@ ngx_int_t ngx_http_waf_free_memory(ngx_conf_t* cf, ngx_http_waf_loc_conf_t* conf

return NGX_HTTP_WAF_SUCCESS;
}


static void _cleanup_lru_cache(void* data) {
ngx_array_t* caches = (ngx_array_t*)data;

for (ngx_uint_t i = 0; i < caches->nelts; i++) {
lru_cache_t* cache = ((lru_cache_t**)caches->elts)[i];
lru_cache_destory(cache);
}
}
2 changes: 1 addition & 1 deletion src/ngx_http_waf_module_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static ngx_command_t ngx_http_waf_commands[] = {
static ngx_http_module_t ngx_http_waf_module_ctx = {
NULL,
ngx_http_waf_init_after_load_config,
NULL,
ngx_http_waf_create_main_conf,
NULL,
NULL,
NULL,
Expand Down
5 changes: 5 additions & 0 deletions src/ngx_http_waf_module_lru_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ void lru_cache_eliminate(lru_cache_t* lru, size_t count) {
}


void lru_cache_destory(lru_cache_t* lru) {
mem_pool_free(&lru->pool, lru);
}


lru_cache_item_t* _lru_cache_hash_find(lru_cache_t* lru, void* key, size_t key_len) {
lru_cache_item_t* ret;
HASH_FIND(hh, lru->hash_head, key, key_len, ret);
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_waf_module_mem_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ngx_int_t mem_pool_init(mem_pool_t* pool, mem_pool_type_e type, void* native_poo
void* mem_pool_calloc(mem_pool_t* pool, ngx_uint_t byte_size) {
void* addr;
switch (pool->type) {
case std: addr = malloc(byte_size); ngx_memzero(addr, byte_size);break;
case std: addr = malloc(byte_size); ngx_memzero(addr, byte_size); break;
case gernal_pool: addr = ngx_pcalloc(pool->native_pool.gernal_pool, byte_size); break;
case slab_pool: addr = ngx_slab_calloc_locked(pool->native_pool.slab_pool, byte_size); break;
default: addr = NULL; break;
Expand Down

0 comments on commit 6b1dceb

Please sign in to comment.