Linux Audio

Check our new training course

Loading...
v5.4
 1// SPDX-License-Identifier: GPL-2.0-or-later
 2/*
 3 * livepatch.c - x86-specific Kernel Live Patching Core
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 4 */
 5
 6#include <linux/module.h>
 7#include <linux/kallsyms.h>
 8#include <linux/livepatch.h>
 9#include <asm/text-patching.h>
10
11/* Apply per-object alternatives. Based on x86 module_finalize() */
12void arch_klp_init_object_loaded(struct klp_patch *patch,
13				 struct klp_object *obj)
 
 
 
 
 
 
 
 
 
14{
15	int cnt;
16	struct klp_modinfo *info;
17	Elf_Shdr *s, *alt = NULL, *para = NULL;
18	void *aseg, *pseg;
19	const char *objname;
20	char sec_objname[MODULE_NAME_LEN];
21	char secname[KSYM_NAME_LEN];
22
23	info = patch->mod->klp_info;
24	objname = obj->name ? obj->name : "vmlinux";
25
26	/* See livepatch core code for BUILD_BUG_ON() explanation */
27	BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
28
29	for (s = info->sechdrs; s < info->sechdrs + info->hdr.e_shnum; s++) {
30		/* Apply per-object .klp.arch sections */
31		cnt = sscanf(info->secstrings + s->sh_name,
32			     ".klp.arch.%55[^.].%127s",
33			     sec_objname, secname);
34		if (cnt != 2)
35			continue;
36		if (strcmp(sec_objname, objname))
37			continue;
38		if (!strcmp(".altinstructions", secname))
39			alt = s;
40		if (!strcmp(".parainstructions", secname))
41			para = s;
42	}
43
44	if (alt) {
45		aseg = (void *) alt->sh_addr;
46		apply_alternatives(aseg, aseg + alt->sh_size);
47	}
48
49	if (para) {
50		pseg = (void *) para->sh_addr;
51		apply_paravirt(pseg, pseg + para->sh_size);
52	}
53}
v4.6
 
 1/*
 2 * livepatch.c - x86-specific Kernel Live Patching Core
 3 *
 4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
 5 * Copyright (C) 2014 SUSE
 6 *
 7 * This program is free software; you can redistribute it and/or
 8 * modify it under the terms of the GNU General Public License
 9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/module.h>
22#include <linux/uaccess.h>
23#include <asm/elf.h>
24#include <asm/livepatch.h>
25
26/**
27 * klp_write_module_reloc() - write a relocation in a module
28 * @mod:	module in which the section to be modified is found
29 * @type:	ELF relocation type (see asm/elf.h)
30 * @loc:	address that the relocation should be written to
31 * @value:	relocation value (sym address + addend)
32 *
33 * This function writes a relocation to the specified location for
34 * a particular module.
35 */
36int klp_write_module_reloc(struct module *mod, unsigned long type,
37			   unsigned long loc, unsigned long value)
38{
39	size_t size = 4;
40	unsigned long val;
41	unsigned long core = (unsigned long)mod->core_layout.base;
42	unsigned long core_size = mod->core_layout.size;
43
44	switch (type) {
45	case R_X86_64_NONE:
46		return 0;
47	case R_X86_64_64:
48		val = value;
49		size = 8;
50		break;
51	case R_X86_64_32:
52		val = (u32)value;
53		break;
54	case R_X86_64_32S:
55		val = (s32)value;
56		break;
57	case R_X86_64_PC32:
58		val = (u32)(value - loc);
59		break;
60	default:
61		/* unsupported relocation type */
62		return -EINVAL;
 
 
 
63	}
64
65	if (loc < core || loc >= core + core_size)
66		/* loc does not point to any symbol inside the module */
67		return -EINVAL;
 
68
69	return probe_kernel_write((void *)loc, &val, size);
 
 
 
70}