Skip to contentSkip to conversation

Clean up unused values

PATCH In review Alec Brown 6 participants 10 messages Jan 20, 2024 – Jan 26, 2024

lore
Share & tools
lore

Alec Brown

author
#0 lore raw

cover Clean up unused values

Coverity listed three unused value bugs in the GRUB. These patches help clean up and remove these uneccessary bits of code.

The Coverity bugs being addressed are:
CID 428875
CID 428876
CID 428877

Alec Brown (3):
fs/jfs.c: Clean up redundant code osdep/unix/getroot.c: Clean up redundant code loader/i386/multiboot_mbi.c: Clean up redundant code

 grub-core/fs/jfs.c                    | 1 -
 grub-core/loader/i386/multiboot_mbi.c | 2 +-
 grub-core/osdep/unix/getroot.c        | 1 -
 3 files changed, 1 insertion(+), 3 deletions(-)

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

author
#1 lore raw

PATCH 1/3 fs/jfs.c: Clean up redundant code

In grub-core/fs/jfs.c, coverity spotted redundant code where the pointer diro was being set to 0 and then being overwritten later without being used. Since this is unnecessary, we can remove the code that sets diro to 0.

Fixes CID 428876
---
 grub-core/fs/jfs.c | 1 -
 1 file changed, 1 deletion(-)
grub-core/fs/jfs.c +0 -1
index 6f7c43904..62e20ef6f 100644
@@ -716,7 +716,6 @@ grub_jfs_find_file (struct grub_jfs_data *data, const char *path,
 	      grub_uint32_t dirino = grub_le_to_cpu32 (data->currinode.inode);
 
 	      grub_jfs_closedir (diro);
-	      diro = 0;
 
 	      if (grub_jfs_read_inode (data, ino, &data->currinode))
 		break;
Signature · 7 lines
2.27.0


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

PATCH 2/3 osdep/unix/getroot.c: Clean up redundant code

In reply to Alec Brown

In grub-core/osdep/unix/getroot.c, coverity spotted redundant code where the double pointer os_dev was being set to 0 and then being overwritten later without being used. Since this is unnecessary, we can remove the code that sets os_dev to 0.

Fixes CID 428875
---
 grub-core/osdep/unix/getroot.c | 1 -
 1 file changed, 1 deletion(-)
grub-core/osdep/unix/getroot.c +0 -1
index cde821eb9..12b111634 100644
@@ -540,7 +540,6 @@ grub_guess_root_devices (const char *dir_in)
       for (cur = os_dev; *cur; cur++)
 	free (*cur);
       free (os_dev);
-      os_dev = 0;
     }
 
   if (stat (dir, &st) < 0)
Signature · 7 lines
2.27.0


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

Re: [PATCH 2/3] osdep/unix/getroot.c: Clean up redundant code

Dear Alec, and grub-devel,

I haven't checked the specific code in question, but do we really want to be removing such null-assignments? (Thinking about multiple patches exactly like this).

In correct code, they are of course redundant by definition, however their intended purpose is that if the code happens to be incorrect, they turn use-after-free bugs into zero page accesses.

Since static analysis of a language like C is inherently conservative, it is entirely possible that it is detecting the redundant assignment, but not the use after free it would have prevented.

With UEFI looking to gain more memory protections, including un-mapped zero-page (alongside NX), it might even be a good idea to add more such assignments instead of removing them.

Best regards,
Mate Kukri

Quoted 29 lines · Alec Brown

In grub-core/osdep/unix/getroot.c, coverity spotted redundant code where the double pointer os_dev was being set to 0 and then being overwritten later without being used. Since this is unnecessary, we can remove the code that sets os_dev to 0.

Fixes CID 428875
---
 grub-core/osdep/unix/getroot.c | 1 -
 1 file changed, 1 deletion(-)
grub-core/osdep/unix/getroot.c +0 -2
index cde821eb9..12b111634 100644
@@ -540,7 +540,6 @@ grub_guess_root_devices (const char *dir_in)
       for (cur = os_dev; *cur; cur++)
        free (*cur);
       free (os_dev);
-      os_dev = 0;
     }
 
   if (stat (dir, &st) < 0)
--

2.27.0

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

#4 lore raw

Re: [PATCH 2/3] osdep/unix/getroot.c: Clean up redundant code

Mate,

Next time please respond to all people/addresses in the original email...

Quoted 6 lines · Mate Kukri

Dear Alec, and grub-devel,

I haven't checked the specific code in question, but do we really want to be removing such null-assignments? (Thinking about multiple patches exactly like this).

Quoted 7 lines

In correct code, they are of course redundant by definition, however their intended purpose is that if the code happens to be incorrect, they turn use-after-free bugs into zero page accesses.

Since static analysis of a language like C is inherently conservative, it is entirely possible that it is detecting the redundant assignment, but not the use after free it would have prevented.

