Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp938856pxk; Fri, 25 Sep 2020 01:41:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvRw3I7knYEbgEb6qp6cYCheJFJlPO4YMy1Jc1yEoviiP3+351KeVLPPcCx7OELXDeSfOc X-Received: by 2002:aa7:c3c8:: with SMTP id l8mr136802edr.368.1601023293712; Fri, 25 Sep 2020 01:41:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601023293; cv=none; d=google.com; s=arc-20160816; b=D+h6dQzb1kyGXM5DO/J03eT65lCcl6NQePmW2o2Kqzo/OHjEPN0fey4XTEbm+HspU5 igzUgQP/hEWRjv7eSKt5dKwaxpgpBIG8lEcaV91Gn53gpDIEaAhU+3VB0npSYnsoc3CP UGSVXC/3AkdS1X2piCL5AaKIPWmNwMkqa/1OZ2qgepJOMZTpfJ2d6GkZ1JQR+W6sLQGe A457MNrIThdCllLFHzC6d9ZXXXqUbdLHmC//YdIHTBnJbSbG5evYP+U4rFj7T19JU0Nb KOHvc9XW9MC/wkzRDpecIBkvm9SlN+lEozRCpAQmirITDWDKAKbFojhWqC0btID//CPh ddJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=8bcdSgoAZ73IZy198rnRan6x5PSCC5tXv1r4zPoC4uo=; b=LYmC/QPkggIrV59nRrIwRuKuNuwQ9CmQZQK0QRCMXt64qqDvgvZRYCRaJ5g+wKmoMa TtW6oVO6CGYLPw83qZ8j/n62ZTraPwl3iPZXQhBTy4pULHvw9uZW/W26PWEX91qzhEMK l6iF3gf1arEhG9C9EKNErII9pmXNuaqRHG5/pH35MivoahwFsExVv9vl55m32M97sJF6 swNlVEhB25pkMRYfMYL036n5CAa/3QJMPYoA4QOoTAYVA6t6nIRfgxrZ++leDo9Drbjq 9f75tS6SbtSetZnAT8wpM9Y5SeZm/n2atpB6ysJoNdh6eXY8txKoz9UURkdsB6nszeq5 90TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=CNu5IziH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id wq13si1350579ejb.722.2020.09.25.01.41.10; Fri, 25 Sep 2020 01:41:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=CNu5IziH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727699AbgIYIis (ORCPT + 99 others); Fri, 25 Sep 2020 04:38:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727067AbgIYIir (ORCPT ); Fri, 25 Sep 2020 04:38:47 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80920C0613CE for ; Fri, 25 Sep 2020 01:38:46 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id a17so2634841wrn.6 for ; Fri, 25 Sep 2020 01:38:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=8bcdSgoAZ73IZy198rnRan6x5PSCC5tXv1r4zPoC4uo=; b=CNu5IziHGSAsmKK8nCUKNhXkCUfo9CYqtOAGtgz/t+upSDVlprZtizhSJr4Hd4NWDs FLUpnGUHgSV2vQpc9QhRZNe7EKp78O5sOjDU4BDE8eop1xscW0lYdEQB0RmABK5/HF+U Vp16JyuGjPPILnnwx1SaEsxE0sNIFsgJYqQN0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=8bcdSgoAZ73IZy198rnRan6x5PSCC5tXv1r4zPoC4uo=; b=WHn+9qtyTJUlseXNNAo2+eIaA75L4tOafYZaLpg6L5Ku5f5gqYipP8OAkLG/Wq54rL qy5hDQ3xHOPjpeoTNeGRQYQ6P+Mpc7ZWMxnc6NY7p8myLdqZH3E+cdZbaznT2VeWipQU dAEKMJwD9KA2b55lBEV9qRme+QeTvQ3658GwKBT3vF6xqXG4V6WPlhR/rAUGAMiqSURd EbK+Bw8i3rqAHEkyy2UztG1KWwiLlfZYZHaznwnHFRV4QdWiIHJxepdlEgcR0R/oVq8Z 5w0/F1F7p+HhgIEmAtiy0m1iOxI/5o5BXro50+OhPqFwJTi54j7vnijHWjONke1WRitO OPjw== X-Gm-Message-State: AOAM5329yQu9Z28qvkYaECYJR3E70jaiLV+rYxwY38va+FX9WraFEEok 3PgHTstjCQXMvtCHu+wQhMHMGg== X-Received: by 2002:adf:a3ca:: with SMTP id m10mr3338923wrb.104.1601023125154; Fri, 25 Sep 2020 01:38:45 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id h8sm1967181wrw.68.2020.09.25.01.38.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Sep 2020 01:38:44 -0700 (PDT) Date: Fri, 25 Sep 2020 10:38:42 +0200 From: Daniel Vetter To: Greg Kroah-Hartman Cc: Peilin Ye , Daniel Vetter , Bartlomiej Zolnierkiewicz , Jiri Slaby , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, syzkaller-bugs@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH 0/3] Prevent out-of-bounds access for built-in font data buffers Message-ID: <20200925083842.GC438822@phenom.ffwll.local> Mail-Followup-To: Greg Kroah-Hartman , Peilin Ye , Bartlomiej Zolnierkiewicz , Jiri Slaby , dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org, syzkaller-bugs@googlegroups.com, linux-kernel@vger.kernel.org References: <0000000000006b9e8d059952095e@google.com> <20200924140937.GA749208@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200924140937.GA749208@kroah.com> X-Operating-System: Linux phenom 5.7.0-1-amd64 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 24, 2020 at 04:09:37PM +0200, Greg Kroah-Hartman wrote: > On Thu, Sep 24, 2020 at 09:38:22AM -0400, Peilin Ye wrote: > > Hi all, > > > > syzbot has reported [1] a global out-of-bounds read issue in > > fbcon_get_font(). A malicious user may resize `vc_font.height` to a large > > value in vt_ioctl(), causing fbcon_get_font() to overflow our built-in > > font data buffers, declared in lib/fonts/font_*.c: > > > > (e.g. lib/fonts/font_8x8.c) > > #define FONTDATAMAX 2048 > > > > static const unsigned char fontdata_8x8[FONTDATAMAX] = { > > > > /* 0 0x00 '^@' */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > 0x00, /* 00000000 */ > > [...] > > > > In order to perform a reliable range check, fbcon_get_font() needs to know > > `FONTDATAMAX` for each built-in font under lib/fonts/. Unfortunately, we > > do not keep that information in our font descriptor, > > `struct console_font`: > > > > (include/uapi/linux/kd.h) > > struct console_font { > > unsigned int width, height; /* font size */ > > unsigned int charcount; > > unsigned char *data; /* font data with height fixed to 32 */ > > }; > > > > To make things worse, `struct console_font` is part of the UAPI, so we > > cannot add a new field to keep track of `FONTDATAMAX`. > > > > Fortunately, the framebuffer layer itself gives us a hint of how to > > resolve this issue without changing UAPI. When allocating a buffer for a > > user-provided font, fbcon_set_font() reserves four "extra words" at the > > beginning of the buffer: > > > > (drivers/video/fbdev/core/fbcon.c) > > new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size, GFP_USER); > > [...] > > new_data += FONT_EXTRA_WORDS * sizeof(int); > > FNTSIZE(new_data) = size; > > FNTCHARCNT(new_data) = charcount; > > REFCOUNT(new_data) = 0; /* usage counter */ > > [...] > > FNTSUM(new_data) = csum; > > > > Later, to get the size of a data buffer, the framebuffer layer simply > > calls FNTSIZE() on it: > > > > (drivers/video/fbdev/core/fbcon.h) > > /* Font */ > > #define REFCOUNT(fd) (((int *)(fd))[-1]) > > #define FNTSIZE(fd) (((int *)(fd))[-2]) > > #define FNTCHARCNT(fd) (((int *)(fd))[-3]) > > #define FNTSUM(fd) (((int *)(fd))[-4]) > > #define FONT_EXTRA_WORDS 4 > > > > Currently, this is only done for user-provided fonts. Let us do the same > > thing for built-in fonts, prepend these "extra words" (including > > `FONTDATAMAX`) to their data buffers, so that other subsystems, like the > > framebuffer layer, can use these macros on all fonts, no matter built-in > > or user-provided. As an example, this series fixes the syzbot issue in > > fbcon_get_font(): > > > > (drivers/video/fbdev/core/fbcon.c) > > if (font->width <= 8) { > > j = vc->vc_font.height; > > + if (font->charcount * j > FNTSIZE(fontdata)) > > + return -EINVAL; > > [...] > > > > Similarly, newport_con also use these macros. It only uses three of them: > > > > (drivers/video/console/newport_con.c) > > /* borrowed from fbcon.c */ > > #define REFCOUNT(fd) (((int *)(fd))[-1]) > > #define FNTSIZE(fd) (((int *)(fd))[-2]) > > #define FNTCHARCNT(fd) (((int *)(fd))[-3]) > > #define FONT_EXTRA_WORDS 3 > > > > To keep things simple, move all these macro definitions to , > > use four words instead of three, and initialize the fourth word in > > newport_set_font() properly. > > > > Many thanks to Greg Kroah-Hartman , who > > reviewed and improved this series! > > > > [1]: KASAN: global-out-of-bounds Read in fbcon_get_font > > https://syzkaller.appspot.com/bug?id=08b8be45afea11888776f897895aef9ad1c3ecfd > > > > Peilin Ye (3): > > fbdev, newport_con: Move FONT_EXTRA_WORDS macros into linux/font.h > > Fonts: Support FONT_EXTRA_WORDS macros for built-in fonts > > fbcon: Fix global-out-of-bounds read in fbcon_get_font() > > > > drivers/video/console/newport_con.c | 7 +------ > > drivers/video/fbdev/core/fbcon.c | 12 ++++++++++++ > > drivers/video/fbdev/core/fbcon.h | 7 ------- > > drivers/video/fbdev/core/fbcon_rotate.c | 1 + > > drivers/video/fbdev/core/tileblit.c | 1 + > > include/linux/font.h | 13 +++++++++++++ > > lib/fonts/font_10x18.c | 9 ++++----- > > lib/fonts/font_6x10.c | 9 +++++---- > > lib/fonts/font_6x11.c | 9 ++++----- > > lib/fonts/font_7x14.c | 9 ++++----- > > lib/fonts/font_8x16.c | 9 ++++----- > > lib/fonts/font_8x8.c | 9 ++++----- > > lib/fonts/font_acorn_8x8.c | 9 ++++++--- > > lib/fonts/font_mini_4x6.c | 8 ++++---- > > lib/fonts/font_pearl_8x8.c | 9 ++++----- > > lib/fonts/font_sun12x22.c | 9 ++++----- > > lib/fonts/font_sun8x16.c | 7 ++++--- > > lib/fonts/font_ter16x32.c | 9 ++++----- > > 18 files changed, 79 insertions(+), 67 deletions(-) > > Gotta love going backwards in arrays :) > > Nice work, whole series is: > > Reviewed-by: Greg Kroah-Hartman > > > Daniel, can you take this through your tree? Applied to drm-misc-fixes, but just missed the pull request train for -rc7. Should land in Linus' tree next week. But I did look at the code, and I have regrets. Macros into untyped arrays and negative indices is very old skool C. It's definitely neater than before, but also can't deny that we're doing dental surgery on a living and fire breathing dragon here :-/ I guess I'll just add this to the list of requirements anyone has to resolve before we're going to resurrect scrollback. Cheers, Daniel > > thanks, > > greg k-h -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch