Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1onqXq-00038W-Aw for mharc-grub-devel@gnu.org; Wed, 26 Oct 2022 20:14:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onqXo-00038N-Pv for grub-devel@gnu.org; Wed, 26 Oct 2022 20:14:48 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onqXl-0004hu-7V for grub-devel@gnu.org; Wed, 26 Oct 2022 20:14:48 -0400 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29QLFsVZ022260 for ; Thu, 27 Oct 2022 00:14:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id; s=corp-2022-7-12; bh=xD5PWEI66yuSjJCsNW+gE3oqQ6f+ICSlrIcKFjdHohw=; b=CtM9hEfBhLYcPDtrQzJaNFHW09vtTeb2WnJkKv5+SkhyET6BXeo3I2u4nUEFxtNEfAT5 Ao1Gciq0ZxucR1xO0fjE71mVLkhHIXUeY+TR2XbJ6KRU2dyxVzAmQJGclzJVw6x5aPsw 7qqBa8kjeLR1BTkvZky/WslNDTM+kOFRRUrFtpQNRlnhr/0bR3oQZT7pWm0clb1LJnZl iMKbovT2qEThDXAvmX42VB7ExpXXR7L37RnySngcy9hCBxHB+XwrAv5wSi7LgkaId5zz +AqXWkwnkuLBceVKpXArjyLD8vX8HEhK9ZK2AJBYVmEBADoO34MKsCs6Nko7OzhhH5LJ Ag== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3kfahe8q76-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 27 Oct 2022 00:14:40 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 29QMG7IE017540 for ; Thu, 27 Oct 2022 00:14:40 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3kfagfxs1j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 27 Oct 2022 00:14:40 +0000 Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 29R0EdKo004155 for ; Thu, 27 Oct 2022 00:14:39 GMT Received: from localhost (dhcp-10-152-14-18.usdhcp.oraclecorp.com [10.152.14.18]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3kfagfxs1b-1; Thu, 27 Oct 2022 00:14:39 +0000 From: Alec Brown To: grub-devel@gnu.org Cc: daniel.kiper@oracle.com, alec.r.brown@oracle.com, darren.kenny@oracle.com Subject: [PATCH v2] video/readers: Add artificial limit to image dimensions Date: Wed, 26 Oct 2022 20:16:44 -0400 Message-Id: <1666829804-25372-1-git-send-email-alec.r.brown@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-26_09,2022-10-26_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 bulkscore=0 malwarescore=0 phishscore=0 mlxlogscore=999 mlxscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2210170000 definitions=main-2210270000 X-Proofpoint-GUID: L-p7gpZYHYIp-mJSAm6OLk5_DYGTAuWp X-Proofpoint-ORIG-GUID: L-p7gpZYHYIp-mJSAm6OLk5_DYGTAuWp Received-SPF: pass client-ip=205.220.165.32; envelope-from=alec.r.brown@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 Oct 2022 00:14:49 -0000 In grub-core/video/readers/jpeg.c, the height and width of a JPEG image don't have an upper limit for how big the JPEG image can be. In coverity, this is getting flagged as an untrusted loop bound. This issue can also seen in PNG and TGA format images as well but coverity isn't flagging it. To prevent this, the constant IMAGE_HW_MAX_PX is being added to bitmap.h, which has a value of 16384, to act as an artifical limit and restrict the height and width of images. This value was picked as it is double the current max resolution size, which is 8K. Fixes: CID 292450 Signed-off-by: Alec Brown --- In v1, the patch set was developed on outdated code and there was already a fix for the second patch. So in this version, the second patch has been dropped. The only thing that has changed in this patch is line numbers. docs/grub.texi | 3 ++- grub-core/video/readers/jpeg.c | 6 +++++- grub-core/video/readers/png.c | 6 +++++- grub-core/video/readers/tga.c | 7 +++++++ include/grub/bitmap.h | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index 0dbbdc374..2d6cd8358 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -1515,7 +1515,8 @@ resolution. @xref{gfxmode}. Set a background image for use with the @samp{gfxterm} graphical terminal. The value of this option must be a file readable by GRUB at boot time, and it must end with @file{.png}, @file{.tga}, @file{.jpg}, or @file{.jpeg}. -The image will be scaled if necessary to fit the screen. +The image will be scaled if necessary to fit the screen. Image height and +width will be restricted by an artificial limit of 16384. @item GRUB_THEME Set a theme for use with the @samp{gfxterm} graphical terminal. diff --git a/grub-core/video/readers/jpeg.c b/grub-core/video/readers/jpeg.c index 09596fbf5..ae634fd41 100644 --- a/grub-core/video/readers/jpeg.c +++ b/grub-core/video/readers/jpeg.c @@ -346,7 +346,11 @@ grub_jpeg_decode_sof (struct grub_jpeg_data *data) data->image_height = grub_jpeg_get_word (data); data->image_width = grub_jpeg_get_word (data); - if ((!data->image_height) || (!data->image_width)) + grub_dprintf ("jpeg", "image height: %d\n", data->image_height); + grub_dprintf ("jpeg", "image width: %d\n", data->image_width); + + if ((!data->image_height) || (!data->image_width) || + (data->image_height > IMAGE_HW_MAX_PX) || (data->image_width > IMAGE_HW_MAX_PX)) return grub_error (GRUB_ERR_BAD_FILE_TYPE, "jpeg: invalid image size"); cc = grub_jpeg_get_byte (data); diff --git a/grub-core/video/readers/png.c b/grub-core/video/readers/png.c index 7f2ba7849..3163e97bf 100644 --- a/grub-core/video/readers/png.c +++ b/grub-core/video/readers/png.c @@ -264,7 +264,11 @@ grub_png_decode_image_header (struct grub_png_data *data) data->image_width = grub_png_get_dword (data); data->image_height = grub_png_get_dword (data); - if ((!data->image_height) || (!data->image_width)) + grub_dprintf ("png", "image height: %d\n", data->image_height); + grub_dprintf ("png", "image width: %d\n", data->image_width); + + if ((!data->image_height) || (!data->image_width) || + (data->image_height > IMAGE_HW_MAX_PX) || (data->image_width > IMAGE_HW_MAX_PX)) return grub_error (GRUB_ERR_BAD_FILE_TYPE, "png: invalid image size"); color_bits = grub_png_get_byte (data); diff --git a/grub-core/video/readers/tga.c b/grub-core/video/readers/tga.c index a9ec3a1b6..f2f563d06 100644 --- a/grub-core/video/readers/tga.c +++ b/grub-core/video/readers/tga.c @@ -340,6 +340,13 @@ grub_video_reader_tga (struct grub_video_bitmap **bitmap, data.image_width = grub_le_to_cpu16 (data.hdr.image_width); data.image_height = grub_le_to_cpu16 (data.hdr.image_height); + grub_dprintf ("tga", "image height: %d\n", data.image_height); + grub_dprintf ("tga", "image width: %d\n", data.image_width); + + /* Check image height and width are within restrictions */ + if ((data.image_height > IMAGE_HW_MAX_PX) || (data.image_width > IMAGE_HW_MAX_PX)) + return grub_error (GRUB_ERR_BAD_FILE_TYPE, "tga: invalid image size"); + /* Check that bitmap encoding is supported. */ switch (data.hdr.image_type) { diff --git a/include/grub/bitmap.h b/include/grub/bitmap.h index 5728f8ca3..149d37bfe 100644 --- a/include/grub/bitmap.h +++ b/include/grub/bitmap.h @@ -24,6 +24,8 @@ #include #include +#define IMAGE_HW_MAX_PX 16384 + struct grub_video_bitmap { /* Bitmap format description. */ -- 2.27.0