Skip to contentSkip to conversation

tftp: Normalize slashes in tftp paths

PATCH In review Javier Martinez Canillas 3 participants 5 messages Oct 31, 2019 – Dec 13, 2019 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tftp: Normalize slashes in tftp paths

From: Lenny Szubowicz <[email protected]>

Some tftp servers do not handle multiple consecutive slashes correctly; this patch avoids sending tftp requests with non-normalized paths.

---

 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
net/tftp.c +25 -3
index 7d90bf66e76..6dbb9cdbb7a 100644
@@ -300,6 +300,25 @@ 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 +356,12 @@ 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;
Signature · 1 line
2.21.0
#1 lore raw

Having // at the beginning of the path may have special meaning according to posix. I don't know if it applies in particular case and if the special meaning is useful for grub to begin with, just something to check

Quoted 69 lines · On Thu, 31 Oct 2019, 11:37 Javier Martinez Canillas, <[email protected]> wrote:

From: Lenny Szubowicz <[email protected]>

Some tftp servers do not handle multiple consecutive slashes correctly; this patch avoids sending tftp requests with non-normalized paths.

---

 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
net/tftp.c +25 -3
index 7d90bf66e76..6dbb9cdbb7a 100644
@@ -300,6 +300,25 @@ 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 +356,12 @@ 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;

--
2.21.0

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by
#2 lore raw
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

Having // at the beginning of the path may have special meaning according to posix. I don't know if it applies in particular case and if the special meaning is useful for grub to begin with, just something to check

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13

"A pathname consisting of a single <slash> shall resolve to the root directory of the process. A null pathname shall not be successfully resolved. If a pathname begins with two successive <slash> characters, the first component following the leading <slash> characters may be interpreted in an implementation-defined manner, although more than two leading <slash> characters shall be treated as a single <slash> character."

I do not see any reason to have "implementation-defined manner" for "//" in the GRUB. So, IMO we should replace any set of consecutive slashes with one slash.

Quoted 24 lines
Quoted 21 lines · On Thu, 31 Oct 2019, 11:37 Javier Martinez Canillas, <[email protected]> wrote:

From: Lenny Szubowicz <[email protected]>

Some tftp servers do not handle multiple consecutive slashes correctly; this patch avoids sending tftp requests with non-normalized paths.

---

 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
net/tftp.c +0 -0
index 7d90bf66e76..6dbb9cdbb7a 100644
@@ -300,6 +300,25 @@ destroy_pq (tftp_data_t data)
   grub_priority_queue_destroy (data->pq);
 }
 
Quoted 3 lines

net/tftp.c destroy_pq (tftp_data_t data) · line 305

Quoted 3 lines
net/tftp.c +3 -0
+/* Create a normalized copy of the filename.
+   Compress any string of consecutive forward slashes to a single forward
+   slash. */

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

Please fix this comment...

Quoted 23 lines
Quoted 23 lines
net/tftp.c +16 -0
+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 +356,12 @@ tftp_open (struct grub_file *file, const char
 *filename)
   rrqlen = 0;
 
Quoted 7 lines

net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 361

Quoted 7 lines
net/tftp.c +3 -3
   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. */

Ditto.

Quoted 3 lines

net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 364

Quoted 3 lines
net/tftp.c +3 -0
+  grub_normalize_filename (rrq, filename);
+  rrqlen += grub_strlen (rrq) + 1;
+  rrq += grub_strlen (rrq) + 1;

If you fix these minor issues feel free to add

You can also add above excerpt from POSIX spec to the commit message. This way everybody will know that this is our deliberate decision.

Daniel

Reviewed-by on v1 1/1
#3 lore raw
In reply to Javier Martinez Canillas
Quoted 8 lines · Javier Martinez Canillas

From: Lenny Szubowicz <[email protected]>

Some tftp servers do not handle multiple consecutive slashes correctly; this patch avoids sending tftp requests with non-normalized paths.

Except some nitpicks which I fix before committing...

Quoted 10 lines

---

 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
net/tftp.c +0 -0
index 7d90bf66e76..6dbb9cdbb7a 100644

net/tftp.c destroy_pq (tftp_data_t data) · line 305

Quoted 6 lines
net/tftp.c +3 -0
@@ -300,6 +300,25 @@ 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. */

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

Quoted 20 lines
net/tftp.c +16 -0
+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)
 {

net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 361

Quoted 9 lines
net/tftp.c +3 -3
@@ -337,9 +356,12 @@ 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. */

Ditto.

net/tftp.c tftp_open (struct grub_file *file, const char *filename) · line 367

Quoted 6 lines
net/tftp.c +3 -0
+  grub_normalize_filename (rrq, filename);
+  rrqlen += grub_strlen (rrq) + 1;
+  rrq += grub_strlen (rrq) + 1;
 
   grub_strcpy (rrq, "octet");
   rrqlen += grub_strlen ("octet") + 1;

Daniel

#4 lore raw

Hello Daniel,

Quoted 6 lines · Daniel Kiper
Quoted 5 lines · Javier Martinez Canillas

From: Lenny Szubowicz <[email protected]>

Some tftp servers do not handle multiple consecutive slashes correctly; this patch avoids sending tftp requests with non-normalized paths.

Quoted 8 lines
Quoted 3 lines

Except some nitpicks which I fix before committing...

Thanks a lot for fixing the issues and taking the patch.

Quoted 13 lines
Quoted 13 lines

---

 grub-core/net/tftp.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
net/tftp.c +0 -0
index 7d90bf66e76..6dbb9cdbb7a 100644
@@ -300,6 +300,25 @@ destroy_pq (tftp_data_t data)
   grub_priority_queue_destroy (data->pq);
 }
 
Quoted 6 lines

net/tftp.c destroy_pq (tftp_data_t data) · line 305

Quoted 3 lines
net/tftp.c +3 -0
+/* Create a normalized copy of the filename.
+   Compress any string of consecutive forward slashes to a single forward
+   slash. */

https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments

Sorry, I'll make sure that comments are correct the next time.

Best regards,

Signature · 3 lines
Javier Martinez Canillas
Software Engineer - Desktop Hardware Enablement
Red Hat