core.c 32 KB

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