core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. * core.c - Kernel Live Patching Core
  3. *
  4. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  5. * Copyright (C) 2014 SUSE
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <linux/ftrace.h>
  26. #include <linux/list.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/livepatch.h>
  29. #include <linux/elf.h>
  30. #include <linux/moduleloader.h>
  31. #include <asm/cacheflush.h>
  32. /**
  33. * struct klp_ops - structure for tracking registered ftrace ops structs
  34. *
  35. * A single ftrace_ops is shared between all enabled replacement functions
  36. * (klp_func structs) which have the same old_addr. This allows the switch
  37. * between function versions to happen instantaneously by updating the klp_ops
  38. * struct's func_stack list. The winner is the klp_func at the top of the
  39. * func_stack (front of the list).
  40. *
  41. * @node: node for the global klp_ops list
  42. * @func_stack: list head for the stack of klp_func's (active func is on top)
  43. * @fops: registered ftrace ops struct
  44. */
  45. struct klp_ops {
  46. struct list_head node;
  47. struct list_head func_stack;
  48. struct ftrace_ops fops;
  49. };
  50. /*
  51. * The klp_mutex protects the global lists and state transitions of any
  52. * structure reachable from them. References to any structure must be obtained
  53. * under mutex protection (except in klp_ftrace_handler(), which uses RCU to
  54. * ensure it gets consistent data).
  55. */
  56. static DEFINE_MUTEX(klp_mutex);
  57. static LIST_HEAD(klp_patches);
  58. static LIST_HEAD(klp_ops);
  59. static struct kobject *klp_root_kobj;
  60. static struct klp_ops *klp_find_ops(unsigned long old_addr)
  61. {
  62. struct klp_ops *ops;
  63. struct klp_func *func;
  64. list_for_each_entry(ops, &klp_ops, node) {
  65. func = list_first_entry(&ops->func_stack, struct klp_func,
  66. stack_node);
  67. if (func->old_addr == old_addr)
  68. return ops;
  69. }
  70. return NULL;
  71. }
  72. static bool klp_is_module(struct klp_object *obj)
  73. {
  74. return obj->name;
  75. }
  76. static bool klp_is_object_loaded(struct klp_object *obj)
  77. {
  78. return !obj->name || obj->mod;
  79. }
  80. /* sets obj->mod if object is not vmlinux and module is found */
  81. static void klp_find_object_module(struct klp_object *obj)
  82. {
  83. struct module *mod;
  84. if (!klp_is_module(obj))
  85. return;
  86. mutex_lock(&module_mutex);
  87. /*
  88. * We do not want to block removal of patched modules and therefore
  89. * we do not take a reference here. The patches are removed by
  90. * klp_module_going() instead.
  91. */
  92. mod = find_module(obj->name);
  93. /*
  94. * Do not mess work of klp_module_coming() and klp_module_going().
  95. * Note that the patch might still be needed before klp_module_going()
  96. * is called. Module functions can be called even in the GOING state
  97. * until mod->exit() finishes. This is especially important for
  98. * patches that modify semantic of the functions.
  99. */
  100. if (mod && mod->klp_alive)
  101. obj->mod = mod;
  102. mutex_unlock(&module_mutex);
  103. }
  104. /* klp_mutex must be held by caller */
  105. static bool klp_is_patch_registered(struct klp_patch *patch)
  106. {
  107. struct klp_patch *mypatch;
  108. list_for_each_entry(mypatch, &klp_patches, list)
  109. if (mypatch == patch)
  110. return true;
  111. return false;
  112. }
  113. static bool klp_initialized(void)
  114. {
  115. return !!klp_root_kobj;
  116. }
  117. struct klp_find_arg {
  118. const char *objname;
  119. const char *name;
  120. unsigned long addr;
  121. unsigned long count;
  122. unsigned long pos;
  123. };
  124. static int klp_find_callback(void *data, const char *name,
  125. struct module *mod, unsigned long addr)
  126. {
  127. struct klp_find_arg *args = data;
  128. if ((mod && !args->objname) || (!mod && args->objname))
  129. return 0;
  130. if (strcmp(args->name, name))
  131. return 0;
  132. if (args->objname && strcmp(args->objname, mod->name))
  133. return 0;
  134. args->addr = addr;
  135. args->count++;
  136. /*
  137. * Finish the search when the symbol is found for the desired position
  138. * or the position is not defined for a non-unique symbol.
  139. */
  140. if ((args->pos && (args->count == args->pos)) ||
  141. (!args->pos && (args->count > 1)))
  142. return 1;
  143. return 0;
  144. }
  145. static int klp_find_object_symbol(const char *objname, const char *name,
  146. unsigned long sympos, unsigned long *addr)
  147. {
  148. struct klp_find_arg args = {
  149. .objname = objname,
  150. .name = name,
  151. .addr = 0,
  152. .count = 0,
  153. .pos = sympos,
  154. };
  155. mutex_lock(&module_mutex);
  156. kallsyms_on_each_symbol(klp_find_callback, &args);
  157. mutex_unlock(&module_mutex);
  158. /*
  159. * Ensure an address was found. If sympos is 0, ensure symbol is unique;
  160. * otherwise ensure the symbol position count matches sympos.
  161. */
  162. if (args.addr == 0)
  163. pr_err("symbol '%s' not found in symbol table\n", name);
  164. else if (args.count > 1 && sympos == 0) {
  165. pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
  166. name, objname);
  167. } else if (sympos != args.count && sympos > 0) {
  168. pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
  169. sympos, name, objname ? objname : "vmlinux");
  170. } else {
  171. *addr = args.addr;
  172. return 0;
  173. }
  174. *addr = 0;
  175. return -EINVAL;
  176. }
  177. static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
  178. {
  179. int i, cnt, vmlinux, ret;
  180. char objname[MODULE_NAME_LEN];
  181. char symname[KSYM_NAME_LEN];
  182. char *strtab = pmod->core_kallsyms.strtab;
  183. Elf_Rela *relas;
  184. Elf_Sym *sym;
  185. unsigned long sympos, addr;
  186. /*
  187. * Since the field widths for objname and symname in the sscanf()
  188. * call are hard-coded and correspond to MODULE_NAME_LEN and
  189. * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
  190. * and KSYM_NAME_LEN have the values we expect them to have.
  191. *
  192. * Because the value of MODULE_NAME_LEN can differ among architectures,
  193. * we use the smallest/strictest upper bound possible (56, based on
  194. * the current definition of MODULE_NAME_LEN) to prevent overflows.
  195. */
  196. BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
  197. relas = (Elf_Rela *) relasec->sh_addr;
  198. /* For each rela in this klp relocation section */
  199. for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
  200. sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
  201. if (sym->st_shndx != SHN_LIVEPATCH) {
  202. pr_err("symbol %s is not marked as a livepatch symbol",
  203. strtab + sym->st_name);
  204. return -EINVAL;
  205. }
  206. /* Format: .klp.sym.objname.symname,sympos */
  207. cnt = sscanf(strtab + sym->st_name,
  208. ".klp.sym.%55[^.].%127[^,],%lu",
  209. objname, symname, &sympos);
  210. if (cnt != 3) {
  211. pr_err("symbol %s has an incorrectly formatted name",
  212. strtab + sym->st_name);
  213. return -EINVAL;
  214. }
  215. /* klp_find_object_symbol() treats a NULL objname as vmlinux */
  216. vmlinux = !strcmp(objname, "vmlinux");
  217. ret = klp_find_object_symbol(vmlinux ? NULL : objname,
  218. symname, sympos, &addr);
  219. if (ret)
  220. return ret;
  221. sym->st_value = addr;
  222. }
  223. return 0;
  224. }
  225. static int klp_write_object_relocations(struct module *pmod,
  226. struct klp_object *obj)
  227. {
  228. int i, cnt, ret = 0;
  229. const char *objname, *secname;
  230. char sec_objname[MODULE_NAME_LEN];
  231. Elf_Shdr *sec;
  232. if (WARN_ON(!klp_is_object_loaded(obj)))
  233. return -EINVAL;
  234. objname = klp_is_module(obj) ? obj->name : "vmlinux";
  235. module_disable_ro(pmod);
  236. /* For each klp relocation section */
  237. for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
  238. sec = pmod->klp_info->sechdrs + i;
  239. secname = pmod->klp_info->secstrings + sec->sh_name;
  240. if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
  241. continue;
  242. /*
  243. * Format: .klp.rela.sec_objname.section_name
  244. * See comment in klp_resolve_symbols() for an explanation
  245. * of the selected field width value.
  246. */
  247. cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
  248. if (cnt != 1) {
  249. pr_err("section %s has an incorrectly formatted name",
  250. secname);
  251. ret = -EINVAL;
  252. break;
  253. }
  254. if (strcmp(objname, sec_objname))
  255. continue;
  256. ret = klp_resolve_symbols(sec, pmod);
  257. if (ret)
  258. break;
  259. ret = apply_relocate_add(pmod->klp_info->sechdrs,
  260. pmod->core_kallsyms.strtab,
  261. pmod->klp_info->symndx, i, pmod);
  262. if (ret)
  263. break;
  264. }
  265. module_enable_ro(pmod);
  266. return ret;
  267. }
  268. static void notrace klp_ftrace_handler(unsigned long ip,
  269. unsigned long parent_ip,
  270. struct ftrace_ops *fops,
  271. struct pt_regs *regs)
  272. {
  273. struct klp_ops *ops;
  274. struct klp_func *func;
  275. ops = container_of(fops, struct klp_ops, fops);
  276. rcu_read_lock();
  277. func = list_first_or_null_rcu(&ops->func_stack, struct klp_func,
  278. stack_node);
  279. if (WARN_ON_ONCE(!func))
  280. goto unlock;
  281. klp_arch_set_pc(regs, (unsigned long)func->new_func);
  282. unlock:
  283. rcu_read_unlock();
  284. }
  285. /*
  286. * Convert a function address into the appropriate ftrace location.
  287. *
  288. * Usually this is just the address of the function, but on some architectures
  289. * it's more complicated so allow them to provide a custom behaviour.
  290. */
  291. #ifndef klp_get_ftrace_location
  292. static unsigned long klp_get_ftrace_location(unsigned long faddr)
  293. {
  294. return faddr;
  295. }
  296. #endif
  297. static void klp_disable_func(struct klp_func *func)
  298. {
  299. struct klp_ops *ops;
  300. if (WARN_ON(func->state != KLP_ENABLED))
  301. return;
  302. if (WARN_ON(!func->old_addr))
  303. return;
  304. ops = klp_find_ops(func->old_addr);
  305. if (WARN_ON(!ops))
  306. return;
  307. if (list_is_singular(&ops->func_stack)) {
  308. unsigned long ftrace_loc;
  309. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  310. if (WARN_ON(!ftrace_loc))
  311. return;
  312. WARN_ON(unregister_ftrace_function(&ops->fops));
  313. WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
  314. list_del_rcu(&func->stack_node);
  315. list_del(&ops->node);
  316. kfree(ops);
  317. } else {
  318. list_del_rcu(&func->stack_node);
  319. }
  320. func->state = KLP_DISABLED;
  321. }
  322. static int klp_enable_func(struct klp_func *func)
  323. {
  324. struct klp_ops *ops;
  325. int ret;
  326. if (WARN_ON(!func->old_addr))
  327. return -EINVAL;
  328. if (WARN_ON(func->state != KLP_DISABLED))
  329. return -EINVAL;
  330. ops = klp_find_ops(func->old_addr);
  331. if (!ops) {
  332. unsigned long ftrace_loc;
  333. ftrace_loc = klp_get_ftrace_location(func->old_addr);
  334. if (!ftrace_loc) {
  335. pr_err("failed to find location for function '%s'\n",
  336. func->old_name);
  337. return -EINVAL;
  338. }
  339. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  340. if (!ops)
  341. return -ENOMEM;
  342. ops->fops.func = klp_ftrace_handler;
  343. ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS |
  344. FTRACE_OPS_FL_DYNAMIC |
  345. FTRACE_OPS_FL_IPMODIFY;
  346. list_add(&ops->node, &klp_ops);
  347. INIT_LIST_HEAD(&ops->func_stack);
  348. list_add_rcu(&func->stack_node, &ops->func_stack);
  349. ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0);
  350. if (ret) {
  351. pr_err("failed to set ftrace filter for function '%s' (%d)\n",
  352. func->old_name, ret);
  353. goto err;
  354. }
  355. ret = register_ftrace_function(&ops->fops);
  356. if (ret) {
  357. pr_err("failed to register ftrace handler for function '%s' (%d)\n",
  358. func->old_name, ret);
  359. ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0);
  360. goto err;
  361. }
  362. } else {
  363. list_add_rcu(&func->stack_node, &ops->func_stack);
  364. }
  365. func->state = KLP_ENABLED;
  366. return 0;
  367. err:
  368. list_del_rcu(&func->stack_node);
  369. list_del(&ops->node);
  370. kfree(ops);
  371. return ret;
  372. }
  373. static void klp_disable_object(struct klp_object *obj)
  374. {
  375. struct klp_func *func;
  376. klp_for_each_func(obj, func)
  377. if (func->state == KLP_ENABLED)
  378. klp_disable_func(func);
  379. obj->state = KLP_DISABLED;
  380. }
  381. static int klp_enable_object(struct klp_object *obj)
  382. {
  383. struct klp_func *func;
  384. int ret;
  385. if (WARN_ON(obj->state != KLP_DISABLED))
  386. return -EINVAL;
  387. if (WARN_ON(!klp_is_object_loaded(obj)))
  388. return -EINVAL;
  389. klp_for_each_func(obj, func) {
  390. ret = klp_enable_func(func);
  391. if (ret) {
  392. klp_disable_object(obj);
  393. return ret;
  394. }
  395. }
  396. obj->state = KLP_ENABLED;
  397. return 0;
  398. }
  399. static int __klp_disable_patch(struct klp_patch *patch)
  400. {
  401. struct klp_object *obj;
  402. /* enforce stacking: only the last enabled patch can be disabled */
  403. if (!list_is_last(&patch->list, &klp_patches) &&
  404. list_next_entry(patch, list)->state == KLP_ENABLED)
  405. return -EBUSY;
  406. pr_notice("disabling patch '%s'\n", patch->mod->name);
  407. klp_for_each_object(patch, obj) {
  408. if (obj->state == KLP_ENABLED)
  409. klp_disable_object(obj);
  410. }
  411. patch->state = KLP_DISABLED;
  412. return 0;
  413. }
  414. /**
  415. * klp_disable_patch() - disables a registered patch
  416. * @patch: The registered, enabled patch to be disabled
  417. *
  418. * Unregisters the patched functions from ftrace.
  419. *
  420. * Return: 0 on success, otherwise error
  421. */
  422. int klp_disable_patch(struct klp_patch *patch)
  423. {
  424. int ret;
  425. mutex_lock(&klp_mutex);
  426. if (!klp_is_patch_registered(patch)) {
  427. ret = -EINVAL;
  428. goto err;
  429. }
  430. if (patch->state == KLP_DISABLED) {
  431. ret = -EINVAL;
  432. goto err;
  433. }
  434. ret = __klp_disable_patch(patch);
  435. err:
  436. mutex_unlock(&klp_mutex);
  437. return ret;
  438. }
  439. EXPORT_SYMBOL_GPL(klp_disable_patch);
  440. static int __klp_enable_patch(struct klp_patch *patch)
  441. {
  442. struct klp_object *obj;
  443. int ret;
  444. if (WARN_ON(patch->state != KLP_DISABLED))
  445. return -EINVAL;
  446. /* enforce stacking: only the first disabled patch can be enabled */
  447. if (patch->list.prev != &klp_patches &&
  448. list_prev_entry(patch, list)->state == KLP_DISABLED)
  449. return -EBUSY;
  450. pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
  451. add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
  452. pr_notice("enabling patch '%s'\n", patch->mod->name);
  453. klp_for_each_object(patch, obj) {
  454. if (!klp_is_object_loaded(obj))
  455. continue;
  456. ret = klp_enable_object(obj);
  457. if (ret)
  458. goto unregister;
  459. }
  460. patch->state = KLP_ENABLED;
  461. return 0;
  462. unregister:
  463. WARN_ON(__klp_disable_patch(patch));
  464. return ret;
  465. }
  466. /**
  467. * klp_enable_patch() - enables a registered patch
  468. * @patch: The registered, disabled patch to be enabled
  469. *
  470. * Performs the needed symbol lookups and code relocations,
  471. * then registers the patched functions with ftrace.
  472. *
  473. * Return: 0 on success, otherwise error
  474. */
  475. int klp_enable_patch(struct klp_patch *patch)
  476. {
  477. int ret;
  478. mutex_lock(&klp_mutex);
  479. if (!klp_is_patch_registered(patch)) {
  480. ret = -EINVAL;
  481. goto err;
  482. }
  483. ret = __klp_enable_patch(patch);
  484. err:
  485. mutex_unlock(&klp_mutex);
  486. return ret;
  487. }
  488. EXPORT_SYMBOL_GPL(klp_enable_patch);
  489. /*
  490. * Sysfs Interface
  491. *
  492. * /sys/kernel/livepatch
  493. * /sys/kernel/livepatch/<patch>
  494. * /sys/kernel/livepatch/<patch>/enabled
  495. * /sys/kernel/livepatch/<patch>/<object>
  496. * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
  497. */
  498. static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct klp_patch *patch;
  502. int ret;
  503. unsigned long val;
  504. ret = kstrtoul(buf, 10, &val);
  505. if (ret)
  506. return -EINVAL;
  507. if (val != KLP_DISABLED && val != KLP_ENABLED)
  508. return -EINVAL;
  509. patch = container_of(kobj, struct klp_patch, kobj);
  510. mutex_lock(&klp_mutex);
  511. if (val == patch->state) {
  512. /* already in requested state */
  513. ret = -EINVAL;
  514. goto err;
  515. }
  516. if (val == KLP_ENABLED) {
  517. ret = __klp_enable_patch(patch);
  518. if (ret)
  519. goto err;
  520. } else {
  521. ret = __klp_disable_patch(patch);
  522. if (ret)
  523. goto err;
  524. }
  525. mutex_unlock(&klp_mutex);
  526. return count;
  527. err:
  528. mutex_unlock(&klp_mutex);
  529. return ret;
  530. }
  531. static ssize_t enabled_show(struct kobject *kobj,
  532. struct kobj_attribute *attr, char *buf)
  533. {
  534. struct klp_patch *patch;
  535. patch = container_of(kobj, struct klp_patch, kobj);
  536. return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
  537. }
  538. static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
  539. static struct attribute *klp_patch_attrs[] = {
  540. &enabled_kobj_attr.attr,
  541. NULL
  542. };
  543. static void klp_kobj_release_patch(struct kobject *kobj)
  544. {
  545. /*
  546. * Once we have a consistency model we'll need to module_put() the
  547. * patch module here. See klp_register_patch() for more details.
  548. */
  549. }
  550. static struct kobj_type klp_ktype_patch = {
  551. .release = klp_kobj_release_patch,
  552. .sysfs_ops = &kobj_sysfs_ops,
  553. .default_attrs = klp_patch_attrs,
  554. };
  555. static void klp_kobj_release_object(struct kobject *kobj)
  556. {
  557. }
  558. static struct kobj_type klp_ktype_object = {
  559. .release = klp_kobj_release_object,
  560. .sysfs_ops = &kobj_sysfs_ops,
  561. };
  562. static void klp_kobj_release_func(struct kobject *kobj)
  563. {
  564. }
  565. static struct kobj_type klp_ktype_func = {
  566. .release = klp_kobj_release_func,
  567. .sysfs_ops = &kobj_sysfs_ops,
  568. };
  569. /*
  570. * Free all functions' kobjects in the array up to some limit. When limit is
  571. * NULL, all kobjects are freed.
  572. */
  573. static void klp_free_funcs_limited(struct klp_object *obj,
  574. struct klp_func *limit)
  575. {
  576. struct klp_func *func;
  577. for (func = obj->funcs; func->old_name && func != limit; func++)
  578. kobject_put(&func->kobj);
  579. }
  580. /* Clean up when a patched object is unloaded */
  581. static void klp_free_object_loaded(struct klp_object *obj)
  582. {
  583. struct klp_func *func;
  584. obj->mod = NULL;
  585. klp_for_each_func(obj, func)
  586. func->old_addr = 0;
  587. }
  588. /*
  589. * Free all objects' kobjects in the array up to some limit. When limit is
  590. * NULL, all kobjects are freed.
  591. */
  592. static void klp_free_objects_limited(struct klp_patch *patch,
  593. struct klp_object *limit)
  594. {
  595. struct klp_object *obj;
  596. for (obj = patch->objs; obj->funcs && obj != limit; obj++) {
  597. klp_free_funcs_limited(obj, NULL);
  598. kobject_put(&obj->kobj);
  599. }
  600. }
  601. static void klp_free_patch(struct klp_patch *patch)
  602. {
  603. klp_free_objects_limited(patch, NULL);
  604. if (!list_empty(&patch->list))
  605. list_del(&patch->list);
  606. kobject_put(&patch->kobj);
  607. }
  608. static int klp_init_func(struct klp_object *obj, struct klp_func *func)
  609. {
  610. if (!func->old_name || !func->new_func)
  611. return -EINVAL;
  612. INIT_LIST_HEAD(&func->stack_node);
  613. func->state = KLP_DISABLED;
  614. /* The format for the sysfs directory is <function,sympos> where sympos
  615. * is the nth occurrence of this symbol in kallsyms for the patched
  616. * object. If the user selects 0 for old_sympos, then 1 will be used
  617. * since a unique symbol will be the first occurrence.
  618. */
  619. return kobject_init_and_add(&func->kobj, &klp_ktype_func,
  620. &obj->kobj, "%s,%lu", func->old_name,
  621. func->old_sympos ? func->old_sympos : 1);
  622. }
  623. /* parts of the initialization that is done only when the object is loaded */
  624. static int klp_init_object_loaded(struct klp_patch *patch,
  625. struct klp_object *obj)
  626. {
  627. struct klp_func *func;
  628. int ret;
  629. ret = klp_write_object_relocations(patch->mod, obj);
  630. if (ret)
  631. return ret;
  632. klp_for_each_func(obj, func) {
  633. ret = klp_find_object_symbol(obj->name, func->old_name,
  634. func->old_sympos,
  635. &func->old_addr);
  636. if (ret)
  637. return ret;
  638. }
  639. return 0;
  640. }
  641. static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
  642. {
  643. struct klp_func *func;
  644. int ret;
  645. const char *name;
  646. if (!obj->funcs)
  647. return -EINVAL;
  648. obj->state = KLP_DISABLED;
  649. obj->mod = NULL;
  650. klp_find_object_module(obj);
  651. name = klp_is_module(obj) ? obj->name : "vmlinux";
  652. ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
  653. &patch->kobj, "%s", name);
  654. if (ret)
  655. return ret;
  656. klp_for_each_func(obj, func) {
  657. ret = klp_init_func(obj, func);
  658. if (ret)
  659. goto free;
  660. }
  661. if (klp_is_object_loaded(obj)) {
  662. ret = klp_init_object_loaded(patch, obj);
  663. if (ret)
  664. goto free;
  665. }
  666. return 0;
  667. free:
  668. klp_free_funcs_limited(obj, func);
  669. kobject_put(&obj->kobj);
  670. return ret;
  671. }
  672. static int klp_init_patch(struct klp_patch *patch)
  673. {
  674. struct klp_object *obj;
  675. int ret;
  676. if (!patch->objs)
  677. return -EINVAL;
  678. mutex_lock(&klp_mutex);
  679. patch->state = KLP_DISABLED;
  680. ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
  681. klp_root_kobj, "%s", patch->mod->name);
  682. if (ret)
  683. goto unlock;
  684. klp_for_each_object(patch, obj) {
  685. ret = klp_init_object(patch, obj);
  686. if (ret)
  687. goto free;
  688. }
  689. list_add_tail(&patch->list, &klp_patches);
  690. mutex_unlock(&klp_mutex);
  691. return 0;
  692. free:
  693. klp_free_objects_limited(patch, obj);
  694. kobject_put(&patch->kobj);
  695. unlock:
  696. mutex_unlock(&klp_mutex);
  697. return ret;
  698. }
  699. /**
  700. * klp_unregister_patch() - unregisters a patch
  701. * @patch: Disabled patch to be unregistered
  702. *
  703. * Frees the data structures and removes the sysfs interface.
  704. *
  705. * Return: 0 on success, otherwise error
  706. */
  707. int klp_unregister_patch(struct klp_patch *patch)
  708. {
  709. int ret = 0;
  710. mutex_lock(&klp_mutex);
  711. if (!klp_is_patch_registered(patch)) {
  712. ret = -EINVAL;
  713. goto out;
  714. }
  715. if (patch->state == KLP_ENABLED) {
  716. ret = -EBUSY;
  717. goto out;
  718. }
  719. klp_free_patch(patch);
  720. out:
  721. mutex_unlock(&klp_mutex);
  722. return ret;
  723. }
  724. EXPORT_SYMBOL_GPL(klp_unregister_patch);
  725. /**
  726. * klp_register_patch() - registers a patch
  727. * @patch: Patch to be registered
  728. *
  729. * Initializes the data structure associated with the patch and
  730. * creates the sysfs interface.
  731. *
  732. * Return: 0 on success, otherwise error
  733. */
  734. int klp_register_patch(struct klp_patch *patch)
  735. {
  736. int ret;
  737. if (!patch || !patch->mod)
  738. return -EINVAL;
  739. if (!is_livepatch_module(patch->mod)) {
  740. pr_err("module %s is not marked as a livepatch module",
  741. patch->mod->name);
  742. return -EINVAL;
  743. }
  744. if (!klp_initialized())
  745. return -ENODEV;
  746. /*
  747. * A reference is taken on the patch module to prevent it from being
  748. * unloaded. Right now, we don't allow patch modules to unload since
  749. * there is currently no method to determine if a thread is still
  750. * running in the patched code contained in the patch module once
  751. * the ftrace registration is successful.
  752. */
  753. if (!try_module_get(patch->mod))
  754. return -ENODEV;
  755. ret = klp_init_patch(patch);
  756. if (ret)
  757. module_put(patch->mod);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL_GPL(klp_register_patch);
  761. int klp_module_coming(struct module *mod)
  762. {
  763. int ret;
  764. struct klp_patch *patch;
  765. struct klp_object *obj;
  766. if (WARN_ON(mod->state != MODULE_STATE_COMING))
  767. return -EINVAL;
  768. mutex_lock(&klp_mutex);
  769. /*
  770. * Each module has to know that klp_module_coming()
  771. * has been called. We never know what module will
  772. * get patched by a new patch.
  773. */
  774. mod->klp_alive = true;
  775. list_for_each_entry(patch, &klp_patches, list) {
  776. klp_for_each_object(patch, obj) {
  777. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  778. continue;
  779. obj->mod = mod;
  780. ret = klp_init_object_loaded(patch, obj);
  781. if (ret) {
  782. pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
  783. patch->mod->name, obj->mod->name, ret);
  784. goto err;
  785. }
  786. if (patch->state == KLP_DISABLED)
  787. break;
  788. pr_notice("applying patch '%s' to loading module '%s'\n",
  789. patch->mod->name, obj->mod->name);
  790. ret = klp_enable_object(obj);
  791. if (ret) {
  792. pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
  793. patch->mod->name, obj->mod->name, ret);
  794. goto err;
  795. }
  796. break;
  797. }
  798. }
  799. mutex_unlock(&klp_mutex);
  800. return 0;
  801. err:
  802. /*
  803. * If a patch is unsuccessfully applied, return
  804. * error to the module loader.
  805. */
  806. pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
  807. patch->mod->name, obj->mod->name, obj->mod->name);
  808. mod->klp_alive = false;
  809. klp_free_object_loaded(obj);
  810. mutex_unlock(&klp_mutex);
  811. return ret;
  812. }
  813. void klp_module_going(struct module *mod)
  814. {
  815. struct klp_patch *patch;
  816. struct klp_object *obj;
  817. if (WARN_ON(mod->state != MODULE_STATE_GOING &&
  818. mod->state != MODULE_STATE_COMING))
  819. return;
  820. mutex_lock(&klp_mutex);
  821. /*
  822. * Each module has to know that klp_module_going()
  823. * has been called. We never know what module will
  824. * get patched by a new patch.
  825. */
  826. mod->klp_alive = false;
  827. list_for_each_entry(patch, &klp_patches, list) {
  828. klp_for_each_object(patch, obj) {
  829. if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
  830. continue;
  831. if (patch->state != KLP_DISABLED) {
  832. pr_notice("reverting patch '%s' on unloading module '%s'\n",
  833. patch->mod->name, obj->mod->name);
  834. klp_disable_object(obj);
  835. }
  836. klp_free_object_loaded(obj);
  837. break;
  838. }
  839. }
  840. mutex_unlock(&klp_mutex);
  841. }
  842. static int __init klp_init(void)
  843. {
  844. int ret;
  845. ret = klp_check_compiler_support();
  846. if (ret) {
  847. pr_info("Your compiler is too old; turning off.\n");
  848. return -EINVAL;
  849. }
  850. klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
  851. if (!klp_root_kobj)
  852. return -ENOMEM;
  853. return 0;
  854. }
  855. module_init(klp_init);