core.c 30 KB

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