From patchwork Fri Dec 11 16:35:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 41364 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 667EC3973034; Fri, 11 Dec 2020 16:36:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 667EC3973034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1607704560; bh=LQapZkCVUihm03QE6CtNE6MFiUHCedv93lNTEWy8B2U=; 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=UySDa42pvAceUxXh0t5ou7mMfgZtnMxliAY4WhCjPtPk6k+LPY6VgKjSRk1XJsnTt fY2FfGiRfwgGr1zrBe8vXf1iHzamS514pJjCy2KYqTF1Q3uLtYTlQ9NahUdg9DA9Uy 9kLHTFx3bGbnNbCySlkfR/+F2V3VCAim4KaFhYHE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2060.outbound.protection.outlook.com [40.107.22.60]) by sourceware.org (Postfix) with ESMTPS id A8308386F45F for ; Fri, 11 Dec 2020 16:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A8308386F45F Received: from DB6PR0802CA0043.eurprd08.prod.outlook.com (2603:10a6:4:a3::29) by AM5PR0801MB1908.eurprd08.prod.outlook.com (2603:10a6:203:4b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.21; Fri, 11 Dec 2020 16:35:53 +0000 Received: from DB5EUR03FT034.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:a3:cafe::de) by DB6PR0802CA0043.outlook.office365.com (2603:10a6:4:a3::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Fri, 11 Dec 2020 16:35:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;sourceware.org; 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 DB5EUR03FT034.mail.protection.outlook.com (10.152.20.87) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Fri, 11 Dec 2020 16:35:53 +0000 Received: ("Tessian outbound 6ec21dac9dd3:v71"); Fri, 11 Dec 2020 16:35:53 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: a682841464b6c36f X-CR-MTA-TID: 64aa7808 Received: from 632265484c9d.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id B1536DF6-AE1B-4240-BF38-B0F3BDE60A7B.1; Fri, 11 Dec 2020 16:35:38 +0000 Received: from EUR03-AM5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 632265484c9d.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 11 Dec 2020 16:35:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eLK9CDmB+tp31OxKH2w8NFkzW89ScNjjacQ0GgkBGcAggP1PUdj3yg8l9I3u8mRId991/MXM/+XlsbbtBEiqgthbUlBuZiCmbV7AMqk5xvvpeur7EVNM2YcR0prwNDmGFOhdyRr1piHfZbPltg2EFKkwail1kPajz/0evvEIYgPQCJhGZGsVLVO6kIM4zAHve49QlQm3/2QCZnzyAY1jnrcIs4Lu2Jl4aWQn5NALf67E++pcYdv0l56yMhvtrJVAsqemcmHR3PgpxXPdPx6e2lTIQDX8eY7j0F1CYS7a21SR1YHjAngFKCJblyttjioJjFiVpHMrM233WBAmq5romw== 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-SenderADCheck; bh=LQapZkCVUihm03QE6CtNE6MFiUHCedv93lNTEWy8B2U=; b=GqUoqnO0ig7Vu+zmKWspxUTsIgV+hR1cYXHqUSXQbZDqfq/pdeoGPBbACXeRWV35OFliGKVFtFbt6y3Hi3uJXp6Ad9NwcDqsvxnsuXHALE5/KdxZzBvxybNpcRkhon/F0q44nbBnEHRV4giedizJ37TfdJ4kg5/8qeQ7OBfRcn036rHeL/yvVFBFt8R72oR2OEaje+xnRXZ4eZvBDkjteSuDeCyc+cx/tqU88eHmEw/+rHi+cn+JvxB1gZ/QoA+pMjt6fSmpQiKs0CFFyBRQMeV2Nk8e7TNIlTwQ6dlwrrnLLVtPTiERcPxi9bnj+F2jGrRelKPstSN04XMPx3iHbg== 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: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) by PA4PR08MB6256.eurprd08.prod.outlook.com (2603:10a6:102:e6::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12; Fri, 11 Dec 2020 16:35:36 +0000 Received: from PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2]) by PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2%4]) with mapi id 15.20.3654.013; Fri, 11 Dec 2020 16:35:36 +0000 To: libc-alpha@sourceware.org Subject: [PATCH 1/2] elf: inline lose for error handling Date: Fri, 11 Dec 2020 16:35:30 +0000 Message-Id: <98caa13064bb554288e3e2984bc11b81ed3d7393.1607703178.git.szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.54] X-ClientProxiedBy: LO2P265CA0300.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a5::24) To PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.54) by LO2P265CA0300.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a5::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Fri, 11 Dec 2020 16:35:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 502f5206-9958-4484-c3fc-08d89df2d3d8 X-MS-TrafficTypeDiagnostic: PA4PR08MB6256:|AM5PR0801MB1908: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:6108;OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: jA5sVifL7QEUsZf1NLoN08696sDD2ZHxAW+31E3JZ7CYFjfAe+5GqrrNhEvdUuaAlGIxxJFeTGDJFUhiq8gdYI8zTWLlXTZ5jbp3Gtt2JRU2ABkgq2n0x9Ut5Eyt+wL5X8Ca4QPY7xXpZ9+Cj7EF5dgmNR50YPXfq0n/+f6QmGMl5QZXB2kYbP4g572BQbOLHlJ3noUQir+0wUDVPRqUfXhY+2tB3S7r//4/TpPJHgCsuT3ZJLOcKBwzaSbTCzJtNCh2XTgbK5C3Ig8v/VmdJCt6+uAwpBXTUWbbLcs0kHcv4kuiUro+bgzaaIw76sFQPr7moQSLFt1pW1jZNRkA1Kq0k5vRsv+V3l3TqyqZ/Dk6WTzaAxZez93iJVMkuEHx X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PR3PR08MB5564.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(136003)(39860400002)(366004)(396003)(376002)(346002)(6486002)(66946007)(5660300002)(316002)(52116002)(6506007)(66476007)(66556008)(186003)(8936002)(478600001)(69590400008)(16526019)(2616005)(83380400001)(26005)(6666004)(86362001)(6916009)(44832011)(8676002)(956004)(6512007)(36756003)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: JJ+OvgyPoJMwL3i4s9PdISIqq1lCwLfp4t/5HPCGTYydC5JChUCcWo7q1L6guqFEycR48OmQctSXd5YiN99Aao/Z4SXvQNUkKgawnSeTgcpo8yYEssiXjaUx2XseD8zwYYgF+fZVhe9sdJouN5lSfWNnBJ4eb8FIkwjD4Bs3lszjN3QzUOO5meAqGRvh+IQBIkR5iHnUERYIhqM01Ttc1E96dGVqCew7CLrPiU5c5rk3JsW03zWQDpo3UbQOwFlqAgEEmtb0Hb4KJAkqjIfGOIV3JThqISbnSMoYR538fM7N5pPSTu3K/PwVANSLYlUF/6qxRy+9ZYHw6LxYx7OEjs7muNUuTbQlJDi2/hXokwWdQrYyTNWo4hfkRw0Nly0CmJmigFvx77Jnf6mGfNDMa/KaDt2Hbeh3RTVo9LR5ELJbEwM2pZc/0+zIvzam/5KUfJpJXBScgh53hfPyT1zsFxsOimeos+Yt0Tbc9JM3+8A2qSAZb8sz7DSHlC/EefeK0mX/LGBuBy0OuRl9MOhQHxfEPoNDSakeCOGOM0wjtrQ/YPfxEZFbOuOrC6f7x8QE2y+IGnOgUFKwiussBAKKJgcxaF1oTdtaOgmqi4fux2G00FiBrMTMdcjHgek96LT2WcJrcefSzp5nX/eyfeMFU563t/J9wFhXZRlg6fGQTF4AEoguqp6AnFHddvtPJRKkzXMTZagD9cQ7H2Q6pt42fxp1xEEJap7NeBz+gVmEYE+pl1j1F2sffHxt3MJE0wEXf20AAxB7+QkxKvxcsI19yQAw5gviRQHFGlzj/MYn2vU/6hWsks1mVPUO0JuhtNF28tt3XqlL3gi+hf54mk6ztRg0jCibSV5FxNQNTvGNbgRtvD2zxkiVfIqIOsPPpJayf4RLk3tavRaKU9tPZQlvy0v8zFwuR6HVCGQM4oJU6Wrfh2t+VOeT/wLD+LaE+TBS/Yol6E+DeUWKUkFlck6CdcZQmmt2j4iFG3VQhTSeSQxSwbFGvfzG1ZfT27K3QZTO X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR08MB6256 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DB5EUR03FT034.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 777a0519-28b7-4157-0a86-08d89df2ca00 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AbiUtcOKIc095g1JgXPtMW1e6yeEQjmwliT23tJoxpTiXHhoU2lMgSJzQIbcPzga+oIb+8aJon1PNXE3KJtmAL1dTyLaNaS8McusbzIAcXjV2Mugnhy1Liqj9IWz46e0Np+7Nbrv23nwQ8uVcZ46XXZK1LMRF3wB7D8AtUEG1nH6FN+c4XEkuQ2ahNfC6NTtOrm+Zr6aXKI5lPiy7ZLmoKUADPJd2G9O66QKaBV+m0u/cG2sgliLbxjchy/Byr6OK2ABAe564Ks6B5LTPJweKUpThxM9AOB6pVvYZns3HqYmCxrUJy31GPwdp3ga4VtxSvVHoM0ab5gVvUIK4gwONq+qdFhVCTLrVGP465UX740mWGEIFVw1GvWJYi3SrXvS6axQg7NQwsY22oLCaJHKyFuSBO61UbYNF6zy2/GC8yKGpGukwQ8C4SYzybR1INPW 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:(4636009)(376002)(346002)(396003)(39860400002)(136003)(46966005)(82740400003)(82310400003)(8676002)(70206006)(70586007)(44832011)(2616005)(8936002)(6512007)(336012)(83380400001)(6916009)(16526019)(26005)(6506007)(956004)(47076004)(86362001)(478600001)(186003)(5660300002)(36756003)(81166007)(6666004)(356005)(316002)(69590400008)(6486002)(2906002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Dec 2020 16:35:53.4690 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 502f5206-9958-4484-c3fc-08d89df2d3d8 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: DB5EUR03FT034.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB1908 X-Spam-Status: No, score=-14.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@sourceware.org Sender: "Libc-alpha" _dl_map_object_from_fd has complex error handling with cleanups. It was managed by a separate function to avoid code bloat at every failure case, but since the code was changed to use gotos there is no longer such code bloat from inlining. Maintaining a separate error handling function is harder as it needs to access local state which has to be passed down. And the same lose function was used in open_verify which is error prone. The goto labels are changed since there is no longer a call. The new code generates slightly smaller binary. Reviewed-by: Adhemerval Zanella --- elf/dl-load.c | 87 +++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index e9afad544a..10ccca20d0 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -838,30 +838,6 @@ _dl_init_paths (const char *llp, const char *source, } -static void -__attribute__ ((noreturn, noinline)) -lose (int code, int fd, const char *name, char *realname, struct link_map *l, - const char *msg, struct r_debug *r, Lmid_t nsid) -{ - /* The file might already be closed. */ - if (fd != -1) - (void) __close_nocancel (fd); - if (l != NULL && l->l_origin != (char *) -1l) - free ((char *) l->l_origin); - free (l); - free (realname); - - if (r != NULL) - { - r->r_state = RT_CONSISTENT; - _dl_debug_state (); - LIBC_PROBE (map_failed, 2, nsid, r); - } - - _dl_signal_error (code, name, NULL, msg); -} - - /* Process PT_GNU_PROPERTY program header PH in module L after PT_LOAD segments are mapped. Only one NT_GNU_PROPERTY_TYPE_0 note is handled which contains processor specific properties. @@ -973,11 +949,25 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (__glibc_unlikely (!_dl_get_file_id (fd, &id))) { errstring = N_("cannot stat shared object"); - call_lose_errno: + lose_errno: errval = errno; - call_lose: - lose (errval, fd, name, realname, l, errstring, - make_consistent ? r : NULL, nsid); + lose: + /* The file might already be closed. */ + if (fd != -1) + (void) __close_nocancel (fd); + if (l != NULL && l->l_origin != (char *) -1l) + free ((char *) l->l_origin); + free (l); + free (realname); + + if (make_consistent && r != NULL) + { + r->r_state = RT_CONSISTENT; + _dl_debug_state (); + LIBC_PROBE (map_failed, 2, nsid, r); + } + + _dl_signal_error (errval, name, NULL, errstring); } /* Look again to see if the real name matched another already loaded. */ @@ -1084,7 +1074,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, fail_new: #endif errstring = N_("cannot create shared object descriptor"); - goto call_lose_errno; + goto lose_errno; } /* Extract the remaining details we need from the ELF header @@ -1103,7 +1093,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, header->e_phoff) != maplength) { errstring = N_("cannot read file data"); - goto call_lose_errno; + goto lose_errno; } } @@ -1149,14 +1139,14 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0)) { errstring = N_("ELF load command alignment not page-aligned"); - goto call_lose; + goto lose; } if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)) != 0)) { errstring = N_("ELF load command address/offset not properly aligned"); - goto call_lose; + goto lose; } struct loadcmd *c = &loadcmds[nloadcmds++]; @@ -1235,7 +1225,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, another error below. But we don't want to go through the calculations below using NLOADCMDS - 1. */ errstring = N_("object file has no loadable segments"); - goto call_lose; + goto lose; } /* dlopen of an executable is not valid because it is not possible @@ -1248,7 +1238,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* This object is loaded at a fixed address. This must never happen for objects loaded with dlopen. */ errstring = N_("cannot dynamically load executable"); - goto call_lose; + goto lose; } /* Length of the sections to be loaded. */ @@ -1261,7 +1251,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds, maplength, has_holes, loader); if (__glibc_unlikely (errstring != NULL)) - goto call_lose; + goto lose; /* Process program headers again after load segments are mapped in case processing requires accessing those segments. Scan program @@ -1284,7 +1274,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (__glibc_unlikely (type == ET_DYN)) { errstring = N_("object file has no dynamic section"); - goto call_lose; + goto lose; } } else @@ -1313,7 +1303,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, = N_("cannot dynamically load position-independent executable"); else errstring = N_("shared object cannot be dlopen()ed"); - goto call_lose; + goto lose; } if (l->l_phdr == NULL) @@ -1326,7 +1316,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (newp == NULL) { errstring = N_("cannot allocate memory for program header"); - goto call_lose_errno; + goto lose_errno; } l->l_phdr = memcpy (newp, phdr, @@ -1359,7 +1349,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (__mprotect ((void *) p, s, PROT_READ|PROT_WRITE) < 0) { errstring = N_("cannot change memory protections"); - goto call_lose_errno; + goto lose_errno; } __stack_prot |= PROT_READ|PROT_WRITE|PROT_EXEC; __mprotect ((void *) p, s, PROT_READ); @@ -1380,7 +1370,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, { errstring = N_("\ cannot enable executable stack as shared object requires"); - goto call_lose; + goto lose; } } @@ -1407,7 +1397,7 @@ cannot enable executable stack as shared object requires"); if (__glibc_unlikely (__close_nocancel (fd) != 0)) { errstring = N_("cannot close file descriptor"); - goto call_lose_errno; + goto lose_errno; } /* Signal that we closed the file. */ fd = -1; @@ -1686,14 +1676,15 @@ open_verify (const char *name, int fd, errval = errno; errstring = (errval == 0 ? N_("file too short") : N_("cannot read file data")); - call_lose: + lose: if (free_name) { char *realname = (char *) name; name = strdupa (realname); free (realname); } - lose (errval, fd, name, NULL, NULL, errstring, NULL, 0); + (void) __close_nocancel (fd); + _dl_signal_error (errval, name, NULL, errstring); } /* See whether the ELF header is what we expect. */ @@ -1753,13 +1744,13 @@ open_verify (const char *name, int fd, /* Otherwise we don't know what went wrong. */ errstring = N_("internal error"); - goto call_lose; + goto lose; } if (__glibc_unlikely (ehdr->e_version != EV_CURRENT)) { errstring = N_("ELF file version does not match current one"); - goto call_lose; + goto lose; } if (! __glibc_likely (elf_machine_matches_host (ehdr))) goto close_and_out; @@ -1767,12 +1758,12 @@ open_verify (const char *name, int fd, && ehdr->e_type != ET_EXEC)) { errstring = N_("only ET_DYN and ET_EXEC can be loaded"); - goto call_lose; + goto lose; } else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr)))) { errstring = N_("ELF file's phentsize not the expected size"); - goto call_lose; + goto lose; } maplength = ehdr->e_phnum * sizeof (ElfW(Phdr)); @@ -1787,7 +1778,7 @@ open_verify (const char *name, int fd, read_error: errval = errno; errstring = N_("cannot read file data"); - goto call_lose; + goto lose; } } From patchwork Fri Dec 11 16:35:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 41365 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com 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 7C27B3973018; Fri, 11 Dec 2020 16:36:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C27B3973018 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1607704583; bh=IuhYGS2skVfw39EjiQ/6PspjrX3VyiETK0IJ5BKtujw=; 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=X1yJvGpB6jMYC9sP8Ek6X3KrYzsW0wT2YEY30KbEFJm6M3IKUEg0xt6+9VpS5EBw1 sj+HSPO2nAO2sF2uxgSQ4jxR5809n5xwr1wVYWL6emZl15yJxA3fpQomKgZuEUMZ0X RBH9taz8ts1LRWliZL+27ddLP3Q1yCGS4xjdfkG8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-eopbgr140047.outbound.protection.outlook.com [40.107.14.47]) by sourceware.org (Postfix) with ESMTPS id EE8883972C3F for ; Fri, 11 Dec 2020 16:36:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EE8883972C3F Received: from AM0PR03CA0105.eurprd03.prod.outlook.com (2603:10a6:208:69::46) by AM5PR0801MB2018.eurprd08.prod.outlook.com (2603:10a6:203:43::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.18; Fri, 11 Dec 2020 16:36:16 +0000 Received: from AM5EUR03FT043.eop-EUR03.prod.protection.outlook.com (2603:10a6:208:69:cafe::56) by AM0PR03CA0105.outlook.office365.com (2603:10a6:208:69::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Fri, 11 Dec 2020 16:36:16 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;sourceware.org; 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 AM5EUR03FT043.mail.protection.outlook.com (10.152.17.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Fri, 11 Dec 2020 16:36:15 +0000 Received: ("Tessian outbound 8b6e0bb22f1c:v71"); Fri, 11 Dec 2020 16:36:15 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 43caea53c17f975d X-CR-MTA-TID: 64aa7808 Received: from a7b767caacef.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 4D760BF9-B065-4452-A010-D52BDC7CB12B.1; Fri, 11 Dec 2020 16:36:00 +0000 Received: from FRA01-MR2-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id a7b767caacef.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 11 Dec 2020 16:36:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iAwzMUv6tTnAgTFHKfeiwaiYy8j6YMChhLLXfJDmSkuTJjXfcyJi4YaoLOaXDDPlvsnrZr4OFX0nvA0tBAzUbKzfXstvX5AYGTLMfmPwkR118JRfPf8iOhVXQw3cQluifpOA3xr/+g0lYT1kkgIeXITNUKxdaiYzxQf5gWzHp/ZBs+U4kx9+/r+LUuPBpIYcC6a8ouUTLp+I8hM+9Z8GQuoWjilZ/IVBp859m6Gtj7q2zA5ugbeMEJGJF0p5iEXYZNE2zeSxQ5LLgpfQHIWphXeW64fJoHm6SvDrRddOzVPGJUvqrmm4gn7oe3I6V1ABMa7OrsqE26aarG7hMfDDHg== 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-SenderADCheck; bh=IuhYGS2skVfw39EjiQ/6PspjrX3VyiETK0IJ5BKtujw=; b=aB2aSFNpUVeCUPCJ2nKh4AnpS+8dy9krxDGGIp8hsOlzfLWXwaeeJ+dccwINhzs4bfrV2v+xjFXK9SsEJ/yt6P1RUZnEFxUCx2cQBu87bfhLhK0Hdny9LgcekAoUN/xlZT9kBYg7qLYuwEa+n2M9o7tEHF9hodJRsJMG9boFVZfrJmDW7l7fKo17yMyDYhdTKY4FNO0oXgSEBEfPPMlq7EZCgHRDacOO8T42WVw/ojYJ/6EyMRS1P2VUyycLn51YCDHraRUtnmuiRWM1JNliKG+Y9xnKDmLPQZwsiXC/rPWvSJXiGYEHsY7v/yre8WuZbjwkxAFZZXr4WB7UGJuUzQ== 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: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) by PR2PR08MB4649.eurprd08.prod.outlook.com (2603:10a6:101:1f::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.21; Fri, 11 Dec 2020 16:35:58 +0000 Received: from PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2]) by PR3PR08MB5564.eurprd08.prod.outlook.com ([fe80::ac13:db5:ef4:2dd2%4]) with mapi id 15.20.3654.013; Fri, 11 Dec 2020 16:35:58 +0000 To: libc-alpha@sourceware.org Subject: [PATCH 2/2] elf: Fix failure handling in _dl_map_object_from_fd Date: Fri, 11 Dec 2020 16:35:47 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.54] X-ClientProxiedBy: SN6PR05CA0022.namprd05.prod.outlook.com (2603:10b6:805:de::35) To PR3PR08MB5564.eurprd08.prod.outlook.com (2603:10a6:102:87::18) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.54) by SN6PR05CA0022.namprd05.prod.outlook.com (2603:10b6:805:de::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3676.9 via Frontend Transport; Fri, 11 Dec 2020 16:35:57 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 3da80762-86fd-4856-939d-08d89df2e147 X-MS-TrafficTypeDiagnostic: PR2PR08MB4649:|AM5PR0801MB2018: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:8882;OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: mqeaPgRig6h4FO98KcDPtARgd6KbZY8stclBpxnfi9E6CV2cZWFAzUGXqdhnkJn1OpXmrW6N27y7YhvjJ/WPM1F1jA9zKeZ0R4slhTA8jsTI9evM95oNSWUY24T/9q1od1qmtZslwJeqaUY7gSDLi6KK8Wo0OxFJ672A7IXtEe6l3BdzQvQm0E/bjc0gkOn7gp71KOfO8WgIRw9+fa227x5zbq6wg5q4wNgCRw5T8DvFgFhcC43CAUV/UxMC2VHWfZtXtPAP1LkS2XEfZfgrJOHcIsJpcA0Wdoh4aOWo4kqy/9K/1PhHJ/joP8hhwMlpGnFa4IHuizFGVJB47+vLstUNjBE0Xo3agBVcmUPS1gL/pVzbAqoNHmc5nkbY6bxV X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PR3PR08MB5564.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(366004)(136003)(396003)(346002)(39860400002)(66946007)(6486002)(26005)(86362001)(83380400001)(52116002)(66476007)(6916009)(69590400008)(2616005)(186003)(6512007)(5660300002)(16526019)(66556008)(6666004)(316002)(8936002)(2906002)(8676002)(6506007)(36756003)(956004)(478600001)(44832011); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 3grhi418CYlGuX+nY5lJDJi0/LVq6KgNF5tg+mvchfLee06dNtKNnj/2U/tV9L+WwNY5Q8+V5VBFYef4O+9dDcKzSAFv+rrTy5EnKDJQA2bfL2SFu2Fljw0yA6+oihWVJ8zc+Y1LBdyP0GwHzUdASuwxUFBFbs6+RBboutgfIuycKc57iZFV3lxp9RmG55O/kpl0x8YrwM5KviJ8PUcUsGY3UVexjieU8c/jBrLFVgeIyXaKSZi/oaAdAeIVkzcsJE8TzoWgnekMpus24yQ9IOxqxQWxJbYOcjaSZ6ysFPRKd9XOU6+W9zSKiSmctmkIsl3cY300eOoOn6x/umVyMKbsmBJn0BQkW3SxrxFgIZ6bS0AGcUqATyiuc0OKIsjCsyCj5Id++tuVOjX3YzenK1GkI8vN2E7yHBqcK1Rha7/wnxHgSTLYIfWEgNBbfgVzTJda5OksUN+DeOjpVPJjHGLHELAuooVrGOBSU2hg/iJQupwFxaQGEHjrEshb9gBzMAvJpguVtWUCEDrUpjYEfcEETt7AFpPiAeNyUxKasdZ5NLLCiuzCi8LEMfRtzvRhK5NSUetNyZw9G5aSlB1yfLLA+KyIhj8apKQcmDoolw13TH+fN0+NgBChgH2GPehT7k/jn6J5VFILNQYhsKj5/umkQN92uUGCf9Xc60/fJmD2JXrJGe0bJnYsyP6GVwf1Xo3YQThAkdzUlYpdZgNyq6377nzTint6H1pIgtxH/ALxgzEU4bR6pgj4dbAYTz57FL0g00lAVV+E2EpYxYwubZ9N1rf9N6XER5jmEqkrLOmm0DaI40DPY3HcGjEZNaU0gohjSA2IRJ9bF4x41MNpPOVXqX+9awcBEGePte4iDcFTOGPZuYDcmhFtPX88ImXvtOUWwK17mxPPRb0EBYd4wQImOLO/LT1UJvP4PcFBboAy5QV2WLzlF4Cv1ml+egdzAlN4KxwRVPJheXJxHVt/hV8nPizxrazOS8PG/7FmAcQFgkjSSRkpm9GXwG09eoDI X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR2PR08MB4649 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT043.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 065fea88-d1cf-47b4-9fd6-08d89df2d711 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: L/eFrhAZadvs8lTQLhFWB489OqJrv+wealkO5or5PBqZi5XRVbQ6rB/5VYe/xVCv3TFvP4GVLVc+ybzZokQAF7dAoqiez/jkdCTepjRV3HqDZfGlfCKxY3A/vO06tjSC+8HONXzv3OIZu+a5zpxCrse06w5hx2uSuR4PArInOV+i/EoXCNIf9I9nVuzXGj+iTWffFFu5BD73iCYhuuwXQ5HpHM4FpEvFe1S8rB3eP5LVdsPci/R9wbhV2SxhPg6Nadtmxm6dN/UzHG/hckAxoiLHtcRZDdZMZ2o9AUwMUmmC4FWXtekh6+pxxsPkqwp1JZUCTAnI9U2xDewta4MjKY1tDPEaKCGZLrbMifP1Y0p9f8KgdB+v13q81EvjBVNmraGOeJyOJ4HY/0AVrKuBUz1zDRXhUTACQfJAh2EoBF39ISmRe0D7UelcknIQ78Qi 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:(4636009)(396003)(346002)(39860400002)(136003)(376002)(46966005)(44832011)(5660300002)(8936002)(70206006)(6916009)(356005)(956004)(86362001)(16526019)(83380400001)(2616005)(6506007)(70586007)(6666004)(2906002)(36756003)(47076004)(6512007)(69590400008)(8676002)(6486002)(81166007)(478600001)(336012)(186003)(82740400003)(316002)(26005)(82310400003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Dec 2020 16:36:15.9396 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3da80762-86fd-4856-939d-08d89df2e147 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: AM5EUR03FT043.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB2018 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@sourceware.org Sender: "Libc-alpha" The failure paths in _dl_map_object_from_fd did not clean every potentially allocated resource up. Handle l_phdr, l_libname and mapped segments in the common failure handling code. There are various bits that may not be cleaned properly on failure (e.g. executable stack, incomplete dl_map_segments) fixing those need further changes. Reviewed-by: Adhemerval Zanella --- elf/dl-load.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/elf/dl-load.c b/elf/dl-load.c index 10ccca20d0..a9a2a1b63f 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -955,8 +955,14 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, /* The file might already be closed. */ if (fd != -1) (void) __close_nocancel (fd); + if (l != NULL && l->l_map_start != 0) + _dl_unmap_segments (l); if (l != NULL && l->l_origin != (char *) -1l) free ((char *) l->l_origin); + if (l != NULL && !l->l_libname->dont_free) + free (l->l_libname); + if (l != NULL && l->l_phdr_allocated) + free ((void *) l->l_phdr); free (l); free (realname); @@ -1251,7 +1257,11 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, errstring = _dl_map_segments (l, fd, header, type, loadcmds, nloadcmds, maplength, has_holes, loader); if (__glibc_unlikely (errstring != NULL)) - goto lose; + { + /* Mappings can be in an inconsistent state: avoid unmap. */ + l->l_map_start = l->l_map_end = 0; + goto lose; + } /* Process program headers again after load segments are mapped in case processing requires accessing those segments. Scan program @@ -1289,15 +1299,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, || (__glibc_unlikely (l->l_flags_1 & DF_1_PIE) && __glibc_unlikely ((mode & __RTLD_OPENEXEC) == 0))) { - /* We are not supposed to load this object. Free all resources. */ - _dl_unmap_segments (l); - - if (!l->l_libname->dont_free) - free (l->l_libname); - - if (l->l_phdr_allocated) - free ((void *) l->l_phdr); - if (l->l_flags_1 & DF_1_PIE) errstring = N_("cannot dynamically load position-independent executable"); @@ -1402,6 +1403,10 @@ cannot enable executable stack as shared object requires"); /* Signal that we closed the file. */ fd = -1; + /* Failures before this point are handled locally via lose. + There are no more failures in this function until return, + to change that the cleanup handling needs to be updated. */ + /* If this is ET_EXEC, we should have loaded it as lt_executable. */ assert (type != ET_EXEC || l->l_type == lt_executable);