core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. regs->ip = (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. func->fops = ops;
  520. func->state = KLP_DISABLED;
  521. ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
  522. obj->kobj, func->old_name);
  523. if (ret) {
  524. kfree(func->fops);
  525. return ret;
  526. }
  527. return 0;
  528. }
  529. /* parts of the initialization that is done only when the object is loaded */
  530. static int klp_init_object_loaded(struct klp_patch *patch,
  531. struct klp_object *obj)
  532. {
  533. struct klp_func *func;
  534. int ret;
  535. if (obj->relocs) {
  536. ret = klp_write_object_relocations(patch->mod, obj);
  537. if (ret)
  538. return ret;
  539. }
  540. for (func = obj->funcs; func->old_name; func++) {
  541. ret = klp_find_verify_func_addr(obj, func);
  542. if (ret)
  543. return ret;
  544. }
  545. return 0;
  546. }
  547. static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
  548. {
  549. struct klp_func *func;
  550. int ret;
  551. const char *name;
  552. if (!obj->funcs)
  553. return -EINVAL;
  554. obj->state = KLP_DISABLED;
  555. klp_find_object_module(obj);
  556. name = klp_is_module(obj) ? obj->name : "vmlinux";
  557. obj->kobj = kobject_create_and_add(name, &patch->kobj);
  558. if (!obj->kobj)
  559. return -ENOMEM;
  560. for (func = obj->funcs; func->old_name; func++) {
  561. ret = klp_init_func(obj, func);
  562. if (ret)
  563. goto free;
  564. }
  565. if (klp_is_object_loaded(obj)) {
  566. ret = klp_init_object_loaded(patch, obj);
  567. if (ret)
  568. goto free;
  569. }
  570. return 0;
  571. free:
  572. klp_free_funcs_limited(obj, func);
  573. kobject_put(obj->kobj);
  574. return ret;
  575. }
  576. static int klp_init_patch(struct klp_patch *patch)
  577. {
  578. struct klp_object *obj;
  579. int ret;
  580. if (!patch->objs)
  581. return -EINVAL;
  582. mutex_lock(&klp_mutex);
  583. patch->state = KLP_DISABLED;
  584. ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
  585. klp_root_kobj, patch->mod->name);
  586. if (ret)
  587. goto unlock;
  588. for (obj = patch->objs; obj->funcs; obj++) {
  589. ret = klp_init_object(patch, obj);
  590. if (ret)
  591. goto free;
  592. }
  593. list_add(&patch->list, &klp_patches);
  594. mutex_unlock(&klp_mutex);
  595. return 0;
  596. free:
  597. klp_free_objects_limited(patch, obj);
  598. kobject_put(&patch->kobj);
  599. unlock:
  600. mutex_unlock(&klp_mutex);
  601. return ret;
  602. }
  603. /**
  604. * klp_unregister_patch() - unregisters a patch
  605. * @patch: Disabled patch to be unregistered
  606. *
  607. * Frees the data structures and removes the sysfs interface.
  608. *
  609. * Return: 0 on success, otherwise error
  610. */
  611. int klp_unregister_patch(struct klp_patch *patch)
  612. {
  613. int ret = 0;
  614. mutex_lock(&klp_mutex);
  615. if (!klp_is_patch_registered(patch)) {
  616. ret = -EINVAL;
  617. goto out;
  618. }
  619. if (patch->state == KLP_ENABLED) {
  620. ret = -EBUSY;
  621. goto out;
  622. }
  623. klp_free_patch(patch);
  624. out:
  625. mutex_unlock(&klp_mutex);
  626. return ret;
  627. }
  628. EXPORT_SYMBOL_GPL(klp_unregister_patch);
  629. /**
  630. * klp_register_patch() - registers a patch
  631. * @patch: Patch to be registered
  632. *
  633. * Initializes the data structure associated with the patch and
  634. * creates the sysfs interface.
  635. *
  636. * Return: 0 on success, otherwise error
  637. */
  638. int klp_register_patch(struct klp_patch *patch)
  639. {
  640. int ret;
  641. if (!klp_initialized())
  642. return -ENODEV;
  643. if (!patch || !patch->mod)
  644. return -EINVAL;
  645. /*
  646. * A reference is taken on the patch module to prevent it from being
  647. * unloaded. Right now, we don't allow patch modules to unload since
  648. * there is currently no method to determine if a thread is still
  649. * running in the patched code contained in the patch module once
  650. * the ftrace registration is successful.
  651. */
  652. if (!try_module_get(patch->mod))
  653. return -ENODEV;
  654. ret = klp_init_patch(patch);
  655. if (ret)
  656. module_put(patch->mod);
  657. return ret;
  658. }
  659. EXPORT_SYMBOL_GPL(klp_register_patch);
  660. static void klp_module_notify_coming(struct klp_patch *patch,
  661. struct klp_object *obj)
  662. {
  663. struct module *pmod = patch->mod;
  664. struct module *mod = obj->mod;
  665. int ret;
  666. ret = klp_init_object_loaded(patch, obj);
  667. if (ret)
  668. goto err;
  669. if (patch->state == KLP_DISABLED)
  670. return;
  671. pr_notice("applying patch '%s' to loading module '%s'\n",
  672. pmod->name, mod->name);
  673. ret = klp_enable_object(obj);
  674. if (!ret)
  675. return;
  676. err:
  677. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  678. pmod->name, mod->name, ret);
  679. }
  680. static void klp_module_notify_going(struct klp_patch *patch,
  681. struct klp_object *obj)
  682. {
  683. struct module *pmod = patch->mod;
  684. struct module *mod = obj->mod;
  685. int ret;
  686. if (patch->state == KLP_DISABLED)
  687. goto disabled;
  688. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  689. pmod->name, mod->name);
  690. ret = klp_disable_object(obj);
  691. if (ret)
  692. pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
  693. pmod->name, mod->name, ret);
  694. disabled:
  695. klp_free_object_loaded(obj);
  696. }
  697. static int klp_module_notify(struct notifier_block *nb, unsigned long action,
  698. void *data)
  699. {
  700. struct module *mod = data;
  701. struct klp_patch *patch;
  702. struct klp_object *obj;
  703. if (action != MODULE_STATE_COMING && action != MODULE_STATE_GOING)
  704. return 0;
  705. mutex_lock(&klp_mutex);
  706. list_for_each_entry(patch, &klp_patches, list) {
  707. for (obj = patch->objs; obj->funcs; obj++) {
  708. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  709. continue;
  710. if (action == MODULE_STATE_COMING) {
  711. obj->mod = mod;
  712. klp_module_notify_coming(patch, obj);
  713. } else /* MODULE_STATE_GOING */
  714. klp_module_notify_going(patch, obj);
  715. break;
  716. }
  717. }
  718. mutex_unlock(&klp_mutex);
  719. return 0;
  720. }
  721. static struct notifier_block klp_module_nb = {
  722. .notifier_call = klp_module_notify,
  723. .priority = INT_MIN+1, /* called late but before ftrace notifier */
  724. };
  725. static int klp_init(void)
  726. {
  727. int ret;
  728. ret = register_module_notifier(&klp_module_nb);
  729. if (ret)
  730. return ret;
  731. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  732. if (!klp_root_kobj) {
  733. ret = -ENOMEM;
  734. goto unregister;
  735. }
  736. return 0;
  737. unregister:
  738. unregister_module_notifier(&klp_module_nb);
  739. return ret;
  740. }
  741. module_init(klp_init);