Blame kmod/patch/kpatch-patch-hook.c

Packit Service ac8aad
/*
Packit Service ac8aad
 * Copyright (C) 2013-2014 Josh Poimboeuf <jpoimboe@redhat.com>
Packit Service ac8aad
 *
Packit Service ac8aad
 * This program is free software; you can redistribute it and/or
Packit Service ac8aad
 * modify it under the terms of the GNU General Public License
Packit Service ac8aad
 * as published by the Free Software Foundation; either version 2
Packit Service ac8aad
 * of the License, or (at your option) any later version.
Packit Service ac8aad
 *
Packit Service ac8aad
 * This program is distributed in the hope that it will be useful,
Packit Service ac8aad
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service ac8aad
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service ac8aad
 * GNU General Public License for more details.
Packit Service ac8aad
 *
Packit Service ac8aad
 * You should have received a copy of the GNU General Public License
Packit Service ac8aad
 * along with this program; if not, write to the Free Software
Packit Service ac8aad
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
Packit Service ac8aad
 * 02110-1301, USA.
Packit Service ac8aad
 */
Packit Service ac8aad
Packit Service ac8aad
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Packit Service ac8aad
Packit Service ac8aad
#include <linux/module.h>
Packit Service ac8aad
#include <linux/printk.h>
Packit Service ac8aad
#include <linux/slab.h>
Packit Service ac8aad
#include <linux/kallsyms.h>
Packit Service ac8aad
#include "kpatch.h"
Packit Service ac8aad
#include "kpatch-patch.h"
Packit Service ac8aad
Packit Service ac8aad
static bool replace;
Packit Service ac8aad
module_param(replace, bool, S_IRUGO);
Packit Service ac8aad
MODULE_PARM_DESC(replace, "replace all previously loaded patch modules");
Packit Service ac8aad
Packit Service ac8aad
extern struct kpatch_patch_func __kpatch_funcs[], __kpatch_funcs_end[];
Packit Service ac8aad
extern struct kpatch_patch_dynrela __kpatch_dynrelas[], __kpatch_dynrelas_end[];
Packit Service ac8aad
extern struct kpatch_pre_patch_callback __kpatch_callbacks_pre_patch[], __kpatch_callbacks_pre_patch_end[];
Packit Service ac8aad
extern struct kpatch_post_patch_callback __kpatch_callbacks_post_patch[], __kpatch_callbacks_post_patch_end[];
Packit Service ac8aad
extern struct kpatch_pre_unpatch_callback __kpatch_callbacks_pre_unpatch[], __kpatch_callbacks_pre_unpatch_end[];
Packit Service ac8aad
extern struct kpatch_post_unpatch_callback __kpatch_callbacks_post_unpatch[], __kpatch_callbacks_post_unpatch_end[];
Packit Service ac8aad
extern unsigned long __kpatch_force_funcs[], __kpatch_force_funcs_end[];
Packit Service ac8aad
extern char __kpatch_checksum[];
Packit Service ac8aad
Packit Service ac8aad
static struct kpatch_module kpmod;
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_enabled_show(struct kobject *kobj,
Packit Service ac8aad
				  struct kobj_attribute *attr, char *buf)
