[PATCH v2 3/5] loongarch: Rename function names

2023-06-07 Thread Xiaotian Wu
According to the relocation documentation, the following function names are
renamed to show their exact meaning:

- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_hi12()
- from grub_loongarch64_xxx64_hi12() to grub_loongarch64_abs64_lo20()

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 include/grub/loongarch64/reloc.h   | 4 ++--
 util/grub-mkimagexx.c  | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 43080e72e..7f923b415 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -109,13 +109,13 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
case R_LARCH_ABS64_LO20:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_lo20 (abs_place, sym_addr);
+   grub_loongarch64_abs64_lo20 (abs_place, sym_addr);
  }
  break;
case R_LARCH_ABS64_HI12:
  {
grub_uint32_t *abs_place = place;
-   grub_loongarch64_xxx64_hi12 (abs_place, sym_addr);
+   grub_loongarch64_abs64_hi12 (abs_place, sym_addr);
  }
  break;
case R_LARCH_PCALA_HI20:
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index e869ce9ac..879ae6189 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -235,7 +235,7 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
   grub_uint32_t val;
@@ -247,7 +247,7 @@ void grub_loongarch64_xxx64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
-void grub_loongarch64_xxx64_lo20 (grub_uint32_t *place, grub_int64_t offset)
+void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
   grub_uint32_t val;
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
index dec7a6b36..8ba355385 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -60,8 +60,8 @@ void grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
 void grub_loongarch64_b26(grub_uint32_t *place, grub_int64_t offset);
 void grub_loongarch64_xxx_hi20   (grub_uint32_t *place, grub_int64_t offset);
 void grub_loongarch64_xxx_lo12   (grub_uint32_t *place, grub_int64_t offset);
-void grub_loongarch64_xxx64_hi12  (grub_uint32_t *place, grub_int64_t offset);
-void grub_loongarch64_xxx64_lo20  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_hi12  (grub_uint32_t *place, grub_int64_t offset);
+void grub_loongarch64_abs64_lo20  (grub_uint32_t *place, grub_int64_t offset);
 
 #define GRUB_LOONGARCH64_RELOCATION(STACK, PLACE, OFFSET)  \
   case R_LARCH_SOP_PUSH_ABSOLUTE:  \
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a3ab04eb1..e50b29533 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1165,10 +1165,10 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 grub_loongarch64_xxx_hi20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_LO20:
-grub_loongarch64_xxx64_lo20 (t32, sym_addr);
+grub_loongarch64_abs64_lo20 (t32, sym_addr);
 break;
   case R_LARCH_ABS64_HI12:
-grub_loongarch64_xxx64_hi12 (t32, sym_addr);
+grub_loongarch64_abs64_hi12 (t32, sym_addr);
 break;
   case R_LARCH_PCALA_HI20:
 {
-- 
2.20.1


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


[PATCH v2 4/5] loongarch: Add ELF relocation types documentation and comments

2023-06-07 Thread Xiaotian Wu
see https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl_helper.c | 25 +
 1 file changed, 25 insertions(+)

diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index 879ae6189..b5d39b282 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -24,6 +24,10 @@
 #include 
 #include 
 
+/*
+ * LoongArch relocations documentation:
+ * https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations
+ */
 static void grub_loongarch64_stack_push (grub_loongarch64_stack_t stack, 
grub_uint64_t x);
 static grub_uint64_t grub_loongarch64_stack_pop (grub_loongarch64_stack_t 
stack);
 
@@ -200,6 +204,11 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B26 relocation for the 28-bit PC-relative jump
+ * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
 void grub_loongarch64_b26 (grub_uint32_t *place, grub_int64_t offset)
 {
   grub_uint32_t val;
@@ -215,6 +224,10 @@ void grub_loongarch64_b26 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_HI20/PCALA_HI20 relocations for 32/64-bit absolute address/PC-relative 
offset
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [31 ... 12]
+ */
 void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
@@ -227,6 +240,10 @@ void grub_loongarch64_xxx_hi20 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS_LO12/PCALA_LO12 relocations for 32/64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [11 ... 0]
+ */
 void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
@@ -235,6 +252,10 @@ void grub_loongarch64_xxx_lo12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (offset << 10) & ~insmask;
 }
 
+/*
+ * ABS64_HI12 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [21 ... 10] = (S+A) [63 ... 52]
+ */
 void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xffc003ff);
@@ -247,6 +268,10 @@ void grub_loongarch64_abs64_hi12 (grub_uint32_t *place, 
grub_int64_t offset)
   *place |= grub_cpu_to_le32 (val) & ~insmask;
 }
 
