Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1q1FLi-0005ur-MS for mharc-grub-devel@gnu.org; Mon, 22 May 2023 19:53:58 -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 1q1FLf-0005tp-QF for grub-devel@gnu.org; Mon, 22 May 2023 19:53:56 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q1FLd-0002yg-HK for grub-devel@gnu.org; Mon, 22 May 2023 19:53:55 -0400 Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34MKNu9m019624; Mon, 22 May 2023 23:53:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=corp-2023-03-30; bh=6/yNibWX7ahRZWcQODlUXQ3qID4H3EJI9yANYCcxC5k=; b=ADUlplamhhlVxstCxVHS4RVndYFhwn9Ihd41FE+ns8EsI80dFxqmign9pcFD2E3tAjyd GKuNL+HNc2DB0OLPLyblMiZz0Zr2MVTFlhlltWLE2S3M6f6RLw8CsN+5KgLvPtg6MBCc PxR4FBqZ+AuEd4iEB1zTH8hiYBHDaPkIcx56WPDM7REKF2GmcCmjOfb4WFSXPz8Hyhz+ fyecX+0lN0H6//8mXtX8esmhWvi5LUGO4Og3+TRjwb+nVto9DgOUzweJQSrmsXdxNHlR r+y1VW6bWOatn7qyjfeMjQ3f0jzrfcG/bBKaBUwzKuGSRs86H5uc5MHhX4aPK3u6sgRE 4A== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qpp8cbxqt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 May 2023 23:53:49 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34MMFJIh024394; Mon, 22 May 2023 23:53:49 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk8tm62c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 May 2023 23:53:49 +0000 Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 34MNnwEf039666; Mon, 22 May 2023 23:53:48 GMT Received: from localhost (lidochen-ol8-1.allregionaliads.osdevelopmeniad.oraclevcn.com [100.100.250.72]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 3qqk8tm628-1; Mon, 22 May 2023 23:53:48 +0000 From: Lidong Chen To: grub-devel@gnu.org Cc: daniel.kiper@oracle.com, mlewando@redhat.com, rharwood@pm.me, lidong.chen@oracle.com Subject: [PATCH 1/1] xfs: Fix issues found while fuzzing the XFS filesystem Date: Mon, 22 May 2023 23:53:48 +0000 Message-Id: <20230522235348.936698-1-lidong.chen@oracle.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-22_18,2023-05-22_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305220201 X-Proofpoint-ORIG-GUID: 4ARo52rVTVjKfOwOkVwMqoxVTNtGwUMe X-Proofpoint-GUID: 4ARo52rVTVjKfOwOkVwMqoxVTNtGwUMe Received-SPF: pass client-ip=205.220.177.32; envelope-from=lidong.chen@oracle.com; helo=mx0b-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, T_SCC_BODY_TEXT_LINE=-0.01 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: Mon, 22 May 2023 23:53:57 -0000 From: Darren Kenny While performing fuzz testing with XFS filesystem images with ASAN enabled, several issues were found where the memory accesses are made beyond the data that is allocated into the struct grub_xfs_data structure's data field. The existing stucture didn't store the size of the memory allocted into the buffer in the data field and had no way to check it. To resolve these issues, the data size is stored to enable accesses into the data buffer. With these checks in place, the fuzzing corpus no longer cause any crashes. Signed-off-by: Darren Kenny Signed-off-by: Robbie Harwood Signed-off-by: Marta Lewandowska Signed-off-by: Lidong Chen --- grub-core/fs/xfs.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/grub-core/fs/xfs.c b/grub-core/fs/xfs.c index d6de7f1a2..a0aaa3aa8 100644 --- a/grub-core/fs/xfs.c +++ b/grub-core/fs/xfs.c @@ -239,6 +239,7 @@ struct grub_fshelp_node struct grub_xfs_data { + grub_size_t datasize; struct grub_xfs_sblock sblock; grub_disk_t disk; int pos; @@ -608,8 +609,20 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock) } else if (node->inode.format == XFS_INODE_FORMAT_EXT) { + grub_addr_t exts_end = 0; + grub_addr_t data_end = 0; + nrec = grub_be_to_cpu32 (node->inode.nextents); exts = (struct grub_xfs_extent *) grub_xfs_inode_data(&node->inode); + + if (grub_mul(sizeof(struct grub_xfs_extent), nrec, &exts_end) || + grub_add((grub_addr_t)node->data, exts_end, &exts_end) || + grub_add((grub_addr_t)node->data, node->data->datasize, &data_end) || + exts_end > data_end) + { + grub_error (GRUB_ERR_BAD_FS, "Invalid number of XFS exts"); + return 0; + } } else { @@ -799,6 +812,12 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, grub_uint8_t *inopos = grub_xfs_inline_de_inopos(dir->data, de); grub_uint8_t c; + if ((inopos + (smallino?4:8)) > + (grub_uint8_t*)dir + grub_xfs_fshelp_size(dir->data)) + { + return grub_error (GRUB_ERR_BAD_FS, "not a correct XFS inode"); + } + /* inopos might be unaligned. */ if (smallino) ino = (((grub_uint32_t) inopos[0]) << 24) @@ -825,6 +844,12 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, de->name[de->len] = c; de = grub_xfs_inline_next_de(dir->data, head, de); + + if ((grub_uint8_t*)de >= (grub_uint8_t*)dir + grub_xfs_fshelp_size(dir->data)) + { + return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry"); + } + } break; } @@ -890,6 +915,10 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir, } filename = (char *)(direntry + 1); + if (filename + direntry->len - 1 > (char *) tail) + { + return grub_error (GRUB_ERR_BAD_FS, "invalid XFS directory entry"); + } /* The byte after the filename is for the filetype, padding, or tag, which is not used by GRUB. So it can be overwritten. */ filename[direntry->len] = '\0'; @@ -934,6 +963,8 @@ grub_xfs_mount (grub_disk_t disk) if (!data) return 0; + data->datasize = sizeof (struct grub_xfs_data); + grub_dprintf("xfs", "Reading sb\n"); /* Read the superblock. */ if (grub_disk_read (disk, 0, 0, @@ -955,6 +986,7 @@ grub_xfs_mount (grub_disk_t disk) if (! data) goto fail; + data->datasize = sz; data->diropen.data = data; data->diropen.ino = grub_be_to_cpu64(data->sblock.rootino); data->diropen.inode_read = 1; -- 2.39.1