Skip to content

Commit 81e140c

Browse files
Ka-Hing Cheungagentzh
authored andcommitted
bugfix: pure C API for ngx.var assignment: we failed to output the error message length.
this might lead to error buffer overreads. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 18b5de5 commit 81e140c

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/ngx_http_lua_variable.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ ngx_http_lua_ffi_var_get(ngx_http_request_t *r, u_char *name_data,
364364
int
365365
ngx_http_lua_ffi_var_set(ngx_http_request_t *r, u_char *name_data,
366366
size_t name_len, u_char *lowcase_buf, u_char *value, size_t value_len,
367-
u_char *errbuf, size_t errlen)
367+
u_char *errbuf, size_t *errlen)
368368
{
369369
u_char *p;
370370
ngx_uint_t hash;
@@ -373,12 +373,15 @@ ngx_http_lua_ffi_var_set(ngx_http_request_t *r, u_char *name_data,
373373
ngx_http_core_main_conf_t *cmcf;
374374

375375
if (r == NULL) {
376-
ngx_snprintf(errbuf, errlen, "no request object found");
376+
*errlen = ngx_snprintf(errbuf, *errlen, "no request object found")
377+
- errbuf;
377378
return NGX_ERROR;
378379
}
379380

380381
if ((r)->connection->fd == (ngx_socket_t) -1) {
381-
ngx_snprintf(errbuf, errlen, "API disabled in the current context");
382+
*errlen = ngx_snprintf(errbuf, *errlen,
383+
"API disabled in the current context")
384+
- errbuf;
382385
return NGX_ERROR;
383386
}
384387

@@ -395,8 +398,10 @@ ngx_http_lua_ffi_var_set(ngx_http_request_t *r, u_char *name_data,
395398
if (v) {
396399
if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
397400
dd("variable not changeable");
398-
ngx_snprintf(errbuf, errlen, "variable \"%*s\" not changeable",
399-
name_len, lowcase_buf);
401+
*errlen = ngx_snprintf(errbuf, *errlen,
402+
"variable \"%*s\" not changeable",
403+
name_len, lowcase_buf)
404+
- errbuf;
400405
return NGX_ERROR;
401406
}
402407

@@ -475,23 +480,27 @@ ngx_http_lua_ffi_var_set(ngx_http_request_t *r, u_char *name_data,
475480
return NGX_OK;
476481
}
477482

478-
ngx_snprintf(errbuf, errlen, "variable \"%*s\" cannot be assigned "
479-
"a value", name_len, lowcase_buf);
483+
*errlen = ngx_snprintf(errbuf, *errlen,
484+
"variable \"%*s\" cannot be assigned "
485+
"a value", name_len, lowcase_buf)
486+
- errbuf;
480487
return NGX_ERROR;
481488
}
482489

483490
/* variable not found */
484491

485-
ngx_snprintf(errbuf, errlen, "variable \"%*s\" not found for writing; "
486-
"maybe it is a built-in variable that is not changeable "
487-
"or you forgot to use \"set $%*s '';\" "
488-
"in the config file to define it first",
489-
name_len, lowcase_buf, name_len, lowcase_buf);
492+
*errlen = ngx_snprintf(errbuf, *errlen,
493+
"variable \"%*s\" not found for writing; "
494+
"maybe it is a built-in variable that is not "
495+
"changeable or you forgot to use \"set $%*s '';\" "
496+
"in the config file to define it first",
497+
name_len, lowcase_buf, name_len, lowcase_buf)
498+
- errbuf;
490499
return NGX_ERROR;
491500

492501
nomem:
493502

494-
ngx_snprintf(errbuf, errlen, "no memory");
503+
*errlen = ngx_snprintf(errbuf, *errlen, "no memory") - errbuf;
495504
return NGX_ERROR;
496505
}
497506
#endif /* NGX_LUA_NO_FFI_API */

0 commit comments

Comments
 (0)