Skip to contentSkip to conversation

ofnet: Initialize structs in bootpath parser

PATCH Superseded Julian Andres Klode 3 participants 5 messages Aug 23, 2018 – Aug 29, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ofnet: Initialize structs in bootpath parser

Code later on checks if variables inside the struct are 0 to see if they have been set, like if there were addresses in the bootpath.

The variables were not initialized however, so the check might suceed with uninitialized data, and a new interface with random addresses has been added. This caused a weird bug in Ubuntu, because when booting from network, we now had two interfaces with the same name, and net_default_mac pointed to the random one.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1785859

---
 grub-core/net/drivers/ieee1275/ofnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
index 002446be1..00abc64bb 100644
@@ -153,8 +153,8 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
-  grub_net_link_level_address_t hw_addr;
+  grub_net_network_level_address_t client_addr = {}, gateway_addr = {}, subnet_mask = {};
+  grub_net_link_level_address_t hw_addr = {};
   grub_net_interface_flags_t flags = 0;
   struct grub_net_network_level_interface *inter = NULL;
   grub_uint16_t vlantag = 0;
Signature · 6 lines
2.17.1


-- 
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en
#1 lore raw

PATCH RESEND 1/1 ofnet: Initialize structs in bootpath parser

Code later on checks if variables inside the struct are 0 to see if they have been set, like if there were addresses in the bootpath.

The variables were not initialized however, so the check might suceed with uninitialized data, and a new interface with random addresses has been added. This caused a weird bug in Ubuntu, because when booting from network, we now had two interfaces with the same name, and net_default_mac pointed to the random one.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1785859

---
 grub-core/net/drivers/ieee1275/ofnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
index 002446be1..00abc64bb 100644
@@ -153,8 +153,8 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
-  grub_net_link_level_address_t hw_addr;
+  grub_net_network_level_address_t client_addr = {}, gateway_addr = {}, subnet_mask = {};
+  grub_net_link_level_address_t hw_addr = {};
   grub_net_interface_flags_t flags = 0;
   struct grub_net_network_level_interface *inter = NULL;
   grub_uint16_t vlantag = 0;
Signature · 1 line
2.17.1
#2 lore raw
Quoted 6 lines · Julian Andres Klode

Code later on checks if variables inside the struct are 0 to see if they have been set, like if there were addresses in the bootpath.

The variables were not initialized however, so the check might suceed with uninitialized data, and a new interface

succeed

Quoted 1 line

with random addresses has been added. This caused a weird

is added (I’d use present tense to describe a problem.) This causes

Quoted 2 lines

bug in Ubuntu, because when booting from network, we now had two interfaces with the same name, and net_default_mac

have

Quoted 1 line

pointed to the random one.

points

Quoted 11 lines

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1785859

---
 grub-core/net/drivers/ieee1275/ofnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 002446be1..00abc64bb 100644

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath, · line 160

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
@@ -153,8 +153,8 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
-  grub_net_link_level_address_t hw_addr;
+  grub_net_network_level_address_t client_addr = {}, gateway_addr = {}, subnet_mask = {};
+  grub_net_link_level_address_t hw_addr = {};
   grub_net_interface_flags_t flags = 0;
   struct grub_net_network_level_interface *inter = NULL;
   grub_uint16_t vlantag = 0;

The diff looks good.

Kind regards,

Paul

#3 lore raw
In reply to Julian Andres Klode

Actually I also have a similar patch posted one month ago for the very same problem.

https://lists.gnu.org/archive/html/grub-devel/2018-07/msg00103.html

But as long as either way will do, I don't mind which one is going to be reviewed and eventually merged. :)

Quoted 22 lines · Julian Andres Klode

Code later on checks if variables inside the struct are 0 to see if they have been set, like if there were addresses in the bootpath.

The variables were not initialized however, so the check might suceed with uninitialized data, and a new interface with random addresses has been added. This caused a weird bug in Ubuntu, because when booting from network, we now had two interfaces with the same name, and net_default_mac pointed to the random one.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1785859

---
 grub-core/net/drivers/ieee1275/ofnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 002446be1..00abc64bb 100644

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath, · line 157

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
@@ -153,8 +153,8 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
-  grub_net_link_level_address_t hw_addr;
+  grub_net_network_level_address_t client_addr = {}, gateway_addr = {}, subnet_mask = {};
+  grub_net_link_level_address_t hw_addr = {};

Well. I personally don't like the idea to use empty initializer list as some compiler would reject it since it is not defined in ISO C standard to be useful to initialize "rest" of the structure to zero, if only part of the initialzer is provided. The gcc accepts it but is an extenstion syntax anyway.

I think better to have at least one initialzer on the list, like {0} or similar, which is standard compliant and also more readable.

Thanks,
Michael

Quoted 11 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
   grub_net_interface_flags_t flags = 0;
   struct grub_net_network_level_interface *inter = NULL;
   grub_uint16_t vlantag = 0;
Signature · 7 lines
2.17.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw
Quoted 7 lines · Michael Chang

Actually I also have a similar patch posted one month ago for the very same problem.

https://lists.gnu.org/archive/html/grub-devel/2018-07/msg00103.html

But as long as either way will do, I don't mind which one is going to be reviewed and eventually merged. :)

I need to send the one with the fixed message. The problem I see with your approach is that some fields might still be uninitialized and maybe used later, hence I went with the full-on zeroes everywhere approach.

Quoted 28 lines · Julian Andres Klode
Quoted 26 lines

Code later on checks if variables inside the struct are 0 to see if they have been set, like if there were addresses in the bootpath.

The variables were not initialized however, so the check might suceed with uninitialized data, and a new interface with random addresses has been added. This caused a weird bug in Ubuntu, because when booting from network, we now had two interfaces with the same name, and net_default_mac pointed to the random one.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1785859

---
 grub-core/net/drivers/ieee1275/ofnet.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -1
index 002446be1..00abc64bb 100644
@@ -153,8 +153,8 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
   char *comma_char = 0;
   char *equal_char = 0;
   grub_size_t field_counter = 0;
-  grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath, · line 157

Quoted 3 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -1
-  grub_net_link_level_address_t hw_addr;
+  grub_net_network_level_address_t client_addr = {}, gateway_addr = {}, subnet_mask = {};
+  grub_net_link_level_address_t hw_addr = {};

Well. I personally don't like the idea to use empty initializer list as some compiler would reject it since it is not defined in ISO C standard to be useful to initialize "rest" of the structure to zero, if only part of the initialzer is provided. The gcc accepts it but is an extenstion syntax anyway.

grub requires gcc(-style) compilers, though, and makes use of GNU C throughout the code.

Quoted 3 lines

I think better to have at least one initialzer on the list, like {0} or similar, which is standard compliant and also more readable.

Then you have a problem if the structure layout changes, unless you use a named initializer.

Signature · 2 lines
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en