From mboxrd@z Thu Jan 1 00:00:00 1970 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 729AA1ACE10 for ; Tue, 24 Sep 2024 21:20:47 +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=1727212849; cv=none; b=oPOj2CpLcGy9XSfmqhEte8MUlLowgPe3TYhqRsf1RUk2eBDl09whVrO+D1C0NySua6J0E0Nfq8X51WwwNKPfBFtlp17jZ8qM0m8ZAsHOiJA0ZZNkPrcJiO6be3zwXuT4T/pr2BAZpfqtv5Q+TS8a4RM5EoC7FphSM1aA3pymtYE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727212849; c=relaxed/simple; bh=hPcayIG4Zzk7TfnQCe5VB/OkeMCLVNaFCb22SLuD7xA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n+XoUIZA/Ww3T6MVPpk8jWFET20DbbcVsQATskU0REzu6xDiR9gtVlwugQNnbvg82OeTJKkZH0iW9sqX50hg2u4dVOfio+bbJ7jFWHZRFfbEGIHtRVa/0h0CevnCc143GFXhyQZf8uOPGgrugvLhLG3M11H7FYX+VWUC69MBMS8= 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=wv2JsPQ+; 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="wv2JsPQ+" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e21d0bfba7so5527007b3.3 for ; Tue, 24 Sep 2024 14:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727212846; x=1727817646; darn=lists.linux.dev; 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=wv2JsPQ+8WzbtlccM09dxIkgVikd1x+1G23sx5Z5im9/SYEzGze/ISjwgxoLAcU7v4 C6GH+rWQHgARpCwH4LcoHp3ItJfbJHOTX27aalmq+WBGVyw6mKMTnQRAmCzjm1hjCBlO sSOeNzkVn/cLtERa+9uqalwOExPGqqbBr5DLJqJdyWsCK1UYTe06RXRcRiebTFPOefW3 y9RfaES4c9OXHMdNHcTlX+5r6K9/J0QTa2De0xBtWe1C3stBfeOqCsV6P3IlCt5x1EUx 7eU1B+Clay4oerS/fPYE8AwAiavU3XCn5A/zPHHDxvYIG68JfczPNNCLmd6Hl6ox0rVf +2FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727212846; x=1727817646; 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=FUh1ntgi+ZORiIDy0uPtTS+IF/FexPFBkwwQfhIX3fkhJuieOO1oh2F5oNPPWJqE8v cwKr0Y/1WFWoH4FO9A1KJDsigvuuBU16WIt5xRc98wEoZJ3+QPKeIm2rbM9VJBBhUba/ EfiBFI2M60lgnqQXAb7lSN/VQQ+BDUgXC724qJgr384igtQ+5JjZ2/F5p4E1sYYy2t8w EAN72CGtT9fZ5+Mvih4DRaqIZpsA0X//M5uA7CvrPwIw3okGSA0zO4vU2+V9PciuU8hF 7SxyWeZcWxLf5aXQA1vlqBnkek6DqkJswGAcJfh685KmeGLYtMODZzv1bwS73doxn8Hu SoXQ== X-Forwarded-Encrypted: i=1; AJvYcCUhfB2lxhI5BgqQImbxWS0PIK7mr39Or6QLumwnGNs17FQ/JrNDLTH9vI9EwR8EYTXC59DSTQ==@lists.linux.dev X-Gm-Message-State: AOJu0Yy7iXSkBFceyt3CyEMdp9Vujfw0PWXjcO6N8bN+8jeKpKjl+VQX 6Lh2F4+mUEYFkHnIhVf4xUv912O7G/Zb9s9HCxVJgrtgQl0m8iPorkOoWLyAPvFA4jPj4qJ08Uk h1/2C6Q== X-Google-Smtp-Source: AGHT+IHDISpnfdzBaOTWR8C0ioj3uPYVqLGAlaWzdMvCzf+f+r+0H0cGN2cqc+XvC1wkkQdhXDCIGGe8xEvv X-Received: from anyblade.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1791]) (user=mmaurer job=sendgmr) by 2002:a25:d608:0:b0:e0b:ab63:b9c8 with SMTP id 3f1490d57ef6-e24da399c47mr364276.11.1727212846139; Tue, 24 Sep 2024 14:20:46 -0700 (PDT) Date: Tue, 24 Sep 2024 21:19:49 +0000 In-Reply-To: <20240924212024.540574-1-mmaurer@google.com> Precedence: bulk X-Mailing-List: asahi@lists.linux.dev 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-8-mmaurer@google.com> Subject: [PATCH v4 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, 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-Type: text/plain; charset="UTF-8" Centralize symbol table detection and property validation. Signed-off-by: Matthew Maurer --- 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; } +/** + * 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 populated. + * + * 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 = 0; + int i; + + for (i = 1; i < info->hdr->e_shnum; i++) { + if (info->sechdrs[i].sh_type == SHT_SYMTAB) { + num_sym_secs++; + sym_idx = i; + } + } + + if (num_sym_secs != 1) { + pr_warn("%s: module has no symbols (stripped?)\n", + info->name ?: "(missing .modinfo section or name field)"); + return -ENOEXEC; + } + + info->index.sym = sym_idx; + + return 0; +} /* * 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 = 0, sym_idx; + int str_idx; err = 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 = elf_validity_cache_index_mod(info); + if (err < 0) + return err; + err = elf_validity_cache_index_sym(info); if (err < 0) return err; - for (i = 1; i < info->hdr->e_shnum; i++) { - shdr = &info->sechdrs[i]; - if (shdr->sh_type == SHT_SYMTAB) { - if (shdr->sh_link == SHN_UNDEF - || shdr->sh_link >= info->hdr->e_shnum) { - pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", - shdr->sh_link, shdr->sh_link, - info->hdr->e_shnum); - goto no_exec; - } - num_sym_secs++; - sym_idx = i; - } - } - - if (num_sym_secs != 1) { - pr_warn("%s: module has no symbols (stripped?)\n", - info->name ?: "(missing .modinfo section or name field)"); - goto no_exec; + str_idx = info->sechdrs[info->index.sym].sh_link; + if (str_idx == SHN_UNDEF || str_idx >= info->hdr->e_shnum) { + pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n", + str_idx, str_idx, info->hdr->e_shnum); + return -ENOEXEC; } - /* Sets internal symbols and strings. */ - info->index.sym = sym_idx; - shdr = &info->sechdrs[sym_idx]; - info->index.str = shdr->sh_link; + /* Sets internal strings. */ + info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; /* 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 = find_pcpusec(info); return 0; - -no_exec: - return -ENOEXEC; } #define COPY_CHUNK_SIZE (16*PAGE_SIZE) -- 2.46.1.824.gd892dcdcdd-goog