core.c 20 KB

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