Skip to contentSkip to conversation

drm: Safe font-data access in log/panic drawing

PATCH In review Thomas Zimmermann 2 participants 10 messages May 29 – Jun 2

Subsystems (6)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover drm: Safe font-data access in log/panic drawing

Looking up glyph shapes with a signed char in drm_draw_get_char_bitmap() is unsafe. It also does not support extended ASCII codes with values larger than 127.

Add the new function font_data_glyph_buf() to the font-data helpers. It looks up the correct glyph from font data or returns NULL if no such glyph exists. Convert DRM's log and panic code to the new function. Also cast the character code to support all 256 ASCII characters.

Tested with drm_log on bochs.

Thomas Zimmermann (4):
lib/fonts: Look up glyph data with font_data_glyph_buf() drm/client: log: Look up glyph shape with font helper drm/panic: Look up glyph shape with font helper drm/draw: Remove unused helper drm_draw_get_char_bitmap()

 drivers/gpu/drm/clients/drm_log.c   | 10 ++++++----
 drivers/gpu/drm/drm_draw_internal.h |  7 -------
 drivers/gpu/drm/drm_panic.c         |  6 ++++--
 include/linux/font.h                |  3 +++
 lib/fonts/fonts.c                   | 31 +++++++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 13 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 1/4 lib/fonts: Look up glyph data with font_data_glyph_buf()

Add font_data_glyph_buf() to retrieve a character's glyph data or NULL otherwise. Console fonts can currently contain 256 or 512 glyphs. The kernel-internal characters are of type char, unsigned short or unsigned int. Catch all of them by accepting unsigned int. Callers possibly have to cast from signed to unsigned types to reach all glyphs in a font.

---
 include/linux/font.h |  3 +++
 lib/fonts/fonts.c    | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
include/linux/font.h +3 -0
index 6845f02d739a..ea23b727388b 100644
@@ -101,6 +101,9 @@ font_data_t *font_data_import(const struct console_font *font, unsigned int vpit
 void font_data_get(font_data_t *fd);
 bool font_data_put(font_data_t *fd);
 unsigned int font_data_size(font_data_t *fd);
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c);
 bool font_data_is_equal(font_data_t *lhs, font_data_t *rhs);
 int font_data_export(font_data_t *fd, struct console_font *font, unsigned int vpitch);
 
lib/fonts/fonts.c +31 -0
index f5d5333450a0..4fc66722d00d 100644
@@ -178,6 +178,37 @@ unsigned int font_data_size(font_data_t *fd)
 }
 EXPORT_SYMBOL_GPL(font_data_size);
 
+static unsigned int font_data_num_glyphs(font_data_t *fd, unsigned int width, unsigned int height)
+{
+	return font_data_size(fd) / font_glyph_size(width, height);
+}
+
+/**
+ * font_data_glyph_buf() - Returns the glyph for a specific character as raw bytes
+ * @fd: The font data
+ * @width: The glyph width in bits per scanline
+ * @vpitch: The number of scanlines per glyph
+ * @c: The character
+ *
+ * Glyphs start at fixed intervals within the font data. font_data_glyph_buf()
+ * returns the glyph shape of the specified character. If no such glyph
+ * exists in the font, it returns NULL.
+ *
+ * Returns:
+ * The character's raw glyph shape, or NULL if no glyph exists for the character. The
+ * provided buffer is read-only.
+ */
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c)
+{
+	if (c >= font_data_num_glyphs(fd, width, vpitch))
+		return NULL;
+
+	return font_data_buf(fd) + font_glyph_size(width, vpitch) * c;
+}
+EXPORT_SYMBOL_GPL(font_data_glyph_buf);
+
 /**
  * font_data_is_equal - Compares font data for equality
  * @lhs: Left-hand side font data
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf()

Quoted 5 lines · Thomas Zimmermann

Add font_data_glyph_buf() to retrieve a character's glyph data or NULL otherwise. Console fonts can currently contain 256 or 512 glyphs. The kernel-internal characters are of type char, unsigned short or unsigned int. Catch all of them by accepting unsigned int. Callers possibly have to cast from signed to unsigned types to reach all glyphs in a font.

Thanks, yes I missed to check font_data_num_glyphs(), and using signed index is also problematic.

Quoted 63 lines
---
  include/linux/font.h |  3 +++
  lib/fonts/fonts.c    | 31 +++++++++++++++++++++++++++++++
  2 files changed, 34 insertions(+)
include/linux/font.h +3 -0
index 6845f02d739a..ea23b727388b 100644
@@ -101,6 +101,9 @@ font_data_t *font_data_import(const struct console_font *font, unsigned int vpit
  void font_data_get(font_data_t *fd);
  bool font_data_put(font_data_t *fd);
  unsigned int font_data_size(font_data_t *fd);
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c);
  bool font_data_is_equal(font_data_t *lhs, font_data_t *rhs);
  int font_data_export(font_data_t *fd, struct console_font *font, unsigned int vpitch);
 
lib/fonts/fonts.c +31 -0
index f5d5333450a0..4fc66722d00d 100644
@@ -178,6 +178,37 @@ unsigned int font_data_size(font_data_t *fd)
  }
  EXPORT_SYMBOL_GPL(font_data_size);
 
+static unsigned int font_data_num_glyphs(font_data_t *fd, unsigned int width, unsigned int height)
+{
+	return font_data_size(fd) / font_glyph_size(width, height);
+}
+
+/**
+ * font_data_glyph_buf() - Returns the glyph for a specific character as raw bytes
+ * @fd: The font data
+ * @width: The glyph width in bits per scanline
+ * @vpitch: The number of scanlines per glyph
+ * @c: The character
+ *
+ * Glyphs start at fixed intervals within the font data. font_data_glyph_buf()
+ * returns the glyph shape of the specified character. If no such glyph
+ * exists in the font, it returns NULL.
+ *
+ * Returns:
+ * The character's raw glyph shape, or NULL if no glyph exists for the character. The
+ * provided buffer is read-only.
+ */
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+					 unsigned int width, unsigned int vpitch,
+					 unsigned int c)
+{
+	if (c >= font_data_num_glyphs(fd, width, vpitch))
+		return NULL;
+
+	return font_data_buf(fd) + font_glyph_size(width, vpitch) * c;
+}
+EXPORT_SYMBOL_GPL(font_data_glyph_buf);
+
  /**
   * font_data_is_equal - Compares font data for equality
   * @lhs: Left-hand side font data
author maintainer
#3 lore raw

Re: [PATCH 1/4] lib/fonts: Look up glyph data with font_data_glyph_buf()

Hi

Am 01.06.26 um 13:18 schrieb Jocelyn Falempe:

Quoted 10 lines · Thomas Zimmermann
Quoted 5 lines

Add font_data_glyph_buf() to retrieve a character's glyph data or NULL otherwise. Console fonts can currently contain 256 or 512 glyphs. The kernel-internal characters are of type char, unsigned short or unsigned int. Catch all of them by accepting unsigned int. Callers possibly have to cast from signed to unsigned types to reach all glyphs in a font.

Thanks, yes I missed to check font_data_num_glyphs(), and using signed index is also problematic.

Thanks for reviewing. I'll merge the series in a few days if no other comments come in.

Best regards
Thomas

Quoted 67 lines
Quoted 66 lines

---
  include/linux/font.h |  3 +++
  lib/fonts/fonts.c    | 31 +++++++++++++++++++++++++++++++

  2 files changed, 34 insertions(+)
include/linux/font.h +0 -0
index 6845f02d739a..ea23b727388b 100644
@@ -101,6 +101,9 @@ font_data_t *font_data_import(const struct 

console_font *font, unsigned int vpit
  void font_data_get(font_data_t *fd);
  bool font_data_put(font_data_t *fd);
  unsigned int font_data_size(font_data_t *fd);
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+                     unsigned int width, unsigned int vpitch,
+                     unsigned int c);
  bool font_data_is_equal(font_data_t *lhs, font_data_t *rhs);
  int font_data_export(font_data_t *fd, struct console_font *font, unsigned int vpitch); diff --git a/lib/fonts/fonts.c b/lib/fonts/fonts.c
index f5d5333450a0..4fc66722d00d 100644

lib/fonts/fonts.c +6 -0
@@ -178,6 +178,37 @@ unsigned int font_data_size(font_data_t *fd)
   }
   EXPORT_SYMBOL_GPL(font_data_size);
   +static unsigned int font_data_num_glyphs(font_data_t *fd, unsigned int width, unsigned int height)
+{
+    return font_data_size(fd) / font_glyph_size(width, height);
+}
+
+/**
+ * font_data_glyph_buf() - Returns the glyph for a specific 

character as raw bytes
+ * @fd: The font data
+ * @width: The glyph width in bits per scanline
+ * @vpitch: The number of scanlines per glyph
+ * @c: The character
+ *
+ * Glyphs start at fixed intervals within the font data. font_data_glyph_buf()
+ * returns the glyph shape of the specified character. If no such glyph
+ * exists in the font, it returns NULL.
+ *
+ * Returns:
+ * The character's raw glyph shape, or NULL if no glyph exists for the character. The
+ * provided buffer is read-only.
+ */

