>
> Hello Mike,
>
> Thank you for addressing the framebuffer pitch issue. I have reviewed the
> patch and have a few additional observations below.
>
> Reported by: Alperen Erkan <[email protected]>
> ---
> i386/i386at/fb_disp.c | 23 ++++++++++++++++++-----
> i386/i386at/fb_disp.h | 4 +++-
> i386/i386at/immc.c | 9 ++++++---
> 3 files changed, 27 insertions(+), 9 deletions(-)
>
> diff --git a/i386/i386at/fb_disp.c b/i386/i386at/fb_disp.c
> index 315bcce2..0b00e5a5 100644
> --- a/i386/i386at/fb_disp.c
> +++ b/i386/i386at/fb_disp.c
> @@ -120,6 +120,7 @@ static uint8_t vgafont14[] = {
>
> static void *fb_addr;
> static uint32_t fb_pixels_w, fb_pixels_h;
> +static uint32_t fb_pitch;
> static uint32_t fb_chars_w, fb_chars_h;
> static uint8_t *fb_chars_buf;
> static uint8_t fb_bpp; /* bytes per pixel (not bits) */
> @@ -137,7 +138,9 @@ static const uint32_t fb_cursor_h = 2;
> #define fb_colbyte_black 0x00
>
> boolean_t
> -fb_disp_init (void *addr, uint32_t width, uint32_t height, uint8_t bpp)
> +fb_disp_init (void *addr,
> + uint32_t pitch, uint32_t width, uint32_t height,
> + uint8_t bpp)
> {
> if (bpp != 24 && bpp != 32)
> return FALSE;
> @@ -148,6 +151,10 @@ fb_disp_init (void *addr, uint32_t width, uint32_t
> height, uint8_t bpp)
>
> fb_addr = addr;
>
> + if (pitch < (fb_pixels_w * fb_bpp))
> + return FALSE;
> +
> + fb_pitch = pitch;
> fb_chars_w = fb_disp_max_cols ();
> fb_chars_h = fb_disp_max_rows ();
>
> The multiplication in the pitch validation is performed in 32-bit unsigned
> arithmetic and may overflow for sufficiently large framebuffer widths. This
> could allow an invalid framebuffer specification to pass the validation.
>
> I suggest promoting fb_pixels_w to uint64_t before multiplication.
>
> Additionally, would it be preferable to perform this validation before
> assigning fb_addr, to avoid partially updating the framebuffer state upon
> failure?
>
> @@ -186,7 +193,7 @@ inline
> static uint8_t *
> fb_xyb2addr (short x, short y)
> {
> - return (fb_addr + (fb_bpp * (x + (y * fb_pixels_w))));
> + return (fb_addr + (fb_bpp * x) + (y * fb_pitch));
> }
>
> uint32_t
>
> The expression y * fb_pitch is also evaluated in 32-bit unsigned
> arithmetic, including on x86_64.
>
> Would it be preferable to use size_t for the offset calculation? Whether
> an overflow is actually reachable depends on the existing coordinate and
> framebuffer mapping constraints.
>
> @@ -237,7 +244,7 @@ fb_paint_cursor (fb_disp_pos_t pos, uint8_t colour)
> addr[2] = colour;
> addr += fb_bpp;
> }
> - addr += (fb_bpp * (fb_pixels_w - fb_font_w));
> + addr += (fb_pitch - (fb_font_w * fb_bpp));
> }
> }
>
> @@ -269,7 +276,7 @@ fb_drawchar (fb_disp_pos_t x, fb_disp_pos_t y, char ch)
> to[2] = colour;
> to += fb_bpp;
> }
> - to += (fb_bpp * (fb_pixels_w - fb_font_w));
> + to += (fb_pitch - (fb_font_w * fb_bpp));
> from++;
> }
> }
> @@ -382,5 +389,11 @@ fb_disp_cls (void)
> {
> assert (fb_addr != NULL);
>
> - memset (fb_addr, fb_colbyte_black, fb_pixels_w * fb_pixels_h * fb_bpp);
> + void *addr = fb_addr;
> +
> + for (uint32_t i = 0; i < fb_pixels_h; i++)
> + {
> + memset (addr, fb_colbyte_black, fb_pixels_w * fb_bpp);
> + addr += fb_pitch;
> + }
> }
> diff --git a/i386/i386at/fb_disp.h b/i386/i386at/fb_disp.h
> index dd154eb6..e86bcfa6 100644
> --- a/i386/i386at/fb_disp.h
> +++ b/i386/i386at/fb_disp.h
> @@ -51,7 +51,9 @@ typedef short fb_disp_pos_t;
> /* Initialise the display with frame buffer's virtual address and
> pixel dimensions. bpp must be 24 or 32. */
> boolean_t
> -fb_disp_init (void *v_addr, uint32_t width, uint32_t height, uint8_t bpp);
> +fb_disp_init (void *v_addr,
> + uint32_t pitch, uint32_t width, uint32_t height,
> + uint8_t bpp);
>
> /* The maximum number of character columns and rows supported by the
> frame buffer pixel dimensions. */
>
> Minor documentation point: please update the function comment to describe
> the new pitch parameter and specify that it represents the number of
> bytes per scanline, including any padding.
>
> diff --git a/i386/i386at/immc.c b/i386/i386at/immc.c
> index c1dfe606..6e4d9661 100644
> --- a/i386/i386at/immc.c
> +++ b/i386/i386at/immc.c
> @@ -171,11 +171,12 @@ immc_l1_tables[IMMC_FB_NUM_PAGETABLES][NPTES]
> __aligned(PAGE_SIZE);
>
> static void
> immc_enable_fb(const uint64_t paddr,
> + const uint32_t pitch,
> const uint32_t width,
> const uint32_t height,
> const uint8_t bpp)
> {
> - uint64_t fb_size = (width * height * (bpp / 8));
> + uint64_t fb_size = ((uint64_t)pitch * (uint64_t)height);
>
> const uint64_t paddr_end = round_page (paddr + fb_size);
> uint64_t paddr_map = trunc_page (paddr);
>
> Although the framebuffer size multiplication is now performed in 64-bit
> arithmetic, the subsequent addition of paddr and fb_size, as well as page
> rounding, may still overflow for sufficiently large or malformed
> framebuffer specifications.
>
> Should these operations also be subject to overflow validation before
> proceeding with the mapping?
>
> @@ -211,7 +212,7 @@ immc_enable_fb(const uint64_t paddr,
> immc_l3_table[0] = (pa_to_pte (_kvtophys (&immc_l2_table))
> | INTEL_PTE_VALID | INTEL_PTE_WRITE);
>
> - uint32_t mapped_height = ((paddr_map - paddr) / width / (bpp / 8));
> + uint32_t mapped_height = ((paddr_map - paddr) / pitch);
>
> if (mapped_height > height)
> mapped_height = height;
>
> A zero pitch would result in a division by zero before the validation in
> fb_disp_init() is reached.
>
> However, I understand from the cover letter that patch 2/3 addresses such
> framebuffer validation issues. I will revisit this point when reviewing the
> subsequent patch.
>
> @@ -224,7 +225,7 @@ immc_enable_fb(const uint64_t paddr,
> (pa_to_pte (_kvtophys (&immc_l3_table))
> | INTEL_PTE_VALID | INTEL_PTE_WRITE);
>
> - if (fb_disp_init (IMMC_FB_VADDR, width, mapped_height, bpp))
> + if (fb_disp_init (IMMC_FB_VADDR, pitch, width, mapped_height, bpp))
> {
> immc_rows = fb_disp_max_rows();
> immc_cols = fb_disp_max_cols();
> @@ -261,6 +262,7 @@ immc_enable(const void *mbi, const uint32_t magic)
> && mb1->fb_info.framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB)
> {
> immc_enable_fb(mb1->fb_info.framebuffer_addr,
> + mb1->fb_info.framebuffer_pitch,
> mb1->fb_info.framebuffer_width,
> mb1->fb_info.framebuffer_height,
> mb1->fb_info.framebuffer_bpp);
> @@ -288,6 +290,7 @@ immc_enable(const void *mbi, const uint32_t magic)
> == MULTIBOOT2_FRAMEBUFFER_TYPE_RGB)
> {
> immc_enable_fb(fb_tag->common.framebuffer_addr,
> + fb_tag->common.framebuffer_pitch,
> fb_tag->common.framebuffer_width,
> fb_tag->common.framebuffer_height,
> fb_tag->common.framebuffer_bpp);
>
> Apart from the observations above, the overall approach appears sound and
> addresses the original framebuffer pitch concern.
>
> As usual, this review is based on static analysis. I have not compiled or
> tested the patch at runtime.
>
> Best regards,
> Alperen Erkan
>