Packit Service ac8aad
{
Packit Service ac8aad
	return sprintf(buf, "%d\n", kpmod.enabled);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_enabled_store(struct kobject *kobj,
Packit Service ac8aad
				   struct kobj_attribute *attr, const char *buf,
Packit Service ac8aad
				   size_t count)
Packit Service ac8aad
{
Packit Service ac8aad
	int ret;
Packit Service ac8aad
	unsigned long val;
Packit Service ac8aad
Packit Service ac8aad
	ret = kstrtoul(buf, 10, &val;;
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		return ret;
Packit Service ac8aad
Packit Service ac8aad
	val = !!val;
Packit Service ac8aad
Packit Service ac8aad
	if (val)
Packit Service ac8aad
		ret = kpatch_register(&kpmod, replace);
Packit Service ac8aad
	else
Packit Service ac8aad
		ret = kpatch_unregister(&kpmod);
Packit Service ac8aad
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		return ret;
Packit Service ac8aad
Packit Service ac8aad
	return count;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_checksum_show(struct kobject *kobj,
Packit Service ac8aad
				   struct kobj_attribute *attr, char *buf)
Packit Service ac8aad
{
Packit Service ac8aad
	return snprintf(buf, PAGE_SIZE, "%s\n", __kpatch_checksum);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_attribute patch_enabled_attr =
Packit Service ac8aad
	__ATTR(enabled, 0644, patch_enabled_show, patch_enabled_store);
Packit Service ac8aad
static struct kobj_attribute patch_checksum_attr =
Packit Service ac8aad
	__ATTR(checksum, 0444, patch_checksum_show, NULL);
Packit Service ac8aad
Packit Service ac8aad
static struct attribute *patch_attrs[] = {
Packit Service ac8aad
	&patch_enabled_attr.attr,
Packit Service ac8aad
	&patch_checksum_attr.attr,
Packit Service ac8aad
	NULL,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static void patch_kobj_free(struct kobject *kobj)
Packit Service ac8aad
{
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_type patch_ktype = {
Packit Service ac8aad
        .release = patch_kobj_free,
Packit Service ac8aad
        .sysfs_ops = &kobj_sysfs_ops,
Packit Service ac8aad
        .default_attrs = patch_attrs,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_func_old_addr_show(struct kobject *kobj,
Packit Service ac8aad
					struct kobj_attribute *attr, char *buf)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_func *func =
Packit Service ac8aad
		container_of(kobj, struct kpatch_func, kobj);
Packit Service ac8aad
Packit Service ac8aad
	return sprintf(buf, "0x%lx\n", func->old_addr);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_func_new_addr_show(struct kobject *kobj,
Packit Service ac8aad
					struct kobj_attribute *attr, char *buf)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_func *func =
Packit Service ac8aad
		container_of(kobj, struct kpatch_func, kobj);
Packit Service ac8aad
Packit Service ac8aad
	return sprintf(buf, "0x%lx\n", func->new_addr);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_attribute patch_old_addr_attr =
Packit Service ac8aad
	__ATTR(old_addr, S_IRUSR, patch_func_old_addr_show, NULL);
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_attribute patch_new_addr_attr =
Packit Service ac8aad
	__ATTR(new_addr, S_IRUSR, patch_func_new_addr_show, NULL);
Packit Service ac8aad
Packit Service ac8aad
static struct attribute *patch_func_kobj_attrs[] = {
Packit Service ac8aad
	&patch_old_addr_attr.attr,
Packit Service ac8aad
	&patch_new_addr_attr.attr,
Packit Service ac8aad
	NULL,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static ssize_t patch_func_kobj_show(struct kobject *kobj,
Packit Service ac8aad
				    struct attribute *attr, char *buf)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kobj_attribute *func_attr =
Packit Service ac8aad
		container_of(attr, struct kobj_attribute, attr);
Packit Service ac8aad
Packit Service ac8aad
	return func_attr->show(kobj, func_attr, buf);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static const struct sysfs_ops patch_func_sysfs_ops = {
Packit Service ac8aad
	.show	= patch_func_kobj_show,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static void patch_func_kobj_free(struct kobject *kobj)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_func *func =
Packit Service ac8aad
		container_of(kobj, struct kpatch_func, kobj);
Packit Service ac8aad
	kfree(func);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_type patch_func_ktype = {
Packit Service ac8aad
	.release	= patch_func_kobj_free,
Packit Service ac8aad
	.sysfs_ops	= &patch_func_sysfs_ops,
Packit Service ac8aad
	.default_attrs	= patch_func_kobj_attrs,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static void patch_object_kobj_free(struct kobject *kobj)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_object *obj =
Packit Service ac8aad
		container_of(kobj, struct kpatch_object, kobj);
Packit Service ac8aad
	kfree(obj);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static struct kobj_type patch_object_ktype = {
Packit Service ac8aad
	.release = patch_object_kobj_free,
Packit Service ac8aad
	.sysfs_ops = &kobj_sysfs_ops,
Packit Service ac8aad
};
Packit Service ac8aad
Packit Service ac8aad
static struct kpatch_object *patch_find_or_add_object(struct list_head *head,
Packit Service ac8aad
						      const char *name)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_object *object;
Packit Service ac8aad
	int ret;
Packit Service ac8aad
Packit Service ac8aad
	list_for_each_entry(object, head, list) {
Packit Service ac8aad
		if (!strcmp(object->name, name))
Packit Service ac8aad
			return object;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	object = kzalloc(sizeof(*object), GFP_KERNEL);
Packit Service ac8aad
	if (!object)
Packit Service ac8aad
		return NULL;
Packit Service ac8aad
Packit Service ac8aad
	object->name = name;
Packit Service ac8aad
	INIT_LIST_HEAD(&object->funcs);
Packit Service ac8aad
	INIT_LIST_HEAD(&object->dynrelas);
Packit Service ac8aad
Packit Service ac8aad
	list_add_tail(&object->list, head);
Packit Service ac8aad
Packit Service ac8aad
	ret = kobject_init_and_add(&object->kobj, &patch_object_ktype,
Packit Service ac8aad
				   &kpmod.kobj, "%s", object->name);
Packit Service ac8aad
	if (ret) {
Packit Service ac8aad
		list_del(&object->list);
Packit Service ac8aad
		kfree(object);
Packit Service ac8aad
		return NULL;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	return object;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static void patch_free_objects(void)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_object *object, *object_safe;
Packit Service ac8aad
	struct kpatch_func *func, *func_safe;
Packit Service ac8aad
	struct kpatch_dynrela *dynrela, *dynrela_safe;
Packit Service ac8aad
Packit Service ac8aad
	list_for_each_entry_safe(object, object_safe, &kpmod.objects, list) {
Packit Service ac8aad
		list_for_each_entry_safe(func, func_safe, &object->funcs,
Packit Service ac8aad
					 list) {
Packit Service ac8aad
			list_del(&func->list);
Packit Service ac8aad
			kobject_put(&func->kobj);
Packit Service ac8aad
		}
Packit Service ac8aad
		list_for_each_entry_safe(dynrela, dynrela_safe,
Packit Service ac8aad
					 &object->dynrelas, list) {
Packit Service ac8aad
			list_del(&dynrela->list);
Packit Service ac8aad
			kfree(dynrela);
Packit Service ac8aad
		}
Packit Service ac8aad
		list_del(&object->list);
Packit Service ac8aad
		kobject_put(&object->kobj);
Packit Service ac8aad
	}
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static int patch_is_func_forced(unsigned long addr)
Packit Service ac8aad
{
Packit Service ac8aad
	unsigned long *a;
Packit Service ac8aad
	for (a = __kpatch_force_funcs; a < __kpatch_force_funcs_end; a++)
Packit Service ac8aad
		if (*a == addr)
Packit Service ac8aad
			return 1;
Packit Service ac8aad
	return 0;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static int patch_make_funcs_list(struct list_head *objects)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_object *object;
Packit Service ac8aad
	struct kpatch_patch_func *p_func;
Packit Service ac8aad
	struct kpatch_func *func;
Packit Service ac8aad
	int ret;
Packit Service ac8aad
Packit Service ac8aad
	for (p_func = __kpatch_funcs; p_func < __kpatch_funcs_end; p_func++) {
Packit Service ac8aad
		object = patch_find_or_add_object(&kpmod.objects,
Packit Service ac8aad
						  p_func->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		func = kzalloc(sizeof(*func), GFP_KERNEL);
Packit Service ac8aad
		if (!func)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		func->new_addr = p_func->new_addr;
Packit Service ac8aad
		func->new_size = p_func->new_size;
Packit Service ac8aad
		func->old_size = p_func->old_size;
Packit Service ac8aad
		func->sympos = p_func->sympos;
Packit Service ac8aad
		func->name = p_func->name;
Packit Service ac8aad
		func->force = patch_is_func_forced(func->new_addr);
Packit Service ac8aad
		list_add_tail(&func->list, &object->funcs);
Packit Service ac8aad
Packit Service ac8aad
		ret = kobject_init_and_add(&func->kobj, &patch_func_ktype,
Packit Service ac8aad
					   &object->kobj, "%s,%lu",
Packit Service ac8aad
					   func->name, func->sympos ? func->sympos : 1);
Packit Service ac8aad
		if (ret)
Packit Service ac8aad
			return ret;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	return 0;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static int patch_make_dynrelas_list(struct list_head *objects)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_object *object;
Packit Service ac8aad
	struct kpatch_patch_dynrela *p_dynrela;
Packit Service ac8aad
	struct kpatch_dynrela *dynrela;
Packit Service ac8aad
Packit Service ac8aad
	for (p_dynrela = __kpatch_dynrelas; p_dynrela < __kpatch_dynrelas_end;
Packit Service ac8aad
	     p_dynrela++) {
Packit Service ac8aad
		object = patch_find_or_add_object(objects, p_dynrela->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		dynrela = kzalloc(sizeof(*dynrela), GFP_KERNEL);
Packit Service ac8aad
		if (!dynrela)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		dynrela->dest = p_dynrela->dest;
Packit Service ac8aad
		dynrela->type = p_dynrela->type;
Packit Service ac8aad
		dynrela->sympos = p_dynrela->sympos;
Packit Service ac8aad
		dynrela->name = p_dynrela->name;
Packit Service ac8aad
		dynrela->external = p_dynrela->external;
Packit Service ac8aad
		dynrela->addend = p_dynrela->addend;
Packit Service ac8aad
		list_add_tail(&dynrela->list, &object->dynrelas);
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	return 0;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static int patch_set_callbacks(struct list_head *objects)
Packit Service ac8aad
{
Packit Service ac8aad
	struct kpatch_pre_patch_callback *p_pre_patch_callback;
Packit Service ac8aad
	struct kpatch_post_patch_callback *p_post_patch_callback;
Packit Service ac8aad
	struct kpatch_pre_unpatch_callback *p_pre_unpatch_callback;
Packit Service ac8aad
	struct kpatch_post_unpatch_callback *p_post_unpatch_callback;
Packit Service ac8aad
	struct kpatch_object *object;
Packit Service ac8aad
Packit Service ac8aad
	for (p_pre_patch_callback = __kpatch_callbacks_pre_patch;
Packit Service ac8aad
	     p_pre_patch_callback < __kpatch_callbacks_pre_patch_end;
Packit Service ac8aad
	     p_pre_patch_callback++) {
Packit Service ac8aad
Packit Service ac8aad
		object = patch_find_or_add_object(objects, p_pre_patch_callback->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		if (object->pre_patch_callback) {
Packit Service ac8aad
			pr_err("extra pre-patch callback for object: %s\n",
Packit Service ac8aad
				object->name);
Packit Service ac8aad
			return -EINVAL;
Packit Service ac8aad
		}
Packit Service ac8aad
Packit Service ac8aad
		object->pre_patch_callback =
Packit Service ac8aad
			(int (*)(struct kpatch_object *)) p_pre_patch_callback->callback;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	for (p_post_patch_callback = __kpatch_callbacks_post_patch;
Packit Service ac8aad
	     p_post_patch_callback < __kpatch_callbacks_post_patch_end;
Packit Service ac8aad
	     p_post_patch_callback++) {
Packit Service ac8aad
Packit Service ac8aad
		object = patch_find_or_add_object(objects, p_post_patch_callback->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		if (object->post_patch_callback) {
Packit Service ac8aad
			pr_err("extra post-patch callback for object: %s\n",
Packit Service ac8aad
				object->name);
Packit Service ac8aad
			return -EINVAL;
Packit Service ac8aad
		}
Packit Service ac8aad
Packit Service ac8aad
		object->post_patch_callback =
Packit Service ac8aad
			(void (*)(struct kpatch_object *)) p_post_patch_callback->callback;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	for (p_pre_unpatch_callback = __kpatch_callbacks_pre_unpatch;
Packit Service ac8aad
	     p_pre_unpatch_callback < __kpatch_callbacks_pre_unpatch_end;
Packit Service ac8aad
	     p_pre_unpatch_callback++) {
Packit Service ac8aad
Packit Service ac8aad
		object = patch_find_or_add_object(objects, p_pre_unpatch_callback->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		if (object->pre_unpatch_callback) {
Packit Service ac8aad
			pr_err("extra pre-unpatch callback for object: %s\n",
Packit Service ac8aad
				object->name);
Packit Service ac8aad
			return -EINVAL;
Packit Service ac8aad
		}
Packit Service ac8aad
Packit Service ac8aad
		object->pre_unpatch_callback =
Packit Service ac8aad
			(void (*)(struct kpatch_object *)) p_pre_unpatch_callback->callback;
Packit Service ac8aad
	}
Packit Service ac8aad
Packit Service ac8aad
	for (p_post_unpatch_callback = __kpatch_callbacks_post_unpatch;
Packit Service ac8aad
	     p_post_unpatch_callback < __kpatch_callbacks_post_unpatch_end;
Packit Service ac8aad
	     p_post_unpatch_callback++) {
Packit Service ac8aad
Packit Service ac8aad
		object = patch_find_or_add_object(objects, p_post_unpatch_callback->objname);
Packit Service ac8aad
		if (!object)
Packit Service ac8aad
			return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
		if (object->post_unpatch_callback) {
Packit Service ac8aad
			pr_err("extra post-unpatch callback for object: %s\n",
Packit Service ac8aad
				object->name);
Packit Service ac8aad
			return -EINVAL;
Packit Service ac8aad
		}
Packit Service ac8aad
Packit Service ac8aad
		object->post_unpatch_callback =
Packit Service ac8aad
			(void (*)(struct kpatch_object *)) p_post_unpatch_callback->callback;
Packit Service ac8aad
	}
Packit Service ac8aad
	return 0;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static int __init patch_init(void)
Packit Service ac8aad
{
Packit Service ac8aad
	int ret;
Packit Service ac8aad
Packit Service ac8aad
	ret = kobject_init_and_add(&kpmod.kobj, &patch_ktype,
Packit Service ac8aad
				   kpatch_root_kobj, "%s",
Packit Service ac8aad
				   THIS_MODULE->name);
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		return -ENOMEM;
Packit Service ac8aad
Packit Service ac8aad
	kpmod.mod = THIS_MODULE;
Packit Service ac8aad
	INIT_LIST_HEAD(&kpmod.objects);
Packit Service ac8aad
Packit Service ac8aad
	ret = patch_make_funcs_list(&kpmod.objects);
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		goto err_objects;
Packit Service ac8aad
Packit Service ac8aad
	ret = patch_make_dynrelas_list(&kpmod.objects);
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		goto err_objects;
Packit Service ac8aad
Packit Service ac8aad
	ret = patch_set_callbacks(&kpmod.objects);
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		goto err_objects;
Packit Service ac8aad
Packit Service ac8aad
	ret = kpatch_register(&kpmod, replace);
Packit Service ac8aad
	if (ret)
Packit Service ac8aad
		goto err_objects;
Packit Service ac8aad
Packit Service ac8aad
	return 0;
Packit Service ac8aad
Packit Service ac8aad
err_objects:
Packit Service ac8aad
	patch_free_objects();
Packit Service ac8aad
	kobject_put(&kpmod.kobj);
Packit Service ac8aad
	return ret;
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
static void __exit patch_exit(void)
Packit Service ac8aad
{
Packit Service ac8aad
	WARN_ON(kpmod.enabled);
Packit Service ac8aad
Packit Service ac8aad
	patch_free_objects();
Packit Service ac8aad
	kobject_put(&kpmod.kobj);
Packit Service ac8aad
}
Packit Service ac8aad
Packit Service ac8aad
module_init(patch_init);
Packit Service ac8aad
module_exit(patch_exit);
Packit Service ac8aad
MODULE_LICENSE("GPL");