core.c 22 KB

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