fbcon: Fix segfault on re-orientation
Subsystems (3)
Thomas Zimmermann
maintainercover fbcon: Fix segfault on re-orientation
fbcon_init() re-rotates the console even when initializing a hidden console. The rotated console then accesses its screen buffer with regular bit ops. This results in a segmentation fault. Fix this by resetting rotation only on new and visible consoles.
The problem can be reproduced by rotating the console on bochs and shutting down the system. When plymouth opens the terminal, it triggers the crash.
Patch 1 fixes the bug. I used an LLM to debug the issue and create the fix. This goes first to simplify backporting.
Patch 2 slightly refactors the fix.
Thomas Zimmermann (2):
fbcon: Don't change shared blitting state for hidden consoles
fbcon: Provide a single implementation of fbcon_set_rotation()
drivers/video/fbdev/core/fbcon.c | 43 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-)
Signature · 1 line
2.56.1
Thomas Zimmermann
maintainerPATCH 1/2 fbcon: Don't change shared blitting state for hidden consoles
All consoles on a framebuffer share par->rotate and par->bitops. fbcon_init() calls set_blitting_type() for each new console, which resets both to the new console's rotation. If the visible console is rotated, it then keeps its rotated rows and columns but draws with unrotated bitops. The next cursor update or scroll writes beyond the framebuffer and oopses, e.g. at shutdown when plymouthd opens a new VT:
BUG: unable to handle page fault for address: ffffd2ce80bd5000 RIP: 0010:sys_imageblit+0x707/0x7e0 soft_cursor+0x1a4/0x250 bit_cursor+0x3ab/0x550 hide_cursor+0x27/0xb0
Only call set_blitting_type() from fbcon_init() for the visible console or when the fbcon state was just created. Compute a new console's rows and columns from its own rotation instead of par->rotate, so that it doesn't inherit the visible console's geometry.
Tested on QEMU with console 0 rotated by 90 degrees. Other rotations and rotate_all are untested.
--- drivers/video/fbdev/core/fbcon.c | 38 +++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-)
drivers/video/fbdev/core/fbcon.c +28 -10
@@ -197,15 +197,20 @@ static void fbcon_set_all_vcs(struct fb_info *info);static struct device *fbcon_device;#ifdef CONFIG_FRAMEBUFFER_CONSOLE_ROTATION +/* Returns the rotation that a console uses when it is displayed. */ +static int fbcon_display_rotation (const struct fb_info *info,const struct fbcon_display *p) +{ +if (!(info->flags &FBINFO_MISC_TILEBLITTING ) && p->con_rotate <4 ) +return p->con_rotate; + +return FB_ROTATE_UR ; +} +static inline void fbcon_set_rotation (struct fb_info *info) {struct fbcon_par *par = info->fbcon_par; -if (!(info->flags &FBINFO_MISC_TILEBLITTING ) && - par->p->con_rotate <4 ) - par->rotate = par->p->con_rotate; -else - par->rotate =0 ; + par->rotate =fbcon_display_rotation (info, par->p); }static void fbcon_rotate (struct fb_info *info,u32 rotate)
@@ -253,6 +258,11 @@ static void fbcon_rotate_all(struct fb_info *info, u32 rotate) fbcon_set_all_vcs(info); }#else +static int fbcon_display_rotation (const struct fb_info *info,const struct fbcon_display *p) +{ +return FB_ROTATE_UR ; +} +static inline void fbcon_set_rotation (struct fb_info *info) {struct fbcon_par *par = info->fbcon_par;
@@ -1105,6 +1115,8 @@ static void fbcon_init(struct vc_data *vc, bool init)struct vc_data *svc = *default_mode;struct fbcon_display *t, *p = &fb_display[vc->vc_num];int logo =1 , new_rows, new_cols, rows, cols; +bool new_par = false; +int rotate;int ret;if (WARN_ON (info_idx ==-1 ))
@@ -1125,9 +1137,11 @@ static void fbcon_init(struct vc_data *vc, bool init)if (var_to_display (p, &info->var, info))return ; -if (!info->fbcon_par) +if (!info->fbcon_par) {if (con2fb_acquire_newinfo (vc, info, vc->vc_num))return ; + new_par = true; + }/* If we are not the first console on this fb, copy the font from that console */
@@ -1185,12 +1199,15 @@ static void fbcon_init(struct vc_data *vc, bool init)if (p->con_rotate ==-1 ) p->con_rotate =FB_ROTATE_UR ; -set_blitting_type (vc, info); +if (new_par ||con_is_visible (vc)) +set_blitting_type (vc, info); cols = vc->vc_cols; rows = vc->vc_rows; - new_cols =FBCON_SWAP (par->rotate, info->var.xres, info->var.yres); - new_rows =FBCON_SWAP (par->rotate, info->var.yres, info->var.xres); +/* Use the console's own rotation; par->rotate belongs to the visible one. */ + rotate =fbcon_display_rotation (info, p); + new_cols =FBCON_SWAP (rotate, info->var.xres, info->var.yres); + new_rows =FBCON_SWAP (rotate, info->var.yres, info->var.xres); new_cols /= vc->vc_font.width; new_rows /= vc->vc_font.height;
@@ -1238,7 +1255,8 @@ static void fbcon_init(struct vc_data *vc, bool init)if (logo)fbcon_prepare_logo (vc, info, cols, rows, new_cols, new_rows); -if (par->bitops->rotate_font && par->bitops->rotate_font (info, vc)) { +if ((new_par ||con_is_visible (vc)) && + par->bitops->rotate_font && par->bitops->rotate_font (info, vc)) { par->rotate =FB_ROTATE_UR ;set_blitting_type (vc, info); }
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 1/2] fbcon: Don't change shared blitting state for hidden consoles
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] fbcon: use of shared par->rotate for hidden consoles in resize and font paths - [High] fbcon: NULL…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] fbcon: use of shared par->rotate for hidden consoles in resize and font paths
- [High] fbcon: NULL par->bitops in fbcon_init() after set_con2fb_map() pre-allocation
- [High] fbcon: out-of-bounds write in fbcon_cursor() when called on a hidden console
- [Low] fbcon: missing Fixes trailer for console rotation bug in fbcon_init()
--
commit a2ce0347bbf7ab81f5e250720e0cef883fe6a77b
Author: Thomas Zimmermann <[email protected]>
Quoted 21 lines
fbcon: Don't change shared blitting state for hidden consoles
All consoles on a framebuffer share
par->rotateandpar->bitops.fbcon_init()callsset_blitting_type()for each new console, which resets both to the new console's rotation. If the visible console is rotated, it then keeps its rotated rows and columns but draws with unrotated bitops. The next cursor update or scroll writes beyond the framebuffer and oopses, e.g. at shutdown when plymouthd opens a new VT:BUG: unable to handle page fault for address: ffffd2ce80bd5000 RIP: 0010:sys_imageblit+0x707/0x7e0 soft_cursor+0x1a4/0x250 bit_cursor+0x3ab/0x550 hide_cursor+0x27/0xb0Only call
set_blitting_type()fromfbcon_init()for the visible console or when the fbcon state was just created. Compute a new console's rows and columns from its own rotation instead ofpar->rotate, so that it doesn't inherit the visible console's geometry.
Quoted 4 lines
Tested on QEMU with console 0 rotated by 90 degrees. Other rotations and
rotate_allare untested.Signed-off-by Thomas Zimmermann <[email protected]>
[Severity: Low]
Should this bug fix include a Fixes tag?
Quoted 4 lines
drivers/video/fbdev/core/fbcon.c +0
-0
[ ... ]
drivers/video/fbdev/core/fbcon.c static void fbcon_init(struct vc_data *vc, bool init) · line 1147
Quoted 10 lines
drivers/video/fbdev/core/fbcon.c +3
-1@@ -1128,9 +1140,11 @@ static void fbcon_init(struct vc_data *vc, bool init)if (var_to_display (p, &info->var, info))return ; -if (!info->fbcon_par) +if (!info->fbcon_par) {if (con2fb_acquire_newinfo (vc, info, vc->vc_num))return ; + new_par = true; + }
[Severity: High]
Can par->bitops remain NULL here if an unallocated console was first mapped to a secondary framebuffer via set_con2fb_map()?
When set_con2fb_map() maps an unallocated VT (vc == NULL) to a new framebuffer, con2fb_acquire_newinfo() allocates info->fbcon_par via fbcon_open() without calling set_blitting_type(). When that background VT is later allocated while another console is visible, fbcon_init() sees info->fbcon_par != NULL (so new_par is false) and !con_is_visible(vc), skipping set_blitting_type() and leaving par->bitops NULL. A subsequent hide_cursor() on that hidden console (for example via KDFONTOP) then dereferences NULL at par->bitops->cursor in fbcon_cursor().
Checking !par->bitops instead of tracking new_par would ensure the blitting state is initialized whenever par->bitops has not been set yet.
[ ... ]
Quoted 8 lines
drivers/video/fbdev/core/fbcon.c +2
-1@@ -1188,12 +1202,15 @@ static void fbcon_init(struct vc_data *vc, bool init)if (p->con_rotate ==-1 ) p->con_rotate =FB_ROTATE_UR ; -set_blitting_type (vc, info); +if (new_par ||con_is_visible (vc)) +set_blitting_type (vc, info);
drivers/video/fbdev/core/fbcon.c static void fbcon_init(struct vc_data *vc, bool init) · line 1215
Quoted 10 lines
drivers/video/fbdev/core/fbcon.c +4
-2cols = vc->vc_cols; rows = vc->vc_rows; - new_cols =FBCON_SWAP (par->rotate, info->var.xres, info->var.yres); - new_rows =FBCON_SWAP (par->rotate, info->var.yres, info->var.xres); +/* Use the console's own rotation; par->rotate belongs to the visible one. */ + rotate =fbcon_display_rotation (info, p); + new_cols =FBCON_SWAP (rotate, info->var.xres, info->var.yres); + new_rows =FBCON_SWAP (rotate, info->var.yres, info->var.xres); new_cols /= vc->vc_font.width; new_rows /= vc->vc_font.height;
[Severity: High]
What happens in the resize and font paths for a hidden console now that par->rotate stays set to the visible console's rotation?
When fbcon_init() is called with init == false and runs vc_resize(vc, new_cols, new_rows) (or when TIOCSWINSZ resizes a hidden console), fbcon_resize() and updatescrollmode() still use FBCON_SWAP(par->rotate, ...) instead of fbcon_display_rotation(info, p). If the visible console is rotated 90 or 270 degrees relative to the hidden console on a non-square display, fbcon_resize() checks new_cols and new_rows against swapped axes and fails with -EINVAL.
Similarly, changing font dimensions on a hidden console in fbcon_do_set_font() skips set_blitting_type() when !con_is_visible(vc) but computes cols and rows from FBCON_SWAP(par->rotate, ...):
drivers/video/fbdev/core/fbcon.c:fbcon_do_set_font() {
...
if (resize) {
int cols, rows;
cols = FBCON_SWAP(par->rotate, info->var.xres, info->var.yres);
rows = FBCON_SWAP(par->rotate, info->var.yres, info->var.xres);
cols /= w;
rows /= h;
ret = vc_resize(vc, cols, rows);
...
}That resizes the hidden console to the visible console's rotated geometry while its own p->con_rotate stays unrotated. Switching to that console later calls set_blitting_type() in fbcon_switch() without resizing vc_cols/vc_rows, leaving rotated dimensions with unrotated bitops and causing sys_imageblit() to write past info->screen_base.
[Severity: High]
Can calling hide_cursor() on a hidden console also write out of bounds in fbcon_cursor()?
With this change, a hidden console has vc_cols and vc_rows sized for its own rotation while par->bitops holds the visible console's rotated bitops. Because neither hide_cursor() nor fbcon_cursor() checks con_is_visible(vc), operations on a background VT that call hide_cursor(vc) (such as con_font_set() or writing cursor coordinates to /dev/vcsaN) invoke par->bitops->cursor() with the hidden console's vc->state.x and vc->state.y. When the visible console is rotated 90 or 270 degrees and the hidden console's cursor position exceeds the rotated axis limit, cw_cursor() or ccw_cursor() computes an out-of-bounds (or wrapped negative) dy and calls soft_cursor() -> sys_imageblit(), writing past info->screen_base.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Thomas Zimmermann
maintainerPATCH 2/2 fbcon: Provide a single implementation of fbcon_set_rotation()
In reply to Thomas ZimmermannMerge both implmentations of fbcon_set_rotation() into one. In the case of CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=n, fbcon_display_rotation() returns FB_ROTATE_UR. So the new code is equivalent to the existing code.
--- drivers/video/fbdev/core/fbcon.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
drivers/video/fbdev/core/fbcon.c +7 -14
@@ -206,13 +206,6 @@ static int fbcon_display_rotation(const struct fb_info *info, const struct fbconreturn FB_ROTATE_UR ; } -static inline void fbcon_set_rotation (struct fb_info *info) -{ -struct fbcon_par *par = info->fbcon_par; - - par->rotate =fbcon_display_rotation (info, par->p); -} -static void fbcon_rotate (struct fb_info *info,u32 rotate) {struct fbcon_par *par = info->fbcon_par;
@@ -263,13 +256,6 @@ static int fbcon_display_rotation(const struct fb_info *info, const struct fbconreturn FB_ROTATE_UR ; } -static inline void fbcon_set_rotation (struct fb_info *info) -{ -struct fbcon_par *par = info->fbcon_par; - - par->rotate =FB_ROTATE_UR ; -} -static void fbcon_rotate (struct fb_info *info,u32 rotate) {return ;
@@ -281,6 +267,13 @@ static void fbcon_rotate_all(struct fb_info *info, u32 rotate) }#endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */ +static void fbcon_set_rotation (struct fb_info *info) +{ +struct fbcon_par *par = info->fbcon_par; + + par->rotate =fbcon_display_rotation (info, par->p); +} +static void fbcon_set_bitops (struct fbcon_par *par) {switch (par->rotate) {
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 2/2] fbcon: Provide a single implementation of fbcon_set_rotation()
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] fbcon: unrotated mode fallback…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] fbcon: unrotated mode fallback clobbered by set_blitting_type()
[1]
--
commit b31c406a9ea618e6fff12f41e21bd7c311a16303
Author: Thomas Zimmermann <[email protected]>
fbcon: Provide a single implementation of fbcon_set_rotation()
This commit merges the CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y and =n implementations of fbcon_set_rotation() into a single function calling fbcon_display_rotation().
Quoted 4 lines
drivers/video/fbdev/core/fbcon.c +0
-0
[ ... ]
drivers/video/fbdev/core/fbcon.c static void fbcon_rotate_all(struct fb_info *info, u32 rotate) · line 276
Quoted 11 lines
drivers/video/fbdev/core/fbcon.c +7
-0@@ -281,6 +267,13 @@ static void fbcon_rotate_all(struct fb_info *info, u32 rotate) }#endif /* CONFIG_FRAMEBUFFER_CONSOLE_ROTATION */ +static void fbcon_set_rotation (struct fb_info *info) +{ +struct fbcon_par *par = info->fbcon_par; + + par->rotate =fbcon_display_rotation (info, par->p); +} +
[Severity: High]
This problem wasn't introduced by this patch, but doesn't fbcon_set_rotation() clobber the unrotated fallback when font rotation allocation fails [1]?
When par->bitops->rotate_font() fails in fbcon_switch(), fbcon_init() or fbcon_modechanged(), the caller falls back to unrotated mode by setting par->rotate = FB_ROTATE_UR and calling set_blitting_type():
drivers/video/fbdev/core/fbcon.c:fbcon_switch() {
...
if (par->bitops->rotate_font && par->bitops->rotate_font(info, vc)) {
par->rotate = FB_ROTATE_UR;
set_blitting_type(vc, info);
}
...
}set_blitting_type() then calls fbcon_set_rotation(info) followed by fbcon_set_bitops(par), which overwrites par->rotate with fbcon_display_rotation(info, par->p) (derived from par->p->con_rotate) and reinstalls the rotated bitops without an allocated rotated font buffer, leaving the console blank.
[1] https://sashiko.dev/#/bug/linux-752875af-5863-44fb-8a6c-f76bb439e2f3
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2