From nobody Fri Nov 29 04:37:59 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 A06BC1AB520 for ; Tue, 24 Sep 2024 21:20: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=1727212842; cv=none; b=ecEXKXb4Hc5x/n8qexRY8ZIzbg3oOA9UAM1vmgJQjMtxlqA2f94FFHu/2A/nk1F56MXXrv/Cd6rDE+JmNDtAKVQUkkATZMZCCSIOnPZ51EfJVxJYXgOmx+YkRteiUfCpe1FuCznqqKo+QW6rEMBWI6wp+6u+PLdBOrphfTXbGmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727212842; c=relaxed/simple; bh=iCsuIDpsaG//12LUTpbXOFHaJGeJHbd09pACcn3fot0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j1WQTRTgNlAQADiS9jUAQO4m0K6QMs1L7n7DczY+csUJlt967wOBq4E01ZRyVCyty6YINDrRBYz8ZLVyzatTqSsMwDfu+HpA3Ftw5xG9WIuflC9vN8X12+mFzBLfytCSrsRYKBu+P6jC2rLHrm0GjcO8OUXvAg1/kGy9kbW1+Pk= 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=tK84PXvc; 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="tK84PXvc" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e24a31ad88aso811637276.1 for ; Tue, 24 Sep 2024 14:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727212839; x=1727817639; 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=tK84PXvcedPzug9FQsQEBP2m/XxzsAr/ghVMJkDUl1dxpc/kxplVVz7/eV+VZ1JWVa 8QrYovJifWWeeygufm4DDonm9t0mMUFCR1+xogixmskYLI/dDah80LvKPxUNns/Zr0x9 SnhEj6i2Cgreu1cG9Fp3lSZrnPQLLV67I/lTrXfrGEG9IIgzqGExldgqUFjlUcJF/Zsj 4MWRtle3B+TiXOYdNVOluotvul6VpL8YqSZE7R8H4sbyzu3KcO++grh+v+RqzFdLew/o v1kMV+1auqfJh8osuY+HpAV9LIHj3wPelOdOeBLGya2bwyuuKjtYW1aF5wblD1e3ZSP3 2XvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727212839; x=1727817639; 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=XqYYq31H16WPshKM7yNO6i3mWF5lZTTHcbv6S1xO0QGgU8sHQ8hyUmQpHokm/FVxEn e6RH1l2vTyySrafvjHejgJOtGaYXToUc9Dr5uRIGG0oFcmNqOU9N75wxwBm89NV5kh98 70HRQR+8ZLBixLlA40r1vgX7DEITBo2gtAdzGsMOkNUK2TJgDgTZbY39dqG7TPw5qnTn qqOm+PXo2mPDQ1Ck8CXZifGw42GIAlfocy9diDISnTRIvwS2mqO3/6UFGBv3SbLLZ48+ DsFIrZZGSw4gZyPHmqY+GQUm/pCFqGfUdIR7rPusOBARyw2P4StwoSGKbzX+M+Poq3kb Vu1A== X-Forwarded-Encrypted: i=1; AJvYcCUEd8Q2H+uh5lJcw1dW7SdCZjXxss6QH4TXwJiS6KVsPmSOCkqcueeZflOlvCuD69c6oA6CZGCKcIbTygo=@vger.kernel.org X-Gm-Message-State: AOJu0YyXlvQWIi0tUkdvlV3Hr44pQ+0qV0jXLaQVUYsADZC6C/J+yW7Y rLCRO2N9WI6DeeE0mTTu4HyHrKXPwrIKHb3NJXl1yVk1DoBHfv+BzR054sK2OR1cAkHrdYIQQGD AVYt93A== X-Google-Smtp-Source: AGHT+IFG4OSI48N35Xi6Gk6iSkzNvCRH8kParDuksWAqlt27lmlsX0YdseGwFG35RPWIbyYxQmf04qTEFaF5 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-e24d46b5e11mr525276.0.1727212839378; Tue, 24 Sep 2024 14:20:39 -0700 (PDT) Date: Tue, 24 Sep 2024 21:19:46 +0000 In-Reply-To: <20240924212024.540574-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: <20240924212024.540574-1-mmaurer@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20240924212024.540574-5-mmaurer@google.com> Subject: [PATCH v4 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, 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 , linux-modules@vger.kernel.org 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 --- 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