grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: "Vladimir 'phcoder' Serbinenko" <phcoder@gmail.com>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: [PATCH 01/01] loader/bsd: Fix loading after unaligned module
Date: Sun, 8 Oct 2023 16:27:30 +0200	[thread overview]
Message-ID: <CAEaD8JM3buSEimOPjBGkaHoG=KLijEgMrJzvGOJyvnz1PUAZZg@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #1.2: Type: text/html, Size: 23 bytes --]

[-- Attachment #2: 0001-loader-bsd-Fix-loading-after-unaligned-module.patch --]
[-- Type: text/x-diff, Size: 2973 bytes --]

From fe519348967c7096e4b9466fc39bd393cd58319a Mon Sep 17 00:00:00 2001
From: Vladimir Serbinenko <phcoder@gmail.com>
Date: Mon, 10 Jul 2023 17:17:35 +0200
Subject: [PATCH 01/01] loader/bsd: Fix loading after unaligned module

Current code implicitly assumes that aligning chunk_size + *kern_end is
the same as aligning on curload which is not the case because
chunk_size starts at zero even if *kern_end is unaligned and ALIGN_PAGE
moved curload to an aligned position but not *kern_end + chunk_size

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 grub-core/loader/i386/bsdXX.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/grub-core/loader/i386/bsdXX.c b/grub-core/loader/i386/bsdXX.c
index 09d909f1b..d3de8a29b 100644
--- a/grub-core/loader/i386/bsdXX.c
+++ b/grub-core/loader/i386/bsdXX.c
@@ -88,10 +88,9 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
   Elf_Shnum shnum;
   grub_addr_t curload, module;
   grub_err_t err;
-  grub_size_t chunk_size = 0;
   void *chunk_src;
 
-  curload = module = ALIGN_PAGE (*kern_end);
+  module = ALIGN_PAGE (*kern_end);
 
   err = read_headers (file, argv[0], &e, &shdr);
   if (err != GRUB_ERR_NONE)
@@ -101,6 +100,8 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
   if (err != GRUB_ERR_NONE)
     goto out;
 
+  curload = module;
+
   for (s = shdr; s < (Elf_Shdr *) ((grub_uint8_t *) shdr + shnum * e.e_shentsize);
        s = (Elf_Shdr *) ((grub_uint8_t *) s + e.e_shentsize))
     {
@@ -108,21 +109,24 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
 	continue;
 
       if (s->sh_addralign)
-	chunk_size = ALIGN_UP (chunk_size + *kern_end, s->sh_addralign)
-	  - *kern_end;
+	curload = ALIGN_UP (curload, s->sh_addralign);
 
-      chunk_size += s->sh_size;
+      curload += s->sh_size;
     }
 
+  *kern_end = ALIGN_PAGE (curload);
+
   {
     grub_relocator_chunk_t ch;
     err = grub_relocator_alloc_chunk_addr (relocator, &ch,
-					   module, chunk_size);
+					   module, curload - module);
     if (err != GRUB_ERR_NONE)
       goto out;
     chunk_src = get_virtual_current_address (ch);
   }
 
+  curload = module;
+
   for (s = shdr; s < (Elf_Shdr *) ((grub_uint8_t *) shdr + shnum * e.e_shentsize);
        s = (Elf_Shdr *) ((grub_uint8_t *) s + e.e_shentsize))
     {
@@ -141,13 +145,13 @@ SUFFIX (grub_freebsd_load_elfmodule_obj) (struct grub_relocator *relocator,
 	{
 	default:
 	case SHT_PROGBITS:
-	  err = load (file, argv[0], (grub_uint8_t *) chunk_src + curload - *kern_end,
+	  err = load (file, argv[0], (grub_uint8_t *) chunk_src + curload - module,
 		      s->sh_offset, s->sh_size);
 	  if (err != GRUB_ERR_NONE)
 	    goto out;
 	  break;
 	case SHT_NOBITS:
-	  grub_memset ((grub_uint8_t *) chunk_src + curload - *kern_end, 0,
+	  grub_memset ((grub_uint8_t *) chunk_src + curload - module, 0,
 		       s->sh_size);
 	  break;
 	}
-- 
2.42.0


[-- Attachment #3: Type: text/plain, Size: 141 bytes --]

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

                 reply	other threads:[~2023-10-08 14:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAEaD8JM3buSEimOPjBGkaHoG=KLijEgMrJzvGOJyvnz1PUAZZg@mail.gmail.com' \
    --to=phcoder@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).