Skip to content

Commit

Permalink
Merge #464
Browse files Browse the repository at this point in the history
464: Modernizations and strictness improvements r=Mic92 a=cgzones



Co-authored-by: Christian Göttsche <[email protected]>
  • Loading branch information
bors[bot] and cgzones committed Feb 21, 2023
2 parents 8d3188d + ff7a5be commit e37f892
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AM_CXXFLAGS = -Wall -std=c++17 -D_FILE_OFFSET_BITS=64
AM_CXXFLAGS = -Wall -Wextra -Wcast-qual -std=c++17 -D_FILE_OFFSET_BITS=64

if WITH_ASAN
AM_CXXFLAGS += -fsanitize=address -fsanitize-address-use-after-scope
Expand Down
139 changes: 94 additions & 45 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <optional>
Expand Down Expand Up @@ -69,14 +70,18 @@ static int forcedPageSize = -1;
#define EM_LOONGARCH 258
#endif


static std::vector<std::string> splitColonDelimitedString(const char * s)
[[nodiscard]] static std::vector<std::string> splitColonDelimitedString(std::string_view s)
{
std::string item;
std::vector<std::string> parts;
std::stringstream ss(s);
while (std::getline(ss, item, ':'))
parts.push_back(item);

size_t pos;
while ((pos = s.find(':')) != std::string_view::npos) {
parts.emplace_back(s.substr(0, pos));
s = s.substr(pos + 1);
}

if (!s.empty())
parts.emplace_back(s);

return parts;
}
Expand Down Expand Up @@ -104,7 +109,7 @@ static std::string downcase(std::string s)
why... */
template<ElfFileParams>
template<class I>
I ElfFile<ElfFileParamNames>::rdi(I i) const
constexpr I ElfFile<ElfFileParamNames>::rdi(I i) const noexcept
{
I r = 0;
if (littleEndian) {
Expand All @@ -131,7 +136,7 @@ static void debug(const char * format, ...)
}


void fmt2(std::ostringstream & out)
static void fmt2([[maybe_unused]] std::ostringstream & out)
{
}

Expand Down Expand Up @@ -162,7 +167,7 @@ struct SysError : std::runtime_error
{ }
};

__attribute__((noreturn)) static void error(const std::string & msg)
[[noreturn]] static void error(const std::string & msg)
{
if (errno)
throw SysError(msg);
Expand Down Expand Up @@ -191,11 +196,11 @@ static FileContents readFile(const std::string & fileName,
while ((portion = read(fd, contents->data() + bytesRead, size - bytesRead)) > 0)
bytesRead += portion;

close(fd);

if (bytesRead != size)
throw SysError(fmt("reading '", fileName, "'"));

close(fd);

return contents;
}

Expand All @@ -207,10 +212,10 @@ struct ElfType
};


ElfType getElfType(const FileContents & fileContents)
[[nodiscard]] static ElfType getElfType(const FileContents & fileContents)
{
/* Check the ELF header for basic validity. */
if (fileContents->size() < static_cast<off_t>(sizeof(Elf32_Ehdr)))
if (fileContents->size() < sizeof(Elf32_Ehdr))
error("missing ELF header");

auto contents = fileContents->data();
Expand All @@ -231,14 +236,32 @@ ElfType getElfType(const FileContents & fileContents)
}


static void checkPointer(const FileContents & contents, void * p, unsigned int size)
static void checkPointer(const FileContents & contents, const void * p, size_t size)
{
auto q = static_cast<unsigned char *>(p);
if (!(q >= contents->data() && q + size <= contents->data() + contents->size()))
if (p < contents->data() || size > contents->size() || p > contents->data() + contents->size() - size)
error("data region extends past file end");
}


static void checkOffset(const FileContents & contents, size_t offset, size_t size)
{
size_t end;

if (offset > contents->size()
|| size > contents->size()
|| __builtin_add_overflow(offset, size, &end)
|| end > contents->size())
error("data offset extends past file end");
}


static std::string extractString(const FileContents & contents, size_t offset, size_t size)
{
checkOffset(contents, offset, size);
return { reinterpret_cast<const char *>(contents->data()) + offset, size };
}


template<ElfFileParams>
ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents)
: fileContents(fContents)
Expand All @@ -255,14 +278,34 @@ ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents)
if (rdi(hdr()->e_type) != ET_EXEC && rdi(hdr()->e_type) != ET_DYN)
error("wrong ELF type");

