From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-HE1-obe.outbound.protection.outlook.com (mail-eopbgr10052.outbound.protection.outlook.com [40.107.1.52]) by sourceware.org (Postfix) with ESMTPS id AD6FE3858D3C for ; Tue, 17 May 2022 09:27:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD6FE3858D3C ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=crveKCoYi4YZ+qEIvXPZWyGNt8IfACiQGE9A/f9YXYsK1mkuiiL5E+wFZkz/t5UCzEjKpda6JIh7KBq22gYFhvBJ8acVRNweimhqMEmdwx0WiQGeH/QBkW/c+iCcgkSB26KmpbudMG6tIIjUrVddT3M8OsqNKSeErCwq8yLZGIC+4dU6VHtTWfINFIfg51CaVoQyqS959n1t+oKFca1tx7k9ULWhOwzYYP89D0xCvvZ+KwqdfAB4Ou48b0ABVWAfnJd1sWfzfeHk/C7q9tVWDbktrHt29CWZMN1K1PAniCctBTIWFe9o6TeW/LQEQMiAZds1frxwMeZ4xE8UhfJrow== 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=rIGE2tOH1VBLqwAJt4ljPx0giXtQXVwAYIaFU6ftDN8=; b=oQfIQafTrkvaDXyc/wnZhObunAC0MrO14xW3mbJoq2BNDKVjXHXlsbWctQhs41vQDueSMlXCEpjIpJRfehzsoX9Iv44klaPZmK7jXG4MY0C2cBwA41G+8+Bo3tiiswxsSM0Y6Zdhyz17eQmdK6HkLqfFt9tGsuwpMmKqmASe/CqkiIlerlI7hyYqAZWn86gcuvwE4vcLo+e8WG/i015kS87KPQR4J2lJe9GR6LfIIwJ+7m9uOpTqP2eWPmSlGzBk77GcquyBDPxbZ3+CT5SU29ObPsgEODQQL+raHgBH0i909rq5BUqKP/gMbeaQiZxx9o0IMiS7TnDCrdNElAzGRw== 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] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com]) Received: from AS8PR07CA0049.eurprd07.prod.outlook.com (2603:10a6:20b:459::30) by AS8PR08MB7791.eurprd08.prod.outlook.com (2603:10a6:20b:52d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Tue, 17 May 2022 09:27:43 +0000 Received: from AM5EUR03FT010.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:459:cafe::36) by AS8PR07CA0049.outlook.office365.com (2603:10a6:20b:459::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.13 via Frontend Transport; Tue, 17 May 2022 09:27:43 +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 AM5EUR03FT010.mail.protection.outlook.com (10.152.16.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13 via Frontend Transport; Tue, 17 May 2022 09:27:43 +0000 Received: ("Tessian outbound 07c1c99345b0:v119"); Tue, 17 May 2022 09:27:43 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: bd2f0f5ea963bda2 X-CR-MTA-TID: 64aa7808 Received: from d502dccd2c6c.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id EF15FA54-EF00-4B9B-967A-2E7C736659E8.1; Tue, 17 May 2022 09:27:36 +0000 Received: from EUR03-AM5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id d502dccd2c6c.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Tue, 17 May 2022 09:27:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TSYyRhRyd/Tce3JhK5fn8VYQdvGwW6tV8Iw4V0669HwZyPG14s4gOcbhhcnJnKUWGsDTHnG19QLdQnRwxtEGWalaIxCaMpNplWykXPH8T8j4TkEDEeh2+emjrSnbdiTaL8PxjSH2ki1TO4zD4tVvoj92H+mFuWjnkHWGcsdHlZOJQ00ZyPasujDwHx+bWE5oILEspDEgQZZ33CFPrr7RIm2KLrptyWRdUPFQqz6YEbmESbXpw5gxi0vn6DFD0V/1T7dMv9N6CvlwJCTMsKRu7SY2iskJ4JH5VabPvWZXfvf/7wOPwPDiqq3VTHVPhgNYPP3T7CbiTeNeJXXpMmYZdg== 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=rIGE2tOH1VBLqwAJt4ljPx0giXtQXVwAYIaFU6ftDN8=; b=Vr5qBSOuwdL0benY4XV60vDNkSZ4qVbpVC2PIYR9AGgBH13FLAfXstR7fozEScyhZz5lmFVeW7l//PDUsv6v12+JqZ0RpfUAkyoQvwID56EudNuUGSizOEnOBgFJIGnhFVMAHg1oRE5nope/ymzTg8Py2WYYOGPMthwS4qP4CdV+N5ORL54Z3ujXwFfyXQQZJZrwrrSzWj2DdH4YJo0cmc4RtQuo5mB1IbIvfNNYMI6lPCaJKaWG/l53TBlihmtIdcvwQuht1Wecc8dBYCaOWBPBtnHDW7zhykRJ0z1LGXGOFybbdFiEpaiCh+mTxFdV5WhE8LuebF6h1xzR7aS/QA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; Received: from DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) by AM8PR08MB6500.eurprd08.prod.outlook.com (2603:10a6:20b:361::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Tue, 17 May 2022 09:27:35 +0000 Received: from DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::9ccc:fe7c:19e5:bfa6]) by DB9PR08MB7179.eurprd08.prod.outlook.com ([fe80::9ccc:fe7c:19e5:bfa6%4]) with mapi id 15.20.5250.018; Tue, 17 May 2022 09:27:35 +0000 Date: Tue, 17 May 2022 10:27:17 +0100 From: Szabolcs Nagy To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v6 1/4] rtld: Use generic argv adjustment in ld.so [BZ #23293] Message-ID: References: Content-Type: multipart/mixed; boundary="eAoBWug0FC8K3VB6" Content-Disposition: inline In-Reply-To: X-ClientProxiedBy: SA9PR13CA0153.namprd13.prod.outlook.com (2603:10b6:806:28::8) To DB9PR08MB7179.eurprd08.prod.outlook.com (2603:10a6:10:2cc::19) MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: ee7c92db-8c61-489c-a0ce-08da37e77f05 X-MS-TrafficTypeDiagnostic: AM8PR08MB6500:EE_|AM5EUR03FT010:EE_|AS8PR08MB7791: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: aMK1WJ5MFldPb6ABYMC7tiXRFlJF54lJ78+LXlPfy1UFLl3f4aeDZpzuTuecWgzI9Yq/GV+HNjfcdsxFUeIIXHbLmC15ngt2YaCkdIYtPVj9+nSXV0zg2Zu68A3vyZ/8bbu9v/9U5Ui0CuhqNG7qWKeai0QZrp6KcS2t7FvJ0GFFg0vypHbWF/yzGprbiJP6MnL+Eqg/NQN77iIunC2zoW+oHWdrEN10qYGgFR6KGDbsmOYvkPHEhPEoRTGkD/fts0vrN5SDmeIZDcixDt+kHlOj+bZdJioVb/w9ZzMAyRNbshe3h8TEjFjBDlFRXWuc5d7Zy6mNrinl8jCmDN/EM8TgwVhYTu4osH9VfWcTMzoOwPmdJCzJEjpqr4FiOLQn7K1wNYmGMUrN793y0L8M5AST2BBr2vfajrBs0b/ASicxQaUE6qDtVKX+wS5zAu4RsqtPhbHbaIlNglKIWxYCVrML28NQZkXVVFwi54sLWE4X+4QNgEagVMfz/0lCwdzJLiQ4RM8XrhwxKEkZrU3p3mRFKGOFWzmA9tw+SiRYTiBsNR6raUiUUYWF5o5kAQGaftVUMB9WI+1NPM64OU5UBaKFfHS0wvCfFjUKepxqIoPchUH7WM/q9UX0lOaqGmHm7vNUQT//KWfOvFCpsiqnA24WXZaWtOd+65hDHcVuqCr/JFx2CrSN1aba1Yg1MuJDipuBHaq7NlLhquamtQfgW2yyIplb2VTYxZV/ZsYemmo= X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR08MB7179.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(186003)(66556008)(6506007)(53546011)(8676002)(66476007)(235185007)(8936002)(508600001)(44832011)(2616005)(38100700002)(316002)(5660300002)(6666004)(2906002)(36756003)(6512007)(26005)(6916009)(33964004)(44144004)(4326008)(66946007)(6486002)(86362001)(41533002)(2700100001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8PR08MB6500 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT010.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: e82691da-3abd-4da1-044b-08da37e77a1e X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Sea4Glrnk7ISToGvH3phUeZYNjb21PAAKjMmfrFeeqRsP7h7yr5Q8zVCk67BFjZV653LJQdp0tWYm/+grQJx1ff5x91625MOpUmD3T4KpWVILSFoXHyq2eQ/t2jj4oENx8vXF/a5T0AhFbWNVPUzr0CNEosrpQHVbW56VcpkrNDeUNN1RB/K+MaQBNCLMTzn94yLG/Efm3xDjjaI8kNtzcF9KnEEiLsow5JHpRK19Zr6hi8fijuVum9yTbhpQVDJ1ucRa0mjQh9V9ptM1CfkomzWKi/qa7TZJ/4/rgTqpeEuu67cszexYmsPzfeUY9kBQz1xFFZePRZxg+otfgxgTW9Xn01wHBCtbMThRtQABxnzjfKVk21Ef+HFjXflR12IrCpN1XDDRwPqVQMtViAg9DDS2KusT5UYr4nnrbN0MLgmgj8JWvo/jteQF373hQFTAdkoxq1csuKj+GpD+LU6fQ20EXzNNRRzfsUckap8SeHR98cIqdTeKINo/Cj/jGTjPWl4JCkJP2VHYK2XeTuYCbprMJ3BrNSGlF2YHA3gB4Y4Pc79YNjmhwtDu/FlS2bGxX6/kZKWyL/pW6cgquUuwjHDFTWYGAQPkIvKquynaIPtTfIuXf+NtXnJlUtuXNcTnl0E4CzvAM47c16DgEdDNuTFFoCSyYHBpb7a6CTjfmvNWOMoRrFPVAR6z7Y8rzZ99zMKl7gr3Gyy/5hvy3ASjBxHNb5G38DHUvpfwywEzZg= 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)(36840700001)(46966006)(40470700004)(44832011)(53546011)(2616005)(235185007)(6666004)(316002)(82310400005)(8936002)(6862004)(8676002)(70586007)(4326008)(70206006)(47076005)(5660300002)(356005)(36756003)(26005)(336012)(6512007)(36860700001)(6486002)(40460700003)(508600001)(2906002)(81166007)(186003)(44144004)(6506007)(86362001)(33964004)(41533002)(2700100001); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 09:27:43.3591 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ee7c92db-8c61-489c-a0ce-08da37e77f05 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: AM5EUR03FT010.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB7791 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.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-List-Received-Date: Tue, 17 May 2022 09:27:50 -0000 --eAoBWug0FC8K3VB6 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline The 05/13/2022 16:56, Adhemerval Zanella wrote: > On 05/05/2022 10:58, Szabolcs Nagy via Libc-alpha wrote: > > +_dl_start_args_adjust (int skip_args) > > +{ > > + void **sp = (void **) (_dl_argv - skip_args - 1); > > Is it fully correctly to materialize the address for 'skip_args' equal to 0? > I don't think it would matter anyway. skip_args == 0 should be fine (any skip_args makes sp point to argc). > > + /* 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 > > Maybe: > > ElfW(auxv_t) ax; > do > { > p = (void**) ((uintptr_t) p + sizeof (ax)); > sp = (void**) ((uintptr_t) sp + sizeof (ax)); > memcpy (&ax, p, sizeof (ax)); > memcpy (sp, &ax, sizeof (ax)); > } > while (ax.a_type != AT_NULL); > > Most targets will inline memcpy and if they do not we don't enable IFUNC > for ld.so. i did this differently, but same in spirit. attaching the patch i committed. > > - 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]) > > { > > Looks ok, but I can't really voucher for hurd code. committed the patch, if it breaks hurd we can fix/revert later. --eAoBWug0FC8K3VB6 Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="v7-0001-rtld-Use-generic-argv-adjustment-in-ld.so-BZ-2329.patch" >From ad43cac44a6860eaefcadadfb2acb349921e96bf Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Fri, 15 Jun 2018 16:14:58 +0100 Subject: [COMMITTED v7] rtld: Use generic argv adjustment in ld.so [BZ #23293] 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 --- 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 578fc14cdb..6e8ed430e2 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1301,6 +1301,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. */ + ElfW(auxv_t) ax; + char *oldp = (char *) (p + 1); + char *newp = (char *) (sp + 1); + do + { + memcpy (&ax, oldp, sizeof (ax)); + memcpy (newp, &ax, sizeof (ax)); + oldp += sizeof (ax); + newp += sizeof (ax); + } + while (ax.a_type != AT_NULL); +#endif +} + static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, @@ -1354,6 +1410,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; @@ -1368,7 +1425,6 @@ dl_main (const ElfW(Phdr) *phdr, GLRO(dl_lazy) = -1; } - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1377,14 +1433,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; } @@ -1394,7 +1448,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; } @@ -1403,7 +1456,6 @@ dl_main (const ElfW(Phdr) *phdr, { GLRO(dl_inhibit_rpath) = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1411,14 +1463,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; } @@ -1426,7 +1476,6 @@ dl_main (const ElfW(Phdr) *phdr, { argv0 = _dl_argv[2]; - _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; } @@ -1434,7 +1483,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; } @@ -1442,7 +1490,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; } @@ -1451,7 +1498,6 @@ dl_main (const ElfW(Phdr) *phdr, { state.mode = rtld_mode_list_tunables; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1460,7 +1506,6 @@ dl_main (const ElfW(Phdr) *phdr, { state.mode = rtld_mode_list_diagnostics; - ++_dl_skip_args; --_dl_argc; ++_dl_argv; } @@ -1506,7 +1551,6 @@ dl_main (const ElfW(Phdr) *phdr, _dl_usage (ld_so_name, NULL); } - ++_dl_skip_args; --_dl_argc; ++_dl_argv; @@ -1605,6 +1649,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]) { -- 2.25.1 --eAoBWug0FC8K3VB6--