Skip to content

Commit aeb8441

Browse files
keessuryasaimadhu
authored andcommitted
x86/boot: Wrap literal addresses in absolute_pointer()
GCC 11 (incorrectly[1]) assumes that literal values cast to (void *) should be treated like a NULL pointer with an offset, and raises diagnostics when doing bounds checking under -Warray-bounds. GCC 12 got "smarter" about finding these: In function 'rdfs8', inlined from 'vga_recalc_vertical' at /srv/code/arch/x86/boot/video-mode.c:124:29, inlined from 'set_mode' at /srv/code/arch/x86/boot/video-mode.c:163:3: /srv/code/arch/x86/boot/boot.h:114:9: warning: array subscript 0 is outside array bounds of 'u8[0]' {aka 'unsigned char[]'} [-Warray-bounds] 114 | asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr)); | ^~~ This has been solved in other places[2] already by using the recently added absolute_pointer() macro. Do the same here. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578 [2] https://lore.kernel.org/all/[email protected]/ Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 9b5a7f4 commit aeb8441

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

arch/x86/boot/boot.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,66 +110,78 @@ typedef unsigned int addr_t;
110110

111111
static inline u8 rdfs8(addr_t addr)
112112
{
113+
u8 *ptr = (u8 *)absolute_pointer(addr);
113114
u8 v;
114-
asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
115+
asm volatile("movb %%fs:%1,%0" : "=q" (v) : "m" (*ptr));
115116
return v;
116117
}
117118
static inline u16 rdfs16(addr_t addr)
118119
{
120+
u16 *ptr = (u16 *)absolute_pointer(addr);
119121
u16 v;
120-
asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
122+
asm volatile("movw %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
121123
return v;
122124
}
123125
static inline u32 rdfs32(addr_t addr)
124126
{
127+
u32 *ptr = (u32 *)absolute_pointer(addr);
125128
u32 v;
126-
asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
129+
asm volatile("movl %%fs:%1,%0" : "=r" (v) : "m" (*ptr));
127130
return v;
128131
}
129132

130133
static inline void wrfs8(u8 v, addr_t addr)
131134
{
132-
asm volatile("movb %1,%%fs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
135+
u8 *ptr = (u8 *)absolute_pointer(addr);
136+
asm volatile("movb %1,%%fs:%0" : "+m" (*ptr) : "qi" (v));
133137
}
134138
static inline void wrfs16(u16 v, addr_t addr)
135139
{
136-
asm volatile("movw %1,%%fs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
140+
u16 *ptr = (u16 *)absolute_pointer(addr);
141+
asm volatile("movw %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
137142
}
138143
static inline void wrfs32(u32 v, addr_t addr)
139144
{
140-
asm volatile("movl %1,%%fs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
145+
u32 *ptr = (u32 *)absolute_pointer(addr);
146+
asm volatile("movl %1,%%fs:%0" : "+m" (*ptr) : "ri" (v));
141147
}
142148

143149
static inline u8 rdgs8(addr_t addr)
144150
{
151+
u8 *ptr = (u8 *)absolute_pointer(addr);
145152
u8 v;
146-
asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*(u8 *)addr));
153+
asm volatile("movb %%gs:%1,%0" : "=q" (v) : "m" (*ptr));
147154
return v;
148155
}
149156
static inline u16 rdgs16(addr_t addr)
150157
{
158+
u16 *ptr = (u16 *)absolute_pointer(addr);
151159
u16 v;
152-
asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*(u16 *)addr));
160+
asm volatile("movw %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
153161
return v;
154162
}
155163
static inline u32 rdgs32(addr_t addr)
156164
{
165+
u32 *ptr = (u32 *)absolute_pointer(addr);
157166
u32 v;
158-
asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*(u32 *)addr));
167+
asm volatile("movl %%gs:%1,%0" : "=r" (v) : "m" (*ptr));
159168
return v;
160169
}
161170

162171
static inline void wrgs8(u8 v, addr_t addr)
163172
{
164-
asm volatile("movb %1,%%gs:%0" : "+m" (*(u8 *)addr) : "qi" (v));
173+
u8 *ptr = (u8 *)absolute_pointer(addr);
174+
asm volatile("movb %1,%%gs:%0" : "+m" (*ptr) : "qi" (v));
165175
}
166176
static inline void wrgs16(u16 v, addr_t addr)
167177
{
168-
asm volatile("movw %1,%%gs:%0" : "+m" (*(u16 *)addr) : "ri" (v));
178+
u16 *ptr = (u16 *)absolute_pointer(addr);
179+
asm volatile("movw %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
169180
}
170181
static inline void wrgs32(u32 v, addr_t addr)
171182
{
172-
asm volatile("movl %1,%%gs:%0" : "+m" (*(u32 *)addr) : "ri" (v));
183+
u32 *ptr = (u32 *)absolute_pointer(addr);
184+
asm volatile("movl %1,%%gs:%0" : "+m" (*ptr) : "ri" (v));
173185
}
174186

175187
/* Note: these only return true/false, not a signed return value! */

arch/x86/boot/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void copy_boot_params(void)
3333
u16 cl_offset;
3434
};
3535
const struct old_cmdline * const oldcmd =
36-
(const struct old_cmdline *)OLD_CL_ADDRESS;
36+
absolute_pointer(OLD_CL_ADDRESS);
3737

3838
BUILD_BUG_ON(sizeof(boot_params) != 4096);
3939
memcpy(&boot_params.hdr, &hdr, sizeof(hdr));

0 commit comments

Comments
 (0)