From patchwork Sat Jan 6 22:03:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 83466 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 97ED1385DC02 for ; Sat, 6 Jan 2024 22:04:06 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 7673E3858D20 for ; Sat, 6 Jan 2024 22:03:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7673E3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7673E3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::629 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704578622; cv=none; b=tRiZjpuoIoUpkrISYzvJ7gxVrIYsLjTkkdYt5B3dWnkgqCPGxB8pIjM7z4ph9qb5WxPnrvzPEh268smc6gfmYOjZQDJmT68KDrvY9c6j/u/T7uC9TRa/t80w+aWq7Bs+shGyDpaloCXwZSRWxyIplFQqD0Gnts5nZK/77zv8oq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704578622; c=relaxed/simple; bh=fEgFoLwNGNAZ3PSAjGExJR9F085P9u/j+OkxQ8evdIE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ak3rPw/BPx53rByoOt4hNx26/6xm9ftOsOppO3NdJ46CxssbzGJMur7qKCM7GNaaJwlITVnyukHpk9QOPW7bJ14BhWxQ3SVmJXXBZNZPrd5hd9b+OjwFA4RGOk3X+SjQiTS6NaZU8z8eyiK9T3wp7KYr+JACaqaoqa2cPKzvyrA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1d480c6342dso5049455ad.2 for ; Sat, 06 Jan 2024 14:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704578619; x=1705183419; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=dhxZqFZZJuLg/HyrHMIN1EB3gVj3M1U02xCHtNbnAeE=; b=DouiBna5p7zqNFirvAyn46So+8i2v1c+5fXT9PqesEdXgz1mO7b/ptyotVlb7BQ0pk yQFOPzaok2z0JbGtMXFMobn4VAfx3me6JNXaxiRF5dAfrTNJaDBmhSXMgrUqFdxBzRyT Bu61+7ZFZV8DK4nNKTs4y5FeL6IuPYMJfgQIxQE7mtjAFoP2Gbe28lRCmzC7oqcjmc6q rTRpey024mVk7rMmHfsyAdwoP6CPQinqiqlxDC4Z6eZQ6nZN+L8PDgq6QtEWyJi8xCs3 NMNj9xN/0M5NT5o3ArB1LYeUciHbEaEHveIpK+T6/elRJivfyD85o71oRIOSQgTYhzIL l/3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704578619; x=1705183419; 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=dhxZqFZZJuLg/HyrHMIN1EB3gVj3M1U02xCHtNbnAeE=; b=qw/Kb/pp7U525QMCHniF9Rvi9cK00xuFnQOMtfK+lpnEe1DZ7wWEsSg00Wo00iIBmo Q/Kg7sbO9EPFHNCkZw8JWAeDf+M8NOZ4VGL5o9IBDWUj0jUACCEP6nzPP3ciqDFQ8asV PXEgMFJ8N2Oy8BP5MNWhAK9TnacOlhJ+7Aw1EKWMQhA4/xkBXk78iA+MlGMod8yoi2oR isMjGF4WIJ1hTQn+FoVADEU9hK/SvotOPIYIZeCeByZ0LsdVq/0Ad1QwBfEyiOo1K7Vc JuFCQVat1IwDawa6cZiOnvSWYjdnRad/ZTOcLcaxELqDH/oP9rjILyNhziB8kxxOQpCC OzyQ== X-Gm-Message-State: AOJu0Yzabqlnb/wLv9rKUmlJpFjyrcQkRZNe6yOkN87DR9/bm2p5s36B 7Ukw2rQmfMvyS7N+qsggDhYy2twsnNE= X-Google-Smtp-Source: AGHT+IGgsY0C+/Zr+opUh6XBzZ36OKyD1TL0ZPulfkOejFQd+fzZ7yWziwQ+4cvrlyccIZFgEm3kSw== X-Received: by 2002:a17:902:e548:b0:1d4:acc3:304 with SMTP id n8-20020a170902e54800b001d4acc30304mr1613624plf.15.1704578619074; Sat, 06 Jan 2024 14:03:39 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.169.119]) by smtp.gmail.com with ESMTPSA id h5-20020a170902748500b001d36df58ba2sm3475764pll.308.2024.01.06.14.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jan 2024 14:03:38 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 2AA857402F4; Sat, 6 Jan 2024 14:03:37 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: goldstein.w.n@gmail.com Subject: [PATCH] x32: Handle displacement overflow in PLT rewrite [BZ #31218] Date: Sat, 6 Jan 2024 14:03:37 -0800 Message-ID: <20240106220337.1665359-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3021.4 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_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, SPF_HELO_NONE, 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 PLT rewrite calculated displacement with ElfW(Addr) disp = value - branch_start - JMP32_INSN_SIZE; On x32, displacement from 0xf7fbe060 to 0x401030 was calculated as unsigned int disp = 0x401030 - 0xf7fbe060 - 5; with disp == 0x8442fcb and caused displacement overflow. The PLT entry was changed to: 0xf7fbe060 <+0>: e9 cb 2f 44 08 jmp 0x401030 0xf7fbe065 <+5>: cc int3 0xf7fbe066 <+6>: cc int3 0xf7fbe067 <+7>: cc int3 0xf7fbe068 <+8>: cc int3 0xf7fbe069 <+9>: cc int3 0xf7fbe06a <+10>: cc int3 0xf7fbe06b <+11>: cc int3 0xf7fbe06c <+12>: cc int3 0xf7fbe06d <+13>: cc int3 0xf7fbe06e <+14>: cc int3 0xf7fbe06f <+15>: cc int3 x32 has 32-bit address range, but it doesn't wrap address around at 4GB, JMP target was changed to 0x100401030 (0xf7fbe060LL + 0x8442fcbLL + 5), which is above 4GB. Always use uint64_t to calculate displacement. This fixes BZ #31218. Reviewed-by: Noah Goldstein --- sysdeps/x86_64/Makefile | 8 +++++ sysdeps/x86_64/dl-machine.h | 5 +-- sysdeps/x86_64/tst-plt-rewrite2.c | 46 ++++++++++++++++++++++++++++ sysdeps/x86_64/tst-plt-rewritemod2.c | 32 +++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 sysdeps/x86_64/tst-plt-rewrite2.c create mode 100644 sysdeps/x86_64/tst-plt-rewritemod2.c diff --git a/sysdeps/x86_64/Makefile b/sysdeps/x86_64/Makefile index 374bca80d0..836e03a224 100644 --- a/sysdeps/x86_64/Makefile +++ b/sysdeps/x86_64/Makefile @@ -186,9 +186,11 @@ endif ifeq (yes,$(have-z-mark-plt)) tests += \ tst-plt-rewrite1 \ + tst-plt-rewrite2 \ # tests modules-names += \ tst-plt-rewritemod1 \ + tst-plt-rewritemod2 \ # modules-names tst-plt-rewrite1-no-pie = yes @@ -200,6 +202,12 @@ $(objpfx)tst-plt-rewrite1.out: /dev/null $(objpfx)tst-plt-rewrite1 $(tst-plt-rewrite1-ENV) $(make-test-out) > $@ 2>&1; \ grep -q -E "changing 'bar' PLT entry in .*/elf/tst-plt-rewritemod1.so' to direct branch" $@; \ $(evaluate-test) + +tst-plt-rewrite2-no-pie = yes +LDFLAGS-tst-plt-rewrite2 = -Wl,-z,now +LDFLAGS-tst-plt-rewritemod2.so = -Wl,-z,now,-z,undefs +tst-plt-rewrite2-ENV = GLIBC_TUNABLES=glibc.cpu.plt_rewrite=2 +$(objpfx)tst-plt-rewrite2: $(objpfx)tst-plt-rewritemod2.so endif endif # $(subdir) == elf diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h index ca290ef082..2d2ce503a5 100644 --- a/sysdeps/x86_64/dl-machine.h +++ b/sysdeps/x86_64/dl-machine.h @@ -607,8 +607,9 @@ x86_64_rewrite_plt (struct link_map *map, ElfW(Addr) plt_rewrite) /* Skip ENDBR64 if IBT isn't enabled. */ if (!ibt_enabled_p) branch_start = ALIGN_DOWN (branch_start, pltent); - /* Get the displacement from the branch target. */ - ElfW(Addr) disp = value - branch_start - JMP32_INSN_SIZE; + /* Get the displacement from the branch target. NB: We must use + 64-bit integer on x32 to avoid overflow. */ + uint64_t disp = (uint64_t) value - branch_start - JMP32_INSN_SIZE; ElfW(Addr) plt_end; ElfW(Addr) pad; diff --git a/sysdeps/x86_64/tst-plt-rewrite2.c b/sysdeps/x86_64/tst-plt-rewrite2.c new file mode 100644 index 0000000000..8427df5605 --- /dev/null +++ b/sysdeps/x86_64/tst-plt-rewrite2.c @@ -0,0 +1,46 @@ +/* Test PLT rewrite with 32-bit displacement overflow. + Copyright (C) 2024 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 + . */ + +#include + +extern int (*func_p) (void); +extern void foo (void); + +int +func (void) +{ + return 0xbadbeef; +} + +void +bar (void) +{ + TEST_VERIFY (func_p == &func); + TEST_VERIFY (func_p () == 0xbadbeef); +} + +static int +do_test (void) +{ + func_p = &func; + foo (); + bar (); + return 0; +} + +#include diff --git a/sysdeps/x86_64/tst-plt-rewritemod2.c b/sysdeps/x86_64/tst-plt-rewritemod2.c new file mode 100644 index 0000000000..4a86d736b5 --- /dev/null +++ b/sysdeps/x86_64/tst-plt-rewritemod2.c @@ -0,0 +1,32 @@ +/* Check PLT rewrite with 32-bit displacement overflow. + Copyright (C) 2024 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 + . */ + +/* foo calls func with indirect branch via PLT. PLT rewrite shouldn't + change it to direct branch if func is too far way. */ + +#include + +int (*func_p) (void); +extern int func (void); + +void +foo (void) +{ + TEST_VERIFY (func () == 0xbadbeef); + TEST_VERIFY (func_p () == 0xbadbeef); +}