2014-03-05 03:34:03 +00:00
|
|
|
/*
|
2014-04-21 16:49:58 +00:00
|
|
|
* Copyright (C) 2013-2014 Josh Poimboeuf <jpoimboe@redhat.com>
|
2014-03-05 03:34:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2014-02-11 16:25:48 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/printk.h>
|
2014-03-14 21:41:00 +00:00
|
|
|
#include <linux/slab.h>
|
2014-05-24 17:09:28 +00:00
|
|
|
#include <linux/kallsyms.h>
|
2014-02-13 03:53:05 +00:00
|
|
|
#include "kpatch.h"
|
2014-06-17 14:51:47 +00:00
|
|
|
#include "kpatch-patch.h"
|
2014-02-11 16:25:48 +00:00
|
|
|
|
2014-04-28 21:18:29 +00:00
|
|
|
static bool replace;
|
|
|
|
module_param(replace, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(replace, "replace all previously loaded patch modules");
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
extern struct kpatch_patch_func __kpatch_funcs[], __kpatch_funcs_end[];
|
|
|
|
extern struct kpatch_patch_dynrela __kpatch_dynrelas[], __kpatch_dynrelas_end[];
|
2014-06-27 17:01:39 +00:00
|
|
|
extern struct kpatch_patch_hook __kpatch_hooks_load[], __kpatch_hooks_load_end[];
|
|
|
|
extern struct kpatch_patch_hook __kpatch_hooks_unload[], __kpatch_hooks_unload_end[];
|
2014-07-02 17:35:18 +00:00
|
|
|
extern unsigned long __kpatch_force_funcs[], __kpatch_force_funcs_end[];
|
2014-09-04 14:38:17 +00:00
|
|
|
extern char __kpatch_checksum[];
|
2014-02-11 16:25:48 +00:00
|
|
|
|
2014-04-21 16:29:01 +00:00
|
|
|
static struct kpatch_module kpmod;
|
2014-04-26 04:05:26 +00:00
|
|
|
|
|
|
|
static ssize_t patch_enabled_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
return sprintf(buf, "%d\n", kpmod.enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t patch_enabled_store(struct kobject *kobj,
|
2014-05-12 20:20:09 +00:00
|
|
|
struct kobj_attribute *attr, const char *buf,
|
2014-04-26 04:05:26 +00:00
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
ret = kstrtoul(buf, 10, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
val = !!val;
|
|
|
|
|
|
|
|
if (val)
|
2014-09-04 14:38:17 +00:00
|
|
|
ret = kpatch_register(&kpmod, replace);
|
|
|
|
else
|
|
|
|
ret = kpatch_unregister(&kpmod);
|
2014-04-26 04:05:26 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static ssize_t patch_checksum_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
2014-09-04 14:38:17 +00:00
|
|
|
{
|
|
|
|
return snprintf(buf, PAGE_SIZE, "%s\n", __kpatch_checksum);
|
|
|
|
}
|
|
|
|
|
2014-04-26 04:05:26 +00:00
|
|
|
static struct kobj_attribute patch_enabled_attr =
|
|
|
|
__ATTR(enabled, 0644, patch_enabled_show, patch_enabled_store);
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct kobj_attribute patch_checksum_attr =
|
|
|
|
__ATTR(checksum, 0444, patch_checksum_show, NULL);
|
2014-09-04 14:38:17 +00:00
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct attribute *patch_attrs[] = {
|
2014-09-04 14:38:17 +00:00
|
|
|
&patch_enabled_attr.attr,
|
2014-09-11 02:10:08 +00:00
|
|
|
&patch_checksum_attr.attr,
|
2014-09-04 14:38:17 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
static void patch_kobj_free(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kobj_type patch_ktype = {
|
|
|
|
.release = patch_kobj_free,
|
|
|
|
.sysfs_ops = &kobj_sysfs_ops,
|
|
|
|
.default_attrs = patch_attrs,
|
2014-09-04 14:38:17 +00:00
|
|
|
};
|
2014-03-14 21:41:00 +00:00
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static ssize_t patch_func_old_addr_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
2014-05-24 17:09:28 +00:00
|
|
|
{
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
struct kpatch_func *func =
|
|
|
|
container_of(kobj, struct kpatch_func, kobj);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
return sprintf(buf, "0x%lx\n", func->old_addr);
|
2014-05-24 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static ssize_t patch_func_new_addr_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
2014-05-24 17:09:28 +00:00
|
|
|
{
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
struct kpatch_func *func =
|
|
|
|
container_of(kobj, struct kpatch_func, kobj);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
return sprintf(buf, "0x%lx\n", func->new_addr);
|
2014-05-24 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct kobj_attribute patch_old_addr_attr =
|
2014-11-08 05:30:32 +00:00
|
|
|
__ATTR(old_addr, S_IRUSR, patch_func_old_addr_show, NULL);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct kobj_attribute patch_new_addr_attr =
|
2014-11-08 05:30:32 +00:00
|
|
|
__ATTR(new_addr, S_IRUSR, patch_func_new_addr_show, NULL);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct attribute *patch_func_kobj_attrs[] = {
|
|
|
|
&patch_old_addr_attr.attr,
|
|
|
|
&patch_new_addr_attr.attr,
|
2014-05-24 17:09:28 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static ssize_t patch_func_kobj_show(struct kobject *kobj,
|
|
|
|
struct attribute *attr, char *buf)
|
2014-05-24 17:09:28 +00:00
|
|
|
{
|
|
|
|
struct kobj_attribute *func_attr =
|
|
|
|
container_of(attr, struct kobj_attribute, attr);
|
|
|
|
|
|
|
|
return func_attr->show(kobj, func_attr, buf);
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static const struct sysfs_ops patch_func_sysfs_ops = {
|
|
|
|
.show = patch_func_kobj_show,
|
2014-05-24 17:09:28 +00:00
|
|
|
};
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
static void patch_func_kobj_free(struct kobject *kobj)
|
|
|
|
{
|
|
|
|
struct kpatch_func *func =
|
|
|
|
container_of(kobj, struct kpatch_func, kobj);
|
|
|
|
kfree(func);
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static struct kobj_type patch_func_ktype = {
|
|
|
|
.release = patch_func_kobj_free,
|
|
|
|
.sysfs_ops = &patch_func_sysfs_ops,
|
|
|
|
.default_attrs = patch_func_kobj_attrs,
|
2014-05-24 17:09:28 +00:00
|
|
|
};
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
static void patch_object_kobj_free(struct kobject *kobj)
|
2014-05-24 17:09:28 +00:00
|
|
|
{
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
struct kpatch_object *obj =
|
|
|
|
container_of(kobj, struct kpatch_object, kobj);
|
|
|
|
kfree(obj);
|
2014-05-24 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
static struct kobj_type patch_object_ktype = {
|
|
|
|
.release = patch_object_kobj_free,
|
|
|
|
.sysfs_ops = &kobj_sysfs_ops,
|
|
|
|
};
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
static struct kpatch_object *patch_find_or_add_object(struct list_head *head,
|
|
|
|
const char *name)
|
2014-02-11 16:25:48 +00:00
|
|
|
{
|
2014-06-17 14:51:47 +00:00
|
|
|
struct kpatch_object *object;
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
int ret;
|
2014-04-21 16:29:01 +00:00
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
list_for_each_entry(object, head, list) {
|
|
|
|
if (!strcmp(object->name, name))
|
|
|
|
return object;
|
2014-05-24 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
object = kzalloc(sizeof(*object), GFP_KERNEL);
|
|
|
|
if (!object)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
object->name = name;
|
|
|
|
INIT_LIST_HEAD(&object->funcs);
|
|
|
|
INIT_LIST_HEAD(&object->dynrelas);
|
2014-06-27 17:01:39 +00:00
|
|
|
INIT_LIST_HEAD(&object->hooks_load);
|
|
|
|
INIT_LIST_HEAD(&object->hooks_unload);
|
2014-06-17 14:51:47 +00:00
|
|
|
|
|
|
|
list_add_tail(&object->list, head);
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
ret = kobject_init_and_add(&object->kobj, &patch_object_ktype,
|
|
|
|
&kpmod.kobj, "%s", object->name);
|
|
|
|
if (ret) {
|
|
|
|
list_del(&object->list);
|
|
|
|
kfree(object);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patch_free_objects(void)
|
|
|
|
{
|
|
|
|
struct kpatch_object *object, *object_safe;
|
|
|
|
struct kpatch_func *func, *func_safe;
|
|
|
|
struct kpatch_dynrela *dynrela, *dynrela_safe;
|
2014-06-27 17:01:39 +00:00
|
|
|
struct kpatch_hook *hook, *hook_safe;
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
list_for_each_entry_safe(object, object_safe, &kpmod.objects, list) {
|
|
|
|
list_for_each_entry_safe(func, func_safe, &object->funcs,
|
|
|
|
list) {
|
|
|
|
list_del(&func->list);
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
kobject_put(&func->kobj);
|
2014-06-17 14:51:47 +00:00
|
|
|
}
|
|
|
|
list_for_each_entry_safe(dynrela, dynrela_safe,
|
|
|
|
&object->dynrelas, list) {
|
|
|
|
list_del(&dynrela->list);
|
|
|
|
kfree(dynrela);
|
|
|
|
}
|
2014-06-27 17:01:39 +00:00
|
|
|
list_for_each_entry_safe(hook, hook_safe,
|
|
|
|
&object->hooks_load, list) {
|
|
|
|
list_del(&hook->list);
|
|
|
|
kfree(hook);
|
|
|
|
}
|
|
|
|
list_for_each_entry_safe(hook, hook_safe,
|
|
|
|
&object->hooks_unload, list) {
|
|
|
|
list_del(&hook->list);
|
|
|
|
kfree(hook);
|
|
|
|
}
|
2014-06-17 14:51:47 +00:00
|
|
|
list_del(&object->list);
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
kobject_put(&object->kobj);
|
2014-06-17 14:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:10:08 +00:00
|
|
|
static int patch_is_func_forced(unsigned long addr)
|
2014-07-02 17:35:18 +00:00
|
|
|
{
|
|
|
|
unsigned long *a;
|
|
|
|
for (a = __kpatch_force_funcs; a < __kpatch_force_funcs_end; a++)
|
|
|
|
if (*a == addr)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:17:58 +00:00
|
|
|
static int patch_make_funcs_list(struct list_head *objects)
|
2014-06-17 14:51:47 +00:00
|
|
|
{
|
|
|
|
struct kpatch_object *object;
|
|
|
|
struct kpatch_patch_func *p_func;
|
|
|
|
struct kpatch_func *func;
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
int ret;
|
2014-06-17 14:51:47 +00:00
|
|
|
|
|
|
|
for (p_func = __kpatch_funcs; p_func < __kpatch_funcs_end; p_func++) {
|
|
|
|
object = patch_find_or_add_object(&kpmod.objects,
|
|
|
|
p_func->objname);
|
2014-06-24 17:50:26 +00:00
|
|
|
if (!object)
|
|
|
|
return -ENOMEM;
|
2014-06-17 14:51:47 +00:00
|
|
|
|
|
|
|
func = kzalloc(sizeof(*func), GFP_KERNEL);
|
2014-06-24 17:50:26 +00:00
|
|
|
if (!func)
|
|
|
|
return -ENOMEM;
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
func->new_addr = p_func->new_addr;
|
|
|
|
func->new_size = p_func->new_size;
|
|
|
|
func->old_size = p_func->old_size;
|
2016-11-15 21:02:09 +00:00
|
|
|
func->sympos = p_func->sympos;
|
2014-06-17 14:51:47 +00:00
|
|
|
func->name = p_func->name;
|
2014-09-11 02:10:08 +00:00
|
|
|
func->force = patch_is_func_forced(func->new_addr);
|
2014-06-17 14:51:47 +00:00
|
|
|
list_add_tail(&func->list, &object->funcs);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
ret = kobject_init_and_add(&func->kobj, &patch_func_ktype,
|
|
|
|
&object->kobj, "%s,%lu",
|
|
|
|
func->name, func->sympos ? func->sympos : 1);
|
2014-05-24 17:09:28 +00:00
|
|
|
if (ret)
|
2014-06-24 17:50:26 +00:00
|
|
|
return ret;
|
2014-06-17 14:51:47 +00:00
|
|
|
}
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-06-17 17:17:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int patch_make_dynrelas_list(struct list_head *objects)
|
|
|
|
{
|
|
|
|
struct kpatch_object *object;
|
|
|
|
struct kpatch_patch_dynrela *p_dynrela;
|
|
|
|
struct kpatch_dynrela *dynrela;
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
for (p_dynrela = __kpatch_dynrelas; p_dynrela < __kpatch_dynrelas_end;
|
|
|
|
p_dynrela++) {
|
2014-06-17 17:17:58 +00:00
|
|
|
object = patch_find_or_add_object(objects, p_dynrela->objname);
|
2014-06-24 17:50:26 +00:00
|
|
|
if (!object)
|
|
|
|
return -ENOMEM;
|
2014-06-17 14:51:47 +00:00
|
|
|
|
|
|
|
dynrela = kzalloc(sizeof(*dynrela), GFP_KERNEL);
|
2014-06-24 17:50:26 +00:00
|
|
|
if (!dynrela)
|
|
|
|
return -ENOMEM;
|
2014-06-17 14:51:47 +00:00
|
|
|
|
|
|
|
dynrela->dest = p_dynrela->dest;
|
|
|
|
dynrela->type = p_dynrela->type;
|
2016-11-15 21:02:09 +00:00
|
|
|
dynrela->sympos = p_dynrela->sympos;
|
2014-06-17 14:51:47 +00:00
|
|
|
dynrela->name = p_dynrela->name;
|
2014-10-04 02:47:35 +00:00
|
|
|
dynrela->external = p_dynrela->external;
|
2014-06-17 14:51:47 +00:00
|
|
|
dynrela->addend = p_dynrela->addend;
|
|
|
|
list_add_tail(&dynrela->list, &object->dynrelas);
|
2014-05-24 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 17:17:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 17:01:39 +00:00
|
|
|
static int patch_make_hook_lists(struct list_head *objects)
|
|
|
|
{
|
|
|
|
struct kpatch_object *object;
|
|
|
|
struct kpatch_patch_hook *p_hook;
|
|
|
|
struct kpatch_hook *hook;
|
|
|
|
|
|
|
|
for (p_hook = __kpatch_hooks_load; p_hook < __kpatch_hooks_load_end;
|
|
|
|
p_hook++) {
|
|
|
|
object = patch_find_or_add_object(objects, p_hook->objname);
|
|
|
|
if (!object)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hook = kzalloc(sizeof(*hook), GFP_KERNEL);
|
|
|
|
if (!hook)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hook->hook = p_hook->hook;
|
|
|
|
list_add_tail(&hook->list, &object->hooks_load);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p_hook = __kpatch_hooks_unload; p_hook < __kpatch_hooks_unload_end;
|
|
|
|
p_hook++) {
|
|
|
|
object = patch_find_or_add_object(objects, p_hook->objname);
|
|
|
|
if (!object)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hook = kzalloc(sizeof(*hook), GFP_KERNEL);
|
|
|
|
if (!hook)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hook->hook = p_hook->hook;
|
|
|
|
list_add_tail(&hook->list, &object->hooks_unload);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-17 17:17:58 +00:00
|
|
|
static int __init patch_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
ret = kobject_init_and_add(&kpmod.kobj, &patch_ktype,
|
|
|
|
kpatch_root_kobj, "%s",
|
|
|
|
THIS_MODULE->name);
|
|
|
|
if (ret)
|
2014-06-17 17:17:58 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
kpmod.mod = THIS_MODULE;
|
|
|
|
INIT_LIST_HEAD(&kpmod.objects);
|
|
|
|
|
|
|
|
ret = patch_make_funcs_list(&kpmod.objects);
|
|
|
|
if (ret)
|
2014-06-24 17:50:26 +00:00
|
|
|
goto err_objects;
|
2014-06-17 17:17:58 +00:00
|
|
|
|
|
|
|
ret = patch_make_dynrelas_list(&kpmod.objects);
|
|
|
|
if (ret)
|
|
|
|
goto err_objects;
|
|
|
|
|
2014-06-27 17:01:39 +00:00
|
|
|
ret = patch_make_hook_lists(&kpmod.objects);
|
|
|
|
if (ret)
|
|
|
|
goto err_objects;
|
|
|
|
|
2014-04-28 21:18:29 +00:00
|
|
|
ret = kpatch_register(&kpmod, replace);
|
2014-04-22 13:54:21 +00:00
|
|
|
if (ret)
|
2014-06-17 14:51:47 +00:00
|
|
|
goto err_objects;
|
2014-04-22 13:54:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
err_objects:
|
|
|
|
patch_free_objects();
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
kobject_put(&kpmod.kobj);
|
2014-04-22 13:54:21 +00:00
|
|
|
return ret;
|
2014-02-11 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit patch_exit(void)
|
|
|
|
{
|
2014-04-26 04:05:26 +00:00
|
|
|
WARN_ON(kpmod.enabled);
|
2014-05-24 17:09:28 +00:00
|
|
|
|
2014-06-17 14:51:47 +00:00
|
|
|
patch_free_objects();
|
kmod: restructure kpatch sysfs tree
Restructure kpatch's sysfs interface and mirror the sysfs tree after
livepatch's sysfs layout. With the current sysfs layout, we cannot
distinguish which object a function belongs to, and we cannot tell which
modules/objects are patched. Therefore, restructure the kpatch sysfs tree
such that module/object information is available. With the new layout, each
patched object has its own directory, with each function being a
subdirectory of its object.
Implement this by embedding a kobject struct within the kpatch_module,
kpatch_func, and kpatch_object structs and supplying their ktypes and
kobject release methods.
Before:
/sys/kernel/kpatch
└── patches
└── <patch_module>
├── checksum
├── enabled
└── functions
├── <function> # from <object1>
│ ├── new_addr
│ └── old_addr
├── <function> # from <object2>
│ ├── new_addr
│ └── old_addr
└─── <function> # from <object3>
├── new_addr
└── old_addr
After:
/sys/kernel/kpatch
└── <patch_module>
├── <object1>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── <object2>
│ └── <function,sympos>
│ ├── new_addr
│ └── old_addr
├── checksum
├── enabled
└── <object3>
└── <function,sympos>
├── new_addr
└── old_addr
2017-02-28 03:17:11 +00:00
|
|
|
kobject_put(&kpmod.kobj);
|
2014-02-11 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(patch_init);
|
|
|
|
module_exit(patch_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|