Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aaron Miller <aaronmiller@fb.com>
Date: Fri, 29 Jul 2016 17:41:38 +0800
Subject: [PATCH] net: read bracketed ipv6 addrs and port numbers (pjones
 fixup)

Various bug fixes related to previous patch.

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/net/http.c |  6 ++++--
 grub-core/net/net.c  | 31 +++++++++++++------------------
 grub-core/net/tftp.c | 19 ++++++++++++++++---
 3 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/grub-core/net/http.c b/grub-core/net/http.c
index 6d99051d345..2b46e4672fc 100644
--- a/grub-core/net/http.c
+++ b/grub-core/net/http.c
@@ -289,7 +289,9 @@ http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)),
 	  nb2 = grub_netbuff_alloc (data->chunk_rem);
 	  if (!nb2)
 	    return grub_errno;
-	  grub_netbuff_put (nb2, data->chunk_rem);
+	  err = grub_netbuff_put (nb2, data->chunk_rem);
+	  if (err)
+	    return grub_errno;
 	  grub_memcpy (nb2->data, nb->data, data->chunk_rem);
 	  if (file->device->net->packs.count >= 20)
 	    {
@@ -406,7 +408,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
 		data->filename, server, port ? port : HTTP_PORT);
   data->sock = grub_net_tcp_open (server,
 				  port ? port : HTTP_PORT, http_receive,
-				  http_err, http_err,
+				  http_err, NULL,
 				  file);
   if (!data->sock)
     {
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
index b062f6fd0cd..6b4b10ba444 100644
--- a/grub-core/net/net.c
+++ b/grub-core/net/net.c
@@ -464,10 +464,11 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)
   int word, quaddot = -1;
   int bracketed = 0;
 
-  if (ptr[0] == '[') {
-    bracketed = 1;
-    ptr++;
-  }
+  if (ptr[0] == '[')
+    {
+      bracketed = 1;
+      ptr++;
+    }
 
   if (ptr[0] == ':' && ptr[1] != ':')
     return 0;
@@ -506,9 +507,8 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)
       grub_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
     }
   grub_memcpy (ip, newip, 16);
-  if (bracketed && *ptr == ']') {
+  if (bracketed && *ptr == ']')
     ptr++;
-  }
   if (rest)
     *rest = ptr;
   return 1;
@@ -1401,7 +1401,7 @@ grub_net_open_real (const char *name)
   char* port_start;
   /* ipv6 or port specified? */
   if ((port_start = grub_strchr (server, ':')))
-  {
+    {
       char* ipv6_begin;
       if((ipv6_begin = grub_strchr (server, '[')))
 	{
@@ -1473,19 +1473,13 @@ grub_net_open_real (const char *name)
 	  {
 	    grub_net_t ret = grub_zalloc (sizeof (*ret));
 	    if (!ret)
-	      return NULL;
-	    ret->protocol = proto;
-	    if (server)
 	      {
-		ret->server = grub_strdup (server);
-		if (!ret->server)
-		  {
-		    grub_free (ret);
-		    return NULL;
-		  }
+		grub_free (host);
+		return NULL;
 	      }
-	    else
-	      ret->server = NULL;
+	    ret->protocol = proto;
+	    ret->port = port;
+	    ret->server = host;
 	    ret->fs = &grub_net_fs;
 	    ret->offset = 0;
 	    ret->eof = 0;
@@ -1516,6 +1510,7 @@ grub_net_open_real (const char *name)
   grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
 	      name);
 
+  grub_free (host);
   return NULL;
 }
 
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
index d13a6c8ed2d..17583cba380 100644
--- a/grub-core/net/tftp.c
+++ b/grub-core/net/tftp.c
@@ -346,7 +346,10 @@ tftp_open (struct grub_file *file, const char *filename)
   grub_netbuff_reserve (&nb, 1500);
   err = grub_netbuff_push (&nb, sizeof (*tftph));
   if (err)
-    return err;
+    {
+      grub_free (data);
+      return err;
+    }
 
   tftph = (struct tftphdr *) nb.data;
 
@@ -384,14 +387,20 @@ tftp_open (struct grub_file *file, const char *filename)
 
   err = grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen));
   if (err)
-    return err;
+    {
+      grub_free (data);
+      return err;
+    }
 
   file->not_easily_seekable = 1;
   file->data = data;
 
   data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
   if (!data->pq)
-    return grub_errno;
+    {
+      grub_free (data);
+      return grub_errno;
+    }
 
   grub_dprintf("tftp", "resolving address for %s\n", file->device->net->server);
   err = grub_net_resolve_address (file->device->net->server, &addr);
@@ -402,6 +411,7 @@ tftp_open (struct grub_file *file, const char *filename)
 		    (unsigned long long)data->file_size,
 		    (unsigned long long)data->block_size);
       destroy_pq (data);
+      grub_free (data);
       return err;
     }
 
@@ -413,6 +423,7 @@ tftp_open (struct grub_file *file, const char *filename)
     {
       grub_dprintf("tftp", "connection failed\n");
       destroy_pq (data);
+      grub_free (data);
       return grub_errno;
     }
 
@@ -426,6 +437,7 @@ tftp_open (struct grub_file *file, const char *filename)
 	{
 	  grub_net_udp_close (data->sock);
 	  destroy_pq (data);
+	  grub_free (data);
 	  return err;
 	}
       grub_net_poll_cards (GRUB_NET_INTERVAL + (i * GRUB_NET_INTERVAL_ADDITION),
@@ -442,6 +454,7 @@ tftp_open (struct grub_file *file, const char *filename)
     {
       grub_net_udp_close (data->sock);
       destroy_pq (data);
+      grub_free (data);
       return grub_errno;
     }