From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD3A11A3BCA for ; Wed, 25 Sep 2024 23:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307566; cv=none; b=W0KCYDiw0/rKL805qZUmsRDUfVRf/y6UzoOVyTSjzS18hRgMClnlX79dY/D9IFavP0tQVB39EAUHYqIG8StHFn3qmcmWbyRpXwzsj0SiDoJnCBgSe1elTlLLUeTjdgXMJNDxY9oCj8gw8tw6PI95HGN5UPBdB/XSnK/04+uR8fE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307566; c=relaxed/simple; bh=87UiLghCgImjI67qGqkZVlUAdkIatHILzWkWX7E7o4U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g8w25POeeMMHeMGZPIUN+Wb/7sszDNwuPhKXmCF+I7ILHv4/2etQHmhw7OLcMOVvopfU6wsGQKlbPYSd5TC4HVFVNSET8YPE+o3UqcnDV9eM2yW1A2xBma+g0981ArJadBHGb43icbroay9elsHDdVwE4c69KOPjvBPquosfW0Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sSv2LwGg; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sSv2LwGg" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1d10fde51cso769570276.1 for ; Wed, 25 Sep 2024 16:39:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307563; x=1727912363; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LyxbaKZI7M7/7zXNo9r/7MnbWQPTGkdTyXhoBixsIfc=; b=sSv2LwGgnR8j+P3liOLGDS+Gd7WpWyGEpltfC2fOq9aHCnwHqKpAttF7d+ujur0gB8 uajSGw9bVI1blAghnlpJjKpoTU8usQLS1/X50XZcXRK2ld2nldpX0shMzOrzMTxdVLIJ o2OFB6nS8m8f30g49HHszNUg4NYHQ7YOfOnn0pR3DzgI2jh4Ex0z/zxC6aNfqKlvgEJB r/zziTXy3Y/X9G/QWZlE+NWZ6N6MwuehcCCRkvO/+09LtjzKlTZoKwgwXnRpuHJO48eN eg7WXZaSbwse0lfJZGVqJ0XJl+5015CPNqjSCTK27vwULOfZQfEkq5wL2ShUhcw7U2j6 u2MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307563; x=1727912363; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LyxbaKZI7M7/7zXNo9r/7MnbWQPTGkdTyXhoBixsIfc=; b=Zcl5IPGCLCOK5GFKoZ+yNZ2LWoTJKh0jb1ISsCzTvGXEBMs3OQkJVoonzSW+KdWVtl v9R6kFEH/cOaXzbGJvqcjv8NB3KzC2Ts855KnlzoWbxwOGbiKVGRqBgtOeHxQvSGwPdY 7F4h3w/POznMm3vTXGdbqhGL557lbCeNVAeUkgJacn5D4tcM3r9G0WUtlFzUAki3scQQ 0xUZ3Y0JDgm0Xz+jnw6jD9vophejLJ6AjdIGYXrjBdzAKzMB/D6+JGnH66Sf+n+6Czth kV9zoPFZr7pHKQ7XBU4/Mn3s6cOgTmlLolhbU3zeJom4OUJwgG1rWexceufm8jyvApFw FJjg== X-Forwarded-Encrypted: i=1; AJvYcCUSzWzOb7RpPDCJ2NVvdfuCZrZihQchhWOdmken+XF9hN0qEwKvJRAoZ+wCAy0ycF4eleu/TUblMROALoU=@vger.kernel.org X-Gm-Message-State: AOJu0YxGdUUFCZ49RlCIR5ApT5rTxCkqRhUSr94yiPZ+azcEb+t69sH2 vz+J3G2UBBR28WNCTg/dcFvickUiTP/1bT+aGH8NhTcz/XLFq1pxkRlj2/7+FLZehLx5TRoXgVL Zw+neeA== X-Google-Smtp-Source: AGHT+IEL4w0/gR8iE4pApg14NOFOUxZBPfsQ63YvAHEvZzW8W2ulmi1Lrysl9VB/6vA6caGgF7tNgGtZN5yi X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:2e88:b0:e22:5f73:1701 with SMTP id 3f1490d57ef6-e24d44c8e18mr28465276.0.1727307563386; Wed, 25 Sep 2024 16:39:23 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:16 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-2-mmaurer@google.com> Subject: [PATCH v5 01/16] module: Take const arg in validate_section_offset From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" `validate_section_offset` doesn't modify the info passed in. Make this clear by adjusting the type signature. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 49b9bca9de12..1a2dd52147ba 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1645,7 +1645,7 @@ bool __weak module_exit_section(const char *name) return strstarts(name, ".exit"); } =20 -static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) +static int validate_section_offset(const struct load_info *info, Elf_Shdr = *shdr) { #if defined(CONFIG_64BIT) unsigned long long secend; --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 064C71A4AB5 for ; Wed, 25 Sep 2024 23:39:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307568; cv=none; b=Kc3g0YPAIcplCbZzrKjiRxw0lTDJMEhrd1wDBsxgt5PrD+MfGcHuJmWU8ZuzUe0XtzkIgES9D19aALvxohP901QusZot3Wos/vr5kFF0czYV82h7zBBiHchrJ7dkfMeF5jlS8zEw5sv0MCMJnpb9HbCt9bBK7AOHQ+2W1xuq1gA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307568; c=relaxed/simple; bh=Mm95RZ0gEd/5ILA/sw+Yul4T3HW7v3bJsG9qmFVzqME=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N8t2wZ5PHBqWB8OgdqtOAULSFc46R5YvOLA4GAA2g3Tja+DbptFewp4arzowMxpGiJG4N6+t1GAMYtHUfT48oIVJmTUoPqZJlON4wkw8OsE2QA5JdqQ/fJZZe3sGFYTKGpHfnFMQqWGthnE7DW4E5itfqggwy89Q4BWzUuYb25g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tXfD7CXj; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tXfD7CXj" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d4bd76f5a8so8901007b3.0 for ; Wed, 25 Sep 2024 16:39:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307566; x=1727912366; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+9YZzoCvxvyDdM8B+em9dMKSDuRRuGPZhSev+lAnN9E=; b=tXfD7CXjz4wZB2/ddpedVBun7FwWYkTwT5jkNs4IJgfnwfXiy4LSVv4RX+OAYrv035 2TQoSEG8TmcTr8s9ZfHyXmLleupcr1QzoBnaf2A01lnHXRIRqv13B9fKWD+ikeogU+Yd 32b68d+IYGSxvqZWpCqR5I7z1urq5x1rRfok5oqB7EW+uWRgzNSIAHLCU+kC08OWluoQ VbdgHFtNj2Bx9fJ+0ji+bXWgXIjfnNT6cCrLP1QrpntizC1VuVjlla95u74qLIm3bqk4 lami4hLe5geRmL2CyPcg+1jcas+bA5m66ScVkkqaORCr69Akq0YPJlo30uy5SniGYJ3R BxLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307566; x=1727912366; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+9YZzoCvxvyDdM8B+em9dMKSDuRRuGPZhSev+lAnN9E=; b=npIs1uOLgpPMZLDBzamZUB4df6Jo4JCQK+LSkNVuOPeh0slZkdWeRtRJhQLOFVdMJs ZZXL5ZCkGgrt8AV4rNOX07TUpLOon4QojzvlBvQNNXRrWZsrmTlcv/ebc0MqTmd7LE8i h4dJSMeBsvdL3LvIee+NoKCOGyBokqMaGZzdQTpy668RGTDtzKtXlMlIvtddUJeVCg0g Zbn/P2TnzUclpeFrVmEqDiCY1mPBAt405aOkEEmFERlSCHTv0sOwMwa0rOiC8bXNUOJ7 FYxCl9OLIt4Vyv4auUQyt53inGF6C1BV5UoCqqZQ71eY5ZV+b3IvUB3HQub4AeGAJAIX ctuA== X-Forwarded-Encrypted: i=1; AJvYcCXFoYFklZ72mTehOybcSkXGf333fp2l2maAOdazY/SvroCFKqzPJ/pxpxq4IjDY2LNxYlK8FmYKk+qAdVI=@vger.kernel.org X-Gm-Message-State: AOJu0YzLr07737foGWpjGnjDxEWiF6B5VNQhKOWwBZtjk3XN91FsZNjk DDQ7JWfFOu+R2kSC+a8dH1XE5V/CGpu66l4bbqbySaYNduRdqwyG8GGv0u7E3K9zNh6PMcZ+gzk y/S7weg== X-Google-Smtp-Source: AGHT+IGHHE+4KWf9auYKryB2lu9/fM/qXObyfiN1uXRNz038JkA/1/xLeMhKPiONI2CFf2+1SxQefRfFrD41 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:7801:0:b0:e0e:89e6:aad4 with SMTP id 3f1490d57ef6-e24d44ca00dmr51018276.0.1727307565822; Wed, 25 Sep 2024 16:39:25 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:17 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-3-mmaurer@google.com> Subject: [PATCH v5 02/16] module: Factor out elf_validity_ehdr From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor out verification of the ELF header and document what is checked. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 70 +++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 1a2dd52147ba..59c977acfb44 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1664,6 +1664,50 @@ static int validate_section_offset(const struct load= _info *info, Elf_Shdr *shdr) return 0; } =20 +/** + * elf_validity_ehdr() - Checks an ELF header for module validity + * @info: Load info containing the ELF header to check + * + * Checks whether an ELF header could belong to a valid module. Checks: + * + * * ELF header is within the data the user provided + * * ELF magic is present + * * It is relocatable (not final linked, not core file, etc.) + * * The header's machine type matches what the architecture expects. + * * Optional arch-specific hook for other properties + * - module_elf_check_arch() is currently only used by PPC to check + * ELF ABI version, but may be used by others in the future. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_ehdr(const struct load_info *info) +{ + if (info->len < sizeof(*(info->hdr))) { + pr_err("Invalid ELF header len %lu\n", info->len); + return -ENOEXEC; + } + if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) !=3D 0) { + pr_err("Invalid ELF header magic: !=3D %s\n", ELFMAG); + return -ENOEXEC; + } + if (info->hdr->e_type !=3D ET_REL) { + pr_err("Invalid ELF header type: %u !=3D %u\n", + info->hdr->e_type, ET_REL); + return -ENOEXEC; + } + if (!elf_check_arch(info->hdr)) { + pr_err("Invalid architecture in ELF header: %u\n", + info->hdr->e_machine); + return -ENOEXEC; + } + if (!module_elf_check_arch(info->hdr)) { + pr_err("Invalid module architecture in ELF header: %u\n", + info->hdr->e_machine); + return -ENOEXEC; + } + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1693,30 +1737,10 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) unsigned int num_info_secs =3D 0, info_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 - if (info->len < sizeof(*(info->hdr))) { - pr_err("Invalid ELF header len %lu\n", info->len); - goto no_exec; - } + err =3D elf_validity_ehdr(info); + if (err < 0) + return err; =20 - if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) !=3D 0) { - pr_err("Invalid ELF header magic: !=3D %s\n", ELFMAG); - goto no_exec; - } - if (info->hdr->e_type !=3D ET_REL) { - pr_err("Invalid ELF header type: %u !=3D %u\n", - info->hdr->e_type, ET_REL); - goto no_exec; - } - if (!elf_check_arch(info->hdr)) { - pr_err("Invalid architecture in ELF header: %u\n", - 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 !=3D sizeof(Elf_Shdr)) { pr_err("Invalid ELF section header size\n"); goto no_exec; --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 181061A4E76 for ; Wed, 25 Sep 2024 23:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307571; cv=none; b=XDRPqjt5BQqEy8bZN+OVpLGT9o6d51z7RBPb8f1ZSZixvelaAbcWN/OeoM3TNaTL80jDNe1P0Z0hVjXP0utWJV+UNC8d7ytAE3+OxPcr8fShHVuS6TbYJ235Mr/+EETBPtw+iVwKP5KTgQYYg1HvgGowfb0QhNd5ZE1oJNw3nmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307571; c=relaxed/simple; bh=WRBs20XgxXhpGUNzmAuEPSHiJfsMkZEkGfwB5C4b2is=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fmhdke2DIDrswzSs8F7hNXvABkUyvHCdLkySAgAGdgj3CtRxEL5E/FB39V0tlVB+0+RdNgDp6x4drlNahISABhi2CH5herZk7f6PvPHGA9Dl2ZOFv3wEIYoLgNxVEbqYzuSgDbDoDW53pRBytPVnL8sg/a+9a30TXwMUeDMXz6c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sOdzlmeY; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sOdzlmeY" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-690404fd230so8029797b3.3 for ; Wed, 25 Sep 2024 16:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307568; x=1727912368; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=srrYfI2SZpfKgIsSq8ppkf2FykJu/mFy8hG5tlqb9h4=; b=sOdzlmeY6RY7bpo53PEnQ0YN0/YFAnlXxFf2ZyhVr/9gJ/T37wmWSna16Pm4dszRsH kCAibe8ECqUZozUmaxsJJ7+d2Dm5yeCNVIMbcVZ+mVHroq8zknRXG3i+PN0xMk0cBaAl KtHEwtBf/e0CK81PJDy1xFIO7XAHMNhTiQ2W/9CFH6eGGuFnl7J0a8aKwnum5Tbv3opT H9UqkbO+NemXGkTfLdo2ya2prR7OXRr+P0NBt/iqNlFXgahpRb5JTkG+yUnuAWhTJFTw LSeXIBkWlkFlvXCLqZPkHXV+roqA0hPB8PmXdgVn9JfShCTD4kB4LdafGD5WjtOjCYBu ZXSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307568; x=1727912368; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=srrYfI2SZpfKgIsSq8ppkf2FykJu/mFy8hG5tlqb9h4=; b=OPCa//ndTPdetULq7gr45Vv/wpOq9eqvv6pjzxgB1S9g9YBJ1K8g53SEuNvjkCzkSs 3i2HqZEYATAJugXtjFFhhUKYkdSm941ZahNEzMyHdx5lluLOeGXyFRsEEmQM2jjbwQx8 Fcgxl8VDiOj+tahaVw3IPAMANuRuRhst5LfoLjaRza56rRMI2d6ZmZVhrT4GFwlIIWSO 0Mt4PN3s1af1tY3Du5OnUvDZv450Tp6ZshtDu/b3/vIXEUpl54CxUqg0h6DXPh5f/UOs Euqr16e49bGWRBiISV6O9f49b3o4EokXqRDzn8hL4AwYviX1Xkn4VdAGQSYloXy7J1/s YI/Q== X-Forwarded-Encrypted: i=1; AJvYcCUglfliz2NgOQObSRahHrRmhMVZerZy4FORm/vw+6G6gqrayuCo0DlBK2lnuYaeAnjJJu/y84O5m+Q7pac=@vger.kernel.org X-Gm-Message-State: AOJu0YwO2luJEnjd7GblVVABS4Zw8wRIjpJ9HgFm+p43U3ydSRbCfh3i SjTS1bj0PghGOQ5+LJHIzywUuctz7/ucD+vNA6zp8V8ZtqYbrl+MNhikWKNNy19sWnGxS6xcnfh cZQiyVw== X-Google-Smtp-Source: AGHT+IFULUhyTdpe6K1NKJ4iYoAXDGeSnRiSbmRLHEzsKyUqZwAanm3jLnuYHrgujVpMDbLQEh9f4w7q22Vi X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:c9c:b0:6b0:d571:3540 with SMTP id 00721157ae682-6e21da796e6mr397427b3.6.1727307567956; Wed, 25 Sep 2024 16:39:27 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:18 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-4-mmaurer@google.com> Subject: [PATCH v5 03/16] module: Factor out elf_validity_cache_sechdrs From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor out and document the validation of section headers. Because we now validate all section offsets and lengths before accessing them, we can remove the ad-hoc checks. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 125 ++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 43 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 59c977acfb44..1f3a07ee59c6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1708,6 +1708,87 @@ static int elf_validity_ehdr(const struct load_info = *info) return 0; } =20 +/** + * elf_validity_cache_sechdrs() - Cache section headers if valid + * @info: Load info to compute section headers from + * + * Checks: + * + * * ELF header is valid (see elf_validity_ehdr()) + * * Section headers are the size we expect + * * Section array fits in the user provided data + * * Section index 0 is NULL + * * Section contents are inbounds + * + * Then updates @info with a &load_info->sechdrs pointer if valid. + * + * Return: %0 if valid, negative error code if validation failed. + */ +static int elf_validity_cache_sechdrs(struct load_info *info) +{ + Elf_Shdr *sechdrs; + Elf_Shdr *shdr; + int i; + int err; + + err =3D elf_validity_ehdr(info); + if (err < 0) + return err; + + if (info->hdr->e_shentsize !=3D sizeof(Elf_Shdr)) { + pr_err("Invalid ELF section header size\n"); + return -ENOEXEC; + } + + /* + * e_shnum is 16 bits, and sizeof(Elf_Shdr) is + * known and small. So e_shnum * sizeof(Elf_Shdr) + * will not overflow unsigned long on any platform. + */ + if (info->hdr->e_shoff >=3D info->len + || (info->hdr->e_shnum * sizeof(Elf_Shdr) > + info->len - info->hdr->e_shoff)) { + pr_err("Invalid ELF section header overflow\n"); + return -ENOEXEC; + } + + sechdrs =3D (void *)info->hdr + info->hdr->e_shoff; + + /* + * The code assumes that section 0 has a length of zero and + * an addr of zero, so check for it. + */ + if (sechdrs[0].sh_type !=3D SHT_NULL + || sechdrs[0].sh_size !=3D 0 + || sechdrs[0].sh_addr !=3D 0) { + pr_err("ELF Spec violation: section 0 type(%d)!=3DSH_NULL or non-zero le= n or addr\n", + sechdrs[0].sh_type); + return -ENOEXEC; + } + + /* Validate contents are inbounds */ + for (i =3D 1; i < info->hdr->e_shnum; i++) { + shdr =3D &sechdrs[i]; + switch (shdr->sh_type) { + case SHT_NULL: + case SHT_NOBITS: + /* No contents, offset/size don't mean anything */ + continue; + default: + err =3D validate_section_offset(info, shdr); + if (err < 0) { + pr_err("Invalid ELF section in module (section %u type %u)\n", + i, shdr->sh_type); + return err; + } + } + } + + info->sechdrs =3D sechdrs; + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1737,29 +1818,10 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) unsigned int num_info_secs =3D 0, info_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 - err =3D elf_validity_ehdr(info); + err =3D elf_validity_cache_sechdrs(info); if (err < 0) return err; =20 - if (info->hdr->e_shentsize !=3D sizeof(Elf_Shdr)) { - pr_err("Invalid ELF section header size\n"); - goto no_exec; - } - - /* - * e_shnum is 16 bits, and sizeof(Elf_Shdr) is - * known and small. So e_shnum * sizeof(Elf_Shdr) - * will not overflow unsigned long on any platform. - */ - if (info->hdr->e_shoff >=3D info->len - || (info->hdr->e_shnum * sizeof(Elf_Shdr) > - info->len - info->hdr->e_shoff)) { - pr_err("Invalid ELF section header overflow\n"); - goto no_exec; - } - - info->sechdrs =3D (void *)info->hdr + info->hdr->e_shoff; - /* * Verify if the section name table index is valid. */ @@ -1772,11 +1834,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) } =20 strhdr =3D &info->sechdrs[info->hdr->e_shstrndx]; - err =3D validate_section_offset(info, strhdr); - if (err < 0) { - pr_err("Invalid ELF section hdr(type %u)\n", strhdr->sh_type); - return err; - } =20 /* * The section name table must be NUL-terminated, as required @@ -1793,18 +1850,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) goto no_exec; } =20 - /* - * The code assumes that section 0 has a length of zero and - * an addr of zero, so check for it. - */ - if (info->sechdrs[0].sh_type !=3D SHT_NULL - || info->sechdrs[0].sh_size !=3D 0 - || info->sechdrs[0].sh_addr !=3D 0) { - pr_err("ELF Spec violation: section 0 type(%d)!=3DSH_NULL or non-zero le= n or addr\n", - info->sechdrs[0].sh_type); - goto no_exec; - } - for (i =3D 1; i < info->hdr->e_shnum; i++) { shdr =3D &info->sechdrs[i]; switch (shdr->sh_type) { @@ -1823,12 +1868,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) sym_idx =3D i; fallthrough; default: - err =3D validate_section_offset(info, shdr); - if (err < 0) { - pr_err("Invalid ELF section in module (section %u type %u)\n", - i, shdr->sh_type); - return err; - } if (strcmp(info->secstrings + shdr->sh_name, ".gnu.linkonce.this_module") =3D=3D 0) { num_mod_secs++; --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 541121A4F0C for ; Wed, 25 Sep 2024 23:39:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307573; cv=none; b=OWs2/bIpON5tX9Ez2BRhwEH7hES75+pDHA5/IWmkdsgaRgwLwFvonLaqJ1tf7jJsReDRl5Awl4ksYBhqoGNdnQi0FmVDsaKRrwL6gLdqyna8NeYFYl0T3by3t9ZXqrH1M8ysrcROKzqUdwzMGx1qIiQbT+8GomHHF/UgDguS9So= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307573; c=relaxed/simple; bh=iCsuIDpsaG//12LUTpbXOFHaJGeJHbd09pACcn3fot0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WIvNKUqxMhwW9biRCX+Urkq+0W7khV0/vLeQh6nI7yTjrJV2UPSNNZalGZveH3eySMuU+WiZO9AI7iZkPpdeCVC4sSgR5Q+xOF9h+tJ71qOPBXajaeq9Y4kD7MhyCXUlacP/TPf1q1Bd2vtDO3LePHYMtD115ayBUKCrCQyfXlI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Q08QR+YD; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Q08QR+YD" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e2265809099so698430276.1 for ; Wed, 25 Sep 2024 16:39:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307570; x=1727912370; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vadaxnt9VyshEs2zQXz9EyB1j9KQoYY2nW6Hpjx9NE8=; b=Q08QR+YDROnyZaJUjcY7Me21ezXVpRsUsIEjPwYd/iZNoJr9Xa297mjQyeJkuRvCaI D5iaL7X4NfI1gPcD5IZyOU0VwXpIP5V4ED5MVPiBjihp0vi+IEr+0XQbltQugWA4f2WP 0G/F3f/hItbI/9FLnfrKhC7XkMYIlqp/mCtqgTYiTLjow38lBnuu4xc3raCRBZFE2A1w riZ+uiqUMV4OOmZwMNSAPd3JLngZGn9rJkBTdAlhIXJdp1PRwTa8Gp781SYdxjqzLDNj XDYCuJE+HS0snFX8GBaFcW/kTyADGLdUpGamjfmGo9LEdmfiPQj+pDEgAZYuqBzQ1Q+2 F5eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307570; x=1727912370; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vadaxnt9VyshEs2zQXz9EyB1j9KQoYY2nW6Hpjx9NE8=; b=FPPUAyb60qlxOYKceEWA1wwTPeCPhyOAaRZey4065ovHQ8AjH9545/1GVUjfAj7WGC bsSyL+ozkKXt5S97PLlLsk6ejjdL0FRAsA7z+PkGoovXlg6VoQSIEaYLnFUPdPCCiF+R TDjdyq5MWq4e5htiEbyvKYrhfFBGqYdZLGDv2P1Qt05wCOTO5PwlZHt58/k7BH/HkOLo mWTHH44Tny3Qp2oK/xBlRwDn9HSWpdyXKxac6AXlhPoKDuzZrYlH8RnwLZzjvZhN+zei 0ksywkALxpIZSIoWQKSRP6THF40QbZPfuLz2hUJvdsSC90MHlu3BFIMhfBeyH/9jHtSq Vd6A== X-Forwarded-Encrypted: i=1; AJvYcCXOYHdDbqy8ModuMV0EdIB09/SkU7ZtsAeAurZbscs4jtQppweWWdpYUbYMlYBiA1unU1AJfQD/iAA9pZA=@vger.kernel.org X-Gm-Message-State: AOJu0YzhI0W5+Ar9hvV1a6GSq9oHB7fTfyNMDnJ7O/ExBNH76BKzEh/T 9dAaLCKafh6QwwVG3BZnt88phUbQo6juUIExSE126O6wpeBlk7EgveavZGKiUe5iQa9AGg790kW pJyN5uw== X-Google-Smtp-Source: AGHT+IGXyiXZNUpD5Qq2WYk35Pvjl1ubLDKQ8ZQZ0AU6QSWocII7bL/lrn6rf8k0qZwiwNOpxGN89htUr/Ws X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82056276.2.1727307570290; Wed, 25 Sep 2024 16:39:30 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:19 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-5-mmaurer@google.com> Subject: [PATCH v5 04/16] module: Factor out elf_validity_cache_secstrings From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor out the validation of section names. There are two behavioral changes: 1. Previously, we did not validate non-SHF_ALLOC sections. This may have once been safe, as find_sec skips non-SHF_ALLOC sections, but find_any_sec, which will be used to load BTF if that is enabled, ignores the SHF_ALLOC flag. Since there's no need to support invalid section names, validate all of them, not just SHF_ALLOC sections. 2. Section names were validated *after* accessing them for the purposes of detecting ".modinfo" and ".gnu.linkonce.this_module". They are now checked prior to the access, which could avoid bad accesses with malformed modules. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 106 ++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 1f3a07ee59c6..6a9159afca02 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1789,6 +1789,71 @@ static int elf_validity_cache_sechdrs(struct load_in= fo *info) return 0; } =20 +/** + * elf_validity_cache_secstrings() - Caches section names if valid + * @info: Load info to cache section names from. Must have valid sechdrs. + * + * Specifically checks: + * + * * Section name table index is inbounds of section headers + * * Section name table is not empty + * * Section name table is NUL terminated + * * All section name offsets are inbounds of the section + * + * Then updates @info with a &load_info->secstrings pointer if valid. + * + * Return: %0 if valid, negative error code if validation failed. + */ +static int elf_validity_cache_secstrings(struct load_info *info) +{ + Elf_Shdr *strhdr, *shdr; + char *secstrings; + int i; + + /* + * Verify if the section name table index is valid. + */ + if (info->hdr->e_shstrndx =3D=3D SHN_UNDEF + || info->hdr->e_shstrndx >=3D info->hdr->e_shnum) { + pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >=3D e_shn= um (%d)\n", + info->hdr->e_shstrndx, info->hdr->e_shstrndx, + info->hdr->e_shnum); + return -ENOEXEC; + } + + strhdr =3D &info->sechdrs[info->hdr->e_shstrndx]; + + /* + * The section name table must be NUL-terminated, as required + * by the spec. This makes strcmp and pr_* calls that access + * strings in the section safe. + */ + secstrings =3D (void *)info->hdr + strhdr->sh_offset; + if (strhdr->sh_size =3D=3D 0) { + pr_err("empty section name table\n"); + return -ENOEXEC; + } + if (secstrings[strhdr->sh_size - 1] !=3D '\0') { + pr_err("ELF Spec violation: section name table isn't null terminated\n"); + return -ENOEXEC; + } + + for (i =3D 0; i < info->hdr->e_shnum; i++) { + shdr =3D &info->sechdrs[i]; + /* SHT_NULL means sh_name has an undefined value */ + if (shdr->sh_type =3D=3D SHT_NULL) + continue; + if (shdr->sh_name >=3D strhdr->sh_size) { + pr_err("Invalid ELF section name in module (section %u type %u)\n", + i, shdr->sh_type); + return -ENOEXEC; + } + } + + info->secstrings =3D secstrings; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1812,7 +1877,7 @@ static int elf_validity_cache_sechdrs(struct load_inf= o *info) static int elf_validity_cache_copy(struct load_info *info, int flags) { unsigned int i; - Elf_Shdr *shdr, *strhdr; + Elf_Shdr *shdr; int err; unsigned int num_mod_secs =3D 0, mod_idx; unsigned int num_info_secs =3D 0, info_idx; @@ -1821,34 +1886,9 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) err =3D elf_validity_cache_sechdrs(info); if (err < 0) return err; - - /* - * Verify if the section name table index is valid. - */ - if (info->hdr->e_shstrndx =3D=3D SHN_UNDEF - || info->hdr->e_shstrndx >=3D info->hdr->e_shnum) { - pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >=3D e_shn= um (%d)\n", - info->hdr->e_shstrndx, info->hdr->e_shstrndx, - info->hdr->e_shnum); - goto no_exec; - } - - strhdr =3D &info->sechdrs[info->hdr->e_shstrndx]; - - /* - * The section name table must be NUL-terminated, as required - * by the spec. This makes strcmp and pr_* calls that access - * strings in the section safe. - */ - info->secstrings =3D (void *)info->hdr + strhdr->sh_offset; - if (strhdr->sh_size =3D=3D 0) { - pr_err("empty section name table\n"); - goto no_exec; - } - if (info->secstrings[strhdr->sh_size - 1] !=3D '\0') { - pr_err("ELF Spec violation: section name table isn't null terminated\n"); - goto no_exec; - } + err =3D elf_validity_cache_secstrings(info); + if (err < 0) + return err; =20 for (i =3D 1; i < info->hdr->e_shnum; i++) { shdr =3D &info->sechdrs[i]; @@ -1877,14 +1917,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) num_info_secs++; info_idx =3D i; } - - if (shdr->sh_flags & SHF_ALLOC) { - if (shdr->sh_name >=3D strhdr->sh_size) { - pr_err("Invalid ELF section name in module (section %u type %u)\n", - i, shdr->sh_type); - return -ENOEXEC; - } - } break; } } --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CEBAF1A76BD for ; Wed, 25 Sep 2024 23:39:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307575; cv=none; b=YCFGbM4hl/y/CSq0ja3K1L3S106cAqnEU3E1P+Bd44dyisUD9TmbGC6dpF/FeNkpivSMj3CkHclChPN8dN7OKtKw5Ixf/Gkt+m1BDkuKbpbgkRMSQ6S7cCRsMO3z/hNuXGuyF4aFN5OVdT74L3IMonBe6YMX3AUfe2LTSz369Hc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307575; c=relaxed/simple; bh=oOvwo7YW3AUcfbqrf+A9OVLdTk2yYzaOPGripVgZWf8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=V7TcAjdQhc2XhH/xRhbGzqhB7bC2jI+J/1qXLPYGjdbYB/l9gXKC3nbOJ5hIBW8i5G2iNesgnnCps+uRXyKSz1C+8lGBhkeeeIZed3q7KPmwN+rRFZ4x1zQsrjoTEXuyjdIzKFXHNpsZJgACR9EL0pwK/WSLbC1TsHgsOW+qj2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=m5Z5tSey; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m5Z5tSey" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e20993a03dso7454677b3.2 for ; Wed, 25 Sep 2024 16:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307573; x=1727912373; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=XKJyYEevVucNlB3/71Ku7Qx+VTTwNT+eh7LmoeHsrUQ=; b=m5Z5tSeyRlsxLy1ersJE+UGxQCyLHbFGDz8D/MbyB/kQa7EYcqRvp/f8uPQukKcrCt EfJ1Aezcx3fQoA6m0su+FwrkOEJYSSEJ9sjwyh81XN3Y497rH+FbhbDjzOEl5RRxJqcp VlypXpgjrZZc7R8C480eVLG4oXVSYm2L+1bi4g0AX+N+4vnBqlNY73Op1ZsJ7ljiUy9q ixYagzg4Gdj+FamIYtiuTGhEiXptWpg5DFWUiQc1qygfowqRpMKtS8WnGuyNST4lx3nZ oxHSLIjZkt0GL4tiNEHelvV57QkjUxh1raAHGuZMGQxxKpuRoMq3uVZFs+7SK92eC2Vp /qxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307573; x=1727912373; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XKJyYEevVucNlB3/71Ku7Qx+VTTwNT+eh7LmoeHsrUQ=; b=SaVSe86fjQ2fjf5uAT0zqgxYuLDXfCNcn17R8uRr2B8PfZkhPypHW6vw1eB+Uj1V+9 rzpQmGepFWUhBi1Itwfg8YwVdvphMQe+SQyHqlZveCXX5NvvDurqo9B/CjgoxRrQOyB7 /Q+hpzyru2MeBHxcU/2L7HnhG2w0KkUwxxZc27RFaGpmZCA6c/KB/UYQ3mKwfXYrY9Jo XUHmBLq68rqBS9dfuRrg5kSREmctlZPa5zq/+1vy6cfsz7c3kc0hHGb+JLYrTMJmK9cg 8WSte/PPGsAohSn1qKCKSl0cx7e/stRq0/WCi2xIDNC+g3KRiDJftgPj8F2bw3GwrYrV zr1w== X-Forwarded-Encrypted: i=1; AJvYcCXci5OQKxshDva4GHevEu5AEmSMAxh97MPvhItoskLmwb6KgF7CHvy+OHavVL6eRjFh6Upd08qxfTDz0Gs=@vger.kernel.org X-Gm-Message-State: AOJu0YwYlRrDqsIbH6IQP6u3T+vP+HV7P5sUrhWONNiM2ahl9o0RRFz+ owprBLKUM2H/H6e30GuJZi1zT7VRjrqBEjGY2kURIij207mp/F94Yx6nnBK+6UWd66/jmU3YLXW WiUN+WA== X-Google-Smtp-Source: AGHT+IGx+nve+AoQTJhC0039Mrm5L1Mmot8nzzFqz6EcNfCPXpgrANZ2tGjsmltzJQkDur51XUIFhuHCnZ0h X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:780e:0:b0:e16:6e0a:bb0b with SMTP id 3f1490d57ef6-e24d44cad51mr4787276.0.1727307572609; Wed, 25 Sep 2024 16:39:32 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:20 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-6-mmaurer@google.com> Subject: [PATCH v5 05/16] module: Factor out elf_validity_cache_index_info From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Centralize .modinfo detection and property validation. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 82 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 6a9159afca02..511d645ac577 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -195,6 +195,38 @@ static unsigned int find_sec(const struct load_info *i= nfo, const char *name) return 0; } =20 +/** + * find_any_unique_sec() - Find a unique section index by name + * @info: Load info for the module to scan + * @name: Name of the section we're looking for + * + * Locates a unique section by name. Ignores SHF_ALLOC. + * + * Return: Section index if found uniquely, zero if absent, negative count + * of total instances if multiple were found. + */ +static int find_any_unique_sec(const struct load_info *info, const char *n= ame) +{ + unsigned int idx; + unsigned int count =3D 0; + int i; + + for (i =3D 1; i < info->hdr->e_shnum; i++) { + if (strcmp(info->secstrings + info->sechdrs[i].sh_name, + name) =3D=3D 0) { + count++; + idx =3D i; + } + } + if (count =3D=3D 1) { + return idx; + } else if (count =3D=3D 0) { + return 0; + } else { + return -count; + } +} + /* Find a module section, or NULL. */ static void *section_addr(const struct load_info *info, const char *name) { @@ -1854,6 +1886,39 @@ static int elf_validity_cache_secstrings(struct load= _info *info) return 0; } =20 +/** + * elf_validity_cache_index_info() - Validate and cache modinfo section + * @info: Load info to populate the modinfo index on. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed + * + * Checks that if there is a .modinfo section, it is unique. + * Then, it caches its index in &load_info->index.info. + * Finally, it tries to populate the name to improve error messages. + * + * Return: %0 if valid, %-ENOEXEC if multiple modinfo sections were found. + */ +static int elf_validity_cache_index_info(struct load_info *info) +{ + int info_idx; + + info_idx =3D find_any_unique_sec(info, ".modinfo"); + + if (info_idx =3D=3D 0) + /* Early return, no .modinfo */ + return 0; + + if (info_idx < 0) { + pr_err("Only one .modinfo section must exist.\n"); + return -ENOEXEC; + } + + info->index.info =3D info_idx; + /* Try to find a name early so we can log errors with a module name */ + info->name =3D get_modinfo(info, "name"); + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1880,13 +1945,15 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) Elf_Shdr *shdr; int err; unsigned int num_mod_secs =3D 0, mod_idx; - unsigned int num_info_secs =3D 0, info_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 err =3D elf_validity_cache_sechdrs(info); if (err < 0) return err; err =3D elf_validity_cache_secstrings(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_info(info); if (err < 0) return err; =20 @@ -1912,24 +1979,11 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) ".gnu.linkonce.this_module") =3D=3D 0) { num_mod_secs++; mod_idx =3D i; - } else if (strcmp(info->secstrings + shdr->sh_name, - ".modinfo") =3D=3D 0) { - num_info_secs++; - info_idx =3D i; } break; } } =20 - if (num_info_secs > 1) { - pr_err("Only one .modinfo section must exist.\n"); - goto no_exec; - } else if (num_info_secs =3D=3D 1) { - /* Try to find a name early so we can log errors with a module name */ - info->index.info =3D info_idx; - info->name =3D get_modinfo(info, "name"); - } - if (num_sym_secs !=3D 1) { pr_warn("%s: module has no symbols (stripped?)\n", info->name ?: "(missing .modinfo section or name field)"); --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B78BE1AB6E4 for ; Wed, 25 Sep 2024 23:39:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307578; cv=none; b=MBmAb3vIyn4+Tbt1n+afcjmwEsJuCP7ZF05rlKfFLlf+q84GZ3KhzNe8cLAy/d+9Vs5dulvcT1Vctk3L8RLsCjm3lrt/D1KpwU8PhUfUU8fUpXRigAJetxAAVVwDxZWhfH+QeaMfvnFqpDRD+uU99MjurwkUKfWqwwm5Fnk99W4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307578; c=relaxed/simple; bh=h/LAOljVw5E3JGGOaE89sERZTa3pvx6H/SQhACbdpCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PXqvmW0iNmR3G+9uZR1mYr3z/kk8pLJ5FFarAJLEXBRvg3L0uG4+HBiUNQeCqSnoN6rBhiVuzJ8axnIaL8hxqGWF2b+8HrA65mOmJKiV6hfNhHWCHjAZ7Bs4QbJKFiyTZVVuAkWHXCkC/hZ8kX8MKfbMK605V04BC3hjxL/eryI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bLzMJc1z; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bLzMJc1z" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e20e4e55bbso9636707b3.0 for ; Wed, 25 Sep 2024 16:39:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307575; x=1727912375; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Q8UL0Q7oSLmAAG+S1LbknTXXfZfG+WAJTpNf/8OfOpQ=; b=bLzMJc1z9tr9FfcPYNuF0FgDcztptHJ1LRBD+erLfNiV2PY2u4HZxOfIeAXGY0EQ/c tdFb1ycVm5t8818XUd5c1lXovrHrUKltTsdKPovlYOU0pKm5sXPPmGwes3ACT/xHfQ+X Y1hFGlLB85haavFgwDwoOCeeFk1BnkyCzcADPdiKdW1ohmGRboABBKPAswFrJQdKpcUk T8f+Lwd+wIFFomvH1yJE8BrP2Nj8DY3B1fJg+8DJP0eYqG6/blOLutMbMv1pM7kDRYqE D/E3li1pycxfJw4Bo/JGXG04spUOrIDbca1MRR8jDLmyEpFtm5eTTsRWRSJfsJZTiS3J dCmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307575; x=1727912375; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Q8UL0Q7oSLmAAG+S1LbknTXXfZfG+WAJTpNf/8OfOpQ=; b=knb46CkfnaZlaVpqZjGw1/Pay6/MPyxV//Msn1r2dXWuUp5GyEgksqh6d7HUUiuVjz pA0lHZ1iP0wsY/Y+n6VBd1d08L0gkQoN6nbS2DaJdEYfsRiQH0JWCdX6MbH7Rk6IHG1I bajPY+F6jNQS4yV/s81z0Ai38pQzyPJKSRtFDCdg0bpLsA36ine5CPSi5iVdGEljis4N 1ewgwzcURKyPHTDh31I01u7Xp+4bcKEq9/TEeMgoQxkKUxvog8GSB6BcLqM5m9MERRjm GkWZEbTDM6qL4iuAxj4Pu7ICCyEIPjZyjD/B1XxQIxpxAJnBqlCz4UEQBH534A6b3ldA KeoA== X-Forwarded-Encrypted: i=1; AJvYcCWVKxfAn833f8A1exHqvbtnxzhdXe9PRWUNLWlbDUwS8P4GX4uNnxZLHR78crHM8ivJo6itCEdT6wwwQ44=@vger.kernel.org X-Gm-Message-State: AOJu0YwoQjdJJLm7D03HCIwzdzRbS52LGTmbtjCLVJr11GwJdYsW0KcD fEi5kYrY+bAIKkvJFO1jF/O/HL6wkqV/ybNZC7Osn9tHLYAI8sy3mQILIbEdHfmc/hrZ5PIQwnc tU+giqw== X-Google-Smtp-Source: AGHT+IG616iIEoJXepMEnFIXh2EvSXgtvB4OvHJFrlY3ghUherNjNwvIMu6DpxK7ywhW+VD5HsliaqgLk/2C X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:d43:0:b0:e02:c06f:1db8 with SMTP id 3f1490d57ef6-e24d7b07255mr3692276.4.1727307575061; Wed, 25 Sep 2024 16:39:35 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:21 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-7-mmaurer@google.com> Subject: [PATCH v5 06/16] module: Factor out elf_validity_cache_index_mod From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Centralize .gnu.linkonce.this_module detection and property validation. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 129 ++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 511d645ac577..ec638187ffcf 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1919,6 +1919,68 @@ static int elf_validity_cache_index_info(struct load= _info *info) return 0; } =20 +/** + * elf_validity_cache_index_mod() - Validates and caches this_module secti= on + * @info: Load info to cache this_module on. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed + * + * The ".gnu.linkonce.this_module" ELF section is special. It is what modp= ost + * uses to refer to __this_module and let's use rely on THIS_MODULE to poi= nt + * to &__this_module properly. The kernel's modpost declares it on each + * modules's *.mod.c file. If the struct module of the kernel changes a fu= ll + * kernel rebuild is required. + * + * We have a few expectations for this special section, this function + * validates all this for us: + * + * * The section has contents + * * The section is unique + * * We expect the kernel to always have to allocate it: SHF_ALLOC + * * The section size must match the kernel's run time's struct module + * size + * + * If all checks pass, the index will be cached in &load_info->index.mod + * + * Return: %0 on validation success, %-ENOEXEC on failure + */ +static int elf_validity_cache_index_mod(struct load_info *info) +{ + Elf_Shdr *shdr; + int mod_idx; + + mod_idx =3D find_any_unique_sec(info, ".gnu.linkonce.this_module"); + if (mod_idx <=3D 0) { + pr_err("module %s: Exactly one .gnu.linkonce.this_module section must ex= ist.\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + shdr =3D &info->sechdrs[mod_idx]; + + if (shdr->sh_type =3D=3D SHT_NOBITS) { + pr_err("module %s: .gnu.linkonce.this_module section must have a size se= t\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + if (!(shdr->sh_flags & SHF_ALLOC)) { + pr_err("module %s: .gnu.linkonce.this_module must occupy memory during p= rocess execution\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + if (shdr->sh_size !=3D sizeof(struct module)) { + pr_err("module %s: .gnu.linkonce.this_module section size must match the= kernel's built struct module size at run time\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.mod =3D mod_idx; + + return 0; +} + + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -1944,7 +2006,6 @@ static int elf_validity_cache_copy(struct load_info *= info, int flags) unsigned int i; Elf_Shdr *shdr; int err; - unsigned int num_mod_secs =3D 0, mod_idx; unsigned int num_sym_secs =3D 0, sym_idx; =20 err =3D elf_validity_cache_sechdrs(info); @@ -1954,16 +2015,15 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) if (err < 0) return err; err =3D elf_validity_cache_index_info(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_mod(info); if (err < 0) return err; =20 for (i =3D 1; i < info->hdr->e_shnum; i++) { shdr =3D &info->sechdrs[i]; - switch (shdr->sh_type) { - case SHT_NULL: - case SHT_NOBITS: - continue; - case SHT_SYMTAB: + if (shdr->sh_type =3D=3D SHT_SYMTAB) { if (shdr->sh_link =3D=3D SHN_UNDEF || shdr->sh_link >=3D info->hdr->e_shnum) { pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr-= >e_shnum(%d)\n", @@ -1973,14 +2033,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) } num_sym_secs++; sym_idx =3D i; - fallthrough; - default: - if (strcmp(info->secstrings + shdr->sh_name, - ".gnu.linkonce.this_module") =3D=3D 0) { - num_mod_secs++; - mod_idx =3D i; - } - break; } } =20 @@ -1996,55 +2048,8 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) info->index.str =3D shdr->sh_link; info->strtab =3D (char *)info->hdr + info->sechdrs[info->index.str].sh_of= fset; =20 - /* - * The ".gnu.linkonce.this_module" ELF section is special. It is - * what modpost uses to refer to __this_module and let's use rely - * on THIS_MODULE to point to &__this_module properly. The kernel's - * modpost declares it on each modules's *.mod.c file. If the struct - * module of the kernel changes a full kernel rebuild is required. - * - * We have a few expectaions for this special section, the following - * code validates all this for us: - * - * o Only one section must exist - * o We expect the kernel to always have to allocate it: SHF_ALLOC - * o The section size must match the kernel's run time's struct module - * size - */ - if (num_mod_secs !=3D 1) { - pr_err("module %s: Only one .gnu.linkonce.this_module section must exist= .\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - shdr =3D &info->sechdrs[mod_idx]; - - /* - * This is already implied on the switch above, however let's be - * pedantic about it. - */ - if (shdr->sh_type =3D=3D SHT_NOBITS) { - pr_err("module %s: .gnu.linkonce.this_module section must have a size se= t\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - if (!(shdr->sh_flags & SHF_ALLOC)) { - pr_err("module %s: .gnu.linkonce.this_module must occupy memory during p= rocess execution\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - if (shdr->sh_size !=3D sizeof(struct module)) { - pr_err("module %s: .gnu.linkonce.this_module section size must match the= kernel's built struct module size at run time\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; - } - - info->index.mod =3D mod_idx; - /* This is temporary: point mod into copy of data. */ - info->mod =3D (void *)info->hdr + shdr->sh_offset; + info->mod =3D (void *)info->hdr + info->sechdrs[info->index.mod].sh_offse= t; =20 /* * If we didn't load the .modinfo 'name' field earlier, fall back to --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE5D31ABECC for ; Wed, 25 Sep 2024 23:39:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307581; cv=none; b=jmfbtfBTam306xBV8qa2QmYdvJYNnCuu04t5sCplPZnKFHykbNwzDQ0lXjr2mq1/m7ZBHA+dhmIXukwf6EvHlhSiE4nSEdt9VTsXartJtN6cB6u/KuBSCuDoga5R1MVnI2l/4OqehkMUH8B98kAgduywIyBVRqvLXJwz7oxO8fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307581; c=relaxed/simple; bh=hPcayIG4Zzk7TfnQCe5VB/OkeMCLVNaFCb22SLuD7xA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ULUdUQwdQKnzAVBntNKOTrrUHl4jcYNoIahYaYkJhNrZcuG0jlRiWxF5iBgKdddNTTbjss2v2ISLT6YxCBfF/tse4rCURkY3R1gjum1poXDdACpX8+RiVcXFxHH13rPJ/a2MXyQ/p8NF/d3mror81PQ2cwRyyH8aEdFXQsgoyoQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wb4Kc+vl; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wb4Kc+vl" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e249fafe1ebso732986276.2 for ; Wed, 25 Sep 2024 16:39:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307578; x=1727912378; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=o0kXAOzmEEQHXzTmLkL/vEUOj2egGiISa865No2odHA=; b=wb4Kc+vlm9vspCrEGu31uSdmGCFV4N+qq3ZdgtPNuVg7PQ3AnM/tZOhfz3qmMsSGKR EJ3oKVJaibX0jMt6Z+MELN82Fl4xPXd8nCqPcMLDXQydk4pva9Aw2Atp1OS9iLAiVwbk /FpGLP8QjK2w8po1aocYEVWGjEc/Z9QISVlarljpEcPa5uNqf7A71TvKtxFc0MTg7clF 5kscch70KYzMTVSw25al7YR2T4jsybqoH2sWWp5pBzSEGjSAHdpvqKthwvZ5YcGadoWF s4eLU3/RbMVaF8/7D3L74rtn815TeO0ECl5FTnNu9mxm2o2dt5uKd1HOZkheUoD/Ccmi 0uPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307578; x=1727912378; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o0kXAOzmEEQHXzTmLkL/vEUOj2egGiISa865No2odHA=; b=LYI/WAtTj6duC4U0vbLwPyoZWVyl1PDfkBFIyDozxFOos3FzPgSPzmL8cmiS46kAsW o5YZUFiprXS0CAE8QvNpZoeAM9qdvJGoqd61TR724NoE/Xb2LyjIBYpN6DcfIVfGUGSG 8c5b9jxTrDqMiEkJYvJm5qGfywiDIqRfEGaGeFeiymU0OaKJp3wavjh/PWv27TcQukqO i8LohY8lw0i0rdXIvmbVeYfG6v5Q1GcM/BYJVZotYSB18JTwUP0MDy5MY8RVX2mGpUVd fhO6WNMlnZhfc79csp6u3KzchZrteNUo5TEPfIGMfIGOmpj2fxL2HrFL4hgpVO5jM0hC Xfqw== X-Forwarded-Encrypted: i=1; AJvYcCVtIQmj2OZNfxBx0iHo0KsBK+aDgb5CW95RDAjodxZWM1WuERcAxhZiTa0DAKoBpSA26KvA4rnIZiPSUJw=@vger.kernel.org X-Gm-Message-State: AOJu0YySHXB7A0YNuq/fgo9ZVcK1W0RRWBksd2ZnzvdAWC/1UprogX6k mrn4u5WEiKm2rSRjUMRIWzx3S/Di1f7yRkuuk5iAV/ru3LrfKrRMdYZHw6j0qysX/Wol4MITZfT tfg5Nhw== X-Google-Smtp-Source: AGHT+IFZUoBFWfBOtauI+q+UpHX4EaqZHRhVqScDHU9VRfOA/W5QmMinhHWYwHFcLrUwulCaskY1L43rYGa0 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:841:0:b0:e20:25f5:a889 with SMTP id 3f1490d57ef6-e24d80d4612mr3980276.5.1727307577820; Wed, 25 Sep 2024 16:39:37 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:22 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-8-mmaurer@google.com> Subject: [PATCH v5 07/16] module: Factor out elf_validity_cache_index_sym From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Centralize symbol table detection and property validation. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 73 ++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index ec638187ffcf..6be58b0a6468 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1980,6 +1980,39 @@ static int elf_validity_cache_index_mod(struct load_= info *info) return 0; } =20 +/** + * elf_validity_cache_index_sym() - Validate and cache symtab index + * @info: Load info to cache symtab index in. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed. + * + * Checks that there is exactly one symbol table, then caches its index in + * &load_info->index.sym. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_sym(struct load_info *info) +{ + unsigned int sym_idx; + unsigned int num_sym_secs =3D 0; + int i; + + for (i =3D 1; i < info->hdr->e_shnum; i++) { + if (info->sechdrs[i].sh_type =3D=3D SHT_SYMTAB) { + num_sym_secs++; + sym_idx =3D i; + } + } + + if (num_sym_secs !=3D 1) { + pr_warn("%s: module has no symbols (stripped?)\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.sym =3D sym_idx; + + return 0; +} =20 /* * Check userspace passed ELF module against our expectations, and cache @@ -2003,10 +2036,8 @@ static int elf_validity_cache_index_mod(struct load_= info *info) */ static int elf_validity_cache_copy(struct load_info *info, int flags) { - unsigned int i; - Elf_Shdr *shdr; int err; - unsigned int num_sym_secs =3D 0, sym_idx; + int str_idx; =20 err =3D elf_validity_cache_sechdrs(info); if (err < 0) @@ -2018,34 +2049,21 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) if (err < 0) return err; err =3D elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_sym(info); if (err < 0) return err; =20 - for (i =3D 1; i < info->hdr->e_shnum; i++) { - shdr =3D &info->sechdrs[i]; - if (shdr->sh_type =3D=3D SHT_SYMTAB) { - if (shdr->sh_link =3D=3D SHN_UNDEF - || shdr->sh_link >=3D info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr-= >e_shnum(%d)\n", - shdr->sh_link, shdr->sh_link, - info->hdr->e_shnum); - goto no_exec; - } - num_sym_secs++; - sym_idx =3D i; - } - } - - if (num_sym_secs !=3D 1) { - pr_warn("%s: module has no symbols (stripped?)\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; + str_idx =3D info->sechdrs[info->index.sym].sh_link; + if (str_idx =3D=3D SHN_UNDEF || str_idx >=3D info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr->e= _shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; } =20 - /* Sets internal symbols and strings. */ - info->index.sym =3D sym_idx; - shdr =3D &info->sechdrs[sym_idx]; - info->index.str =3D shdr->sh_link; + /* Sets internal strings. */ + info->index.str =3D str_idx; info->strtab =3D (char *)info->hdr + info->sechdrs[info->index.str].sh_of= fset; =20 /* This is temporary: point mod into copy of data. */ @@ -2066,9 +2084,6 @@ static int elf_validity_cache_copy(struct load_info *= info, int flags) info->index.pcpu =3D find_pcpusec(info); =20 return 0; - -no_exec: - return -ENOEXEC; } =20 #define COPY_CHUNK_SIZE (16*PAGE_SIZE) --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAECF1AC449 for ; Wed, 25 Sep 2024 23:39:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307582; cv=none; b=NHILiHg/IPnjtY5YAvkrHoOEcu+5SC6MHm8LIroXYKwvN4BJZbf5TRaMqWXjB2vXTYP+73o1w5jTBFXvEidt6io8raf0cjfLAG+gia/UqENPUTqFmfILNexLg2wnfz57B/DE5u7KXiWTbvIhGLiSo+fb1EdYmRJkO97xtCRjIvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307582; c=relaxed/simple; bh=wM975yYhhnJbskoT3Xe/Z8RaXLTIAwoZIK7NoqKeDB8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JQsxk6QtAlHG2MQbo9VpSqCZd1/UtYfYrzLhakv3wKgmSqhzOFvkaFFe9f7t4nT0hVbknWSHU0WMI/w+6H8A+5Ue9CT1e7m/8PuJw9z5SCCRarT6l8WkAd+IGCvlCLgXa27HRVSHW+0yXvfuROAIhu4Mks3ZdlMarFof839T1zI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ace5t6Cv; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ace5t6Cv" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1159fb161fso624212276.1 for ; Wed, 25 Sep 2024 16:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307580; x=1727912380; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fOjsxdYdAkInxupUorws184MmwFGmal1AWb/bAcHrOQ=; b=Ace5t6CvyKVH55R5F+8+SFEGTxJt+z7edGdIMLwXZb2veNJnyJiw+inHBKPQefkXcj yfj9g09w66ixqXNGIKfYucTJ/eQohq0K1Zw2pRs/eTkiv0VfbbVbxWuoBf/BqOEqslT5 /ztQBPjaqKWZO0jdXdJa3Wu5L1+NUjStuEM5JR2vT+eTeJuhMuhiklq/b6z3OdsGFcwm dEwg8wI3mrLRBO4tM79Mpzhv34Gs4wka9YKLR6aAjcdY5/2JuRczf900R1uv+5+F6+33 A11AMc3aVg0SeQnHWFLMN4hDviU7z4O6SwauY33tJ1U/86KDOoxW2/k2Gqmtpu/SXwey eadg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307580; x=1727912380; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fOjsxdYdAkInxupUorws184MmwFGmal1AWb/bAcHrOQ=; b=uf46Jo6WTveSdePp16VbPe3blucG+z9f7I6zl7Rrry8U9pxe8Pw1cmowyOy7+yuypt pgqyzMKFNIhjmfkDb3OoOjoC0v0vtXv8KYrGkUM81TTdfjXIu8J3+Mpd3WNUXSyY023m jmpUzwwh2By1vfzyEc4hw58nqUAOt9k9QpPuDH4VY2nCLTwhffJQDPjP/FTPdK6FFwkZ 0A+kv4dE9Ic+ZjO6KcIdJBZEY99IKQ3Oq9LISqCB79NtbSoAbTSsAnL6GRbx5Z8U0saS TTRACOct2uLcL1YKxtx2GrC/Jj7B6UVXA9baVYIQgTCFJxDkkBSxLhEvD0NxgvU+yw72 FFqQ== X-Forwarded-Encrypted: i=1; AJvYcCU/OoSUHat+bdXijH5hq67FnDmDvJGtCdWQ6Rg9APr+Cn6byV+pytHn+YXwBKJxxSq8YxnUJQFMJcAv/7Q=@vger.kernel.org X-Gm-Message-State: AOJu0YzkLzpD1U46L4Z6BvH2PGuxHr5PxVGurm42zF9eBD/OAjGmtZa5 SG8BhAYu3nIU9fcgYtsuCfbU1prtpCt28PNoiLhpPi0jISsvGFF+lRA4SFM0HLAwo65ExDJq/Kq cF4uJBA== X-Google-Smtp-Source: AGHT+IHdWqR9IrD2hZQX0Xo9yhQJzQmApDf52iNmKjMPjmcrCoxOYUdkaSUs0GEug6EnSPPHsMtafWiB8pKN X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a5b:841:0:b0:e24:9af1:43e7 with SMTP id 3f1490d57ef6-e24d7fdf205mr3591276.5.1727307579861; Wed, 25 Sep 2024 16:39:39 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:23 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-9-mmaurer@google.com> Subject: [PATCH v5 08/16] module: Factor out elf_validity_cache_index_str From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pull out index validation for the symbol string section. Note that this does not validate the *contents* of the string table, only shape and presence of the section. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 6be58b0a6468..43140475aac0 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2014,6 +2014,31 @@ static int elf_validity_cache_index_sym(struct load_= info *info) return 0; } =20 +/** + * elf_validity_cache_index_str() - Validate and cache strtab index + * @info: Load info to cache strtab index in. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed. + * Must have &load_info->index.sym populated. + * + * Looks at the symbol table's associated string table, makes sure it is + * in-bounds, and caches it. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_str(struct load_info *info) +{ + unsigned int str_idx =3D info->sechdrs[info->index.sym].sh_link; + + if (str_idx =3D=3D SHN_UNDEF || str_idx >=3D info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr->e= _shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; + } + + info->index.str =3D str_idx; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2037,7 +2062,6 @@ static int elf_validity_cache_index_sym(struct load_i= nfo *info) static int elf_validity_cache_copy(struct load_info *info, int flags) { int err; - int str_idx; =20 err =3D elf_validity_cache_sechdrs(info); if (err < 0) @@ -2054,16 +2078,11 @@ static int elf_validity_cache_copy(struct load_info= *info, int flags) err =3D elf_validity_cache_index_sym(info); if (err < 0) return err; - - str_idx =3D info->sechdrs[info->index.sym].sh_link; - if (str_idx =3D=3D SHN_UNDEF || str_idx >=3D info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=3DSHN_UNDEF(%d) or (sh_link(%d) >=3D hdr->e= _shnum(%d)\n", - str_idx, str_idx, info->hdr->e_shnum); - return -ENOEXEC; - } + err =3D elf_validity_cache_index_str(info); + if (err < 0) + return err; =20 /* Sets internal strings. */ - info->index.str =3D str_idx; info->strtab =3D (char *)info->hdr + info->sechdrs[info->index.str].sh_of= fset; =20 /* This is temporary: point mod into copy of data. */ --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 447171A42DD for ; Wed, 25 Sep 2024 23:39:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307584; cv=none; b=HSN3WuZgHUK272pcoinLXJegyVXE7n3R5pcmjXKn+iufiEHXR4YRnABhtk2wIVxw3KVygZJBev2iC3eVcWovU4EMe/trbNjRXsnLJGKOwOppNpOZ2OXxGdkfxgAurLGA4J/gYgvhOSFcWUj4He8zxPqDQ01udeliJnWXFXtVHow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307584; c=relaxed/simple; bh=uwfaf/NAzm+40ipxYEoBkkFYyfKzhKmnZzjM3YJU8lE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ByBUyeWxdFnPegb5qS/btQner/G+MAu1UTjTbHLYRVXjNPJJWhH/xwAGPQ8HDdA/6X07r+oRqRCbMhPNXMfqt9bPR8jbXLNJ0KSRPddxjfigVhSht4VLVT2Yy630N/gYB6oo09Dc5RH+FRXZt2t8m/eckL96wP9O0ugKa9AwK2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Dnn4lcx6; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Dnn4lcx6" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e0353b731b8so587547276.2 for ; Wed, 25 Sep 2024 16:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307582; x=1727912382; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tc4NXPfplG38lAGlz62WMnJTBJMM1sboCG51BTx1YyI=; b=Dnn4lcx6ETC3b56/fMVL5Sowje5BnLypRNCI6y7V/UppV+ykNC/QI9yXrPLZa4MZDt IiHnuatUNziMF/+SokFMxI0gK60XEbhHCBIUJk6S/fo1t/1cq++ylzYyVRuLE9Nh1qfU v2AibX5noO2NCGTeCsNF7VFL8XgWV4/+kWp8VSXbVFom43FlVRqHkSHr9uKSyEh5DFL5 y2dhTc823caiGsStgcaR3298Pw1aegvPXvVBHk+4CYlTHMqbdoyv1fwD3aIFipfMDUcU 5o31nY4/exqQ5P3Bpj7AO0SDYu6DhamliBw7OMpApYnlI43yFEggoO8VVNhZP9eWf7kJ 9XjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307582; x=1727912382; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tc4NXPfplG38lAGlz62WMnJTBJMM1sboCG51BTx1YyI=; b=OvFMOboCvh9dQrMI9ObUvI9JUFw3+QpSD0SjgMMG0OTXFrPzxwTCeJuZGeOIhkE67e C+5daoxLzYrFx3ElWWNazjWIdKn1HA5+yPsxDc4osXV+YGCMCTDt0lZ6O1R903aJ121G 1N45Kg9kNZQxBVI5Xeg4lQPQPFUfzdbMeexGfbg63kiok76ClWNv6eLukT0U4xWvRNGQ //N9fSekdSblVHIMPlwJNqazFgrB5pKVnexhaOXJGO+Jj8t3nEfhSTi68SC+v7f9pPVw 4XRjYmjo6m1ppA1l06uJ3LlLFDoykYTk7xFRM+TmN9Mr5519ld80ogqsLzn46fz7A5o1 D24Q== X-Forwarded-Encrypted: i=1; AJvYcCWDLd5nM5R8LdNIBQywleEddIsnPzyc/Y+O6/756i2MaHpExWgh+L9YaEUJOFBeoqTJQwPg4pjdbUulpP8=@vger.kernel.org X-Gm-Message-State: AOJu0YxCQfyflzVrXi/E8MbT9j/sH920Qp/yUBsBlzH4YMTIZqt1Sz+z iQEGTwXtLzqms158K+BnDWXsFGbX69NaGUzVeiqJJBMNxz1sjGl7s0DGwU7mqh2wgaIqFEYyhCs iobLL6g== X-Google-Smtp-Source: AGHT+IF+swNEdiAvpBogXsgKrBiSvXnXm1t/O+Acxf832i2L6CrfB3YUi3r7S2mXXta4Ea8IRO3aupfjGYBp X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:1367:b0:e1a:22d5:d9eb with SMTP id 3f1490d57ef6-e24d716247fmr3341276.1.1727307582039; Wed, 25 Sep 2024 16:39:42 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:24 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-10-mmaurer@google.com> Subject: [PATCH v5 09/16] module: Group section index calculations together From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Group all the index detection together to make the parent function easier to read. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 68 +++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 43140475aac0..e04a228c694a 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2039,6 +2039,56 @@ static int elf_validity_cache_index_str(struct load_= info *info) return 0; } =20 +/** + * elf_validity_cache_index() - Resolve, validate, cache section indices + * @info: Load info to read from and update. + * &load_info->sechdrs and &load_info->secstrings must be populate= d. + * @flags: Load flags, relevant to suppress version loading, see + * uapi/linux/module.h + * + * Populates &load_info->index, validating as it goes. + * See child functions for per-field validation: + * + * * elf_validity_cache_index_info() + * * elf_validity_cache_index_mod() + * * elf_validity_cache_index_sym() + * * elf_validity_cache_index_str() + * + * If versioning is not suppressed via flags, load the version index from + * a section called "__versions" with no validation. + * + * If CONFIG_SMP is enabled, load the percpu section by name with no + * validation. + * + * Return: 0 on success, negative error code if an index failed validation. + */ +static int elf_validity_cache_index(struct load_info *info, int flags) +{ + int err; + + err =3D elf_validity_cache_index_info(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_sym(info); + if (err < 0) + return err; + err =3D elf_validity_cache_index_str(info); + if (err < 0) + return err; + + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) + info->index.vers =3D 0; /* Pretend no __versions section! */ + else + info->index.vers =3D find_sec(info, "__versions"); + + info->index.pcpu =3D find_pcpusec(info); + + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2069,16 +2119,7 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) err =3D elf_validity_cache_secstrings(info); if (err < 0) return err; - err =3D elf_validity_cache_index_info(info); - if (err < 0) - return err; - err =3D elf_validity_cache_index_mod(info); - if (err < 0) - return err; - err =3D elf_validity_cache_index_sym(info); - if (err < 0) - return err; - err =3D elf_validity_cache_index_str(info); + err =3D elf_validity_cache_index(info, flags); if (err < 0) return err; =20 @@ -2095,13 +2136,6 @@ static int elf_validity_cache_copy(struct load_info = *info, int flags) if (!info->name) info->name =3D info->mod->name; =20 - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) - info->index.vers =3D 0; /* Pretend no __versions section! */ - else - info->index.vers =3D find_sec(info, "__versions"); - - info->index.pcpu =3D find_pcpusec(info); - return 0; } =20 --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C41E1AD3EB for ; Wed, 25 Sep 2024 23:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307587; cv=none; b=mLPdgCHwz/5BGQJRww+p5B6PtJPCKubO7MhVQmBQulMhJANy2Nus93DGpEny26F5wTUK7Q0DJDcs2Du3GU+S+eedGAToZnAMi/naGiDvGjbUqSMVBEzLK+CIklrm3HGHn7nvVILYwSEkmm5ofWSHfeIzzH7coVdu8I1ib7H6qCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307587; c=relaxed/simple; bh=RFwYTaeqemLtmezDNnI4k0C/4IQdB480nO3xNfeeufc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HsmFN+FN1ieu93oBItkLT1UDa+uEgSI0m6SK2XAs3glMHUUQ9EF94mlQDG4KcLLn326kEAYzY9YEZSwrCSnpa0vNx4oPuVdQZubqMW38aAjNpv5c/cdK4URnKaS0vtQC5PmGdeAj+h6EoScmd5QYCX1vvBPGKg+nPBjoF4vo05Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MZmAsWA3; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MZmAsWA3" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d7124938d1so11644247b3.0 for ; Wed, 25 Sep 2024 16:39:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307584; x=1727912384; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=extRuG4D1AdoHKvsMv2fLx/d2D/gEpHsXYLRVzC3Sa0=; b=MZmAsWA3xomFzJ3UUFSEudjq/FEAUJacydlEI1beul+6uGuE6Dk612LIlvkuQsgQxE LKFw1JuiaBXdG1uHQJtonGotcE3vuZBlVdDPI5OlmeijfeydlODQgykSy2MKioYhpxBx tpDeVANLnJsAfhxUabBpeAZO2v32QtQ0ZnkCZwOjiQMrKcSKK7qk+g9nJqLz2wWRoIkn 1thJdq3czEqvSmthv4qaSVD3DLOipzTmOnKwOsYfep36BabfVuSbJIbAk7IsFiWHeb9V bLKfqdSTWXI08j85AFD+OBSQAOnaiR4vQbZCgv8iuHqgKqpIcqasuCk+F8w4TSNp76rw kF9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307584; x=1727912384; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=extRuG4D1AdoHKvsMv2fLx/d2D/gEpHsXYLRVzC3Sa0=; b=ew9ApeTdHbNFgd1d3vp5xuPZIJKJF4xYqexjcLxGf08nNB4nA1l2dfQHcn/88d7ISa 1hN8GNNerYT6QsdPc7N0aifEozGtAC34L4TXpOPsSmXDYKBjKKODg4KAdg1rpDt7k/mg knSQuiljW81cdaV8U04M4QO9cSU/mhc5G8AxCAvuDHhrmXbGvsw6xAvEu4/KZwgO5sKd WvxJiXH5ZdJXmPwFRDufFKyE3v/F0NtmjHjSs6l8d2PPJce50GhMuUf7Amtzia3y1xsK PNs+hJ7tzS1P9JZqhrtcv4l1SzH9+7Y1uizEDesWrsw8xU7xpHVMdijFCZCuFEqNwNNY vuGg== X-Forwarded-Encrypted: i=1; AJvYcCUGMWm1xkOJ65oQvvtIIDZEx9VC4YHonb22wm62xuPfbMAZiQiTb/Zmsj+/w//ezGKbye+6Nwzi6GjefQI=@vger.kernel.org X-Gm-Message-State: AOJu0YxKUhJo6iRRP9oKSF+d+wcaBApJXJyXrArX5r5ZUH88nfk+3Cm2 92zre0gENc3dKhm/OwH8E6eNHUOjZsZucSDi59fkOcxUiU5fl5KvOfi++P3Tod1wPgqTRzLpOMO DmzfbXg== X-Google-Smtp-Source: AGHT+IHi0gSVKxF9796sY6n1cVcgNiz/fsNc836mupUZtqmyvJTlnsLOXeqwbXzuTlIf+2iUj78FZGeorFh0 X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:2a92:b0:6e2:2600:ed65 with SMTP id 00721157ae682-6e22600f35dmr247467b3.1.1727307584197; Wed, 25 Sep 2024 16:39:44 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:25 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-11-mmaurer@google.com> Subject: [PATCH v5 10/16] module: Factor out elf_validity_cache_strtab From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch only moves the existing strtab population to a function. Validation comes in a following patch, this is split out to make the new validation checks more clearly separated. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index e04a228c694a..c082d5d41a8d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2089,6 +2089,23 @@ static int elf_validity_cache_index(struct load_info= *info, int flags) return 0; } =20 +/** + * elf_validity_cache_strtab() - Cache symbol string table + * @info: Load info to read from and update. + * Must have &load_info->sechdrs and &load_info->secstrings populat= ed. + * Must have &load_info->index populated. + * + * Return: 0 on success, negative error code if a check failed. + */ +static int elf_validity_cache_strtab(struct load_info *info) +{ + Elf_Shdr *str_shdr =3D &info->sechdrs[info->index.str]; + char *strtab =3D (char *)info->hdr + str_shdr->sh_offset; + + info->strtab =3D strtab; + return 0; +} + /* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go. @@ -2122,9 +2139,9 @@ static int elf_validity_cache_copy(struct load_info *= info, int flags) err =3D elf_validity_cache_index(info, flags); if (err < 0) return err; - - /* Sets internal strings. */ - info->strtab =3D (char *)info->hdr + info->sechdrs[info->index.str].sh_of= fset; + err =3D elf_validity_cache_strtab(info); + if (err < 0) + return err; =20 /* This is temporary: point mod into copy of data. */ info->mod =3D (void *)info->hdr + info->sechdrs[info->index.mod].sh_offse= t; --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C7B91AD40E for ; Wed, 25 Sep 2024 23:39:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307589; cv=none; b=KzfFkp65/R63CMOLdGO2SDEB711KXEDGaEFqwSEZQWDI2VGx8R4s2Ww7HWsVQTNgOOx+6U/MXn716DSajHV7BeNu/Ci+dpYV/0yWUxhjJ2LJMds+6Hp2ODedr0s64SJVyZMhlRwQx4ticZnOC99men80wmkNW9h5Hz+yetNhk8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307589; c=relaxed/simple; bh=9QDYzeFqtIgpifJubbDxlmlH7eEjUZaH/2YQ84rdY0c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GH9haDa2EUeQTrZ2S+BCe291bOvwrIk5w4Qhz4EQhceUi8ibG2E5+nWWvWAkAuj5nzFOekAWt84jxr17gft3XiB3aKY/tKFVxyGyiFSDGvv07CTKDP27p2xeofQaBv17wFtqk3CCWS12KWeU88FA98/sYYc9m6nCiJLHfjusM/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OTdjCMgM; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OTdjCMgM" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e2270a147aso10320177b3.0 for ; Wed, 25 Sep 2024 16:39:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307586; x=1727912386; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6yXvv/vrTLDrEzmPTfvxgBAIug2sA8OdrjoRUJniL2M=; b=OTdjCMgM8YjxDuEHL8ypwKq7l22iOrFWtkQjFufh9dEToAFONT32xfWhlYlYyjYPzh I5yjUlBc5jn4Th5d13BoVfmIoA1V0zqYnXOcpw8JE6CfyloFRCLBJEcsnywsNqhoSqSd l+MXDGR7HPpopbirbgIsdc7HUZpIOWUK+4UXwxNw+ky3BUrtyITuuUucrkU5oFqjY2pe /VvWCgQmo4PptPK5LWwbjMRdZaRDriPq55Ltk721/L1aM9+bL7owhnDXnKTx3/3OnOEn eEXBxTZphjfst/D4skhIKnZka6ofbVS2t+NF37pF6X+HDhksD+a8UgUiSGnPZZ9dtavR fvFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307586; x=1727912386; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6yXvv/vrTLDrEzmPTfvxgBAIug2sA8OdrjoRUJniL2M=; b=ojJitMrBj1Q5IwR8EzqFfGTMkJcY+rYx5UnlBV97gJJPOey/QXs9guTiFnPCMnW1bi yLl0kvLSavCshoA4AEsgwIARqfYcfKhf7H+oov7OtpYbMTmQAz7kQ1DvEB6d7Qr+M++3 SKM/tJ3XseYe4dk+ZKHNE1uLAOiZZr3M1YEYxBg0htMOXqK+sBmFJXu0I5rlkYA6zOJE nwNMfBOlIww/1ChrI8DknhezCNNta9nkQ7KsIP7aG15vIv4OA94VWJRwaCr6mTDdXG7h IGX1URlNQVpqcK6BcWjiB/Y6IL9YLZZUGyk7Yeu6yswaXsCGI/AiZ4tkVMjlvhiKStGm fL6w== X-Forwarded-Encrypted: i=1; AJvYcCV0BfjakkIQKXAqILF4bFhCWzBZkj5+M/2F99DdYM7cw/0eQuFE1rgNb2ey+pX2mugDyqoZIdotA1CVsBg=@vger.kernel.org X-Gm-Message-State: AOJu0YxMnccHRIAPNF621hSPQhbSZCYKbBPGLzKfGIMplWIcZBFqiu5s 8/bFx6/1sra1feKaQKZgycMxV0C1mdQLfRc5AMMisyODTKZF4l+JY8/cvZcg0XlOAoUJFVpMSA+ FP+hbGQ== X-Google-Smtp-Source: AGHT+IFOctLtu2fDpWSwtgdrlWQhzeyg6ur9X7N6yKJb4yvjNshlLawCmOC09mIbp13RxLe82I6Qn8MDC9MI X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:6583:b0:6dd:bc47:1ce1 with SMTP id 00721157ae682-6e21d0b47e5mr368107b3.0.1727307586448; Wed, 25 Sep 2024 16:39:46 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:26 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-12-mmaurer@google.com> Subject: [PATCH v5 11/16] module: Additional validation in elf_validity_cache_strtab From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Validate properties of the strtab that are depended on elsewhere, but were previously unchecked: * String table nonempty (offset 0 is valid) * String table has a leading NUL (offset 0 corresponds to "") * String table is NUL terminated (strfoo functions won't run out of the table while reading). * All symbols names are inbounds of the string table. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/main.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index c082d5d41a8d..b40b632f00a6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2090,17 +2090,53 @@ static int elf_validity_cache_index(struct load_inf= o *info, int flags) } =20 /** - * elf_validity_cache_strtab() - Cache symbol string table + * elf_validity_cache_strtab() - Validate and cache symbol string table * @info: Load info to read from and update. * Must have &load_info->sechdrs and &load_info->secstrings populat= ed. * Must have &load_info->index populated. * + * Checks: + * + * * The string table is not empty. + * * The string table starts and ends with NUL (required by ELF spec). + * * Every &Elf_Sym->st_name offset in the symbol table is inbounds of the + * string table. + * + * And caches the pointer as &load_info->strtab in @info. + * * Return: 0 on success, negative error code if a check failed. */ static int elf_validity_cache_strtab(struct load_info *info) { Elf_Shdr *str_shdr =3D &info->sechdrs[info->index.str]; + Elf_Shdr *sym_shdr =3D &info->sechdrs[info->index.sym]; char *strtab =3D (char *)info->hdr + str_shdr->sh_offset; + Elf_Sym *syms =3D (void *)info->hdr + sym_shdr->sh_offset; + int i; + + if (str_shdr->sh_size =3D=3D 0) { + pr_err("empty symbol string table\n"); + return -ENOEXEC; + } + if (strtab[0] !=3D '\0') { + pr_err("symbol string table missing leading NUL\n"); + return -ENOEXEC; + } + if (strtab[str_shdr->sh_size - 1] !=3D '\0') { + pr_err("symbol string table isn't NUL terminated\n"); + return -ENOEXEC; + } + + /* + * Now that we know strtab is correctly structured, check symbol + * starts are inbounds before they're used later. + */ + for (i =3D 0; i < sym_shdr->sh_size / sizeof(*syms); i++) { + if (syms[i].st_name >=3D str_shdr->sh_size) { + pr_err("symbol name out of bounds in string table"); + return -ENOEXEC; + } + } =20 info->strtab =3D strtab; return 0; --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D90001AD9E5 for ; Wed, 25 Sep 2024 23:39:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307591; cv=none; b=GO3ozgd6FT33K335IdLyxxbSZbI9hzSv7Jttwmq8GUQqgtP0WDuR9tI+27cmmzmef+493oZVRycwROBjQTMApFqwB+sefvuoJtFC65BVclvzNzi8LF7Dq2Snb/ZtOyVVCCrB1yguGTnZnmc+MWh8Gr/kk9w8eYPeB/G6Sy1TYbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307591; c=relaxed/simple; bh=W7tU7P3GNG+a73WgyQWeP1re2H5Hhpn0LeAD9h0akfE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oaeHad6jIU/WCp63Bb6Y4ZSVFeWVqF+vtsacgKPEF7imS9as01wd0R7NLzthsBGamz2DdaLzb+9Ek1tZKeu+i/yhGKssJ2lG2jB2HplAbHLcHAcQZ5eWT/kzXI8ZQGMTEGo5pfEz0+jBmbWjrTdVgCBIqnFewDGf3LTr7GUikf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PY6Kja1y; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PY6Kja1y" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e22f0be237so9001347b3.1 for ; Wed, 25 Sep 2024 16:39:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307588; x=1727912388; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t2yLoLuY3hdxUDQj4FAKHilbBg2KnzEPjHr+p4Rv35M=; b=PY6Kja1yO8FnJye7bHsyQXHe4y1e4dfN3dHXxgTZkfBU0quAuB+SylDK6cVtwedwgf 3H48Hrh/I5tNAO5dWh64Db7Tov4QBC8jLuPu36yeMrKswEuAsYL8tj2wu5bTG5nqiwjz eLk8/CRbto8lTlXKPfeyiAPrK9vVaYvGYMhnPQVW4EWpfGixMu14TXcRHAo0kEQHRMVy MQe3GzHPK8KU8gzuywJ73078/Ny9zUI9wHtpSute0IM8QOpeWiyu+PfR1J5wjR/lWNdR 6cjB8zpWx5Hk81ano1+ywkNJuJHVMIYea+V26Dyx0v6KiI9MPOjjydEIgQDIo29Z+Hio q08w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307588; x=1727912388; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=t2yLoLuY3hdxUDQj4FAKHilbBg2KnzEPjHr+p4Rv35M=; b=t0n8mFipaTmt1Mi39wVpJHq6QRKO8eNxmvtak2G8OyRbvtUo4R7xq3Np+fwOoXSSwr BsHSb/KiHaV443Jy81xgxFVeDkawkzpwQVSQUu2CB6W6p4/rAOwTBmMIO+uNT+DA9f/r BFqrPGw/xC49kDX38ihuCVHe5TvGfN4Bx8UJJfFbfHZ9pZr13RNUV/AhAwTrLzo6Y8eb esM+nsT3ohWacmHZk8EuOGC8MFcvcFJcrzroX4VslG8tN08zkT6zcLXzAYJRtqvZV7tT g5kPgFxzVcp3BwuLE5jPHRAginRMcKmBct0m/rlGmDhpIyYoWZHdE2BumOMoKJe7LeTg lNzQ== X-Forwarded-Encrypted: i=1; AJvYcCUA87qRGp1H3IBLVvmew9N9fYaEIjiaQRM5K29mMtH5kN3nSSotkt+0GgwlCbo4TDXb4OQZbqoE9lIREGI=@vger.kernel.org X-Gm-Message-State: AOJu0YyC0Dd5Pw2B0d7GcQeaDEiNBjfhceaPINdVR3Blzu1zJbxXBrFH /TP3c7V2SV/yaUug5aYJt61jcBVymZyvoZJ4j5XxmZe7CANTJ0VxQJwTrmryL9FicHeRIUKt8GW oKFoTsQ== X-Google-Smtp-Source: AGHT+IGo3KQ6k/CMKWp9luhAJxulkW1/EkZDzA8kfvweM/2fd5g67sil+zl5Hoq7sgFCeukL28HlOifIuPFz X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:b120:0:b0:e22:624b:aea2 with SMTP id 3f1490d57ef6-e24d46b5e11mr4072276.0.1727307588565; Wed, 25 Sep 2024 16:39:48 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:27 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-13-mmaurer@google.com> Subject: [PATCH v5 12/16] module: Reformat struct for code style From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using commas to declare struct members makes adding new members to this struct not as nice with patch management. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- kernel/module/internal.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2ebece8a789f..daef2be83902 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -80,7 +80,12 @@ struct load_info { unsigned int used_pages; #endif struct { - unsigned int sym, str, mod, vers, info, pcpu; + unsigned int sym; + unsigned int str; + unsigned int mod; + unsigned int vers; + unsigned int info; + unsigned int pcpu; } index; }; =20 --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1BF11A4B99 for ; Wed, 25 Sep 2024 23:39:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307598; cv=none; b=WPSk11pDhInSWxISagqpP0kKeTso1rkqC8lJeUuOXuFdvafFPcPHcYpFYvC38KNXtP+EjATHQOHxpQmAv/Iw1Xq6MrQ5FGTEKwSpN2HdKpoNSowVvf4LS17G/H1+M9PHtr8ZL3lpxAV3XAiLGSNGdI1dVzkq6NKJEzEWK7Rx6uk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307598; c=relaxed/simple; bh=KqzbIRZaiTcASB5/z7j1pBJp9u/nLEzrmBQfqdwdS+0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r3xqYZwoacdnKcw/C8OiBD7TQRo63XKSDwvTE3ZhAGhmblIj8XLZQNX5Zk6kZFvWkA5SRFqoGG3geU+d6v9QwxFY2EYn0LNMMEytp6KxgfpXxHuzYO/C7i+KdY+veBQMh5vCVxwJGowu40GDk6uMiQZS8Dav8qtw4t4tniRHp6U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oKP1Z8NY; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oKP1Z8NY" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e230808464so6353457b3.3 for ; Wed, 25 Sep 2024 16:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307596; x=1727912396; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=c9tQH+085gbT8g6utnUHSGPzzhaq/BvRPx+qqG98o3U=; b=oKP1Z8NYzCW3wdrGLbSd0+z4YaAtfoT+55NhpDw9t6rLvl0jWbL70Dl2B/Kh5tmKYJ G2GupxkGy83tyRGyRSwtyT2FpBXLIt2wWabdNX8ecp8Q2pvr7ur+V5PCfEkE/+QQAfmD doEyyE0OPuqskrD0GERU3wJK2bYsEcRh+wphK+xOCG4PE7A5T8fic6AcJjgLfY1bIffK /CbN1lGPMw30iGanZXxYrQ0ALBD+BIzgPamajUHMsFR/lRfTlwLxJXbYLLPl0Dr0HhEc SppOLafq9h5SiDULiY5MytW1aEBXpqMqaP3MuoJrIfc3QQdNjcXXYfo484YrusBlvFOK t6YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307596; x=1727912396; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=c9tQH+085gbT8g6utnUHSGPzzhaq/BvRPx+qqG98o3U=; b=sDo6b9v37uzdZlTlIBbhztrEgJ4OwSvcT020INX7NEMDvsd6hLCOEmrJchJBRMG46H YZlXX4Sx2LAHxu6A3qp8iKRgObPb/a3g17P2K/JfDa2+ek7ZPUVw8+bvHe4ysh/Vg+sM utK0dUQXMmd395qOIxk0SYfxlVnc4AjiaquVDHkXQaMLe2cSEnKlen1xd2z6m7kRHqqA EHIKp1ehGbFyFNuZpHOvM56ATD5+m3e8EFgvr6QS6WEQhjgFjOptO7sqpPPz40SmTmSe 1HLuU9u0i+mdreLVrAXbr4p1RfLYonKX1h49UHJJX8fhXq+kMSuoqqE2PrwhbUeQPNNT sbEQ== X-Forwarded-Encrypted: i=1; AJvYcCXdnGfkU+vhJgknCVgscytYRYa/C03+PgwyTApC+f7ZbJvz5nmjNZAbkun9wyJfZMsI1k5PYSVN7N1ORJI=@vger.kernel.org X-Gm-Message-State: AOJu0YxZjBivUYmZvWiOwKm4hGZZQS3vricUbOdPrwmA5gLV550T1uKX jvoAvB18gz1pW6HLsKgYppJED3i4FwC2WT5m8OQiYwxqk2xf9/9sVwbjrXDjq7nKbkvLc7l8mWd Y3U5sxw== X-Google-Smtp-Source: AGHT+IHf83nSsh8dSI5MT6Lq2qwz18kf/IKNLkJKmhvyxAcTbUnK8QftG83szzLWCAGb7csPDeD/aKPeenOu X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82063276.2.1727307595844; Wed, 25 Sep 2024 16:39:55 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:28 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-14-mmaurer@google.com> Subject: [PATCH v5 13/16] export_report: Rehabilitate script From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor , Matthew Maurer Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" * modules.order has .o files when in a build dir, support this * .mod.c source layout has changed, update regexes to match * Add a stage 3, to be more robust against additional .mod.c content Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- scripts/export_report.pl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/export_report.pl b/scripts/export_report.pl index feb3d5542a62..dcef915405f3 100755 --- a/scripts/export_report.pl +++ b/scripts/export_report.pl @@ -55,6 +55,7 @@ sub collectcfiles { open my $fh, '< modules.order' or die "cannot open modules.order: $!\n= "; while (<$fh>) { s/\.ko$/.mod.c/; + s/\.o$/.mod.c/; push (@file, $_) } close($fh); @@ -120,10 +121,14 @@ foreach my $thismod (@allcfiles) { next; } if ($state =3D=3D 1) { - $state =3D 2 if ($_ =3D~ /__attribute__\(\(section\("__versions"\)\)\)/= ); + $state =3D 2 if ($_ =3D~ /__used __section\("__versions"\)/); next; } if ($state =3D=3D 2) { + if ( $_ =3D~ /};/ ) { + $state =3D 3; + next; + } if ( $_ !~ /0x[0-9a-f]+,/ ) { next; } @@ -133,7 +138,7 @@ foreach my $thismod (@allcfiles) { push(@{$MODULE{$thismod}} , $sym); } } - if ($state !=3D 2) { + if ($state !=3D 3) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; } --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B1F71A4AA1 for ; Wed, 25 Sep 2024 23:40:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307608; cv=none; b=Qx/mSNRRVW2wSH3CxrsCUxbc/2yjxmTX9ReHyzdBRXW21NoVN72AoA4/ywyZGwvHiAuLo4Ns+4ibWdNRvo53Ov9EIaJ4R2ERQmi+Q19TkA1/M9xpFx599kG/m/bPBASD9YUlpHhmfwsnD8V7NfP6pyzu7VPTXiUGork/a2gPnOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307608; c=relaxed/simple; bh=2eETH+bQv7kJed3DaM5YjE+D9ntyBg0gUQUR8ngWEMQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CHmTdbKQ2qW/oe8xhTRBhwM7nj1iThUitnTs4N1114xhH0eTSNEP1jnDUBgQ5rnPwhLMj0vSRyaVH7wFlAhKNw+vmLjxb0ghk/2eExKoydTJX2dmk6hfxmv9BlHJAwnMz+WA5HvRkSaxMcuCRl73zDp+mkfgEh8jIYmBrpS/+NQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2iR2qP/y; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2iR2qP/y" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e2261adfdeso9741687b3.2 for ; Wed, 25 Sep 2024 16:40:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307604; x=1727912404; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qRfAN3nQHQDKwo/EaN0h5CCoEGoDY7NA47N7/1nCHZE=; b=2iR2qP/yDlLLsNZtgkDXkX5S314JbEOa9P2kZKbM6mgkNtfEtDliK2hugHMY6j1YAp f6aZNxF/BoXMQBDKB4V/eAkNl7k/9cHNaDfl6ePmcC9VIPeGQEBALQB0Jjwl4FlHEu4o TXk0dld9TNCnw+Oa1lteSxKiPUrzlUuFBYs/zDih+00Hn4oWFzy60e87JFiw53cvySyy 3d/nahQYvG5yeLQ+0VYhoGQZKRByI97Ratb2NBW61hXWLadpJWF4Rj2XpG82jDkqhol+ xvUWRLO8S58lLA909Uj8HL9/CrorcWykb7lyjneiJOUmMnuqdjFqNelW/Av1ctgxZE4c gkiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307604; x=1727912404; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qRfAN3nQHQDKwo/EaN0h5CCoEGoDY7NA47N7/1nCHZE=; b=c8pjv0XuM7J4q+7auIa8B70gTIjZLKxw9ePYQvJDKR8Ow8Yjq54PYJxhB7HZN32hR0 mZPxRVnl9K/tUkrvzbMRDFlj5rMZMT55PIu06lxNhyzllYxtOzlar3zOE+TUa/wPkafM 4fjHVo29upzGJEU18r5la1wmOG6OIj2UEA8q4sVdToWyOTH15rdQW55zF8EyMBrBOVkP 9ztOwnMIrIjF1W+xQwFOqsOwf0eM3KcX18umQdYAcArqowa3wn6SHAEgSntLy9HdX/1y rPubnncPnJTkpaLv9La6dYklqkgr40lr3j5EjDFJ2yPGLD7RbLmLMpVhgj6SODNkcZgk 8zOQ== X-Forwarded-Encrypted: i=1; AJvYcCWL5PAcP14HfkPsxmyujmDPee3s7pi2a9MkU3Zso9hmsfTTRDCT6JKulvenqZmDZzibemQMxDM7mp094zY=@vger.kernel.org X-Gm-Message-State: AOJu0YyMoqstejXVtf24K8UDZ66if9rA4fL3aCwJkJZjQf2fSh9pJhyB PPVKRdyEniys1K6x4JB22keiTdEqc1s7hf5xHBf+1UM92Og0NKzzJVJSG0f6tnUmcuwkTscHt8/ v0+lj6w== X-Google-Smtp-Source: AGHT+IGmuyyop25FpZ1x0NuUxB8uoOi/RaucBwgtD94Sl8/jMpFz75dImfgJY5lpsN7Qk8pZTSNyBUlZTG5H X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:690c:20a7:b0:6e2:2c72:3abb with SMTP id 00721157ae682-6e22c723c48mr192977b3.7.1727307603320; Wed, 25 Sep 2024 16:40:03 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:29 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-15-mmaurer@google.com> Subject: [PATCH v5 14/16] modules: Support extended MODVERSIONS info From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Michael Ellerman , Alex Gaynor , Benjamin Gray , Naveen N Rao , Matthew Maurer Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Nicholas Piggin , Christophe Leroy , Madhavan Srinivasan , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , linuxppc-dev@lists.ozlabs.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer). Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- arch/powerpc/kernel/module_64.c | 23 ++++++++- kernel/module/internal.h | 11 ++++ kernel/module/main.c | 92 ++++++++++++++++++++++++++++++--- kernel/module/version.c | 45 ++++++++++++++++ 4 files changed, 161 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/kernel/module_64.c b/arch/powerpc/kernel/module_6= 4.c index e9bab599d0c2..4e7b156dd8b2 100644 --- a/arch/powerpc/kernel/module_64.c +++ b/arch/powerpc/kernel/module_64.c @@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *= vers, } } =20 +static void dedotify_ext_version_names(char *str_seq, unsigned long size) +{ + unsigned long out =3D 0; + unsigned long in; + char last =3D '\0'; + + for (in =3D 0; in < size; in++) { + /* Skip one leading dot */ + if (last =3D=3D '\0' && str_seq[in] =3D=3D '.') + in++; + last =3D str_seq[in]; + str_seq[out++] =3D last; + } + /* Zero the trailing portion of the names table for robustness */ + memset(&str_seq[out], 0, size - out); +} + /* * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. * seem to be defined (value set later). @@ -424,10 +441,12 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, me->arch.toc_section =3D i; if (sechdrs[i].sh_addralign < 8) sechdrs[i].sh_addralign =3D 8; - } - else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")=3D=3D0) + } else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") =3D=3D = 0) dedotify_versions((void *)hdr + sechdrs[i].sh_offset, sechdrs[i].sh_size); + else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names") = =3D=3D 0) + dedotify_ext_version_names((void *)hdr + sechdrs[i].sh_offset, + sechdrs[i].sh_size); =20 if (sechdrs[i].sh_type =3D=3D SHT_SYMTAB) dedotify((void *)hdr + sechdrs[i].sh_offset, diff --git a/kernel/module/internal.h b/kernel/module/internal.h index daef2be83902..59959c21b205 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -86,6 +86,8 @@ struct load_info { unsigned int vers; unsigned int info; unsigned int pcpu; + unsigned int vers_ext_crc; + unsigned int vers_ext_name; } index; }; =20 @@ -389,6 +391,15 @@ void module_layout(struct module *mod, struct modversi= on_info *ver, struct kerne struct kernel_symbol *ks, struct tracepoint * const *tp); int check_modstruct_version(const struct load_info *info, struct module *m= od); int same_magic(const char *amagic, const char *bmagic, bool has_crcs); +struct modversion_info_ext { + size_t remaining; + const s32 *crc; + const char *name; +}; +void modversion_ext_start(const struct load_info *info, struct modversion_= info_ext *ver); +void modversion_ext_advance(struct modversion_info_ext *ver); +#define for_each_modversion_info_ext(ver, info) \ + for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_= advance(&ver)) #else /* !CONFIG_MODVERSIONS */ static inline int check_version(const struct load_info *info, const char *symname, diff --git a/kernel/module/main.c b/kernel/module/main.c index b40b632f00a6..9a9feca344f8 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2039,6 +2039,82 @@ static int elf_validity_cache_index_str(struct load_= info *info) return 0; } =20 +/** + * elf_validity_cache_index_versions() - Validate and cache version indices + * @info: Load info to cache version indices in. + * Must have &load_info->sechdrs and &load_info->secstrings popula= ted. + * @flags: Load flags, relevant to suppress version loading, see + * uapi/linux/module.h + * + * If we're ignoring modversions based on @flags, zero all version indices + * and return validity. Othewrise check: + * + * * If "__version_ext_crcs" is present, "__version_ext_names" is present + * * There is a name present for every crc + * + * Then populate: + * + * * &load_info->index.vers + * * &load_info->index.vers_ext_crc + * * &load_info->index.vers_ext_names + * + * if present. + * + * Return: %0 if valid, %-ENOEXEC on failure. + */ +static int elf_validity_cache_index_versions(struct load_info *info, int f= lags) +{ + unsigned int vers_ext_crc; + unsigned int vers_ext_name; + size_t crc_count; + size_t remaining_len; + size_t name_size; + char *name; + + /* If modversions were suppressed, pretend we didn't find any */ + if (flags & MODULE_INIT_IGNORE_MODVERSIONS) { + info->index.vers =3D 0; + info->index.vers_ext_crc =3D 0; + info->index.vers_ext_name =3D 0; + return 0; + } + + vers_ext_crc =3D find_sec(info, "__version_ext_crcs"); + vers_ext_name =3D find_sec(info, "__version_ext_names"); + + /* If we have one field, we must have the other */ + if (!!vers_ext_crc !=3D !!vers_ext_name) { + pr_err("extended version crc+name presence does not match"); + return -ENOEXEC; + } + + /* + * If we have extended version information, we should have the same + * number of entries in every section. + */ + if (vers_ext_crc) { + crc_count =3D info->sechdrs[vers_ext_crc].sh_size / sizeof(s32); + name =3D (void *)info->hdr + + info->sechdrs[vers_ext_name].sh_offset; + remaining_len =3D info->sechdrs[vers_ext_name].sh_size; + + while (crc_count--) { + name_size =3D strnlen(name, remaining_len) + 1; + if (name_size > remaining_len) { + pr_err("more extended version crcs than names"); + return -ENOEXEC; + } + remaining_len -=3D name_size; + name +=3D name_size; + } + } + + info->index.vers =3D find_sec(info, "__versions"); + info->index.vers_ext_crc =3D vers_ext_crc; + info->index.vers_ext_name =3D vers_ext_name; + return 0; +} + /** * elf_validity_cache_index() - Resolve, validate, cache section indices * @info: Load info to read from and update. @@ -2053,9 +2129,7 @@ static int elf_validity_cache_index_str(struct load_i= nfo *info) * * elf_validity_cache_index_mod() * * elf_validity_cache_index_sym() * * elf_validity_cache_index_str() - * - * If versioning is not suppressed via flags, load the version index from - * a section called "__versions" with no validation. + * * elf_validity_cache_index_versions() * * If CONFIG_SMP is enabled, load the percpu section by name with no * validation. @@ -2078,11 +2152,9 @@ static int elf_validity_cache_index(struct load_info= *info, int flags) err =3D elf_validity_cache_index_str(info); if (err < 0) return err; - - if (flags & MODULE_INIT_IGNORE_MODVERSIONS) - info->index.vers =3D 0; /* Pretend no __versions section! */ - else - info->index.vers =3D find_sec(info, "__versions"); + err =3D elf_validity_cache_index_versions(info, flags); + if (err < 0) + return err; =20 info->index.pcpu =3D find_pcpusec(info); =20 @@ -2293,6 +2365,10 @@ static int rewrite_section_headers(struct load_info = *info, int flags) =20 /* Track but don't keep modinfo and version sections. */ info->sechdrs[info->index.vers].sh_flags &=3D ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_crc].sh_flags &=3D + ~(unsigned long)SHF_ALLOC; + info->sechdrs[info->index.vers_ext_name].sh_flags &=3D + ~(unsigned long)SHF_ALLOC; info->sechdrs[info->index.info].sh_flags &=3D ~(unsigned long)SHF_ALLOC; =20 return 0; diff --git a/kernel/module/version.c b/kernel/module/version.c index 53f43ac5a73e..c246d4087970 100644 --- a/kernel/module/version.c +++ b/kernel/module/version.c @@ -19,11 +19,28 @@ int check_version(const struct load_info *info, unsigned int versindex =3D info->index.vers; unsigned int i, num_versions; struct modversion_info *versions; + struct modversion_info_ext version_ext; =20 /* Exporting module didn't supply crcs? OK, we're already tainted. */ if (!crc) return 1; =20 + /* If we have extended version info, rely on it */ + if (info->index.vers_ext_crc) { + for_each_modversion_info_ext(version_ext, info) { + if (strcmp(version_ext.name, symname) !=3D 0) + continue; + if (*version_ext.crc =3D=3D *crc) + return 1; + pr_debug("Found checksum %X vs module %X\n", + *crc, *version_ext.crc); + goto bad_version; + } + pr_warn_once("%s: no extended symbol version for %s\n", + info->name, symname); + return 1; + } + /* No versions at all? modprobe --force does this. */ if (versindex =3D=3D 0) return try_to_force_load(mod, symname) =3D=3D 0; @@ -87,6 +104,34 @@ int same_magic(const char *amagic, const char *bmagic, return strcmp(amagic, bmagic) =3D=3D 0; } =20 +void modversion_ext_start(const struct load_info *info, + struct modversion_info_ext *start) +{ + unsigned int crc_idx =3D info->index.vers_ext_crc; + unsigned int name_idx =3D info->index.vers_ext_name; + Elf_Shdr *sechdrs =3D info->sechdrs; + + /* + * Both of these fields are needed for this to be useful + * Any future fields should be initialized to NULL if absent. + */ + if (crc_idx =3D=3D 0 || name_idx =3D=3D 0) { + start->remaining =3D 0; + return; + } + + start->crc =3D (const s32 *)sechdrs[crc_idx].sh_addr; + start->name =3D (const char *)sechdrs[name_idx].sh_addr; + start->remaining =3D sechdrs[crc_idx].sh_size / sizeof(*start->crc); +} + +void modversion_ext_advance(struct modversion_info_ext *vers) +{ + vers->remaining--; + vers->crc++; + vers->name +=3D strlen(vers->name) + 1; +} + /* * Generate the signature for all relevant module structures here. * If these change, we don't want to try to parse the module. --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E74321A4B99 for ; Wed, 25 Sep 2024 23:40:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307609; cv=none; b=Yjv2GgkQzroP0R23M23WpgHBHjUnwTautw7t45osPeYQ5llQIXyVG803U1uiaCnuSP4ZHD1pcHwhW5dT770s3j8dVunx7v0Uvr20P7nKyy35+ROvBmhGju+xUxlen1c8Mrd1aF3QigsFy+9x0qq+5/RyDjEXxpAmtgeV/niboZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307609; c=relaxed/simple; bh=HByXTJfGaacs4MV7ivXCvJq3lGnRepYcPD/jPTnE9OU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kOZtkmHePqPezug1cjtjVbP38SFhpR8OGWiX+TyrNKU8f6VQ9NMTmdugGCDPaBDV2oQA7PlN9GlM5NmaDlmVZFokB2IjCo3C9RyyopTRHltEf5tUNGpZyGZ51vmzJNfUVw01YhCRPS2fCwGnbR+Ar4p1hDevnAqf4XP8ucfRpxE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1SuUZMLC; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1SuUZMLC" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6db791c42e3so7873327b3.1 for ; Wed, 25 Sep 2024 16:40:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307607; x=1727912407; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RglsluUoLriSL7HbUF8axxP+liCt7+SgicjK27E/sOo=; b=1SuUZMLCLU82vqpwJ2mBACpsNRBNdC6WVgRnSbUsvKFPxY7L7MPK+yD6KEXXfewbGy 2IlShZajn1rcm4+Adt4FxnGW3f8lAaVwWTOvCgLyz4lO80El+dumIJ5vxIPD6tmYdPmT IMI3YebIeq3Y99RT7LKLtANVjjuWVoW6yoISfA7j4SPLgTKRTU9wL08lQDX9W+vKXrhG Db6PoovDNizNI94JlyD676F7yGDuXQY2UQvVpdGALp+JbBfwrqrSEZMrivm/JTHAtx7t OcWJSv3aWbGd6GN2g4L7o9O40Yc1ANrd8kOlS5e3fr9INjBgQxOPt+Z5QkgtJ8wOA5Xm 7dYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307607; x=1727912407; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RglsluUoLriSL7HbUF8axxP+liCt7+SgicjK27E/sOo=; b=VoWNq3Co2GiWsStCzUQrjMWTtyLEa2PaEXHzYkILAKwoKL4KgqZM5Sj2xZ40tyAqEY emPFG30Mc1DerhimsbaJZZYymlvPq3TNRsqXb2qOvykPY2y8cI28QZiGF4U4n0t4roUU b/lcRlNPMRxXwXh+U6BEROsNtGU24v07N1x0xIq+OMQlfuxdB1fweNpdRiJNGVQDlGqo JusOBCTG5uKRQv+fbh4MjcbB9u/lc9cONuYicX7L8gRjqDxoiG9O5vTWivVf1zj/p6tz g7jNYDL3KOLRya7wUE1b52Wej/eYtMmLchD5Gq2Aei7UqhGYfrqVBGagEGmvx+QeuHHJ vDcw== X-Forwarded-Encrypted: i=1; AJvYcCW3Duj8AwCnK8rBIQzQrJQnknxeR6vKzsKz5jT4EX8mWaauaZIXlyBILibqBGo4UFJRtmrV0FVoCQzyYoQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/UTo1yMf7fckMCRT44bl1ZhXtesvih7Usd+cTjFoEL8xTjIGs DYVzuS0dcrpDWEhax72Uv4FRopOK33kZRsEbNRKncXd/C5MLBQCX+SaySCkqK9obwEfCCiNB6J0 U781Ejg== X-Google-Smtp-Source: AGHT+IGASCvkEn2P1gDzoqsewJkQJHaAHK03+0AmWd/xawb1XFk5R/bQgX0n7dG9j+urvSvhJWBpQ1ZaL5iI X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a05:6902:118f:b0:e24:9584:52d3 with SMTP id 3f1490d57ef6-e24d71625d8mr82065276.2.1727307606698; Wed, 25 Sep 2024 16:40:06 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:30 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-16-mmaurer@google.com> Subject: [PATCH v5 15/16] modpost: Produce extended modversion information From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Matthew Maurer , Nathan Chancellor , Nicolas Schier , Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Generate both the existing modversions format and the new extended one when running modpost. We no longer generate an error on long symbols in modpost, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- scripts/mod/modpost.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index 107393a8c48a..f8b7b793d2a2 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1840,15 +1840,48 @@ static void add_versions(struct buffer *b, struct m= odule *mod) continue; } if (strlen(s->name) >=3D MODULE_NAME_LEN) { - error("too long symbol \"%s\" [%s.ko]\n", - s->name, mod->name); - break; + /* this symbol will only be in the extended info */ + continue; } buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); } =20 buf_printf(b, "};\n"); + + buf_printf(b, "static const s32 ____version_ext_crcs[]\n"); + buf_printf(b, "__used __section(\"__version_ext_crcs\") =3D {\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + */ + continue; + } + buf_printf(b, "\t%#8x,\n", s->crc); + } + buf_printf(b, "};\n"); + + buf_printf(b, "static const char ____version_ext_names[]\n"); + buf_printf(b, "__used __section(\"__version_ext_names\") =3D\n"); + list_for_each_entry(s, &mod->unresolved_symbols, list) { + if (!s->module) + continue; + if (!s->crc_valid) { + /* + * We already warned on this when producing the legacy + * modversions table. + * We need to skip its name too, as the indexes in + * both tables need to align. + */ + continue; + } + buf_printf(b, "\t\"%s\\0\"\n", s->name); + } + buf_printf(b, ";\n"); } =20 static void add_depends(struct buffer *b, struct module *mod) --=20 2.46.1.824.gd892dcdcdd-goog From nobody Fri Nov 29 01:36:27 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 17F4C1A4AA1 for ; Wed, 25 Sep 2024 23:40:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307616; cv=none; b=giiJV5n9NvlwHAEGJYMWo6e+Shz7tF67n04Gn9VrS2q7NCljdktiYWZWRo4dwkitDsol/NG2eAYn4LXg5GvD3i0M8bzezXsMUydXAE7n62IWjlJEkb7t98lTVj7moK3waxsmtJrZ5TnoAc8f0RIW4fkzoGHEV3O+RDnUyV4bLWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727307616; c=relaxed/simple; bh=CxcOGYVl0RHNxURp1Y+1xpXbMOZLvm56O9AP4mZATd8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fHTqB59ddl84fCZriN42Km7Mk80h7SWUNx+5NOXvJ9mGVTG+l8dZ4nWTKeq/p+U5vxg0LK35fUNfJ87wDPmrJ6aVsiPU3d1zVTsiItYUiKyloEoXzzU7q2dJMutqE8kj1Onh1lCqz/xadwNTI/TEhG3slt0ZdpkSIiLTuTit0DU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aRV+NzUG; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aRV+NzUG" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e02fff66a83so696138276.0 for ; Wed, 25 Sep 2024 16:40:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727307614; x=1727912414; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WplRKYGF+6Vdtojy3WxnxZnhza95+GyPbQDFLT+DHQA=; b=aRV+NzUGcQL4/ON7uIiCHk8jIbKQL9jQ5QyJkiVpL7vm2kJlCNj8gzPruXe8klTwfl Zi+BHkx2vbPtz78f4TDt5anS2Wb6eEdqIdUyvVzVfcbpCj5vW7Ytkrmb0NHShIm4LFif mq9PTj2fjOJp25iZVq0OpBa/MHbgZ1+rkgh8eMPwdH+Pl/ucPhCprx30plQsooIQxkTK rev4RuTRDiegtTCAXbdUBKvwwXR25AMOo+wD7W/JQYl6j2ObteqKMNwyBCxh9Uddo0yT zi+UVJj3nm7Lnk8rCO571Jzwo1xeSNreuGNM47rKJH6Y/gJqnbLPJFnHp298w9R+Hau8 kKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727307614; x=1727912414; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WplRKYGF+6Vdtojy3WxnxZnhza95+GyPbQDFLT+DHQA=; b=CTSu657njgLjiZ2A0leyoChfG+KXjGATZcW8hKejkviXNduEV8ZoLo1/5KMjmYSaBp 7HgENngiIEqgw/cTlm6nPgJ9gahGFGkAM2O8xgLZlEAYY/geYEYm4I38xkdCemLIfAF2 Z1BysA8fLpB3fZH836FgD5j6aXbg5NsTPEDl0a6ZuM01EVSnRhy7odFhreLYBhrxbeUz WXurn+GtMPYnvZNHp9S3mOVTgU4A8eALlKKczBbLoJuNKhRimAAfBF3ZBSb19dl8XCKa 99vAKKktNtfCaQ2NfGytX2kJ4J4lmfdXsQU7ezO6s8gNonyW4JaFTqBLlVGaaUu9pEZ8 b/KQ== X-Forwarded-Encrypted: i=1; AJvYcCWe+oBymm9fEb8fws/yrdS5rsQmvGKvkUC47R+mY3jIVWZMhTT7Bqs6EYspSdnzDQr9nkKqM3uERlPHv3I=@vger.kernel.org X-Gm-Message-State: AOJu0YxfyarBKLd4b/3TwYTjb48iGLhHPboKkKqx2BXUzbEdS42GR0e6 jhQTb+QbrNVi1v44ri/LXdSyPUBUR4K41qcow+7yVFO4ACc5QLnmRLNvuRi1z58uG7N/4lUHEh8 cx88DrQ== X-Google-Smtp-Source: AGHT+IEf40pL03JKKHoArutuPYYFcfXs6+lqlBIsFYP4JGQ4Mtdii+GrGo8PKjLJPO4DM/u/M2ebjWWjQD3t X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:780e:0:b0:e16:6e0a:bb0b with SMTP id 3f1490d57ef6-e24d44cad51mr4791276.0.1727307613856; Wed, 25 Sep 2024 16:40:13 -0700 (PDT) Date: Wed, 25 Sep 2024 23:38:31 +0000 In-Reply-To: <20240925233854.90072-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240925233854.90072-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240925233854.90072-17-mmaurer@google.com> Subject: [PATCH v5 16/16] export_report: Use new version info format From: Matthew Maurer To: masahiroy@kernel.org, ndesaulniers@google.com, ojeda@kernel.org, gary@garyguo.net, mcgrof@kernel.org, Alex Gaynor , Matthew Maurer Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, neal@gompa.dev, marcan@marcan.st, j@jannau.net, asahi@lists.linux.dev, linux-modules@vger.kernel.org, Boqun Feng , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The new version info format has a superset of symbols in the old format. Since this is a tool for in-tree modules, we don't need to parse the old one with this tool any longer. Signed-off-by: Matthew Maurer Acked-by: Neal Gompa Reviewed-by: Sami Tolvanen Tested-by: Sami Tolvanen --- scripts/export_report.pl | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scripts/export_report.pl b/scripts/export_report.pl index dcef915405f3..6a37df6f947f 100755 --- a/scripts/export_report.pl +++ b/scripts/export_report.pl @@ -114,31 +114,29 @@ foreach my $thismod (@allcfiles) { } =20 my $state=3D0; + # State map: + # 0 - Looking for names + # 1 - Scanning names + # 2 - Done while ( <$module> ) { chomp; if ($state =3D=3D 0) { - $state =3D 1 if ($_ =3D~ /static const struct modversion_info/); + $state =3D 1 if ($_ =3D~ /__used __section\("__version_ext_names"\)/); next; } if ($state =3D=3D 1) { - $state =3D 2 if ($_ =3D~ /__used __section\("__versions"\)/); - next; - } - if ($state =3D=3D 2) { - if ( $_ =3D~ /};/ ) { - $state =3D 3; - next; - } - if ( $_ !~ /0x[0-9a-f]+,/ ) { + if ( $_ =3D~ /;/ ) { + $state =3D 2; next; } - my $sym =3D (split /([,"])/,)[4]; + $_ =3D~ /"(.*)\\0"/; + my $sym =3D $1; my ($module, $value, $symbol, $gpl) =3D @{$SYMBOL{$sym}}; $SYMBOL{ $sym } =3D [ $module, $value+1, $symbol, $gpl]; push(@{$MODULE{$thismod}} , $sym); } } - if ($state !=3D 3) { + if ($state !=3D 2) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; } --=20 2.46.1.824.gd892dcdcdd-goog