Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb3d5cc6 authored by Jesper Juhl's avatar Jesper Juhl Committed by Rusty Russell
Browse files

modpost: Stop grab_file() from leaking filedescriptors if fstat() fails



In case the open() call succeeds but the subsequent fstat() call
fails, then we'll return without close()'ing the filedescriptor.

Signed-off-by: default avatarJesper Juhl <jj@chaosbits.net>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 61011677
Loading
Loading
Loading
Loading
+6 −3
Original line number Original line Diff line number Diff line
@@ -337,17 +337,20 @@ static void sym_update_crc(const char *name, struct module *mod,
void *grab_file(const char *filename, unsigned long *size)
void *grab_file(const char *filename, unsigned long *size)
{
{
	struct stat st;
	struct stat st;
	void *map;
	void *map = MAP_FAILED;
	int fd;
	int fd;


	fd = open(filename, O_RDONLY);
	fd = open(filename, O_RDONLY);
	if (fd < 0 || fstat(fd, &st) != 0)
	if (fd < 0)
		return NULL;
		return NULL;
	if (fstat(fd, &st))
		goto failed;


	*size = st.st_size;
	*size = st.st_size;
	map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
	map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
	close(fd);


failed:
	close(fd);
	if (map == MAP_FAILED)
	if (map == MAP_FAILED)
		return NULL;
		return NULL;
	return map;
	return map;