core.c 22 KB

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