From patchwork Sun Dec 5 14:45:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jarno X-Patchwork-Id: 48512 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 3577B3858010 for ; Sun, 5 Dec 2021 14:47:12 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hall.aurel32.net (hall.aurel32.net [IPv6:2001:bc8:30d7:100::1]) by sourceware.org (Postfix) with ESMTPS id 573283858430 for ; Sun, 5 Dec 2021 14:46:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 573283858430 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=aurel32.net Authentication-Results: sourceware.org; spf=none smtp.mailfrom=aurel32.net DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=aurel32.net ; s=202004.hall; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date: Subject:Cc:To:From:Content-Type:From:Reply-To:Subject:Content-ID: Content-Description:In-Reply-To:References:X-Debbugs-Cc; bh=PA9wIJnggv5NWo3jb+CB13oc4l04h/6KKPj4QcKi+Ro=; b=I0JlMhsUDuB5KtINW/fkMKItA+ vodlpvt/gorbBLKeqFpzHXt35/4jiR0w9/tAhOujs3jRqP4bW2j7lrxrGFB8sA1K6yxwytJkewmXL q7VfFytAFL8+o39/2XCoJRlD118Itm5vS4Z4OzdR+Ndtrsj9HyFcDRndNSZGYwkwzmMn+g9zQGKsz EvPNSRwLuqAholzz6pl4z675kVw+vwdqqLGkbBjmwRnP0oBkdmXBHxHkAR4AtkeKebs3G+GtECyfe FkMD7KogDPAFLtjK4wLD3/rCeVekedAefjs9+4YwWm2mca7+k06cWEU8JwJoARYYH3YwNKiBthKm9 Y86VJI9g==; Received: from [2a01:e34:ec5d:a741:8a4c:7c4e:dc4c:1787] (helo=ohm.rr44.fr) by hall.aurel32.net with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mtsm7-0000n6-E7; Sun, 05 Dec 2021 15:45:59 +0100 Received: from aurel32 by ohm.rr44.fr with local (Exim 4.94.2) (envelope-from ) id 1mtsm6-00EugH-Pr; Sun, 05 Dec 2021 15:45:58 +0100 From: Aurelien Jarno To: libc-alpha@sourceware.org Subject: [PATCH] localedef: check magic value on archive load [BZ #28650] Date: Sun, 5 Dec 2021 15:45:53 +0100 Message-Id: <20211205144553.3554426-1-aurelien@aurel32.net> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_PASS, SPF_NONE, TXREP 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: Aurelien Jarno Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" localedef currently blindly trust the archive header. When passed an archive file with the wrong endianess, this leads to a segmentation fault: $ localedef --big-endian --list-archive /usr/lib/locale/locale-archive Segmentation fault (core dumped) When passed non-archive files, asserts are reported on the best case, but sometimes it can lead to a segmentation fault: $ localedef --list-archive /bin/true localedef: programs/locarchive.c:1643: show_archive_content: Assertion `used < GET (head->namehash_used)' failed. Aborted (core dumped) $ localedef --list-archive /usr/lib/locale/C.utf8/LC_COLLATE Segmentation fault (core dumped) This patch improves the user experience by looking at the magic value, which is always written, but never checked. It should still be possible to trigger a segmentation fault with crafted files, but this already catch many cases. Reviewed-by: Adhemerval Zanella --- locale/programs/locarchive.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c index eeec3ab648..477499bd40 100644 --- a/locale/programs/locarchive.c +++ b/locale/programs/locarchive.c @@ -654,6 +654,13 @@ open_archive (struct locarhandle *ah, bool readonly) error (EXIT_FAILURE, errno, _("cannot read archive header")); } + /* Check the magic value */ + if (GET (head.magic) != AR_MAGIC) + { + (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead)); + error (EXIT_FAILURE, 0, _("bad magic value in archive header")); + } + ah->fd = fd; ah->mmaped = st.st_size;