Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3717466rdh; Thu, 28 Sep 2023 23:32:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3bwkISw4WgsTyvQj+uAyZpFFeJIqxnFqv0Eu+OGu3pMtrKr1MEzA/kIwQmsHS3KqfkB7y X-Received: by 2002:a17:90b:1e53:b0:276:6be8:8bfe with SMTP id pi19-20020a17090b1e5300b002766be88bfemr5506695pjb.23.1695969142116; Thu, 28 Sep 2023 23:32:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695969142; cv=none; d=google.com; s=arc-20160816; b=guq53w0rsVNaL72CdmVQve8YVF6z+vqBPI4Nc7j3ka+wOgyodIvXdFjnIezlaPTls/ VLFOVI2fluk1q0EK9hO13+d1T6vMk+0frP35w7edVvD5n4N2llx55RoL5oQvPLTvvGSh 91P2GkgU3zfT9899GFReqhkc6ttWnz9hU33Dox09q9R24YY/LJuYMNfXuj5sdVu5CdcI qmVRI6G4a5gq2zN7OcwZS108fCMfyH8g0VTqQMthzFn+17qodf6i5stIzjmsEAr0awZl JzkeiYZ97IUxDnvZ7em0D2Bld7caeJJG2sV24VLiaYg06l1x04U0Rd0uh0kqwt2/8fkP 2MzA== 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=95+jGh9HzPfjIjeeQbyhCMVgDdqV+t+zi0ml5yOYuB8=; fh=uM8fZIJrIH8hFNh2+0CTuXQ2rc4vyoNeB2hYGvjxI3M=; b=SBmXh138hx7i9agDOmqglLSSABGykWZtE+4K5opo8wFvWZP6EuicFvtLBbDIrHZMDb Sv8ymxTjxHWIO3j6syFGpyy9azL5LLakR2jgwkAhaBCDbGI91tfje425AfYRgIvwC54s CRG1c8Z4tSMnid8lQwq1kdPabKLuM9qH4b8T2dZBDeeYkuibVckb27q2MxzqCbcaIPgG mLMuPa2Uuk+psA0vqtZ7bvmU5Xi6uu6PTrOieEN5zUy2v1qQSo9/0l5TSrJcNukGWDgx cgKg7Inmasv72FNVDrzc/raYFEHUAZIHuYyLXxQ8VibS9sZFG5monqPQYPB0bWYhwDmA d7yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jxYfHi6U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id m15-20020a17090aab0f00b0026b043c8bbesi861553pjq.136.2023.09.28.23.32.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 23:32:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jxYfHi6U; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CA46880A2211; Thu, 28 Sep 2023 20:25:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232530AbjI2DYm (ORCPT + 99 others); Thu, 28 Sep 2023 23:24:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232321AbjI2DYl (ORCPT ); Thu, 28 Sep 2023 23:24:41 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D55D1AA for ; Thu, 28 Sep 2023 20:24:39 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-692d2e8c003so152778b3a.1 for ; Thu, 28 Sep 2023 20:24:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695957879; x=1696562679; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=95+jGh9HzPfjIjeeQbyhCMVgDdqV+t+zi0ml5yOYuB8=; b=jxYfHi6UrmlN+2RUW3BFPUPF6/N0gHYlZIWZzjNHw7WzqWZzyCSQIRcCf7rXysywY3 s1yOxdFMqC5M6da9Zpq4ZD/D53DypziHPfjATX/sfflu3nUOB5j8/C0VJBMaHXLMRUrV CLytRsuOuOhoJWKJqFJCuVH8yPGfPTe1n9hmw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695957879; x=1696562679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=95+jGh9HzPfjIjeeQbyhCMVgDdqV+t+zi0ml5yOYuB8=; b=fXNxYNXXC7ydaKD5S0tX+0IPGcSwKxp/i2UjcXgixYNgx8pidXvkZvaF4PJ4fkrOOm I4HbLrRY3zEDK6Vo1oCoouPT/VbnrAsqEpx4v8+blyMvAjO0sQBBJJ6eMLCyzYiN9zD5 V10DZbIkGuDSGDA8JfITge1wYXWGTTtPDJ/GMdQFbkuf0FI05ElmvCNxfHp5+oG3SnAz BVGC/Kljm1wPMJuz/RiuIObytDTSAfRvhGppHN7/7TgeCnpfuQ5QXPU4gNQ3zKcrZSmR Msu7rOlwqhQmV4oBXSgq7eiXKpwa5+gdeAwett3b3OwQlhFWkHt7XFa+xb0BUiCcqSYr iV5A== X-Gm-Message-State: AOJu0YxL/keAGbnEjoMaMCPpcGOQrEURuMlnDt35wMsf+AWIp+yL47h7 /wbfqxPftfcsDnY/0WWlqBkJoA== X-Received: by 2002:a05:6a20:72a5:b0:153:4ea6:d12e with SMTP id o37-20020a056a2072a500b001534ea6d12emr5304382pzk.17.1695957878823; Thu, 28 Sep 2023 20:24:38 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id b5-20020aa78705000000b0068ff267f094sm3481738pfo.158.2023.09.28.20.24.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 20:24:36 -0700 (PDT) From: Kees Cook To: Eric Biederman Cc: Kees Cook , Alexander Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Sebastian Ott , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Pedro Falcato , Andrew Morton , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v4 4/6] binfmt_elf: Use elf_load() for library Date: Thu, 28 Sep 2023 20:24:32 -0700 Message-Id: <20230929032435.2391507-4-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230929031716.it.155-kees@kernel.org> References: <20230929031716.it.155-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1627; i=keescook@chromium.org; h=from:subject; bh=HDRWJgiZi2B41NjfcoHun/GXVPB8PhMH+K8nRgD/QEY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBlFkNyd7onj53UAnvTrNuqjrOoqdzlhhnL6T0n4 waDipxrCuyJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZRZDcgAKCRCJcvTf3G3A JgqhEACQaxzh9TUbPcDbZo8niTtocmDdmljr+QUAo/kUcZsO7WwVGlruTUeAl3DsFH2PvI/7f8q 6Gzf2F516CRNsoFBvkxeJHFWa21p4CjiRwEYH+rdFGIQwe4ZXPrdSrcJhWVB35JNjqR8CK/aRK2 NAJeh7WuuaTnpcDhFmPiXWQuRt1noAczm74SRZU8IZ06ucQ49FNFgl7FPCGA0RLuWRQDEc4zXaQ BQktUi2h0of64YlxMdZfk+SQMGz2lOqO7a3F8Zy9PazbSnHb5k2NrkreHnNxtncdID9uE6SiKy+ JVJ8/BMveYU1W8I+asdhp8xe2CV19g1Qmlq4ZVCoeGxeV9OtFE+c8gjwaTlFdRjzRgfVqvqVmtp OYM3ExS5b6+tEeavoEpUw88foXpj9yoZLMwL8pVfsXc2FYIlPsrtaFfUyacQhttqevffosf/oUf bksChMr+i07cgy1L/yGOx/6wCfvU8HTwH+VMXDpSSowba7S7GbBmvxw9EtoVWjqJAGNdtQ6FvJZ 6SXLe7ag42PtcuMWe8C14GuF1yYIcvu/o/vpy24R3zfZUovoJAGPuns5yKBok4/GV2c31zPoB/p 5ZXXOtl28un+F7p/qF9lnnk6I8GbyO4YgCPqLeXph77PUUZfI4r38oOBt9/p+hKk0yQ+7nD6eH+ 8MpI/toIRLPKXkg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 28 Sep 2023 20:25:45 -0700 (PDT) While load_elf_library() is a libc5-ism, we can still replace most of its contents with elf_load() as well, further simplifying the code. Cc: Alexander Viro Cc: Christian Brauner Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org Suggested-by: Eric Biederman Signed-off-by: Kees Cook --- fs/binfmt_elf.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index db47cb802f89..f8b4747f87ed 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1351,30 +1351,15 @@ static int load_elf_library(struct file *file) eppnt++; /* Now use mmap to map the library into memory. */ - error = vm_mmap(file, - ELF_PAGESTART(eppnt->p_vaddr), - (eppnt->p_filesz + - ELF_PAGEOFFSET(eppnt->p_vaddr)), + error = elf_load(file, ELF_PAGESTART(eppnt->p_vaddr), + eppnt, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED_NOREPLACE | MAP_PRIVATE, - (eppnt->p_offset - - ELF_PAGEOFFSET(eppnt->p_vaddr))); - if (error != ELF_PAGESTART(eppnt->p_vaddr)) - goto out_free_ph; + 0); - elf_bss = eppnt->p_vaddr + eppnt->p_filesz; - if (padzero(elf_bss)) { - error = -EFAULT; + if (error != ELF_PAGESTART(eppnt->p_vaddr)) goto out_free_ph; - } - len = ELF_PAGEALIGN(eppnt->p_filesz + eppnt->p_vaddr); - bss = ELF_PAGEALIGN(eppnt->p_memsz + eppnt->p_vaddr); - if (bss > len) { - error = vm_brk(len, bss - len); - if (error) - goto out_free_ph; - } error = 0; out_free_ph: -- 2.34.1