From patchwork Wed Jul 19 14:56:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yauheni Kaliuta X-Patchwork-Id: 9852319 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2EAB9602C8 for ; Wed, 19 Jul 2017 14:54:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FFBD285FD for ; Wed, 19 Jul 2017 14:54:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14BB928676; Wed, 19 Jul 2017 14:54:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF401285FD for ; Wed, 19 Jul 2017 14:54:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755008AbdGSOxu (ORCPT ); Wed, 19 Jul 2017 10:53:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55706 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932147AbdGSOxp (ORCPT ); Wed, 19 Jul 2017 10:53:45 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C46BA73B06; Wed, 19 Jul 2017 14:53:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C46BA73B06 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=yauheni.kaliuta@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C46BA73B06 Received: from astarta.redhat.com (ovpn-116-109.ams2.redhat.com [10.36.116.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B0FB080AAE; Wed, 19 Jul 2017 14:53:43 +0000 (UTC) From: Yauheni Kaliuta To: Lucas De Marchi Cc: linux-modules , Ard Biesheuvel , linux-kernel@vger.kernel.org Subject: [PATCH] libkmod-elf: resolve CRC if module is built with MODULE_REL_CRCS Date: Wed, 19 Jul 2017 17:56:49 +0300 Message-Id: <20170719145649.20334-1-yauheni.kaliuta@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 19 Jul 2017 14:53:44 +0000 (UTC) Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Normally exported symbol's crc is stored as absolute (SHN_ABS) value of special named symbol __crc_. When the kernel and modules are built with the config option CONFIG_MODULE_REL_CRCS, all the CRCs are put in a special section and the __crc_ symbols values are offsets in the section. See patch description of the commit: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=56067812d5b0e737ac2063e94a50f76b810d6ca3 Add kmod support of this configuration. Signed-off-by: Yauheni Kaliuta Reviewed-by: Ard Biesheuvel --- libkmod/libkmod-elf.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c index 90da89aebbaf..ef4a8a3142a1 100644 --- a/libkmod/libkmod-elf.c +++ b/libkmod/libkmod-elf.c @@ -747,6 +747,31 @@ static inline uint8_t kmod_symbol_bind_from_elf(uint8_t elf_value) } } +static uint64_t kmod_elf_resolve_crc(const struct kmod_elf *elf, uint64_t crc, uint16_t shndx) +{ + int err; + uint64_t off, size; + uint32_t nameoff; + + if (shndx == SHN_ABS || shndx == SHN_UNDEF) + return crc; + + err = elf_get_section_info(elf, shndx, &off, &size, &nameoff); + if (err < 0) { + ELFDBG("Cound not find section index %"PRIu16" for crc", shndx); + return (uint64_t)-1; + } + + if (crc > (size - sizeof(uint32_t))) { + ELFDBG("CRC offset %"PRIu64" is too big, section %"PRIu16" size is %"PRIu64"\n", + crc, shndx, size); + return (uint64_t)-1; + } + + crc = elf_get_uint(elf, off + crc, sizeof(uint32_t)); + return crc; +} + /* array will be allocated with strings in a single malloc, just free *array */ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **array) { @@ -830,6 +855,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar uint32_t name_off; uint64_t crc; uint8_t info, bind; + uint16_t shndx; #define READV(field) \ elf_get_uint(elf, sym_off + offsetof(typeof(*s), field),\ @@ -839,11 +865,13 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar name_off = READV(st_name); crc = READV(st_value); info = READV(st_info); + shndx = READV(st_shndx); } else { Elf64_Sym *s; name_off = READV(st_name); crc = READV(st_value); info = READV(st_info); + shndx = READV(st_shndx); } #undef READV name = elf_get_mem(elf, str_off + name_off); @@ -856,7 +884,7 @@ int kmod_elf_get_symbols(const struct kmod_elf *elf, struct kmod_modversion **ar else bind = ELF64_ST_BIND(info); - a[count].crc = crc; + a[count].crc = kmod_elf_resolve_crc(elf, crc, shndx); a[count].bind = kmod_symbol_bind_from_elf(bind); a[count].symbol = itr; slen = strlen(name);