Skip to content
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

chore: update lief #23

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lief https://github.com/lief-project/LIEF/ 9cf3b56cf004407c56695a61688ecc8ac511ef0e
lief https://github.com/lief-project/LIEF/ b183666a082d19ffc91ed0763f49e1d4f3814a59
vendorpull https://github.com/jviotti/vendorpull 8445d0d0b15d308906ee395cc3313cba461865c4
14 changes: 5 additions & 9 deletions vendor/lief/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
cmake_minimum_required(VERSION 3.5)

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Modules
Expand Down Expand Up @@ -464,15 +469,6 @@ endif()

target_compile_definitions(LIB_LIEF PUBLIC -D_GLIBCXX_USE_CXX11_ABI=1)

# Enable support for MD2 and MD4 for parsing the Authenticode sigs of older
# executables. Also, some older signed executables use certs with the
# SpcSpAgencyInfo Critical Extension, which mbed TLS doesn't support, so set
# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION to have it skip this
# extension.
add_definitions(-DMBEDTLS_FS_IO -DMBEDTLS_PEM_PARSE_C -DMBEDTLS_BIGNUM_C
-DMBEDTLS_X509_CRT_PARSE_C -DMBEDTLS_PEM_WRITE_C
-DMBEDTLS_PKCS1_V15 -DMBEDTLS_PKCS1_V21)

# LIEF Sanitizer options
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set(SANITIZER_FLAGS -fno-omit-frame-pointer -g -O1)
Expand Down
12 changes: 10 additions & 2 deletions vendor/lief/api/c/MachO/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ using namespace LIEF::MachO;

