From patchwork Thu Jan 13 16:21:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Pavlu X-Patchwork-Id: 49992 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3B9D3951E6B for ; Thu, 13 Jan 2022 16:21:42 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 2646E385782A for ; Thu, 13 Jan 2022 16:21:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2646E385782A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 1C6B61F3A8; Thu, 13 Jan 2022 16:21:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1642090878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=9vCha3+2AozYrHiLaG3LOgOT8yOMk7eVrhFcpCUYTXI=; b=Y55k+Z+/bCoF2wv9AeFvb90kVdubHgIS3u9F0Shq52aPZHAmk7OXFPYxDvjJvPeOx6F/xr gfYw4RnlN96BT3U9lk8nXrPW1ijqimq2pWAZn+V4SyI1/+yGrIJotJbLllc7utOKIBLaPA ja+VcdiYZeJZXBOfwzbaJZYKKPm4mU0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1642090878; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=9vCha3+2AozYrHiLaG3LOgOT8yOMk7eVrhFcpCUYTXI=; b=7uualcmEvklt/nNqbTZR//gvJTgGZYSq6d0s1rdwW0wM2jEpGaDAikpv5l3ijSQuXhhg2C glc6+MofjAozyVCw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0AAF71330C; Thu, 13 Jan 2022 16:21:18 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 1W4MAn5R4GG6LwAAMHmgww (envelope-from ); Thu, 13 Jan 2022 16:21:18 +0000 From: Petr Pavlu To: fweimer@redhat.com, libc-alpha@sourceware.org Subject: [PATCH] resolv: Fix a domain search list check Date: Thu, 13 Jan 2022 17:21:00 +0100 Message-Id: <20220113162100.24013-1-ppavlu@suse.cz> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_LOTSOFHASH, SPF_HELO_NONE, SPF_PASS, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Petr Pavlu Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Petr Pavlu Fix a domain search list check in resolv_conf_matches() which verifies that a resolv_conf extended state matches a given __res_state. The check counts a length of entries referenced by the resp->dnsrch array and tests whether it exceeds the combined storage space for the search list, but wrongly uses a size of resp->dnsrch instead of resp->defdname. Signed-off-by: Petr Pavlu --- Notes: The following is an artificial example which demonstrates the issue: > $ cat test.c > #include > #include > #include > #include > #include > #include > #include > #include > > int main(int argc, char *argv[]) > { > if (argc < 2) { > fprintf(stderr, "Usage: %s hostname\n", argv[0]); > return 1; > } > const char *hostname = argv[1]; > > if (res_init() == -1) { > fprintf(stderr, "res_init() failed\n"); > return 1; > } > > printf("System search domains:\n"); > for (size_t i = 0; i < sizeof(_res.dnsrch) / sizeof(_res.dnsrch[0]); i++) > if (_res.dnsrch[i] != NULL) > printf(" %zu: %s\n", i, _res.dnsrch[i]); > > printf("Custom search domains:\n"); > memset(_res.dnsrch, 0, sizeof(_res.dnsrch)); > memset(_res.defdname, 0, sizeof(_res.defdname)); > _res.dnsrch[0] = _res.defdname; > snprintf(_res.defdname, sizeof(_res.defdname), "%s", > "012345678901234567890123456789012345678901234567890123456789.example.org"); > for (size_t i = 0; i < sizeof(_res.dnsrch) / sizeof(_res.dnsrch[0]); i++) > if (_res.dnsrch[i] != NULL) > printf(" %zu: %s\n", i, _res.dnsrch[i]); > > struct addrinfo hints, *result; > memset(&hints, 0, sizeof(hints)); > hints.ai_family = AF_INET; > hints.ai_socktype = SOCK_STREAM; > int s = getaddrinfo(hostname, NULL, &hints, &result); > if (s != 0) { > fprintf(stderr, "getaddrinfo() failed: %s\n", gai_strerror(s)); > return 1; > } > > printf("Resolved addresses:\n"); > for (struct addrinfo *p = result; p != NULL; p = p->ai_next) { > struct sockaddr_in *h = (struct sockaddr_in *) p->ai_addr; > printf(" %s\n", inet_ntoa(h->sin_addr)); > } > freeaddrinfo(result); > > return 0; > } > $ gcc -Wall -pedantic -lresolv test.c > $ cat /etc/resolv > search 012345678901234567890123456789012345678901234567890123456789.example.org gnu.org > [...] Previous behaviour: > $ ./a.out savannah > System search domains: > 0: 012345678901234567890123456789012345678901234567890123456789.example.org > 1: gnu.org > Custom search domains: > 0: 012345678901234567890123456789012345678901234567890123456789.example.org > Resolved addresses: > 209.51.188.72 Updated version which recognizes that _res was modified: > $ ./a.out savannah > System search domains: > 0: 012345678901234567890123456789012345678901234567890123456789.example.org > 1: gnu.org > Custom search domains: > 0: 012345678901234567890123456789012345678901234567890123456789.example.org > getaddrinfo() failed: Name or service not known resolv/resolv_conf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolv/resolv_conf.c b/resolv/resolv_conf.c index 6dd552bb47..ef872dbc14 100644 --- a/resolv/resolv_conf.c +++ b/resolv/resolv_conf.c @@ -310,7 +310,7 @@ resolv_conf_matches (const struct __res_state *resp, exceeds MAXDNSRCH, or if the combined storage space for the search list exceeds what can be stored in resp->defdname. */ - if (i == MAXDNSRCH || search_list_size > sizeof (resp->dnsrch)) + if (i == MAXDNSRCH || search_list_size > sizeof (resp->defdname)) break; /* Otherwise, a mismatch indicates a match failure. */ return false;