Re: [PATCH v3 3/4] cryptodisk: Move global variables into grub_cryptomount_args struct

2021-12-01 Thread Glenn Washburn
On Thu, 18 Nov 2021 15:06:56 +0100
Daniel Kiper  wrote:

> On Tue, Oct 12, 2021 at 06:26:28PM -0500, Glenn Washburn wrote:
> > Signed-off-by: Glenn Washburn 
> > ---
> >  grub-core/disk/cryptodisk.c | 26 +-
> >  grub-core/disk/geli.c   |  9 -
> >  grub-core/disk/luks.c   | 11 +--
> >  grub-core/disk/luks2.c  |  6 +++---
> >  include/grub/cryptodisk.h   | 10 --
> >  5 files changed, 29 insertions(+), 33 deletions(-)
> >
> > diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
> > index a5f7b860c..5b38606ed 100644
> > --- a/grub-core/disk/cryptodisk.c
> > +++ b/grub-core/disk/cryptodisk.c
> > @@ -984,9 +984,6 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
> >
> >  #endif
> >
> > -static int check_boot, have_it;
> > -static char *search_uuid;
> > -
> >  static void
> >  cryptodisk_close (grub_cryptodisk_t dev)
> >  {
> > @@ -1014,7 +1011,7 @@ grub_cryptodisk_scan_device_real (const char *name,
> >
> >FOR_CRYPTODISK_DEVS (cr)
> >{
> > -dev = cr->scan (source, search_uuid, check_boot);
> > +dev = cr->scan (source, cargs);
> >  if (grub_errno)
> >return grub_errno;
> >  if (!dev)
> > @@ -1049,7 +1046,7 @@ grub_cryptodisk_scan_device_real (const char *name,
> >
> >  grub_cryptodisk_insert (dev, name, source);
> >
> > -have_it = 1;
> > +cargs->found_uuid = 1;
> 
> Please say in the commit message you are changing variable/member name too.
> Or maybe it would be better if you do this in separate patch.

I'll see about moving the next patch such that this is not a concern.

> >  goto cleanup;
> >}
> > @@ -1091,7 +1088,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, 
> > const char *cheat)
> >
> >FOR_CRYPTODISK_DEVS (cr)
> >{
> > -dev = cr->scan (source, search_uuid, check_boot);
> > +dev = cr->scan (source, NULL);
> >  if (grub_errno)
> >return grub_errno;
> >  if (!dev)
> > @@ -1135,7 +1132,7 @@ grub_cryptodisk_scan_device (const char *name,
> >
> >if (err)
> >  grub_print_error ();
> > -  return have_it && search_uuid ? 1 : 0;
> > +  return (cargs->found_uuid && cargs->search_uuid) ? 1 : 0;
> 
> I think this should be enough:
>   return (cargs->found_uuid && cargs->search_uuid != NULL)
> 
> >  }
> >
> >  static grub_err_t
> > @@ -1153,7 +1150,6 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
> > argc, char **args)
> >cargs.key_len = grub_strlen (state[3].arg);
> >  }
> >
> > -  have_it = 0;
> 
> cargs->found_uuid = 0?
> 
> >if (state[0].set) /* uuid */
> >  {
> >grub_cryptodisk_t dev;
> > @@ -1166,21 +1162,18 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, 
> > int argc, char **args)
> >   return GRUB_ERR_NONE;
> > }
> >
> > -  check_boot = state[2].set;
> > -  search_uuid = args[0];
> > +  cargs.check_boot = state[2].set;
> > +  cargs.search_uuid = args[0];
> >grub_device_iterate (_cryptodisk_scan_device, );
> > -  search_uuid = NULL;
> 
> cargs.search_uuid = NULL?

The initializer used for the struct declaration should already take
care of this.

