core.c 24 KB

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