core.c 22 KB

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