+/*
+ * ABS64_LO20 relocation for the 64-bit absolute address
+ * (*(uint32_t *) PC) [24 ... 5] = (S+A) [51 ... 32]
+ */
 void grub_loongarch64_abs64_lo20 (grub_uint32_t *place, grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfe1f);
-- 
2.20.1


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


[PATCH v2 2/5] loongarch: Optimize code using pc variable

2023-06-07 Thread Xiaotian Wu
We already have the pc variable, no need to calculate it again.

Signed-off-by: Xiaotian Wu 
---
 util/grub-mkimagexx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index e7d5bc631..a3ab04eb1 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -1156,7 +1156,7 @@ SUFFIX (relocate_addrs) (Elf_Ehdr *e, struct 
section_metadata *smd,
 {
   grub_int64_t off;
 
-  off = sym_addr - target_section_addr - offset - 
image_target->vaddr_offset;
+  off = sym_addr - pc;
 
   grub_loongarch64_b26 (t32, off);
 }
-- 
2.20.1


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


[PATCH v2 1/5] Use the correct format specifier for formatted output

2023-06-07 Thread Xiaotian Wu
Use "PRIxGRUB_INT64_T" format specifier for "grub_int64_t" type, and drop the
casts code.

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/arm64/dl_helper.c   | 4 ++--
 grub-core/kern/loongarch64/dl_helper.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/grub-core/kern/arm64/dl_helper.c b/grub-core/kern/arm64/dl_helper.c
index cf7d432a3..10e3d1ec2 100644
--- a/grub-core/kern/arm64/dl_helper.c
+++ b/grub-core/kern/arm64/dl_helper.c
@@ -46,9 +46,9 @@ grub_arm64_set_26_offset (grub_uint32_t *place, 
grub_int64_t offset)
 {
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   *place &= insmask;
   *place |= grub_cpu_to_le32 (offset >> 2) & ~insmask;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index cda1a53c8..e869ce9ac 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -205,9 +205,9 @@ void grub_loongarch64_b26 (grub_uint32_t *place, 
grub_int64_t offset)
   grub_uint32_t val;
   const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc00);
 
-  grub_dprintf ("dl", "  reloc_64 %p %c= 0x%llx\n",
+  grub_dprintf ("dl", "  reloc_b26 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
place, offset > 0 ? '+' : '-',
-   offset < 0 ? (long long) -(unsigned long long) offset : offset);
+   offset < 0 ? -offset : offset);
 
   val = ((offset >> 18) & 0x3ff) | (((offset >> 2) & 0x) << 10);
 
-- 
2.20.1


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


[PATCH v2 5/5] loongarch: Add relaxation support

2023-06-07 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1],
the next version of binutils will not be able to build grub.

So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX [2] relocations to
enhance grub compatibility.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b
[2]: 
https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#relocations

Signed-off-by: Xiaotian Wu 
---
 grub-core/kern/loongarch64/dl.c| 17 +++
 grub-core/kern/loongarch64/dl_helper.c | 39 ++
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  2 ++
 util/grub-mkimagexx.c  | 22 +++
 util/grub-module-verifier.c|  3 ++
 6 files changed, 86 insertions(+)

diff --git a/grub-core/kern/loongarch64/dl.c b/grub-core/kern/loongarch64/dl.c
index 7f923b415..c22d2bd52 100644
--- a/grub-core/kern/loongarch64/dl.c
+++ b/grub-core/kern/loongarch64/dl.c
@@ -87,11 +87,28 @@ grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  }
  break;
case R_LARCH_MARK_LA:
+   case R_LARCH_RELAX:
  break;
case R_LARCH_SOP_PUSH_PCREL:
case R_LARCH_SOP_PUSH_PLT_PCREL:
  grub_loongarch64_sop_push (&stack, sym_addr - (grub_uint64_t)place);
  break;
