Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2013458pxb; Sat, 21 Nov 2020 06:03:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJwneA5ltLFHTTDQPwRSaZUPYhP+HaRpATLTVul/IPlH/Ab4Df+vqIchD/tQfFkJEFBl0HVV X-Received: by 2002:a17:906:1c87:: with SMTP id g7mr32267987ejh.37.1605967405080; Sat, 21 Nov 2020 06:03:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605967405; cv=none; d=google.com; s=arc-20160816; b=sXZJuE9R4+5VYlVrIuTQQ1QiivncWgiusNYwy1cjVizrsbSolTC6yYUuMB1vD+Jkju lCo6vSo+5K3C6LA0drQXZM8SZwiAHOBWCLUnzis01IuNsQo8NSzvAO4JXtOx4UESz01r 6bYwdhAHg94Hp3kZS0trzSu94yiP8xfYohb8HRL2yWPDYuM8afpMM51M18tJEgc7Ukfk XkxSuCaUpJ6HppJ/sAlQuvCJpHiImEUJ2crPJnHsDoPOputfjbzfPTxcAd1TpXfJjNns 45kHKvNzJDLdejE65zFazssb/2PXECJr/ZX0z/xeNyrDG/Dw6IN0Z7l92ujXmUMwNEBy /Y+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oQxhkMEdQnyYr8WsxFvRrEHJSPLBuk0HpQQMWna+wtU=; b=g3hOa9HyMUL4nI5+KyHdgnmU5//EBi5YnlixQZ9e6cobTojaYkGEI1/Xw/SdDAV7Xk 6S8kd//TGYebKD71vgwmz3USwoJg9jBLviqTNFzspD/PHZx1wkrWAraAsU8n1I5oJUSC jZDfdhPRp/OQu3aILUYB2zyHGsvTyC9tdvl5tnAnJ+syZnSE27Nprq77TC/YWErUjj9+ 6r1J4Oxcu0DSDbwwEwuPeWGDScBR5CD3fYOEAF3v1u0tfPigBrWWnZTXTgKRQEAZWqXc RvXKhYFP4HJHuc+UoQuzngOEGn2J6lACahFc6Pf/exrdk+ng7HkSiCpYg2rzgiAvWEw+ OI1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XYXgafoU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si2044893ejr.248.2020.11.21.06.02.59; Sat, 21 Nov 2020 06:03:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XYXgafoU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727930AbgKUN5z (ORCPT + 99 others); Sat, 21 Nov 2020 08:57:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727912AbgKUN5t (ORCPT ); Sat, 21 Nov 2020 08:57:49 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D088C061A4B; Sat, 21 Nov 2020 05:57:48 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id p19so10129841wmg.0; Sat, 21 Nov 2020 05:57:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oQxhkMEdQnyYr8WsxFvRrEHJSPLBuk0HpQQMWna+wtU=; b=XYXgafoUb29o52G8xh9L2ZFZ+KugeI8XofyMumqdgSFpurfLAV9cs05Ipy2VLHou8d BdAPAZMSIZk38Xn5U2WOI3u+CniV4QnPfPYRqPNa2A9usI21W9xjjF4OAk1IGYE1jS2b sDjXcZb7yJwSh+p/XvMFpK1eUum2gwOjVtfgaBglebricVWISyyss71YWYhjRzBGKq96 PDD0qfJxqLFhvpFbnjB7+/ASV8pg+AtK9E2cXZpK4LeVMjuS2dPNZJQi1telxtCy0DyC nRDBrxHudjMAlOiDbsrFw+ztKawvTAVjKusWXV8e64/BmReWdhh2tIUGHPqYy1nq+P6b Q7pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oQxhkMEdQnyYr8WsxFvRrEHJSPLBuk0HpQQMWna+wtU=; b=rsUsqcoEdL9oeQRfBqsE+tnv37k+5n1WjkD2C1qbpFycirW1fMDQEWRDJEJevUaLOa 2jc0O9hfcgkpjBDVlfbjTEHgTH2PEQzKj4kXJY1W+y4KUxUrQ2GRLhG2DE0V+Xb52Xmk hSoA+Q48lJuGwHm6cnrb0bj8UH5lK1Eanl1UtpgblU3q7YYhYKS7zcG6No7CJ0xBUf0n QqYdJRKS+pr/GkzOq2XEBo17UBgG6/FpML7O7ougPY7RTRQdtn5BYzaWGnS5Zb6IYJFw /DUQ67B0VY50tehk1xw0Dhp6cwpTBDAUul4TNyMJNgxBHkxocCH27SOaQMGw9QlV0ajz w3pg== X-Gm-Message-State: AOAM532k6SN64k9WjAQwn3cK7dQ/liIJoH1ieoIjTTknunl1YysLMhOK xOCMARGUYzqoXLPh2/cI7mk= X-Received: by 2002:a1c:bbc4:: with SMTP id l187mr15763634wmf.133.1605967067308; Sat, 21 Nov 2020 05:57:47 -0800 (PST) Received: from localhost.localdomain ([170.253.49.0]) by smtp.googlemail.com with ESMTPSA id 17sm41689951wma.3.2020.11.21.05.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Nov 2020 05:57:46 -0800 (PST) From: Alejandro Colomar To: Alexander Viro , linux-fsdevel@vger.kernel.org Cc: Alejandro Colomar , linux-kernel@vger.kernel.org Subject: [PATCH 4/4] fs/binfmt_elf.c: Cosmetic Date: Sat, 21 Nov 2020 14:57:36 +0100 Message-Id: <20201121135736.295705-5-alx.manpages@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201121135736.295705-1-alx.manpages@gmail.com> References: <20201121135736.295705-1-alx.manpages@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Non-trivial changes: Invert 'if's to simplify logic. Use 'goto' in conjunction with the above, when appropriate. Signed-off-by: Alejandro Colomar --- fs/binfmt_elf.c | 115 +++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index b5e1e0a0917a..dbd50b5bf238 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1079,65 +1079,68 @@ static int load_elf_binary(struct linux_binprm *bprm) */ if (elf_ex->e_type == ET_EXEC || load_addr_set) { elf_flags |= MAP_FIXED; - } else if (elf_ex->e_type == ET_DYN) { - /* - * This logic is run once for the first LOAD Program - * Header for ET_DYN binaries to calculate the - * randomization (load_bias) for all the LOAD - * Program Headers, and to calculate the entire - * size of the ELF mapping (total_size). (Note that - * load_addr_set is set to true later once the - * initial mapping is performed.) - * - * There are effectively two types of ET_DYN - * binaries: programs (i.e. PIE: ET_DYN with INTERP) - * and loaders (ET_DYN without INTERP, since they - * _are_ the ELF interpreter). The loaders must - * be loaded away from programs since the program - * may otherwise collide with the loader (especially - * for ET_EXEC which does not have a randomized - * position). For example to handle invocations of - * "./ld.so someprog" to test out a new version of - * the loader, the subsequent program that the - * loader loads must avoid the loader itself, so - * they cannot share the same load range. Sufficient - * room for the brk must be allocated with the - * loader as well, since brk must be available with - * the loader. - * - * Therefore, programs are loaded offset from - * ELF_ET_DYN_BASE and loaders are loaded into the - * independently randomized mmap region (0 load_bias - * without MAP_FIXED). - */ - if (interpreter) { - load_bias = ELF_ET_DYN_BASE; - if (current->flags & PF_RANDOMIZE) - load_bias += arch_mmap_rnd(); - alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum); - if (alignment) - load_bias &= ~(alignment - 1); - elf_flags |= MAP_FIXED; - } else - load_bias = 0; + goto proceed_normally; + } + if (elf_ex->e_type != ET_DYN) + goto proceed_normally; + /* + * This logic is run once for the first LOAD Program + * Header for ET_DYN binaries to calculate the + * randomization (load_bias) for all the LOAD + * Program Headers, and to calculate the entire + * size of the ELF mapping (total_size). (Note that + * load_addr_set is set to true later once the + * initial mapping is performed.) + * + * There are effectively two types of ET_DYN + * binaries: programs (i.e. PIE: ET_DYN with INTERP) + * and loaders (ET_DYN without INTERP, since they + * _are_ the ELF interpreter). The loaders must + * be loaded away from programs since the program + * may otherwise collide with the loader (especially + * for ET_EXEC which does not have a randomized + * position). For example to handle invocations of + * "./ld.so someprog" to test out a new version of + * the loader, the subsequent program that the + * loader loads must avoid the loader itself, so + * they cannot share the same load range. Sufficient + * room for the brk must be allocated with the + * loader as well, since brk must be available with + * the loader. + * + * Therefore, programs are loaded offset from + * ELF_ET_DYN_BASE and loaders are loaded into the + * independently randomized mmap region (0 load_bias + * without MAP_FIXED). + */ + if (interpreter) { + load_bias = ELF_ET_DYN_BASE; + if (current->flags & PF_RANDOMIZE) + load_bias += arch_mmap_rnd(); + alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum); + if (alignment) + load_bias &= ~(alignment - 1); + elf_flags |= MAP_FIXED; + } else { + load_bias = 0; + } - /* - * Since load_bias is used for all subsequent loading - * calculations, we must lower it by the first vaddr - * so that the remaining calculations based on the - * ELF vaddrs will be correctly offset. The result - * is then page aligned. - */ - load_bias = ELF_PAGESTART(load_bias - vaddr); + /* + * Since load_bias is used for all subsequent loading + * calculations, we must lower it by the first vaddr + * so that the remaining calculations based on the + * ELF vaddrs will be correctly offset. The result + * is then page aligned. + */ + load_bias = ELF_PAGESTART(load_bias - vaddr); - total_size = total_mapping_size(elf_phdata, - elf_ex->e_phnum); - if (!total_size) { - retval = -EINVAL; - goto out_free_dentry; - } + total_size = total_mapping_size(elf_phdata, + elf_ex->e_phnum); + if (!total_size) { + retval = -EINVAL; + goto out_free_dentry; } - +proceed_normally: /* FIXME: a better label name? */ error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags, total_size); if (BAD_ADDR(error)) { -- 2.28.0