if (rdi(hdr()->e_phoff) + rdi(hdr()->e_phnum) * rdi(hdr()->e_phentsize) > fileContents->size())
error("program header table out of bounds");
{
auto ph_offset = rdi(hdr()->e_phoff);
auto ph_num = rdi(hdr()->e_phnum);
auto ph_entsize = rdi(hdr()->e_phentsize);
size_t ph_size, ph_end;

if (__builtin_mul_overflow(ph_num, ph_entsize, &ph_size)
|| __builtin_add_overflow(ph_offset, ph_size, &ph_end)
|| ph_end > fileContents->size()) {
error("program header table out of bounds");
}
}

if (rdi(hdr()->e_shnum) == 0)
error("no section headers. The input file is probably a statically linked, self-decompressing binary");

if (rdi(hdr()->e_shoff) + rdi(hdr()->e_shnum) * rdi(hdr()->e_shentsize) > fileContents->size())
error("section header table out of bounds");
{
auto sh_offset = rdi(hdr()->e_shoff);
auto sh_num = rdi(hdr()->e_shnum);
auto sh_entsize = rdi(hdr()->e_shentsize);
size_t sh_size, sh_end;

if (__builtin_mul_overflow(sh_num, sh_entsize, &sh_size)
|| __builtin_add_overflow(sh_offset, sh_size, &sh_end)
|| sh_end > fileContents->size()) {
error("section header table out of bounds");
}
}

if (rdi(hdr()->e_phentsize) != sizeof(Elf_Phdr))
error("program headers have wrong size");
Expand All @@ -286,12 +329,15 @@ ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents)
/* Get the section header string table section (".shstrtab"). Its
index in the section header table is given by e_shstrndx field
of the ELF header. */
unsigned int shstrtabIndex = rdi(hdr()->e_shstrndx);
auto shstrtabIndex = rdi(hdr()->e_shstrndx);
if (shstrtabIndex >= shdrs.size())
error("string table index out of bounds");

unsigned int shstrtabSize = rdi(shdrs[shstrtabIndex].sh_size);
char * shstrtab = (char * ) fileContents->data() + rdi(shdrs[shstrtabIndex].sh_offset);
auto shstrtabSize = rdi(shdrs[shstrtabIndex].sh_size);
size_t shstrtabptr;
if (__builtin_add_overflow(reinterpret_cast<size_t>(fileContents->data()), rdi(shdrs[shstrtabIndex].sh_offset), &shstrtabptr))
error("string table overflow");
const char *shstrtab = reinterpret_cast<const char *>(shstrtabptr);
checkPointer(fileContents, shstrtab, shstrtabSize);

if (shstrtabSize == 0)
Expand All @@ -309,7 +355,7 @@ ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents)


template<ElfFileParams>
unsigned int ElfFile<ElfFileParamNames>::getPageSize() const
unsigned int ElfFile<ElfFileParamNames>::getPageSize() const noexcept
{
if (forcedPageSize > 0)
return forcedPageSize;
Expand Down Expand Up @@ -531,7 +577,7 @@ std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr) co


