kern: mountroot: avoid fd leak in .md parsing

parse_dir_md() opens /dev/mdctl but only closes the resulting fd on
success, not upon failure of the ioctl or when we exceed the md unit
max.

Reviewed by:	kib (slightly previous version)
Sponsored by:	NetApp, Inc.
Sponsored by:	Klara, Inc.
X-NetApp-PR:	#62
Differential Revision:	https://reviews.freebsd.org/D31229
This commit is contained in:
Kyle Evans 2021-07-20 05:23:11 -05:00
parent fa46a46a82
commit 23ecfa9d5b

View file

@ -580,6 +580,7 @@ parse_dir_md(char **conf)
int error, fd, len;
td = curthread;
fd = -1;
error = parse_token(conf, &tok);
if (error)
@ -635,9 +636,9 @@ parse_dir_md(char **conf)
root_mount_mddev = mdio->md_unit;
printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
error = kern_close(td, fd);
out:
if (fd >= 0)
(void)kern_close(td, fd);
free(mdio, M_TEMP);
return (error);
}