[ELF] Fix out-of-bounds write in memset(&Out::first, ...)

Fix r285764: there is no guarantee that Out::first is placed before other
static data members of `struct Out`. After `bufferStart` was introduced, this
out-of-bounds write is destined in many compilers. It is likely benign, though.

And move `Out::elfHeader->size` assignment beside `Out::elfHeader->sectionIndex`
This commit is contained in:
Fangrui Song 2021-11-28 14:47:57 -08:00
parent cecc6893a0
commit d060cc1f98
4 changed files with 5 additions and 4 deletions

View File

@ -2281,7 +2281,6 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
// Create elfHeader early. We need a dummy section in
// addReservedSymbols to mark the created symbols as not absolute.
Out::elfHeader = make<OutputSection>("", 0, SHF_ALLOC);
Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);

View File

@ -33,7 +33,6 @@ using namespace lld;
using namespace lld::elf;
uint8_t *Out::bufferStart;
uint8_t Out::first;
PhdrEntry *Out::tlsPhdr;
OutputSection *Out::elfHeader;
OutputSection *Out::programHeaders;

View File

@ -128,7 +128,6 @@ std::vector<InputSection *> getInputSections(const OutputSection *os);
// until Writer is initialized.
struct Out {
static uint8_t *bufferStart;
static uint8_t first;
static PhdrEntry *tlsPhdr;
static OutputSection *elfHeader;
static OutputSection *programHeaders;

View File

@ -281,7 +281,10 @@ static OutputSection *findSection(StringRef name, unsigned partition = 1) {
template <class ELFT> void elf::createSyntheticSections() {
// Initialize all pointers with NULL. This is needed because
// you can call lld::elf::main more than once as a library.
memset(&Out::first, 0, sizeof(Out));
Out::tlsPhdr = nullptr;
Out::preinitArray = nullptr;
Out::initArray = nullptr;
Out::finiArray = nullptr;
// Add the .interp section first because it is not a SyntheticSection.
// The removeUnusedSyntheticSections() function relies on the
@ -2054,6 +2057,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// to 1 to make __ehdr_start defined. The section number is not
// particularly relevant.
Out::elfHeader->sectionIndex = 1;
Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
for (size_t i = 0, e = outputSections.size(); i != e; ++i) {
OutputSection *sec = outputSections[i];