template<ElfFileParams>
Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sectionName)
const Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sectionName) const
{
auto shdr = tryFindSectionHeader(sectionName);
if (!shdr) {
Expand All @@ -545,7 +591,7 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sec


template<ElfFileParams>
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::tryFindSectionHeader(const SectionName & sectionName)
std::optional<std::reference_wrapper<const Elf_Shdr>> ElfFile<ElfFileParamNames>::tryFindSectionHeader(const SectionName & sectionName) const
{
auto i = getSectionIndex(sectionName);
if (i)
Expand All @@ -555,7 +601,7 @@ std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::tryF


template<ElfFileParams>
unsigned int ElfFile<ElfFileParamNames>::getSectionIndex(const SectionName & sectionName)
unsigned int ElfFile<ElfFileParamNames>::getSectionIndex(const SectionName & sectionName) const
{
for (unsigned int i = 1; i < rdi(hdr()->e_shnum); ++i)
if (getSectionName(shdrs.at(i)) == sectionName) return i;
Expand All @@ -579,7 +625,7 @@ std::string & ElfFile<ElfFileParamNames>::replaceSection(const SectionName & sec
s = std::string(i->second);
} else {
auto shdr = findSectionHeader(sectionName);
s = std::string((char *) fileContents->data() + rdi(shdr.sh_offset), rdi(shdr.sh_size));
s = extractString(fileContents, rdi(shdr.sh_offset), rdi(shdr.sh_size));
}

s.resize(size);
Expand All @@ -598,7 +644,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
clobbering previously written new section contents. */
for (auto & i : replacedSections) {
const std::string & sectionName = i.first;
Elf_Shdr & shdr = findSectionHeader(sectionName);
const Elf_Shdr & shdr = findSectionHeader(sectionName);
if (rdi(shdr.sh_type) != SHT_NOBITS)
memset(fileContents->data() + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size));
}
Expand All @@ -617,7 +663,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
debug("rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n",
sectionName.c_str(), rdi(shdr.sh_offset), rdi(shdr.sh_size), curOff, i->second.size());

memcpy(fileContents->data() + curOff, (unsigned char *) i->second.c_str(),
memcpy(fileContents->data() + curOff, i->second.c_str(),
i->second.size());

/* Update the section header for this section. */
Expand Down Expand Up @@ -1186,10 +1232,13 @@ static void setSubstr(std::string & s, unsigned int pos, const std::string & t)


template<ElfFileParams>
std::string ElfFile<ElfFileParamNames>::getInterpreter()
std::string ElfFile<ElfFileParamNames>::getInterpreter() const
{
auto shdr = findSectionHeader(".interp");
return std::string((char *) fileContents->data() + rdi(shdr.sh_offset), rdi(shdr.sh_size) - 1);
auto size = rdi(shdr.sh_size);
if (size > 0)
size--;
return extractString(fileContents, rdi(shdr.sh_offset), size);
}

template<ElfFileParams>
Expand Down Expand Up @@ -1312,7 +1361,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
std::string & newDynamic = replaceSection(".dynamic", rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn));

unsigned int idx = 0;
for (; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++);
for (; rdi(reinterpret_cast<const Elf_Dyn *>(newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++);
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by one. */
Expand Down Expand Up @@ -1468,7 +1517,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
case rpPrint: {
printf("%s\n", rpath ? rpath : "");
return;
};
}
case rpRemove: {
if (!rpath) {
debug("no RPATH to delete\n");
Expand All @@ -1481,7 +1530,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
if (!rpath) {
debug("no RPATH to shrink\n");
return;
;}
}
newRPath = shrinkRPath(rpath, neededLibs, allowedRpathPrefixes);
break;
}
Expand Down Expand Up @@ -1547,7 +1596,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn));

unsigned int idx = 0;
for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
for ( ; rdi(reinterpret_cast<const Elf_Dyn *>(newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by one. */
Expand Down Expand Up @@ -1749,7 +1798,7 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn) * libs.size());

unsigned int idx = 0;
for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
for ( ; rdi(reinterpret_cast<const Elf_Dyn *>(newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by the number of new entries. */
Expand All @@ -1772,13 +1821,13 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
}

template<ElfFileParams>
void ElfFile<ElfFileParamNames>::printNeededLibs() // const
void ElfFile<ElfFileParamNames>::printNeededLibs() const
{
const auto shdrDynamic = findSectionHeader(".dynamic");
const auto shdrDynStr = findSectionHeader(".dynstr");
const char *strTab = (char *)fileContents->data() + rdi(shdrDynStr.sh_offset);
const char *strTab = (const char *)fileContents->data() + rdi(shdrDynStr.sh_offset);

const Elf_Dyn *dyn = (Elf_Dyn *) (fileContents->data() + rdi(shdrDynamic.sh_offset));
const Elf_Dyn *dyn = (const Elf_Dyn *) (fileContents->data() + rdi(shdrDynamic.sh_offset));

for (; rdi(dyn->d_tag) != DT_NULL; dyn++) {
if (rdi(dyn->d_tag) == DT_NEEDED) {
Expand Down Expand Up @@ -1811,7 +1860,7 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn));

unsigned int idx = 0;
for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
for ( ; rdi(reinterpret_cast<const Elf_Dyn *>(newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by one. */
Expand Down Expand Up @@ -1844,7 +1893,7 @@ void ElfFile<ElfFileParamNames>::addDebugTag()
rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn));

unsigned int idx = 0;
for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
for ( ; rdi(reinterpret_cast<const Elf_Dyn *>(newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by one. */
Expand Down Expand Up @@ -2073,7 +2122,7 @@ static void patchElf()
}
}

std::string resolveArgument(const char *arg) {
[[nodiscard]] static std::string resolveArgument(const char *arg) {
if (strlen(arg) > 0 && arg[0] == '@') {
FileContents cnts = readFile(arg + 1);
return std::string((char *)cnts->data(), cnts->size());
Expand All @@ -2083,7 +2132,7 @@ std::string resolveArgument(const char *arg) {
}


void showHelp(const std::string & progName)
static void showHelp(const std::string & progName)
{
fprintf(stderr, "syntax: %s\n\
[--set-interpreter FILENAME]\n\
Expand Down Expand Up @@ -2118,7 +2167,7 @@ void showHelp(const std::string & progName)
}


int mainWrapped(int argc, char * * argv)
static int mainWrapped(int argc, char * * argv)
{
if (argc <= 1) {
showHelp(argv[0]);
Expand Down
Loading

0 comments on commit e37f892

Please sign in to comment.