We know the Coverity makes mistakes. So, we carefully check its reports. These ones are not exceptions. Here the Coverity is correct and it is safe to remove redundant code. I expect somebody was changing the code at some point and forgot to drop surplus assignments.

Daniel

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

#5 lore raw

Re: [PATCH 2/3] osdep/unix/getroot.c: Clean up redundant code

Quoted 16 lines · Daniel Kiper

Mate,

Next time please respond to all people/addresses in the original email...

Quoted 10 lines · Mate Kukri

Dear Alec, and grub-devel,

I haven't checked the specific code in question, but do we really want to be removing such null-assignments? (Thinking about multiple patches exactly like this).

In correct code, they are of course redundant by definition, however their intended purpose is that if the code happens to be incorrect, they turn use-after-free bugs into zero page accesses.

Quoted 9 lines
Quoted 3 lines

Since static analysis of a language like C is inherently conservative, it is entirely possible that it is detecting the redundant assignment, but not the use after free it would have prevented.

We know the Coverity makes mistakes. So, we carefully check its reports. These ones are not exceptions. Here the Coverity is correct and it is safe to remove redundant code. I expect somebody was changing the code at some point and forgot to drop surplus assignments.

Nope. It was to keep an invariant and ensure we don't double-free

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

author
#6 lore raw

PATCH 3/3 loader/i386/multiboot_mbi.c: Clean up redundant code

In reply to Alec Brown

In grub-core/loader/i386/multiboot_mbi.c, coverity spotted redundant code where the variable err was being set to GRUB_ERR_NONE and then being overwritten later without being used. Since this is unnecessary, we can remove the code that sets err to GRUB_ERR_NONE.

Fixes CID 428877
---
 grub-core/loader/i386/multiboot_mbi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/loader/i386/multiboot_mbi.c +1 -1
index 11a6e224f..fae5d6fb8 100644
@@ -86,7 +86,7 @@ load_kernel (grub_file_t file, const char *filename,
 	return GRUB_ERR_NONE;
       }
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
-	grub_errno = err = GRUB_ERR_NONE;
+	grub_errno = GRUB_ERR_NONE;
     }
   if (header->flags & MULTIBOOT_AOUT_KLUDGE)
     {
Signature · 7 lines
2.27.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw
In reply to Alec Brown
Quoted 8 lines · Alec Brown

Coverity listed three unused value bugs in the GRUB. These patches help clean up and remove these uneccessary bits of code.

The Coverity bugs being addressed are:
CID 428875
CID 428876
CID 428877

Quoted 4 lines

Alec Brown (3):
fs/jfs.c: Clean up redundant code osdep/unix/getroot.c: Clean up redundant code loader/i386/multiboot_mbi.c: Clean up redundant code

For all patches Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

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

#8 lore raw
In reply to Alec Brown

I oppose to all 3 patches. These assignments are not redundant but keep an important invariant: the variable in question can be passed to free(). For this it needs to either be NULL or point to a valid allocated memory. In this code this ensures that we never double free even after code changes

Quoted 21 lines · Alec Brown

Coverity listed three unused value bugs in the GRUB. These patches help clean
up and remove these uneccessary bits of code.

The Coverity bugs being addressed are:
CID 428875
CID 428876
CID 428877

Alec Brown (3):
fs/jfs.c: Clean up redundant code osdep/unix/getroot.c: Clean up redundant code loader/i386/multiboot_mbi.c: Clean up redundant code

 grub-core/fs/jfs.c                    | 1 -
 grub-core/loader/i386/multiboot_mbi.c | 2 +-
 grub-core/osdep/unix/getroot.c        | 1 -
 3 files changed, 1 insertion(+), 3 deletions(-)
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

#9 lore raw

If I may add a small point, apart from the invariant being kept, initializing on definition also makes bug behavior more deterministic. Hunting for a bug that only manifests on certain uninitialized values in memory is hard.

Quoted 31 lines · Vladimir 'phcoder' Serbinenko

I oppose to all 3 patches. These assignments are not redundant but keep an important invariant: the variable in question can be passed to free(). For this it needs to either be NULL or point to a valid allocated memory. In this code this ensures that we never double free even after code changes

Quoted 21 lines · Alec Brown

Coverity listed three unused value bugs in the GRUB. These patches help clean up and remove these uneccessary bits of code.

The Coverity bugs being addressed are:
CID 428875
CID 428876
CID 428877

Alec Brown (3):
fs/jfs.c: Clean up redundant code osdep/unix/getroot.c: Clean up redundant code loader/i386/multiboot_mbi.c: Clean up redundant code

 grub-core/fs/jfs.c                    | 1 -
 grub-core/loader/i386/multiboot_mbi.c | 2 +-
 grub-core/osdep/unix/getroot.c        | 1 -
 3 files changed, 1 insertion(+), 3 deletions(-)
Quoted 4 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