+   case R_LARCH_B16:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b16 (abs_place, off);
+ }
+ break;
+   case R_LARCH_B21:
+ {
+   grub_uint32_t *abs_place = place;
+   grub_ssize_t off = sym_addr - (grub_addr_t) place;
+
+   grub_loongarch64_b21 (abs_place, off);
+ }
+ break;
case R_LARCH_B26:
  {
grub_uint32_t *abs_place = place;
diff --git a/grub-core/kern/loongarch64/dl_helper.c 
b/grub-core/kern/loongarch64/dl_helper.c
index b5d39b282..a3569b1e5 100644
--- a/grub-core/kern/loongarch64/dl_helper.c
+++ b/grub-core/kern/loongarch64/dl_helper.c
@@ -204,6 +204,45 @@ grub_loongarch64_sop_32_s_0_10_10_16_s2 
(grub_loongarch64_stack_t stack,
   *place =(*place) | ((a >> 18) & 0x3ff);
 }
 
+/*
+ * B16 relocation for the 18-bit PC-relative jump
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b16 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003ff);
+
+  grub_dprintf ("dl", "  reloc_b16 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? -offset : offset);
+
+  val = ((offset >> 2) & 0x) << 10;
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
+/*
+ * B21 relocation for the 23-bit PC-relative jump
+ * (*(uint32_t *) PC) [4 ... 0] = (S+A-PC) [22 ... 18]
+ * (*(uint32_t *) PC) [25 ... 10] = (S+A-PC) [17 ... 2]
+ */
+void grub_loongarch64_b21 (grub_uint32_t *place, grub_int64_t offset)
+{
+  grub_uint32_t val;
+  const grub_uint32_t insmask = grub_cpu_to_le32_compile_time (0xfc0003e0);
+
+  grub_dprintf ("dl", "  reloc_b21 %p %c= 0x%" PRIxGRUB_INT64_T "\n",
+   place, offset > 0 ? '+' : '-',
+   offset < 0 ? -offset : offset);
+
+  val = ((offset >> 18) & 0x1f) | (((offset >> 2) & 0x) << 10);
+
+  *place &= insmask;
+  *place |= grub_cpu_to_le32 (val) & ~insmask;
+}
+
 /*
  * B26 relocation for the 28-bit PC-relative jump
  * (*(uint32_t *) PC) [9 ... 0] = (S+A-PC) [27 ... 18]
diff --git a/include/grub/elf.h b/include/grub/elf.h
index bd313a70b..12349c9e4 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -2558,6 +2558,8 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_SOP_POP_32_S_5_2043
 #define R_LARCH_SOP_POP_32_S_0_5_10_16_S2 44
 #define R_LARCH_SOP_POP_32_S_0_10_10_16_S245
+#define R_LARCH_B16  64
+#define R_LARCH_B21  65
 #define R_LARCH_B26  66
 #define R_LARCH_ABS_HI20 67
 #define R_LARCH_ABS_LO12 68
@@ -2565,6 +2567,7 @@ typedef Elf32_Addr Elf32_Conflict;
 #define R_LARCH_ABS64_HI12   70
 #define R_LARCH_PCALA_HI20   71
 #define R_LARCH_PCALA_LO12   72
+#define R_LARCH_RELAX100
 
 extern grub_err_t grub_elf32_get_shnum (Elf32_Ehdr *e, Elf32_Shnum *shnum);
 extern grub_err_t grub_elf32_get_shstrndx (Elf32_Ehdr *e, Elf32_Word 
*shstrndx);
diff --git a/include/grub/loongarch64/reloc.h b/include/grub/loongarch64/reloc.h
index 8ba355385..01d4f6831 100644
--- a/include/grub/loongarch64/reloc.h
+++ b/include/grub/loongarch64/reloc.h
@@ -57,6 +57,8 @@ void grub_loongarch64_sop_32_s_0_5_10_16_s2  
(grub_loongarch64_stack_t stack,
 void grub_loongarch64_sop_32_s_0_10_10_16_s2 (grub_loongarch64_st

[PATCH v2 0/5] loongarch: add relaxation support

2023-06-07 Thread Xiaotian Wu
Because the binutils of the loongarch architecture adds relaxation support [1], 
the next version of binutils will not be able to build grub.

So we added the R_LARCH_B16, R_LARCH_B21 and R_LARCH_RELAX relocations to 
enhance grub compatibility.

[1]: 
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=56576f4a722b7398d35802ecf7d4185c27d6d69b

v1->v2:
- split patch
- drop cast code

Xiaotian Wu (5):
  Use the correct format specifier for formatted output
  loongarch: Optimize code using pc variable
  loongarch: Rename function names
  loongarch: Add ELF relocation types documentation and comments
  loongarch: Add relaxation support

 grub-core/kern/arm64/dl_helper.c   |  4 +-
 grub-core/kern/loongarch64/dl.c| 21 +++-
 grub-core/kern/loongarch64/dl_helper.c | 72 --
 include/grub/elf.h |  3 ++
 include/grub/loongarch64/reloc.h   |  6 ++-
 util/grub-mkimagexx.c  | 28 --
 util/grub-module-verifier.c|  3 ++
 7 files changed, 124 insertions(+), 13 deletions(-)

-- 
2.20.1


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


Re: [PATCH] osdep/linux: Fix md array device enumeration

2023-06-07 Thread Daniel Kiper
On Tue, Jun 06, 2023 at 11:02:31AM -0700, Kees Cook wrote:
> On Tue, Jun 6, 2023 at 10:27 AM Julian Andres Klode
>  wrote:
> >
> > On Tue, Jun 06, 2023 at 07:09:26PM +0200, Daniel Kiper wrote:
> > > On Tue, Jun 06, 2023 at 06:15:27PM +0200, Julian Andres Klode wrote:
> > > > On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:
> > > [...]
> > > This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array
> > > device enumeration).
>
> Oh good. I really thought it had landed already, so thanks for
> checking. I got worried this morning when I saw the email to
> grub-devel. :P "Wasn't that fixed already?" :) But thank you for
> making sure it hadn't gotten lost! Is there a way to close the tracker
> item for it?

I think you should be able to do that. If not I can try to do that for you.

> > [...]
> > > I realized right now that MD_MAX_DISKS defined in commit c39f27cd6
> > > (osdep/linux: Fix md array device enumeration) is not in sync with
> > > commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more
> > > than 1024 disks). I think we should sync both numbers down to 1024...
> >
> > +1
>
> Yeah, seems reasonable, though as I hinted in the original patch, this
> number appeared to have been arbitrarily chosen by mdadm at the time.

OK, we will bump it to 4096 as well.

Daniel

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


Re: [PATCH v2 1/1] fs/udf: Fix out of bounds access

2023-06-07 Thread Darren Kenny
Hi Li,,

LGTM!

Reviewed-by: Darren Kenny 

Thanks,

Darren.

On Wednesday, 2023-06-07 at 01:31:06 UTC, Lidong Chen wrote:
> Implemented a boundary check before advancing the allocation
> descriptors pointer.
>
> Signed-off-by: Lidong Chen 
> Reviewed-by: Darren Kenny 
> Reviewed-by: Daniel Kiper 
> ---
>  grub-core/fs/udf.c | 38 ++
>  1 file changed, 38 insertions(+)
>
> diff --git a/grub-core/fs/udf.c b/grub-core/fs/udf.c
> index 7679ea309..58884d2ba 100644
> --- a/grub-core/fs/udf.c
> +++ b/grub-core/fs/udf.c
> @@ -114,6 +114,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
>  #define GRUB_UDF_PARTMAP_TYPE_1  1
>  #define GRUB_UDF_PARTMAP_TYPE_2  2
>  
> +#define GRUB_UDF_INVALID_STRUCT_PTR(_ptr, _struct)   \
> +  ((char *) (_ptr) >= end_ptr || \
> +   ((grub_ssize_t)(end_ptr - (char*)(_ptr)) < (grub_ssize_t)sizeof(_struct)))
> +
>  struct grub_udf_lb_addr
>  {
>grub_uint32_t block_num;
> @@ -458,6 +462,7 @@ grub_udf_read_block (grub_fshelp_node_t node, 
> grub_disk_addr_t fileblock)
>char *ptr;
>grub_ssize_t len;
>grub_disk_addr_t filebytes;
> +  char *end_ptr;
>  
>switch (U16 (node->block.fe.tag.tag_ident))
>  {
> @@ -476,9 +481,17 @@ grub_udf_read_block (grub_fshelp_node_t node, 
> grub_disk_addr_t fileblock)
>return 0;
>  }
>  
> +  end_ptr = (char *) node + get_fshelp_size (node->data);
> +
>if ((U16 (node->block.fe.icbtag.flags) & GRUB_UDF_ICBTAG_FLAG_AD_MASK)
>== GRUB_UDF_ICBTAG_FLAG_AD_SHORT)
>  {
> +  if (GRUB_UDF_INVALID_STRUCT_PTR(ptr, struct grub_udf_short_ad))
> + {
> +   grub_error (GRUB_ERR_BAD_FS, "corrupted UDF file system");
> +   return 0;
> + }
> +
>struct grub_udf_short_ad *ad = (struct grub_udf_short_ad *) ptr;
>  
>filebytes = fileblock * U32 (node->data->lvd.bsize);
> @@ -542,10 +555,22 @@ grub_udf_read_block (grub_fshelp_node_t node, 
> grub_disk_addr_t fileblock)
> filebytes -= adlen;
> ad++;
> len -= sizeof (struct grub_udf_short_ad);
> +
> +   if (GRUB_UDF_INVALID_STRUCT_PTR(ad, struct grub_udf_short_ad))
> + {
> +   grub_error (GRUB_ERR_BAD_FS, "corrupted UDF file system");
> +   return 0;
> + }
>   }
>  }
>else
>  {
> +  if (GRUB_UDF_INVALID_STRUCT_PTR(ptr, struct grub_udf_long_ad))
> + {
> +   grub_error (GRUB_ERR_BAD_FS, "corrupted UDF file system");
> +   return 0;
> + }
> +
>struct grub_udf_long_ad *ad = (struct grub_udf_long_ad *) ptr;
>  
>filebytes = fileblock * U32 (node->data->lvd.bsize);
> @@ -611,6 +636,12 @@ grub_udf_read_block (grub_fshelp_node_t node, 
> grub_disk_addr_t fileblock)
> filebytes -= adlen;
> ad++;
> len -= sizeof (struct grub_udf_long_ad);
> +
> +   if (GRUB_UDF_INVALID_STRUCT_PTR(ad, struct grub_udf_long_ad))
> + {
> +   grub_error (GRUB_ERR_BAD_FS, "corrupted UDF file system");
> +   return 0;
> + }
>   }
>  }
>  
> @@ -630,6 +661,7 @@ grub_udf_read_file (grub_fshelp_node_t node,
>  case GRUB_UDF_ICBTAG_FLAG_AD_IN_ICB:
>{
>   char *ptr;
> + char *end_ptr = (char *) node + get_fshelp_size (node->data);
>  
>   ptr = ((U16 (node->block.fe.tag.tag_ident) == GRUB_UDF_TAG_IDENT_FE) ?
>  ((char *) &node->block.fe.ext_attr[0]
> @@ -637,6 +669,12 @@ grub_udf_read_file (grub_fshelp_node_t node,
>  ((char *) &node->block.efe.ext_attr[0]
>  + U32 (node->block.efe.ext_attr_length)));
>  
> + if ((ptr + pos + len) > end_ptr)
> +   {
> + grub_error (GRUB_ERR_BAD_FS, "corrupted UDF file system");
> + return 0;
> +   }
> +
>   grub_memcpy (buf, ptr + pos, len);
>  
>   return len;
> -- 
> 2.39.1

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


Re: [PATCH] osdep/linux: Fix md array device enumeration

2023-06-07 Thread Kees Cook
On Wed, Jun 07, 2023 at 03:39:24PM +0200, Daniel Kiper wrote:
> On Tue, Jun 06, 2023 at 11:02:31AM -0700, Kees Cook wrote:
> > On Tue, Jun 6, 2023 at 10:27 AM Julian Andres Klode
> >  wrote:
> > >
> > > On Tue, Jun 06, 2023 at 07:09:26PM +0200, Daniel Kiper wrote:
> > > > On Tue, Jun 06, 2023 at 06:15:27PM +0200, Julian Andres Klode wrote:
> > > > > On Tue, Jun 06, 2023 at 06:10:21PM +0200, Julian Andres Klode wrote:
> > > > [...]
> > > > This patch is in upstream as commit c39f27cd6 (osdep/linux: Fix md array
> > > > device enumeration).
> >
> > Oh good. I really thought it had landed already, so thanks for
> > checking. I got worried this morning when I saw the email to
> > grub-devel. :P "Wasn't that fixed already?" :) But thank you for
> > making sure it hadn't gotten lost! Is there a way to close the tracker
> > item for it?
> 
> I think you should be able to do that.

Ah-ha, yes, I've closed it now. :)
https://salsa.debian.org/grub-team/grub/-/merge_requests/23

> 
> > > [...]
> > > > I realized right now that MD_MAX_DISKS defined in commit c39f27cd6
> > > > (osdep/linux: Fix md array device enumeration) is not in sync with
> > > > commit 2a5e3c1f2 (disk/diskfilter: Don't make a RAID array with more
> > > > than 1024 disks). I think we should sync both numbers down to 1024...
> > >
> > > +1
> >
> > Yeah, seems reasonable, though as I hinted in the original patch, this
> > number appeared to have been arbitrarily chosen by mdadm at the time.
> 
> OK, we will bump it to 4096 as well.

Yeah, I think _technically_ it can be higher than 1024, though ... I
struggle to imagine this for a boot device. ;)

-- 
Kees Cook

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