core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * core.c - Kernel Live Patching Core
  4. *
  5. * Copyright (C) 2014 Seth Jennings <[email protected]>
  6. * Copyright (C) 2014 SUSE
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/list.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/livepatch.h>
  16. #include <linux/elf.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/completion.h>
  19. #include <linux/memory.h>
  20. #include <linux/rcupdate.h>
  21. #include <asm/cacheflush.h>
  22. #include "core.h"
  23. #include "patch.h"
  24. #include "state.h"
  25. #include "transition.h"
  26. /*
  27. * klp_mutex is a coarse lock which serializes access to klp data. All
  28. * accesses to klp-related variables and structures must have mutex protection,
  29. * except within the following functions which carefully avoid the need for it:
  30. *
  31. * - klp_ftrace_handler()
  32. * - klp_update_patch_state()
  33. */
  34. DEFINE_MUTEX(klp_mutex);
  35. /*
  36. * Actively used patches: enabled or in transition. Note that replaced
  37. * or disabled patches are not listed even though the related kernel
  38. * module still can be loaded.
  39. */
  40. LIST_HEAD(klp_patches);
  41. static struct kobject *klp_root_kobj;
  42. static bool klp_is_module(struct klp_object *obj)
  43. {
  44. return obj->name;
  45. }
  46. /* sets obj->mod if object is not vmlinux and module is found */
  47. static void klp_find_object_module(struct klp_object *obj)
  48. {
  49. struct module *mod;
  50. if (!klp_is_module(obj))
  51. return;
  52. rcu_read_lock_sched();
  53. /*
  54. * We do not want to block removal of patched modules and therefore
  55. * we do not take a reference here. The patches are removed by
  56. * klp_module_going() instead.
  57. */
  58. mod = find_module(obj->name);
  59. /*
  60. * Do not mess work of klp_module_coming() and klp_module_going().
  61. * Note that the patch might still be needed before klp_module_going()
  62. * is called. Module functions can be called even in the GOING state
  63. * until mod->exit() finishes. This is especially important for
  64. * patches that modify semantic of the functions.
  65. */
  66. if (mod && mod->klp_alive)
  67. obj->mod = mod;
  68. rcu_read_unlock_sched();
  69. }
  70. static bool klp_initialized(void)
  71. {
  72. return !!klp_root_kobj;
  73. }
  74. static struct klp_func *klp_find_func(struct klp_object *obj,
  75. struct klp_func *old_func)
  76. {
  77. struct klp_func *func;
  78. klp_for_each_func(obj, func) {
  79. if ((strcmp(old_func->old_name, func->old_name) == 0) &&
  80. (old_func->old_sympos == func->old_sympos)) {
  81. return func;
  82. }
  83. }
  84. return NULL;
  85. }
  86. static struct klp_object *klp_find_object(struct klp_patch *patch,
  87. struct klp_object *old_obj)
  88. {
  89. struct klp_object *obj;
  90. klp_for_each_object(patch, obj) {
  91. if (klp_is_module(old_obj)) {
  92. if (klp_is_module(obj) &&
  93. strcmp(old_obj->name, obj->name) == 0) {
  94. return obj;
  95. }
  96. } else if (!klp_is_module(obj)) {
  97. return obj;
  98. }
  99. }
  100. return NULL;
  101. }
  102. struct klp_find_arg {
  103. const char *objname;
  104. const char *name;
  105. unsigned long addr;
  106. unsigned long count;
  107. unsigned long pos;
  108. };
  109. static int klp_find_callback(void *data, const char *name,
  110. struct module *mod, unsigned long addr)
  111. {
  112. struct klp_find_arg *args = data;
  113. if ((mod && !args->objname) || (!mod && args->objname))
  114. return 0;
  115. if (strcmp(args->name, name))
  116. return 0;
  117. if (args->objname && strcmp(args->objname, mod->name))
  118. return 0;
  119. args->addr = addr;
  120. args->count++;
  121. /*
  122. * Finish the search when the symbol is found for the desired position
  123. * or the position is not defined for a non-unique symbol.
  124. */
  125. if ((args->pos && (args->count == args->pos)) ||
  126. (!args->pos && (args->count > 1)))
  127. return 1;
  128. return 0;
  129. }
  130. static int klp_find_object_symbol(const char *objname, const char *name,
  131. unsigned long sympos, unsigned long *addr)
  132. {
  133. struct klp_find_arg args = {
  134. .objname = objname,
  135. .name = name,
  136. .addr = 0,
  137. .count = 0,
  138. .pos = sympos,
  139. };
  140. if (objname)
  141. module_kallsyms_on_each_symbol(klp_find_callback, &args);
  142. else
  143. kallsyms_on_each_symbol(klp_find_callback, &args);
  144. /*
  145. * Ensure an address was found. If sympos is 0, ensure symbol is unique;
  146. * otherwise ensure the symbol position count matches sympos.
  147. */
  148. if (args.addr == 0)
  149. pr_err("symbol '%s' not found in symbol table\n", name);
  150. else if (args.count > 1 && sympos == 0) {
  151. pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
  152. name, objname);
  153. } else if (sympos != args.count && sympos > 0) {
  154. pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
  155. sympos, name, objname ? objname : "vmlinux");
  156. } else {
  157. *addr = args.addr;
  158. return 0;
  159. }
  160. *addr = 0;
  161. return -EINVAL;
  162. }
  163. static int klp_resolve_symbols(Elf_Shdr *sechdrs, const char *strtab,
  164. unsigned int symndx, Elf_Shdr *relasec,
  165. const char *sec_objname)
  166. {
  167. int i, cnt, ret;
  168. char sym_objname[MODULE_NAME_LEN];
  169. char sym_name[KSYM_NAME_LEN];
  170. Elf_Rela *relas;
  171. Elf_Sym *sym;
  172. unsigned long sympos, addr;
  173. bool sym_vmlinux;
  174. bool sec_vmlinux = !strcmp(sec_objname, "vmlinux");
  175. /*
  176. * Since the field widths for sym_objname and sym_name in the sscanf()
  177. * call are hard-coded and correspond to MODULE_NAME_LEN and
  178. * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
  179. * and KSYM_NAME_LEN have the values we expect them to have.
  180. *
  181. * Because the value of MODULE_NAME_LEN can differ among architectures,
  182. * we use the smallest/strictest upper bound possible (56, based on
  183. * the current definition of MODULE_NAME_LEN) to prevent overflows.
  184. */
  185. BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 512);
  186. relas = (Elf_Rela *) relasec->sh_addr;
  187. /* For each rela in this klp relocation section */
  188. for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
  189. sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info);
  190. if (sym->st_shndx != SHN_LIVEPATCH) {
  191. pr_err("symbol %s is not marked as a livepatch symbol\n",
  192. strtab + sym->st_name);
  193. return -EINVAL;
  194. }
  195. /* Format: .klp.sym.sym_objname.sym_name,sympos */
  196. cnt = sscanf(strtab + sym->st_name,
  197. ".klp.sym.%55[^.].%511[^,],%lu",
  198. sym_objname, sym_name, &sympos);
  199. if (cnt != 3) {
  200. pr_err("symbol %s has an incorrectly formatted name\n",
  201. strtab + sym->st_name);
  202. return -EINVAL;
  203. }
  204. sym_vmlinux = !strcmp(sym_objname, "vmlinux");
  205. /*
  206. * Prevent module-specific KLP rela sections from referencing
  207. * vmlinux symbols. This helps prevent ordering issues with
  208. * module special section initializations. Presumably such
  209. * symbols are exported and normal relas can be used instead.
  210. */
  211. if (!sec_vmlinux && sym_vmlinux) {
  212. pr_err("invalid access to vmlinux symbol '%s' from module-specific livepatch relocation section\n",
  213. sym_name);
  214. return -EINVAL;
  215. }
  216. /* klp_find_object_symbol() treats a NULL objname as vmlinux */
  217. ret = klp_find_object_symbol(sym_vmlinux ? NULL : sym_objname,
  218. sym_name, sympos, &addr);
  219. if (ret)
  220. return ret;
  221. sym->st_value = addr;
  222. }
  223. return 0;
  224. }
  225. /*
  226. * At a high-level, there are two types of klp relocation sections: those which
  227. * reference symbols which live in vmlinux; and those which reference symbols
  228. * which live in other modules. This function is called for both types:
  229. *
  230. * 1) When a klp module itself loads, the module code calls this function to
  231. * write vmlinux-specific klp relocations (.klp.rela.vmlinux.* sections).
  232. * These relocations are written to the klp module text to allow the patched
  233. * code/data to reference unexported vmlinux symbols. They're written as
  234. * early as possible to ensure that other module init code (.e.g.,
  235. * jump_label_apply_nops) can access any unexported vmlinux symbols which
  236. * might be referenced by the klp module's special sections.
  237. *
  238. * 2) When a to-be-patched module loads -- or is already loaded when a
  239. * corresponding klp module loads -- klp code calls this function to write
  240. * module-specific klp relocations (.klp.rela.{module}.* sections). These
  241. * are written to the klp module text to allow the patched code/data to
  242. * reference symbols which live in the to-be-patched module or one of its
  243. * module dependencies. Exported symbols are supported, in addition to
  244. * unexported symbols, in order to enable late module patching, which allows
  245. * the to-be-patched module to be loaded and patched sometime *after* the
  246. * klp module is loaded.
  247. */
  248. int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,
  249. const char *shstrtab, const char *strtab,
  250. unsigned int symndx, unsigned int secndx,
  251. const char *objname)
  252. {
  253. int cnt, ret;
  254. char sec_objname[MODULE_NAME_LEN];
  255. Elf_Shdr *sec = sechdrs + secndx;
  256. /*
  257. * Format: .klp.rela.sec_objname.section_name
  258. * See comment in klp_resolve_symbols() for an explanation
  259. * of the selected field width value.
  260. */
  261. cnt = sscanf(shstrtab + sec->sh_name, ".klp.rela.%55[^.]",
  262. sec_objname);
  263. if (cnt != 1) {
  264. pr_err("section %s has an incorrectly formatted name\n",
  265. shstrtab + sec->sh_name);
  266. return -EINVAL;
  267. }
  268. if (strcmp(objname ? objname : "vmlinux", sec_objname))
  269. return 0;
  270. ret = klp_resolve_symbols(sechdrs, strtab, symndx, sec, sec_objname);
  271. if (ret)
  272. return ret;
  273. return apply_relocate_add(sechdrs, strtab, symndx, secndx, pmod);
  274. }
  275. /*
  276. * Sysfs Interface
  277. *
  278. * /sys/kernel/livepatch
  279. * /sys/kernel/livepatch/<patch>
  280. * /sys/kernel/livepatch/<patch>/enabled
  281. * /sys/kernel/livepatch/<patch>/transition
  282. * /sys/kernel/livepatch/<patch>/force
  283. * /sys/kernel/livepatch/<patch>/<object>
  284. * /sys/kernel/livepatch/<patch>/<object>/patched
  285. * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
  286. */
  287. static int __klp_disable_patch(struct klp_patch *patch);
  288. static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
  289. const char *buf, size_t count)
  290. {
  291. struct klp_patch *patch;
  292. int ret;
  293. bool enabled;
  294. ret = kstrtobool(buf, &enabled);
  295. if (ret)
  296. return ret;
  297. patch = container_of(kobj, struct klp_patch, kobj);
  298. mutex_lock(&klp_mutex);
  299. if (patch->enabled == enabled) {
  300. /* already in requested state */
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. /*
  305. * Allow to reverse a pending transition in both ways. It might be
  306. * necessary to complete the transition without forcing and breaking
  307. * the system integrity.
  308. *
  309. * Do not allow to re-enable a disabled patch.
  310. */
  311. if (patch == klp_transition_patch)
  312. klp_reverse_transition();
  313. else if (!enabled)
  314. ret = __klp_disable_patch(patch);
  315. else
  316. ret = -EINVAL;
  317. out:
  318. mutex_unlock(&klp_mutex);
  319. if (ret)
  320. return ret;
  321. return count;
  322. }
  323. static ssize_t enabled_show(struct kobject *kobj,
  324. struct kobj_attribute *attr, char *buf)
  325. {
  326. struct klp_patch *patch;
  327. patch = container_of(kobj, struct klp_patch, kobj);
  328. return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
  329. }
  330. static ssize_t transition_show(struct kobject *kobj,
  331. struct kobj_attribute *attr, char *buf)
  332. {
  333. struct klp_patch *patch;
  334. patch = container_of(kobj, struct klp_patch, kobj);
  335. return snprintf(buf, PAGE_SIZE-1, "%d\n",
  336. patch == klp_transition_patch);
  337. }
  338. static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
  339. const char *buf, size_t count)
  340. {
  341. struct klp_patch *patch;
  342. int ret;
  343. bool val;
  344. ret = kstrtobool(buf, &val);
  345. if (ret)
  346. return ret;
  347. if (!val)
  348. return count;
  349. mutex_lock(&klp_mutex);
  350. patch = container_of(kobj, struct klp_patch, kobj);
  351. if (patch != klp_transition_patch) {
  352. mutex_unlock(&klp_mutex);
  353. return -EINVAL;
  354. }
  355. klp_force_transition();
  356. mutex_unlock(&klp_mutex);
  357. return count;
  358. }
  359. static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
  360. static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
  361. static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
  362. static struct attribute *klp_patch_attrs[] = {
  363. &enabled_kobj_attr.attr,
  364. &transition_kobj_attr.attr,
  365. &force_kobj_attr.attr,
  366. NULL
  367. };
  368. ATTRIBUTE_GROUPS(klp_patch);
  369. static ssize_t patched_show(struct kobject *kobj,
  370. struct kobj_attribute *attr, char *buf)
  371. {
  372. struct klp_object *obj;
  373. obj = container_of(kobj, struct klp_object, kobj);
  374. return sysfs_emit(buf, "%d\n", obj->patched);
  375. }
  376. static struct kobj_attribute patched_kobj_attr = __ATTR_RO(patched);
  377. static struct attribute *klp_object_attrs[] = {
  378. &patched_kobj_attr.attr,
  379. NULL,
  380. };
  381. ATTRIBUTE_GROUPS(klp_object);
  382. static void klp_free_object_dynamic(struct klp_object *obj)
  383. {
  384. kfree(obj->name);
  385. kfree(obj);
  386. }
  387. static void klp_init_func_early(struct klp_object *obj,
  388. struct klp_func *func);
  389. static void klp_init_object_early(struct klp_patch *patch,
  390. struct klp_object *obj);
  391. static struct klp_object *klp_alloc_object_dynamic(const char *name,
  392. struct klp_patch *patch)
  393. {
  394. struct klp_object *obj;
  395. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  396. if (!obj)
  397. return NULL;
  398. if (name) {
  399. obj->name = kstrdup(name, GFP_KERNEL);
  400. if (!obj->name) {
  401. kfree(obj);
  402. return NULL;
  403. }
  404. }
  405. klp_init_object_early(patch, obj);
  406. obj->dynamic = true;
  407. return obj;
  408. }
  409. static void klp_free_func_nop(struct klp_func *func)
  410. {
  411. kfree(func->old_name);
  412. kfree(func);
  413. }
  414. static struct klp_func *klp_alloc_func_nop(struct klp_func *old_func,
  415. struct klp_object *obj)
  416. {
  417. struct klp_func *func;
  418. func = kzalloc(sizeof(*func), GFP_KERNEL);
  419. if (!func)
  420. return NULL;
  421. if (old_func->old_name) {
  422. func->old_name = kstrdup(old_func->old_name, GFP_KERNEL);
  423. if (!func->old_name) {
  424. kfree(func);
  425. return NULL;
  426. }
  427. }
  428. klp_init_func_early(obj, func);
  429. /*
  430. * func->new_func is same as func->old_func. These addresses are
  431. * set when the object is loaded, see klp_init_object_loaded().
  432. */
  433. func->old_sympos = old_func->old_sympos;
  434. func->nop = true;
  435. return func;
  436. }
  437. static int klp_add_object_nops(struct klp_patch *patch,
  438. struct klp_object *old_obj)
  439. {
  440. struct klp_object *obj;
  441. struct klp_func *func, *old_func;
  442. obj = klp_find_object(patch, old_obj);
  443. if (!obj) {
  444. obj = klp_alloc_object_dynamic(old_obj->name, patch);
  445. if (!obj)
  446. return -ENOMEM;
  447. }
  448. klp_for_each_func(old_obj, old_func) {
  449. func = klp_find_func(obj, old_func);
  450. if (func)
  451. continue;
  452. func = klp_alloc_func_nop(old_func, obj);
  453. if (!func)
  454. return -ENOMEM;
  455. }
  456. return 0;
  457. }
  458. /*
  459. * Add 'nop' functions which simply return to the caller to run
  460. * the original function. The 'nop' functions are added to a
  461. * patch to facilitate a 'replace' mode.
  462. */
  463. static int klp_add_nops(struct klp_patch *patch)
  464. {
  465. struct klp_patch *old_patch;
  466. struct klp_object *old_obj;
  467. klp_for_each_patch(old_patch) {
  468. klp_for_each_object(old_patch, old_obj) {
  469. int err;
  470. err = klp_add_object_nops(patch, old_obj);
  471. if (err)
  472. return err;
  473. }
  474. }
  475. return 0;
  476. }
  477. static void klp_kobj_release_patch(struct kobject *kobj)
  478. {
  479. struct klp_patch *patch;
  480. patch = container_of(kobj, struct klp_patch, kobj);
  481. complete(&patch->finish);
  482. }
  483. static struct kobj_type klp_ktype_patch = {
  484. .release = klp_kobj_release_patch,
  485. .sysfs_ops = &kobj_sysfs_ops,
  486. .default_groups = klp_patch_groups,
  487. };
  488. static void klp_kobj_release_object(struct kobject *kobj)
  489. {
  490. struct klp_object *obj;
  491. obj = container_of(kobj, struct klp_object, kobj);
  492. if (obj->dynamic)
  493. klp_free_object_dynamic(obj);
  494. }
  495. static struct kobj_type klp_ktype_object = {
  496. .release = klp_kobj_release_object,
  497. .sysfs_ops = &kobj_sysfs_ops,
  498. .default_groups = klp_object_groups,
  499. };
  500. static void klp_kobj_release_func(struct kobject *kobj)
  501. {
  502. struct klp_func *func;
  503. func = container_of(kobj, struct klp_func, kobj);
  504. if (func->nop)
  505. klp_free_func_nop(func);
  506. }
  507. static struct kobj_type klp_ktype_func = {
  508. .release = klp_kobj_release_func,
  509. .sysfs_ops = &kobj_sysfs_ops,
  510. };
  511. static void __klp_free_funcs(struct klp_object *obj, bool nops_only)
  512. {
  513. struct klp_func *func, *tmp_func;
  514. klp_for_each_func_safe(obj, func, tmp_func) {
  515. if (nops_only && !func->nop)
  516. continue;
  517. list_del(&func->node);
  518. kobject_put(&func->kobj);
  519. }
  520. }
  521. /* Clean up when a patched object is unloaded */
  522. static void klp_free_object_loaded(struct klp_object *obj)
  523. {
  524. struct klp_func *func;
  525. obj->mod = NULL;
  526. klp_for_each_func(obj, func) {
  527. func->old_func = NULL;
  528. if (func->nop)
  529. func->new_func = NULL;
  530. }
  531. }
  532. static void __klp_free_objects(struct klp_patch *patch, bool nops_only)
  533. {
  534. struct klp_object *obj, *tmp_obj;
  535. klp_for_each_object_safe(patch, obj, tmp_obj) {
  536. __klp_free_funcs(obj, nops_only);
  537. if (nops_only && !obj->dynamic)
  538. continue;
  539. list_del(&obj->node);
  540. kobject_put(&obj->kobj);
  541. }
  542. }
  543. static void klp_free_objects(struct klp_patch *patch)
  544. {
  545. __klp_free_objects(patch, false);
  546. }
  547. static void klp_free_objects_dynamic(struct klp_patch *patch)
  548. {
  549. __klp_free_objects(patch, true);
  550. }
  551. /*
  552. * This function implements the free operations that can be called safely
  553. * under klp_mutex.
  554. *
  555. * The operation must be completed by calling klp_free_patch_finish()
  556. * outside klp_mutex.
  557. */
  558. static void klp_free_patch_start(struct klp_patch *patch)
  559. {
  560. if (!list_empty(&patch->list))
  561. list_del(&patch->list);
  562. klp_free_objects(patch);
  563. }
  564. /*
  565. * This function implements the free part that must be called outside
  566. * klp_mutex.
  567. *
  568. * It must be called after klp_free_patch_start(). And it has to be
  569. * the last function accessing the livepatch structures when the patch
  570. * gets disabled.
  571. */
  572. static void klp_free_patch_finish(struct klp_patch *patch)
  573. {
  574. /*
  575. * Avoid deadlock with enabled_store() sysfs callback by
  576. * calling this outside klp_mutex. It is safe because
  577. * this is called when the patch gets disabled and it
  578. * cannot get enabled again.
  579. */
  580. kobject_put(&patch->kobj);
  581. wait_for_completion(&patch->finish);
  582. /* Put the module after the last access to struct klp_patch. */
  583. if (!patch->forced)
  584. module_put(patch->mod);
  585. }
  586. /*
  587. * The livepatch might be freed from sysfs interface created by the patch.
  588. * This work allows to wait until the interface is destroyed in a separate
  589. * context.
  590. */
  591. static void klp_free_patch_work_fn(struct work_struct *work)
  592. {
  593. struct klp_patch *patch =
  594. container_of(work, struct klp_patch, free_work);
  595. klp_free_patch_finish(patch);
  596. }
  597. void klp_free_patch_async(struct klp_patch *patch)
  598. {
  599. klp_free_patch_start(patch);
  600. schedule_work(&patch->free_work);
  601. }
  602. void klp_free_replaced_patches_async(struct klp_patch *new_patch)
  603. {
  604. struct klp_patch *old_patch, *tmp_patch;
  605. klp_for_each_patch_safe(old_patch, tmp_patch) {
  606. if (old_patch == new_patch)
  607. return;
  608. klp_free_patch_async(old_patch);
  609. }
  610. }
  611. static int klp_init_func(struct klp_object *obj, struct klp_func *func)
  612. {
  613. if (!func->old_name)
  614. return -EINVAL;
  615. /*
  616. * NOPs get the address later. The patched module must be loaded,
  617. * see klp_init_object_loaded().
  618. */
  619. if (!func->new_func && !func->nop)
  620. return -EINVAL;
  621. if (strlen(func->old_name) >= KSYM_NAME_LEN)
  622. return -EINVAL;
  623. INIT_LIST_HEAD(&func->stack_node);
  624. func->patched = false;
  625. func->transition = false;
  626. /* The format for the sysfs directory is <function,sympos> where sympos
  627. * is the nth occurrence of this symbol in kallsyms for the patched
  628. * object. If the user selects 0 for old_sympos, then 1 will be used
  629. * since a unique symbol will be the first occurrence.
  630. */
  631. return kobject_add(&func->kobj, &obj->kobj, "%s,%lu",
  632. func->old_name,
  633. func->old_sympos ? func->old_sympos : 1);
  634. }
  635. static int klp_apply_object_relocs(struct klp_patch *patch,
  636. struct klp_object *obj)
  637. {
  638. int i, ret;
  639. struct klp_modinfo *info = patch->mod->klp_info;
  640. for (i = 1; i < info->hdr.e_shnum; i++) {
  641. Elf_Shdr *sec = info->sechdrs + i;
  642. if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
  643. continue;
  644. ret = klp_apply_section_relocs(patch->mod, info->sechdrs,
  645. info->secstrings,
  646. patch->mod->core_kallsyms.strtab,
  647. info->symndx, i, obj->name);
  648. if (ret)
  649. return ret;
  650. }
  651. return 0;
  652. }
  653. /* parts of the initialization that is done only when the object is loaded */
  654. static int klp_init_object_loaded(struct klp_patch *patch,
  655. struct klp_object *obj)
  656. {
  657. struct klp_func *func;
  658. int ret;
  659. if (klp_is_module(obj)) {
  660. /*
  661. * Only write module-specific relocations here
  662. * (.klp.rela.{module}.*). vmlinux-specific relocations were
  663. * written earlier during the initialization of the klp module
  664. * itself.
  665. */
  666. ret = klp_apply_object_relocs(patch, obj);
  667. if (ret)
  668. return ret;
  669. }
  670. klp_for_each_func(obj, func) {
  671. ret = klp_find_object_symbol(obj->name, func->old_name,
  672. func->old_sympos,
  673. (unsigned long *)&func->old_func);
  674. if (ret)
  675. return ret;
  676. ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
  677. &func->old_size, NULL);
  678. if (!ret) {
  679. pr_err("kallsyms size lookup failed for '%s'\n",
  680. func->old_name);
  681. return -ENOENT;
  682. }
  683. if (func->nop)
  684. func->new_func = func->old_func;
  685. ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
  686. &func->new_size, NULL);
  687. if (!ret) {
  688. pr_err("kallsyms size lookup failed for '%s' replacement\n",
  689. func->old_name);
  690. return -ENOENT;
  691. }
  692. }
  693. return 0;
  694. }
  695. static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
  696. {
  697. struct klp_func *func;
  698. int ret;
  699. const char *name;
  700. if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
  701. return -EINVAL;
  702. obj->patched = false;
  703. obj->mod = NULL;
  704. klp_find_object_module(obj);
  705. name = klp_is_module(obj) ? obj->name : "vmlinux";
  706. ret = kobject_add(&obj->kobj, &patch->kobj, "%s", name);
  707. if (ret)
  708. return ret;
  709. klp_for_each_func(obj, func) {
  710. ret = klp_init_func(obj, func);
  711. if (ret)
  712. return ret;
  713. }
  714. if (klp_is_object_loaded(obj))
  715. ret = klp_init_object_loaded(patch, obj);
  716. return ret;
  717. }
  718. static void klp_init_func_early(struct klp_object *obj,
  719. struct klp_func *func)
  720. {
  721. kobject_init(&func->kobj, &klp_ktype_func);
  722. list_add_tail(&func->node, &obj->func_list);
  723. }
  724. static void klp_init_object_early(struct klp_patch *patch,
  725. struct klp_object *obj)
  726. {
  727. INIT_LIST_HEAD(&obj->func_list);
  728. kobject_init(&obj->kobj, &klp_ktype_object);
  729. list_add_tail(&obj->node, &patch->obj_list);
  730. }
  731. static void klp_init_patch_early(struct klp_patch *patch)
  732. {
  733. struct klp_object *obj;
  734. struct klp_func *func;
  735. INIT_LIST_HEAD(&patch->list);
  736. INIT_LIST_HEAD(&patch->obj_list);
  737. kobject_init(&patch->kobj, &klp_ktype_patch);
  738. patch->enabled = false;
  739. patch->forced = false;
  740. INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
  741. init_completion(&patch->finish);
  742. klp_for_each_object_static(patch, obj) {
  743. klp_init_object_early(patch, obj);
  744. klp_for_each_func_static(obj, func) {
  745. klp_init_func_early(obj, func);
  746. }
  747. }
  748. }
  749. static int klp_init_patch(struct klp_patch *patch)
  750. {
  751. struct klp_object *obj;
  752. int ret;
  753. ret = kobject_add(&patch->kobj, klp_root_kobj, "%s", patch->mod->name);
  754. if (ret)
  755. return ret;
  756. if (patch->replace) {
  757. ret = klp_add_nops(patch);
  758. if (ret)
  759. return ret;
  760. }
  761. klp_for_each_object(patch, obj) {
  762. ret = klp_init_object(patch, obj);
  763. if (ret)
  764. return ret;
  765. }
  766. list_add_tail(&patch->list, &klp_patches);
  767. return 0;
  768. }
  769. static int __klp_disable_patch(struct klp_patch *patch)
  770. {
  771. struct klp_object *obj;
  772. if (WARN_ON(!patch->enabled))
  773. return -EINVAL;
  774. if (klp_transition_patch)
  775. return -EBUSY;
  776. klp_init_transition(patch, KLP_UNPATCHED);
  777. klp_for_each_object(patch, obj)
  778. if (obj->patched)
  779. klp_pre_unpatch_callback(obj);
  780. /*
  781. * Enforce the order of the func->transition writes in
  782. * klp_init_transition() and the TIF_PATCH_PENDING writes in
  783. * klp_start_transition(). In the rare case where klp_ftrace_handler()
  784. * is called shortly after klp_update_patch_state() switches the task,
  785. * this ensures the handler sees that func->transition is set.
  786. */
  787. smp_wmb();
  788. klp_start_transition();
  789. patch->enabled = false;
  790. klp_try_complete_transition();
  791. return 0;
  792. }
  793. static int __klp_enable_patch(struct klp_patch *patch)
  794. {
  795. struct klp_object *obj;
  796. int ret;
  797. if (klp_transition_patch)
  798. return -EBUSY;
  799. if (WARN_ON(patch->enabled))
  800. return -EINVAL;
  801. pr_notice("enabling patch '%s'\n", patch->mod->name);
  802. klp_init_transition(patch, KLP_PATCHED);
  803. /*
  804. * Enforce the order of the func->transition writes in
  805. * klp_init_transition() and the ops->func_stack writes in
  806. * klp_patch_object(), so that klp_ftrace_handler() will see the
  807. * func->transition updates before the handler is registered and the
  808. * new funcs become visible to the handler.
  809. */
  810. smp_wmb();
  811. klp_for_each_object(patch, obj) {
  812. if (!klp_is_object_loaded(obj))
  813. continue;
  814. ret = klp_pre_patch_callback(obj);
  815. if (ret) {
  816. pr_warn("pre-patch callback failed for object '%s'\n",
  817. klp_is_module(obj) ? obj->name : "vmlinux");
  818. goto err;
  819. }
  820. ret = klp_patch_object(obj);
  821. if (ret) {
  822. pr_warn("failed to patch object '%s'\n",
  823. klp_is_module(obj) ? obj->name : "vmlinux");
  824. goto err;
  825. }
  826. }
  827. klp_start_transition();
  828. patch->enabled = true;
  829. klp_try_complete_transition();
  830. return 0;
  831. err:
  832. pr_warn("failed to enable patch '%s'\n", patch->mod->name);
  833. klp_cancel_transition();
  834. return ret;
  835. }
  836. /**
  837. * klp_enable_patch() - enable the livepatch
  838. * @patch: patch to be enabled
  839. *
  840. * Initializes the data structure associated with the patch, creates the sysfs
  841. * interface, performs the needed symbol lookups and code relocations,
  842. * registers the patched functions with ftrace.
  843. *
  844. * This function is supposed to be called from the livepatch module_init()
  845. * callback.
  846. *
  847. * Return: 0 on success, otherwise error
  848. */
  849. int klp_enable_patch(struct klp_patch *patch)
  850. {
  851. int ret;
  852. struct klp_object *obj;
  853. if (!patch || !patch->mod || !patch->objs)
  854. return -EINVAL;
  855. klp_for_each_object_static(patch, obj) {
  856. if (!obj->funcs)
  857. return -EINVAL;
  858. }
  859. if (!is_livepatch_module(patch->mod)) {
  860. pr_err("module %s is not marked as a livepatch module\n",
  861. patch->mod->name);
  862. return -EINVAL;
  863. }
  864. if (!klp_initialized())
  865. return -ENODEV;
  866. if (!klp_have_reliable_stack()) {
  867. pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
  868. pr_warn("The livepatch transition may never complete.\n");
  869. }
  870. mutex_lock(&klp_mutex);
  871. if (!klp_is_patch_compatible(patch)) {
  872. pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
  873. patch->mod->name);
  874. mutex_unlock(&klp_mutex);
  875. return -EINVAL;
  876. }
  877. if (!try_module_get(patch->mod)) {
  878. mutex_unlock(&klp_mutex);
  879. return -ENODEV;
  880. }
  881. klp_init_patch_early(patch);
  882. ret = klp_init_patch(patch);
  883. if (ret)
  884. goto err;
  885. ret = __klp_enable_patch(patch);
  886. if (ret)
  887. goto err;
  888. mutex_unlock(&klp_mutex);
  889. return 0;
  890. err:
  891. klp_free_patch_start(patch);
  892. mutex_unlock(&klp_mutex);
  893. klp_free_patch_finish(patch);
  894. return ret;
  895. }
  896. EXPORT_SYMBOL_GPL(klp_enable_patch);
  897. /*
  898. * This function unpatches objects from the replaced livepatches.
  899. *
  900. * We could be pretty aggressive here. It is called in the situation where
  901. * these structures are no longer accessed from the ftrace handler.
  902. * All functions are redirected by the klp_transition_patch. They
  903. * use either a new code or they are in the original code because
  904. * of the special nop function patches.
  905. *
  906. * The only exception is when the transition was forced. In this case,
  907. * klp_ftrace_handler() might still see the replaced patch on the stack.
  908. * Fortunately, it is carefully designed to work with removed functions
  909. * thanks to RCU. We only have to keep the patches on the system. Also
  910. * this is handled transparently by patch->module_put.
  911. */
  912. void klp_unpatch_replaced_patches(struct klp_patch *new_patch)
  913. {
  914. struct klp_patch *old_patch;
  915. klp_for_each_patch(old_patch) {
  916. if (old_patch == new_patch)
  917. return;
  918. old_patch->enabled = false;
  919. klp_unpatch_objects(old_patch);
  920. }
  921. }
  922. /*
  923. * This function removes the dynamically allocated 'nop' functions.
  924. *
  925. * We could be pretty aggressive. NOPs do not change the existing
  926. * behavior except for adding unnecessary delay by the ftrace handler.
  927. *
  928. * It is safe even when the transition was forced. The ftrace handler
  929. * will see a valid ops->func_stack entry thanks to RCU.
  930. *
  931. * We could even free the NOPs structures. They must be the last entry
  932. * in ops->func_stack. Therefore unregister_ftrace_function() is called.
  933. * It does the same as klp_synchronize_transition() to make sure that
  934. * nobody is inside the ftrace handler once the operation finishes.
  935. *
  936. * IMPORTANT: It must be called right after removing the replaced patches!
  937. */
  938. void klp_discard_nops(struct klp_patch *new_patch)
  939. {
  940. klp_unpatch_objects_dynamic(klp_transition_patch);
  941. klp_free_objects_dynamic(klp_transition_patch);
  942. }
  943. /*
  944. * Remove parts of patches that touch a given kernel module. The list of
  945. * patches processed might be limited. When limit is NULL, all patches
  946. * will be handled.
  947. */
  948. static void klp_cleanup_module_patches_limited(struct module *mod,
  949. struct klp_patch *limit)
  950. {
  951. struct klp_patch *patch;
  952. struct klp_object *obj;
  953. klp_for_each_patch(patch) {
  954. if (patch == limit)
  955. break;
  956. klp_for_each_object(patch, obj) {
  957. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  958. continue;
  959. if (patch != klp_transition_patch)
  960. klp_pre_unpatch_callback(obj);
  961. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  962. patch->mod->name, obj->mod->name);
  963. klp_unpatch_object(obj);
  964. klp_post_unpatch_callback(obj);
  965. klp_free_object_loaded(obj);
  966. break;
  967. }
  968. }
  969. }
  970. int klp_module_coming(struct module *mod)
  971. {
  972. int ret;
  973. struct klp_patch *patch;
  974. struct klp_object *obj;
  975. if (WARN_ON(mod->state != MODULE_STATE_COMING))
  976. return -EINVAL;
  977. if (!strcmp(mod->name, "vmlinux")) {
  978. pr_err("vmlinux.ko: invalid module name\n");
  979. return -EINVAL;
  980. }
  981. mutex_lock(&klp_mutex);
  982. /*
  983. * Each module has to know that klp_module_coming()
  984. * has been called. We never know what module will
  985. * get patched by a new patch.
  986. */
  987. mod->klp_alive = true;
  988. klp_for_each_patch(patch) {
  989. klp_for_each_object(patch, obj) {
  990. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  991. continue;
  992. obj->mod = mod;
  993. ret = klp_init_object_loaded(patch, obj);
  994. if (ret) {
  995. pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
  996. patch->mod->name, obj->mod->name, ret);
  997. goto err;
  998. }
  999. pr_notice("applying patch '%s' to loading module '%s'\n",
  1000. patch->mod->name, obj->mod->name);
  1001. ret = klp_pre_patch_callback(obj);
  1002. if (ret) {
  1003. pr_warn("pre-patch callback failed for object '%s'\n",
  1004. obj->name);
  1005. goto err;
  1006. }
  1007. ret = klp_patch_object(obj);
  1008. if (ret) {
  1009. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  1010. patch->mod->name, obj->mod->name, ret);
  1011. klp_post_unpatch_callback(obj);
  1012. goto err;
  1013. }
  1014. if (patch != klp_transition_patch)
  1015. klp_post_patch_callback(obj);
  1016. break;
  1017. }
  1018. }
  1019. mutex_unlock(&klp_mutex);
  1020. return 0;
  1021. err:
  1022. /*
  1023. * If a patch is unsuccessfully applied, return
  1024. * error to the module loader.
  1025. */
  1026. pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
  1027. patch->mod->name, obj->mod->name, obj->mod->name);
  1028. mod->klp_alive = false;
  1029. obj->mod = NULL;
  1030. klp_cleanup_module_patches_limited(mod, patch);
  1031. mutex_unlock(&klp_mutex);
  1032. return ret;
  1033. }
  1034. void klp_module_going(struct module *mod)
  1035. {
  1036. if (WARN_ON(mod->state != MODULE_STATE_GOING &&
  1037. mod->state != MODULE_STATE_COMING))
  1038. return;
  1039. mutex_lock(&klp_mutex);
  1040. /*
  1041. * Each module has to know that klp_module_going()
  1042. * has been called. We never know what module will
  1043. * get patched by a new patch.
  1044. */
  1045. mod->klp_alive = false;
  1046. klp_cleanup_module_patches_limited(mod, NULL);
  1047. mutex_unlock(&klp_mutex);
  1048. }
  1049. static int __init klp_init(void)
  1050. {
  1051. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  1052. if (!klp_root_kobj)
  1053. return -ENOMEM;
  1054. return 0;
  1055. }
  1056. module_init(klp_init);