Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp9146039ybl; Fri, 17 Jan 2020 07:05:32 -0800 (PST) X-Google-Smtp-Source: APXvYqzA1cCtiZzNS4hZeYOrI/IHUk0rUpHSK0JAUX9eGQBaeA/Xk3RenvsP78cJzWa+7oIYECK0 X-Received: by 2002:a05:6830:1501:: with SMTP id k1mr6355590otp.125.1579273531805; Fri, 17 Jan 2020 07:05:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579273531; cv=none; d=google.com; s=arc-20160816; b=oDUZRQOiTBhk0vWbShTrOz1LTWFcRmi2Ns8mu0W0WGz1S0TueQCKvEWhW4rKd81C6/ nLLOBd4W8hkxoIRmKcUm1e7qr4vBHO5/IkZRhiQgw7RVT0Z0zxjUUEhVnVG2YJWt6477 mSKmYx/keHiTMz1cqb2RGxNdRE15NFsAafTZzwN38iWqwZvyGJvUNNOCWaqQXi7XY6bH oLrrLXJsj/gvdDtX+tnBdCnH2BZOh6vgJuqh08jMut9eRDkFQs7aAbkorrZAkb1VSl1Q WiWE3Q6mZaplf1RlNxngAtnqAdUl/HhZ9RKZ/wdTxCebg3/9H7/ka6AXOTtJec6QOvxq aWNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=45bTWcX8DrAOVrc/x4pdE+iRUQmkGBXyvmMW7nlK5to=; b=pQ/uAEqZ+flpsR2PjZfWey467rd0PjAJKApHH0q5x8hfapx6GbyX0+NAnLZQhwEId4 l1pAHeAan779n+Ii0q2xH6kAdYbU8angi0vOBp3dTxeuGSCsiIQNquDvsStzhoE/iXg1 vwrIcJUyKE9MYTOYpW6sLHkdrDCgInhHN6f+kTArlw1xjgDWL3cVl22m3JCO9BDn7Fcw zQ46X8j0L/mlwmORYnDPDQXhtzrpNriH7Yb2f/1C/7IUILQVREjn8/zbyc24gty7ihCD CoKA+CgLKlO8QI+0hBaWqAdSgeTXz9tnMtiHaGjIRu/R8710GV7pjl70vqNWYrldukKb MaWA== 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 z22si1455213otq.94.2020.01.17.07.05.18; Fri, 17 Jan 2020 07:05:31 -0800 (PST) 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 S1729212AbgAQPEG (ORCPT + 99 others); Fri, 17 Jan 2020 10:04:06 -0500 Received: from mx2.suse.de ([195.135.220.15]:46352 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729073AbgAQPEE (ORCPT ); Fri, 17 Jan 2020 10:04:04 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id F0010BBBB; Fri, 17 Jan 2020 15:04:02 +0000 (UTC) From: Petr Mladek To: Jiri Kosina , Josh Poimboeuf , Miroslav Benes Cc: Joe Lawrence , Kamalesh Babulal , Nicolai Stange , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [POC 20/23] module/livepatch: Relocate local variables in the module loaded when the livepatch is being loaded Date: Fri, 17 Jan 2020 16:03:20 +0100 Message-Id: <20200117150323.21801-21-pmladek@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200117150323.21801-1-pmladek@suse.com> References: <20200117150323.21801-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 4776deb7418c..d721e79ac691 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -211,6 +211,10 @@ bool klp_break_recursion(struct module *mod); int klp_module_coming(struct module *mod); void klp_module_going(struct module *mod); +int klp_resolve_symbols(Elf_Shdr *sechdrs, + unsigned int relsec, + struct module *pmod); + void klp_copy_process(struct task_struct *child); void klp_update_patch_state(struct task_struct *task); diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 9e00871fbc06..4da6bbd687d0 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -196,12 +196,15 @@ static int klp_find_object_symbol(const char *objname, const char *name, return -EINVAL; } -static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod) +int klp_resolve_symbols(Elf_Shdr *sechdrs, + 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; unsigned long sympos, addr; @@ -251,54 +254,6 @@ static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod) return 0; } -static int klp_write_object_relocations(struct klp_object *obj) -{ - int i, cnt, ret = 0; - const char *objname, *secname; - char sec_objname[MODULE_NAME_LEN]; - struct module *pmod; - Elf_Shdr *sec; - - objname = klp_is_module(obj) ? obj->name : "vmlinux"; - pmod = obj->mod; - - /* For each klp relocation section */ - for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) { - sec = pmod->klp_info->sechdrs + i; - secname = pmod->klp_info->secstrings + sec->sh_name; - if (!(sec->sh_flags & SHF_RELA_LIVEPATCH)) - continue; - - /* - * Format: .klp.rela.sec_objname.section_name - * See comment in klp_resolve_symbols() for an explanation - * of the selected field width value. - */ - cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname); - if (cnt != 1) { - pr_err("section %s has an incorrectly formatted name\n", - secname); - ret = -EINVAL; - break; - } - - if (strcmp(objname, sec_objname)) - continue; - - ret = klp_resolve_symbols(sec, pmod); - if (ret) - break; - - ret = apply_relocate_add(pmod->klp_info->sechdrs, - pmod->core_kallsyms.strtab, - pmod->klp_info->symndx, i, pmod); - if (ret) - break; - } - - return ret; -} - /* * Sysfs Interface * @@ -758,18 +713,9 @@ static int klp_init_object_loaded(struct klp_object *obj) int ret; mutex_lock(&text_mutex); - module_disable_ro(obj->mod); - ret = klp_write_object_relocations(obj); - if (ret) { - module_enable_ro(obj->mod, true); - mutex_unlock(&text_mutex); - return ret; - } - arch_klp_init_object_loaded(obj); module_enable_ro(obj->mod, true); - mutex_unlock(&text_mutex); klp_for_each_func(obj, func) { 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; } -- 2.16.4