ngx_mruby v2.7.11
Behavior changes: read before upgrading
Section titled “Behavior changes: read before upgrading”1. Nginx::Var assigns a variable that has a set_handler, such as $args or $limit_rate
Section titled “1. Nginx::Var assigns a variable that has a set_handler, such as $args or $limit_rate”Security advisory (assigning a variable that has a set_handler through Nginx::Var): GHSA-r7v5-479r-hmqr.
- Before: the
Nginx::Varaccessor (v.name = value) andNginx::Var#set(name, value), when the name is an nginx variable that has a set_handler — a changeable variable whose assignment nginx routes through a handler, such as$argsor$limit_rate— allocated the value struct (ngx_http_variable_value_t) withngx_pallocand called the set_handler before filling its fields. The set_handler readlenanddatafrom the uninitialised struct:$limit_rateparsed an uninitialised size, and$argscopied an uninitialised length and pointer intor->args. The assigned value therefore did not take effect, and using the variable afterwards — reading it back, or sending$argstoward a client — read uninitialised worker memory, which could end the worker process or send worker memory out. - Now: ngx_mruby fills the value struct (its flags,
dataandlen) before it calls the set_handler, as nginx does when it assigns such a variable. Both the accessor (ngx_mrb_var_set_vector) andNginx::Var#set(ngx_mrb_var_set_func) are changed, so the assigned value takes effect for every variable that has a set_handler. - Why it changed:
- The defect: the set_handler was called with a value struct straight from
ngx_palloc, whose fields were not set, so it read an uninitialised length and pointer. - Why the old behavior could not be kept: the assignment did not reach the variable, and
the uninitialised read is memory-unsafety — on a reused worker the memory could hold
fragments of earlier data and heap pointers, and
$argscan carry them toward a client. - What the documentation promised: the accessor,
Nginx::Var#method_missing, andNginx::Var#setsay they set the value to the nginx variable. They do not say that assigning a variable that has a set_handler leaves it unset or reads other memory.
- The defect: the set_handler was called with a value struct straight from
- Affected: a configuration whose mruby code assigns
$args,$limit_rateor another variable that has a set_handler throughNginx::Var(the accessor or#set) and then uses the variable. - What to do: nothing; the assignment works. On an earlier release that you cannot
upgrade, set these variables through the nginx configuration instead of
Nginx::Var. - Tests: port 18104 (
test/t/cases/var_setter_init.rb).
2. Deleting a request or response header stays inside the header list, matches the exact name, and keeps a header added afterwards
Section titled “2. Deleting a request or response header stays inside the header list, matches the exact name, and keeps a header added afterwards”Security advisory (deleting a request or response header): GHSA-2gcp-h74q-c9g6.
- Before:
ngx_mrb_del_request_header(Nginx::Headers_in#delete/Nginx::Headers_out#delete, and the[]=update path, which deletes before it adds) walked the header ngx_list and, on a match in the middle of a full part, split the part: it made a second part that pointed inside the part’s array and setheaders->lastto it, so the nextngx_list_pushwrote one element past the end of that array — a heap buffer overflow into adjacent worker-pool memory, which could end the worker process. It also compared only the requested name’s length, so a name that is a prefix of a longer stored header matched and deleted that longer header, and a stored name shorter than the requested name was read past its end; and when the first part emptied while a second part existed, it adopted the next part without movingheaders->last, so a header added afterwards was written into the detached part and could not be read back. - Now: the delete removes the matching element without moving the other elements, so the
pointers nginx caches into this list stay on the same headers: the
ngx_table_elt_t *it keeps for a well-known header (r->headers_in.user_agentand the like) and the same-namenextchains (for example Cookie or X-Forwarded-For). It keepsheaders->nallocat or below the number of elements the last part’s array can hold from its current start (used elements included), songx_list_push, which starts a new part once a part reachesnalloc, does not write past the last part’s array. It compares the length of the name before its bytes, so a name that is a prefix of a stored header no longer matches and the compare does not read past a shorter stored key. It keepsheaders->lastandheaders->nallocin step when an emptied part is unlinked and when the first part adopts the next. - Why it changed:
- The defect: the split left
headers->lastpointing inside a full array so the next push wrote past it; the compare ignored the stored name’s length; and adopting the next part did not moveheaders->last. - Why the old behavior could not be kept: the out-of-bounds write is memory-unsafety (heap corruption that can end the worker); deleting a longer header by a prefix of its name is wrong; and a header added after the first part emptied was lost.
- What the documentation promised:
Nginx::Headers_in#deleteandNginx::Headers_out#deletesay delete removes the header of the given name. They do not say that it removes a header whose name only begins with the given name, that it writes past a list part, or that a header added afterwards can be lost.
- The defect: the split left
- Affected: a configuration whose mruby code deletes request or response headers (and, for the overflow, adds headers to a request that already carries enough headers to fill a list part); and, for the prefix case, a configuration that deleted a header by a name that is a prefix of another header and relied on both being removed.
- What to do: nothing for the overflow and the lost header. For the prefix case, delete each header by its exact name.
- Tests: port 18105 (
test/t/cases/header_del.rb).
New features
Section titled “New features”None.
None.
Build and test changes
Section titled “Build and test changes”test/t/cases/var_setter_init.rb(withtest/conf/conf.d/72-var-setter-init.conf, port 18104, andtest/html/var_setter_init.rb) assigns$argsthrough the setter ofNginx::Varand throughNginx::Var#setand reads it back (GHSA-r7v5-479r-hmqr)test/t/cases/header_del.rb(withtest/conf/conf.d/73-header-del.conf, port 18105, andtest/html/header_del.rb) deletes a request header: a name that is a prefix of a header, emptying the full first part while a second part exists, deleting from the middle of a full part before adding headers, and deleting a middle header and then the first header so that a laterUser-Agentkeeps the pointer nginx caches for it (r->headers_in.user_agent, which$http_user_agentreads) (GHSA-2gcp-h74q-c9g6)
Upgrading
Section titled “Upgrading”- No directive was added, renamed or removed. Rebuild the module with this release the way
you built 2.7.10, and replace the binary or the dynamic module as usual.
Nginx.module_versionreturns2.7.11.
Read “Behavior changes: read before upgrading” above before upgrading.