Skip to content

v2.7.11

Choose a tag to compare

@matsumotory matsumotory released this 17 Nov 07:26
7f0f1c5

Behavior changes: read before upgrading

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::Var accessor (v.name = value) and Nginx::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 $args or $limit_rate —
    allocated the value struct (ngx_http_variable_value_t) with ngx_palloc and called
    the set_handler before filling its fields. The set_handler read len and data from
    the uninitialised struct: $limit_rate parsed an uninitialised size, and $args
    copied an uninitialised length and pointer into r->args. The assigned value
    therefore did not take effect, and using the variable afterwards — reading it back, or
    sending $args toward 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, data and len) before it calls the
    set_handler, as nginx does when it assigns such a variable. Both the accessor
    (ngx_mrb_var_set_vector) and Nginx::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 $args can carry them toward a
      client.
    • What the documentation promised: the accessor,
      Nginx::Var#method_missing,
      and Nginx::Var#set say 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.
  • Affected: a configuration whose mruby code assigns $args, $limit_rate or another
    variable that has a set_handler through Nginx::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

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 set headers->last to it, so the next
    ngx_list_push wrote 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 moving headers->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_agent and the like) and the same-name
    next chains (for example Cookie or X-Forwarded-For). It keeps headers->nalloc at or
    below the number of elements the last part's array can hold from its current start (used
    elements included), so ngx_list_push, which starts a new part once a part reaches
    nalloc, 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 keeps headers->last and
    headers->nalloc in step when an emptied part is unlinked and when the first part adopts
    the next.
  • Why it changed:
    • The defect: the split left headers->last pointing 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 move headers->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#delete and
      Nginx::Headers_out#delete say 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.
  • 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).

更新前に確認する挙動の変更

1. Nginx::Varが、$argsや$limit_rateのようにset_handlerを持つ変数に値を正しく代入する

Nginx::Varでset_handlerを持つ変数に代入する件のアドバイザリ:GHSA-r7v5-479r-hmqr

  • 変更前:Nginx::Varのアクセサ(v.name = value)とNginx::Var#set(name, value)は、set_handlerを持つnginxの変数に代入するとき、値の構造体(ngx_http_variable_value_t)をngx_pallocで確保し、そのフィールドを埋める前にset_handlerを呼んでいました。set_handlerを持つ変数とは、変更できる変数のうち、代入をnginxがハンドラで処理するもので、$argsや$limit_rateがその例です。set_handlerは、未初期化の構造体からlenとdataを読みます。$limit_rateは未初期化のサイズを解釈し、$argsは未初期化の長さとポインタをr->argsにコピーしていました。そのため、代入した値は反映されませんでした。また、その後に変数を使うと、ワーカーの未初期化のメモリを読みました。値を読み戻すときや、$argsをクライアントへ送るときがこれにあたります。その結果、ワーカープロセスが終了するか、ワーカーのメモリが外へ送られることがありました。
  • 変更後:ngx_mrubyは、set_handlerを呼ぶ前に、値の構造体のフラグ、data、lenを埋めるようになりました。nginxがこのような変数に代入するときと同じ順序です。アクセサ(ngx_mrb_var_set_vector)とNginx::Var#set(ngx_mrb_var_set_func)の両方を変更したので、set_handlerを持つすべての変数で、代入した値が反映されます。
  • 変更の理由:
    • 不具合:set_handlerに渡す値の構造体は、ngx_pallocで確保したままで、フィールドが設定されていませんでした。そのため、set_handlerは未初期化の長さとポインタを読みました。
    • 前の挙動を残せなかった理由:代入した値が変数に届きませんでした。また、未初期化のメモリを読むことは、メモリ安全性の問題です。ワーカーはメモリを使い回すので、そのメモリに以前のデータの断片やヒープのポインタが残っていることがあり、それが$argsを通じてクライアントへ送られることがあります。
    • ドキュメントが示していた挙動:アクセサのNginx::Var#method_missingとNginx::Var#setは、nginxの変数に値を設定すると書いています。set_handlerを持つ変数に代入すると、変数が設定されないままになることや、関係の無いメモリを読むことについては、ドキュメントに書かれていません。
  • 影響の範囲:mrubyのコードで、$args、$limit_rateなどset_handlerを持つ変数にNginx::Varのアクセサか#setで代入し、その後にその変数を使う設定です。
  • 必要な対応:対応は不要です。代入した値は反映されます。以前のリリースのまま更新できない場合は、これらの変数の値は、Nginx::Varではなくnginxの設定ファイルで指定してください。
  • テスト:test/t/cases/var_setter_init.rbのポート18104のテストです。

2. リクエストかレスポンスのヘッダの削除が、ヘッダのリストの外に書き込まず、名前が完全に一致するヘッダだけを削除し、後で追加したヘッダを残す

リクエストかレスポンスのヘッダの削除の件のアドバイザリ:GHSA-2gcp-h74q-c9g6

  • 変更前:ngx_mrb_del_request_headerは、ヘッダのngx_listを順に調べ、満杯のpartの途中で名前が一致すると、そのpartを分割していました。この関数は、Nginx::Headers_in#deleteとNginx::Headers_out#deleteから呼ばれます。[]=も、ヘッダを更新するとき、追加の前の削除にこの関数を呼びます。分割では、元のpartの配列の途中を指す2つ目のpartを作り、headers->lastをそのpartに設定していました。そのため、次のngx_list_pushは、その配列の末尾の1つ先に要素を書き込みました。これは、隣接するワーカーのプールのメモリを壊すヒープバッファオーバーフローで、ワーカープロセスが終了することがありました。また、名前の比較では、削除に指定した名前の長さの分だけを比べていました。そのため、保存されているヘッダの名前の接頭辞にあたる名前で削除すると、そのヘッダを削除しました。また、指定した名前より短い保存済みのキーでは、その末尾を越えて読みました。さらに、2つ目のpartがあるときに最初のpartが空になると、headers->lastを動かさずに、次のpartを最初のpartの位置に移していました。その結果、その後に追加したヘッダは、リストから切り離されたpartに書き込まれ、読み戻せなくなっていました。
  • 変更後:削除は、一致した要素を取り除くとき、ほかの要素を動かしません。そのため、nginxがこのリストの要素を指して保持しているポインタは、同じヘッダを指したままになります。このポインタは2種類です。1つは、nginxが専用のフィールドを持つヘッダのngx_table_elt_t *で、r->headers_in.user_agentなどがあります。もう1つは、同じ名前のヘッダをつなぐnextで、CookieやX-Forwarded-Forがその例です。また、headers->nallocを、最後のpartの配列が現在の先頭から保持できる要素数以下に保ちます。この要素数には、使用中の要素も含みます。ngx_list_pushは、partの要素の数がnallocに達すると新しいpartを確保するので、最後のpartの配列の先へ書き込まなくなります。名前の比較では、バイト列の前に長さを比べます。そのため、保存されているヘッダの名前の接頭辞にあたる名前は一致しなくなり、指定した名前より短い保存済みのキーの末尾を越えて読むこともなくなります。空になったpartをリストから外すときと、次のpartを最初のpartの位置に移すときには、headers->lastとheaders->nallocを、互いに合うように必ず更新します。
  • 変更の理由:
    • 不具合:分割によってheaders->lastが満杯の配列の途中を指したので、次のpushがその配列の先に書き込みました。名前の比較は、保存されている名前の長さを見ていませんでした。次のpartを最初のpartの位置に移すときに、headers->lastを動かしていませんでした。
    • 前の挙動を残せなかった理由:配列の範囲外への書き込みはメモリ安全性の問題で、ヒープを壊してワーカーを終了させることがあります。長い名前のヘッダを、その名前の接頭辞で削除するのは誤りです。最初のpartが空になった後に追加したヘッダは、失われていました。
    • ドキュメントが示していた挙動:Nginx::Headers_in#deleteとNginx::Headers_out#deleteは、deleteが指定した名前のヘッダを削除すると書いています。名前が指定した名前で始まるだけのヘッダを削除すること、リストのpartの範囲を越えて書き込むこと、後から追加したヘッダを失うことは、ドキュメントに書かれていません。
  • 影響の範囲:mrubyのコードで、リクエストかレスポンスのヘッダを削除する設定です。オーバーフローの影響を受けるのは、ヘッダを削除したうえで、リストのpartを満杯にするだけのヘッダを持つリクエストに、さらにヘッダを追加する設定です。接頭辞の件で影響を受けるのは、別のヘッダの名前の接頭辞にあたる名前でヘッダを削除し、その両方のヘッダが削除されることを前提にしていた設定です。
  • 必要な対応:オーバーフローと、追加したヘッダが失われる件については、対応は不要です。接頭辞の件については、ヘッダごとに正確な名前で削除してください。
  • テスト:test/t/cases/header_del.rbのポート18105のテストです。