core.c 22 KB

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