mips/vdso: Fix resource leaks in genvdso.c

[ Upstream commit a859647b4e6bfeb192284d27d24b6a0c914cae1d ]

Close "fd" before the return of map_vdso() and close "out_file"
in main().

Signed-off-by: Peng Fan <fanpeng@loongson.cn>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Peng Fan 2020-07-14 20:30:18 +08:00 committed by Greg Kroah-Hartman
parent 644e4f2425
commit a48f3e8654

View File

@ -126,6 +126,7 @@ static void *map_vdso(const char *path, size_t *_size)
if (fstat(fd, &stat) != 0) { if (fstat(fd, &stat) != 0) {
fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name, fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name,
path, strerror(errno)); path, strerror(errno));
close(fd);
return NULL; return NULL;
} }
@ -134,6 +135,7 @@ static void *map_vdso(const char *path, size_t *_size)
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name, fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name,
path, strerror(errno)); path, strerror(errno));
close(fd);
return NULL; return NULL;
} }
@ -143,6 +145,7 @@ static void *map_vdso(const char *path, size_t *_size)
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) { if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name, fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name,
path); path);
close(fd);
return NULL; return NULL;
} }
@ -154,6 +157,7 @@ static void *map_vdso(const char *path, size_t *_size)
default: default:
fprintf(stderr, "%s: '%s' has invalid ELF class\n", fprintf(stderr, "%s: '%s' has invalid ELF class\n",
program_name, path); program_name, path);
close(fd);
return NULL; return NULL;
} }
@ -165,6 +169,7 @@ static void *map_vdso(const char *path, size_t *_size)
default: default:
fprintf(stderr, "%s: '%s' has invalid ELF data order\n", fprintf(stderr, "%s: '%s' has invalid ELF data order\n",
program_name, path); program_name, path);
close(fd);
return NULL; return NULL;
} }
@ -172,15 +177,18 @@ static void *map_vdso(const char *path, size_t *_size)
fprintf(stderr, fprintf(stderr,
"%s: '%s' has invalid ELF machine (expected EM_MIPS)\n", "%s: '%s' has invalid ELF machine (expected EM_MIPS)\n",
program_name, path); program_name, path);
close(fd);
return NULL; return NULL;
} else if (swap_uint16(ehdr->e_type) != ET_DYN) { } else if (swap_uint16(ehdr->e_type) != ET_DYN) {
fprintf(stderr, fprintf(stderr,
"%s: '%s' has invalid ELF type (expected ET_DYN)\n", "%s: '%s' has invalid ELF type (expected ET_DYN)\n",
program_name, path); program_name, path);
close(fd);
return NULL; return NULL;
} }
*_size = stat.st_size; *_size = stat.st_size;
close(fd);
return addr; return addr;
} }
@ -284,10 +292,12 @@ int main(int argc, char **argv)
/* Calculate and write symbol offsets to <output file> */ /* Calculate and write symbol offsets to <output file> */
if (!get_symbols(dbg_vdso_path, dbg_vdso)) { if (!get_symbols(dbg_vdso_path, dbg_vdso)) {
unlink(out_path); unlink(out_path);
fclose(out_file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
fprintf(out_file, "};\n"); fprintf(out_file, "};\n");
fclose(out_file);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }