From patchwork Fri Nov 11 05:57:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 18479 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp563204wru; Thu, 10 Nov 2022 21:59:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf5L/BueAx815xufeTNcryBNUFEusXy2slBE1UDMmfDVC76YBFNMiRd4IG1X9XF3HeX7AOFe X-Received: by 2002:a63:1d48:0:b0:46f:6017:de59 with SMTP id d8-20020a631d48000000b0046f6017de59mr373871pgm.160.1668146390952; Thu, 10 Nov 2022 21:59:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668146390; cv=none; d=google.com; s=arc-20160816; b=Ymv/0ZVcTWzyMK7kiFvYR8i3elCeOh3vNuEsodWVLuxRSeYc0iELzkHYGR3O9FeI8M zklnBcJgJKF67OHAQvRN3rz2RyHwDF0J2IVQ8kBVYm8DsJ5u9kjzTcU6HUiUrwdDlPnh ZP1+g32RWp0RvjZwpptufMHPnZHHl29ffzdIyMiaeKqqtTEbzqGDWVNlGJ4U5vtVvJU8 VhwpLbgW8EPJi/qzKaAJ8rTJxo1GkqYo7+SF0Ia626U/j7p2tbTl61P06EBVkeBFrAoM h/UjCJsMsbqkesQ3pB9i/GMFxz0nOtJZtQacf0dp3BA6K7qRm6wMxaYRruV1FjI2Bnsx tX3Q== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=KHGjxOmAPLTjSOKR1E4aRsoBMeGm6miKnYzYvwRoo/8=; b=TrwPvq4cm8u51QcQnz04zal/yJ4r5ofQjGEWnesXDQZBy+VT0zTQBfGzP06ZgGn0k8 o/B6JXxzZNy5F3Yq2KQbu2+ATOmYH+2LFNXoGmimvgDu9eZz11FWh1qOEuxmQVgw9P/e wtlxX/WLbAeDoYDjN8tobTkRNPR3iYJ9eOdtCgPCc+k9X/2BVu7YUOX6I1UXBv6RksC1 DglfwNsYtn25aWYHg6/hydExXi+LQdR9Muoqaq4CyYjO4GL12WXwa5CPUWXVAX4Kl9Fb KFzBYGXIK0+mDZJecyTw/XYVZy1e9qh1AwrboWSeYDRz2Olhpt/ZU76d3i4MLpO9J58t 43ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gohmZcSy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u6-20020a17090341c600b0018552c4f4c3si1851253ple.615.2022.11.10.21.59.37; Thu, 10 Nov 2022 21:59:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=gohmZcSy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231300AbiKKF6B (ORCPT + 99 others); Fri, 11 Nov 2022 00:58:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbiKKF56 (ORCPT ); Fri, 11 Nov 2022 00:57:58 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAC646339 for ; Thu, 10 Nov 2022 21:57:57 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id c15-20020a17090a1d0f00b0021365864446so3848120pjd.4 for ; Thu, 10 Nov 2022 21:57:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=KHGjxOmAPLTjSOKR1E4aRsoBMeGm6miKnYzYvwRoo/8=; b=gohmZcSym6vfl404dEer4RoKFe3HbwM9Rjry0f1+FaJjNGXrOUU3IzieHCGv9fVlXU 20HPKuKbTfKr27+1pOgyQagqOwshsJdmX4++YUlZIJXEP0+OKkA7uvi2RIXbNPR061cE wLFe9pHX6mFxXca6OZ0XiYwMbnGgeQ3SH20Mw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=KHGjxOmAPLTjSOKR1E4aRsoBMeGm6miKnYzYvwRoo/8=; b=k2LfWIkgZUNiBUzsPmuGaA0O9MgQvqvSxFrv11R2sC66GNneRpdT7OjdmZgQ+7vSeU ytoczFtDD8GH9RXhZf4uudZqIJIUM2aTswnTxdvs6qPI64VT9SEL2U6kU4gwzs4nbZEB hFMnepA+kW1wUHNEafy7gGOdVVZp7ku31isnsaJbEnYp1CwbU1FvUa1OWA3eG4a1fb0n ijiio9U7Xx62+7ACd3ulfbiFCB6ktP3qEB+HEZ6WFWVEiUkU3qi3ZKq1y4cleMeTEpZ4 YyBFjt7NAvtZOeCbCGWLQEeqgPgw0ppfjVzxjlRWt8BBOY2+/CUJ8g8DJ7LM3hPGV+n/ h3Dw== X-Gm-Message-State: ANoB5pkYNrklfMv8Rmnyin0N7XgqB3EQIrJ7okSaN7LTWtssuHOWfECG n/5V9v5w1o7CSQ41nPDfGxWSCA== X-Received: by 2002:a17:902:efd1:b0:17b:4ace:b67f with SMTP id ja17-20020a170902efd100b0017b4aceb67fmr1229537plb.12.1668146277473; Thu, 10 Nov 2022 21:57:57 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id y197-20020a62cece000000b0056c47a5c34dsm662595pfg.122.2022.11.10.21.57.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 21:57:57 -0800 (PST) From: Kees Cook To: Pedro Falcato Cc: Kees Cook , Rich Felker , Fangrui Song , Alexander Viro , Eric Biederman , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] binfmt_elf: Allow .bss in any interp PT_LOAD Date: Thu, 10 Nov 2022 21:57:54 -0800 Message-Id: <20221111055747.never.202-kees@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1993; h=from:subject:message-id; bh=IFv7zG9ua/TUw6RPzUQWoXA14J5yfz12EiQFTB5lii0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjbeRibkBnJb+JHCktBamRZlj///oiFFeK7VpOpPsB 44aQDc2JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY23kYgAKCRCJcvTf3G3AJnJvD/ wMpSquxigcVJfg5ewqR3cpfP5urArMS1hWUeQchcdyXTMfEUFgqyt9Ty+7OE6vdD/z3R3uiC3lpphP FBxBvEkLF5ZynousHcyUauCkIiwZT1PLigA2z5DGCbu2tRUOeUZEI4uaYvMxWwJf+LoEIgjeWcXlHH QfBmP/w18M2HZvcH5ma39HbdZQPEU9Nn1sa4/WuZLf6xs+a6yVjJL2JpWk1Z6Md0kGbSu/n7KQkvRM OtJvFB3QrDrI8Mhm/UgZ/44bBI8kpStf/wXcD2PCGfSie6D+bPUvNlMk0t8eVQ0tq22d3po13Shtph N8IJDsgmVLbS0jbJLbWS5DQN5RlRrvmd+ss6JeZeCxMQVyJsUbft/Yydnvd6uxj/WLMK/Y1SsMrBgR vvBEdrAbJ1uO6PTohfPWyTOBG4P0nwz5/GodjBItoS6ceZjbf5tr82KhuQ8skBW+uztDUbi98znHu7 494BhsMzVXPETrqKnMIshOd0fSUYuawrnNvCqKGN0O4MYRiv06uCFYyyK45PqZOsFT2NA7U1hC8FF+ 11Hn4IoGq6LaAsuVbgTfBE7LR0lxPnBLwXLrsf3woKx8wRL7SCI7XKcYsaHeu0JFg/ukPkU1tMpiII tv6vNwpD91PO6uTpbXuWBE8xgYj3IBtoPba44NCzuwf246ajS31oQK2S+zVA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749178269808236412?= X-GMAIL-MSGID: =?utf-8?q?1749178269808236412?= Traditionally, only the final PT_LOAD for load_elf_interp() supported having p_memsz > p_filesz. Recently, lld's construction of musl's libc.so on PowerPC64 started having two PT_LOAD program headers with p_memsz > p_filesz. As the least invasive change possible, check for p_memsz > p_filesz for each PT_LOAD in load_elf_interp. Reported-by: Rich Felker Link: https://maskray.me/blog/2022-11-05-lld-musl-powerpc64 Cc: Pedro Falcato Cc: Fangrui Song Cc: Alexander Viro Cc: Eric Biederman Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org Signed-off-by: Kees Cook --- Rich, Pedro, and Fangrui, are you able to test this change? I haven't constructed a trivial reproducer yet (though it would be nice to have a self-contained test-case). --- fs/binfmt_elf.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 528e2ac8931f..3f07945ff085 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -673,18 +673,19 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, last_bss = k; bss_prot = elf_prot; } + + /* + * Now fill out any zeroed region (e.g. .bss): first pad the + * last page from the file up to the page boundary, and zero + * it from elf_bss up to the end of the page. + */ + if (last_bss > elf_bss && padzero(elf_bss)) { + error = -EFAULT; + goto out; + } } } - /* - * Now fill out the bss section: first pad the last page from - * the file up to the page boundary, and zero it from elf_bss - * up to the end of the page. - */ - if (padzero(elf_bss)) { - error = -EFAULT; - goto out; - } /* * Next, align both the file and mem bss up to the page size, * since this is where elf_bss was just zeroed up to, and where