070-extensions-string-Review-parse_string-function.patch 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. From da5b32fb4656ab69fe1156eb7e36c7c961839e8a Mon Sep 17 00:00:00 2001
  2. From: Phil Sutter <[email protected]>
  3. Date: Wed, 8 Jun 2022 13:45:13 +0200
  4. Subject: [PATCH] extensions: string: Review parse_string() function
  5. * Compare against sizeof(info->pattern) which is more clear than having
  6. to know that this buffer is of size XT_STRING_MAX_PATTERN_SIZE
  7. * Invert the check and error early to reduce indenting
  8. * Pass info->patlen to memcpy() to avoid reading past end of 's'
  9. Signed-off-by: Phil Sutter <[email protected]>
  10. ---
  11. extensions/libxt_string.c | 13 ++++++-------
  12. 1 file changed, 6 insertions(+), 7 deletions(-)
  13. --- a/extensions/libxt_string.c
  14. +++ b/extensions/libxt_string.c
  15. @@ -78,14 +78,13 @@ static void string_init(struct xt_entry_
  16. static void
  17. parse_string(const char *s, struct xt_string_info *info)
  18. -{
  19. +{
  20. /* xt_string does not need \0 at the end of the pattern */
  21. - if (strlen(s) <= XT_STRING_MAX_PATTERN_SIZE) {
  22. - memcpy(info->pattern, s, XT_STRING_MAX_PATTERN_SIZE);
  23. - info->patlen = strnlen(s, XT_STRING_MAX_PATTERN_SIZE);
  24. - return;
  25. - }
  26. - xtables_error(PARAMETER_PROBLEM, "STRING too long \"%s\"", s);
  27. + if (strlen(s) > sizeof(info->pattern))
  28. + xtables_error(PARAMETER_PROBLEM, "STRING too long \"%s\"", s);
  29. +
  30. + info->patlen = strnlen(s, sizeof(info->pattern));
  31. + memcpy(info->pattern, s, info->patlen);
  32. }
  33. static void