Skip to content
Open
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
94 changes: 89 additions & 5 deletions apps/uab/header/src/light_elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

#include "utils.h"

#include <elf.h>

Check warning on line 9 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <elf.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <cstddef>

Check warning on line 11 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstddef> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdint>

Check warning on line 12 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstring> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring>

Check warning on line 13 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <filesystem> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <filesystem>

Check warning on line 14 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <optional> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <optional>

Check warning on line 15 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <vector> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string_view>
#include <vector>

Check warning on line 17 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <fcntl.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

namespace lightElf {
Expand Down Expand Up @@ -160,29 +163,110 @@

[[nodiscard]] std::optional<SectionHeader> getSectionHeader(const std::string &name) const
{
// Everything used below comes from the file being inspected: for a .uab the
// caller is the untrusted bundle itself, so e_shentsize/e_shnum/e_shoff and
// every sh_name must be treated as attacker controlled input.
//
// The section header table is only well formed when each entry is exactly as
// large as SectionHeader. A tampered e_shentsize used to be handed straight
// to pread, so a value larger than sizeof(SectionHeader) wrote past the end of
// the on-stack `shdr` below and a smaller value left it only partially filled.
// Reject anything else instead of reading, which keeps the stack object safe.
if (header.e_shentsize != section_size) {
return std::nullopt;
}

// The table referenced by e_shoff/e_shnum has to fit inside the file, otherwise
// the entries the loop walks do not exist and reads would return short data.
if (!sectionTableInsideFile()) {
return std::nullopt;
}

SectionHeader shdr;
auto offset = header.e_shoff;
const auto *data = rawSectionNames.data();

for (auto index = 0; index < header.e_shnum; ++index) {
if (::pread(fd, &shdr, header.e_shentsize, offset) == -1) {
// Read exactly one SectionHeader, never header.e_shentsize bytes: the two
// are equal at this point, but pinning the read to section_size keeps the

Check warning on line 190 in apps/uab/header/src/light_elf.h

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'fd' shadows outer variable
// stack buffer safe even if the validation above is ever reordered.
const auto bytesRead = ::pread(fd, &shdr, section_size, offset);
if (bytesRead == -1) {
throw std::runtime_error("failed to read section header of" + name + ": "
+ ::strerror(errno));
}
if (bytesRead != static_cast<ssize_t>(section_size)) {
return std::nullopt;
}

auto curName = std::string_view(data + shdr.sh_name);
if (!curName.empty() && curName == name) {
// sh_name is an offset into the section-name string table. A malformed
// entry is skipped rather than matched, so a single corrupt offset cannot
// make the string_view scan past the end of rawSectionNames.
const auto curName = sectionName(shdr);
if (curName && !curName->empty() && *curName == name) {
return shdr;
}

offset += header.e_shentsize;
offset += section_size;
}

return std::nullopt;
}

private:
constexpr static auto section_size = sizeof(SectionHeader);

// Returns the size of the file that fd refers to, or std::nullopt when it cannot be
// determined. Needed to prove the section header table lies inside the file before
// any of it is read.
[[nodiscard]] std::optional<std::uintmax_t> fileSize() const noexcept
{
struct stat st{};
if (::fstat(fd, &st) == -1) {
return std::nullopt;
}

return static_cast<std::uintmax_t>(st.st_size);
}

// Checks e_shoff + e_shnum * sizeof(SectionHeader) <= file size without letting the
// untrusted e_shoff/e_shnum overflow the computation.
[[nodiscard]] bool sectionTableInsideFile() const noexcept
{
const auto size = fileSize();
if (!size) {
return false;
}

const auto tableOffset = static_cast<std::uint64_t>(header.e_shoff);
if (tableOffset > *size) {
return false;
}

const auto tableSize = static_cast<std::uint64_t>(header.e_shnum) * section_size;
return tableSize <= *size - tableOffset;
}

// Resolves sh_name into a bounded view of the section-name string table. The view
// must not be built from a raw pointer into the table: an out-of-range sh_name or a
// table that is not NUL terminated at sh_name would make the length scan read past
// rawSectionNames. Returns std::nullopt for such malformed entries.
[[nodiscard]] std::optional<std::string_view>
sectionName(const SectionHeader &shdr) const noexcept
{
if (shdr.sh_name >= rawSectionNames.size()) {
return std::nullopt;
}

const auto *begin = rawSectionNames.data() + shdr.sh_name;
const auto remaining = rawSectionNames.size() - shdr.sh_name;
const auto *terminator = static_cast<const char *>(std::memchr(begin, '\0', remaining));
if (terminator == nullptr) {
return std::nullopt;
}

return std::string_view{ begin, static_cast<std::size_t>(terminator - begin) };
}

constexpr static auto header_size = sizeof(ElfHeader);

std::vector<char> rawSectionNames;
Expand Down
1 change: 1 addition & 0 deletions libs/linglong/tests/ll-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pfl_add_executable(
src/linglong/package/fallback_version_test.cpp
src/linglong/package/layer_dir_test.cpp
src/linglong/package/layer_packager_test.cpp
src/linglong/package/light_elf_test.cpp
src/linglong/package_manager/action_test.cpp
src/linglong/package_manager/data_monitor_test.cpp
src/linglong/package_manager/task_test.cpp
Expand Down
Loading
Loading