From patchwork Fri Dec 2 00:36:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 61334 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 B09F63858C33 for ; Fri, 2 Dec 2022 00:36:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B09F63858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669941391; bh=fOTuW7j3EvN3rzmGWgWAWU4xJ1iKOQ2ijTt0CMIYVCU=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Wa6fx4SI0aXf8pv/ilgg5VJvxHeT7sK5E22XdY5SLnZxCtrkH41ohbVkACzNboMR4 g55kd8fglOmmuBXIGsDkeWnvUBcJlJpl0MTLjbZXadeOjY9Ey7ShSZ5zKVTNC1astE oQQsila6nd5RxslT8j/XK2tVw1qbJ9FRXFq9T0Ik= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id 6FFD33858D35 for ; Fri, 2 Dec 2022 00:36:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6FFD33858D35 Received: by mail-pg1-x533.google.com with SMTP id 62so3067779pgb.13 for ; Thu, 01 Dec 2022 16:36:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fOTuW7j3EvN3rzmGWgWAWU4xJ1iKOQ2ijTt0CMIYVCU=; b=Di2Y9xi6lFbK96Th5iL8ZAlEVnYfbCkws7ejpxgxjkYWkILusAErPlmEbLh2EJAZmH 785O6FFJs2IqrF9uCyi66+OxHPiUfJ7iuZhTmUlOpUhRxPnKc0cj9WUb1yOUHXh7dnM1 exHUenKU5bmOi46idpePFKuYRTRpWNp3fRVm/umetRl00ZnVy+nVx54IyPCj/T1TxZ1z ywoq6NM/5UDW+sxPRUtRCCC5QqPD+z5xypHsJrYdTgCuRrHaXJ0nqrFpMJf+HwOK5aj2 CKCCJHJHowAuNAr/n130cUYi9FqEtMhj5FXfBO9916iGIWWLCcfqFFfFF7BbZt24lSN2 BLbQ== X-Gm-Message-State: ANoB5pkiZmWQTinoWUpWzpeIncVDXsVe6nWL7IWYgBa27IDMzgU6liPk AJOW1GowibU8XjLZhzYId2dugK65Mcw= X-Google-Smtp-Source: AA0mqf59+dEGIW8qaXUSWDdbIqLI6EwHrxM8iq/3k/4FWxvwNi0lnqT7OPbxBv9rurzf+G6rll6aSA== X-Received: by 2002:a62:ee14:0:b0:566:900d:6073 with SMTP id e20-20020a62ee14000000b00566900d6073mr14535pfi.24.1669941365247; Thu, 01 Dec 2022 16:36:05 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.140]) by smtp.gmail.com with ESMTPSA id c10-20020aa7952a000000b00574a3dc1453sm3967698pfp.14.2022.12.01.16.36.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 16:36:04 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id F2D05740148; Thu, 1 Dec 2022 16:36:02 -0800 (PST) To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH] x86-64 strncpy: Properly handle the length parameter [BZ# 29839] Date: Thu, 1 Dec 2022 16:36:02 -0800 Message-Id: <20221202003602.358708-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3023.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" On x32, the size_t parameter may be passed in the lower 32 bits of a 64-bit register with the non-zero upper 32 bits. The string/memory functions written in assembly can only use the lower 32 bits of a 64-bit register as length or must clear the upper 32 bits before using the full 64-bit register for length. This pach fixes strncpy for x32. Tested on x86-64 and x32. On x86-64, libc.so is the same with and without the fix. Reviewed-by: Noah Goldstein --- sysdeps/x86_64/multiarch/strncpy-avx2.S | 4 ++++ sysdeps/x86_64/multiarch/strncpy-evex.S | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/sysdeps/x86_64/multiarch/strncpy-avx2.S b/sysdeps/x86_64/multiarch/strncpy-avx2.S index e9afd8fbed..3e6350ce4a 100644 --- a/sysdeps/x86_64/multiarch/strncpy-avx2.S +++ b/sysdeps/x86_64/multiarch/strncpy-avx2.S @@ -52,6 +52,10 @@ .section SECTION(.text), "ax", @progbits ENTRY(STRNCPY) +# ifdef __ILP32__ + /* Clear the upper 32 bits. */ + movl %edx, %edx +# endif /* Filter zero length strings and very long strings. Zero length strings just return, very long strings are handled by just running rep stos{b|l} to zero set (which will almost diff --git a/sysdeps/x86_64/multiarch/strncpy-evex.S b/sysdeps/x86_64/multiarch/strncpy-evex.S index 49eaf4cbd9..dec8cccc2b 100644 --- a/sysdeps/x86_64/multiarch/strncpy-evex.S +++ b/sysdeps/x86_64/multiarch/strncpy-evex.S @@ -80,6 +80,10 @@ .section SECTION(.text), "ax", @progbits ENTRY(STRNCPY) +# ifdef __ILP32__ + /* Clear the upper 32 bits. */ + movl %edx, %edx +# endif /* Filter zero length strings and very long strings. Zero length strings just return, very long strings are handled by just running rep stos{b|l} to zero set (which will almost