Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1574332ima; Thu, 25 Oct 2018 01:09:00 -0700 (PDT) X-Google-Smtp-Source: AJdET5dG9UL/t8V1sqLSBx/EDE092nOUPGAL38AmwswxRkX2RR0gLG2+5N7VQcCBHC8flWgBkJor X-Received: by 2002:a17:902:2ec1:: with SMTP id r59-v6mr526667plb.243.1540454940638; Thu, 25 Oct 2018 01:09:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540454940; cv=none; d=google.com; s=arc-20160816; b=Z9hGH6xg/uSRvdATLudbwhY3eBgQpoTXpOqCHBFKx+fjfr/zkmyPaKPGfGymjNE422 lWWwY7hxU8BZGuaiyDCihLbSIXkv9XkZ3yC8kS2fsH1gkXDua64PO23k6AFBs/pZZ74s RW/CJEz4jBTwG319NpltWzFD1o+Mi1qOxtGk1at60RA9x4xh+4Xbi2wV7XP3aVa8fZez VpZxHik+nomWF0FxCj2YY5mNJf4vsWi5f5zAKvucgZfW0gam2/iZ+ofx8VpsKhh3u5jv ryUmjdAraq02Ti9nxrCIjFZZx7bPtRhPtXyA3d2E8vjzlDsOv9Rzg96uclh3jnwovGWk j7Zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=rZxPnOJ3BjCL6Xgi0uH1enRJFbQz0XkdrecNd6rBylQ=; b=DR11xITBUimWP5njx4dRQ4bq8eyusdKFnFBQo5G5WN/OcL4OHHmd7Hg/5fJiGs/+Zp Oanez6aTlsN7CKlVi9hnC5lYGcn6vczzfyKAlK6Hz5HI6kFBv8shOmXzUKXzn7TiNpjY BXVKs9HEE8twxmmAiZNbqYDJh1JqTG3MOa5aDps1Yqt2nmWtt55tPcw9+lZUx1e5sDoU ZOIYMp+H+j39blVdvvswRNDjERpdDTMmVuJNHK85e0VOmNR+hX0vovBFGkgjUvsivn9y EiS8dLlDqDST42Rk+4YVpQJBQqBWxUfioVuqt9HSeQCXHjcTBUhOvVGuWReFWgVGqxcS 0fzQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a5-v6si6988584plp.261.2018.10.25.01.08.44; Thu, 25 Oct 2018 01:09:00 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727167AbeJYQj5 (ORCPT + 99 others); Thu, 25 Oct 2018 12:39:57 -0400 Received: from mx2.suse.de ([195.135.220.15]:34930 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726797AbeJYQj5 (ORCPT ); Thu, 25 Oct 2018 12:39:57 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id CE4C6AFAE; Thu, 25 Oct 2018 08:08:17 +0000 (UTC) Date: Thu, 25 Oct 2018 10:08:16 +0200 From: Petr Mladek To: Jessica Yu Cc: Torsten Duwe , Will Deacon , Catalin Marinas , Julien Thierry , Steven Rostedt , Josh Poimboeuf , Ingo Molnar , Ard Biesheuvel , Arnd Bergmann , AKASHI Takahiro , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org Subject: Re: [PATCH] arm64/module: use mod->klp_info section header information Message-ID: <20181025080816.525dppcfrrevf6jc@pathway.suse.cz> References: <20181001140910.086E768BC7@newverein.lst.de> <20181001141652.5478C68BE1@newverein.lst.de> <20181023175553.gaobskk26koft6s2@linux-8ccs> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181023175553.gaobskk26koft6s2@linux-8ccs> User-Agent: NeoMutt/20170421 (1.8.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue 2018-10-23 19:55:54, Jessica Yu wrote: > The arm64 module loader keeps a pointer into info->sechdrs to keep track > of section header information for .plt section(s). A pointer to the > relevent section header (struct elf64_shdr) in info->sechdrs is stored > in mod->arch.{init,core}.plt. This pointer may be accessed while > applying relocations in apply_relocate_add() for example. And unlike > normal modules, livepatch modules can call apply_relocate_add() after > module load. But the info struct (and therefore info->sechdrs) gets > freed at the end of load_module() and so mod->arch.{init,core}.plt > becomes an invalid pointer after the module is done loading. > > Luckily, livepatch modules already keep a copy of Elf section header > information in mod->klp_info. So make sure livepatch modules on arm64 > have access to the section headers in klp_info and set > mod->arch.{init,core}.plt to the appropriate section header in > mod->klp_info so that they can call apply_relocate_add() even after > module load. > > diff --git a/kernel/module.c b/kernel/module.c > index f475f30eed8c..f3ac04cc9fc3 100644 > --- a/kernel/module.c > +++ b/kernel/module.c > @@ -3367,6 +3367,8 @@ int __weak module_finalize(const Elf_Ehdr *hdr, > > static int post_relocation(struct module *mod, const struct load_info *info) > { > + int err; > + > /* Sort exception table now relocations are done. */ > sort_extable(mod->extable, mod->extable + mod->num_exentries); > > @@ -3377,8 +3379,18 @@ static int post_relocation(struct module *mod, const struct load_info *info) > /* Setup kallsyms-specific fields. */ > add_kallsyms(mod, info); > > + if (is_livepatch_module(mod)) { > + err = copy_module_elf(mod, info); > + if (err < 0) > + return err; > + } > + > /* Arch-specific module finalizing. */ > - return module_finalize(info->hdr, info->sechdrs, mod); > + err = module_finalize(info->hdr, info->sechdrs, mod); > + if (err < 0) if (err < 0 && is_livepatch_module(mod)) > + free_module_elf(mod); > + > + return err; > } Also we need to free the copied stuff in load_module() when anything called after post_relocation() fails. I think that the following would work: --- a/kernel/module.c +++ b/kernel/module.c @@ -3823,6 +3823,8 @@ static int load_module(struct load_info *info, const char __user *uargs, kfree(mod->args); free_arch_cleanup: module_arch_cleanup(mod); + if (is_livepatch_module(mod)) + free_module_elf(mod); free_modinfo: free_modinfo(mod); free_unload: But I suggest to just move copy_module_elf() up and keep calling it from load_module() directly. It would make the error handling more clear. Best Regards, Petr