core.c 21 KB

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