]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
tftp: Normalize slashes in TFTP paths
authorLenny Szubowicz <lszubowi@redhat.com>
Thu, 31 Oct 2019 10:33:39 +0000 (11:33 +0100)
committerDaniel Kiper <daniel.kiper@oracle.com>
Fri, 6 Dec 2019 19:26:36 +0000 (20:26 +0100)
Some TFTP servers do not handle multiple consecutive slashes correctly.
This patch avoids sending TFTP requests with non-normalized paths.

Signed-off-by: Lenny Szubowicz <lszubowi@redhat.com>
Signed-off-by: Mark Salter <msalter@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/net/tftp.c

index 7d90bf66e7637cdbc385f19cf7b69780b0455631..b26d2ed7aa24b7fce6cd93a8f1820e1e2ebec148 100644 (file)
@@ -300,6 +300,26 @@ destroy_pq (tftp_data_t data)
   grub_priority_queue_destroy (data->pq);
 }
 
+/*
+ * Create a normalized copy of the filename. Compress any string of consecutive
+ * forward slashes to a single forward slash.
+ */
+static void
+grub_normalize_filename (char *normalized, const char *filename)
+{
+  char *dest = normalized;
+  const char *src = filename;
+
+  while (*src != '\0')
+    {
+      if (src[0] == '/' && src[1] == '/')
+        src++;
+      else
+        *dest++ = *src++;
+    }
+  *dest = '\0';
+}
+
 static grub_err_t
 tftp_open (struct grub_file *file, const char *filename)
 {
@@ -337,9 +357,14 @@ tftp_open (struct grub_file *file, const char *filename)
   rrqlen = 0;
 
   tftph->opcode = grub_cpu_to_be16_compile_time (TFTP_RRQ);
-  grub_strcpy (rrq, filename);
-  rrqlen += grub_strlen (filename) + 1;
-  rrq += grub_strlen (filename) + 1;
+
+  /*
+   * Copy and normalize the filename to work-around issues on some TFTP
+   * servers when file names are being matched for remapping.
+   */
+  grub_normalize_filename (rrq, filename);
+  rrqlen += grub_strlen (rrq) + 1;
+  rrq += grub_strlen (rrq) + 1;
 
   grub_strcpy (rrq, "octet");
   rrqlen += grub_strlen ("octet") + 1;