Skip to contentSkip to conversation

ofdisk: allocate space for vscsi table

PATCH In review Paulo Flabiano Smorigo 3 participants 7 messages Nov 9, 2015 – Nov 10, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ofdisk: allocate space for vscsi table

The table must be allocated in order to avoid memory leak. Also added some vscsi paths for illustration purpose.

---
 grub-core/disk/ieee1275/ofdisk.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
grub-core/disk/ieee1275/ofdisk.c +25 -5
index afec9ba..4ea6089 100644
@@ -211,6 +211,11 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
 {
   if (grub_strcmp (alias->type, "vscsi") == 0)
     {
+      /* Example of vscsi paths:
+       *  /vdevice/v-scsi@30000002/disk@8100000000000000
+       *  /vdevice/v-scsi@30000002/disk@8200000000000000
+       *  /vdevice/v-scsi@30000002/disk@8400000000000000  */
+
       static grub_ieee1275_ihandle_t ihandle;
       struct set_color_args
       {
@@ -222,27 +227,41 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
 	grub_ieee1275_cell_t table;
       }
       args;
-      char *buf, *bufptr;
+      char *buf, *bufptr, *table;
       unsigned i;
+      grub_uint32_t table_size;
 
       if (grub_ieee1275_open (alias->path, &ihandle))
 	return;
-    
+
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;
+      table = grub_malloc (table_size);
+
+      if (!table)
+	return;
+
       INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 3);
       args.method = (grub_ieee1275_cell_t) "vscsi-report-luns";
       args.ihandle = ihandle;
-      args.table = 0;
+      args.table = (grub_ieee1275_cell_t) table;
       args.nentries = 0;
 
       if (IEEE1275_CALL_ENTRY_FN (&args) == -1 || args.catch_result)
 	{
 	  grub_ieee1275_close (ihandle);
-	  return;
+          grub_free (table);
+          return;
 	}
 
       buf = grub_malloc (grub_strlen (alias->path) + 32);
       if (!buf)
-	return;
+        {
+          grub_ieee1275_close (ihandle);
+          grub_free (table);
+          return;
+        }
+
       bufptr = grub_stpcpy (buf, alias->path);
 
       for (i = 0; i < args.nentries; i++)
@@ -257,6 +276,7 @@ dev_iterate (const struct grub_ieee1275_devalias *alias)
 	    }
 	}
       grub_ieee1275_close (ihandle);
+      grub_free (table);
       grub_free (buf);
       return;
     }
Signature · 1 line
2.1.0
#1 lore raw

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit? You don't even pass this limit to the function in any way. You basically get a buffer overflow. Should we perhaps pass the limit in nentries? Or do something similar?

#2 lore raw
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit? You don't even pass this limit to the function in any way. You basically get a buffer overflow. Should we perhaps pass the limit in nentries? Or do something similar?

For sas there is an input attribute called max that I use as an upper limit. AFAIK, vscsi method doesn't have it. nentries is a output attribute.

I'll investigate it.

Quoted 7 lines

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

#3 lore raw

Le 9 nov. 2015 1:00 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 11 lines
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit? You don't even pass this limit to the function in any way. You basically get a buffer overflow. Should we perhaps pass the limit in nentries? Or do something similar?

Quoted 6 lines

For sas there is an input attribute called max that I use as an upper limit. AFAIK, vscsi method doesn't have it. nentries is a output attribute.

I'll investigate it.

Can't we just properly free returned memory? Or perhaps even just tolerate the leak and only ensure that it happens only once per controller per GRUB run?

Quoted 12 lines
Quoted 7 lines

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

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

#4 lore raw
Quoted 18 lines · Vladimir 'phcoder' Serbinenko

Le 9 nov. 2015 1:00 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 15 lines
Quoted 7 lines · Vladimir 'φ-coder/phcoder' Serbinenko

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit? You don't even pass this limit to the function in any way. You basically get a buffer overflow. Should we perhaps pass the limit in nentries? Or do something similar?

