Skip to content

Commit 26d148c

Browse files
authored
[dfs][iso9660] fix: validate directory record name length (#12062)
1 parent 07fcaa6 commit 26d148c

1 file changed

Lines changed: 77 additions & 23 deletions

File tree

‎components/dfs/dfs_v1/filesystems/iso9660/dfs_iso9660.c‎

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,41 @@ static time_t iso9660_convert_unixtime2(struct iso9660_date2 *date)
215215
return mktime(&tm);
216216
}
217217

218+
/*
219+
* Advance to the next directory record. The caller must have validated that
220+
* the current record length fits in the current sector.
221+
*
222+
* Returns 0 on success, -1 at the end of the directory.
223+
*/
224+
static int iso9660_next_dirent(struct iso9660 *iso,
225+
struct iso9660_dir **dirent, rt_uint32_t *lba, rt_size_t *sz)
226+
{
227+
rt_uint32_t dlen = rt_le32_to_cpu((*dirent)->len);
228+
229+
*dirent = (void *)*dirent + dlen;
230+
*sz += dlen;
231+
232+
if (ISO9660_BLKSZ - *sz < sizeof(**dirent))
233+
{
234+
/* Sector end, goto the next sector */
235+
if (rt_device_read(iso->dev, ++*lba, iso->swap, 1) <= 0)
236+
{
237+
return -1;
238+
}
239+
240+
*dirent = (void *)iso->swap;
241+
*sz = 0;
242+
}
243+
244+
if (rt_le32_to_cpu((*dirent)->first_sector) == 0)
245+
{
246+
/* Is end, no found. */
247+
return -1;
248+
}
249+
250+
return 0;
251+
}
252+
218253
static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
219254
struct iso9660_iterate *it)
220255
{
@@ -264,32 +299,45 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
264299

265300
do {
266301
/* Ignore "." and ".." */
267-
do {
302+
for (;;)
303+
{
268304
rt_uint32_t dlen = rt_le32_to_cpu(dirent->len);
305+
rt_uint32_t dnamelen = rt_le32_to_cpu(dirent->namelen);
306+
307+
/*
308+
* Both the record length and the name length come from the
309+
* medium: make sure the record is complete inside the current
310+
* sector and that the name is contained in the record before
311+
* reading name[0] or passing it to the converters.
312+
*/
313+
if (dnamelen == 0 ||
314+
dlen < sizeof(*dirent) ||
315+
dnamelen > dlen - sizeof(*dirent) ||
316+
dlen > ISO9660_BLKSZ - sz)
317+
{
318+
goto _fail;
319+
}
269320

270-
dirent = (void *)dirent + dlen;
271-
sz += dlen;
272-
273-
if (ISO9660_BLKSZ - sz < sizeof(*dirent))
321+
if (dirent->name[0] >> 1 != 0 || dnamelen != 1)
274322
{
275-
/* Sector end, goto the next sector */
276-
if (rt_device_read(iso->dev, ++lba, iso->swap, 1) <= 0)
277-
{
278-
goto _fail;
279-
}
280-
dirent = (void *)iso->swap;
281-
sz = 0;
323+
/* This is not "." or "..", use this record. */
324+
break;
282325
}
283326

284-
if (rt_le32_to_cpu(dirent->first_sector) == 0)
327+
if (iso9660_next_dirent(iso, &dirent, &lba, &sz) < 0)
285328
{
286-
/* Is end, no found. */
287329
goto _fail;
288330
}
289-
} while (dirent->name[0] >> 1 == 0 && rt_le32_to_cpu(dirent->namelen) == 1);
331+
}
290332

291333
namelen = rt_le32_to_cpu(dirent->namelen);
292334

335+
/* The name length comes from the medium, never trust it */
336+
if (namelen >= MAX_NAMELEN)
337+
{
338+
namelen = MAX_NAMELEN - 1;
339+
}
340+
293341
if (iso->joliet)
294342
{
295343
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,
298346
{
299347
if (!(rt_le32_to_cpu(dirent->flags) & FLAG_TYPE_DIR))
300348
{
301-
/* Remove ";1" */
302-
namelen -= 2;
349+
/* Remove ";1", but only when the name is long enough */
350+
if (namelen >= 2)
351+
{
352+
namelen -= 2;
353+
}
303354
}
304355

305356
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,
336387
/* Iterate end */
337388
return RT_NULL;
338389
}
339-
340-
/* No next entry */
341-
continue;
342390
}
343-
344-
if (!rt_strncmp(sname, path, len))
391+
else if (!rt_strncmp(sname, path, len))
345392
{
346393
/* The end of path, found ok */
347394
if (!path[len])
@@ -350,7 +397,7 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
350397
return fd;
351398
}
352399

353-
/* Next entry */
400+
/* Enter the sub directory */
354401
lba = rt_le32_to_cpu(dirent->first_sector);
355402
if (rt_device_read(iso->dev, lba, iso->swap, 1) <= 0)
356403
{
@@ -361,6 +408,13 @@ static struct iso9660_fd *iso9660_lookup(struct iso9660 *iso, const char *path,
361408

362409
path += len + 1;
363410
len = strchrnul(path, '/') - path;
411+
continue;
412+
}
413+
414+
/* Not the entry we are looking for, or more entries requested */
415+
if (iso9660_next_dirent(iso, &dirent, &lba, &sz) < 0)
416+
{
417+
goto _fail;
364418
}
365419
} while (len);
366420

0 commit comments

Comments
 (0)