linux/lib/fonts/fonts.c
Gaosheng Cui 6fe888c4d2 lib/fonts: fix undefined behavior in bit shift for get_default_font
Shifting signed 32-bit value by 31 bits is undefined, so changing
significant bit to unsigned.  The UBSAN warning calltrace like below:

UBSAN: shift-out-of-bounds in lib/fonts/fonts.c:139:20
left shift of 1 by 31 places cannot be represented in type 'int'
 <TASK>
 dump_stack_lvl+0x7d/0xa5
 dump_stack+0x15/0x1b
 ubsan_epilogue+0xe/0x4e
 __ubsan_handle_shift_out_of_bounds+0x1e7/0x20c
 get_default_font+0x1c7/0x1f0
 fbcon_startup+0x347/0x3a0
 do_take_over_console+0xce/0x270
 do_fbcon_takeover+0xa1/0x170
 do_fb_registered+0x2a8/0x340
 fbcon_fb_registered+0x47/0xe0
 register_framebuffer+0x294/0x4a0
 __drm_fb_helper_initial_config_and_unlock+0x43c/0x880 [drm_kms_helper]
 drm_fb_helper_initial_config+0x52/0x80 [drm_kms_helper]
 drm_fbdev_client_hotplug+0x156/0x1b0 [drm_kms_helper]
 drm_fbdev_generic_setup+0xfc/0x290 [drm_kms_helper]
 bochs_pci_probe+0x6ca/0x772 [bochs]
 local_pci_probe+0x4d/0xb0
 pci_device_probe+0x119/0x320
 really_probe+0x181/0x550
 __driver_probe_device+0xc6/0x220
 driver_probe_device+0x32/0x100
 __driver_attach+0x195/0x200
 bus_for_each_dev+0xbb/0x120
 driver_attach+0x27/0x30
 bus_add_driver+0x22e/0x2f0
 driver_register+0xa9/0x190
 __pci_register_driver+0x90/0xa0
 bochs_pci_driver_init+0x52/0x1000 [bochs]
 do_one_initcall+0x76/0x430
 do_init_module+0x61/0x28a
 load_module+0x1f82/0x2e50
 __do_sys_finit_module+0xf8/0x190
 __x64_sys_finit_module+0x23/0x30
 do_syscall_64+0x58/0x80
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
 </TASK>

Link: https://lkml.kernel.org/r/20221031113829.4183153-1-cuigaosheng1@huawei.com
Fixes: c81f717cb9 ("fbcon: Fix typo and bogus logic in get_default_font")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-11-18 13:55:09 -08:00

154 lines
3.0 KiB
C

/*
* `Soft' font definitions
*
* Created 1995 by Geert Uytterhoeven
* Rewritten 1998 by Martin Mares <mj@ucw.cz>
*
* 2001 - Documented with DocBook
* - Brad Douglas <brad@neruo.com>
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#if defined(__mc68000__)
#include <asm/setup.h>
#endif
#include <linux/font.h>
static const struct font_desc *fonts[] = {
#ifdef CONFIG_FONT_8x8
&font_vga_8x8,
#endif
#ifdef CONFIG_FONT_8x16
&font_vga_8x16,
#endif
#ifdef CONFIG_FONT_6x11
&font_vga_6x11,
#endif
#ifdef CONFIG_FONT_7x14
&font_7x14,
#endif
#ifdef CONFIG_FONT_SUN8x16
&font_sun_8x16,
#endif
#ifdef CONFIG_FONT_SUN12x22
&font_sun_12x22,
#endif
#ifdef CONFIG_FONT_10x18
&font_10x18,
#endif
#ifdef CONFIG_FONT_ACORN_8x8
&font_acorn_8x8,
#endif
#ifdef CONFIG_FONT_PEARL_8x8
&font_pearl_8x8,
#endif
#ifdef CONFIG_FONT_MINI_4x6
&font_mini_4x6,
#endif
#ifdef CONFIG_FONT_6x10
&font_6x10,
#endif
#ifdef CONFIG_FONT_TER16x32
&font_ter_16x32,
#endif
#ifdef CONFIG_FONT_6x8
&font_6x8,
#endif
};
#define num_fonts ARRAY_SIZE(fonts)
#ifdef NO_FONTS
#error No fonts configured.
#endif
/**
* find_font - find a font
* @name: string name of a font
*
* Find a specified font with string name @name.
*
* Returns %NULL if no font found, or a pointer to the
* specified font.
*
*/
const struct font_desc *find_font(const char *name)
{
unsigned int i;
BUILD_BUG_ON(!num_fonts);
for (i = 0; i < num_fonts; i++)
if (!strcmp(fonts[i]->name, name))
return fonts[i];
return NULL;
}
EXPORT_SYMBOL(find_font);
/**
* get_default_font - get default font
* @xres: screen size of X
* @yres: screen size of Y
* @font_w: bit array of supported widths (1 - 32)
* @font_h: bit array of supported heights (1 - 32)
*
* Get the default font for a specified screen size.
* Dimensions are in pixels.
*
* Returns %NULL if no font is found, or a pointer to the
* chosen font.
*
*/
const struct font_desc *get_default_font(int xres, int yres, u32 font_w,
u32 font_h)
{
int i, c, cc, res;
const struct font_desc *f, *g;
g = NULL;
cc = -10000;
for (i = 0; i < num_fonts; i++) {
f = fonts[i];
c = f->pref;
#if defined(__mc68000__)
#ifdef CONFIG_FONT_PEARL_8x8
if (MACH_IS_AMIGA && f->idx == PEARL8x8_IDX)
c = 100;
#endif
#ifdef CONFIG_FONT_6x11
if (MACH_IS_MAC && xres < 640 && f->idx == VGA6x11_IDX)
c = 100;
#endif
#endif
if ((yres < 400) == (f->height <= 8))
c += 1000;
/* prefer a bigger font for high resolution */
res = (xres / f->width) * (yres / f->height) / 1000;
if (res > 20)
c += 20 - res;
if ((font_w & (1U << (f->width - 1))) &&
(font_h & (1U << (f->height - 1))))
c += 1000;
if (c > cc) {
cc = c;
g = f;
}
}
return g;
}
EXPORT_SYMBOL(get_default_font);
MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
MODULE_DESCRIPTION("Console Fonts");
MODULE_LICENSE("GPL");