For sas there is an input attribute called max that I use as an upper limit. AFAIK, vscsi method doesn't have it. nentries is a output attribute.

Quoted 5 lines
Quoted 2 lines

I'll investigate it.

Can't we just properly free returned memory? Or perhaps even just tolerate the leak and only ensure that it happens only once per controller per GRUB run?

Talked to the Open firmware team and we don't need to allocate the space. The vscsi implementation takes care of all of the memory management. The result of the call stays in a reserved memory and is never freed.

For vscsi-report-luns method there are no input values.

So, this patch isn't necessary. Should I put this info as a comment in the code?

Quoted 19 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines

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

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

#5 lore raw

Le 9 nov. 2015 5:34 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 7 lines
Quoted 4 lines · Vladimir 'phcoder' Serbinenko

Le 9 nov. 2015 1:00 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines · Vladimir 'φ-coder/phcoder' Serbinenko

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit?

You

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

don't even pass this limit to the function in any way. You basically

get

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

a buffer overflow. Should we perhaps pass the limit in nentries? Or

do

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 1 line

something similar?

For sas there is an input attribute called max that I use as an upper limit. AFAIK, vscsi method doesn't have it. nentries is a output attribute.

I'll investigate it.

Can't we just properly free returned memory? Or perhaps even just

tolerate

Quoted 1 line
Quoted 1 line

the leak and only ensure that it happens only once per controller per

GRUB

Quoted 10 lines
Quoted 1 line

run?

Talked to the Open firmware team and we don't need to allocate the space. The vscsi implementation takes care of all of the memory management. The result of the call stays in a reserved memory and is never freed.

For vscsi-report-luns method there are no input values.

So, this patch isn't necessary. Should I put this info as a comment in

the code?

Quoted 1 line

Sure, add this as a comment

Quoted 24 lines
Quoted 14 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines

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

Quoted 5 lines

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

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

#6 lore raw
Quoted 40 lines · Vladimir 'phcoder' Serbinenko

Le 9 nov. 2015 5:34 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 37 lines
Quoted 21 lines · Vladimir 'phcoder' Serbinenko

Le 9 nov. 2015 1:00 PM, "Paulo Flabiano Smorigo" <[email protected]> a écrit :

Quoted 18 lines
Quoted 10 lines · Vladimir 'φ-coder/phcoder' Serbinenko

grub-core/disk/ieee1275/ofdisk.c dev_iterate (const struct grub_ieee1275_devalias *alias) · line 238

Quoted 2 lines · Paulo Flabiano Smorigo
grub-core/disk/ieee1275/ofdisk.c +2 -0
+      /* 64 entries should be enough */
+      table_size = sizeof (grub_uint64_t) * 64;

Can we do something better than assuming a particular upper limit? You
don't even pass this limit to the function in any way. You basically get
a buffer overflow. Should we perhaps pass the limit in nentries? Or do
something similar?

For sas there is an input attribute called max that I use as an upper limit. AFAIK, vscsi method doesn't have it. nentries is a output attribute.

Quoted 7 lines
Quoted 2 lines

I'll investigate it.

Can't we just properly free returned memory? Or perhaps even just tolerate
the leak and only ensure that it happens only once per controller per GRUB
run?

Talked to the Open firmware team and we don't need to allocate the space. The vscsi implementation takes care of all of the memory management. The result of the call stays in a reserved memory and is never freed.

Quoted 6 lines
Quoted 5 lines

For vscsi-report-luns method there are no input values.

So, this patch isn't necessary. Should I put this info as a comment in the code?

Sure, add this as a comment

Done. Commit ID: a50dbb743e53e5e643f27daa84261e6e9d7747c1

Quoted 31 lines
Quoted 20 lines
Quoted 14 lines
Quoted 8 lines
Quoted 7 lines

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

Quoted 4 lines

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

Quoted 5 lines

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

Quoted 4 lines

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

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