From patchwork Fri Sep 29 03:24:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13403634 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9772DE743DB for ; Fri, 29 Sep 2023 03:24:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 464EA8D00D8; Thu, 28 Sep 2023 23:24:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3ED968D0002; Thu, 28 Sep 2023 23:24:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2673A8D00D8; Thu, 28 Sep 2023 23:24:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 177CF8D0002 for ; Thu, 28 Sep 2023 23:24:43 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DBE541CA4B6 for ; Fri, 29 Sep 2023 03:24:42 +0000 (UTC) X-FDA: 81288192804.23.FC2CE76 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf08.hostedemail.com (Postfix) with ESMTP id EFE26160004 for ; Fri, 29 Sep 2023 03:24:39 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CBRYFhRU; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf08.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.170 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695957880; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=95+jGh9HzPfjIjeeQbyhCMVgDdqV+t+zi0ml5yOYuB8=; b=CeVzXhjd1XYrLJKeM+uUf62V5D6WTGHR1ZPFCaK+TvELym7NGAEZZxzvDFvh6T5AHE+L14 C5ti7wVWgFBuyIyeB3BCabvdeBoaoLCRgEqr6AP04FZXHliBV+tUKbKYTswPsVt9BtgjNP JzVlY3izrZUFBOceSR4bMrsYJdTDNFQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CBRYFhRU; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf08.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.170 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695957880; a=rsa-sha256; cv=none; b=4BnsB1MdgnBD9pqfQTGpZSWMUzXpOagJEFyq7T2Q3fSdaZUcRSeNRxceE6tabz7ScnwesI C4OiCFhBQ5gbq9/jcc2ZpC26fg2S/jk9UA1Rt/ipBwz/Dt6MEs91OiGm1jZyVr7WeTyPk6 qVmRkvtWQq5d1WqYNZsf9c07sVI0O+M= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-68fdcc37827so161779b3a.0 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=kvack.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=CBRYFhRUOHiXqjAXnpymsWaxoXdPfXncahWlw8Ybkjp5lYDaRCSi0/xZ0Ng/xIIK1r wZvUuBqLphQDSNzYfciCMKazMl1ZDgUPlL/5KhU6kkQ7pHA65XET20FBygYMiMdtIuO6 /MJIv+qo/zXCeNLo+Qd3v17XOi4X8F4MLIGC4= 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=uIqXRZQWx5IK0xbJbYJCNDpwlIcSN/I29NCJK+sR62gykV4FjMvS2NghHInVGciogl kqG6V5Rw2Mjc15IAY89ndmdOi8BPkN3IbEJgmXCeBhdrH9kdLw9SJfjiOtMEEnq2OwP5 44B63P3OlluR22rFEWq4Kbzwh6l5Zm9vh1sEmlRhnmhxCYJJOPoWGpVmyK4EBoXs6jbp AOFSZpN7dOXo0o54aL5/WXdjz4gs67GXA49ciRY99II3+QsvMgSKdhcVi5zPQQsKROTI e8eUgCt252UwKT4oEqe6u70f/SDx4BGITXzJKK04hnrAbuymKSo4V9wPP6zMKRPu+jmF gRiQ== X-Gm-Message-State: AOJu0Yw8r18IPE/AlO5PW39ZlIGZ31rbu0fTjiS1POt0tZfAMqUIOCyD TXxnVNyZLNrQWTZCJL0MvqVCNQ== X-Google-Smtp-Source: AGHT+IENptQn7XIn+gHi7Xk/Rom3FrwjZlYNl2dbdfUf0uD5PWaNHfgd4GwUs3Rvuj8dtt6Ixw6ezw== 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_Wei?= =?utf-8?q?=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 X-Rspamd-Queue-Id: EFE26160004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8ms4x4jxu4cjxieyk1yo1mexmf98uoaf X-HE-Tag: 1695957879-488806 X-HE-Meta: U2FsdGVkX1+9Hyst2to69+g4wWG+/qtrcg5a/0xTNAk+I3dss+1ziLYQY9G53vnFdHMjEJoKQbu91yWGAnTigzE/G+rjK4lDXXJsNuXwZIxRK6FDcrVFL4fxVD0t9IsUeh1Ds2getL/3vyYErKUlbzEofApEvheWv8pd3QE48+35iQwjVeyqrZD4V7BzycMy3CTTLSLdnvHjnDrQ1o/f9iSz3RIRt8brXXL/yKBriyuXR5aAEuAU4ycQXhN+3q2dGORNTaPS1YK+umDLBUOPpm6hz9gnTrDU/roFKJmtnBk2MfKIk7banLNmWueu+vg31eY2W/agcEBSZBytoJTBozJtgA4rRsS4ty6hzJq/Au9r8kakaUAUk8k3NgdoYGhn4sSjSOHBAskKLdrGzak2pJcFZUta2szRFX0P+9llSJWK0Pp2BbLXn04p1VoctlE7Y7/QP1+A8jG5svBj1Phyvrwpi0pFlKsGKp06pgjV7haWUkyqaN9pTH4FeXESneV4s1v/9j3fDYncp1xguBR3wupgzf/niDlA1ShFPQLR9tJEzboQISNnFFILimJ0xE4Cu3bqM7Lk0R5cr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bte6DYtDDtuCBjS5OcelUHjolbyi7dgy4RMLRHbX4uvGwm+9jK39cZciPfJ+vqDwGy3VFuIj183lMKaDGURiVM7V X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: