CVE-2022-35173 (C, published 2022-08-18T06:15Z)
Description: value: An issue was discovered in Nginx NJS v0.7.5. The JUMP offset for a break instruction was not set to a correct offset during code generation, leading to a segmentation violation.
Severity: nan (CVSS v3 base score: 7.5)
Weakness: CWE-754 \u2014 Improper Check for Unusual or Exceptional Conditions
The product does not check or incorrectly checks for unusual or exceptional conditions that are not expected to occur frequently during day to day operation of the product.
Repository: https://github.com/nginx/njs
Files changed:
src/njs_generator.c
src/test/njs_unit_test.c
Commit message: Fixed break instruction in a try-catch block.

Previously, JUMP offset for a break instruction inside a try-catch
block was not set to a correct offset during code generation
when a return instruction was present in inner try-catch block.

The fix is to update the JUMP offset appropriately.

This closes #553 issue on Github.


Vulnerable code:
                                        NJS_GENERATOR_ALL
                                        | NJS_GENERATOR_TRY, dest_label);
                 "  var r1 = 0, r2 = 0, r3 = 0;"
                 "  a:{ try { try { "
                 "              if (n == 0) { break a; } "
                 "              if (n == 1) { throw 'a'; } "
                 "            } "
                 "            catch (e) { break a; } finally { r1++; } } "
                 "      catch (e) {} "
                 "      finally { r2++; } "
                 "      r3++;  "
                 "  }; "
                 "return [r1, r2, r3]"
                 "}; njs.dump([f(0), f(1), f(3)])"),

Fixed code:
            /*
             * block can be NULL when &ctx->try_exit_label is "@return"
             * for outermost try-catch block.
             */


            } else {

                /*
                 * when block == NULL, we still want to patch the "finally"
                 * instruction break_offset.
                 */

                block = njs_generate_find_block(vm, generator->block,
                                                NJS_GENERATOR_ALL,
                                                &no_label);

                if (block != NULL) {
                    patch = njs_generate_make_exit_patch(vm, block, &no_label,
                                njs_code_offset(generator, finally)
                                + offsetof(njs_vmcode_finally_t, break_offset));
                    if (njs_slow_path(patch == NULL)) {
                        return NJS_ERROR;
                    }
                }
                                        NJS_GENERATOR_ALL, dest_label);

        } else {

            block = njs_generate_find_block(vm, generator->block,
                                            NJS_GENERATOR_ALL, &no_label);
            if (block != NULL) {
                patch = njs_generate_make_exit_patch(vm, block, &no_label,
                                njs_code_offset(generator, finally)
                                + offsetof(njs_vmcode_finally_t, break_offset));
                if (njs_slow_path(patch == NULL)) {
                    return NJS_ERROR;
                }
            }
              "  var r1 = 0, r2 = 0, r3 = 0;"
              "  a:{ try { try { "
              "              if (n == 0) { break a; } "
              "              if (n == 1) { throw 'a'; } "
              "            } "
              "            catch (e) { break a; } finally { r1++; } } "
              "      catch (e) {} "
              "      finally { r2++; } "
              "      r3++;  "
              "  }; "
              "return [r1, r2, r3]"
              "}; njs.dump([f(0), f(1), f(3)])"),

    { njs_str("function f(n) {"
              "    while (1)"
              "           try {"
              "              if (n == 0) { break; }"
              "              if (n == 1) { throw 'a'; }"
              ""
              "              try { return 42; }"
              "              catch (a) {}"
              ""
              "            } catch (b) { return b; }"
              "};"
              "njs.dump([f(0), f(1), f(2)])"),
      njs_str("[undefined,'a',42]") },

    { njs_str("function f(n, r) {"
              "    while (1)"
              "           try {"
              "              if (n == 0) { break; }"
              "              if (n == 1) { throw 'a'; }"
              ""
              "              try { return 42; }"
              "              catch (a) {}"
              "              finally { r.push('in');}"
              ""
              "            } catch (b) { return b; }"
              "            finally { r.push('out'); }"
              "};"
              "function g(n) { var r = []; return [f(n, r), r]}"
              "njs.dump([g(0), g(1), g(2)])"),
      njs_str("[[undefined,['out']],['a',['out']],[42,['in','out']]]") },