core.c 21 KB

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