From patchwork Thu May 5 13:58:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 53519 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 1353A3857827 for ; Thu, 5 May 2022 14:00:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1353A3857827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1651759222; bh=VqfHVVFde5W0zoZgSHULAv3YQvoopC5jeytq92eCi04=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=go1gUJgOAYmEFDGPuC/b4o8ZwaIuvYTI+KbNpWS5VzUBHj/kqZqX8TER729OBfgpV SqQO4ohhMKFhWQwA9sgBkUwmvy/EauCqd2aWIpa0Ut1L7klgZB1hhxFsrGmeM+h5YH bEJI0zVfKF4zVEx3ibJ+vKKN1ryiNRYlqqVGWSRw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70057.outbound.protection.outlook.com [40.107.7.57]) by sourceware.org (Postfix) with ESMTPS id 51A813858D3C for ; Thu, 5 May 2022 13:59:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 51A813858D3C ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=J6vYwjT3GquzET14KzFndSDmGcOaXnyMdaGM+1M6fs6LVTGnPgIBxo7gTm0bXYQH3ghZyUo/GfGtiFyXfTBWgTLfjbbHthagPquTesbS98AQst4HH1BTT1+YXQAnvZgQ5l7Jfelcg1O5KL15wtecoyFf6y2w+um2hzp6MgWnlbDUvVPfl++zQ4FXWWh4yEz4ZPYAm+Qs+M7tG3u1FfhWVEkBmBNNMR3afHmlksV4Fx/elMY/5eSXCf2vc+Wxo0KIuoqclb1HpleQ5NraRQ70Xgs/MjSIXmkbERDBeHZbIWHev44BUXg4XuaMWi4nb06If2T6eADXM7cSrYuKPj9toA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VqfHVVFde5W0zoZgSHULAv3YQvoopC5jeytq92eCi04=; b=Tj75onUytHKZ/1bLxoH61ZrCJ6tmp1E08PwcHzkfwY6SH6qjl/Bbfb16SWCjBSK8VcdeUr/HgRLqmAryZeJH81KNYJtBlWs5mxO73miWa5oFBPCEoNwyX/DLR4O4GsG3pKSxqFglpvn5ORkus7y9vfnnX47WVJVtbslwm3I++wm2e0SuavQy/90eg78DYcK1Hsw0fLJ/rPv6pQkmx63h/gXXE9TAuKs/+JMof6W3fAt+2PkyIOkXADe6vOP8Jb9dM5Y4Fw8U54mmwAofsbXCiE2mes8lB8ZK8pUiLTiObqUxDbL7ZiHg7QXtoDBtjjgTYzBKOQZowz63YwiCAiHiVw== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1, 1, smtp.mailfrom=arm.com] dmarc=[1, 1, header.from=arm.com]) Received: from AS9PR06CA0568.eurprd06.prod.outlook.com (2603:10a6:20b:485::30) by DB8PR08MB5498.eurprd08.prod.outlook.com (2603:10a6:10:11c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.24; Thu, 5 May 2022 13:59:03 +0000 Received: from AM5EUR03FT005.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:485:cafe::f0) by AS9PR06CA0568.outlook.office365.com (2603:10a6:20b:485::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.14 via Frontend Transport; Thu, 5 May 2022 13:59:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT005.mail.protection.outlook.com (10.152.16.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.15 via Frontend Transport; Thu, 5 May 2022 13:59:03 +0000 Received: ("Tessian outbound 62985e3c34b6:v118"); Thu, 05 May 2022 13:59:03 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 70ae863129179b0f X-CR-MTA-TID: 64aa7808 Received: from b745abee7bb6.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 22CDE61A-E3BB-4EB5-ADE7-3F91D8FCBB58.1; Thu, 05 May 2022 13:58:56 +0000 Received: from EUR02-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id b745abee7bb6.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 05 May 2022 13:58:56 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=htWT628GcXS0tyk4lZOzQBaO8chrcXUne+oDwWA2OuXyZoMVfxOba7bbPTwDzD6T++2jIr5tUn5f9MePUIE8b/WZ/kLCPQBJqJuv8P90HeV4xqoj1ERTZEBGWckNd1jefQrTgPZcEf/lxp10b+Qlc1cnBxVDVapX9R9cWJqDXBWUdS813+bgM9/efWNMBwkGhU/mnIO7mgvTIrtrNeMMrNXqQ+Gb/T3qyeABcPPJrO+4Tc93dOVrb+zVTxD+LAavO0yAAI/tRNn6OaHRqT2BSNuLSkgk5DUtRdDkUDGv92c7ctvqFBucOpznireOV3XjMB+3yIt1bowvZQjubwiATw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VqfHVVFde5W0zoZgSHULAv3YQvoopC5jeytq92eCi04=; b=h6SMYleCyzEFQLY/6KsTalr7xcbSaBJ0kXYdrkQDAWK7m3wXQd8xrahsIrJV8uC3c7aZOyclKtFcAcEpI7la/wymgKwhhq5UR/qDvHb/bn0uARlBUfU1ApTXp/2YpJChraTB/TcaB7MfK+JpPWpQ7VaMXURkEO0hX1XceL9KshCE9eaPimSg/gZnbuBcZyd1esTW/YS9/xQVU8lyUoRVtsOjAKVaASgIVHDYOlzgEHZK42286CsJR1uDr/vFC5rT+kHCG0I9tqh+nSBGXefw7YrZQRPe4e0w2IUuH+H+YCiAI5YOg4g0/04Qi6ZR4q+TvWWCGYIhzSAxk19h7sr2Rw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from AS9PR06CA0265.eurprd06.prod.outlook.com (2603:10a6:20b:45f::34) by AM9PR08MB6786.eurprd08.prod.outlook.com (2603:10a6:20b:305::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.14; Thu, 5 May 2022 13:58:54 +0000 Received: from AM5EUR03FT050.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:45f:cafe::46) by AS9PR06CA0265.outlook.office365.com (2603:10a6:20b:45f::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.14 via Frontend Transport; Thu, 5 May 2022 13:58:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; Received: from nebula.arm.com (40.67.248.234) by AM5EUR03FT050.mail.protection.outlook.com (10.152.17.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5227.15 via Frontend Transport; Thu, 5 May 2022 13:58:54 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.27; Thu, 5 May 2022 13:58:52 +0000 Received: from armchair.cambridge.arm.com (10.2.80.71) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.2308.27 via Frontend Transport; Thu, 5 May 2022 13:58:52 +0000 To: Subject: [PATCH v6 1/4] rtld: Use generic argv adjustment in ld.so [BZ #23293] Date: Thu, 5 May 2022 14:58:48 +0100 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-Office365-Filtering-Correlation-Id: 8e493dbb-1133-493c-1c75-08da2e9f6999 X-MS-TrafficTypeDiagnostic: AM9PR08MB6786:EE_|AM5EUR03FT005:EE_|DB8PR08MB5498:EE_ X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: duLUEkjA7iuWx4O9C2Z9OdhdN1ERepYrijAW+3pWDmokc2rtyGM8biOUDw6sZdWb/uuZeIbqvdV23X6Caa4+tkB3A21hHmIKBDVH2QDqFhZ4kURXSO2D9bBSY1OyyMqQOSz7wz7JuAQ0DC3f33RCj/txzaoPFoGnx4NQ5sMjEee3fK+k0nqWBOD2pCL44UJS1pNwz5mKidPkas6icy4u7S5xKyhEGCcpHdotjE5UuNxuciBwRIZBaKZbRIv3BnGLWTTmIFB6Hi5GFUv5YNE1PccmacfepiyE6Ygyjl+jX2aykJtH1i6xapzltkwT9B8gKrr0xbZSxi3JuFP91hRhxbO/3cpeHmj782MIXGmGXmYg7ZWvUxBW78B14ygmBtlv0sf7W996EKiMeWKGyvsaH+zX3i6v3JamEv5Be1Leq6Efr2v6fN0yy1TXh8NT4XkfLZVxiQwEY0/2Ih4qtqk+8U+ob+Ugjts7o65YBohxvWn203K9qKvHq9EwYF+brVt3VPQGAP927GHPsiz5i3qAeKON6aeqw8W5P8V2HfWmLFru63zcuVzy28FotE6p/4lYdod1oy7j2LscoxfP48lLo7qaV/nuhRcaFF5qXLN9EpsWXARPl7BFzJQQW/k1E7nKVdnfqGGS4lT4yGb0shJtN5kngx1KvYNzSPFeX2Njvg0PEhEWvEc68z8ADkaLn2xF/sLM0Xc8Vsl1OJuea1FUpr9Ha+sCR6kAiJAQVY8qWQ8= X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(40460700003)(336012)(2616005)(356005)(426003)(186003)(7696005)(5660300002)(6666004)(8936002)(36756003)(44832011)(82310400005)(36860700001)(83380400001)(316002)(2906002)(86362001)(6916009)(26005)(508600001)(8676002)(70586007)(70206006)(81166007)(47076005)(41533002)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB6786 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 933ab7f7-4cb9-4b3f-2e01-08da2e9f644d X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LLrDG8FhVfdU43ud97v5b5dWZnqBdFRePJ2HC2bOmUJTMQi/XjdAG6WfZ9SvRtLGe1ZgMXMuUqfwQhF7mEbgWNFL4yzwUkUOkphoHUgxEw858zo+tL6mljrev8wBpjAc2bvjDa0lP5uY+p6lS9Ar/jXEIydUm5jWIxntmkPGkCQhFgYLgZgduXGES0B7X6LYHvCcZbyfj3oYcL7HogR4FLqQAhHLYbf/Si/JsPv4N9R0roXCVPX7VBJy6yB0PVJa7Fan/tcNRHz374E5SGR850JmwwLEfMGejiB6rPiw4TUhwnaQCZZMYyhxqQkyfGOs8FwCvZ8+OWWePfCsmQU5AcVOO2Ov60lDRfCS5Sp/8n0XVKD7NIvlZ+p33Ec6Ae6HPD/3SfLFJxhRcTzXkpQqfJ3C/7FWb/lgHbkDnUA7sRcgJG9BFBer0VRrDETwzcReAoEryYAKq9BLd8mB0uw5oVEea74dcYWeqSZkkIfbg9yD5C+nJ8NcktwEggne/wIaU3GKcygc5eJvNe10GmZZVO5DwVwdxeCWgwAXu4nfdvD4AzpM5on82kk6NEgviYX8uW34MMGJEuMoA6RRYdI7Qmlu1nfMaqk/DFlyHBymNOyGDEOTWpMDcjokG5w1ArWn0z75TDeF09vB9VKjOrOHqWu5LGEG0Uy4D02FnBT8sQQEwQGlawR71JoYC6kNZUSycWGA6K/DSDJY7VldVkoYVA== X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(70586007)(40460700003)(2616005)(70206006)(81166007)(8676002)(5660300002)(186003)(8936002)(82310400005)(83380400001)(6916009)(508600001)(26005)(44832011)(7696005)(2906002)(36860700001)(47076005)(36756003)(86362001)(336012)(426003)(6666004)(316002)(41533002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 May 2022 13:59:03.1885 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8e493dbb-1133-493c-1c75-08da2e9f6999 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT005.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR08MB5498 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY 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: , X-Patchwork-Original-From: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When an executable is invoked as ./ld.so [ld.so-args] ./exe [exe-args] then the argv is adujusted in ld.so before calling the entry point of the executable so ld.so args are not visible to it. On most targets this requires moving argv, env and auxv on the stack to ensure correct stack alignment at the entry point. This had several issues: - The code for this adjustment on the stack is written in asm as part of the target specific ld.so _start code which is hard to maintain. - The adjustment is done after _dl_start returns, where it's too late to update GLRO(dl_auxv), as it is already readonly, so it points to memory that was clobbered by the adjustment. This is bug 23293. - _environ is also wrong in ld.so after the adjustment, but it is likely not used after _dl_start returns so this is not user visible. - _dl_argv was updated, but for this it was moved out of relro, which changes security properties across targets unnecessarily. This patch introduces a generic _dl_start_args_adjust function that handles the argument adjustments after ld.so processed its own args and before relro protection is applied. The same algorithm is used on all targets, _dl_skip_args is now 0, so existing target specific adjustment code is no longer used. The bug affects aarch64, alpha, arc, arm, csky, ia64, nios2, s390-32 and sparc, other targets don't need the change in principle, only for consistency. The GNU Hurd start code relied on _dl_skip_args after dl_main returned, now it checks directly if args were adjusted and fixes the Hurd startup data accordingly. Follow up patches can remove _dl_skip_args and DL_ARGV_NOT_RELRO. Tested on aarch64-linux-gnu and cross tested on i686-gnu. Reviewed-by: Adhemerval Zanella Reviewed-by: Adhemerval Zanella --- v6: - don't pass start_argptr to _dl_main, just use _dl_argv-1. - add comment for _dl_start_args_adjust. - add assert checks to _dl_start_args_adjust and simplify it a bit. v5: - Hurd specific changes. v4: - New code is unconditionally used on all targets. - Hide auxv adjustments behind HAVE_AUX_VECTOR. - DL_NEED_START_ARGS_ADJUST macro is removed. - _dl_skip_args is no longer unused. - start_argptr is passed down to dl_main instead of using a global. - moved aarch64 DL_ARGV_NOT_RELRO removal to separate patch. v2: - use p != NULL, and a_type != AT_NULL - remove the confusing paragraph from the commit message. --- elf/rtld.c | 73 ++++++++++++++++++++++++++++------- sysdeps/mach/hurd/dl-sysdep.c | 30 +++++++------- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/elf/rtld.c b/elf/rtld.c index 3b2e05bf4c..b5070d453f 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1306,6 +1306,62 @@ rtld_setup_main_map (struct link_map *main_map) return has_interp; } +/* Adjusts the contents of the stack and related globals for the user + entry point. The ld.so processed skip_args arguments and bumped + _dl_argv and _dl_argc accordingly. Those arguments are removed from + argv here. */ +static void +_dl_start_args_adjust (int skip_args) +{ + void **sp = (void **) (_dl_argv - skip_args - 1); + void **p = sp + skip_args; + + if (skip_args == 0) + return; + + /* Sanity check. */ + intptr_t argc = (intptr_t) sp[0] - skip_args; + assert (argc == _dl_argc); + + /* Adjust argc on stack. */ + sp[0] = (void *) (intptr_t) _dl_argc; + + /* Update globals in rtld. */ + _dl_argv -= skip_args; + _environ -= skip_args; + + /* Shuffle argv down. */ + do + *++sp = *++p; + while (*p != NULL); + + assert (_environ == (char **) (sp + 1)); + + /* Shuffle envp down. */ + do + *++sp = *++p; + while (*p != NULL); + +#ifdef HAVE_AUX_VECTOR + void **auxv = (void **) GLRO(dl_auxv) - skip_args; + GLRO(dl_auxv) = (ElfW(auxv_t *)) auxv; /* Aliasing violation. */ + assert (auxv == sp + 1); + + /* Shuffle auxv down. */ + void *a, *b; /* Use a pair of pointers for an auxv entry. */ + unsigned long a_type; + do + { + a_type = ((ElfW(auxv_t) *) (p + 1))->a_type; + a = *++p; + b = *++p; + *++sp = a; + *++sp = b; + } + while (a_type != AT_NULL); +#endif +} + static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, @@ -1359,6 +1415,7 @@ dl_main (const ElfW(Phdr) *phdr, rtld_is_main = true; char *argv0 = NULL; + char **orig_argv = _dl_argv; /* Note the place where the dynamic linker actually came from. */ GL(dl_rtld_map).l_name = rtld_progname; @@ -1373,7 +1430,6 @@ dl_main (const ElfW(Phdr) *phdr, GLRO(dl_lazy) = -1; } - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1382,14 +1438,12 @@ dl_main (const ElfW(Phdr) *phdr, if (state.mode != rtld_mode_help) state.mode = rtld_mode_verify; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } else if (! strcmp (_dl_argv[1], "--inhibit-cache")) { GLRO(dl_inhibit_cache) = 1; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1399,7 +1453,6 @@ dl_main (const ElfW(Phdr) *phdr, state.library_path = _dl_argv[2]; state.library_path_source = "--library-path"; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1408,7 +1461,6 @@ dl_main (const ElfW(Phdr) *phdr, { GLRO(dl_inhibit_rpath) = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1416,14 +1468,12 @@ dl_main (const ElfW(Phdr) *phdr, { audit_list_add_string (&state.audit_list, _dl_argv[2]); - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2) { state.preloadarg = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1431,7 +1481,6 @@ dl_main (const ElfW(Phdr) *phdr, { argv0 = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1439,7 +1488,6 @@ dl_main (const ElfW(Phdr) *phdr, && _dl_argc > 2) { state.glibc_hwcaps_prepend = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1447,7 +1495,6 @@ dl_main (const ElfW(Phdr) *phdr, && _dl_argc > 2) { state.glibc_hwcaps_mask = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1456,7 +1503,6 @@ dl_main (const ElfW(Phdr) *phdr, { state.mode = rtld_mode_list_tunables; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1465,7 +1511,6 @@ dl_main (const ElfW(Phdr) *phdr, { state.mode = rtld_mode_list_diagnostics; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1511,7 +1556,6 @@ dl_main (const ElfW(Phdr) *phdr, _dl_usage (ld_so_name, NULL); } - ++_dl_skip_args; --_dl_argc; ++_dl_argv; @@ -1610,6 +1654,9 @@ dl_main (const ElfW(Phdr) *phdr, /* Set the argv[0] string now that we've processed the executable. */ if (argv0 != NULL) _dl_argv[0] = argv0; + + /* Adjust arguments for the application entry point. */ + _dl_start_args_adjust (_dl_argv - orig_argv); } else { diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c index 3cbe075615..8373962e62 100644 --- a/sysdeps/mach/hurd/dl-sysdep.c +++ b/sysdeps/mach/hurd/dl-sysdep.c @@ -76,6 +76,7 @@ _dl_sysdep_start (void **start_argptr, { void go (intptr_t *argdata) { + char *orig_argv0; char **p; /* Cache the information in various global variables. */ @@ -84,6 +85,8 @@ _dl_sysdep_start (void **start_argptr, _environ = &_dl_argv[_dl_argc + 1]; for (p = _environ; *p++;); /* Skip environ pointers and terminator. */ + orig_argv0 = _dl_argv[0]; + if ((void *) p == _dl_argv[0]) { static struct hurd_startup_data nodata; @@ -173,30 +176,23 @@ _dl_sysdep_start (void **start_argptr, /* The call above might screw a few things up. - First of all, if _dl_skip_args is nonzero, we are ignoring - the first few arguments. However, if we have no Hurd startup - data, it is the magical convention that ARGV[0] == P. The + P is the location after the terminating NULL of the list of + environment variables. It has to point to the Hurd startup + data or if that's missing then P == ARGV[0] must hold. The startup code in init-first.c will get confused if this is not the case, so we must rearrange things to make it so. We'll - overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args]. + recompute P and move the Hurd data or the new ARGV[0] there. - Secondly, if we need to be secure, it removes some dangerous - environment variables. If we have no Hurd startup date this - changes P (since that's the location after the terminating - NULL in the list of environment variables). We do the same - thing as in the first case but make sure we recalculate P. - If we do have Hurd startup data, we have to move the data - such that it starts just after the terminating NULL in the - environment list. + Note: directly invoked ld.so can move arguments and env vars. We use memmove, since the locations might overlap. */ - if (__libc_enable_secure || _dl_skip_args) - { - char **newp; - for (newp = _environ; *newp++;); + char **newp; + for (newp = _environ; *newp++;); - if (_dl_argv[-_dl_skip_args] == (char *) p) + if (newp != p || _dl_argv[0] != orig_argv0) + { + if (orig_argv0 == (char *) p) { if ((char *) newp != _dl_argv[0]) {