From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id B64993858D35 for ; Fri, 2 Dec 2022 00:37:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B64993858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x62f.google.com with SMTP id p24so3246132plw.1 for ; Thu, 01 Dec 2022 16:37:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Kw+ryq/9uG8GA4OY9dfqq8uFRHmJmvxNd6nro2NK9yo=; b=osXZeM1BchZ68jtbz9LdG22BEznG/EqoYZhd4PhKp6s46IzOGY+ue6hv/b0Oh44H2E Pw+1E6wpi4USAmeSbhuNhOGjTAMrA1v/97QCROluWrZ+1zbazumkbtPxNvY9BuMr4fQA zX4zxmr4dK6agVGEwARFtrcTm3euQyc+k0XDUynkPqLqUl4+vI4mKu/TQQ3TNJU7DCzm SuzcS4A/DnwbnEt8rNaFmmOrT+EtK+2LuR4gYf1onMsylt2cO+0SKpnfSPUTzuUFoyTQ SgPxEVyAVs2BqlF/6JdoL6J2LFrfKbjh5cwuDx8ITunug5Yr3aoHR65J5Shc1JQVtlL0 Jtfw== 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=Kw+ryq/9uG8GA4OY9dfqq8uFRHmJmvxNd6nro2NK9yo=; b=T8ajc+1Od39B0IFGXtuayFhh8SxWUtsxTDFaoNVc3DgN4NjuiJjGVDk77tuwg+2sQo ZCdjZALqZJ+SFsxGsIReQx7kTrDiOGACz1mdTuKkOOtAeJJJk7zXrI2ydg2EFQgkuHPK epQTmY1PyBjes448jMzxgHFX8V0ki9UsjDlKVFW1J6kONzm3KBjUiLFRTnofZbA/DIbp 5lUi6fvN9dO8wWAOqTm/zr0w5kCrbhh4QiTZsybAktG58zr/XOzDcpNRZsQ4PcscRb8O Q3psQ0zCiob5C2hK0bA4q8MY6a1jgGRQgMTHT36GnixJsReV1aRINKmRUjUfjzBJxIG/ tkjw== X-Gm-Message-State: ANoB5plfreALoTUeqy//BSvXiNIukjGx/o5RgKKORCpsSCer4/LITC14 0O94F1Gv0hi5VzXou/Tv6ofAlN+22Ek= X-Google-Smtp-Source: AA0mqf7lQW1ObtAf+4/+j47J88ZumxFjczrNZxtsiOcbHrvH6zyzwz24nNm0Zv90QR3rHJywh9mhXQ== X-Received: by 2002:a17:90a:7e81:b0:212:8210:c8f2 with SMTP id j1-20020a17090a7e8100b002128210c8f2mr16931pjl.2.1669941433498; Thu, 01 Dec 2022 16:37:13 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.30.140]) by smtp.gmail.com with ESMTPSA id s29-20020aa78bdd000000b00575b6d7c458sm3834275pfd.21.2022.12.01.16.37.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 16:37:13 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 7C069740148; Thu, 1 Dec 2022 16:37:11 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: Noah Goldstein Subject: [PATCH] x86-64 strncat: Properly handle the length parameter [BZ# 24097] Date: Thu, 1 Dec 2022 16:37:11 -0800 Message-Id: <20221202003711.358774-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3023.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,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 List-Id: 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 strncat for x32. Tested on x86-64 and x32. On x86-64, libc.so is the same with and without the fix. --- .../x86_64/multiarch/strcat-sse2-unaligned.S | 4 ++ sysdeps/x86_64/multiarch/strncat-avx2.S | 4 ++ sysdeps/x86_64/multiarch/strncat-evex.S | 5 ++ sysdeps/x86_64/x32/Makefile | 2 +- sysdeps/x86_64/x32/tst-size_t-strncat.c | 59 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 sysdeps/x86_64/x32/tst-size_t-strncat.c diff --git a/sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S b/sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S index 9d2ca1d504..b0f9c5fa83 100644 --- a/sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S +++ b/sysdeps/x86_64/multiarch/strcat-sse2-unaligned.S @@ -35,6 +35,10 @@ ENTRY (STRCAT) mov %rdi, %r9 # ifdef USE_AS_STRNCAT +# ifdef __ILP32__ + /* Clear the upper 32 bits. */ + movl %edx, %edx +# endif mov %rdx, %r8 # endif diff --git a/sysdeps/x86_64/multiarch/strncat-avx2.S b/sysdeps/x86_64/multiarch/strncat-avx2.S index ffa58bd0de..73baeee473 100644 --- a/sysdeps/x86_64/multiarch/strncat-avx2.S +++ b/sysdeps/x86_64/multiarch/strncat-avx2.S @@ -51,6 +51,10 @@ .section SECTION(.text), "ax", @progbits ENTRY(STRNCAT) +# 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 using the non-length variant {wcs|str}cat. */ diff --git a/sysdeps/x86_64/multiarch/strncat-evex.S b/sysdeps/x86_64/multiarch/strncat-evex.S index bced4e8944..1ef3a22209 100644 --- a/sysdeps/x86_64/multiarch/strncat-evex.S +++ b/sysdeps/x86_64/multiarch/strncat-evex.S @@ -79,6 +79,11 @@ .section SECTION(.text), "ax", @progbits ENTRY(STRNCAT) +# ifdef __ILP32__ + /* Clear the upper 32 bits. */ + movl %edx, %edx +# endif + movq %rdi, %rax /* NB: It's safe to filter out zero-length strings WITHOUT diff --git a/sysdeps/x86_64/x32/Makefile b/sysdeps/x86_64/x32/Makefile index 31732aa248..a015789a4f 100644 --- a/sysdeps/x86_64/x32/Makefile +++ b/sysdeps/x86_64/x32/Makefile @@ -10,7 +10,7 @@ ifeq ($(subdir),string) tests += tst-size_t-memchr tst-size_t-memcmp tst-size_t-memcpy \ tst-size_t-memrchr tst-size_t-memset tst-size_t-strncasecmp \ tst-size_t-strncmp tst-size_t-strncpy tst-size_t-strnlen \ - tst-size_t-memcmp-2 + tst-size_t-memcmp-2 tst-size_t-strncat endif ifeq ($(subdir),wcsmbs) diff --git a/sysdeps/x86_64/x32/tst-size_t-strncat.c b/sysdeps/x86_64/x32/tst-size_t-strncat.c new file mode 100644 index 0000000000..eda93f5af9 --- /dev/null +++ b/sysdeps/x86_64/x32/tst-size_t-strncat.c @@ -0,0 +1,59 @@ +/* Test strncat with size_t in the lower 32 bits of 64-bit register. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define TEST_NAME "strncat" +#include "test-size_t.h" + +IMPL (strncat, 1) + +typedef char *(*proto_t) (char *, const char*, size_t); + +static void * +__attribute__ ((noinline, noclone)) +do_strncat (parameter_t a, parameter_t b) +{ + return CALL (&b, a.p, b.p, a.len); +} + +static int +test_main (void) +{ + test_init (); + + parameter_t dest = { { page_size - 1 }, buf1 }; + parameter_t src = { { 0 }, buf2 }; + + int ret = 0; + FOR_EACH_IMPL (impl, 0) + { + src.fn = impl->fn; + buf1[0] = '\0'; + do_strncat (dest, src); + int res = strncmp (dest.p, src.p, dest.len); + if (res) + { + error (0, 0, "Wrong result in function %s: %i != 0", + impl->name, res); + ret = 1; + } + } + + return ret ? EXIT_FAILURE : EXIT_SUCCESS; +} + +#include -- 2.38.1