Skip to content

Commit 9741ff9

Browse files
committed
fb: Fix UAF after fb shutdown
fb_console_reserve_lines() in mmu_shutdown() was causing a blit from the freed framebuffer, putting heap metadata junk at the top left corner. Signed-off-by: Hector Martin <[email protected]>
1 parent cdb6d41 commit 9741ff9

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

src/fb.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ void fb_unblit(u32 x, u32 y, u32 w, u32 h, void *data, u32 stride)
144144
{
145145
u8 *p = data;
146146

147+
if (!console.initialized)
148+
return;
149+
147150
for (u32 i = 0; i < h; i++) {
148151
for (u32 j = 0; j < w; j++) {
149152
rgb_t color = fb_get_pixel(x + j, y + i);
@@ -157,6 +160,9 @@ void fb_unblit(u32 x, u32 y, u32 w, u32 h, void *data, u32 stride)
157160

158161
void fb_fill(u32 x, u32 y, u32 w, u32 h, rgb_t color)
159162
{
163+
if (!console.initialized)
164+
return;
165+
160166
u32 c = rgb2pixel_30(color);
161167
for (u32 i = 0; i < h; i++)
162168
memset32(&fb.ptr[x + (y + i) * fb.stride], c, w * 4);
@@ -165,34 +171,52 @@ void fb_fill(u32 x, u32 y, u32 w, u32 h, rgb_t color)
165171

166172
void fb_clear(rgb_t color)
167173
{
174+
if (!console.initialized)
175+
return;
176+
168177
u32 c = rgb2pixel_30(color);
169178
memset32(fb.ptr, c, fb.stride * fb.height * 4);
170179
fb_update();
171180
}
172181

173182
void fb_blit_image(u32 x, u32 y, const struct image *img)
174183
{
184+
if (!console.initialized)
185+
return;
186+
175187
fb_blit(x, y, img->width, img->height, img->ptr, img->width, PIX_FMT_XRGB);
176188
}
177189

178190
void fb_unblit_image(u32 x, u32 y, struct image *img)
179191
{
192+
if (!console.initialized)
193+
return;
194+
180195
fb_unblit(x, y, img->width, img->height, img->ptr, img->width);
181196
}
182197

183198
void fb_blit_logo(const struct image *logo)
184199
{
200+
if (!console.initialized)
201+
return;
202+
185203
fb_blit_image((fb.width - logo->width) / 2, (fb.height - logo->height) / 2, logo);
186204
}
187205

188206
void fb_display_logo(void)
189207
{
208+
if (!console.initialized)
209+
return;
210+
190211
printf("fb: display logo\n");
191212
fb_blit_logo(logo);
192213
}
193214

194215
void fb_restore_logo(void)
195216
{
217+
if (!console.initialized)
218+
return;
219+
196220
if (!orig_logo.ptr)
197221
return;
198222
fb_blit_logo(&orig_logo);
@@ -259,6 +283,9 @@ static void fb_putchar(u8 c)
259283

260284
void fb_console_scroll(u32 n)
261285
{
286+
if (!console.initialized)
287+
return;
288+
262289
u32 row = 0;
263290
n = min(n, console.cursor.row);
264291
for (; row < console.cursor.max_row - n; ++row)
@@ -270,6 +297,9 @@ void fb_console_scroll(u32 n)
270297

271298
void fb_console_reserve_lines(u32 n)
272299
{
300+
if (!console.initialized)
301+
return;
302+
273303
if ((console.cursor.max_row - console.cursor.row) <= n)
274304
fb_console_scroll(1 + n - (console.cursor.max_row - console.cursor.row));
275305
fb_update();
@@ -404,14 +434,14 @@ void fb_shutdown(bool restore_logo)
404434
return;
405435

406436
console.active = false;
407-
console.initialized = false;
408437
fb_clear_console();
409438
if (restore_logo) {
410439
fb_restore_logo();
411440
free(orig_logo.ptr);
412441
orig_logo.ptr = NULL;
413442
}
414443
free(fb.ptr);
444+
console.initialized = false;
415445
}
416446

417447
void fb_reinit(void)

0 commit comments

Comments
 (0)