From: "Omar Polo" Subject: _asr_use_resolver can fail To: tech@openbsd.org Date: Thu, 12 Nov 2026 14:51:48 +0100 diff below fixes the two callers of _asr_use_resolver() that could deref NULL if it fails. I cannot think of valid situations where this can really happen, except for contrived examples where an application first allocate near the limits and only then try to do DNS. Anyway, it's a gratious crash in libc that's easily avoidable. I haven't touched the callers of res_init() because all of them will re-enter asr/ after calling it which are handling ENOMEM situation and return the proper error (so the various gethostbyname, getnetbyaddr, etc etc...) should i also update the manpage documenting that res_init() can return -1 on ENOMEM? thoughts/okays? P.S. while here also try to simplify _asr_use_resolver, the -w diff is as follows. if okay, i'll land these as two different commits. : --- lib/libc/asr/asr.c : +++ lib/libc/asr/asr.c : @@ -355,37 +355,35 @@ struct asr_ctx * : _asr_use_resolver(void *arg) : { : struct asr_ctx *ac = arg; : struct asr *asr; : struct asr **priv; : : if (ac) { : asr_ctx_ref(ac); : return ac; : } : - else { : + : DPRINT("using thread-local resolver\n"); : priv = _THREAD_PRIVATE_DT(_asr, _asr, _asr_resolver_done_tp, : &_asr); : if (*priv == NULL) { : DPRINT("setting up thread-local resolver\n"); : *priv = _asr_resolver(); : } : asr = *priv; : - } : - if (asr != NULL) { : + if (asr == NULL) : + return (NULL); : asr_check_reload(asr); : asr_ctx_ref(asr->a_ctx); : return (asr->a_ctx); : } : - return (NULL); : -} diff 3bdfb71a2da7b7ccacbc235c726e6b1a82cdf969 684973398ae5eca4587fa83af4843eda4221d9ad commit - 3bdfb71a2da7b7ccacbc235c726e6b1a82cdf969 commit + 684973398ae5eca4587fa83af4843eda4221d9ad blob - 904d40ce658f8f77729983bd90af88b436cd6468 blob + a426a0a782a7e66ef9489fb17d70deeaaacd9e38 --- lib/libc/asr/asr.c +++ lib/libc/asr/asr.c @@ -355,36 +355,34 @@ struct asr_ctx * _asr_use_resolver(void *arg) { struct asr_ctx *ac = arg; struct asr *asr; struct asr **priv; if (ac) { asr_ctx_ref(ac); return ac; } - else { - DPRINT("using thread-local resolver\n"); - priv = _THREAD_PRIVATE_DT(_asr, _asr, _asr_resolver_done_tp, - &_asr); - if (*priv == NULL) { - DPRINT("setting up thread-local resolver\n"); - *priv = _asr_resolver(); - } - asr = *priv; + + DPRINT("using thread-local resolver\n"); + priv = _THREAD_PRIVATE_DT(_asr, _asr, _asr_resolver_done_tp, + &_asr); + if (*priv == NULL) { + DPRINT("setting up thread-local resolver\n"); + *priv = _asr_resolver(); } - if (asr != NULL) { - asr_check_reload(asr); - asr_ctx_ref(asr->a_ctx); - return (asr->a_ctx); - } - return (NULL); + asr = *priv; + if (asr == NULL) + return (NULL); + asr_check_reload(asr); + asr_ctx_ref(asr->a_ctx); + return (asr->a_ctx); } static void asr_ctx_ref(struct asr_ctx *ac) { DPRINT("asr: asr_ctx_ref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount); ac->ac_refcount += 1; } /* blob - 04243c47bef132e21171db2f15e33d590ec1457f blob + 93a36ab35bc91594444933f6b583eb9a0a64ab0e --- lib/libc/asr/res_init.c +++ lib/libc/asr/res_init.c @@ -35,20 +35,22 @@ struct __res_state_ext _res_ext; int h_errno; int res_init(void) { static void *resinit_mutex; struct asr_ctx *ac; int i; ac = _asr_use_resolver(NULL); + if (ac == NULL) + return (-1); /* * The first thread to call res_init() will setup the global _res * structure from the async context, not overriding fields set early * by the user. */ _MUTEX_LOCK(&resinit_mutex); if (!(_res.options & RES_INIT)) { if (_res.retry == 0) _res.retry = ac->ac_nsretries; blob - 3e02dbba908d4c7193166cdbd5b9a353322b3ae7 blob + a1f372edf09801e725f086eae0350da3d4c64d07 --- lib/libc/asr/res_mkquery.c +++ lib/libc/asr/res_mkquery.c @@ -48,20 +48,22 @@ res_mkquery(int op, const char *dname, int class, int if (strlcpy(fqdn, dname, sizeof(fqdn)) >= sizeof(fqdn) || strlcat(fqdn, ".", sizeof(fqdn)) >= sizeof(fqdn)) return (-1); dname = fqdn; } if (_asr_dname_from_fqdn(dname, dn, sizeof(dn)) == -1) return (-1); ac = _asr_use_resolver(NULL); + if (ac == NULL) + return (-1); memset(&h, 0, sizeof h); h.id = res_randomid(); if (ac->ac_options & RES_RECURSE) h.flags |= RD_MASK; if (ac->ac_options & RES_USE_CD) h.flags |= CD_MASK; if (ac->ac_options & RES_TRUSTAD) h.flags |= AD_MASK; h.qdcount = 1;