summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2026-11-25 17:52:28 +0800
committerGitHub <[email protected]>2026-11-25 17:52:28 +0800
commit26d148c3c71f9101fb285247ce8ccb0ae3efddc1 (patch)
tree416d4d3ab93ae9d0e7c977c4de6478d31a9d829e
parent07fcaa6928d96ace89bf5afe90435d63a2bc9251 (diff)
[dfs][iso9660] fix: validate directory record name length (#12062)
-rw-r--r--components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c98
1 files changed, 76 insertions, 22 deletions
diff --git a/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c
index 2358280a79..1b6cdec46d 100644
--- a/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c
+++ b/components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c
@@ -215,6 +215,41 @@ static time_t iso9660_convert_unixtime2(struct iso9660_date2 *date)
return mktime(&tm);
}
+/*
+ * Advance to the next directory record. The caller must have validated that
+ * the current record length fits in the current sector.
+ *
+ * Returns 0 on success, -1 at the end of the directory.
+ */
+static int iso9660_next_dirent(struct iso9660 *iso,
+ struct iso9660_dir **dirent, rt_uint32_t *lba, rt_size_t *sz)
+{
+ rt_uint32_t dlen = rt_le32_to_cpu((*dirent)->len);
+
+ *dirent = (void *)*dirent + dlen;
+ *sz += dlen;
+
+ if (ISO9660_BLKSZ - *sz < sizeof(**dirent))
+ {
+ /* Sector end, goto the next sector */
+ if (rt_device_read(iso->dev, ++*lba, iso->swap, 1) <= 0)
+ {
+ return -1;
+ }
+
+ *dirent = (void *)iso->swap;
+ *sz = 0;
+ }
+
+ if (rt_le32_to_cpu((*dirent)->first_sector) == 0)
+ {
+ /* Is end, no found. */
+ return -1;
+ }
+
+ return 0;
+}
+
static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
struct iso9660_iterate *it)
{
@@ -264,32 +299,45 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
do {
/* Ignore "." and ".." */
- do {
+ for (;;)
+ {
rt_uint32_t dlen = rt_le32_to_cpu(dirent->len);
+ rt_uint32_t dnamelen = rt_le32_to_cpu(dirent->namelen);
- dirent = (void *)dirent + dlen;
- sz += dlen;
+ /*
+ * Both the record length and the name length come from the
+ * medium: make sure the record is complete inside the current
+ * sector and that the name is contained in the record before
+ * reading name[0] or passing it to the converters.
+ */
+ if (dnamelen == 0 ||
+ dlen < sizeof(*dirent) ||
+ dnamelen > dlen - sizeof(*dirent) ||
+ dlen > ISO9660_BLKSZ - sz)
+ {
+ goto _fail;
+ }
- if (ISO9660_BLKSZ - sz < sizeof(*dirent))
+ if (dirent->name[0] >> 1 != 0 || dnamelen != 1)
{
- /* Sector end, goto the next sector */
- if (rt_device_read(iso->dev, ++lba, iso->swap, 1) <= 0)
- {
- goto _fail;
- }
- dirent = (void *)iso->swap;
- sz = 0;
+ /* This is not "." or "..", use this record. */
+ break;
}
- if (rt_le32_to_cpu(dirent->first_sector) == 0)
+ if (iso9660_next_dirent(iso, &dirent, &lba, &sz) < 0)
{
- /* Is end, no found. */
goto _fail;
}
- } while (dirent->name[0] >> 1 == 0 && rt_le32_to_cpu(dirent->namelen) == 1);
+ }
namelen = rt_le32_to_cpu(dirent->namelen);
+ /* The name length comes from the medium, never trust it */
+ if (namelen >= MAX_NAMELEN)
+ {
+ namelen = MAX_NAMELEN - 1;
+ }
+
if (iso->joliet)
{
iso9660_convert_string(sname, (rt_uint16_t *)dirent->name, namelen);
@@ -298,8 +346,11 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
{
if (!(rt_le32_to_cpu(dirent->flags) & FLAG_TYPE_DIR))
{
- /* Remove ";1" */
- namelen -= 2;
+ /* Remove ";1", but only when the name is long enough */
+ if (namelen >= 2)
+ {
+ namelen -= 2;
+ }
}
iso9660_convert_lower(sname, (rt_uint8_t *)dirent->name, namelen);
@@ -336,12 +387,8 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
/* Iterate end */
return RT_NULL;
}
-
- /* No next entry */
- continue;
}
-
- if (!rt_strncmp(sname, path, len))
+ else if (!rt_strncmp(sname, path, len))
{
/* The end of path, found ok */
if (!path[len])
@@ -350,7 +397,7 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
return fd;
}
- /* Next entry */
+ /* Enter the sub directory */
lba = rt_le32_to_cpu(dirent->first_sector);
if (rt_device_read(iso->dev, lba, iso->swap, 1) <= 0)
{
@@ -361,6 +408,13 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
path += len + 1;
len = strchrnul(path, '/') - path;
+ continue;
+ }
+
+ /* Not the entry we are looking for, or more entries requested */
+ if (iso9660_next_dirent(iso, &dirent, &lba, &sz) < 0)
+ {
+ goto _fail;
}
} while (len);