lib/fonts/fonts.c +11 -0
+const unsigned char *font_data_glyph_buf(font_data_t *fd,
+                     unsigned int width, unsigned int vpitch,
+                     unsigned int c)
+{
+    if (c >= font_data_num_glyphs(fd, width, vpitch))
+        return NULL;
+
+    return font_data_buf(fd) + font_glyph_size(width, vpitch) * c;
+}
+EXPORT_SYMBOL_GPL(font_data_glyph_buf);
+

  /**
   * font_data_is_equal - Compares font data for equality
   * @lhs: Left-hand side font data

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
author maintainer
#4 lore raw

PATCH 2/4 drm/client: log: Look up glyph shape with font helper

In reply to Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing glyphs gracefully. Enable extended ASCII by casting to unsigned char.

---
 drivers/gpu/drm/clients/drm_log.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpu/drm/clients/drm_log.c +6 -4
index 8d21b785bead..e3e02c84a4cf 100644
@@ -122,10 +122,12 @@ static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
 	iosys_map_incr(&map, r.y1 * fb->pitches[0]);
 	for (i = 0; i < len && i < scanout->columns; i++) {
 		u32 color = (i < prefix_len) ? scanout->prefix_color : scanout->front_color;
-		src = drm_draw_get_char_bitmap(font, s[i], font_pitch);
-		drm_log_blit(&map, fb->pitches[0], src, font_pitch,
-			     scanout->scaled_font_h, scanout->scaled_font_w,
-			     px_width, color);
+		src = font_data_glyph_buf(font->data, font->width, font->height,
+					  (unsigned char)s[i]);
+		if (src)
+			drm_log_blit(&map, fb->pitches[0], src, font_pitch,
+				     scanout->scaled_font_h, scanout->scaled_font_w,
+				     px_width, color);
 		iosys_map_incr(&map, scanout->scaled_font_w * px_width);
 	}
 
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] drm/client: log: Look up glyph shape with font helper

Quoted 2 lines · Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing glyphs gracefully. Enable extended ASCII by casting to unsigned char.

Thanks, it looks good to me.

Quoted 27 lines
---
  drivers/gpu/drm/clients/drm_log.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
drivers/gpu/drm/clients/drm_log.c +6 -4
index 8d21b785bead..e3e02c84a4cf 100644
@@ -122,10 +122,12 @@ static void drm_log_draw_line(struct drm_log_scanout *scanout, const char *s,
  	iosys_map_incr(&map, r.y1 * fb->pitches[0]);
  	for (i = 0; i < len && i < scanout->columns; i++) {
  		u32 color = (i < prefix_len) ? scanout->prefix_color : scanout->front_color;
-		src = drm_draw_get_char_bitmap(font, s[i], font_pitch);
-		drm_log_blit(&map, fb->pitches[0], src, font_pitch,
-			     scanout->scaled_font_h, scanout->scaled_font_w,
-			     px_width, color);
+		src = font_data_glyph_buf(font->data, font->width, font->height,
+					  (unsigned char)s[i]);
+		if (src)
+			drm_log_blit(&map, fb->pitches[0], src, font_pitch,
+				     scanout->scaled_font_h, scanout->scaled_font_w,
+				     px_width, color);
  		iosys_map_incr(&map, scanout->scaled_font_w * px_width);
  	}
 
author maintainer
#6 lore raw

PATCH 3/4 drm/panic: Look up glyph shape with font helper

In reply to Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing glyphs gracefully. Enable extended ASCII by casting to unsigned char.

---
 drivers/gpu/drm/drm_panic.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_panic.c +4 -2
index d6d3b8d85dea..e576c4791861 100644
@@ -443,9 +443,11 @@ static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
 			rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
 
 		for (j = 0; j < line_len; j++) {
-			src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch);
+			src = font_data_glyph_buf(font->data, font->width, font->height,
+						  (unsigned char)msg[i].txt[j]);
 			rec.x2 = rec.x1 + font->width;
-			drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
+			if (src)
+				drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
 			rec.x1 += font->width;
 		}
 	}
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] drm/panic: Look up glyph shape with font helper

Quoted 2 lines · Thomas Zimmermann

Look up glyph shapes with font_data_glyph_buf(). Handle non-existing glyphs gracefully. Enable extended ASCII by casting to unsigned char.

Thanks, it looks good to me.

Quoted 24 lines
---
  drivers/gpu/drm/drm_panic.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/drm_panic.c +4 -2
index d6d3b8d85dea..e576c4791861 100644
@@ -443,9 +443,11 @@ static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
  			rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
 
  		for (j = 0; j < line_len; j++) {
-			src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch);
+			src = font_data_glyph_buf(font->data, font->width, font->height,
+						  (unsigned char)msg[i].txt[j]);
  			rec.x2 = rec.x1 + font->width;
-			drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
+			if (src)
+				drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
  			rec.x1 += font->width;
  		}
  	}
author maintainer
#8 lore raw

PATCH 4/4 drm/draw: Remove unused helper drm_draw_get_char_bitmap()

In reply to Thomas Zimmermann

Glyph-shape lookup has been integrated into the font-data interface and all callers have been updated. Remove the old helper.

---
 drivers/gpu/drm/drm_draw_internal.h | 7 -------
 1 file changed, 7 deletions(-)
drivers/gpu/drm/drm_draw_internal.h +0 -7
index 261967145635..44ddcee4744c 100644
@@ -7,7 +7,6 @@
 #ifndef __DRM_DRAW_INTERNAL_H__
 #define __DRM_DRAW_INTERNAL_H__
 
-#include <linux/font.h>
 #include <linux/types.h>
 
 struct iosys_map;
@@ -18,12 +17,6 @@ static inline bool drm_draw_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, in
 	return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0;
 }
 
-static inline const u8 *drm_draw_get_char_bitmap(const struct font_desc *font,
-						 char c, size_t font_pitch)
-{
-	return font->data + (c * font->height) * font_pitch;
-}
-
 bool drm_draw_can_convert_from_xrgb8888(u32 format);
 
 u32 drm_draw_color_from_xrgb8888(u32 color, u32 format);
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] drm/draw: Remove unused helper drm_draw_get_char_bitmap()

Quoted 2 lines · Thomas Zimmermann

Glyph-shape lookup has been integrated into the font-data interface and all callers have been updated. Remove the old helper.

Thanks, it looks good to me.

Quoted 31 lines
---
  drivers/gpu/drm/drm_draw_internal.h | 7 -------
  1 file changed, 7 deletions(-)
drivers/gpu/drm/drm_draw_internal.h +0 -7
index 261967145635..44ddcee4744c 100644
@@ -7,7 +7,6 @@
  #ifndef __DRM_DRAW_INTERNAL_H__
  #define __DRM_DRAW_INTERNAL_H__
 
-#include <linux/font.h>
  #include <linux/types.h>
 
  struct iosys_map;
@@ -18,12 +17,6 @@ static inline bool drm_draw_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, in
  	return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0;
  }
 
-static inline const u8 *drm_draw_get_char_bitmap(const struct font_desc *font,
-						 char c, size_t font_pitch)
-{
-	return font->data + (c * font->height) * font_pitch;
-}
-
  bool drm_draw_can_convert_from_xrgb8888(u32 format);
 
  u32 drm_draw_color_from_xrgb8888(u32 color, u32 format);