0027-net-tftp-Fix-stack-buffer-overflow-in-tftp_open.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. From 8284fca0f096d01f566eadfdc790232df9f2934e Mon Sep 17 00:00:00 2001
  2. From: B Horn <b@horn.uk>
  3. Date: Thu, 18 Apr 2024 17:32:34 +0100
  4. Subject: [PATCH] net/tftp: Fix stack buffer overflow in tftp_open()
  5. An overly long filename can be passed to tftp_open() which would cause
  6. grub_normalize_filename() to write out of bounds.
  7. Fixed by adding an extra argument to grub_normalize_filename() for the
  8. space available, making it act closer to a strlcpy(). As several fixed
  9. strings are strcpy()'d after into the same buffer, their total length is
  10. checked to see if they exceed the remaining space in the buffer. If so,
  11. return an error.
  12. On the occasion simplify code a bit by removing unneeded rrqlen zeroing.
  13. Reported-by: B Horn <b@horn.uk>
  14. Signed-off-by: B Horn <b@horn.uk>
  15. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  16. Upstream: 0707accab1b9be5d3645d4700dde3f99209f9367
  17. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  18. ---
  19. grub-core/net/tftp.c | 38 ++++++++++++++++++++++++--------------
  20. 1 file changed, 24 insertions(+), 14 deletions(-)
  21. diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
  22. index 409b1d09b..336b78691 100644
  23. --- a/grub-core/net/tftp.c
  24. +++ b/grub-core/net/tftp.c
  25. @@ -266,17 +266,19 @@ tftp_receive (grub_net_udp_socket_t sock __attribute__ ((unused)),
  26. * forward slashes to a single forward slash.
  27. */
  28. static void
  29. -grub_normalize_filename (char *normalized, const char *filename)
  30. +grub_normalize_filename (char *normalized, const char *filename, int c)
  31. {
  32. char *dest = normalized;
  33. const char *src = filename;
  34. - while (*src != '\0')
  35. + while (*src != '\0' && c > 0)
  36. {
  37. if (src[0] == '/' && src[1] == '/')
  38. src++;
  39. - else
  40. + else {
  41. + c--;
  42. *dest++ = *src++;
  43. + }
  44. }
  45. *dest = '\0';
  46. }
  47. @@ -287,7 +289,7 @@ tftp_open (struct grub_file *file, const char *filename)
  48. struct tftphdr *tftph;
  49. char *rrq;
  50. int i;
  51. - int rrqlen;
  52. + int rrqlen, rrqsize;
  53. int hdrlen;
  54. grub_uint8_t open_data[1500];
  55. struct grub_net_buff nb;
  56. @@ -315,37 +317,45 @@ tftp_open (struct grub_file *file, const char *filename)
  57. tftph = (struct tftphdr *) nb.data;
  58. - rrq = (char *) tftph->u.rrq;
  59. - rrqlen = 0;
  60. -
  61. tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_RRQ);
  62. + rrq = (char *) tftph->u.rrq;
  63. + rrqsize = sizeof (tftph->u.rrq);
  64. +
  65. /*
  66. * Copy and normalize the filename to work-around issues on some TFTP
  67. * servers when file names are being matched for remapping.
  68. */
  69. - grub_normalize_filename (rrq, filename);
  70. - rrqlen += grub_strlen (rrq) + 1;
  71. + grub_normalize_filename (rrq, filename, rrqsize);
  72. +
  73. + rrqlen = grub_strlen (rrq) + 1;
  74. rrq += grub_strlen (rrq) + 1;
  75. - grub_strcpy (rrq, "octet");
  76. + /* Verify there is enough space for the remaining components. */
  77. rrqlen += grub_strlen ("octet") + 1;
  78. + rrqlen += grub_strlen ("blksize") + 1;
  79. + rrqlen += grub_strlen ("1024") + 1;
  80. + rrqlen += grub_strlen ("tsize") + 1;
  81. + rrqlen += grub_strlen ("0") + 1;
  82. +
  83. + if (rrqlen >= rrqsize) {
  84. + grub_free (data);
  85. + return grub_error (GRUB_ERR_BAD_FILENAME, N_("filename too long"));
  86. + }
  87. +
  88. + grub_strcpy (rrq, "octet");
  89. rrq += grub_strlen ("octet") + 1;
  90. grub_strcpy (rrq, "blksize");
  91. - rrqlen += grub_strlen ("blksize") + 1;
  92. rrq += grub_strlen ("blksize") + 1;
  93. grub_strcpy (rrq, "1024");
  94. - rrqlen += grub_strlen ("1024") + 1;
  95. rrq += grub_strlen ("1024") + 1;
  96. grub_strcpy (rrq, "tsize");
  97. - rrqlen += grub_strlen ("tsize") + 1;
  98. rrq += grub_strlen ("tsize") + 1;
  99. grub_strcpy (rrq, "0");
  100. - rrqlen += grub_strlen ("0") + 1;
  101. rrq += grub_strlen ("0") + 1;
  102. hdrlen = sizeof (tftph->opcode) + rrqlen;
  103. --
  104. 2.50.1