core.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * core.c - 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/list.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/livepatch.h>
  29. /**
  30. * struct klp_ops - structure for tracking registered ftrace ops structs
  31. *
  32. * A single ftrace_ops is shared between all enabled replacement functions
  33. * (klp_func structs) which have the same old_addr. This allows the switch
  34. * between function versions to happen instantaneously by updating the klp_ops
  35. * struct's func_stack list. The winner is the klp_func at the top of the
  36. * func_stack (front of the list).
  37. *
  38. * @node: node for the global klp_ops list
  39. * @func_stack: list head for the stack of klp_func's (active func is on top)
  40. * @fops: registered ftrace ops struct
  41. */
  42. struct klp_ops {
  43. struct list_head node;
  44. struct list_head func_stack;
  45. struct ftrace_ops fops;
  46. };
  47. /*
  48. * The klp_mutex protects the global lists and state transitions of any
  49. * structure reachable from them. References to any structure must be obtained
  50. * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
  51. * ensure it gets consistent data).
  52. */
  53. static DEFINE_MUTEX(klp_mutex);
  54. static LIST_HEAD(klp_patches);
  55. static LIST_HEAD(klp_ops);
  56. static struct kobject *klp_root_kobj;
  57. static struct klp_ops *klp_find_ops(unsigned long old_addr)
  58. {
  59. struct klp_ops *ops;
  60. struct klp_func *func;
  61. list_for_each_entry(ops, &klp_ops, node) {
  62. func = list_first_entry(&ops->func_stack, struct klp_func,
  63. stack_node);
  64. if (func->old_addr == old_addr)
  65. return ops;
  66. }
  67. return NULL;
  68. }
  69. static bool klp_is_module(struct klp_object *obj)
  70. {
  71. return obj->name;
  72. }
  73. static bool klp_is_object_loaded(struct klp_object *obj)
  74. {
  75. return !obj->name || obj->mod;
  76. }
  77. /* sets obj->mod if object is not vmlinux and module is found */
  78. static void klp_find_object_module(struct klp_object *obj)
  79. {
  80. struct module *mod;
  81. if (!klp_is_module(obj))
  82. return;
  83. mutex_lock(&module_mutex);
  84. /*
  85. * We do not want to block removal of patched modules and therefore
  86. * we do not take a reference here. The patches are removed by
  87. * a going module handler instead.
  88. */
  89. mod = find_module(obj->name);
  90. /*
  91. * Do not mess work of the module coming and going notifiers.
  92. * Note that the patch might still be needed before the going handler
  93. * is called. Module functions can be called even in the GOING state
  94. * until mod->exit() finishes. This is especially important for
  95. * patches that modify semantic of the functions.
  96. */
  97. if (mod && mod->klp_alive)
  98. obj->mod = mod;
  99. mutex_unlock(&module_mutex);
  100. }
  101. /* klp_mutex must be held by caller */
  102. static bool klp_is_patch_registered(struct klp_patch *patch)
  103. {
  104. struct klp_patch *mypatch;
  105. list_for_each_entry(mypatch, &klp_patches, list)
  106. if (mypatch == patch)
  107. return true;
  108. return false;
  109. }
  110. static bool klp_initialized(void)
  111. {
  112. return klp_root_kobj;
  113. }
  114. struct klp_find_arg {
  115. const char *objname;
  116. const char *name;
  117. unsigned long addr;
  118. /*
  119. * If count == 0, the symbol was not found. If count == 1, a unique
  120. * match was found and addr is set. If count > 1, there is
  121. * unresolvable ambiguity among "count" number of symbols with the same
  122. * name in the same object.
  123. */
  124. unsigned long count;
  125. };
  126. static int klp_find_callback(void *data, const char *name,
  127. struct module *mod, unsigned long addr)
  128. {
  129. struct klp_find_arg *args = data;
  130. if ((mod && !args->objname) || (!mod && args->objname))
  131. return 0;
  132. if (strcmp(args->name, name))
  133. return 0;
  134. if (args->objname && strcmp(args->objname, mod->name))
  135. return 0;
  136. /*
  137. * args->addr might be overwritten if another match is found
  138. * but klp_find_object_symbol() handles this and only returns the
  139. * addr if count == 1.
  140. */
  141. args->addr = addr;
  142. args->count++;
  143. return 0;
  144. }
  145. static int klp_find_object_symbol(const char *objname, const char *name,
  146. unsigned long *addr)
  147. {
  148. struct klp_find_arg args = {
  149. .objname = objname,
  150. .name = name,
  151. .addr = 0,
  152. .count = 0
  153. };
  154. kallsyms_on_each_symbol(klp_find_callback, &args);
  155. if (args.count == 0)
  156. pr_err("symbol '%s' not found in symbol table\n", name);
  157. else if (args.count > 1)
  158. pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
  159. args.count, name, objname);
  160. else {
  161. *addr = args.addr;
  162. return 0;
  163. }
  164. *addr = 0;
  165. return -EINVAL;
  166. }
  167. struct klp_verify_args {
  168. const char *name;
  169. const unsigned long addr;
  170. };
  171. static int klp_verify_callback(void *data, const char *name,
  172. struct module *mod, unsigned long addr)
  173. {
  174. struct klp_verify_args *args = data;
  175. if (!mod &&
  176. !strcmp(args->name, name) &&
  177. args->addr == addr)
  178. return 1;
  179. return 0;
  180. }
  181. static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
  182. {
  183. struct klp_verify_args args = {
  184. .name = name,
  185. .addr = addr,
  186. };
  187. if (kallsyms_on_each_symbol(klp_verify_callback, &args))
  188. return 0;
  189. pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?\n",
  190. name, addr);
  191. return -EINVAL;
  192. }
  193. static int klp_find_verify_func_addr(struct klp_object *obj,
  194. struct klp_func *func)
  195. {
  196. int ret;
  197. #if defined(CONFIG_RANDOMIZE_BASE)
  198. /* KASLR is enabled, disregard old_addr from user */
  199. func->old_addr = 0;
  200. #endif
  201. if (!func->old_addr || klp_is_module(obj))
  202. ret = klp_find_object_symbol(obj->name, func->old_name,
  203. &func->old_addr);
  204. else
  205. ret = klp_verify_vmlinux_symbol(func->old_name,
  206. func->old_addr);
  207. return ret;
  208. }
  209. /*
  210. * external symbols are located outside the parent object (where the parent
  211. * object is either vmlinux or the kmod being patched).
  212. */
  213. static int klp_find_external_symbol(struct module *pmod, const char *name,
  214. unsigned long *addr)
  215. {
  216. const struct kernel_symbol *sym;
  217. /* first, check if it's an exported symbol */
  218. preempt_disable();
  219. sym = find_symbol(name, NULL, NULL, true, true);
  220. if (sym) {
  221. *addr = sym->value;
  222. preempt_enable();
  223. return 0;
  224. }
  225. preempt_enable();
  226. /* otherwise check if it's in another .o within the patch module */
  227. return klp_find_object_symbol(pmod->name, name, addr);
  228. }
  229. static int klp_write_object_relocations(struct module *pmod,
  230. struct klp_object *obj)
  231. {
  232. int ret;
  233. struct klp_reloc *reloc;
  234. if (WARN_ON(!klp_is_object_loaded(obj)))
  235. return -EINVAL;
  236. if (WARN_ON(!obj->relocs))
  237. return -EINVAL;
  238. for (reloc = obj->relocs; reloc->name; reloc++) {
  239. if (!klp_is_module(obj)) {
  240. ret = klp_verify_vmlinux_symbol(reloc->name,
  241. reloc->val);
  242. if (ret)
  243. return ret;
  244. } else {
  245. /* module, reloc->val needs to be discovered */
  246. if (reloc->external)
  247. ret = klp_find_external_symbol(pmod,
  248. reloc->name,
  249. &reloc->val);
  250. else
  251. ret = klp_find_object_symbol(obj->mod->name,
  252. reloc->name,
  253. &reloc->val);
  254. if (ret)
  255. return ret;
  256. }
  257. ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
  258. reloc->val + reloc->addend);
  259. if (ret) {
  260. pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
  261. reloc->name, reloc->val, ret);
  262. return ret;
  263. }
  264. }
  265. return 0;
  266. }
  267. static void notrace klp_ftrace_handler(unsigned long ip,
  268. unsigned long parent_ip,
  269. struct ftrace_ops *fops,
  270. struct pt_regs *regs)
  271. {
  272. struct klp_ops *ops;
  273. struct klp_func *func;
  274. ops = container_of(fops, struct klp_ops, fops);
  275. rcu_read_lock();
  276. func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
  277. stack_node);
  278. if (WARN_ON_ONCE(!func))
  279. goto unlock;
  280. klp_arch_set_pc(regs, (unsigned long)func->new_func);
  281. unlock:
  282. rcu_read_unlock();
  283. }
  284. static void klp_disable_func(struct klp_func *func)
  285. {
  286. struct klp_ops *ops;
  287. WARN_ON(func->state != KLP_ENABLED);
  288. WARN_ON(!func->old_addr);
  289. ops = klp_find_ops(func->old_addr);
  290. if (WARN_ON(!ops))
  291. return;
  292. if (list_is_singular(&ops->func_stack)) {
  293. WARN_ON(unregister_ftrace_function(&ops->fops));
  294. WARN_ON(ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0));
  295. list_del_rcu(&func->stack_node);
  296. list_del(&ops->node);
  297. kfree(ops);
  298. } else {
  299. list_del_rcu(&func->stack_node);
  300. }
  301. func->state = KLP_DISABLED;
  302. }
  303. static int klp_enable_func(struct klp_func *func)
  304. {
  305. struct klp_ops *ops;
  306. int ret;
  307. if (WARN_ON(!func->old_addr))
  308. return -EINVAL;
  309. if (WARN_ON(func->state != KLP_DISABLED))
  310. return -EINVAL;
  311. ops = klp_find_ops(func->old_addr);
  312. if (!ops) {
  313. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  314. if (!ops)
  315. return -ENOMEM;
  316. ops->fops.func = klp_ftrace_handler;
  317. ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
  318. FTRACE_OPS_FL_DYNAMIC |
  319. FTRACE_OPS_FL_IPMODIFY;
  320. list_add(&ops->node, &klp_ops);
  321. INIT_LIST_HEAD(&ops->func_stack);
  322. list_add_rcu(&func->stack_node, &ops->func_stack);
  323. ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 0, 0);
  324. if (ret) {
  325. pr_err("failed to set ftrace filter for function '%s' (%d)\n",
  326. func->old_name, ret);
  327. goto err;
  328. }
  329. ret = register_ftrace_function(&ops->fops);
  330. if (ret) {
  331. pr_err("failed to register ftrace handler for function '%s' (%d)\n",
  332. func->old_name, ret);
  333. ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
  334. goto err;
  335. }
  336. } else {
  337. list_add_rcu(&func->stack_node, &ops->func_stack);
  338. }
  339. func->state = KLP_ENABLED;
  340. return 0;
  341. err:
  342. list_del_rcu(&func->stack_node);
  343. list_del(&ops->node);
  344. kfree(ops);
  345. return ret;
  346. }
  347. static void klp_disable_object(struct klp_object *obj)
  348. {
  349. struct klp_func *func;
  350. for (func = obj->funcs; func->old_name; func++)
  351. if (func->state == KLP_ENABLED)
  352. klp_disable_func(func);
  353. obj->state = KLP_DISABLED;
  354. }
  355. static int klp_enable_object(struct klp_object *obj)
  356. {
  357. struct klp_func *func;
  358. int ret;
  359. if (WARN_ON(obj->state != KLP_DISABLED))
  360. return -EINVAL;
  361. if (WARN_ON(!klp_is_object_loaded(obj)))
  362. return -EINVAL;
  363. for (func = obj->funcs; func->old_name; func++) {
  364. ret = klp_enable_func(func);
  365. if (ret) {
  366. klp_disable_object(obj);
  367. return ret;
  368. }
  369. }
  370. obj->state = KLP_ENABLED;
  371. return 0;
  372. }
  373. static int __klp_disable_patch(struct klp_patch *patch)
  374. {
  375. struct klp_object *obj;
  376. /* enforce stacking: only the last enabled patch can be disabled */
  377. if (!list_is_last(&patch->list, &klp_patches) &&
  378. list_next_entry(patch, list)->state == KLP_ENABLED)
  379. return -EBUSY;
  380. pr_notice("disabling patch '%s'\n", patch->mod->name);
  381. for (obj = patch->objs; obj->funcs; obj++) {
  382. if (obj->state == KLP_ENABLED)
  383. klp_disable_object(obj);
  384. }
  385. patch->state = KLP_DISABLED;
  386. return 0;
  387. }
  388. /**
  389. * klp_disable_patch() - disables a registered patch
  390. * @patch: The registered, enabled patch to be disabled
  391. *
  392. * Unregisters the patched functions from ftrace.
  393. *
  394. * Return: 0 on success, otherwise error
  395. */
  396. int klp_disable_patch(struct klp_patch *patch)
  397. {
  398. int ret;
  399. mutex_lock(&klp_mutex);
  400. if (!klp_is_patch_registered(patch)) {
  401. ret = -EINVAL;
  402. goto err;
  403. }
  404. if (patch->state == KLP_DISABLED) {
  405. ret = -EINVAL;
  406. goto err;
  407. }
  408. ret = __klp_disable_patch(patch);
  409. err:
  410. mutex_unlock(&klp_mutex);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(klp_disable_patch);
  414. static int __klp_enable_patch(struct klp_patch *patch)
  415. {
  416. struct klp_object *obj;
  417. int ret;
  418. if (WARN_ON(patch->state != KLP_DISABLED))
  419. return -EINVAL;
  420. /* enforce stacking: only the first disabled patch can be enabled */
  421. if (patch->list.prev != &klp_patches &&
  422. list_prev_entry(patch, list)->state == KLP_DISABLED)
  423. return -EBUSY;
  424. pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
  425. add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
  426. pr_notice("enabling patch '%s'\n", patch->mod->name);
  427. for (obj = patch->objs; obj->funcs; obj++) {
  428. if (!klp_is_object_loaded(obj))
  429. continue;
  430. ret = klp_enable_object(obj);
  431. if (ret)
  432. goto unregister;
  433. }
  434. patch->state = KLP_ENABLED;
  435. return 0;
  436. unregister:
  437. WARN_ON(__klp_disable_patch(patch));
  438. return ret;
  439. }
  440. /**
  441. * klp_enable_patch() - enables a registered patch
  442. * @patch: The registered, disabled patch to be enabled
  443. *
  444. * Performs the needed symbol lookups and code relocations,
  445. * then registers the patched functions with ftrace.
  446. *
  447. * Return: 0 on success, otherwise error
  448. */
  449. int klp_enable_patch(struct klp_patch *patch)
  450. {
  451. int ret;
  452. mutex_lock(&klp_mutex);
  453. if (!klp_is_patch_registered(patch)) {
  454. ret = -EINVAL;
  455. goto err;
  456. }
  457. ret = __klp_enable_patch(patch);
  458. err:
  459. mutex_unlock(&klp_mutex);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL_GPL(klp_enable_patch);
  463. /*
  464. * Sysfs Interface
  465. *
  466. * /sys/kernel/livepatch
  467. * /sys/kernel/livepatch/<patch>
  468. * /sys/kernel/livepatch/<patch>/enabled
  469. * /sys/kernel/livepatch/<patch>/<object>
  470. * /sys/kernel/livepatch/<patch>/<object>/<func>
  471. */
  472. static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
  473. const char *buf, size_t count)
  474. {
  475. struct klp_patch *patch;
  476. int ret;
  477. unsigned long val;
  478. ret = kstrtoul(buf, 10, &val);
  479. if (ret)
  480. return -EINVAL;
  481. if (val != KLP_DISABLED && val != KLP_ENABLED)
  482. return -EINVAL;
  483. patch = container_of(kobj, struct klp_patch, kobj);
  484. mutex_lock(&klp_mutex);
  485. if (val == patch->state) {
  486. /* already in requested state */
  487. ret = -EINVAL;
  488. goto err;
  489. }
  490. if (val == KLP_ENABLED) {
  491. ret = __klp_enable_patch(patch);
  492. if (ret)
  493. goto err;
  494. } else {
  495. ret = __klp_disable_patch(patch);
  496. if (ret)
  497. goto err;
  498. }
  499. mutex_unlock(&klp_mutex);
  500. return count;
  501. err:
  502. mutex_unlock(&klp_mutex);
  503. return ret;
  504. }
  505. static ssize_t enabled_show(struct kobject *kobj,
  506. struct kobj_attribute *attr, char *buf)
  507. {
  508. struct klp_patch *patch;
  509. patch = container_of(kobj, struct klp_patch, kobj);
  510. return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
  511. }
  512. static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
  513. static struct attribute *klp_patch_attrs[] = {
  514. &enabled_kobj_attr.attr,
  515. NULL
  516. };
  517. static void klp_kobj_release_patch(struct kobject *kobj)
  518. {
  519. /*
  520. * Once we have a consistency model we'll need to module_put() the
  521. * patch module here. See klp_register_patch() for more details.
  522. */
  523. }
  524. static struct kobj_type klp_ktype_patch = {
  525. .release = klp_kobj_release_patch,
  526. .sysfs_ops = &kobj_sysfs_ops,
  527. .default_attrs = klp_patch_attrs,
  528. };
  529. static void klp_kobj_release_func(struct kobject *kobj)
  530. {
  531. }
  532. static struct kobj_type klp_ktype_func = {
  533. .release = klp_kobj_release_func,
  534. .sysfs_ops = &kobj_sysfs_ops,
  535. };
  536. /*
  537. * Free all functions' kobjects in the array up to some limit. When limit is
  538. * NULL, all kobjects are freed.
  539. */
  540. static void klp_free_funcs_limited(struct klp_object *obj,
  541. struct klp_func *limit)
  542. {
  543. struct klp_func *func;
  544. for (func = obj->funcs; func->old_name && func != limit; func++)
  545. kobject_put(&func->kobj);
  546. }
  547. /* Clean up when a patched object is unloaded */
  548. static void klp_free_object_loaded(struct klp_object *obj)
  549. {
  550. struct klp_func *func;
  551. obj->mod = NULL;
  552. for (func = obj->funcs; func->old_name; func++)
  553. func->old_addr = 0;
  554. }
  555. /*
  556. * Free all objects' kobjects in the array up to some limit. When limit is
  557. * NULL, all kobjects are freed.
  558. */
  559. static void klp_free_objects_limited(struct klp_patch *patch,
  560. struct klp_object *limit)
  561. {
  562. struct klp_object *obj;
  563. for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
  564. klp_free_funcs_limited(obj, NULL);
  565. kobject_put(obj->kobj);
  566. }
  567. }
  568. static void klp_free_patch(struct klp_patch *patch)
  569. {
  570. klp_free_objects_limited(patch, NULL);
  571. if (!list_empty(&patch->list))
  572. list_del(&patch->list);
  573. kobject_put(&patch->kobj);
  574. }
  575. static int klp_init_func(struct klp_object *obj, struct klp_func *func)
  576. {
  577. INIT_LIST_HEAD(&func->stack_node);
  578. func->state = KLP_DISABLED;
  579. return kobject_init_and_add(&func->kobj, &klp_ktype_func,
  580. obj->kobj, "%s", func->old_name);
  581. }
  582. /* parts of the initialization that is done only when the object is loaded */
  583. static int klp_init_object_loaded(struct klp_patch *patch,
  584. struct klp_object *obj)
  585. {
  586. struct klp_func *func;
  587. int ret;
  588. if (obj->relocs) {
  589. ret = klp_write_object_relocations(patch->mod, obj);
  590. if (ret)
  591. return ret;
  592. }
  593. for (func = obj->funcs; func->old_name; func++) {
  594. ret = klp_find_verify_func_addr(obj, func);
  595. if (ret)
  596. return ret;
  597. }
  598. return 0;
  599. }
  600. static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
  601. {
  602. struct klp_func *func;
  603. int ret;
  604. const char *name;
  605. if (!obj->funcs)
  606. return -EINVAL;
  607. obj->state = KLP_DISABLED;
  608. obj->mod = NULL;
  609. klp_find_object_module(obj);
  610. name = klp_is_module(obj) ? obj->name : "vmlinux";
  611. obj->kobj = kobject_create_and_add(name, &patch->kobj);
  612. if (!obj->kobj)
  613. return -ENOMEM;
  614. for (func = obj->funcs; func->old_name; func++) {
  615. ret = klp_init_func(obj, func);
  616. if (ret)
  617. goto free;
  618. }
  619. if (klp_is_object_loaded(obj)) {
  620. ret = klp_init_object_loaded(patch, obj);
  621. if (ret)
  622. goto free;
  623. }
  624. return 0;
  625. free:
  626. klp_free_funcs_limited(obj, func);
  627. kobject_put(obj->kobj);
  628. return ret;
  629. }
  630. static int klp_init_patch(struct klp_patch *patch)
  631. {
  632. struct klp_object *obj;
  633. int ret;
  634. if (!patch->objs)
  635. return -EINVAL;
  636. mutex_lock(&klp_mutex);
  637. patch->state = KLP_DISABLED;
  638. ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
  639. klp_root_kobj, "%s", patch->mod->name);
  640. if (ret)
  641. goto unlock;
  642. for (obj = patch->objs; obj->funcs; obj++) {
  643. ret = klp_init_object(patch, obj);
  644. if (ret)
  645. goto free;
  646. }
  647. list_add_tail(&patch->list, &klp_patches);
  648. mutex_unlock(&klp_mutex);
  649. return 0;
  650. free:
  651. klp_free_objects_limited(patch, obj);
  652. kobject_put(&patch->kobj);
  653. unlock:
  654. mutex_unlock(&klp_mutex);
  655. return ret;
  656. }
  657. /**
  658. * klp_unregister_patch() - unregisters a patch
  659. * @patch: Disabled patch to be unregistered
  660. *
  661. * Frees the data structures and removes the sysfs interface.
  662. *
  663. * Return: 0 on success, otherwise error
  664. */
  665. int klp_unregister_patch(struct klp_patch *patch)
  666. {
  667. int ret = 0;
  668. mutex_lock(&klp_mutex);
  669. if (!klp_is_patch_registered(patch)) {
  670. ret = -EINVAL;
  671. goto out;
  672. }
  673. if (patch->state == KLP_ENABLED) {
  674. ret = -EBUSY;
  675. goto out;
  676. }
  677. klp_free_patch(patch);
  678. out:
  679. mutex_unlock(&klp_mutex);
  680. return ret;
  681. }
  682. EXPORT_SYMBOL_GPL(klp_unregister_patch);
  683. /**
  684. * klp_register_patch() - registers a patch
  685. * @patch: Patch to be registered
  686. *
  687. * Initializes the data structure associated with the patch and
  688. * creates the sysfs interface.
  689. *
  690. * Return: 0 on success, otherwise error
  691. */
  692. int klp_register_patch(struct klp_patch *patch)
  693. {
  694. int ret;
  695. if (!klp_initialized())
  696. return -ENODEV;
  697. if (!patch || !patch->mod)
  698. return -EINVAL;
  699. /*
  700. * A reference is taken on the patch module to prevent it from being
  701. * unloaded. Right now, we don't allow patch modules to unload since
  702. * there is currently no method to determine if a thread is still
  703. * running in the patched code contained in the patch module once
  704. * the ftrace registration is successful.
  705. */
  706. if (!try_module_get(patch->mod))
  707. return -ENODEV;
  708. ret = klp_init_patch(patch);
  709. if (ret)
  710. module_put(patch->mod);
  711. return ret;
  712. }
  713. EXPORT_SYMBOL_GPL(klp_register_patch);
  714. static void klp_module_notify_coming(struct klp_patch *patch,
  715. struct klp_object *obj)
  716. {
  717. struct module *pmod = patch->mod;
  718. struct module *mod = obj->mod;
  719. int ret;
  720. ret = klp_init_object_loaded(patch, obj);
  721. if (ret)
  722. goto err;
  723. if (patch->state == KLP_DISABLED)
  724. return;
  725. pr_notice("applying patch '%s' to loading module '%s'\n",
  726. pmod->name, mod->name);
  727. ret = klp_enable_object(obj);
  728. if (!ret)
  729. return;
  730. err:
  731. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  732. pmod->name, mod->name, ret);
  733. }
  734. static void klp_module_notify_going(struct klp_patch *patch,
  735. struct klp_object *obj)
  736. {
  737. struct module *pmod = patch->mod;
  738. struct module *mod = obj->mod;
  739. if (patch->state == KLP_DISABLED)
  740. goto disabled;
  741. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  742. pmod->name, mod->name);
  743. klp_disable_object(obj);
  744. disabled:
  745. klp_free_object_loaded(obj);
  746. }
  747. static int klp_module_notify(struct notifier_block *nb, unsigned long action,
  748. void *data)
  749. {
  750. struct module *mod = data;
  751. struct klp_patch *patch;
  752. struct klp_object *obj;
  753. if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
  754. return 0;
  755. mutex_lock(&klp_mutex);
  756. /*
  757. * Each module has to know that the notifier has been called.
  758. * We never know what module will get patched by a new patch.
  759. */
  760. if (action == MODULE_STATE_COMING)
  761. mod->klp_alive = true;
  762. else /* MODULE_STATE_GOING */
  763. mod->klp_alive = false;
  764. list_for_each_entry(patch, &klp_patches, list) {
  765. for (obj = patch->objs; obj->funcs; obj++) {
  766. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  767. continue;
  768. if (action == MODULE_STATE_COMING) {
  769. obj->mod = mod;
  770. klp_module_notify_coming(patch, obj);
  771. } else /* MODULE_STATE_GOING */
  772. klp_module_notify_going(patch, obj);
  773. break;
  774. }
  775. }
  776. mutex_unlock(&klp_mutex);
  777. return 0;
  778. }
  779. static struct notifier_block klp_module_nb = {
  780. .notifier_call = klp_module_notify,
  781. .priority = INT_MIN+1, /* called late but before ftrace notifier */
  782. };
  783. static int klp_init(void)
  784. {
  785. int ret;
  786. ret = klp_check_compiler_support();
  787. if (ret) {
  788. pr_info("Your compiler is too old; turning off.\n");
  789. return -EINVAL;
  790. }
  791. ret = register_module_notifier(&klp_module_nb);
  792. if (ret)
  793. return ret;
  794. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  795. if (!klp_root_kobj) {
  796. ret = -ENOMEM;
  797. goto unregister;
  798. }
  799. return 0;
  800. unregister:
  801. unregister_module_notifier(&klp_module_nb);
  802. return ret;
  803. }
  804. module_init(klp_init);