From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by mx.groups.io with SMTP id smtpd.web11.50756.1658297839861575854 for ; Tue, 19 Jul 2022 23:17:20 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QlYAs46S; spf=pass (domain: gmail.com, ip: 209.85.218.42, mailfrom: savvamtr@gmail.com) Received: by mail-ej1-f42.google.com with SMTP id j22so31244890ejs.2 for ; Tue, 19 Jul 2022 23:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dgpSV07dlAtMMOhpBiCJOLRhXLR5xtGddUE/orS9X+Y=; b=QlYAs46Su2Ucd5vdCIpafXf8kp6/40DCQgQUFVu1ylz2HbyOXsn1OBDOKuCJ8IUmn8 YixXvHhPOzEstgS102zM3PYjiMMj07jccFUa8CeRjP3cMEC1Wcuz7s1VsiI5/H8z7UTL CNcHKAXN4Idd8+oIkCCqToVZ8fK1QGbGxFg9Dqzo6eJHFRQfbxXNEjxIuAEYBUQzAuUF bQWa6tPNgCw015E9QNYkCJUmuvcmySVlem84+4u59+he/9y7S3mZHqRjhFCaVdXl4Jl1 gIbwQ1Mj4rcl+BHWDdo8S2vYjbjlsHdVYXdQEWlnQyBT4f4X5AOuEJgtiKXGVbcHRlU6 SlpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dgpSV07dlAtMMOhpBiCJOLRhXLR5xtGddUE/orS9X+Y=; b=H7H5U9Es3rFzhO6lglQxvGzfQALJZTyj7mW9HiZGdyM+g0UP3g6nOJBa3YvBWirwJf dI+R1NkGWrjCFlTWbvOuiuugFTFfr3KIHfvEwd/9q4sUb+ABllGGasiZtWPFu/kzF/ha lFYzxdq7Q4Y2EhqRg1+xmrFqQTDdaLcrWau5tOKCa6SWLH8jPqwFAcJoSKFrMgZ5ZG5Z CYygGwKHY+6gfbjzSnPXpCbCKW65fFLmiQfvccZ0I57XuJSA8nu8PXXm6LTwQDhqWxDB E/nJYJ8a2C3GUR/VMeFyp7omUwXxGXxcMJUJC9bQFi/5piRu44pARILTP+kZ9g24TQ9u 8wzw== X-Gm-Message-State: AJIora8XGdACdmHoJAKKNacCfLBmpOKlh3CwnKMsRs1VWvPhzyjF05oe 83r924BwffzcBqtKdCHwB7jCsSD3i954SfZMzzk= X-Google-Smtp-Source: AGRyM1tMJbJKUR44l4uwLbnMXatBeuAt21Do3rAqVmlwmXwvCRGwjLbn+q20eUIC2mFT4I32svlxtQ== X-Received: by 2002:a17:906:8448:b0:72b:5659:9873 with SMTP id e8-20020a170906844800b0072b56599873mr34623821ejy.117.1658297838051; Tue, 19 Jul 2022 23:17:18 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([207.180.219.167]) by smtp.gmail.com with ESMTPSA id f17-20020a056402151100b0043b986751a7sm2104378edw.41.2022.07.19.23.17.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Jul 2022 23:17:17 -0700 (PDT) From: "Savva Mitrofanov" To: devel@edk2.groups.io Cc: =?UTF-8?q?Marvin=20H=C3=A4user?= , Pedro Falcato , Vitaly Cheptsov Subject: [edk2-platforms][PATCH 1/2] Ext4Pkg: Add symbolic links support Date: Wed, 20 Jul 2022 12:17:01 +0600 Message-Id: <20220720061702.40770-2-savvamtr@gmail.com> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220720061702.40770-1-savvamtr@gmail.com> References: <20220720061702.40770-1-savvamtr@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Provided support for symlink file type. Added routine which allows reading and following them through recursive open() call. As a security meausure implemented simple symlink loop check with nest level limit equal 8. Also this patch moves Ext4Open functionality to internal routine. Cc: Marvin H=C3=A4user Cc: Pedro Falcato Cc: Vitaly Cheptsov Signed-off-by: Savva Mitrofanov --- Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h | 2 +- Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h | 99 +++++- Features/Ext4Pkg/Ext4Dxe/File.c | 365 ++++++++++++++++++-- Features/Ext4Pkg/Ext4Dxe/Inode.c | 38 ++ 4 files changed, 470 insertions(+), 34 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h b/Features/Ext4Pkg/Ext4Dxe= /Ext4Disk.h index a55cd2fa68ad..6f83dcf65429 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Disk.h @@ -171,7 +171,7 @@ #define EXT4_DIRTY_FL 0x00000100=0D #define EXT4_COMPRBLK_FL 0x00000200=0D #define EXT4_NOCOMPR_FL 0x00000400=0D -#define EXT4_ECOMPR_FL 0x00000800=0D +#define EXT4_ENCRYPT_FL 0x00000800=0D #define EXT4_BTREE_FL 0x00001000=0D #define EXT4_INDEX_FL 0x00002000=0D #define EXT4_JOURNAL_DATA_FL 0x00004000=0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h b/Features/Ext4Pkg/Ext4Dxe/= Ext4Dxe.h index b1508482b0a7..a1eb32aa2cff 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h @@ -31,7 +31,10 @@ =0D #include "Ext4Disk.h"=0D =0D -#define EXT4_NAME_MAX 255=0D +#define SYMLOOP_MAX 8=0D +#define EXT4_NAME_MAX 255=0D +#define EFI_PATH_MAX 4096=0D +#define EXT4_FAST_SYMLINK_SIZE 60=0D =0D #define EXT4_DRIVER_VERSION 0x0000=0D =0D @@ -324,11 +327,11 @@ number of read bytes. **/=0D EFI_STATUS=0D Ext4Read (=0D - IN EXT4_PARTITION *Partition,=0D - IN EXT4_FILE *File,=0D - OUT VOID *Buffer,=0D - IN UINT64 Offset,=0D - IN OUT UINTN *Length=0D + IN EXT4_PARTITION *Partition,=0D + IN EXT4_FILE *File,=0D + OUT VOID *Buffer,=0D + IN UINT64 Offset,=0D + IN OUT UINTN *Length=0D );=0D =0D /**=0D @@ -368,6 +371,7 @@ struct _Ext4File { =0D UINT64 OpenMode;=0D UINT64 Position;=0D + UINT32 Symloops;=0D =0D EXT4_PARTITION *Partition;=0D =0D @@ -497,6 +501,45 @@ Ext4SetupFile ( IN EXT4_PARTITION *Partition=0D );=0D =0D +/**=0D + Opens a new file relative to the source file's location.=0D +=0D + @param[out] FoundFile A pointer to the location to return the opened ha= ndle for the new=0D + file.=0D + @param[in] Source A pointer to the EXT4_FILE instance that is the f= ile=0D + handle to the source location. This would typical= ly be an open=0D + handle to a directory.=0D + @param[in] FileName The Null-terminated string of the name of the fil= e to be opened.=0D + The file name may contain the following path modi= fiers: "\", ".",=0D + and "..".=0D + @param[in] OpenMode The mode to open the file. The only valid combina= tions that the=0D + file may be opened with are: Read, Read/Write, or= Create/Read/Write.=0D + @param[in] Attributes Only valid for EFI_FILE_MODE_CREATE, in which cas= e these are the=0D + attribute bits for the newly created file.=0D +=0D + @retval EFI_SUCCESS The file was opened.=0D + @retval EFI_NOT_FOUND The specified file could not be found on th= e device.=0D + @retval EFI_NO_MEDIA The device has no medium.=0D + @retval EFI_MEDIA_CHANGED The device has a different medium in it or = the medium is no=0D + longer supported.=0D + @retval EFI_DEVICE_ERROR The device reported an error.=0D + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.=0D + @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or op= en a file for write=0D + when the media is write-protected.=0D + @retval EFI_ACCESS_DENIED The service denied access to the file.=0D + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open= the file.=0D + @retval EFI_VOLUME_FULL The volume is full.=0D +=0D +**/=0D +EFI_STATUS=0D +Ext4OpenInternal (=0D + OUT EXT4_FILE **FoundFile,=0D + IN EXT4_FILE *Source,=0D + IN CHAR16 *FileName,=0D + IN UINT64 OpenMode,=0D + IN UINT64 Attributes=0D + );=0D +=0D /**=0D Closes a file.=0D =0D @@ -774,6 +817,28 @@ Ext4FileIsDir ( IN CONST EXT4_FILE *File=0D );=0D =0D +/**=0D + Checks if a file is a symlink.=0D + @param[in] File Pointer to the opened file.=0D +=0D + @return TRUE if file is a symlink.=0D +**/=0D +BOOLEAN=0D +Ext4FileIsSymlink (=0D + IN CONST EXT4_FILE *File=0D + );=0D +=0D +/**=0D + Detects if a symlink is a fast symlink.=0D + @param[in] File Pointer to the opened file.=0D +=0D + @return TRUE if file is a fast symlink.=0D +**/=0D +BOOLEAN=0D +Ext4SymlinkIsFastSymlink (=0D + IN CONST EXT4_FILE *File=0D + );=0D +=0D /**=0D Checks if a file is a regular file.=0D @param[in] File Pointer to the opened file.=0D @@ -797,7 +862,7 @@ Ext4FileIsReg ( it's a regular file or a directory, since most other file types= =0D don't make sense under UEFI.=0D **/=0D -#define Ext4FileIsOpenable(File) (Ext4FileIsReg(File) || Ext4FileIsDir(Fi= le))=0D +#define Ext4FileIsOpenable(File) (Ext4FileIsReg (File) || Ext4FileIsDir (= File) || Ext4FileIsSymlink (File))=0D =0D #define EXT4_INODE_HAS_FIELD(Inode, Field) = \=0D (Inode->i_extra_isize + EXT4_GOOD_OLD_INODE_SIZE >=3D = \=0D @@ -935,6 +1000,26 @@ Ext4ReadDir ( IN OUT UINTN *OutLength=0D );=0D =0D +/**=0D + Reads a symlink file.=0D +=0D + @param[in] Partition Pointer to the ext4 partition.=0D + @param[in] File Pointer to the open symlink file.=0D + @param[out] Symlink Pointer to the output unicode symlink string= .=0D +=0D + @retval EFI_SUCCESS Symlink was read.=0D + @retval EFI_ACCESS_DENIED Symlink is encrypted.=0D + @retval EFI_OUT_OF_RESOURCES Memory allocation error.=0D + @retval EFI_INVALID_PARAMETER Symlink path has incorrect length=0D + @retval EFI_VOLUME_CORRUPTED Symlink read block size differ from inode = value=0D +**/=0D +EFI_STATUS=0D +Ext4ReadSymlink (=0D + IN EXT4_PARTITION *Partition,=0D + IN EXT4_FILE *File,=0D + OUT CHAR16 **Symlink=0D + );=0D +=0D /**=0D Initialises the (empty) extents map, that will work as a cache of exten= ts.=0D =0D diff --git a/Features/Ext4Pkg/Ext4Dxe/File.c b/Features/Ext4Pkg/Ext4Dxe/Fil= e.c index ff1746d5640a..0fb9e05e6647 100644 --- a/Features/Ext4Pkg/Ext4Dxe/File.c +++ b/Features/Ext4Pkg/Ext4Dxe/File.c @@ -134,14 +134,230 @@ Ext4DirCanLookup ( return (File->Inode->i_mode & EXT4_INO_PERM_EXEC_OWNER) =3D=3D EXT4_INO_= PERM_EXEC_OWNER;=0D }=0D =0D +/**=0D + Reads a fast symlink file.=0D +=0D + @param[in] Partition Pointer to the ext4 partition.=0D + @param[in] File Pointer to the open symlink file.=0D + @param[out] AsciiSymlink Pointer to the output ascii symlink str= ing.=0D + @param[out] AsciiSymlinkSize Pointer to the output ascii symlink str= ing length.=0D +=0D + @retval EFI_SUCCESS Fast symlink was read.=0D + @retval EFI_OUT_OF_RESOURCES Memory allocation error.=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +Ext4ReadFastSymlink (=0D + IN EXT4_PARTITION *Partition,=0D + IN EXT4_FILE *File,=0D + OUT CHAR8 **AsciiSymlink,=0D + OUT UINT32 *AsciiSymlinkSize=0D + )=0D +{=0D + EFI_STATUS Status;=0D + CHAR8 *AsciiSymlinkTmp;=0D +=0D + AsciiSymlinkTmp =3D AllocatePool (EXT4_FAST_SYMLINK_SIZE + 1);=0D + if (AsciiSymlinkTmp =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + DEBUG ((DEBUG_FS, "[ext4] Failed to allocate symlink ascii string buff= er\n"));=0D + return Status;=0D + }=0D +=0D + CopyMem (AsciiSymlinkTmp, File->Inode->i_data, EXT4_FAST_SYMLINK_SIZE);= =0D +=0D + //=0D + // Add null-terminator=0D + //=0D + AsciiSymlinkTmp[EXT4_FAST_SYMLINK_SIZE] =3D '\0';=0D +=0D + *AsciiSymlink =3D AsciiSymlinkTmp;=0D + *AsciiSymlinkSize =3D EXT4_FAST_SYMLINK_SIZE + 1;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Reads a slow symlink file.=0D +=0D + @param[in] Partition Pointer to the ext4 partition.=0D + @param[in] File Pointer to the open symlink file.=0D + @param[out] AsciiSymlink Pointer to the output ascii symlink str= ing.=0D + @param[out] AsciiSymlinkSize Pointer to the output ascii symlink str= ing length.=0D +=0D + @retval EFI_SUCCESS Slow symlink was read.=0D + @retval EFI_OUT_OF_RESOURCES Memory allocation error.=0D + @retval EFI_INVALID_PARAMETER Slow symlink path has incorrect length=0D + @retval EFI_VOLUME_CORRUPTED Symlink read block size differ from inode = value=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +Ext4ReadSlowSymlink (=0D + IN EXT4_PARTITION *Partition,=0D + IN EXT4_FILE *File,=0D + OUT CHAR8 **AsciiSymlink,=0D + OUT UINT32 *AsciiSymlinkSize=0D + )=0D +{=0D + EFI_STATUS Status;=0D + CHAR8 *SymlinkTmp;=0D + UINT64 SymlinkSizeTmp;=0D + UINT32 SymlinkAllocateSize;=0D + UINTN ReadSize;=0D +=0D + SymlinkSizeTmp =3D EXT4_INODE_SIZE (File->Inode);=0D +=0D + //=0D + // Allocate EXT4_INODE_SIZE + 1=0D + //=0D + if (SymlinkSizeTmp <=3D EFI_PATH_MAX - 1) {=0D + SymlinkAllocateSize =3D (UINT32)SymlinkSizeTmp + 1;=0D + } else {=0D + DEBUG ((=0D + DEBUG_FS,=0D + "[ext4] Error! Symlink path maximum length was hit!\n"=0D + ));=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + SymlinkTmp =3D AllocatePool (SymlinkAllocateSize);=0D + if (SymlinkTmp =3D=3D NULL) {=0D + DEBUG ((DEBUG_FS, "[ext4] Failed to allocate symlink ascii string buff= er\n"));=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D +=0D + ReadSize =3D (UINTN)SymlinkSizeTmp;=0D + Status =3D Ext4Read (Partition, File, SymlinkTmp, File->Position, &Rea= dSize);=0D + if (!EFI_ERROR (Status)) {=0D + File->Position +=3D ReadSize;=0D + } else {=0D + DEBUG ((DEBUG_FS, "[ext4] Failed to read symlink from blocks with Stat= us %r\n", Status));=0D + FreePool (SymlinkTmp);=0D + return Status;=0D + }=0D +=0D + //=0D + // Add null-terminator=0D + //=0D + SymlinkTmp[SymlinkSizeTmp] =3D '\0';=0D +=0D + //=0D + // It is not clear, should we check that symlink allocation size is=0D + // equal symlink string size or not. However there is no checks in exist= ing=0D + // Ext4 implementations, so we also don't check it here relying on the f= act=0D + // we terminated string ourselves above.=0D + //=0D + // ASSERT (SymlinkAllocateSize =3D=3D AsciiStrSize (SymlinkTmp));=0D + //=0D +=0D + if (SymlinkSizeTmp !=3D ReadSize) {=0D + DEBUG ((=0D + DEBUG_FS,=0D + "[ext4] Error! The sz of the read block doesn't match the value from= the inode!\n"=0D + ));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + *AsciiSymlinkSize =3D SymlinkAllocateSize;=0D + *AsciiSymlink =3D SymlinkTmp;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Reads a symlink file.=0D +=0D + @param[in] Partition Pointer to the ext4 partition.=0D + @param[in] File Pointer to the open symlink file.=0D + @param[out] Symlink Pointer to the output unicode symlink string= .=0D +=0D + @retval EFI_SUCCESS Symlink was read.=0D + @retval EFI_ACCESS_DENIED Symlink is encrypted.=0D + @retval EFI_OUT_OF_RESOURCES Memory allocation error.=0D + @retval EFI_INVALID_PARAMETER Symlink path has incorrect length=0D + @retval EFI_VOLUME_CORRUPTED Symlink read block size differ from inode = value=0D +**/=0D +EFI_STATUS=0D +Ext4ReadSymlink (=0D + IN EXT4_PARTITION *Partition,=0D + IN EXT4_FILE *File,=0D + OUT CHAR16 **Symlink=0D + )=0D +{=0D + EFI_STATUS Status;=0D + CHAR8 *SymlinkTmp;=0D + UINT32 SymlinkSize;=0D + CHAR16 *Symlink16Tmp;=0D + CHAR16 *Needle;=0D +=0D + //=0D + // Assume that we alread read Inode via Ext4ReadInode=0D + // Skip reading, just check encryption flag=0D + //=0D + if ((File->Inode->i_flags & EXT4_ENCRYPT_FL) !=3D 0) {=0D + Status =3D EFI_ACCESS_DENIED;=0D + DEBUG ((DEBUG_FS, "[ext4] Error, symlink is encrypted\n"));=0D + return Status;=0D + }=0D +=0D + if (Ext4SymlinkIsFastSymlink (File)) {=0D + Status =3D Ext4ReadFastSymlink (Partition, File, &SymlinkTmp, &Symlink= Size);=0D + } else {=0D + Status =3D Ext4ReadSlowSymlink (Partition, File, &SymlinkTmp, &Symlink= Size);=0D + }=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_FS, "[ext4] Symlink read error with Status %r\n", Status= ));=0D + return Status;=0D + }=0D +=0D + Symlink16Tmp =3D AllocateZeroPool (SymlinkSize * sizeof (CHAR16));=0D + if (Symlink16Tmp =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + DEBUG ((DEBUG_FS, "[ext4] Failed to allocate symlink unicode string bu= ffer\n"));=0D + FreePool (SymlinkTmp);=0D + return Status;=0D + }=0D +=0D + Status =3D AsciiStrToUnicodeStrS (=0D + SymlinkTmp,=0D + Symlink16Tmp,=0D + SymlinkSize=0D + );=0D +=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((=0D + DEBUG_FS,=0D + "[ext4] Failed to convert ascii symlink to unicode with Status %r\n"= ,=0D + Status=0D + ));=0D + FreePool (Symlink16Tmp);=0D + FreePool (SymlinkTmp);=0D + return Status;=0D + }=0D +=0D + //=0D + // Convert to UEFI slashes=0D + //=0D + Needle =3D Symlink16Tmp;=0D + while ((Needle =3D StrStr (Needle, L"/")) !=3D NULL) {=0D + *Needle++ =3D L'\\';=0D + }=0D +=0D + *Symlink =3D Symlink16Tmp;=0D +=0D + FreePool (SymlinkTmp);=0D + return Status;=0D +}=0D +=0D /**=0D Opens a new file relative to the source file's location.=0D =0D - @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that = is the file=0D + @param[out] FoundFile A pointer to the location to return the opened ha= ndle for the new=0D + file.=0D + @param[in] Source A pointer to the EXT4_FILE instance that is the f= ile=0D handle to the source location. This would typical= ly be an open=0D handle to a directory.=0D - @param[out] NewHandle A pointer to the location to return the opened ha= ndle for the new=0D - file.=0D @param[in] FileName The Null-terminated string of the name of the fil= e to be opened.=0D The file name may contain the following path modi= fiers: "\", ".",=0D and "..".=0D @@ -165,13 +381,12 @@ Ext4DirCanLookup ( =0D **/=0D EFI_STATUS=0D -EFIAPI=0D -Ext4Open (=0D - IN EFI_FILE_PROTOCOL *This,=0D - OUT EFI_FILE_PROTOCOL **NewHandle,=0D - IN CHAR16 *FileName,=0D - IN UINT64 OpenMode,=0D - IN UINT64 Attributes=0D +Ext4OpenInternal (=0D + OUT EXT4_FILE **FoundFile,=0D + IN EXT4_FILE *Source,=0D + IN CHAR16 *FileName,=0D + IN UINT64 OpenMode,=0D + IN UINT64 Attributes=0D )=0D {=0D EXT4_FILE *Current;=0D @@ -180,13 +395,14 @@ Ext4Open ( CHAR16 PathSegment[EXT4_NAME_MAX + 1];=0D UINTN Length;=0D EXT4_FILE *File;=0D + CHAR16 *Symlink;=0D EFI_STATUS Status;=0D =0D - Current =3D (EXT4_FILE *)This;=0D + Current =3D Source;=0D Partition =3D Current->Partition;=0D Level =3D 0;=0D =0D - DEBUG ((DEBUG_FS, "[ext4] Ext4Open %s\n", FileName));=0D + DEBUG ((DEBUG_FS, "[ext4] Ext4OpenInternal %s\n", FileName));=0D // If the path starts with a backslash, we treat the root directory as t= he base directory=0D if (FileName[0] =3D=3D L'\\') {=0D FileName++;=0D @@ -194,6 +410,11 @@ Ext4Open ( }=0D =0D while (FileName[0] !=3D L'\0') {=0D + if (Partition->Root->Symloops > SYMLOOP_MAX) {=0D + DEBUG ((DEBUG_FS, "[ext4] Symloop limit is hit !\n"));=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D // Discard leading path separators=0D while (FileName[0] =3D=3D L'\\') {=0D FileName++;=0D @@ -238,18 +459,45 @@ Ext4Open ( }=0D =0D // Check if this is a valid file to open in EFI=0D -=0D - // What to do with symlinks? They're nonsense when absolute but may=0D - // be useful when they're relative. Right now, they're ignored, since = they=0D - // bring a lot of trouble for something that's not as useful in our ca= se.=0D - // If you want to link, use hard links.=0D -=0D if (!Ext4FileIsOpenable (File)) {=0D Ext4CloseInternal (File);=0D // This looks like an /okay/ status to return.=0D return EFI_ACCESS_DENIED;=0D }=0D =0D + //=0D + // Reading symlink and then trying to follow it=0D + //=0D + if (Ext4FileIsSymlink (File)) {=0D + Partition->Root->Symloops++;=0D + DEBUG ((DEBUG_FS, "[ext4] File %s is symlink, trying to read it\n", = PathSegment));=0D + Status =3D Ext4ReadSymlink (Partition, File, &Symlink);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_FS, "[ext4] Error reading %s symlink!\n", PathSegmen= t));=0D + return Status;=0D + }=0D +=0D + DEBUG ((DEBUG_FS, "[ext4] File %s is linked to %s\n", PathSegment, S= ymlink));=0D + //=0D + // Close symlink file=0D + //=0D + Ext4CloseInternal (File);=0D + //=0D + // Open linked file by recursive call of Ext4OpenFile=0D + //=0D + Status =3D Ext4OpenInternal (FoundFile, Current, Symlink, OpenMode, = Attributes);=0D + FreePool (Symlink);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_FS, "[ext4] Error opening linked file %s\n", Symlink= ));=0D + return Status;=0D + }=0D +=0D + //=0D + // Set File to newly opened=0D + //=0D + File =3D *FoundFile;=0D + }=0D +=0D if (Level !=3D 0) {=0D // Careful not to close the base directory=0D Ext4CloseInternal (Current);=0D @@ -273,12 +521,75 @@ Ext4Open ( return EFI_ACCESS_DENIED;=0D }=0D =0D - *NewHandle =3D &Current->Protocol;=0D + *FoundFile =3D Current;=0D =0D DEBUG ((DEBUG_FS, "[ext4] Opened filename %s\n", Current->Dentry->Name))= ;=0D return EFI_SUCCESS;=0D }=0D =0D +/**=0D + Opens a new file relative to the source file's location.=0D + @param[in] This A pointer to the EFI_FILE_PROTOCOL instance that = is the file=0D + handle to the source location. This would typical= ly be an open=0D + handle to a directory.=0D + @param[out] NewHandle A pointer to the location to return the opened ha= ndle for the new=0D + file.=0D + @param[in] FileName The Null-terminated string of the name of the fil= e to be opened.=0D + The file name may contain the following path modi= fiers: "\", ".",=0D + and "..".=0D + @param[in] OpenMode The mode to open the file. The only valid combina= tions that the=0D + file may be opened with are: Read, Read/Write, or= Create/Read/Write.=0D + @param[in] Attributes Only valid for EFI_FILE_MODE_CREATE, in which cas= e these are the=0D + attribute bits for the newly created file.=0D + @retval EFI_SUCCESS The file was opened.=0D + @retval EFI_NOT_FOUND The specified file could not be found on th= e device.=0D + @retval EFI_NO_MEDIA The device has no medium.=0D + @retval EFI_MEDIA_CHANGED The device has a different medium in it or = the medium is no=0D + longer supported.=0D + @retval EFI_DEVICE_ERROR The device reported an error.=0D + @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.=0D + @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or op= en a file for write=0D + when the media is write-protected.=0D + @retval EFI_ACCESS_DENIED The service denied access to the file.=0D + @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open= the file.=0D + @retval EFI_VOLUME_FULL The volume is full.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +Ext4Open (=0D + IN EFI_FILE_PROTOCOL *This,=0D + OUT EFI_FILE_PROTOCOL **NewHandle,=0D + IN CHAR16 *FileName,=0D + IN UINT64 OpenMode,=0D + IN UINT64 Attributes=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EXT4_FILE *FoundFile;=0D + EXT4_FILE *Source;=0D +=0D + Source =3D (EXT4_FILE *)This;=0D +=0D + //=0D + // Reset Symloops counter=0D + //=0D + Source->Partition->Root->Symloops =3D 0;=0D +=0D + Status =3D Ext4OpenInternal (=0D + &FoundFile,=0D + Source,=0D + FileName,=0D + OpenMode,=0D + Attributes=0D + );=0D +=0D + if (!EFI_ERROR (Status)) {=0D + *NewHandle =3D &FoundFile->Protocol;=0D + }=0D +=0D + return Status;=0D +}=0D +=0D /**=0D Closes a specified file handle.=0D =0D @@ -588,7 +899,7 @@ Ext4GetVolumeName ( =0D // s_volume_name is only valid on dynamic revision; old filesystems don'= t support this=0D if (Partition->SuperBlock.s_rev_level =3D=3D EXT4_DYNAMIC_REV) {=0D - CopyMem (TempVolName, (CONST CHAR8 *)Partition->SuperBlock.s_volume_na= me, 16);=0D + CopyMem (TempVolName, Partition->SuperBlock.s_volume_name, 16);=0D TempVolName[16] =3D '\0';=0D =0D Status =3D UTF8StrToUCS2 (TempVolName, &VolumeName);=0D @@ -754,12 +1065,14 @@ Ext4GetInfo ( OUT VOID *Buffer=0D )=0D {=0D + EXT4_FILE *File;=0D EXT4_PARTITION *Partition;=0D =0D - Partition =3D ((EXT4_FILE *)This)->Partition;=0D + File =3D (EXT4_FILE *)This;=0D + Partition =3D File->Partition;=0D =0D if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {=0D - return Ext4GetFileInfo ((EXT4_FILE *)This, Buffer, BufferSize);=0D + return Ext4GetFileInfo (File, Buffer, BufferSize);=0D }=0D =0D if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {=0D @@ -870,12 +1183,12 @@ Ext4SetInfo ( )=0D {=0D EXT4_FILE *File;=0D - EXT4_PARTITION *Part;=0D + EXT4_PARTITION *Partition;=0D =0D - File =3D (EXT4_FILE *)This;=0D - Part =3D File->Partition;=0D + File =3D (EXT4_FILE *)This;=0D + Partition =3D File->Partition;=0D =0D - if (Part->ReadOnly) {=0D + if (Partition->ReadOnly) {=0D return EFI_WRITE_PROTECTED;=0D }=0D =0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Inode.c b/Features/Ext4Pkg/Ext4Dxe/In= ode.c index 831f5946e870..e95299c3aee0 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Inode.c +++ b/Features/Ext4Pkg/Ext4Dxe/Inode.c @@ -255,6 +255,44 @@ Ext4FileIsDir ( return (File->Inode->i_mode & EXT4_INO_TYPE_DIR) =3D=3D EXT4_INO_TYPE_DI= R;=0D }=0D =0D +/**=0D + Checks if a file is a symlink.=0D + @param[in] File Pointer to the opened file.=0D +=0D + @return TRUE if file is a symlink.=0D +**/=0D +BOOLEAN=0D +Ext4FileIsSymlink (=0D + IN CONST EXT4_FILE *File=0D + )=0D +{=0D + return (File->Inode->i_mode & EXT4_INO_TYPE_SYMLINK) =3D=3D EXT4_INO_TYP= E_SYMLINK;=0D +}=0D +=0D +/**=0D + Detects if a symlink is a fast symlink.=0D + @param[in] File Pointer to the opened file.=0D +=0D + @return TRUE if file is a fast symlink.=0D +**/=0D +BOOLEAN=0D +Ext4SymlinkIsFastSymlink (=0D + IN CONST EXT4_FILE *File=0D + )=0D +{=0D + //=0D + // REF: https://github.com/heatd/Onyx/blob/master/kernel/kernel/fs/ext2/= symlink.cpp#L26=0D + // Essentially, we're comparing the extended attribute blocks=0D + // with the inode's i_blocks, and if it's zero we know the inode isn't s= toring=0D + // the link in filesystem blocks, so we look to the ext2_inode->i_data.= =0D + //=0D + INTN ExtAttrBlocks =3D File->Inode->i_file_acl ? (File->Partition->Bloc= kSize >> 9) : 0;=0D +=0D + return ( File->Inode->i_blocks =3D=3D ExtAttrBlocks=0D + && EXT4_INODE_SIZE (File->Inode) <=3D 60=0D + );=0D +}=0D +=0D /**=0D Checks if a file is a regular file.=0D @param[in] File Pointer to the opened file.=0D --=20 2.37.0