Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp746480ybb; Fri, 3 Apr 2020 11:03:27 -0700 (PDT) X-Google-Smtp-Source: APiQypLfCH0QvN36tqGCfGPFTOpQflOuzUG7GpGG9y0N/kvuhdsqCRfnnr7C/+1ufn5UGOveutzp X-Received: by 2002:a9d:70d0:: with SMTP id w16mr7910702otj.9.1585937006931; Fri, 03 Apr 2020 11:03:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585937006; cv=none; d=google.com; s=arc-20160816; b=01nT7Wjo5ddeNXILd85JKfb7ClOm9hVWoVPmT2n2DRKI2yFriX0lGBA8sWMakk5hhY /E8U1B/LKnx7KSrDkB/Kak+Gnr8u9VPkfJxnscvC05W1t7HNYLoQwqWpes+acHBvRcdK LhwvHqL1bs6ck4K3U4hEUIwNiQq/6RZuV3sCGM1p/5ml7MEQgOu5OgxMQNid5gaLYhbX KxBHrXJwzW0AVPb4DXbyT9ng4V3FWDM1aKknklhP+nI2OEX5/8AuWkAKn0fmSLnZtvCH WaE71FMScJuj5Gc26QoqDgySV91HxK7x+VW7bNVIB4Qm642W27bC1YmOrMmVQ3RNvUUE tEvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=0NiMkDJ11j6iIzUVzg7SnDb83MwqOSNwZhS3zwBcV2Y=; b=Sv337+5wcXhMw+g7uPhsPnluvtOPZvZtNvLyezQ6PkNGoZHcRTtq59s/KNmB6mAhZs mMR9q3Fu5AkYfzHPgmjyiCzp3cmEJLsb4rhMCtW8AKV1GOEoVCJTsiqBaLjOmn1mxdXo PrhT6GxQ+wQlYgig3+mR2Pt+rs66+r1VlHhl5wGtSnoJeOgNuIYhgayy5lc0ebFMtaHS wR031c+f5xB2n+vbb84Qm8NDBZfqjdFQBLXv1D7TM2Zza2a96UCRFklrpdfJqcaKg0f1 CWY37agamfygLTtYBFqoYfXir0xvElKlnb2nivvv7JhIkNI/RjSi2tM/APF64GBiL0vC Q5CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aHlID9tE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y1si4182142otq.87.2020.04.03.11.03.13; Fri, 03 Apr 2020 11:03:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aHlID9tE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404197AbgDCSAq (ORCPT + 99 others); Fri, 3 Apr 2020 14:00:46 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:60090 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2404096AbgDCSAq (ORCPT ); Fri, 3 Apr 2020 14:00:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585936845; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=0NiMkDJ11j6iIzUVzg7SnDb83MwqOSNwZhS3zwBcV2Y=; b=aHlID9tEKRmEW5JEuilOyelPmmW5E//w6ipraMYYuvgsxiDNBnRBk9+gcsaEtxh3bdL9Yf xeohdkoZ5rNCDD9cz6c6y8dbTMsTL+IZ4gpv1GVRZ4OBNsvBv6KpJk9IMP3IbzMRcfT5r1 49XbfyZRnBXQXF/nDktcklSjPqmf7ps= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-446-YokxTUBLPHi7KLMQlFoTWQ-1; Fri, 03 Apr 2020 14:00:39 -0400 X-MC-Unique: YokxTUBLPHi7KLMQlFoTWQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8665F801E66; Fri, 3 Apr 2020 18:00:37 +0000 (UTC) Received: from redhat.com (ovpn-114-27.phx2.redhat.com [10.3.114.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8F27F60BF3; Fri, 3 Apr 2020 18:00:36 +0000 (UTC) Date: Fri, 3 Apr 2020 14:00:34 -0400 From: Joe Lawrence To: Petr Mladek Cc: Jiri Kosina , Josh Poimboeuf , Miroslav Benes , Kamalesh Babulal , Nicolai Stange , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [POC 20/23] module/livepatch: Relocate local variables in the module loaded when the livepatch is being loaded Message-ID: <20200403180034.GB30284@redhat.com> References: <20200117150323.21801-1-pmladek@suse.com> <20200117150323.21801-21-pmladek@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200117150323.21801-21-pmladek@suse.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 17, 2020 at 04:03:20PM +0100, Petr Mladek wrote: > The special SHF_RELA_LIVEPATCH section is still needed to find static > (non-exported) symbols. But it can be done together with the other > relocations when the livepatch module is being loaded. > > There is no longer needed to copy the info section. The related > code in the module loaded will get removed in separate patch. > > Signed-off-by: Petr Mladek > --- > include/linux/livepatch.h | 4 +++ > kernel/livepatch/core.c | 62 +++-------------------------------------------- > kernel/module.c | 16 +++++++----- > 3 files changed, 18 insertions(+), 64 deletions(-) > > > [ ... snip ... ] > > diff --git a/kernel/module.c b/kernel/module.c > index bd92854b42c2..c14b5135db27 100644 > --- a/kernel/module.c > +++ b/kernel/module.c > @@ -2410,16 +2410,20 @@ static int apply_relocations(struct module *mod, const struct load_info *info) > if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC)) > continue; > > - /* Livepatch relocation sections are applied by livepatch */ > - if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH) > - continue; > - > - if (info->sechdrs[i].sh_type == SHT_REL) > + /* Livepatch need to resolve static symbols. */ > + if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH) { > + err = klp_resolve_symbols(info->sechdrs, i, mod); > + if (err < 0) > + break; > + err = apply_relocate_add(info->sechdrs, info->strtab, > + info->index.sym, i, mod); > + } else if (info->sechdrs[i].sh_type == SHT_REL) { > err = apply_relocate(info->sechdrs, info->strtab, > info->index.sym, i, mod); > - else if (info->sechdrs[i].sh_type == SHT_RELA) > + } else if (info->sechdrs[i].sh_type == SHT_RELA) { > err = apply_relocate_add(info->sechdrs, info->strtab, > info->index.sym, i, mod); > + } > if (err < 0) > break; > } Hi Petr, At first I thought there was a simple order of operations problem here with respect to klp_resolve_symbols() accessing core_kallsyms before they were setup by add_kallsyms(): load_module apply_relocations /* Livepatch need to resolve static symbols. */ if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH) { err = klp_resolve_symbols(info->sechdrs, i, mod); klp_resolve_symbols sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info); ^^^^^^^^^^^^^^^^^^^^ used before init (below) ... post_relocation add_kallsyms /* * Now populate the cut down core kallsyms for after init * and set types up while we still have access to sections. */ mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs; mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs; mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs; ^^^^^^^^^^^^^^^^^^^^^ core_kallsyms initialized here But after tinkering with the patchset, a larger problem is that klp_resolve_symbols() writes st_values to the core_kallsyms copies, but then apply_relocate_add() references the originals in the load_info structure. I assume that klp_resolve_symbols() originally looked at the core_kallsyms copies for handling the late module patching case. If we no longer need to support that, then how about this slight modification to klp_resolve_symbols() to make it look more the like apply_relocate{_add,} calls? -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 3b27ef1a7291..54d5a4045e5a 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -210,6 +210,8 @@ int klp_module_coming(struct module *mod); void klp_module_going(struct module *mod); int klp_resolve_symbols(Elf_Shdr *sechdrs, + const char *strtab, + unsigned int symindex, unsigned int relsec, struct module *pmod); diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index cc0ac93fe8cd..02638e3b09b0 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -197,13 +197,14 @@ static int klp_find_object_symbol(const char *objname, const char *name, } int klp_resolve_symbols(Elf_Shdr *sechdrs, + const char *strtab, + unsigned int symindex, unsigned int relsec, struct module *pmod) { int i, cnt, vmlinux, ret; char objname[MODULE_NAME_LEN]; char symname[KSYM_NAME_LEN]; - char *strtab = pmod->core_kallsyms.strtab; Elf_Shdr *relasec = sechdrs + relsec; Elf_Rela *relas; Elf_Sym *sym; @@ -224,7 +225,8 @@ int klp_resolve_symbols(Elf_Shdr *sechdrs, relas = (Elf_Rela *) relasec->sh_addr; /* For each rela in this klp relocation section */ for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) { - sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info); + sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + + ELF_R_SYM(relas[i].r_info); if (sym->st_shndx != SHN_LIVEPATCH) { pr_err("symbol %s is not marked as a livepatch symbol\n", strtab + sym->st_name); diff --git a/kernel/module.c b/kernel/module.c index d435bad80d7d..a65f089f19c9 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -2320,7 +2320,8 @@ static int apply_relocations(struct module *mod, const struct load_info *info) /* Livepatch need to resolve static symbols. */ if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH) { - err = klp_resolve_symbols(info->sechdrs, i, mod); + err = klp_resolve_symbols(info->sechdrs, info->strtab, + info->index.sym, i, mod); if (err < 0) break; err = apply_relocate_add(info->sechdrs, info->strtab, -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -->8-- -- Joe