> 
> > -  if (!have_it)
> > +  if (!cargs.found_uuid)
> > return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such cryptodisk found");
> >return GRUB_ERR_NONE;
> >  }
> >else if (state[1].set || (argc == 0 && state[2].set)) /* -a|-b */
> >  {
> > -  search_uuid = NULL;
> > -  check_boot = state[2].set;
> > +  cargs.check_boot = state[2].set;
> >grub_device_iterate (_cryptodisk_scan_device, );
> > -  search_uuid = NULL;
> 
> Ditto. If this is correct then I think it should be shortly explained in
> the commit message why you can drop these assignments.

Since the storage for search_uuid is not global anymore, the struct
initializer tkes care of this. I'll add a line in the commit message.

> >return GRUB_ERR_NONE;
> >  }
> >else
> > @@ -1192,8 +1185,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
> > argc, char **args)
> >char *disklast = NULL;
> >grub_size_t len;
> >
> > -  search_uuid = NULL;
> 
> Ditto.
> 
> > -  check_boot = state[2].set;
> > +  cargs.check_boot = state[2].set;
> >diskname = args[0];
> >len = grub_strlen (diskname);
> >if (len && diskname[0] == '(' && diskname[len - 1] == ')')
> > diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
> > index 32f34d5c3..32d35521b 100644
> > --- a/grub-core/disk/geli.c
> > +++ b/grub-core/disk/geli.c
> > @@ -240,8 +240,7 @@ grub_util_get_geli_uuid (const char *dev)
> >  #endif
> >
> >  static grub_cryptodisk_t
> > -configure_ciphers (grub_disk_t disk, const char *check_uuid,
> > -  int boot_only)
> > +configure_ciphers (grub_disk_t disk, grub_cryptomount_args_t cargs)
> >  {
> >grub_cryptodisk_t newdev;
> >struct grub_geli_phdr header;
> > @@ -289,7 +288,7 @@ configure_ciphers (grub_disk_t disk, const char 

Re: [PATCH v3 3/4] cryptodisk: Move global variables into grub_cryptomount_args struct

2021-12-01 Thread Glenn Washburn
On Sun, 14 Nov 2021 10:56:15 +0100
Patrick Steinhardt  wrote:

> On Tue, Oct 12, 2021 at 06:26:28PM -0500, Glenn Washburn wrote:
> > Signed-off-by: Glenn Washburn 
> > ---
> >  grub-core/disk/cryptodisk.c | 26 +-
> >  grub-core/disk/geli.c   |  9 -
> >  grub-core/disk/luks.c   | 11 +--
> >  grub-core/disk/luks2.c  |  6 +++---
> >  include/grub/cryptodisk.h   | 10 --
> >  5 files changed, 29 insertions(+), 33 deletions(-)
> > 
> > diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
> > index a5f7b860c..5b38606ed 100644
> > --- a/grub-core/disk/cryptodisk.c
> > +++ b/grub-core/disk/cryptodisk.c
> > @@ -984,9 +984,6 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
> >  
> >  #endif
> >  
> > -static int check_boot, have_it;
> > -static char *search_uuid;
> > -
> >  static void
> >  cryptodisk_close (grub_cryptodisk_t dev)
> >  {
> > @@ -1014,7 +1011,7 @@ grub_cryptodisk_scan_device_real (const char *name,
> >  
> >FOR_CRYPTODISK_DEVS (cr)
> >{
> > -dev = cr->scan (source, search_uuid, check_boot);
> > +dev = cr->scan (source, cargs);
> >  if (grub_errno)
> >return grub_errno;
> >  if (!dev)
> > @@ -1049,7 +1046,7 @@ grub_cryptodisk_scan_device_real (const char *name,
> >  
> >  grub_cryptodisk_insert (dev, name, source);
> >  
> > -have_it = 1;
> > +cargs->found_uuid = 1;
> >  
> >  goto cleanup;
> >}
> > @@ -1091,7 +1088,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, 
> > const char *cheat)
> >  
> >FOR_CRYPTODISK_DEVS (cr)
> >{
> > -dev = cr->scan (source, search_uuid, check_boot);
> > +dev = cr->scan (source, NULL);
> 
> If I didn't get this wrong, then all scan implementations
> unconditionally dereference the `grub_cryptomount_args_t` pointer.
> So why does this work, and shouldn't we pass down a struct which has the
> `search_uuid` and `check_boot` parameters properly set up?

I'm not sure that this does work, good catch. I don't have a setup to
test GRUB utils in conjunction with cryptodisk. If you do (or anyone
else does), testing would be greatly appreciated. Regardless, I believe
this is an issue.

My reading of the previous code is that search_uuid and check_boot are
not explicitly initialized here. The only reasonable conclusion I come
to is that they are initialized to zero by default by the compiler. So
I'll create a cargs struct with all fields initalized to zero and pass
that in. Does this sound good?

Glenn

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: [PATCH v3 3/4] cryptodisk: Move global variables into grub_cryptomount_args struct

2021-11-18 Thread Daniel Kiper
On Tue, Oct 12, 2021 at 06:26:28PM -0500, Glenn Washburn wrote:
> Signed-off-by: Glenn Washburn 
> ---
>  grub-core/disk/cryptodisk.c | 26 +-
>  grub-core/disk/geli.c   |  9 -
>  grub-core/disk/luks.c   | 11 +--
>  grub-core/disk/luks2.c  |  6 +++---
>  include/grub/cryptodisk.h   | 10 --
>  5 files changed, 29 insertions(+), 33 deletions(-)
>
> diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
> index a5f7b860c..5b38606ed 100644
> --- a/grub-core/disk/cryptodisk.c
> +++ b/grub-core/disk/cryptodisk.c
> @@ -984,9 +984,6 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
>
>  #endif
>
> -static int check_boot, have_it;
> -static char *search_uuid;
> -
>  static void
>  cryptodisk_close (grub_cryptodisk_t dev)
>  {
> @@ -1014,7 +1011,7 @@ grub_cryptodisk_scan_device_real (const char *name,
>
>FOR_CRYPTODISK_DEVS (cr)
>{
> -dev = cr->scan (source, search_uuid, check_boot);
> +dev = cr->scan (source, cargs);
>  if (grub_errno)
>return grub_errno;
>  if (!dev)
> @@ -1049,7 +1046,7 @@ grub_cryptodisk_scan_device_real (const char *name,
>
>  grub_cryptodisk_insert (dev, name, source);
>
> -have_it = 1;
> +cargs->found_uuid = 1;

Please say in the commit message you are changing variable/member name too.
Or maybe it would be better if you do this in separate patch.

>  goto cleanup;
>}
> @@ -1091,7 +1088,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, 
> const char *cheat)
>
>FOR_CRYPTODISK_DEVS (cr)
>{
> -dev = cr->scan (source, search_uuid, check_boot);
> +dev = cr->scan (source, NULL);
>  if (grub_errno)
>return grub_errno;
>  if (!dev)
> @@ -1135,7 +1132,7 @@ grub_cryptodisk_scan_device (const char *name,
>
>if (err)
>  grub_print_error ();
> -  return have_it && search_uuid ? 1 : 0;
> +  return (cargs->found_uuid && cargs->search_uuid) ? 1 : 0;

I think this should be enough:
  return (cargs->found_uuid && cargs->search_uuid != NULL)

>  }
>
>  static grub_err_t
> @@ -1153,7 +1150,6 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
> argc, char **args)
>cargs.key_len = grub_strlen (state[3].arg);
>  }
>
> -  have_it = 0;

cargs->found_uuid = 0?

>if (state[0].set) /* uuid */
>  {
>grub_cryptodisk_t dev;
> @@ -1166,21 +1162,18 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
> argc, char **args)
> return GRUB_ERR_NONE;
>   }
>
> -  check_boot = state[2].set;
> -  search_uuid = args[0];
> +  cargs.check_boot = state[2].set;
> +  cargs.search_uuid = args[0];
>grub_device_iterate (_cryptodisk_scan_device, );
> -  search_uuid = NULL;

cargs.search_uuid = NULL?

> -  if (!have_it)
> +  if (!cargs.found_uuid)
>   return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such cryptodisk found");
>return GRUB_ERR_NONE;
>  }
>else if (state[1].set || (argc == 0 && state[2].set)) /* -a|-b */
>  {
> -  search_uuid = NULL;
> -  check_boot = state[2].set;
> +  cargs.check_boot = state[2].set;
>grub_device_iterate (_cryptodisk_scan_device, );
> -  search_uuid = NULL;

Ditto. If this is correct then I think it should be shortly explained in
the commit message why you can drop these assignments.

>return GRUB_ERR_NONE;
>  }
>else
> @@ -1192,8 +1185,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
> argc, char **args)
>char *disklast = NULL;
>grub_size_t len;
>
> -  search_uuid = NULL;

Ditto.

> -  check_boot = state[2].set;
> +  cargs.check_boot = state[2].set;
>diskname = args[0];
>len = grub_strlen (diskname);
>if (len && diskname[0] == '(' && diskname[len - 1] == ')')
> diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
> index 32f34d5c3..32d35521b 100644
> --- a/grub-core/disk/geli.c
> +++ b/grub-core/disk/geli.c
> @@ -240,8 +240,7 @@ grub_util_get_geli_uuid (const char *dev)
>  #endif
>
>  static grub_cryptodisk_t
> -configure_ciphers (grub_disk_t disk, const char *check_uuid,
> -int boot_only)
> +configure_ciphers (grub_disk_t disk, grub_cryptomount_args_t cargs)
>  {
>grub_cryptodisk_t newdev;
>struct grub_geli_phdr header;
> @@ -289,7 +288,7 @@ configure_ciphers (grub_disk_t disk, const char 
> *check_uuid,
>return NULL;
>  }
>
> -  if (boot_only && !(grub_le_to_cpu32 (header.flags) & GRUB_GELI_FLAGS_BOOT))
> +  if (cargs->check_boot && !(grub_le_to_cpu32 (header.flags) & 
> GRUB_GELI_FLAGS_BOOT))
>  {
>grub_dprintf ("geli", "not a boot volume\n");
>return NULL;
> @@ -302,9 +301,9 @@ configure_ciphers (grub_disk_t disk, const char 
> *check_uuid,
>return NULL;
>  }
>
> -  if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
> +  if (cargs->search_uuid && grub_strcasecmp (cargs->search_uuid, uuid) != 0)
>  {
> -  grub_dprintf ("geli", "%s != 

Re: [PATCH v3 3/4] cryptodisk: Move global variables into grub_cryptomount_args struct

2021-11-14 Thread Patrick Steinhardt
On Tue, Oct 12, 2021 at 06:26:28PM -0500, Glenn Washburn wrote:
> Signed-off-by: Glenn Washburn 
> ---
>  grub-core/disk/cryptodisk.c | 26 +-
>  grub-core/disk/geli.c   |  9 -
>  grub-core/disk/luks.c   | 11 +--
>  grub-core/disk/luks2.c  |  6 +++---
>  include/grub/cryptodisk.h   | 10 --
>  5 files changed, 29 insertions(+), 33 deletions(-)
> 
> diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
> index a5f7b860c..5b38606ed 100644
> --- a/grub-core/disk/cryptodisk.c
> +++ b/grub-core/disk/cryptodisk.c
> @@ -984,9 +984,6 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
>  
>  #endif
>  
> -static int check_boot, have_it;
> -static char *search_uuid;
> -
>  static void
>  cryptodisk_close (grub_cryptodisk_t dev)
>  {
> @@ -1014,7 +1011,7 @@ grub_cryptodisk_scan_device_real (const char *name,
>  
>FOR_CRYPTODISK_DEVS (cr)
>{
> -dev = cr->scan (source, search_uuid, check_boot);
> +dev = cr->scan (source, cargs);
>  if (grub_errno)
>return grub_errno;
>  if (!dev)
> @@ -1049,7 +1046,7 @@ grub_cryptodisk_scan_device_real (const char *name,
>  
>  grub_cryptodisk_insert (dev, name, source);
>  
> -have_it = 1;
> +cargs->found_uuid = 1;
>  
>  goto cleanup;
>}
> @@ -1091,7 +1088,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, 
> const char *cheat)
>  
>FOR_CRYPTODISK_DEVS (cr)
>{
> -dev = cr->scan (source, search_uuid, check_boot);
> +dev = cr->scan (source, NULL);

If I didn't get this wrong, then all scan implementations
unconditionally dereference the `grub_cryptomount_args_t` pointer.
So why does this work, and shouldn't we pass down a struct which has the
`search_uuid` and `check_boot` parameters properly set up?

Patrick


signature.asc
Description: PGP signature
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v3 3/4] cryptodisk: Move global variables into grub_cryptomount_args struct

2021-10-12 Thread Glenn Washburn
Signed-off-by: Glenn Washburn 
---
 grub-core/disk/cryptodisk.c | 26 +-
 grub-core/disk/geli.c   |  9 -
 grub-core/disk/luks.c   | 11 +--
 grub-core/disk/luks2.c  |  6 +++---
 include/grub/cryptodisk.h   | 10 --
 5 files changed, 29 insertions(+), 33 deletions(-)

diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index a5f7b860c..5b38606ed 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -984,9 +984,6 @@ grub_util_cryptodisk_get_uuid (grub_disk_t disk)
 
 #endif
 
-static int check_boot, have_it;
-static char *search_uuid;
-
 static void
 cryptodisk_close (grub_cryptodisk_t dev)
 {
@@ -1014,7 +1011,7 @@ grub_cryptodisk_scan_device_real (const char *name,
 
   FOR_CRYPTODISK_DEVS (cr)
   {
-dev = cr->scan (source, search_uuid, check_boot);
+dev = cr->scan (source, cargs);
 if (grub_errno)
   return grub_errno;
 if (!dev)
@@ -1049,7 +1046,7 @@ grub_cryptodisk_scan_device_real (const char *name,
 
 grub_cryptodisk_insert (dev, name, source);
 
-have_it = 1;
+cargs->found_uuid = 1;
 
 goto cleanup;
   }
@@ -1091,7 +1088,7 @@ grub_cryptodisk_cheat_mount (const char *sourcedev, const 
char *cheat)
 
   FOR_CRYPTODISK_DEVS (cr)
   {
-dev = cr->scan (source, search_uuid, check_boot);
+dev = cr->scan (source, NULL);
 if (grub_errno)
   return grub_errno;
 if (!dev)
@@ -1135,7 +1132,7 @@ grub_cryptodisk_scan_device (const char *name,
   
   if (err)
 grub_print_error ();
-  return have_it && search_uuid ? 1 : 0;
+  return (cargs->found_uuid && cargs->search_uuid) ? 1 : 0;
 }
 
 static grub_err_t
@@ -1153,7 +1150,6 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
argc, char **args)
   cargs.key_len = grub_strlen (state[3].arg);
 }
 
-  have_it = 0;
   if (state[0].set) /* uuid */
 {
   grub_cryptodisk_t dev;
@@ -1166,21 +1162,18 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
argc, char **args)
  return GRUB_ERR_NONE;
}
 
-  check_boot = state[2].set;
-  search_uuid = args[0];
+  cargs.check_boot = state[2].set;
+  cargs.search_uuid = args[0];
   grub_device_iterate (_cryptodisk_scan_device, );
-  search_uuid = NULL;
 
-  if (!have_it)
+  if (!cargs.found_uuid)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no such cryptodisk found");
   return GRUB_ERR_NONE;
 }
   else if (state[1].set || (argc == 0 && state[2].set)) /* -a|-b */
 {
-  search_uuid = NULL;
-  check_boot = state[2].set;
+  cargs.check_boot = state[2].set;
   grub_device_iterate (_cryptodisk_scan_device, );
-  search_uuid = NULL;
   return GRUB_ERR_NONE;
 }
   else
@@ -1192,8 +1185,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int 
argc, char **args)
   char *disklast = NULL;
   grub_size_t len;
 
-  search_uuid = NULL;
-  check_boot = state[2].set;
+  cargs.check_boot = state[2].set;
   diskname = args[0];
   len = grub_strlen (diskname);
   if (len && diskname[0] == '(' && diskname[len - 1] == ')')
diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c
index 32f34d5c3..32d35521b 100644
--- a/grub-core/disk/geli.c
+++ b/grub-core/disk/geli.c
@@ -240,8 +240,7 @@ grub_util_get_geli_uuid (const char *dev)
 #endif
 
 static grub_cryptodisk_t
-configure_ciphers (grub_disk_t disk, const char *check_uuid,
-  int boot_only)
+configure_ciphers (grub_disk_t disk, grub_cryptomount_args_t cargs)
 {
   grub_cryptodisk_t newdev;
   struct grub_geli_phdr header;
@@ -289,7 +288,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   return NULL;
 }
 
-  if (boot_only && !(grub_le_to_cpu32 (header.flags) & GRUB_GELI_FLAGS_BOOT))
+  if (cargs->check_boot && !(grub_le_to_cpu32 (header.flags) & 
GRUB_GELI_FLAGS_BOOT))
 {
   grub_dprintf ("geli", "not a boot volume\n");
   return NULL;
@@ -302,9 +301,9 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   return NULL;
 }
 
-  if (check_uuid && grub_strcasecmp (check_uuid, uuid) != 0)
+  if (cargs->search_uuid && grub_strcasecmp (cargs->search_uuid, uuid) != 0)
 {
-  grub_dprintf ("geli", "%s != %s\n", uuid, check_uuid);
+  grub_dprintf ("geli", "%s != %s\n", uuid, cargs->search_uuid);
   return NULL;
 }
 
diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c
index 51646cefe..6ced312c7 100644
--- a/grub-core/disk/luks.c
+++ b/grub-core/disk/luks.c
@@ -63,8 +63,7 @@ gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, 
grub_uint8_t * src,
  grub_size_t blocknumbers);
 
 static grub_cryptodisk_t
-configure_ciphers (grub_disk_t disk, const char *check_uuid,
-  int check_boot)
+configure_ciphers (grub_disk_t disk, grub_cryptomount_args_t cargs)
 {
   grub_cryptodisk_t newdev;
   const char *iptr;
@@ -76,7 +75,7 @@ configure_ciphers (grub_disk_t disk, const