Skip to content

Commit 82affb9

Browse files
committed
Fix out-of-bound memory access
Related to #1426
1 parent 4f782fb commit 82affb9

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/input-files.cc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,27 +1258,17 @@ void SharedFile<E>::parse(Context<E> &ctx) {
12581258

12591259
template <typename E>
12601260
std::vector<std::string_view> SharedFile<E>::get_dt_needed(Context<E> &ctx) {
1261-
// Get the contents of the dynamic segment
1262-
std::span<ElfDyn<E>> dynamic;
1263-
for (ElfPhdr<E> &phdr : this->get_phdrs())
1264-
if (phdr.p_type == PT_DYNAMIC)
1265-
dynamic = {(ElfDyn<E> *)(this->mf->data + phdr.p_offset),
1266-
(size_t)phdr.p_memsz / sizeof(ElfDyn<E>)};
1267-
1268-
// Find a string table
1269-
char *strtab = nullptr;
1270-
for (ElfDyn<E> &dyn : dynamic)
1271-
if (dyn.d_tag == DT_STRTAB)
1272-
strtab = (char *)this->mf->data + dyn.d_val;
1273-
1274-
if (!strtab)
1261+
ElfShdr<E> *sec = this->find_section(SHT_DYNAMIC);
1262+
if (!sec)
12751263
return {};
12761264

1277-
// Find all DT_NEEDED entries
1265+
std::span<ElfDyn<E>> dynamic = this->template get_data<ElfDyn<E>>(ctx, *sec);
1266+
std::string_view strtab = this->get_string(ctx, sec->sh_link);
1267+
12781268
std::vector<std::string_view> vec;
12791269
for (ElfDyn<E> &dyn : dynamic)
12801270
if (dyn.d_tag == DT_NEEDED)
1281-
vec.push_back(strtab + dyn.d_val);
1271+
vec.push_back(strtab.data() + dyn.d_val);
12821272
return vec;
12831273
}
12841274

src/passes.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ void check_shlib_undefined(Context<E> &ctx) {
10861086

10871087
// Obtain a list of known shared library names.
10881088
std::unordered_set<std::string_view> sonames;
1089-
for (SharedFile<E> *file : ctx.dsos)
1089+
for (std::unique_ptr<SharedFile<E>> &file : ctx.dso_pool)
10901090
sonames.insert(file->soname);
10911091

10921092
tbb::parallel_for_each(ctx.dsos, [&](SharedFile<E> *file) {

test/no-allow-shlib-undefined2.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
. $(dirname $0)/common.inc
3+
4+
cat <<EOF | $CC -B. -shared -fPIC -o $t/libfoo.so -xc -
5+
void foo() {}
6+
EOF
7+
8+
cat <<EOF | $CC -B. -shared -fPIC -o $t/libbar.so -xc - -L$t -lfoo
9+
void foo();
10+
void bar() { foo(); }
11+
EOF
12+
13+
cat <<EOF | $CC -c -o $t/a.o -c -xc -
14+
int bar();
15+
int main() { bar(); }
16+
EOF
17+
18+
$CC -B. -o $t/exe1 $t/a.o -Wl,--no-allow-shlib-undefined -L$t -lfoo -lbar
19+
$CC -B. -o $t/exe2 $t/a.o -Wl,--no-allow-shlib-undefined -L$t -lbar
20+
21+
mv $t/libfoo.so $t/libfoo.so.bak
22+
echo | $CC -B. -shared -fPIC -o $t/libfoo.so -xc -
23+
24+
not $CC -B. -o $t/exe3 $t/a.o -Wl,--no-allow-shlib-undefined -L$t -lfoo -lbar

0 commit comments

Comments
 (0)