From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 87A65385B50D; Fri, 2 Dec 2022 16:19:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87A65385B50D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669997948; bh=whxH0QdpcshZ+GGQYneF9N9R+DEsxWUNy8oI5zV7C9o=; h=From:To:Subject:Date:From; b=xP3m2mMsx6stk4WJzICZme8qEmdGeCaO1cYrmji6ji/5GPiMry9KFWLVS5KxNUNxe fRfVs7fBw7qkpXrThsI4rXXAs++MWegfKPNsvNV2S+LOPkUovCrEw+Imp46GnT4VIi HmY4Op324AhuOd3w3Dw9eE3rtD1FlPvsvWrIEJyI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: H.J. Lu To: glibc-cvs@sourceware.org Subject: [glibc] x86-64 strncpy: Properly handle the length parameter [BZ# 29839] X-Act-Checkin: glibc X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: f566b028524149ddfebe7f9770a3befb13b81a13 X-Git-Newrev: e5672763c44f16ddbc42809f5def7c6a962602bd Message-Id: <20221202161908.87A65385B50D@sourceware.org> Date: Fri, 2 Dec 2022 16:19:08 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e5672763c44f16ddbc42809f5def7c6a962602bd commit e5672763c44f16ddbc42809f5def7c6a962602bd Author: H.J. Lu Date: Thu Dec 1 16:36:02 2022 -0800 x86-64 strncpy: Properly handle the length parameter [BZ# 29839] 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 Diff: --- 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