From patchwork Mon Dec 11 12:50:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ludwig Rydberg X-Patchwork-Id: 81900 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 8B669385843F for ; Mon, 11 Dec 2023 12:52:19 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out3.simply.com (smtp-out3.simply.com [94.231.106.210]) by sourceware.org (Postfix) with ESMTPS id 4B16D3858C53 for ; Mon, 11 Dec 2023 12:52:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4B16D3858C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gaisler.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gaisler.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4B16D3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=94.231.106.210 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702299127; cv=none; b=LEKtnpiuOp3uz2OurBgirLrC0E6eISC6wZgMPFLvgofk/CXxcddXb8A2a4mO56cCzj0lsgG+Gebx2XsOsC5ahbAzy0uwTvas+7fHJ6M1KxDdmpL8AHvz8PosnElQRWZ81T8zSQhlpv2lrr8gAtcBtfCOBgtOdYAXSbTFn1WlSvs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702299127; c=relaxed/simple; bh=3ob+6IZvSVB13uKq4hYh9Mq6e3Bupck5Ab3ZW0UtUxg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Hg0NrWwJbk8jywZ8xmKuJ76IXNjd/RcwIrEXFSH4c761J1nanBmuauC+/mZsoOznU+XmuB1N4T3viwOMGElRlYRv9sgehR0ISD5uDLIHNqDoLk7FtrI5dq6XVAsDMJu54qBMZDDAdJGv7MXQ0VMfDGzUudsfMN9Gsc8ILr++8Yc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (localhost [127.0.0.1]) by smtp.simply.com (Simply.com) with ESMTP id 4SphTZ0BB7z685P; Mon, 11 Dec 2023 13:52:02 +0100 (CET) Received: from ludwig.got.gaisler.com (h-98-128-223-123.NA.cust.bahnhof.se [98.128.223.123]) by smtp.simply.com (Simply.com) with ESMTPA id 4SphTY5m9bz6821; Mon, 11 Dec 2023 13:52:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaisler.com; s=unoeuro; t=1702299121; bh=xXyxOn7WldRAs+CziVo2kWNmTG+1/ZB4upuTWWS+7PA=; h=From:To:Cc:Subject:Date; b=RbWNdsSdSeqxBl0nA75yO+nPhSwrU8xMu4zgUbbt128kFV58GF1e8uTaL105ou5++ u2zsrtw5TD0+/d4iHiKdmoc+mLDseG6w5bIY6TPQigFqeakhoAcIt7BaIZ2Uq0TuNM qzf+lvOYiCQoGNeiW2d5GMeAG2PyaYjkMBzaQieM= From: Ludwig Rydberg To: libc-alpha@sourceware.org Cc: software@gaisler.com, fweimer@redhat.com, Ludwig Rydberg , Andreas Larsson Subject: [PATCH] resolv: Fix a few unaligned accesses to fields in HEADER Date: Mon, 11 Dec 2023 13:50:41 +0100 Message-Id: <20231211125041.10418-1-ludwig.rydberg@gaisler.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org After refactoring the alloca usage in 40c0add7d4 ("resolve: Remove __res_context_query alloca usage") a few unaligned accesses to HEADER fields surfaced. These unaligned accesses led to problems when running the resolv test suite on sparc32-linux (leon) as many tests failed due to SIGBUS crashes. The issue(s) occured during T_QUERY_A_AND_AAAA queries as the second query now can start on an unaligned address (previously it was explicitly aligned). With this patch the unaligned accesses are now fixed by using the UHEADER instead to ensure the fields are accessed with byte loads/stores. The patch has been verfied by running the resolv test suite on sparc32 and x86_64. Signed-off-by: Ludwig Rydberg Signed-off-by: Andreas Larsson Reviewed-by: Florian Weimer --- resolv/res_mkquery.c | 6 +++--- resolv/res_queriesmatch.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) base-commit: b3bee76c5f59498b9c189608f0a3132e2013fa1a diff --git a/resolv/res_mkquery.c b/resolv/res_mkquery.c index e9316b3c3772..a46a92432b50 100644 --- a/resolv/res_mkquery.c +++ b/resolv/res_mkquery.c @@ -100,7 +100,7 @@ __res_context_mkquery (struct resolv_context *ctx, int op, const char *dname, int class, int type, const unsigned char *data, unsigned char *buf, int buflen) { - HEADER *hp; + UHEADER *hp; unsigned char *cp; int n; unsigned char *dnptrs[20], **dpp, **lastdnptr; @@ -112,7 +112,7 @@ __res_context_mkquery (struct resolv_context *ctx, int op, const char *dname, if ((buf == NULL) || (buflen < HFIXEDSZ)) return -1; memset (buf, 0, HFIXEDSZ); - hp = (HEADER *) buf; + hp = (UHEADER *) buf; /* We randomize the IDs every time. The old code just incremented by one after the initial randomization which still predictable if the application does multiple requests. */ @@ -250,7 +250,7 @@ __res_nopt (struct resolv_context *ctx, int n0, unsigned char *buf, int buflen, int anslen) { uint16_t flags = 0; - HEADER *hp = (HEADER *) buf; + UHEADER *hp = (UHEADER *) buf; unsigned char *cp = buf + n0; unsigned char *ep = buf + buflen; diff --git a/resolv/res_queriesmatch.c b/resolv/res_queriesmatch.c index ba1c1d0c0cf6..37db01a1b048 100644 --- a/resolv/res_queriesmatch.c +++ b/resolv/res_queriesmatch.c @@ -95,14 +95,14 @@ __libc_res_queriesmatch (const unsigned char *buf1, const unsigned char *eom1, /* Only header section present in replies to dynamic update packets. */ - if ((((HEADER *) buf1)->opcode == ns_o_update) && - (((HEADER *) buf2)->opcode == ns_o_update)) + if ((((UHEADER *) buf1)->opcode == ns_o_update) && + (((UHEADER *) buf2)->opcode == ns_o_update)) return 1; /* Note that we initially do not convert QDCOUNT to the host byte order. We can compare it with the second buffer's QDCOUNT value without doing this. */ - int qdcount = ((HEADER *) buf1)->qdcount; + int qdcount = ((UHEADER *) buf1)->qdcount; if (qdcount != ((UHEADER *) buf2)->qdcount) return 0;