15a2072
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a2072
From: Lenny Szubowicz <lszubowi@redhat.com>
15a2072
Date: Mon, 29 Aug 2016 11:04:48 -0400
15a2072
Subject: [PATCH] Normalize slashes in tftp paths.
15a2072
15a2072
Some tftp servers do not handle multiple consecutive slashes correctly;
15a2072
this patch avoids sending tftp requests with non-normalized paths.
15a2072
7e98da0
Signed-off-by: Lenny Szubowicz <lszubowi@redhat.com>
7e98da0
[msalter: fix malformed tftp packets]
7e98da0
Signed-off-by: Mark Salter <msalter@redhat.com>
15a2072
---
7e98da0
 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
7e98da0
 1 file changed, 25 insertions(+), 3 deletions(-)
15a2072
15a2072
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
7e98da0
index 1157524fc50..dcd82494309 100644
15a2072
--- a/grub-core/net/tftp.c
15a2072
+++ b/grub-core/net/tftp.c
15a2072
@@ -300,6 +300,25 @@ destroy_pq (tftp_data_t data)
15a2072
   grub_priority_queue_destroy (data->pq);
15a2072
 }
15a2072
 
15a2072
+/* Create a normalized copy of the filename.
15a2072
+   Compress any string of consecutive forward slashes to a single forward
15a2072
+   slash. */
15a2072
+static void
15a2072
+grub_normalize_filename (char *normalized, const char *filename)
15a2072
+{
15a2072
+  char *dest = normalized;
15a2072
+  char *src = filename;
15a2072
+
15a2072
+  while (*src != '\0')
15a2072
+    {
15a2072
+      if (src[0] == '/' && src[1] == '/')
15a2072
+	src++;
15a2072
+      else
15a2072
+	*dest++ = *src++;
15a2072
+    }
15a2072
+  *dest = '\0';
15a2072
+}
15a2072
+
15a2072
 static grub_err_t
15a2072
 tftp_open (struct grub_file *file, const char *filename)
15a2072
 {
7e98da0
@@ -337,9 +356,12 @@ tftp_open (struct grub_file *file, const char *filename)
15a2072
   rrqlen = 0;
15a2072
 
15a2072
   tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_RRQ);
15a2072
-  grub_strcpy (rrq, filename);
7e98da0
-  rrqlen += grub_strlen (filename) + 1;
7e98da0
-  rrq += grub_strlen (filename) + 1;
15a2072
+
15a2072
+  /* Copy and normalize the filename to work-around issues on some tftp
15a2072
+     servers when file names are being matched for remapping. */
15a2072
+  grub_normalize_filename (rrq, filename);
7e98da0
+  rrqlen += grub_strlen (rrq) + 1;
7e98da0
+  rrq += grub_strlen (rrq) + 1;
15a2072
 
7e98da0
   grub_strcpy (rrq, "octet");
7e98da0
   rrqlen += grub_strlen ("octet") + 1;