From patchwork Mon Oct 31 12:07:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 1697163 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=112.213.38.117; helo=lists.ozlabs.org; envelope-from=linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=iVH3xGKf; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N1BlJ3d0vz23lV for ; Mon, 31 Oct 2022 23:09:00 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4N1BlJ2T4Fz3cJt for ; Mon, 31 Oct 2022 23:09:00 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=iVH3xGKf; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::633; helo=mail-pl1-x633.google.com; envelope-from=npiggin@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=iVH3xGKf; dkim-atps=neutral Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4N1Bjz4BCjz3bnZ for ; Mon, 31 Oct 2022 23:07:51 +1100 (AEDT) Received: by mail-pl1-x633.google.com with SMTP id g24so10550408plq.3 for ; Mon, 31 Oct 2022 05:07:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=N8KXzElrk7eZcMvhOMGLSocH1L3elk+opngelcioVZk=; b=iVH3xGKfWj//tRC8mM1EDHEmdHABtRA8Pk7XmKUNly4hgpoPYEh/S/T7cSVvmsVZ9V EZCIICLmWJQwpFAUZ4Hh1SULQNKji2cPYa3jzPzxndTJivwgpcRuH4zA8ovmgpGzZc9b Q4dSuejOClokJ1UkY6ntHGI29r4injlv6Jam6f0Lc5kgkMQRK3gG/Z1phb5GSU16uha7 gpTXWLzMDB14ykOoPDZyiiYpnbkXnZDls+w8cewlMBjcK9c8Y5IhrvwDZl7wahO8PrUA GSLu7V+XPYwbxchdEm/1ZJf08sZwJKetrWbh9h4jrZdoGjdmv2Ez4HuQXWT1VYA55ISF GzKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=N8KXzElrk7eZcMvhOMGLSocH1L3elk+opngelcioVZk=; b=26B0/4NbqXBSKOnf4GXF8yqsVxB3vew6cJLCd/2aOSCzfi6LzIcq79VIRV9jc6ly3X t8zjNO1WEK9gO1IajI5X4eu2RBrYz7Evb6tNibSQjjQo//n9BgIZPNgZ7Q+XT4Ojnvpx ZojVEkNjABZtVUHlQbUnyavFJEKn3UU8s+OWPMdwd/PhEIzV4TkGMkxvSsUyhnUq6FPA 3ce4xhmOgU/24ay8UPNwKB00m6UZEvrdca2wZaqTE9MCdtfSgVRNFUaqdjnc+vinTqga teEIQXXAcbDSi35RSS3dm3A11ck2D59iDfIx80DpkXtN35LP4jer9/N/JpMYvV231FvI Y5lw== X-Gm-Message-State: ACrzQf1etjWHwK550/FichPn4VyvmJ9Cla+ec81Cn6Ryo5p1Tz1fh282 51gQ7E7LWHafQzQHyfKBoPJgDkR9kh8= X-Google-Smtp-Source: AMsMyM6MFVIN9ZCxpO8+Mlfo/DtTtXcUUJD5nQ8a+6f0AqXpe5QBHKUoFQkKe6J3rVoTNuJG6AvzXg== X-Received: by 2002:a17:903:1c2:b0:187:feb:1f31 with SMTP id e2-20020a17090301c200b001870feb1f31mr10217779plh.92.1667218069206; Mon, 31 Oct 2022 05:07:49 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (193-116-106-251.tpgi.com.au. [193.116.106.251]) by smtp.gmail.com with ESMTPSA id p17-20020a170902e75100b0017e64da44c5sm1595134plf.203.2022.10.31.05.07.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 05:07:48 -0700 (PDT) From: Nicholas Piggin To: linux-modules@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v5 1/2] module: add module_elf_check_arch for module-specific checks Date: Mon, 31 Oct 2022 22:07:32 +1000 Message-Id: <20221031120733.3956781-2-npiggin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221031120733.3956781-1-npiggin@gmail.com> References: <20221031120733.3956781-1-npiggin@gmail.com> MIME-Version: 1.0 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Luis Chamberlain , linux-kernel@vger.kernel.org, Nicholas Piggin , Jessica Yu Errors-To: linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" The elf_check_arch() function is also used to test compatibility of usermode binaries. Kernel modules may have more specific requirements, for example powerpc would like to test for ABI version compatibility. Add a weak module_elf_check_arch() that defaults to true, and call it from elf_validity_check(). Cc: Michael Ellerman Signed-off-by: Jessica Yu [np: added changelog, adjust name, rebase] Signed-off-by: Nicholas Piggin Acked-by: Luis Chamberlain --- include/linux/moduleloader.h | 3 +++ kernel/module/main.c | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h index 9e09d11ffe5b..7b4587a19189 100644 --- a/include/linux/moduleloader.h +++ b/include/linux/moduleloader.h @@ -13,6 +13,9 @@ * must be implemented by each architecture. */ +/* arch may override to do additional checking of ELF header architecture */ +bool module_elf_check_arch(Elf_Ehdr *hdr); + /* Adjust arch-specific sections. Return 0 on success. */ int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, diff --git a/kernel/module/main.c b/kernel/module/main.c index d02d39c7174e..7b3f6fb0d428 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1674,6 +1674,11 @@ static int elf_validity_check(struct load_info *info) info->hdr->e_machine); goto no_exec; } + if (!module_elf_check_arch(info->hdr)) { + pr_err("Invalid module architecture in ELF header: %u\n", + info->hdr->e_machine); + goto no_exec; + } if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) { pr_err("Invalid ELF section header size\n"); goto no_exec; @@ -2247,6 +2252,11 @@ static void flush_module_icache(const struct module *mod) (unsigned long)mod->core_layout.base + mod->core_layout.size); } +bool __weak module_elf_check_arch(Elf_Ehdr *hdr) +{ + return true; +} + int __weak module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, char *secstrings, From patchwork Mon Oct 31 12:07:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 1697164 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=VBJYqllw; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N1BmJ72FLz23lV for ; Mon, 31 Oct 2022 23:09:52 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4N1BmJ5sfMz3cNj for ; Mon, 31 Oct 2022 23:09:52 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=VBJYqllw; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::631; helo=mail-pl1-x631.google.com; envelope-from=npiggin@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=VBJYqllw; dkim-atps=neutral Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4N1Bk35Qscz3bqt for ; Mon, 31 Oct 2022 23:07:55 +1100 (AEDT) Received: by mail-pl1-x631.google.com with SMTP id y4so10560691plb.2 for ; Mon, 31 Oct 2022 05:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=1zed+8ilXn6MGIJcL2z90P1TobfWQ+njImejMWMmf58=; b=VBJYqllwLFKjjSog/J2zrGa1E0jj6lPzM1Ul9Z7cCsRz8OTNshgJ2ToIavollcnHLt 3Xh0awyDJZ+YLP1fO/40IlMN/ZwdoBVWWA7qOMRW2GjrY3h/PvN5a0KcCKfcMSzGIW10 2S+wm31G2aDkYuffcnTHjN3SnfuTN7VvJNmPpHJ93uK1NYPOeADA+cZefMhs73PR+sOo TSnoO7bwOPE3ThPzfvkY4EPsR3Exf5qS48DKXjV/V5IqgHufQmzgGB7T+Wed/ZIX8AXC AsUkncP/ob33JkaFN+r3IsiYeqp8Br6SNYftE8LGqsLFD8C83+O5bRsNxT0bUEcR/sbl aCkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=1zed+8ilXn6MGIJcL2z90P1TobfWQ+njImejMWMmf58=; b=x0PYG24ZW1Qy/EKa+2jx87msBihf3jJjxkhAlxoH29o4W6vAR7CuZJTu/O1OV69kka hKPI/ktMT5BPc+2cEib1QFJ6iM5K8T8XTPQi0CmYOeEcZdjnj9whAm6PAjGMI1MXeY2q 4OwQIfBXaZXn04brYW/MpC1Z1OELTaHJZSBBjNE6TUWouF5jZMlYYLUEw8CZdfy9prg2 mfUyJy2kCFWcct+QAMbl3q5rwqVaQ2iG3UEMYjtRupDUTi+T6U6U4G1C2FqL9PGlIFSt Y63zTM/MVZ6TwRaWT89lXfSrGrik6J6uPTcpGWEsyg/gqt0+ThQnPwI/6uyMtwazgHdS 0kGg== X-Gm-Message-State: ACrzQf1u1MQy+EaNv3U7+xU+s8NjB5kQ2nKszrIPtN6zEMXtiykg4FTs eKgJ0LpqK6huc4omsHbjxNonG88MKl8= X-Google-Smtp-Source: AMsMyM6XhgTq1aU0IovI+tOfx0sloE3/zK0Q6l/El5DFFIfDlPvfv+ImgWi15cABY0LrTsEWLwRPBg== X-Received: by 2002:a17:90a:fe5:b0:212:cae0:7482 with SMTP id 92-20020a17090a0fe500b00212cae07482mr14516891pjz.108.1667218073416; Mon, 31 Oct 2022 05:07:53 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (193-116-106-251.tpgi.com.au. [193.116.106.251]) by smtp.gmail.com with ESMTPSA id p17-20020a170902e75100b0017e64da44c5sm1595134plf.203.2022.10.31.05.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Oct 2022 05:07:52 -0700 (PDT) From: Nicholas Piggin To: linux-modules@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v5 2/2] powerpc/64: Add module check for ELF ABI version Date: Mon, 31 Oct 2022 22:07:33 +1000 Message-Id: <20221031120733.3956781-3-npiggin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20221031120733.3956781-1-npiggin@gmail.com> References: <20221031120733.3956781-1-npiggin@gmail.com> MIME-Version: 1.0 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Luis Chamberlain , linux-kernel@vger.kernel.org, Nicholas Piggin , Jessica Yu Errors-To: linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" Override the generic module ELF check to provide a check for the ELF ABI version. This becomes important if we allow big-endian ELF ABI V2 builds but it doesn't hurt to check now. Cc: Jessica Yu Signed-off-by: Michael Ellerman [np: split patch, added changelog, adjust to Jessica's proposal] Signed-off-by: Nicholas Piggin --- arch/powerpc/kernel/module.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index f6d6ae0a1692..d46bf9bfda26 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -19,6 +19,23 @@ static LIST_HEAD(module_bug_list); +#ifdef CONFIG_PPC64 +bool module_elf_check_arch(Elf_Ehdr *hdr) +{ + unsigned long abi_level = hdr->e_flags & 0x3; + + if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2)) { + if (abi_level != 2) + return false; + } else { + if (abi_level >= 2) + return false; + } + + return true; +} +#endif + static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, const char *name)