Macho_Binary_t** macho_parse(const char *file) {
FatBinary* fat = Parser::parse(file).release();
if (fat == nullptr) {
return nullptr;
}

size_t nb_bin = fat->size();

auto** c_macho_binaries = static_cast<Macho_Binary_t**>(
malloc((fat->size() + 1) * sizeof(Macho_Binary_t**)));

for (size_t i = 0; i < fat->size(); ++i) {
for (size_t i = 0; i < nb_bin; ++i) {
Binary* binary = fat->at(i);
if (binary != nullptr) {
c_macho_binaries[i] = static_cast<Macho_Binary_t*>(malloc(sizeof(Macho_Binary_t)));
init_c_binary(c_macho_binaries[i], binary);
}
}

c_macho_binaries[fat->size()] = nullptr;
fat->release_all_binaries();

c_macho_binaries[nb_bin] = nullptr;
delete fat;

return c_macho_binaries;
}
Expand Down
6 changes: 6 additions & 0 deletions vendor/lief/api/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
cmake_minimum_required(VERSION 3.1)

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

include(ExternalProject)
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/api/python/DEX/objects/pyType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void create<Type>(py::module& m) {

case Type::TYPES::CLASS:
{
return py::cast(type.cls());
return py::cast(type.cls(), py::return_value_policy::reference);
}

case Type::TYPES::PRIMITIVE:
Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/api/python/ELF/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ void create<Binary>(py::module& m) {
"permutation"_a)

.def("write",
&Binary::write,
static_cast<void (Binary::*)(const std::string&)>(&Binary::write),
"Rebuild the binary and write it in a file",
"output"_a,
py::return_value_policy::reference_internal)
Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/api/python/ELF/objects/pyBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void create<Builder>(py::module& m) {
py::return_value_policy::reference_internal)

.def("write",
&Builder::write,
static_cast<void (Builder::*)(const std::string&) const>(&Builder::write),
"Write the build result into the ``output`` file",
"output"_a)

Expand Down
6 changes: 4 additions & 2 deletions vendor/lief/api/python/MachO/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void create<Binary>(py::module& m) {
"address"_a)

.def("write",
&Binary::write,
static_cast<void (Binary::*)(const std::string&)>(&Binary::write),
"Rebuild the binary and write and write its content if the file given in parameter",
"output"_a,
py::return_value_policy::reference_internal)
Expand Down Expand Up @@ -584,7 +584,9 @@ void create<Binary>(py::module& m) {
py::return_value_policy::reference_internal)

.def("shift",
&Binary::shift,
[] (Binary& self, size_t width) {
return error_or(&Binary::shift, self, width);
},
R"delim(
Shift the content located right after the Load commands table.
This operation can be used to add a new command
Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/api/python/PE/objects/pyBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void create<Binary>(py::module& m) {
"Remove all imported libraries")

.def("write",
&Binary::write,
static_cast<void (Binary::*)(const std::string&)>(&Binary::write),
"Build the binary and write the result to the given ``output`` file",
"output_path"_a)

Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/api/python/PE/objects/pyBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void create<Builder>(py::module& m) {
py::return_value_policy::reference)

.def("write",
&Builder::write,
static_cast<void (Builder::*)(const std::string&) const>(&Builder::write),
"Write the build result into the ``output`` file",
"output"_a)

Expand Down
2 changes: 1 addition & 1 deletion vendor/lief/cmake/LIEFOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cmake_dependent_option(LIEF_OAT "Build LIEF with OAT module" ON
"LIEF_ELF;LIEF_DEX" OFF)

# VDEX format depends on the DEX module
cmake_dependent_option(LIEF_VDEX "Build LIEF with DEX module" ON
cmake_dependent_option(LIEF_VDEX "Build LIEF with VDEX module" ON
"LIEF_DEX" OFF)

# Sanitizer
Expand Down
7 changes: 7 additions & 0 deletions vendor/lief/doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Changelog

:ELF:

* Fix a heap overflow found by :github_user:`CCWANG19` (:issue:`763`)
* :github_user:`aeflores` fixed an issue when there are multiple versions associated with a symbol
(see: :issue:`749` for the details).
* Handle binaries compiled with the `-static-pie` flag correctly (see: :issue:`747`)
* Add support for modifying section-less binaries. The ELF :class:`~lief.ELF.Section` objects gain
the :meth:`lief.ELF.Section.as_frame` method which defines the section as a *framed* section.

Expand All @@ -27,6 +31,7 @@ Changelog

:MachO:

* Fix a segfault when the Mach-O binary does not have segments (found by :github_user:`CCWANG19` via :issue:`764`)
* Enable to create exports
* Fix the layout of the binaries modified by LIEF such as they can be (re)signed.
* Add support for `LC_DYLD_CHAINED_FIXUPS` and `LC_DYLD_EXPORTS_TRIE`
Expand Down Expand Up @@ -56,6 +61,8 @@ Changelog

:General Design:

* :github_user:`ZehMatt` added the support to write LIEF binaries object through a `std::ostream` interface
(:commit:`9d55f538602989c69454639565910884c5c5ac7c`)
* Remove the exceptions


Expand Down
6 changes: 3 additions & 3 deletions vendor/lief/doc/sphinx/tutorials/03_elf_change_symbols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Basically, this program takes an integer as argument and performs some computati

.. code-block:: console

$ hasme 123
$ hashme 123
228886645.836282

.. image:: ../_static/tutorial/03/hashme.png
Expand All @@ -86,7 +86,7 @@ First we have to load both the library and the binary:
import lief


hasme = lief.parse("hasme")
hashme = lief.parse("hashme")
libm = lief.parse("/usr/lib/libm.so.6")

Then when change the name of the two imported functions in the **binary**:
Expand All @@ -109,7 +109,7 @@ finally we swap ``log`` with ``sin`` and ``pow`` with ``cos`` in the **library**
import lief


hasme = lief.parse("hasme")
hashme = lief.parse("hashme")
libm = lief.parse("/usr/lib/libm.so.6")


Expand Down
3 changes: 2 additions & 1 deletion vendor/lief/include/LIEF/Abstract/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class LIEF_API Binary : public Object {
virtual uint64_t imagebase() const = 0;

//! Constructor functions that are called prior any other functions
virtual LIEF::Binary::functions_t ctor_functions() const = 0;
virtual functions_t ctor_functions() const = 0;

//! Convert the given offset into a virtual address.
//!
Expand All @@ -195,6 +195,7 @@ class LIEF_API Binary : public Object {

//! Build & transform the Binary object representation into a *real* executable
virtual void write(const std::string& name) = 0;
virtual void write(std::ostream& os) = 0;

LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& binary);

Expand Down
5 changes: 4 additions & 1 deletion vendor/lief/include/LIEF/BinaryStream/BinaryStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ class BinaryStream {
}
// Even though offset + size < ... => offset < ...
// the addition could overflow so it's worth checking both
const bool read_ok = offset <= this->size() && (offset + size) <= this->size();
const bool read_ok = offset <= this->size() && (offset + size) <= this->size()
/* Check for an overflow */
&& (static_cast<int64_t>(offset) >= 0 && static_cast<int64_t>(size) >= 0)
&& (static_cast<int64_t>(offset + size) >= 0);
if (!read_ok) {
return make_error_code(lief_errors::read_error);
}
Expand Down
4 changes: 3 additions & 1 deletion vendor/lief/include/LIEF/DEX/Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifndef LIEF_DEX_CLASS_H_
#define LIEF_DEX_CLASS_H_

#include <climits>

#include "LIEF/visibility.h"
#include "LIEF/Object.hpp"
#include "LIEF/iterators.hpp"
Expand Down Expand Up @@ -129,7 +131,7 @@ class LIEF_API Class : public Object {
fields_t fields_;
std::string source_filename_;

uint32_t original_index_ = -1u;
uint32_t original_index_ = UINT_MAX;
};

} // Namespace DEX
Expand Down
4 changes: 3 additions & 1 deletion vendor/lief/include/LIEF/DEX/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef LIEF_DEX_FIELD_H_
#define LIEF_DEX_FIELD_H_

#include <climits>

#include "LIEF/DEX/enums.hpp"

#include "LIEF/visibility.h"
Expand Down Expand Up @@ -84,7 +86,7 @@ class LIEF_API Field : public Object {
Class* parent_ = nullptr;
Type* type_ = nullptr;
uint32_t access_flags_ = 0;
uint32_t original_index_ = -1u;
uint32_t original_index_ = UINT_MAX;
bool is_static_ = false;
};

Expand Down
3 changes: 2 additions & 1 deletion vendor/lief/include/LIEF/DEX/Method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef LIEF_DEX_METHOD_H_
#define LIEF_DEX_METHOD_H_

#include <climits>

#include "LIEF/visibility.h"
#include "LIEF/Object.hpp"
Expand Down Expand Up @@ -99,7 +100,7 @@ class LIEF_API Method : public Object {
Class* parent_ = nullptr;
Prototype* prototype_ = nullptr;
uint32_t access_flags_ = ACCESS_FLAGS::ACC_UNKNOWN;
uint32_t original_index_ = -1u;
uint32_t original_index_ = UINT_MAX;
bool is_virtual_ = false;

uint64_t code_offset_ = 0;
Expand Down
5 changes: 5 additions & 0 deletions vendor/lief/include/LIEF/ELF/Binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,11 @@ class LIEF_API Binary : public LIEF::Binary {
//! @param filename Path for the written ELF binary
void write(const std::string& filename) override;

//! Reconstruct the binary object and write it in `os` stream
//!
//! @param Output stream for the written ELF binary
void write(std::ostream& os) override;

//! Reconstruct the binary object and return its content as a byte vector
std::vector<uint8_t> raw();

Expand Down
3 changes: 3 additions & 0 deletions vendor/lief/include/LIEF/ELF/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class LIEF_API Builder {
//! Write the built ELF binary in the ``filename`` given in parameter
void write(const std::string& filename) const;

//! Write the built ELF binary in the stream ``os`` given in parameter
void write(std::ostream& os) const;

protected:
template<typename ELF_T>
ok_error_t build();
Expand Down
10 changes: 8 additions & 2 deletions vendor/lief/include/LIEF/MachO/Binary.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* Copyright 2017 - 2022 R. Thomas
* Copyright 2017 - 2022 Quarkslab
*
Expand Down Expand Up @@ -226,6 +227,11 @@ class LIEF_API Binary : public LIEF::Binary {
//! @param filename Path to write the reconstructed binary
void write(const std::string& filename) override;

//! Reconstruct the binary object and write the result in the given `os` stream
//!
//! @param os Output stream to write the reconstructed binary
void write(std::ostream& os) override;

//! Reconstruct the binary object and return its content as bytes
std::vector<uint8_t> raw();

Expand Down Expand Up @@ -642,7 +648,7 @@ class LIEF_API Binary : public LIEF::Binary {

//! Shift the content located right after the Load commands table.
//! This operation can be used to add a new command
void shift(size_t value);
ok_error_t shift(size_t value);

//! Shift the position on the __LINKEDIT data by `width`
ok_error_t shift_linkedit(size_t width);
Expand All @@ -661,7 +667,7 @@ class LIEF_API Binary : public LIEF::Binary {
//! Default constructor
Binary();

void shift_command(size_t width, size_t from_offset);
void shift_command(size_t width, uint64_t from_offset);

//! Insert a Segment command in the cache field (segments_)
//! and keep a consistent state of the indexes.
Expand Down
4 changes: 2 additions & 2 deletions vendor/lief/include/LIEF/MachO/BinaryParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ class LIEF_API BinaryParser : public LIEF::Parser {
friend class MachO::Parser;

//! Maximum number of relocations
constexpr static size_t MAX_RELOCATIONS = std::numeric_limits<uint16_t>::max();
constexpr static size_t MAX_RELOCATIONS = (std::numeric_limits<uint16_t>::max)();

//! Maximum number of MachO LoadCommand
constexpr static size_t MAX_COMMANDS = std::numeric_limits<uint16_t>::max();
constexpr static size_t MAX_COMMANDS = (std::numeric_limits<uint16_t>::max)();

public:
static std::unique_ptr<Binary> parse(const std::string& file);
Expand Down
9 changes: 8 additions & 1 deletion vendor/lief/include/LIEF/MachO/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,26 @@ class LIEF_API Builder {
static ok_error_t write(Binary& binary, std::vector<uint8_t>& out);
static ok_error_t write(Binary& binary, std::vector<uint8_t>& out, config_t config);

static ok_error_t write(Binary& binary, std::ostream& out);
static ok_error_t write(Binary& binary, std::ostream& out, config_t config);

static ok_error_t write(FatBinary& fat, const std::string& filename);
static ok_error_t write(FatBinary& fat, const std::string& filename, config_t config);

static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out);
static ok_error_t write(FatBinary& fat, std::vector<uint8_t>& out, config_t config);

static ok_error_t write(FatBinary& fat, std::ostream& out);
static ok_error_t write(FatBinary& fat, std::ostream& out, config_t config);

~Builder();
private:
ok_error_t build();

const std::vector<uint8_t>& get_build();
ok_error_t write(const std::string& filename) const;

ok_error_t write(std::ostream& os) const;

Builder(Binary& binary, config_t config);
Builder(std::vector<Binary*> binaries, config_t config);

Expand Down
2 changes: 2 additions & 0 deletions vendor/lief/include/LIEF/MachO/FatBinary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class LIEF_API FatBinary {
it_binaries end();
it_const_binaries end() const;

void release_all_binaries();

//! Get a pointer to the last MachO::Binary object presents in this Fat Binary.
//! It returns a nullptr if no binary are present.
std::unique_ptr<Binary> pop_back();
Expand Down
Loading