Skip to content

Commit 3caddc1

Browse files
sectroyerzyv
authored andcommitted
Ticket #5084: fix integer overflow in binary CPIO handler
Add a bounds check on st_size immediately after it is computed, before it is used in any arithmetic or allocation. A reasonable upper bound for a symlink target is MC_MAXPATHLEN (4096). Signed-off-by: Michał Majchrowicz <sectroyer@gmail.com> Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
1 parent b6012a2 commit 3caddc1

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/vfs/cpio/cpio.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ cpio_create_entry (struct vfs_class *me, struct vfs_s_super *super, struct stat
518518
// FIXME: do we must read from arch->fd in case of inode != NULL only or in any
519519
// case?
520520

521-
inode->linkname = g_malloc (st->st_size + 1);
521+
if (st->st_size < 0 || (gsize) st->st_size >= G_MAXSIZE)
522+
return STATUS_FAIL;
523+
inode->linkname = g_malloc ((gsize) st->st_size + 1);
522524

523525
if (mc_read (arch->fd, inode->linkname, st->st_size) < st->st_size)
524526
{
@@ -597,7 +599,12 @@ cpio_read_bin_head (struct vfs_class *me, struct vfs_s_super *super)
597599
st.st_rdev = u.buf.c_rdev;
598600
#endif
599601
st.st_size = ((off_t) u.buf.c_filesizes[0] << 16) | u.buf.c_filesizes[1];
600-
602+
if (st.st_size < 0 || st.st_size > MC_MAXPATHLEN)
603+
{
604+
message (D_ERROR, MSG_ERROR, _ ("Corrupted cpio header encountered in\n%s"), super->name);
605+
g_free (name);
606+
return STATUS_FAIL;
607+
}
601608
vfs_zero_stat_times (&st);
602609

603610
st.st_atime = st.st_mtime = st.st_ctime =
@@ -751,7 +758,12 @@ cpio_read_crc_head (struct vfs_class *me, struct vfs_s_super *super)
751758
u.st.st_rdev = makedev (hd.c_rdev, hd.c_rdevmin);
752759
#endif
753760
u.st.st_size = hd.c_filesize;
754-
761+
if (u.st.st_size < 0 || u.st.st_size > MC_MAXPATHLEN)
762+
{
763+
message (D_ERROR, MSG_ERROR, _ ("Corrupted cpio header encountered in\n%s"), super->name);
764+
g_free (name);
765+
return STATUS_FAIL;
766+
}
755767
vfs_zero_stat_times (&u.st);
756768
u.st.st_atime = u.st.st_mtime = u.st.st_ctime = hd.c_mtime;
757769

0 commit comments

Comments
 (0)