core.c 22 KB

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