core.c 19 KB

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