res_find: Fix fallback logic

The fallback logic was broken if hints were found in multiple environments.
If we found a hint in either the loader environment or the static
environment, fallback would be incremented excessively when we returned to
the environment-selection bits. These checks should have also been guarded
by the fbacklvl checks. As a result, fbacklvl could quickly get to a point
where we skip either the static environment and/or the static hints
depending on which environments contained valid hints.

The impact of this bug is minimal, mostly affecting mips boards that use
static hints and may have hints in either the loader environment or the
static environment.

There may be better ways to express the searchable environments and
describing their characteristics (immutable, already searched, etc.) but
this may be revisited after 12 branches.

Reported by:	Dan Nelson <dnelson_1901@yahoo.com>
Triaged by:	Dan Nelson <dnelson_1901@yahoo.com>
MFC after:	3 days
This commit is contained in:
kevans 2018-08-18 19:45:56 +00:00
parent 453e23bd43
commit ad8cc64e2b

View File

@ -174,30 +174,37 @@ fallback:
if (dyn_used || fbacklvl >= FBACK_STATIC)
return (ENOENT);
if (fbacklvl <= FBACK_MDENV &&
_res_checkenv(md_envp)) {
hintp = md_envp;
goto found;
switch (fbacklvl) {
case FBACK_MDENV:
fbacklvl++;
if (_res_checkenv(md_envp)) {
hintp = md_envp;
break;
}
/* FALLTHROUGH */
case FBACK_STENV:
fbacklvl++;
if (!stenv_skip && _res_checkenv(kern_envp)) {
hintp = kern_envp;
break;
} else
stenv_skip = true;
/* FALLTHROUGH */
case FBACK_STATIC:
fbacklvl++;
/* We'll fallback to static_hints if needed/can */
if (!sthints_skip &&
_res_checkenv(static_hints))
hintp = static_hints;
else
sthints_skip = true;
break;
default:
return (ENOENT);
}
fbacklvl++;
if (!stenv_skip && fbacklvl <= FBACK_STENV &&
_res_checkenv(kern_envp)) {
hintp = kern_envp;
goto found;
} else
stenv_skip = true;
fbacklvl++;
/* We'll fallback to static_hints if needed/can */
if (!sthints_skip && fbacklvl <= FBACK_STATIC &&
_res_checkenv(static_hints))
hintp = static_hints;
else
sthints_skip = true;
found:
fbacklvl++;
}
if (hintp == NULL)