Skip to content

Commit e5629b7

Browse files
flandrplusvic
authored andcommitted
elf: validate input buffer length <= elf header size (#748)
1 parent 3e4fbdb commit e5629b7

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

libyara/exefiles.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,30 @@ int yr_get_elf_type(
139139

140140
elf_ident = (elf_ident_t*) buffer;
141141

142-
if (yr_le32toh(elf_ident->magic) == ELF_MAGIC)
143-
{
144-
return elf_ident->_class;
145-
}
146-
else
142+
if (yr_le32toh(elf_ident->magic) != ELF_MAGIC)
147143
{
148144
return 0;
149145
}
146+
147+
switch (elf_ident->_class) {
148+
case ELF_CLASS_32:
149+
if (buffer_length < sizeof(elf32_header_t))
150+
{
151+
return 0;
152+
}
153+
break;
154+
case ELF_CLASS_64:
155+
if (buffer_length < sizeof(elf64_header_t))
156+
{
157+
return 0;
158+
}
159+
break;
160+
default:
161+
/* Unexpected class */
162+
return 0;
163+
}
164+
165+
return elf_ident->_class;
150166
}
151167

152168

0 commit comments

Comments
 (0)