From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by mx.groups.io with SMTP id smtpd.web11.41079.1673480157883044457 for ; Wed, 11 Jan 2023 15:35:58 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=D7kCjQmx; spf=pass (domain: gmail.com, ip: 209.85.221.46, mailfrom: pedro.falcato@gmail.com) Received: by mail-wr1-f46.google.com with SMTP id az7so16547866wrb.5 for ; Wed, 11 Jan 2023 15:35:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ViLsvRM2rHyMKQvbzFRDCO41TfOAU4Ygf7HZ1fXyRPk=; b=D7kCjQmxCmTh8g2ZBG3rxcZD3/DNlD/TgRw+hR1GJLT1ZwfaZ0qmxj+389rwTmNrqn jF2nBXfq1vWPrR9+A2oFnrp5VvPn+7DJZGs6+hRQCDtPIdeg4Tk54Oh98fsC4M+C3R7f dB7yOXFIJ8Gf+TN0KDISuNppr+6NYGJkRydfPn7ALsWXPbZwt7Jk73B5rkZA6vslFGxP 1hImRKQq/d5ObLhj9wHKgehSolKFTtCtDl8ANoiaZVgsyvafyCf5Xqs8cB2vlP/GG9FK sS0YPOZ2aGzLlNL1RdeR1bkYWeOD4Q/6J7dzvpqjViT3a/LY003xmVif/6LqqPlRM8Gn Ua2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ViLsvRM2rHyMKQvbzFRDCO41TfOAU4Ygf7HZ1fXyRPk=; b=PUGamkIwqxnuyFsRHKAJsOSV0LxqKFSFjOJh0sxnQQexRVe/YmNB7R8B35GezU31ib hcZtYgkDwFvCKK/O/W1Kby2cTptoGdJpCbpgKjDpmWHYtB2uxHKkLxJsRgYZMyPxixgx 2VEmPDf1567WtBiwGqsy/F3jCEEMkToCYFV0azvKQcs27QQlM06AriYZBcBW5q/kmq+z x3npIomDi4kkcc6B4PhA4w95IOJsslSq9HQDkAkLCJwkA+wuhBuShhyQD0wImV9YEOJ0 IV67SeJd12A43J/t7FujR+/60YNwD6tgdEJfIbgtVDCUGb8bLbLVrctq9wDUv9U51r83 gVmw== X-Gm-Message-State: AFqh2kqqD3pRa0FOqWZfIoSLmPZs/OHnmkoNuw7wPfFLFTwiI+RNERas 16DC61Df2/cB5gvW0L6L/hwvgTvUPP+76A== X-Google-Smtp-Source: AMrXdXt8vtd1LEp5LYoCm0XNOjarukz7j36XGwDiyTVcJp7NGd8pjl80odiFW60wLhTcOnic5YYTtA== X-Received: by 2002:a5d:5045:0:b0:27b:b974:2c01 with SMTP id h5-20020a5d5045000000b0027bb9742c01mr40094955wrt.71.1673480155944; Wed, 11 Jan 2023 15:35:55 -0800 (PST) Return-Path: Received: from PC-PEDRO-ARCH.lan ([2001:8a0:7280:5801:9441:3dce:686c:bfc7]) by smtp.gmail.com with ESMTPSA id u24-20020adfa198000000b002bc84c55758sm7796607wru.63.2023.01.11.15.35.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 15:35:55 -0800 (PST) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , =?UTF-8?q?Marvin=20H=C3=A4user?= Subject: [PATCH 1/1] Ext4Pkg: Fix and clarify handling regarding non-utf8 dir entries Date: Wed, 11 Jan 2023 23:35:48 +0000 Message-Id: <20230111233548.247738-1-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, the handling was mixed and/or non-existent regarding non utf-8 dirent names. Clarify it. Signed-off-by: Pedro Falcato Cc: Marvin Häuser --- Features/Ext4Pkg/Ext4Dxe/Directory.c | 37 ++++++++++++++++++++++------ Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h | 8 +++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Directory.c b/Features/Ext4Pkg/Ext4Dxe/Directory.c index 34c06b77b142..5a704dc800a5 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -1,7 +1,7 @@ /** @file Directory related routines - Copyright (c) 2021 Pedro Falcato All rights reserved. + Copyright (c) 2021 - 2023 Pedro Falcato All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -16,8 +16,9 @@ @param[in] Entry Pointer to a EXT4_DIR_ENTRY. @param[out] Ucs2FileName Pointer to an array of CHAR16's, of size EXT4_NAME_MAX + 1. - @retval EFI_SUCCESS The filename was succesfully retrieved and converted to UCS2. - @retval !EFI_SUCCESS Failure. + @retval EFI_SUCCESS The filename was succesfully retrieved and converted to UCS2. + @retval EFI_INVALID_PARAMETER The filename is not valid UTF-8. + @retval !EFI_SUCCESS Failure. **/ EFI_STATUS Ext4GetUcs2DirentName ( @@ -174,10 +175,16 @@ Ext4RetrieveDirent ( * need to form valid ASCII/UTF-8 sequences. */ if (EFI_ERROR (Status)) { - // If we error out, skip this entry - // I'm not sure if this is correct behaviour, but I don't think there's a precedent here. - BlockOffset += Entry->rec_len; - continue; + if (Status == EFI_INVALID_PARAMETER) { + // If we error out due to a bad UTF-8 sequence (see Ext4GetUcs2DirentName), skip this entry. + // I'm not sure if this is correct behaviour, but I don't think there's a precedent here. + BlockOffset += Entry->rec_len; + continue; + } + + // Other sorts of errors should just error out. + FreePool (Buf); + return Status; } if ((Entry->name_len == StrLen (Name)) && @@ -436,6 +443,7 @@ Ext4ReadDir ( EXT4_FILE *TempFile; BOOLEAN ShouldSkip; BOOLEAN IsDotOrDotDot; + CHAR16 DirentUcs2Name[EXT4_NAME_MAX + 1]; DirIno = File->Inode; Status = EFI_SUCCESS; @@ -503,6 +511,21 @@ Ext4ReadDir ( continue; } + // Test if the dirent is valid utf-8. This is already done inside Ext4OpenDirent but EFI_INVALID_PARAMETER + // has the danger of its meaning being overloaded in many places, so we can't skip according to that. + // So test outside of it, explicitly. + Status = Ext4GetUcs2DirentName (&Entry, DirentUcs2Name); + + if (EFI_ERROR (Status)) { + if (Status == EFI_INVALID_PARAMETER) { + // Bad UTF-8, skip. + Offset += Entry.rec_len; + continue; + } + + goto Out; + } + Status = Ext4OpenDirent (Partition, EFI_FILE_MODE_READ, &TempFile, &Entry, File); if (EFI_ERROR (Status)) { diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h index 81ba568c5947..933ae422df44 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h @@ -944,11 +944,11 @@ Ext4StrCmpInsensitive ( Retrieves the filename of the directory entry and converts it to UTF-16/UCS-2 @param[in] Entry Pointer to a EXT4_DIR_ENTRY. - @param[out] Ucs2FileName Pointer to an array of CHAR16's, of size -EXT4_NAME_MAX + 1. + @param[out] Ucs2FileName Pointer to an array of CHAR16's, of size EXT4_NAME_MAX + 1. - @retval EFI_SUCCESS Unicode collation was successfully initialised. - @retval !EFI_SUCCESS Failure. + @retval EFI_SUCCESS The filename was succesfully retrieved and converted to UCS2. + @retval EFI_INVALID_PARAMETER The filename is not valid UTF-8. + @retval !EFI_SUCCESS Failure. **/ EFI_STATUS Ext4GetUcs2DirentName ( -- 2.39.0