Skip to contentSkip to conversation

x86: limit the fs segment to the pointer size

PATCH New Masahiro Yamada 3 participants 4 messages Jan 8, 2020 – Feb 3, 2020 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: limit the fs segment to the pointer size

The fs segment is only used to get the global data pointer. If it is accessed beyond sizeof(new_gd->arch.gd_addr), it is a bug.

To specify the byte-granule limit size, drop the G bit, so the flag field is 0x8093 instead of 0xc093, and set the limit field to sizeof(new_gd->arch.gd_addr) - 1.

---

 arch/x86/cpu/i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/cpu/i386/cpu.c +3 -2
index 2b27617ca3a4..72fefdd3adca 100644
@@ -137,8 +137,9 @@ void arch_setup_gd(gd_t *new_gd)
 
 	/* FS: data, read/write, 4 GB, base (Global Data Pointer) */
 	new_gd->arch.gd_addr = new_gd;
-	gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
-		     (ulong)&new_gd->arch.gd_addr, 0xfffff);
+	gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
+					(ulong)&new_gd->arch.gd_addr,
+					sizeof(new_gd->arch.gd_addr) - 1);
 
 	/* 16-bit CS: code, read/execute, 64 kB, base 0 */
 	gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 x86: limit the fs segment to the pointer size

Quoted 8 lines · Masahiro Yamada

The fs segment is only used to get the global data pointer. If it is accessed beyond sizeof(new_gd->arch.gd_addr), it is a bug.

To specify the byte-granule limit size, drop the G bit, so the flag field is 0x8093 instead of 0xc093, and set the limit field to sizeof(new_gd->arch.gd_addr) - 1.

Quoted 5 lines

---

 arch/x86/cpu/i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
#2 lore raw

PATCH 1/1 x86: limit the fs segment to the pointer size

In reply to Masahiro Yamada
Quoted 19 lines · Masahiro Yamada

The fs segment is only used to get the global data pointer. If it is accessed beyond sizeof(new_gd->arch.gd_addr), it is a bug.

To specify the byte-granule limit size, drop the G bit, so the flag field is 0x8093 instead of 0xc093, and set the limit field to sizeof(new_gd->arch.gd_addr) - 1.

---

 arch/x86/cpu/i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/cpu/i386/cpu.c +0 -0
index 2b27617ca3a4..72fefdd3adca 100644

arch/x86/cpu/i386/cpu.c void arch_setup_gd(gd_t *new_gd) · line 138

Quoted 2 lines
arch/x86/cpu/i386/cpu.c +0 -0
@@ -137,8 +137,9 @@ void arch_setup_gd(gd_t *new_gd)
 
        /* FS: data, read/write, 4 GB, base (Global Data Pointer) */

nits: this comment should be updated too

arch/x86/cpu/i386/cpu.c void arch_setup_gd(gd_t *new_gd)

Quoted 10 lines
arch/x86/cpu/i386/cpu.c +3 -3
        new_gd->arch.gd_addr = new_gd;
-       gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
-                    (ulong)&new_gd->arch.gd_addr, 0xfffff);
+       gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
+                                       (ulong)&new_gd->arch.gd_addr,
+                                       sizeof(new_gd->arch.gd_addr) - 1);
 
        /* 16-bit CS: code, read/execute, 64 kB, base 0 */
        gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
--
#3 lore raw

PATCH 1/1 x86: limit the fs segment to the pointer size

Quoted 22 lines · Bin Meng
Quoted 20 lines · Masahiro Yamada

The fs segment is only used to get the global data pointer. If it is accessed beyond sizeof(new_gd->arch.gd_addr), it is a bug.

To specify the byte-granule limit size, drop the G bit, so the flag field is 0x8093 instead of 0xc093, and set the limit field to sizeof(new_gd->arch.gd_addr) - 1.

---

 arch/x86/cpu/i386/cpu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/cpu/i386/cpu.c +0 -0
index 2b27617ca3a4..72fefdd3adca 100644
@@ -137,8 +137,9 @@ void arch_setup_gd(gd_t *new_gd)
 
Quoted 3 lines

arch/x86/cpu/i386/cpu.c void arch_setup_gd(gd_t *new_gd) · line 138

Quoted 1 line
arch/x86/cpu/i386/cpu.c +0 -0
        /* FS: data, read/write, 4 GB, base (Global Data Pointer) */

nits: this comment should be updated too

Fixed the comments, and

Quoted 8 lines
Quoted 8 lines
arch/x86/cpu/i386/cpu.c +3 -2
        new_gd->arch.gd_addr = new_gd;
-       gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0xc093,
-                    (ulong)&new_gd->arch.gd_addr, 0xfffff);
+       gdt_addr[X86_GDT_ENTRY_32BIT_FS] = GDT_ENTRY(0x8093,
+                                       (ulong)&new_gd->arch.gd_addr,
+                                       sizeof(new_gd->arch.gd_addr) - 1);
 
Quoted 6 lines

arch/x86/cpu/i386/cpu.c void arch_setup_gd(gd_t *new_gd)

Quoted 3 lines
arch/x86/cpu/i386/cpu.c +0 -1
        /* 16-bit CS: code, read/execute, 64 kB, base 0 */
        gdt_addr[X86_GDT_ENTRY_16BIT_CS] = GDT_ENTRY(0x009b, 0, 0x0ffff);
--

applied to u-boot-x86, thanks!