* [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks
@ 2013-11-21 0:34 Geyslan G. Bem
2013-11-21 1:39 ` Al Viro
0 siblings, 1 reply; 4+ messages in thread
From: Geyslan G. Bem @ 2013-11-21 0:34 UTC (permalink / raw)
To: viro; +Cc: linux-fsdevel, linux-kernel, kernel-br, Geyslan G. Bem
The member 'e_ehsize' that holds the ELF header size is compared
with the elfhdr struct size. If not equal, goes out.
If 'e_phoff' holds 0 the object has no program header table, so
goes out.
Ensures the file being loaded has the correct data encoding, checking
'e_ident[EI_DATA]' against 'ELF_DATA'.
Besides the checks being in accordance with the ELF Specifications,
they increase the binary consistency reducing the use of malformed ones.
Signed-off-by: Geyslan G. Bem <geyslan@gmail.com>
---
fs/binfmt_elf.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 571a423..326ca39 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -603,14 +603,20 @@ static int load_elf_binary(struct linux_binprm *bprm)
if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
goto out;
+ if (loc->elf_ex.e_ehsize != sizeof(struct elfhdr))
+ goto out;
if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
goto out;
if (!elf_check_arch(&loc->elf_ex))
goto out;
+ if (loc->elf_ex.e_ident[EI_DATA] != ELF_DATA)
+ goto out;
if (!bprm->file->f_op->mmap)
goto out;
/* Now read in all of the header information */
+ if (loc->elf_ex.e_phoff == 0)
+ goto out;
if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
goto out;
if (loc->elf_ex.e_phnum < 1 ||
--
1.8.4.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks
2013-11-21 0:34 [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks Geyslan G. Bem
@ 2013-11-21 1:39 ` Al Viro
2013-11-21 10:52 ` Geyslan Gregório Bem
0 siblings, 1 reply; 4+ messages in thread
From: Al Viro @ 2013-11-21 1:39 UTC (permalink / raw)
To: Geyslan G. Bem; +Cc: linux-fsdevel, linux-kernel, kernel-br
On Wed, Nov 20, 2013 at 09:34:31PM -0300, Geyslan G. Bem wrote:
> The member 'e_ehsize' that holds the ELF header size is compared
> with the elfhdr struct size. If not equal, goes out.
> If 'e_phoff' holds 0 the object has no program header table, so
> goes out.
> Ensures the file being loaded has the correct data encoding, checking
> 'e_ident[EI_DATA]' against 'ELF_DATA'.
>
> Besides the checks being in accordance with the ELF Specifications,
> they increase the binary consistency reducing the use of malformed ones.
This is completely misguided. We are allowed to reject such binaries,
but what's the point of doing that?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks
2013-11-21 1:39 ` Al Viro
@ 2013-11-21 10:52 ` Geyslan Gregório Bem
2013-11-22 9:45 ` Geyslan Gregório Bem
0 siblings, 1 reply; 4+ messages in thread
From: Geyslan Gregório Bem @ 2013-11-21 10:52 UTC (permalink / raw)
To: Al Viro
Cc: open list:FILESYSTEMS (VFS...), linux-kernel@vger.kernel.org (open list),
LKML, kernel-br
2013/11/20 Al Viro <viro@zeniv.linux.org.uk>:
> On Wed, Nov 20, 2013 at 09:34:31PM -0300, Geyslan G. Bem wrote:
>> The member 'e_ehsize' that holds the ELF header size is compared
>> with the elfhdr struct size. If not equal, goes out.
>> If 'e_phoff' holds 0 the object has no program header table, so
>> goes out.
>> Ensures the file being loaded has the correct data encoding, checking
>> 'e_ident[EI_DATA]' against 'ELF_DATA'.
>>
>> Besides the checks being in accordance with the ELF Specifications,
>> they increase the binary consistency reducing the use of malformed ones.
>
> This is completely misguided. We are allowed to reject such binaries,
> but what's the point of doing that?
Viro, First of all, thanks for reply.
The security (or anti-security) guys are used to mess up with the not checked
header fields for their "benefits": anti-debugging, injection and so on.
Concerning to 'e_phoff': when it is 0 the check avoids that 'elf_phdr' been read
from a erroneous offset (ELF header). I know that without this check the binary
will goes out anyway. But it reduces wasting cpu cycles.
Regarding 'e_ident[EI_DATA]': that check also prevents a farther code reading
when the binary, although been the correct arch, is compiled with a different
data encoding (MSB vs LSB).
So checking besides increase the binary consistency, guarantee some mislead
and fewer cpu cycles.
--
Regards,
Geyslan G. Bem
hackingbits.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks
2013-11-21 10:52 ` Geyslan Gregório Bem
@ 2013-11-22 9:45 ` Geyslan Gregório Bem
0 siblings, 0 replies; 4+ messages in thread
From: Geyslan Gregório Bem @ 2013-11-22 9:45 UTC (permalink / raw)
To: Al Viro
Cc: open list:FILESYSTEMS (VFS...), linux-kernel@vger.kernel.org (open list),
LKML, kernel-br
2013/11/21 Geyslan Gregório Bem <geyslan@gmail.com>:
> 2013/11/20 Al Viro <viro@zeniv.linux.org.uk>:
>> On Wed, Nov 20, 2013 at 09:34:31PM -0300, Geyslan G. Bem wrote:
>>> The member 'e_ehsize' that holds the ELF header size is compared
>>> with the elfhdr struct size. If not equal, goes out.
>>> If 'e_phoff' holds 0 the object has no program header table, so
>>> goes out.
>>> Ensures the file being loaded has the correct data encoding, checking
>>> 'e_ident[EI_DATA]' against 'ELF_DATA'.
>>>
>>> Besides the checks being in accordance with the ELF Specifications,
>>> they increase the binary consistency reducing the use of malformed ones.
>>
>> This is completely misguided. We are allowed to reject such binaries,
>> but what's the point of doing that?
>
> Viro, First of all, thanks for reply.
>
> The security (or anti-security) guys are used to mess up with the not checked
> header fields for their "benefits": anti-debugging, injection and so on.
>
> Concerning to 'e_phoff': when it is 0 the check avoids that 'elf_phdr' been read
> from a erroneous offset (ELF header). I know that without this check the binary
> will goes out anyway. But it reduces wasting cpu cycles.
>
> Regarding 'e_ident[EI_DATA]': that check also prevents a farther code reading
> when the binary, although been the correct arch, is compiled with a different
> data encoding (MSB vs LSB).
>
> So checking besides increase the binary consistency, guarantee some mislead
> and fewer cpu cycles.
>
> --
> Regards,
>
> Geyslan G. Bem
> hackingbits.com
Another good reason is that ld does reject such binaries (I hex edit
one to hold MSB value in header):
uzumaki@hb ~ $ /lib/ld-linux-x86-64.so.2 ./a.out
./a.out: error while loading shared libraries: ./a.out: ELF file data
encoding not little-endian
After zeroing the phoff:
uzumaki@hb ~ $ /lib/ld-linux-x86-64.so.2 ./a.out
./a.out: error while loading shared libraries: ./a.out: object file
has no loadable segments
I really think that is a way to get a more robust binfmt consistency check.
What you think?
--
Regards,
Geyslan G. Bem
hackingbits.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-11-22 9:45 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-21 0:34 [PATCH v2] fs: binfmt_elf: Add ELF header consistency checks Geyslan G. Bem
2013-11-21 1:39 ` Al Viro
2013-11-21 10:52 ` Geyslan Gregório Bem
2013-11-22 9:45 ` Geyslan Gregório Bem
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).