core.c 23 KB

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