core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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/list.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/livepatch.h>
  28. #include <linux/elf.h>
  29. #include <linux/moduleloader.h>
  30. #include <linux/completion.h>
  31. #include <asm/cacheflush.h>
  32. #include "core.h"
  33. #include "patch.h"
  34. #include "transition.h"
  35. /*
  36. * klp_mutex is a coarse lock which serializes access to klp data. All
  37. * accesses to klp-related variables and structures must have mutex protection,
  38. * except within the following functions which carefully avoid the need for it:
  39. *
  40. * - klp_ftrace_handler()
  41. * - klp_update_patch_state()
  42. */
  43. DEFINE_MUTEX(klp_mutex);
  44. static LIST_HEAD(klp_patches);
  45. static struct kobject *klp_root_kobj;
  46. static bool klp_is_module(struct klp_object *obj)
  47. {
  48. return obj->name;
  49. }
  50. static bool klp_is_object_loaded(struct klp_object *obj)
  51. {
  52. return !obj->name || obj->mod;
  53. }
  54. /* sets obj->mod if object is not vmlinux and module is found */
  55. static void klp_find_object_module(struct klp_object *obj)
  56. {
  57. struct module *mod;
  58. if (!klp_is_module(obj))
  59. return;
  60. mutex_lock(&module_mutex);
  61. /*
  62. * We do not want to block removal of patched modules and therefore
  63. * we do not take a reference here. The patches are removed by
  64. * klp_module_going() instead.
  65. */
  66. mod = find_module(obj->name);
  67. /*
  68. * Do not mess work of klp_module_coming() and klp_module_going().
  69. * Note that the patch might still be needed before klp_module_going()
  70. * is called. Module functions can be called even in the GOING state
  71. * until mod->exit() finishes. This is especially important for
  72. * patches that modify semantic of the functions.
  73. */
  74. if (mod && mod->klp_alive)
  75. obj->mod = mod;
  76. mutex_unlock(&module_mutex);
  77. }
  78. static bool klp_is_patch_registered(struct klp_patch *patch)
  79. {
  80. struct klp_patch *mypatch;
  81. list_for_each_entry(mypatch, &klp_patches, list)
  82. if (mypatch == patch)
  83. return true;
  84. return false;
  85. }
  86. static bool klp_initialized(void)
  87. {
  88. return !!klp_root_kobj;
  89. }
  90. struct klp_find_arg {
  91. const char *objname;
  92. const char *name;
  93. unsigned long addr;
  94. unsigned long count;
  95. unsigned long pos;
  96. };
  97. static int klp_find_callback(void *data, const char *name,
  98. struct module *mod, unsigned long addr)
  99. {
  100. struct klp_find_arg *args = data;
  101. if ((mod && !args->objname) || (!mod && args->objname))
  102. return 0;
  103. if (strcmp(args->name, name))
  104. return 0;
  105. if (args->objname && strcmp(args->objname, mod->name))
  106. return 0;
  107. args->addr = addr;
  108. args->count++;
  109. /*
  110. * Finish the search when the symbol is found for the desired position
  111. * or the position is not defined for a non-unique symbol.
  112. */
  113. if ((args->pos && (args->count == args->pos)) ||
  114. (!args->pos && (args->count > 1)))
  115. return 1;
  116. return 0;
  117. }
  118. static int klp_find_object_symbol(const char *objname, const char *name,
  119. unsigned long sympos, unsigned long *addr)
  120. {
  121. struct klp_find_arg args = {
  122. .objname = objname,
  123. .name = name,
  124. .addr = 0,
  125. .count = 0,
  126. .pos = sympos,
  127. };
  128. mutex_lock(&module_mutex);
  129. if (objname)
  130. module_kallsyms_on_each_symbol(klp_find_callback, &args);
  131. else
  132. kallsyms_on_each_symbol(klp_find_callback, &args);
  133. mutex_unlock(&module_mutex);
  134. /*
  135. * Ensure an address was found. If sympos is 0, ensure symbol is unique;
  136. * otherwise ensure the symbol position count matches sympos.
  137. */
  138. if (args.addr == 0)
  139. pr_err("symbol '%s' not found in symbol table\n", name);
  140. else if (args.count > 1 && sympos == 0) {
  141. pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
  142. name, objname);
  143. } else if (sympos != args.count && sympos > 0) {
  144. pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
  145. sympos, name, objname ? objname : "vmlinux");
  146. } else {
  147. *addr = args.addr;
  148. return 0;
  149. }
  150. *addr = 0;
  151. return -EINVAL;
  152. }
  153. static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
  154. {
  155. int i, cnt, vmlinux, ret;
  156. char objname[MODULE_NAME_LEN];
  157. char symname[KSYM_NAME_LEN];
  158. char *strtab = pmod->core_kallsyms.strtab;
  159. Elf_Rela *relas;
  160. Elf_Sym *sym;
  161. unsigned long sympos, addr;
  162. /*
  163. * Since the field widths for objname and symname in the sscanf()
  164. * call are hard-coded and correspond to MODULE_NAME_LEN and
  165. * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
  166. * and KSYM_NAME_LEN have the values we expect them to have.
  167. *
  168. * Because the value of MODULE_NAME_LEN can differ among architectures,
  169. * we use the smallest/strictest upper bound possible (56, based on
  170. * the current definition of MODULE_NAME_LEN) to prevent overflows.
  171. */
  172. BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
  173. relas = (Elf_Rela *) relasec->sh_addr;
  174. /* For each rela in this klp relocation section */
  175. for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
  176. sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
  177. if (sym->st_shndx != SHN_LIVEPATCH) {
  178. pr_err("symbol %s is not marked as a livepatch symbol\n",
  179. strtab + sym->st_name);
  180. return -EINVAL;
  181. }
  182. /* Format: .klp.sym.objname.symname,sympos */
  183. cnt = sscanf(strtab + sym->st_name,
  184. ".klp.sym.%55[^.].%127[^,],%lu",
  185. objname, symname, &sympos);
  186. if (cnt != 3) {
  187. pr_err("symbol %s has an incorrectly formatted name\n",
  188. strtab + sym->st_name);
  189. return -EINVAL;
  190. }
  191. /* klp_find_object_symbol() treats a NULL objname as vmlinux */
  192. vmlinux = !strcmp(objname, "vmlinux");
  193. ret = klp_find_object_symbol(vmlinux ? NULL : objname,
  194. symname, sympos, &addr);
  195. if (ret)
  196. return ret;
  197. sym->st_value = addr;
  198. }
  199. return 0;
  200. }
  201. static int klp_write_object_relocations(struct module *pmod,
  202. struct klp_object *obj)
  203. {
  204. int i, cnt, ret = 0;
  205. const char *objname, *secname;
  206. char sec_objname[MODULE_NAME_LEN];
  207. Elf_Shdr *sec;
  208. if (WARN_ON(!klp_is_object_loaded(obj)))
  209. return -EINVAL;
  210. objname = klp_is_module(obj) ? obj->name : "vmlinux";
  211. /* For each klp relocation section */
  212. for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
  213. sec = pmod->klp_info->sechdrs + i;
  214. secname = pmod->klp_info->secstrings + sec->sh_name;
  215. if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
  216. continue;
  217. /*
  218. * Format: .klp.rela.sec_objname.section_name
  219. * See comment in klp_resolve_symbols() for an explanation
  220. * of the selected field width value.
  221. */
  222. cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
  223. if (cnt != 1) {
  224. pr_err("section %s has an incorrectly formatted name\n",
  225. secname);
  226. ret = -EINVAL;
  227. break;
  228. }
  229. if (strcmp(objname, sec_objname))
  230. continue;
  231. ret = klp_resolve_symbols(sec, pmod);
  232. if (ret)
  233. break;
  234. ret = apply_relocate_add(pmod->klp_info->sechdrs,
  235. pmod->core_kallsyms.strtab,
  236. pmod->klp_info->symndx, i, pmod);
  237. if (ret)
  238. break;
  239. }
  240. return ret;
  241. }
  242. static int __klp_disable_patch(struct klp_patch *patch)
  243. {
  244. if (klp_transition_patch)
  245. return -EBUSY;
  246. /* enforce stacking: only the last enabled patch can be disabled */
  247. if (!list_is_last(&patch->list, &klp_patches) &&
  248. list_next_entry(patch, list)->enabled)
  249. return -EBUSY;
  250. klp_init_transition(patch, KLP_UNPATCHED);
  251. /*
  252. * Enforce the order of the func->transition writes in
  253. * klp_init_transition() and the TIF_PATCH_PENDING writes in
  254. * klp_start_transition(). In the rare case where klp_ftrace_handler()
  255. * is called shortly after klp_update_patch_state() switches the task,
  256. * this ensures the handler sees that func->transition is set.
  257. */
  258. smp_wmb();
  259. klp_start_transition();
  260. klp_try_complete_transition();
  261. patch->enabled = false;
  262. return 0;
  263. }
  264. /**
  265. * klp_disable_patch() - disables a registered patch
  266. * @patch: The registered, enabled patch to be disabled
  267. *
  268. * Unregisters the patched functions from ftrace.
  269. *
  270. * Return: 0 on success, otherwise error
  271. */
  272. int klp_disable_patch(struct klp_patch *patch)
  273. {
  274. int ret;
  275. mutex_lock(&klp_mutex);
  276. if (!klp_is_patch_registered(patch)) {
  277. ret = -EINVAL;
  278. goto err;
  279. }
  280. if (!patch->enabled) {
  281. ret = -EINVAL;
  282. goto err;
  283. }
  284. ret = __klp_disable_patch(patch);
  285. err:
  286. mutex_unlock(&klp_mutex);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(klp_disable_patch);
  290. static int __klp_enable_patch(struct klp_patch *patch)
  291. {
  292. struct klp_object *obj;
  293. int ret;
  294. if (klp_transition_patch)
  295. return -EBUSY;
  296. if (WARN_ON(patch->enabled))
  297. return -EINVAL;
  298. /* enforce stacking: only the first disabled patch can be enabled */
  299. if (patch->list.prev != &klp_patches &&
  300. !list_prev_entry(patch, list)->enabled)
  301. return -EBUSY;
  302. /*
  303. * A reference is taken on the patch module to prevent it from being
  304. * unloaded.
  305. *
  306. * Note: For immediate (no consistency model) patches we don't allow
  307. * patch modules to unload since there is no safe/sane method to
  308. * determine if a thread is still running in the patched code contained
  309. * in the patch module once the ftrace registration is successful.
  310. */
  311. if (!try_module_get(patch->mod))
  312. return -ENODEV;
  313. pr_notice("enabling patch '%s'\n", patch->mod->name);
  314. klp_init_transition(patch, KLP_PATCHED);
  315. /*
  316. * Enforce the order of the func->transition writes in
  317. * klp_init_transition() and the ops->func_stack writes in
  318. * klp_patch_object(), so that klp_ftrace_handler() will see the
  319. * func->transition updates before the handler is registered and the
  320. * new funcs become visible to the handler.
  321. */
  322. smp_wmb();
  323. klp_for_each_object(patch, obj) {
  324. if (!klp_is_object_loaded(obj))
  325. continue;
  326. ret = klp_patch_object(obj);
  327. if (ret) {
  328. pr_warn("failed to enable patch '%s'\n",
  329. patch->mod->name);
  330. klp_cancel_transition();
  331. return ret;
  332. }
  333. }
  334. klp_start_transition();
  335. klp_try_complete_transition();
  336. patch->enabled = true;
  337. return 0;
  338. }
  339. /**
  340. * klp_enable_patch() - enables a registered patch
  341. * @patch: The registered, disabled patch to be enabled
  342. *
  343. * Performs the needed symbol lookups and code relocations,
  344. * then registers the patched functions with ftrace.
  345. *
  346. * Return: 0 on success, otherwise error
  347. */
  348. int klp_enable_patch(struct klp_patch *patch)
  349. {
  350. int ret;
  351. mutex_lock(&klp_mutex);
  352. if (!klp_is_patch_registered(patch)) {
  353. ret = -EINVAL;
  354. goto err;
  355. }
  356. ret = __klp_enable_patch(patch);
  357. err:
  358. mutex_unlock(&klp_mutex);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL_GPL(klp_enable_patch);
  362. /*
  363. * Sysfs Interface
  364. *
  365. * /sys/kernel/livepatch
  366. * /sys/kernel/livepatch/<patch>
  367. * /sys/kernel/livepatch/<patch>/enabled
  368. * /sys/kernel/livepatch/<patch>/transition
  369. * /sys/kernel/livepatch/<patch>/<object>
  370. * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
  371. */
  372. static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. struct klp_patch *patch;
  376. int ret;
  377. bool enabled;
  378. ret = kstrtobool(buf, &enabled);
  379. if (ret)
  380. return ret;
  381. patch = container_of(kobj, struct klp_patch, kobj);
  382. mutex_lock(&klp_mutex);
  383. if (!klp_is_patch_registered(patch)) {
  384. /*
  385. * Module with the patch could either disappear meanwhile or is
  386. * not properly initialized yet.
  387. */
  388. ret = -EINVAL;
  389. goto err;
  390. }
  391. if (patch->enabled == enabled) {
  392. /* already in requested state */
  393. ret = -EINVAL;
  394. goto err;
  395. }
  396. if (patch == klp_transition_patch) {
  397. klp_reverse_transition();
  398. } else if (enabled) {
  399. ret = __klp_enable_patch(patch);
  400. if (ret)
  401. goto err;
  402. } else {
  403. ret = __klp_disable_patch(patch);
  404. if (ret)
  405. goto err;
  406. }
  407. mutex_unlock(&klp_mutex);
  408. return count;
  409. err:
  410. mutex_unlock(&klp_mutex);
  411. return ret;
  412. }
  413. static ssize_t enabled_show(struct kobject *kobj,
  414. struct kobj_attribute *attr, char *buf)
  415. {
  416. struct klp_patch *patch;
  417. patch = container_of(kobj, struct klp_patch, kobj);
  418. return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
  419. }
  420. static ssize_t transition_show(struct kobject *kobj,
  421. struct kobj_attribute *attr, char *buf)
  422. {
  423. struct klp_patch *patch;
  424. patch = container_of(kobj, struct klp_patch, kobj);
  425. return snprintf(buf, PAGE_SIZE-1, "%d\n",
  426. patch == klp_transition_patch);
  427. }
  428. static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
  429. static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
  430. static struct attribute *klp_patch_attrs[] = {
  431. &enabled_kobj_attr.attr,
  432. &transition_kobj_attr.attr,
  433. NULL
  434. };
  435. static void klp_kobj_release_patch(struct kobject *kobj)
  436. {
  437. struct klp_patch *patch;
  438. patch = container_of(kobj, struct klp_patch, kobj);
  439. complete(&patch->finish);
  440. }
  441. static struct kobj_type klp_ktype_patch = {
  442. .release = klp_kobj_release_patch,
  443. .sysfs_ops = &kobj_sysfs_ops,
  444. .default_attrs = klp_patch_attrs,
  445. };
  446. static void klp_kobj_release_object(struct kobject *kobj)
  447. {
  448. }
  449. static struct kobj_type klp_ktype_object = {
  450. .release = klp_kobj_release_object,
  451. .sysfs_ops = &kobj_sysfs_ops,
  452. };
  453. static void klp_kobj_release_func(struct kobject *kobj)
  454. {
  455. }
  456. static struct kobj_type klp_ktype_func = {
  457. .release = klp_kobj_release_func,
  458. .sysfs_ops = &kobj_sysfs_ops,
  459. };
  460. /*
  461. * Free all functions' kobjects in the array up to some limit. When limit is
  462. * NULL, all kobjects are freed.
  463. */
  464. static void klp_free_funcs_limited(struct klp_object *obj,
  465. struct klp_func *limit)
  466. {
  467. struct klp_func *func;
  468. for (func = obj->funcs; func->old_name && func != limit; func++)
  469. kobject_put(&func->kobj);
  470. }
  471. /* Clean up when a patched object is unloaded */
  472. static void klp_free_object_loaded(struct klp_object *obj)
  473. {
  474. struct klp_func *func;
  475. obj->mod = NULL;
  476. klp_for_each_func(obj, func)
  477. func->old_addr = 0;
  478. }
  479. /*
  480. * Free all objects' kobjects in the array up to some limit. When limit is
  481. * NULL, all kobjects are freed.
  482. */
  483. static void klp_free_objects_limited(struct klp_patch *patch,
  484. struct klp_object *limit)
  485. {
  486. struct klp_object *obj;
  487. for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
  488. klp_free_funcs_limited(obj, NULL);
  489. kobject_put(&obj->kobj);
  490. }
  491. }
  492. static void klp_free_patch(struct klp_patch *patch)
  493. {
  494. klp_free_objects_limited(patch, NULL);
  495. if (!list_empty(&patch->list))
  496. list_del(&patch->list);
  497. }
  498. static int klp_init_func(struct klp_object *obj, struct klp_func *func)
  499. {
  500. if (!func->old_name || !func->new_func)
  501. return -EINVAL;
  502. INIT_LIST_HEAD(&func->stack_node);
  503. func->patched = false;
  504. func->transition = false;
  505. /* The format for the sysfs directory is <function,sympos> where sympos
  506. * is the nth occurrence of this symbol in kallsyms for the patched
  507. * object. If the user selects 0 for old_sympos, then 1 will be used
  508. * since a unique symbol will be the first occurrence.
  509. */
  510. return kobject_init_and_add(&func->kobj, &klp_ktype_func,
  511. &obj->kobj, "%s,%lu", func->old_name,
  512. func->old_sympos ? func->old_sympos : 1);
  513. }
  514. /* Arches may override this to finish any remaining arch-specific tasks */
  515. void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
  516. struct klp_object *obj)
  517. {
  518. }
  519. /* parts of the initialization that is done only when the object is loaded */
  520. static int klp_init_object_loaded(struct klp_patch *patch,
  521. struct klp_object *obj)
  522. {
  523. struct klp_func *func;
  524. int ret;
  525. module_disable_ro(patch->mod);
  526. ret = klp_write_object_relocations(patch->mod, obj);
  527. if (ret) {
  528. module_enable_ro(patch->mod, true);
  529. return ret;
  530. }
  531. arch_klp_init_object_loaded(patch, obj);
  532. module_enable_ro(patch->mod, true);
  533. klp_for_each_func(obj, func) {
  534. ret = klp_find_object_symbol(obj->name, func->old_name,
  535. func->old_sympos,
  536. &func->old_addr);
  537. if (ret)
  538. return ret;
  539. ret = kallsyms_lookup_size_offset(func->old_addr,
  540. &func->old_size, NULL);
  541. if (!ret) {
  542. pr_err("kallsyms size lookup failed for '%s'\n",
  543. func->old_name);
  544. return -ENOENT;
  545. }
  546. ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
  547. &func->new_size, NULL);
  548. if (!ret) {
  549. pr_err("kallsyms size lookup failed for '%s' replacement\n",
  550. func->old_name);
  551. return -ENOENT;
  552. }
  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->patched = false;
  564. obj->mod = NULL;
  565. klp_find_object_module(obj);
  566. name = klp_is_module(obj) ? obj->name : "vmlinux";
  567. ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
  568. &patch->kobj, "%s", name);
  569. if (ret)
  570. return ret;
  571. klp_for_each_func(obj, func) {
  572. ret = klp_init_func(obj, func);
  573. if (ret)
  574. goto free;
  575. }
  576. if (klp_is_object_loaded(obj)) {
  577. ret = klp_init_object_loaded(patch, obj);
  578. if (ret)
  579. goto free;
  580. }
  581. return 0;
  582. free:
  583. klp_free_funcs_limited(obj, func);
  584. kobject_put(&obj->kobj);
  585. return ret;
  586. }
  587. static int klp_init_patch(struct klp_patch *patch)
  588. {
  589. struct klp_object *obj;
  590. int ret;
  591. if (!patch->objs)
  592. return -EINVAL;
  593. mutex_lock(&klp_mutex);
  594. patch->enabled = false;
  595. init_completion(&patch->finish);
  596. ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
  597. klp_root_kobj, "%s", patch->mod->name);
  598. if (ret) {
  599. mutex_unlock(&klp_mutex);
  600. return ret;
  601. }
  602. klp_for_each_object(patch, obj) {
  603. ret = klp_init_object(patch, obj);
  604. if (ret)
  605. goto free;
  606. }
  607. list_add_tail(&patch->list, &klp_patches);
  608. mutex_unlock(&klp_mutex);
  609. return 0;
  610. free:
  611. klp_free_objects_limited(patch, obj);
  612. mutex_unlock(&klp_mutex);
  613. kobject_put(&patch->kobj);
  614. wait_for_completion(&patch->finish);
  615. return ret;
  616. }
  617. /**
  618. * klp_unregister_patch() - unregisters a patch
  619. * @patch: Disabled patch to be unregistered
  620. *
  621. * Frees the data structures and removes the sysfs interface.
  622. *
  623. * Return: 0 on success, otherwise error
  624. */
  625. int klp_unregister_patch(struct klp_patch *patch)
  626. {
  627. int ret;
  628. mutex_lock(&klp_mutex);
  629. if (!klp_is_patch_registered(patch)) {
  630. ret = -EINVAL;
  631. goto err;
  632. }
  633. if (patch->enabled) {
  634. ret = -EBUSY;
  635. goto err;
  636. }
  637. klp_free_patch(patch);
  638. mutex_unlock(&klp_mutex);
  639. kobject_put(&patch->kobj);
  640. wait_for_completion(&patch->finish);
  641. return 0;
  642. err:
  643. mutex_unlock(&klp_mutex);
  644. return ret;
  645. }
  646. EXPORT_SYMBOL_GPL(klp_unregister_patch);
  647. /**
  648. * klp_register_patch() - registers a patch
  649. * @patch: Patch to be registered
  650. *
  651. * Initializes the data structure associated with the patch and
  652. * creates the sysfs interface.
  653. *
  654. * There is no need to take the reference on the patch module here. It is done
  655. * later when the patch is enabled.
  656. *
  657. * Return: 0 on success, otherwise error
  658. */
  659. int klp_register_patch(struct klp_patch *patch)
  660. {
  661. if (!patch || !patch->mod)
  662. return -EINVAL;
  663. if (!is_livepatch_module(patch->mod)) {
  664. pr_err("module %s is not marked as a livepatch module\n",
  665. patch->mod->name);
  666. return -EINVAL;
  667. }
  668. if (!klp_initialized())
  669. return -ENODEV;
  670. /*
  671. * Architectures without reliable stack traces have to set
  672. * patch->immediate because there's currently no way to patch kthreads
  673. * with the consistency model.
  674. */
  675. if (!klp_have_reliable_stack() && !patch->immediate) {
  676. pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
  677. return -ENOSYS;
  678. }
  679. return klp_init_patch(patch);
  680. }
  681. EXPORT_SYMBOL_GPL(klp_register_patch);
  682. int klp_module_coming(struct module *mod)
  683. {
  684. int ret;
  685. struct klp_patch *patch;
  686. struct klp_object *obj;
  687. if (WARN_ON(mod->state != MODULE_STATE_COMING))
  688. return -EINVAL;
  689. mutex_lock(&klp_mutex);
  690. /*
  691. * Each module has to know that klp_module_coming()
  692. * has been called. We never know what module will
  693. * get patched by a new patch.
  694. */
  695. mod->klp_alive = true;
  696. list_for_each_entry(patch, &klp_patches, list) {
  697. klp_for_each_object(patch, obj) {
  698. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  699. continue;
  700. obj->mod = mod;
  701. ret = klp_init_object_loaded(patch, obj);
  702. if (ret) {
  703. pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
  704. patch->mod->name, obj->mod->name, ret);
  705. goto err;
  706. }
  707. /*
  708. * Only patch the module if the patch is enabled or is
  709. * in transition.
  710. */
  711. if (!patch->enabled && patch != klp_transition_patch)
  712. break;
  713. pr_notice("applying patch '%s' to loading module '%s'\n",
  714. patch->mod->name, obj->mod->name);
  715. ret = klp_patch_object(obj);
  716. if (ret) {
  717. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  718. patch->mod->name, obj->mod->name, ret);
  719. goto err;
  720. }
  721. break;
  722. }
  723. }
  724. mutex_unlock(&klp_mutex);
  725. return 0;
  726. err:
  727. /*
  728. * If a patch is unsuccessfully applied, return
  729. * error to the module loader.
  730. */
  731. pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
  732. patch->mod->name, obj->mod->name, obj->mod->name);
  733. mod->klp_alive = false;
  734. klp_free_object_loaded(obj);
  735. mutex_unlock(&klp_mutex);
  736. return ret;
  737. }
  738. void klp_module_going(struct module *mod)
  739. {
  740. struct klp_patch *patch;
  741. struct klp_object *obj;
  742. if (WARN_ON(mod->state != MODULE_STATE_GOING &&
  743. mod->state != MODULE_STATE_COMING))
  744. return;
  745. mutex_lock(&klp_mutex);
  746. /*
  747. * Each module has to know that klp_module_going()
  748. * has been called. We never know what module will
  749. * get patched by a new patch.
  750. */
  751. mod->klp_alive = false;
  752. list_for_each_entry(patch, &klp_patches, list) {
  753. klp_for_each_object(patch, obj) {
  754. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  755. continue;
  756. /*
  757. * Only unpatch the module if the patch is enabled or
  758. * is in transition.
  759. */
  760. if (patch->enabled || patch == klp_transition_patch) {
  761. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  762. patch->mod->name, obj->mod->name);
  763. klp_unpatch_object(obj);
  764. }
  765. klp_free_object_loaded(obj);
  766. break;
  767. }
  768. }
  769. mutex_unlock(&klp_mutex);
  770. }
  771. static int __init klp_init(void)
  772. {
  773. int ret;
  774. ret = klp_check_compiler_support();
  775. if (ret) {
  776. pr_info("Your compiler is too old; turning off.\n");
  777. return -EINVAL;
  778. }
  779. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  780. if (!klp_root_kobj)
  781. return -ENOMEM;
  782. return 0;
  783. }
  784. module_init(klp_init);