core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. kallsyms_on_each_symbol(klp_find_callback, &args);
  155. if (args.count == 0)
  156. pr_err("symbol '%s' not found in symbol table\n", name);
  157. else if (args.count > 1)
  158. pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
  159. args.count, name, objname);
  160. else {
  161. *addr = args.addr;
  162. return 0;
  163. }
  164. *addr = 0;
  165. return -EINVAL;
  166. }
  167. struct klp_verify_args {
  168. const char *name;
  169. const unsigned long addr;
  170. };
  171. static int klp_verify_callback(void *data, const char *name,
  172. struct module *mod, unsigned long addr)
  173. {
  174. struct klp_verify_args *args = data;
  175. if (!mod &&
  176. !strcmp(args->name, name) &&
  177. args->addr == addr)
  178. return 1;
  179. return 0;
  180. }
  181. static int klp_verify_vmlinux_symbol(const char *name, unsigned long addr)
  182. {
  183. struct klp_verify_args args = {
  184. .name = name,
  185. .addr = addr,
  186. };
  187. if (kallsyms_on_each_symbol(klp_verify_callback, &args))
  188. return 0;
  189. pr_err("symbol '%s' not found at specified address 0x%016lx, kernel mismatch?\n",
  190. name, addr);
  191. return -EINVAL;
  192. }
  193. static int klp_find_verify_func_addr(struct klp_object *obj,
  194. struct klp_func *func)
  195. {
  196. int ret;
  197. #if defined(CONFIG_RANDOMIZE_BASE)
  198. /* KASLR is enabled, disregard old_addr from user */
  199. func->old_addr = 0;
  200. #endif
  201. if (!func->old_addr || klp_is_module(obj))
  202. ret = klp_find_object_symbol(obj->name, func->old_name,
  203. &func->old_addr);
  204. else
  205. ret = klp_verify_vmlinux_symbol(func->old_name,
  206. func->old_addr);
  207. return ret;
  208. }
  209. /*
  210. * external symbols are located outside the parent object (where the parent
  211. * object is either vmlinux or the kmod being patched).
  212. */
  213. static int klp_find_external_symbol(struct module *pmod, const char *name,
  214. unsigned long *addr)
  215. {
  216. const struct kernel_symbol *sym;
  217. /* first, check if it's an exported symbol */
  218. preempt_disable();
  219. sym = find_symbol(name, NULL, NULL, true, true);
  220. if (sym) {
  221. *addr = sym->value;
  222. preempt_enable();
  223. return 0;
  224. }
  225. preempt_enable();
  226. /* otherwise check if it's in another .o within the patch module */
  227. return klp_find_object_symbol(pmod->name, name, addr);
  228. }
  229. static int klp_write_object_relocations(struct module *pmod,
  230. struct klp_object *obj)
  231. {
  232. int ret;
  233. struct klp_reloc *reloc;
  234. if (WARN_ON(!klp_is_object_loaded(obj)))
  235. return -EINVAL;
  236. if (WARN_ON(!obj->relocs))
  237. return -EINVAL;
  238. for (reloc = obj->relocs; reloc->name; reloc++) {
  239. if (!klp_is_module(obj)) {
  240. ret = klp_verify_vmlinux_symbol(reloc->name,
  241. reloc->val);
  242. if (ret)
  243. return ret;
  244. } else {
  245. /* module, reloc->val needs to be discovered */
  246. if (reloc->external)
  247. ret = klp_find_external_symbol(pmod,
  248. reloc->name,
  249. &reloc->val);
  250. else
  251. ret = klp_find_object_symbol(obj->mod->name,
  252. reloc->name,
  253. &reloc->val);
  254. if (ret)
  255. return ret;
  256. }
  257. ret = klp_write_module_reloc(pmod, reloc->type, reloc->loc,
  258. reloc->val + reloc->addend);
  259. if (ret) {
  260. pr_err("relocation failed for symbol '%s' at 0x%016lx (%d)\n",
  261. reloc->name, reloc->val, ret);
  262. return ret;
  263. }
  264. }
  265. return 0;
  266. }
  267. static void notrace klp_ftrace_handler(unsigned long ip,
  268. unsigned long parent_ip,
  269. struct ftrace_ops *fops,
  270. struct pt_regs *regs)
  271. {
  272. struct klp_ops *ops;
  273. struct klp_func *func;
  274. ops = container_of(fops, struct klp_ops, fops);
  275. rcu_read_lock();
  276. func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
  277. stack_node);
  278. if (WARN_ON_ONCE(!func))
  279. goto unlock;
  280. klp_arch_set_pc(regs, (unsigned long)func->new_func);
  281. unlock:
  282. rcu_read_unlock();
  283. }
  284. static int klp_disable_func(struct klp_func *func)
  285. {
  286. struct klp_ops *ops;
  287. int ret;
  288. if (WARN_ON(func->state != KLP_ENABLED))
  289. return -EINVAL;
  290. if (WARN_ON(!func->old_addr))
  291. return -EINVAL;
  292. ops = klp_find_ops(func->old_addr);
  293. if (WARN_ON(!ops))
  294. return -EINVAL;
  295. if (list_is_singular(&ops->func_stack)) {
  296. ret = unregister_ftrace_function(&ops->fops);
  297. if (ret) {
  298. pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
  299. func->old_name, ret);
  300. return ret;
  301. }
  302. ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
  303. if (ret)
  304. pr_warn("function unregister succeeded but failed to clear the filter\n");
  305. list_del_rcu(&func->stack_node);
  306. list_del(&ops->node);
  307. kfree(ops);
  308. } else {
  309. list_del_rcu(&func->stack_node);
  310. }
  311. func->state = KLP_DISABLED;
  312. return 0;
  313. }
  314. static int klp_enable_func(struct klp_func *func)
  315. {
  316. struct klp_ops *ops;
  317. int ret;
  318. if (WARN_ON(!func->old_addr))
  319. return -EINVAL;
  320. if (WARN_ON(func->state != KLP_DISABLED))
  321. return -EINVAL;
  322. ops = klp_find_ops(func->old_addr);
  323. if (!ops) {
  324. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  325. if (!ops)
  326. return -ENOMEM;
  327. ops->fops.func = klp_ftrace_handler;
  328. ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
  329. FTRACE_OPS_FL_DYNAMIC |
  330. FTRACE_OPS_FL_IPMODIFY;
  331. list_add(&ops->node, &klp_ops);
  332. INIT_LIST_HEAD(&ops->func_stack);
  333. list_add_rcu(&func->stack_node, &ops->func_stack);
  334. ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 0, 0);
  335. if (ret) {
  336. pr_err("failed to set ftrace filter for function '%s' (%d)\n",
  337. func->old_name, ret);
  338. goto err;
  339. }
  340. ret = register_ftrace_function(&ops->fops);
  341. if (ret) {
  342. pr_err("failed to register ftrace handler for function '%s' (%d)\n",
  343. func->old_name, ret);
  344. ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
  345. goto err;
  346. }
  347. } else {
  348. list_add_rcu(&func->stack_node, &ops->func_stack);
  349. }
  350. func->state = KLP_ENABLED;
  351. return 0;
  352. err:
  353. list_del_rcu(&func->stack_node);
  354. list_del(&ops->node);
  355. kfree(ops);
  356. return ret;
  357. }
  358. static int klp_disable_object(struct klp_object *obj)
  359. {
  360. struct klp_func *func;
  361. int ret;
  362. for (func = obj->funcs; func->old_name; func++) {
  363. if (func->state != KLP_ENABLED)
  364. continue;
  365. ret = klp_disable_func(func);
  366. if (ret)
  367. return ret;
  368. }
  369. obj->state = KLP_DISABLED;
  370. return 0;
  371. }
  372. static int klp_enable_object(struct klp_object *obj)
  373. {
  374. struct klp_func *func;
  375. int ret;
  376. if (WARN_ON(obj->state != KLP_DISABLED))
  377. return -EINVAL;
  378. if (WARN_ON(!klp_is_object_loaded(obj)))
  379. return -EINVAL;
  380. for (func = obj->funcs; func->old_name; func++) {
  381. ret = klp_enable_func(func);
  382. if (ret)
  383. goto unregister;
  384. }
  385. obj->state = KLP_ENABLED;
  386. return 0;
  387. unregister:
  388. WARN_ON(klp_disable_object(obj));
  389. return ret;
  390. }
  391. static int __klp_disable_patch(struct klp_patch *patch)
  392. {
  393. struct klp_object *obj;
  394. int ret;
  395. /* enforce stacking: only the last enabled patch can be disabled */
  396. if (!list_is_last(&patch->list, &klp_patches) &&
  397. list_next_entry(patch, list)->state == KLP_ENABLED)
  398. return -EBUSY;
  399. pr_notice("disabling patch '%s'\n", patch->mod->name);
  400. for (obj = patch->objs; obj->funcs; obj++) {
  401. if (obj->state != KLP_ENABLED)
  402. continue;
  403. ret = klp_disable_object(obj);
  404. if (ret)
  405. return ret;
  406. }
  407. patch->state = KLP_DISABLED;
  408. return 0;
  409. }
  410. /**
  411. * klp_disable_patch() - disables a registered patch
  412. * @patch: The registered, enabled patch to be disabled
  413. *
  414. * Unregisters the patched functions from ftrace.
  415. *
  416. * Return: 0 on success, otherwise error
  417. */
  418. int klp_disable_patch(struct klp_patch *patch)
  419. {
  420. int ret;
  421. mutex_lock(&klp_mutex);
  422. if (!klp_is_patch_registered(patch)) {
  423. ret = -EINVAL;
  424. goto err;
  425. }
  426. if (patch->state == KLP_DISABLED) {
  427. ret = -EINVAL;
  428. goto err;
  429. }
  430. ret = __klp_disable_patch(patch);
  431. err:
  432. mutex_unlock(&klp_mutex);
  433. return ret;
  434. }
  435. EXPORT_SYMBOL_GPL(klp_disable_patch);
  436. static int __klp_enable_patch(struct klp_patch *patch)
  437. {
  438. struct klp_object *obj;
  439. int ret;
  440. if (WARN_ON(patch->state != KLP_DISABLED))
  441. return -EINVAL;
  442. /* enforce stacking: only the first disabled patch can be enabled */
  443. if (patch->list.prev != &klp_patches &&
  444. list_prev_entry(patch, list)->state == KLP_DISABLED)
  445. return -EBUSY;
  446. pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
  447. add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
  448. pr_notice("enabling patch '%s'\n", patch->mod->name);
  449. for (obj = patch->objs; obj->funcs; obj++) {
  450. klp_find_object_module(obj);
  451. if (!klp_is_object_loaded(obj))
  452. continue;
  453. ret = klp_enable_object(obj);
  454. if (ret)
  455. goto unregister;
  456. }
  457. patch->state = KLP_ENABLED;
  458. return 0;
  459. unregister:
  460. WARN_ON(__klp_disable_patch(patch));
  461. return ret;
  462. }
  463. /**
  464. * klp_enable_patch() - enables a registered patch
  465. * @patch: The registered, disabled patch to be enabled
  466. *
  467. * Performs the needed symbol lookups and code relocations,
  468. * then registers the patched functions with ftrace.
  469. *
  470. * Return: 0 on success, otherwise error
  471. */
  472. int klp_enable_patch(struct klp_patch *patch)
  473. {
  474. int ret;
  475. mutex_lock(&klp_mutex);
  476. if (!klp_is_patch_registered(patch)) {
  477. ret = -EINVAL;
  478. goto err;
  479. }
  480. ret = __klp_enable_patch(patch);
  481. err:
  482. mutex_unlock(&klp_mutex);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL_GPL(klp_enable_patch);
  486. /*
  487. * Sysfs Interface
  488. *
  489. * /sys/kernel/livepatch
  490. * /sys/kernel/livepatch/<patch>
  491. * /sys/kernel/livepatch/<patch>/enabled
  492. * /sys/kernel/livepatch/<patch>/<object>
  493. * /sys/kernel/livepatch/<patch>/<object>/<func>
  494. */
  495. static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
  496. const char *buf, size_t count)
  497. {
  498. struct klp_patch *patch;
  499. int ret;
  500. unsigned long val;
  501. ret = kstrtoul(buf, 10, &val);
  502. if (ret)
  503. return -EINVAL;
  504. if (val != KLP_DISABLED && val != KLP_ENABLED)
  505. return -EINVAL;
  506. patch = container_of(kobj, struct klp_patch, kobj);
  507. mutex_lock(&klp_mutex);
  508. if (val == patch->state) {
  509. /* already in requested state */
  510. ret = -EINVAL;
  511. goto err;
  512. }
  513. if (val == KLP_ENABLED) {
  514. ret = __klp_enable_patch(patch);
  515. if (ret)
  516. goto err;
  517. } else {
  518. ret = __klp_disable_patch(patch);
  519. if (ret)
  520. goto err;
  521. }
  522. mutex_unlock(&klp_mutex);
  523. return count;
  524. err:
  525. mutex_unlock(&klp_mutex);
  526. return ret;
  527. }
  528. static ssize_t enabled_show(struct kobject *kobj,
  529. struct kobj_attribute *attr, char *buf)
  530. {
  531. struct klp_patch *patch;
  532. patch = container_of(kobj, struct klp_patch, kobj);
  533. return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
  534. }
  535. static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
  536. static struct attribute *klp_patch_attrs[] = {
  537. &enabled_kobj_attr.attr,
  538. NULL
  539. };
  540. static void klp_kobj_release_patch(struct kobject *kobj)
  541. {
  542. /*
  543. * Once we have a consistency model we'll need to module_put() the
  544. * patch module here. See klp_register_patch() for more details.
  545. */
  546. }
  547. static struct kobj_type klp_ktype_patch = {
  548. .release = klp_kobj_release_patch,
  549. .sysfs_ops = &kobj_sysfs_ops,
  550. .default_attrs = klp_patch_attrs,
  551. };
  552. static void klp_kobj_release_func(struct kobject *kobj)
  553. {
  554. }
  555. static struct kobj_type klp_ktype_func = {
  556. .release = klp_kobj_release_func,
  557. .sysfs_ops = &kobj_sysfs_ops,
  558. };
  559. /*
  560. * Free all functions' kobjects in the array up to some limit. When limit is
  561. * NULL, all kobjects are freed.
  562. */
  563. static void klp_free_funcs_limited(struct klp_object *obj,
  564. struct klp_func *limit)
  565. {
  566. struct klp_func *func;
  567. for (func = obj->funcs; func->old_name && func != limit; func++)
  568. kobject_put(&func->kobj);
  569. }
  570. /* Clean up when a patched object is unloaded */
  571. static void klp_free_object_loaded(struct klp_object *obj)
  572. {
  573. struct klp_func *func;
  574. obj->mod = NULL;
  575. for (func = obj->funcs; func->old_name; func++)
  576. func->old_addr = 0;
  577. }
  578. /*
  579. * Free all objects' kobjects in the array up to some limit. When limit is
  580. * NULL, all kobjects are freed.
  581. */
  582. static void klp_free_objects_limited(struct klp_patch *patch,
  583. struct klp_object *limit)
  584. {
  585. struct klp_object *obj;
  586. for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
  587. klp_free_funcs_limited(obj, NULL);
  588. kobject_put(obj->kobj);
  589. }
  590. }
  591. static void klp_free_patch(struct klp_patch *patch)
  592. {
  593. klp_free_objects_limited(patch, NULL);
  594. if (!list_empty(&patch->list))
  595. list_del(&patch->list);
  596. kobject_put(&patch->kobj);
  597. }
  598. static int klp_init_func(struct klp_object *obj, struct klp_func *func)
  599. {
  600. INIT_LIST_HEAD(&func->stack_node);
  601. func->state = KLP_DISABLED;
  602. return kobject_init_and_add(&func->kobj, &klp_ktype_func,
  603. obj->kobj, "%s", func->old_name);
  604. }
  605. /* parts of the initialization that is done only when the object is loaded */
  606. static int klp_init_object_loaded(struct klp_patch *patch,
  607. struct klp_object *obj)
  608. {
  609. struct klp_func *func;
  610. int ret;
  611. if (obj->relocs) {
  612. ret = klp_write_object_relocations(patch->mod, obj);
  613. if (ret)
  614. return ret;
  615. }
  616. for (func = obj->funcs; func->old_name; func++) {
  617. ret = klp_find_verify_func_addr(obj, func);
  618. if (ret)
  619. return ret;
  620. }
  621. return 0;
  622. }
  623. static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
  624. {
  625. struct klp_func *func;
  626. int ret;
  627. const char *name;
  628. if (!obj->funcs)
  629. return -EINVAL;
  630. obj->state = KLP_DISABLED;
  631. obj->mod = NULL;
  632. klp_find_object_module(obj);
  633. name = klp_is_module(obj) ? obj->name : "vmlinux";
  634. obj->kobj = kobject_create_and_add(name, &patch->kobj);
  635. if (!obj->kobj)
  636. return -ENOMEM;
  637. for (func = obj->funcs; func->old_name; 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. for (obj = patch->objs; obj->funcs; 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 void 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. goto err;
  746. if (patch->state == KLP_DISABLED)
  747. return;
  748. pr_notice("applying patch '%s' to loading module '%s'\n",
  749. pmod->name, mod->name);
  750. ret = klp_enable_object(obj);
  751. if (!ret)
  752. return;
  753. err:
  754. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  755. pmod->name, mod->name, ret);
  756. }
  757. static void klp_module_notify_going(struct klp_patch *patch,
  758. struct klp_object *obj)
  759. {
  760. struct module *pmod = patch->mod;
  761. struct module *mod = obj->mod;
  762. int ret;
  763. if (patch->state == KLP_DISABLED)
  764. goto disabled;
  765. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  766. pmod->name, mod->name);
  767. ret = klp_disable_object(obj);
  768. if (ret)
  769. pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
  770. pmod->name, mod->name, ret);
  771. disabled:
  772. klp_free_object_loaded(obj);
  773. }
  774. static int klp_module_notify(struct notifier_block *nb, unsigned long action,
  775. void *data)
  776. {
  777. struct module *mod = data;
  778. struct klp_patch *patch;
  779. struct klp_object *obj;
  780. if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
  781. return 0;
  782. mutex_lock(&klp_mutex);
  783. /*
  784. * Each module has to know that the notifier has been called.
  785. * We never know what module will get patched by a new patch.
  786. */
  787. if (action == MODULE_STATE_COMING)
  788. mod->klp_alive = true;
  789. else /* MODULE_STATE_GOING */
  790. mod->klp_alive = false;
  791. list_for_each_entry(patch, &klp_patches, list) {
  792. for (obj = patch->objs; obj->funcs; obj++) {
  793. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  794. continue;
  795. if (action == MODULE_STATE_COMING) {
  796. obj->mod = mod;
  797. klp_module_notify_coming(patch, obj);
  798. } else /* MODULE_STATE_GOING */
  799. klp_module_notify_going(patch, obj);
  800. break;
  801. }
  802. }
  803. mutex_unlock(&klp_mutex);
  804. return 0;
  805. }
  806. static struct notifier_block klp_module_nb = {
  807. .notifier_call = klp_module_notify,
  808. .priority = INT_MIN+1, /* called late but before ftrace notifier */
  809. };
  810. static int klp_init(void)
  811. {
  812. int ret;
  813. ret = klp_check_compiler_support();
  814. if (ret) {
  815. pr_info("Your compiler is too old; turning off.\n");
  816. return -EINVAL;
  817. }
  818. ret = register_module_notifier(&klp_module_nb);
  819. if (ret)
  820. return ret;
  821. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  822. if (!klp_root_kobj) {
  823. ret = -ENOMEM;
  824. goto unregister;
  825. }
  826. return 0;
  827. unregister:
  828. unregister_module_notifier(&klp_module_nb);
  829. return ret;
  830. }
  831. module_init(klp_init);