Handle empty relro segment or incorrectly sized file.
If the file has no relro segment, the generated relro file will have length 0, which caused mmap to fail. If the relro file has nonzero size, but is too short (e.g. because it's for the wrong version of the library), the linker would segfault while comparing the data. Fix both these issues: don't try to map a zero length file, and don't try to compare data that would be beyond the end of the file. Improve test to explicitly generate two versions of the library: one with -z relro, and one with -z norelro, so we can test both cases; also explicitly test the case where the relro file has length 0. Bug: 14299541 Change-Id: Id8b95585edda90e8bb5de452a35b70ed2d224934
This commit is contained in:
@@ -591,9 +591,12 @@ int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, El
|
||||
return -1;
|
||||
}
|
||||
off_t file_size = file_stat.st_size;
|
||||
void* temp_mapping = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (temp_mapping == MAP_FAILED) {
|
||||
return -1;
|
||||
void* temp_mapping = NULL;
|
||||
if (file_size > 0) {
|
||||
temp_mapping = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (temp_mapping == MAP_FAILED) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
size_t file_offset = 0;
|
||||
|
||||
@@ -614,6 +617,13 @@ int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count, El
|
||||
size_t match_offset = 0;
|
||||
size_t size = seg_page_end - seg_page_start;
|
||||
|
||||
if (file_size - file_offset < size) {
|
||||
// File is too short to compare to this segment. The contents are likely
|
||||
// different as well (it's probably for a different library version) so
|
||||
// just don't bother checking.
|
||||
break;
|
||||
}
|
||||
|
||||
while (match_offset < size) {
|
||||
// Skip over dissimilar pages.
|
||||
while (match_offset < size &&
|
||||
|
Reference in New Issue
Block a user