0146-kern-misc-Add-function-to-check-printf-format-agains.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. From 83603bea6ce8fdff5ab3fbc4c9e592a8c71a8706 Mon Sep 17 00:00:00 2001
  2. From: Thomas Frauendorfer | Miray Software <tf@miray.de>
  3. Date: Thu, 4 Feb 2021 19:02:33 +0100
  4. Subject: [PATCH] kern/misc: Add function to check printf() format against
  5. expected format
  6. The grub_printf_fmt_check() function parses the arguments of an untrusted
  7. printf() format and an expected printf() format and then compares the
  8. arguments counts and arguments types. The arguments count in the untrusted
  9. format string must be less or equal to the arguments count in the expected
  10. format string and both arguments types must match.
  11. To do this the parse_printf_arg_fmt() helper function is extended in the
  12. following way:
  13. 1. Add a return value to report errors to the grub_printf_fmt_check().
  14. 2. Add the fmt_check argument to enable stricter format verification:
  15. - the function expects that arguments definitions are always
  16. terminated by a supported conversion specifier.
  17. - positional parameters, "$", are not allowed, as they cannot be
  18. validated correctly with the current implementation. For example
  19. "%s%1$d" would assign the first args entry twice while leaving the
  20. second one unchanged.
  21. - Return an error if preallocated space in args is too small and
  22. allocation fails for the needed size. The grub_printf_fmt_check()
  23. should verify all arguments. So, if validation is not possible for
  24. any reason it should return an error.
  25. This also adds a case entry to handle "%%", which is the escape
  26. sequence to print "%" character.
  27. 3. Add the max_args argument to check for the maximum allowed arguments
  28. count in a printf() string. This should be set to the arguments count
  29. of the expected format. Then the parse_printf_arg_fmt() function will
  30. return an error if the arguments count is exceeded.
  31. The two additional arguments allow us to use parse_printf_arg_fmt() in
  32. printf() and grub_printf_fmt_check() calls.
  33. When parse_printf_arg_fmt() is used by grub_printf_fmt_check() the
  34. function parse user provided untrusted format string too. So, in
  35. that case it is better to be too strict than too lenient.
  36. Signed-off-by: Thomas Frauendorfer | Miray Software <tf@miray.de>
  37. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  38. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  39. ---
  40. grub-core/kern/misc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++---
  41. include/grub/misc.h | 16 ++++++++++
  42. 2 files changed, 94 insertions(+), 4 deletions(-)
  43. diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
  44. index 22417f7..90317b6 100644
  45. --- a/grub-core/kern/misc.c
  46. +++ b/grub-core/kern/misc.c
  47. @@ -632,8 +632,26 @@ grub_lltoa (char *str, int c, unsigned long long n)
  48. return p;
  49. }
  50. -static void
  51. -parse_printf_arg_fmt (const char *fmt0, struct printf_args *args)
  52. +/*
  53. + * Parse printf() fmt0 string into args arguments.
  54. + *
  55. + * The parsed arguments are either used by a printf() function to format the fmt0
  56. + * string or they are used to compare a format string from an untrusted source
  57. + * against a format string with expected arguments.
  58. + *
  59. + * When the fmt_check is set to !0, e.g. 1, then this function is executed in
  60. + * printf() format check mode. This enforces stricter rules for parsing the
  61. + * fmt0 to limit exposure to possible errors in printf() handling. It also
  62. + * disables positional parameters, "$", because some formats, e.g "%s%1$d",
  63. + * cannot be validated with the current implementation.
  64. + *
  65. + * The max_args allows to set a maximum number of accepted arguments. If the fmt0
  66. + * string defines more arguments than the max_args then the parse_printf_arg_fmt()
  67. + * function returns an error. This is currently used for format check only.
  68. + */
  69. +static grub_err_t
  70. +parse_printf_arg_fmt (const char *fmt0, struct printf_args *args,
  71. + int fmt_check, grub_size_t max_args)
  72. {
  73. const char *fmt;
  74. char c;
  75. @@ -660,7 +678,12 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args)
  76. fmt++;
  77. if (*fmt == '$')
  78. - fmt++;
  79. + {
  80. + if (fmt_check)
  81. + return grub_error (GRUB_ERR_BAD_ARGUMENT,
  82. + "positional arguments are not supported");
  83. + fmt++;
  84. + }
  85. if (*fmt =='-')
  86. fmt++;
  87. @@ -691,9 +714,19 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args)
  88. case 's':
  89. args->count++;
  90. break;
  91. + case '%':
  92. + /* "%%" is the escape sequence to output "%". */
  93. + break;
  94. + default:
  95. + if (fmt_check)
  96. + return grub_error (GRUB_ERR_BAD_ARGUMENT, "unexpected format");
  97. + break;
  98. }
  99. }
  100. + if (fmt_check && args->count > max_args)
  101. + return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many arguments");
  102. +
  103. if (args->count <= ARRAY_SIZE (args->prealloc))
  104. args->ptr = args->prealloc;
  105. else
  106. @@ -701,6 +734,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args)
  107. args->ptr = grub_calloc (args->count, sizeof (args->ptr[0]));
  108. if (!args->ptr)
  109. {
  110. + if (fmt_check)
  111. + return grub_errno;
  112. +
  113. grub_errno = GRUB_ERR_NONE;
  114. args->ptr = args->prealloc;
  115. args->count = ARRAY_SIZE (args->prealloc);
  116. @@ -791,6 +827,8 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args)
  117. break;
  118. }
  119. }
  120. +
  121. + return GRUB_ERR_NONE;
  122. }
  123. static void
  124. @@ -798,7 +836,7 @@ parse_printf_args (const char *fmt0, struct printf_args *args, va_list args_in)
  125. {
  126. grub_size_t n;
  127. - parse_printf_arg_fmt (fmt0, args);
  128. + parse_printf_arg_fmt (fmt0, args, 0, 0);
  129. for (n = 0; n < args->count; n++)
  130. switch (args->ptr[n].type)
  131. @@ -1105,6 +1143,42 @@ grub_xasprintf (const char *fmt, ...)
  132. return ret;
  133. }
  134. +grub_err_t
  135. +grub_printf_fmt_check (const char *fmt, const char *fmt_expected)
  136. +{
  137. + struct printf_args args_expected, args_fmt;
  138. + grub_err_t ret;
  139. + grub_size_t n;
  140. +
  141. + if (fmt == NULL || fmt_expected == NULL)
  142. + return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid format");
  143. +
  144. + ret = parse_printf_arg_fmt (fmt_expected, &args_expected, 1, GRUB_SIZE_MAX);
  145. + if (ret != GRUB_ERR_NONE)
  146. + return ret;
  147. +
  148. + /* Limit parsing to the number of expected arguments. */
  149. + ret = parse_printf_arg_fmt (fmt, &args_fmt, 1, args_expected.count);
  150. + if (ret != GRUB_ERR_NONE)
  151. + {
  152. + free_printf_args (&args_expected);
  153. + return ret;
  154. + }
  155. +
  156. + for (n = 0; n < args_fmt.count; n++)
  157. + if (args_fmt.ptr[n].type != args_expected.ptr[n].type)
  158. + {
  159. + ret = grub_error (GRUB_ERR_BAD_ARGUMENT, "arguments types do not match");
  160. + break;
  161. + }
  162. +
  163. + free_printf_args (&args_expected);
  164. + free_printf_args (&args_fmt);
  165. +
  166. + return ret;
  167. +}
  168. +
  169. +
  170. /* Abort GRUB. This function does not return. */
  171. static void __attribute__ ((noreturn))
  172. grub_abort (void)
  173. diff --git a/include/grub/misc.h b/include/grub/misc.h
  174. index ee48eb7..d1c5709 100644
  175. --- a/include/grub/misc.h
  176. +++ b/include/grub/misc.h
  177. @@ -440,6 +440,22 @@ grub_error_load (const struct grub_error_saved *save)
  178. grub_errno = save->grub_errno;
  179. }
  180. +/*
  181. + * grub_printf_fmt_checks() a fmt string for printf() against an expected
  182. + * format. It is intended for cases where the fmt string could come from
  183. + * an outside source and cannot be trusted.
  184. + *
  185. + * While expected fmt accepts a printf() format string it should be kept
  186. + * as simple as possible. The printf() format strings with positional
  187. + * parameters are NOT accepted, neither for fmt nor for fmt_expected.
  188. + *
  189. + * The fmt is accepted if it has equal or less arguments than fmt_expected
  190. + * and if the type of all arguments match.
  191. + *
  192. + * Returns GRUB_ERR_NONE if fmt is acceptable.
  193. + */
  194. +grub_err_t EXPORT_FUNC (grub_printf_fmt_check) (const char *fmt, const char *fmt_expected);
  195. +
  196. #if BOOT_TIME_STATS
  197. struct grub_boot_time
  198. {
  199. --
  200. 2.14.2