0002-yylex-Make-lexer-fatal-errors-actually-be-fatal.patch 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. From a7ab0cc98fa89a3d5098c29cbe44bcd24b0a6454 Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Wed, 15 Apr 2020 15:45:02 -0400
  4. Subject: [PATCH] yylex: Make lexer fatal errors actually be fatal
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. When presented with a command that can't be tokenized to anything
  9. smaller than YYLMAX characters, the parser calls YY_FATAL_ERROR(errmsg),
  10. expecting that will stop further processing, as such:
  11. #define YY_DO_BEFORE_ACTION \
  12. yyg->yytext_ptr = yy_bp; \
  13. yyleng = (int) (yy_cp - yy_bp); \
  14. yyg->yy_hold_char = *yy_cp; \
  15. *yy_cp = '\0'; \
  16. if ( yyleng >= YYLMAX ) \
  17. YY_FATAL_ERROR( "token too large, exceeds YYLMAX" ); \
  18. yy_flex_strncpy( yytext, yyg->yytext_ptr, yyleng + 1 , yyscanner); \
  19. yyg->yy_c_buf_p = yy_cp;
  20. The code flex generates expects that YY_FATAL_ERROR() will either return
  21. for it or do some form of longjmp(), or handle the error in some way at
  22. least, and so the strncpy() call isn't in an "else" clause, and thus if
  23. YY_FATAL_ERROR() is *not* actually fatal, it does the call with the
  24. questionable limit, and predictable results ensue.
  25. Unfortunately, our implementation of YY_FATAL_ERROR() is:
  26. #define YY_FATAL_ERROR(msg) \
  27. do { \
  28. grub_printf (_("fatal error: %s\n"), _(msg)); \
  29. } while (0)
  30. The same pattern exists in yyless(), and similar problems exist in users
  31. of YY_INPUT(), several places in the main parsing loop,
  32. yy_get_next_buffer(), yy_load_buffer_state(), yyensure_buffer_stack,
  33. yy_scan_buffer(), etc.
  34. All of these callers expect YY_FATAL_ERROR() to actually be fatal, and
  35. the things they do if it returns after calling it are wildly unsafe.
  36. Fixes: CVE-2020-10713
  37. Signed-off-by: Peter Jones <pjones@redhat.com>
  38. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  39. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  40. ---
  41. grub-core/script/yylex.l | 4 ++--
  42. 1 file changed, 2 insertions(+), 2 deletions(-)
  43. diff --git a/grub-core/script/yylex.l b/grub-core/script/yylex.l
  44. index 7b44c37b7..b7203c823 100644
  45. --- a/grub-core/script/yylex.l
  46. +++ b/grub-core/script/yylex.l
  47. @@ -37,11 +37,11 @@
  48. /*
  49. * As we don't have access to yyscanner, we cannot do much except to
  50. - * print the fatal error.
  51. + * print the fatal error and exit.
  52. */
  53. #define YY_FATAL_ERROR(msg) \
  54. do { \
  55. - grub_printf (_("fatal error: %s\n"), _(msg)); \
  56. + grub_fatal (_("fatal error: %s\n"), _(msg));\
  57. } while (0)
  58. #define COPY(str, hint) \
  59. --
  60. 2.26.2