Lines Matching +full:attr +full:- +full:cnt +full:- +full:name
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * core.c - Kernel Live Patching Core
31 * accesses to klp-related variables and structures must have mutex protection,
34 * - klp_ftrace_handler()
35 * - klp_update_patch_state()
36 * - __klp_sched_try_switch()
51 return obj->name;
54 /* sets obj->mod if object is not vmlinux and module is found */
68 mod = find_module(obj->name);
73 * until mod->exit() finishes. This is especially important for
76 if (mod && mod->klp_alive)
77 obj->mod = mod;
91 if ((strcmp(old_func->old_name, func->old_name) == 0) &&
92 (old_func->old_sympos == func->old_sympos)) {
108 strcmp(old_obj->name, obj->name) == 0) {
120 const char *name;
130 args->addr = addr;
131 args->count++;
135 * or the position is not defined for a non-unique symbol.
137 if ((args->pos && (args->count == args->pos)) ||
138 (!args->pos && (args->count > 1)))
144 static int klp_find_callback(void *data, const char *name, unsigned long addr)
148 if (strcmp(args->name, name))
154 static int klp_find_object_symbol(const char *objname, const char *name,
158 .name = name,
167 kallsyms_on_each_match_symbol(klp_match_callback, name, &args);
174 pr_err("symbol '%s' not found in symbol table\n", name);
177 name, objname);
180 sympos, name, objname ? objname : "vmlinux");
187 return -EINVAL;
194 int i, cnt, ret;
205 * call are hard-coded and correspond to MODULE_NAME_LEN and
215 relas = (Elf_Rela *) relasec->sh_addr;
217 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
219 if (sym->st_shndx != SHN_LIVEPATCH) {
221 strtab + sym->st_name);
222 return -EINVAL;
226 cnt = sscanf(strtab + sym->st_name,
229 if (cnt != 3) {
230 pr_err("symbol %s has an incorrectly formatted name\n",
231 strtab + sym->st_name);
232 return -EINVAL;
238 * Prevent module-specific KLP rela sections from referencing
244 pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section\n",
246 return -EINVAL;
255 sym->st_value = addr;
270 * At a high-level, there are two types of klp relocation sections: those which
275 * write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections).
282 * 2) When a to-be-patched module loads -- or is already loaded when a
283 * corresponding klp module loads -- klp code calls this function to write
284 * module-specific klp relocations (.klp.rela.{module}.* sections). These
286 * reference symbols which live in the to-be-patched module or one of its
289 * the to-be-patched module to be loaded and patched sometime *after* the
297 int cnt, ret;
306 cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]",
308 if (cnt != 1) {
309 pr_err("section %s has an incorrectly formatted name\n",
310 shstrtab + sec->sh_name);
311 return -EINVAL;
355 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
370 if (patch->enabled == enabled) {
372 ret = -EINVAL;
381 * Do not allow to re-enable a disabled patch.
388 ret = -EINVAL;
399 struct kobj_attribute *attr, char *buf)
404 return sysfs_emit(buf, "%d\n", patch->enabled);
408 struct kobj_attribute *attr, char *buf)
416 static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
435 return -EINVAL;
446 struct kobj_attribute *attr, char *buf)
451 return sysfs_emit(buf, "%d\n", patch->replace);
455 struct kobj_attribute *attr, char *buf)
481 &enabled_kobj_attr.attr,
482 &transition_kobj_attr.attr,
483 &force_kobj_attr.attr,
484 &replace_kobj_attr.attr,
485 &stack_order_kobj_attr.attr,
491 struct kobj_attribute *attr, char *buf)
496 return sysfs_emit(buf, "%d\n", obj->patched);
501 &patched_kobj_attr.attr,
508 kfree(obj->name);
517 static struct klp_object *klp_alloc_object_dynamic(const char *name,
526 if (name) {
527 obj->name = kstrdup(name, GFP_KERNEL);
528 if (!obj->name) {
535 obj->dynamic = true;
542 kfree(func->old_name);
555 if (old_func->old_name) {
556 func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
557 if (!func->old_name) {
565 * func->new_func is same as func->old_func. These addresses are
568 func->old_sympos = old_func->old_sympos;
569 func->nop = true;
583 obj = klp_alloc_object_dynamic(old_obj->name, patch);
585 return -ENOMEM;
595 return -ENOMEM;
632 complete(&patch->finish);
647 if (obj->dynamic)
663 if (func->nop)
677 if (nops_only && !func->nop)
680 list_del(&func->node);
681 kobject_put(&func->kobj);
690 obj->mod = NULL;
693 func->old_func = NULL;
695 if (func->nop)
696 func->new_func = NULL;
707 if (nops_only && !obj->dynamic)
710 list_del(&obj->node);
711 kobject_put(&obj->kobj);
734 if (!list_empty(&patch->list))
735 list_del(&patch->list);
756 kobject_put(&patch->kobj);
757 wait_for_completion(&patch->finish);
760 if (!patch->forced)
761 module_put(patch->mod);
780 schedule_work(&patch->free_work);
796 if (!func->old_name)
797 return -EINVAL;
803 if (!func->new_func && !func->nop)
804 return -EINVAL;
806 if (strlen(func->old_name) >= KSYM_NAME_LEN)
807 return -EINVAL;
809 INIT_LIST_HEAD(&func->stack_node);
810 func->patched = false;
811 func->transition = false;
818 return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
819 func->old_name,
820 func->old_sympos ? func->old_sympos : 1);
828 struct klp_modinfo *info = patch->mod->klp_info;
830 for (i = 1; i < info->hdr.e_shnum; i++) {
831 Elf_Shdr *sec = info->sechdrs + i;
833 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
836 ret = klp_write_section_relocs(patch->mod, info->sechdrs,
837 info->secstrings,
838 patch->mod->core_kallsyms.strtab,
839 info->symndx, i, obj->name, apply);
868 * Only write module-specific relocations here
869 * (.klp.rela.{module}.*). vmlinux-specific relocations were
879 ret = klp_find_object_symbol(obj->name, func->old_name,
880 func->old_sympos,
881 (unsigned long *)&func->old_func);
885 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
886 &func->old_size, NULL);
889 func->old_name);
890 return -ENOENT;
893 if (func->nop)
894 func->new_func = func->old_func;
896 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
897 &func->new_size, NULL);
900 func->old_name);
901 return -ENOENT;
912 const char *name;
914 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
915 return -EINVAL;
917 obj->patched = false;
918 obj->mod = NULL;
922 name = klp_is_module(obj) ? obj->name : "vmlinux";
923 ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
942 kobject_init(&func->kobj, &klp_ktype_func);
943 list_add_tail(&func->node, &obj->func_list);
949 INIT_LIST_HEAD(&obj->func_list);
950 kobject_init(&obj->kobj, &klp_ktype_object);
951 list_add_tail(&obj->node, &patch->obj_list);
959 INIT_LIST_HEAD(&patch->list);
960 INIT_LIST_HEAD(&patch->obj_list);
961 kobject_init(&patch->kobj, &klp_ktype_patch);
962 patch->enabled = false;
963 patch->forced = false;
964 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
965 init_completion(&patch->finish);
981 ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
985 if (patch->replace) {
997 list_add_tail(&patch->list, &klp_patches);
1006 if (WARN_ON(!patch->enabled))
1007 return -EINVAL;
1010 return -EBUSY;
1015 if (obj->patched)
1019 * Enforce the order of the func->transition writes in
1023 * this ensures the handler sees that func->transition is set.
1028 patch->enabled = false;
1040 return -EBUSY;
1042 if (WARN_ON(patch->enabled))
1043 return -EINVAL;
1045 pr_notice("enabling patch '%s'\n", patch->mod->name);
1050 * Enforce the order of the func->transition writes in
1051 * klp_init_transition() and the ops->func_stack writes in
1053 * func->transition updates before the handler is registered and the
1064 pr_warn("pre-patch callback failed for object '%s'\n",
1065 klp_is_module(obj) ? obj->name : "vmlinux");
1072 klp_is_module(obj) ? obj->name : "vmlinux");
1078 patch->enabled = true;
1083 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
1090 * klp_enable_patch() - enable the livepatch
1107 if (!patch || !patch->mod || !patch->objs)
1108 return -EINVAL;
1111 if (!obj->funcs)
1112 return -EINVAL;
1116 if (!is_livepatch_module(patch->mod)) {
1118 patch->mod->name);
1119 return -EINVAL;
1123 return -ENODEV;
1134 patch->mod->name);
1136 return -EINVAL;
1139 if (!try_module_get(patch->mod)) {
1141 return -ENODEV;
1182 * this is handled transparently by patch->module_put.
1192 old_patch->enabled = false;
1204 * will see a valid ops->func_stack entry thanks to RCU.
1207 * in ops->func_stack. Therefore unregister_ftrace_function() is called.
1235 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1242 patch->mod->name, obj->mod->name);
1259 if (WARN_ON(mod->state != MODULE_STATE_COMING))
1260 return -EINVAL;
1262 if (!strcmp(mod->name, "vmlinux")) {
1263 pr_err("vmlinux.ko: invalid module name\n");
1264 return -EINVAL;
1273 mod->klp_alive = true;
1277 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
1280 obj->mod = mod;
1285 patch->mod->name, obj->mod->name, ret);
1290 patch->mod->name, obj->mod->name);
1294 pr_warn("pre-patch callback failed for object '%s'\n",
1295 obj->name);
1302 patch->mod->name, obj->mod->name, ret);
1325 patch->mod->name, obj->mod->name, obj->mod->name);
1326 mod->klp_alive = false;
1327 obj->mod = NULL;
1336 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
1337 mod->state != MODULE_STATE_COMING))
1346 mod->klp_alive = false;
1357 return -ENOMEM;