Skip to content

Commit 811a380

Browse files
mclapinskiavagin
authored andcommitted
files-reg: don't change the file pos in get_build_id
At this point the correct position is already restored, so reading from the fd results in the position being moved forward by 5 bytes. Fixes: 9191f87 ("criu/files-reg.c: add build-id validation functionality") Signed-off-by: Michal Clapinski <[email protected]>
1 parent ab73a84 commit 811a380

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

criu/files-reg.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,39 +1650,36 @@ static int get_build_id_64(Elf64_Ehdr *file_header, unsigned char **build_id, co
16501650
*/
16511651
static int get_build_id(const int fd, const struct stat *fd_status, unsigned char **build_id)
16521652
{
1653-
char buf[SELFMAG + 1];
1654-
void *start_addr;
1653+
char *start_addr;
16551654
size_t mapped_size;
16561655
int ret = -1;
16571656

1658-
if (read(fd, buf, SELFMAG + 1) != SELFMAG + 1)
1659-
return -1;
1660-
1661-
/*
1662-
* The first 4 bytes contain a magic number identifying the file as an
1663-
* ELF file. They should contain the characters ‘\x7f’, ‘E’, ‘L’, and
1664-
* ‘F’, respectively. These characters are together defined as ELFMAG.
1665-
*/
1666-
if (strncmp(buf, ELFMAG, SELFMAG))
1667-
return -1;
1668-
16691657
/*
16701658
* If the build-id exists, then it will most likely be present in the
16711659
* beginning of the file. Therefore at most only the first 1 MB of the
16721660
* file is mapped.
16731661
*/
16741662
mapped_size = min_t(size_t, fd_status->st_size, BUILD_ID_MAP_SIZE);
16751663
start_addr = mmap(0, mapped_size, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0);
1676-
if (start_addr == MAP_FAILED) {
1664+
if ((void*)start_addr == MAP_FAILED) {
16771665
pr_warn("Couldn't mmap file with fd %d\n", fd);
16781666
return -1;
16791667
}
16801668

1681-
if (buf[EI_CLASS] == ELFCLASS32)
1682-
ret = get_build_id_32(start_addr, build_id, fd, mapped_size);
1683-
if (buf[EI_CLASS] == ELFCLASS64)
1684-
ret = get_build_id_64(start_addr, build_id, fd, mapped_size);
1669+
/*
1670+
* The first 4 bytes contain a magic number identifying the file as an
1671+
* ELF file. They should contain the characters ‘\x7f’, ‘E’, ‘L’, and
1672+
* ‘F’, respectively. These characters are together defined as ELFMAG.
1673+
*/
1674+
if (memcmp(start_addr, ELFMAG, SELFMAG))
1675+
goto out;
16851676

1677+
if (start_addr[EI_CLASS] == ELFCLASS32)
1678+
ret = get_build_id_32((Elf32_Ehdr *)start_addr, build_id, fd, mapped_size);
1679+
if (start_addr[EI_CLASS] == ELFCLASS64)
1680+
ret = get_build_id_64((Elf64_Ehdr *)start_addr, build_id, fd, mapped_size);
1681+
1682+
out:
16861683
munmap(start_addr, mapped_size);
16871684
return ret;
16881685
}

0 commit comments

Comments
 (0)