-
Notifications
You must be signed in to change notification settings - Fork 170
Check PIE binary for linux in Object.kind() #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
use alloc::vec::Vec; | ||
use core::convert::TryInto; | ||
use core::fmt::Debug; | ||
use core::mem; | ||
|
||
use crate::elf; | ||
use crate::elf::{DF_1_PIE, DT_FLAGS_1}; | ||
use crate::endian::{self, Endian, Endianness, U32}; | ||
use crate::pod::Pod; | ||
use crate::read::{ | ||
self, util, Architecture, ByteString, Bytes, Error, Export, FileFlags, Import, Object, | ||
ObjectKind, ReadError, ReadRef, SectionIndex, StringTable, SymbolIndex, | ||
}; | ||
use alloc::vec::Vec; | ||
use core::convert::TryInto; | ||
use core::fmt::Debug; | ||
use core::mem; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't move these. |
||
use super::{ | ||
CompressionHeader, Dyn, ElfComdat, ElfComdatIterator, ElfDynamicRelocationIterator, ElfSection, | ||
|
@@ -297,8 +297,29 @@ where | |
match self.header.e_type(self.endian) { | ||
elf::ET_REL => ObjectKind::Relocatable, | ||
elf::ET_EXEC => ObjectKind::Executable, | ||
// TODO: check for `DF_1_PIE`? | ||
elf::ET_DYN => ObjectKind::Dynamic, | ||
elf::ET_DYN => { | ||
let mut is_pie = false; | ||
let table: &SectionTable<'_, Elf, R> = self.elf_section_table(); | ||
if let Ok(Some(dyn_sec)) = table.dynamic(self.endian(), self.data()) { | ||
for v in dyn_sec.0 { | ||
let tag = v.tag32(self.endian()); | ||
let val = v.val32(self.endian()); | ||
if let Some(tag) = tag { | ||
if let Some(val) = val { | ||
if tag == DT_FLAGS_1 && val & DF_1_PIE == DF_1_PIE { | ||
is_pie = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLVM only started setting this flag in 2020: https://reviews.llvm.org/D80872 Any ET_DYN file with non-zero e_entry and/or a PT_INTERP can be run as executable. And it is possible to make a file that is both a valid executable and a valid shared library. For example the dynamic linker on Linux generally is both. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So maybe we should replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fiy this is how I originally wanted to check the permissions but it's hard to implement it here (i need to know the path) |
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if is_pie { | ||
ObjectKind::PIE | ||
} else { | ||
ObjectKind::Dynamic | ||
} | ||
} | ||
elf::ET_CORE => ObjectKind::Core, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain more about how you intend to use this, or even better give a link to the code where you use this. Do you actually need The question mark on the TODO was because I wasn't sure this is something we actually want. We also need to be careful because existing users may not want this change, so we'll need to review those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Actually I need this for addr2line crate of yours https://github.com/AFLplusplus/LibAFL/blob/main/libafl_qemu/src/modules/utils/addr2line.rs#L111
To detect this difference is necessary for me to use addr2line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So case 1 is |
||
_ => ObjectKind::Unknown, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't import these. Use
elf::DF_1_PIE
etc.