main.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2002 Richard Henderson
  4. * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
  5. */
  6. #define INCLUDE_VERMAGIC
  7. #include <linux/export.h>
  8. #include <linux/extable.h>
  9. #include <linux/moduleloader.h>
  10. #include <linux/module_signature.h>
  11. #include <linux/trace_events.h>
  12. #include <linux/init.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/buildid.h>
  15. #include <linux/fs.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kernel_read_file.h>
  18. #include <linux/slab.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/elf.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/capability.h>
  26. #include <linux/cpu.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/errno.h>
  29. #include <linux/err.h>
  30. #include <linux/vermagic.h>
  31. #include <linux/notifier.h>
  32. #include <linux/sched.h>
  33. #include <linux/device.h>
  34. #include <linux/string.h>
  35. #include <linux/mutex.h>
  36. #include <linux/rculist.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/cacheflush.h>
  39. #include <linux/set_memory.h>
  40. #include <asm/mmu_context.h>
  41. #include <linux/license.h>
  42. #include <asm/sections.h>
  43. #include <linux/tracepoint.h>
  44. #include <linux/ftrace.h>
  45. #include <linux/livepatch.h>
  46. #include <linux/async.h>
  47. #include <linux/percpu.h>
  48. #include <linux/kmemleak.h>
  49. #include <linux/jump_label.h>
  50. #include <linux/pfn.h>
  51. #include <linux/bsearch.h>
  52. #include <linux/dynamic_debug.h>
  53. #include <linux/audit.h>
  54. #include <linux/cfi.h>
  55. #include <uapi/linux/module.h>
  56. #include "internal.h"
  57. #ifdef CONFIG_RKP
  58. #include <linux/rkp.h>
  59. #endif
  60. #define CREATE_TRACE_POINTS
  61. #include <trace/events/module.h>
  62. #undef CREATE_TRACE_POINTS
  63. #include <trace/hooks/module.h>
  64. /*
  65. * Mutex protects:
  66. * 1) List of modules (also safely readable with preempt_disable),
  67. * 2) module_use links,
  68. * 3) mod_tree.addr_min/mod_tree.addr_max.
  69. * (delete and add uses RCU list operations).
  70. */
  71. DEFINE_MUTEX(module_mutex);
  72. LIST_HEAD(modules);
  73. /* Work queue for freeing init sections in success case */
  74. static void do_free_init(struct work_struct *w);
  75. static DECLARE_WORK(init_free_wq, do_free_init);
  76. static LLIST_HEAD(init_free_list);
  77. struct mod_tree_root mod_tree __cacheline_aligned = {
  78. .addr_min = -1UL,
  79. };
  80. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  81. struct mod_tree_root mod_data_tree __cacheline_aligned = {
  82. .addr_min = -1UL,
  83. };
  84. #endif
  85. #define module_addr_min mod_tree.addr_min
  86. #define module_addr_max mod_tree.addr_max
  87. struct symsearch {
  88. const struct kernel_symbol *start, *stop;
  89. const s32 *crcs;
  90. enum mod_license license;
  91. };
  92. /*
  93. * Bounds of module text, for speeding up __module_address.
  94. * Protected by module_mutex.
  95. */
  96. static void __mod_update_bounds(void *base, unsigned int size, struct mod_tree_root *tree)
  97. {
  98. unsigned long min = (unsigned long)base;
  99. unsigned long max = min + size;
  100. if (min < tree->addr_min)
  101. tree->addr_min = min;
  102. if (max > tree->addr_max)
  103. tree->addr_max = max;
  104. }
  105. static void mod_update_bounds(struct module *mod)
  106. {
  107. __mod_update_bounds(mod->core_layout.base, mod->core_layout.size, &mod_tree);
  108. if (mod->init_layout.size)
  109. __mod_update_bounds(mod->init_layout.base, mod->init_layout.size, &mod_tree);
  110. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  111. __mod_update_bounds(mod->data_layout.base, mod->data_layout.size, &mod_data_tree);
  112. #endif
  113. }
  114. /* Block module loading/unloading? */
  115. int modules_disabled;
  116. core_param(nomodule, modules_disabled, bint, 0);
  117. /* Waiting for a module to finish initializing? */
  118. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  119. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  120. int register_module_notifier(struct notifier_block *nb)
  121. {
  122. return blocking_notifier_chain_register(&module_notify_list, nb);
  123. }
  124. EXPORT_SYMBOL(register_module_notifier);
  125. int unregister_module_notifier(struct notifier_block *nb)
  126. {
  127. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  128. }
  129. EXPORT_SYMBOL(unregister_module_notifier);
  130. /*
  131. * We require a truly strong try_module_get(): 0 means success.
  132. * Otherwise an error is returned due to ongoing or failed
  133. * initialization etc.
  134. */
  135. static inline int strong_try_module_get(struct module *mod)
  136. {
  137. BUG_ON(mod && mod->state == MODULE_STATE_UNFORMED);
  138. if (mod && mod->state == MODULE_STATE_COMING)
  139. return -EBUSY;
  140. if (try_module_get(mod))
  141. return 0;
  142. else
  143. return -ENOENT;
  144. }
  145. static inline void add_taint_module(struct module *mod, unsigned flag,
  146. enum lockdep_ok lockdep_ok)
  147. {
  148. add_taint(flag, lockdep_ok);
  149. set_bit(flag, &mod->taints);
  150. }
  151. /*
  152. * A thread that wants to hold a reference to a module only while it
  153. * is running can call this to safely exit.
  154. */
  155. void __noreturn __module_put_and_kthread_exit(struct module *mod, long code)
  156. {
  157. module_put(mod);
  158. kthread_exit(code);
  159. }
  160. EXPORT_SYMBOL(__module_put_and_kthread_exit);
  161. /* Find a module section: 0 means not found. */
  162. static unsigned int find_sec(const struct load_info *info, const char *name)
  163. {
  164. unsigned int i;
  165. for (i = 1; i < info->hdr->e_shnum; i++) {
  166. Elf_Shdr *shdr = &info->sechdrs[i];
  167. /* Alloc bit cleared means "ignore it." */
  168. if ((shdr->sh_flags & SHF_ALLOC)
  169. && strcmp(info->secstrings + shdr->sh_name, name) == 0)
  170. return i;
  171. }
  172. return 0;
  173. }
  174. /* Find a module section, or NULL. */
  175. static void *section_addr(const struct load_info *info, const char *name)
  176. {
  177. /* Section 0 has sh_addr 0. */
  178. return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
  179. }
  180. /* Find a module section, or NULL. Fill in number of "objects" in section. */
  181. static void *section_objs(const struct load_info *info,
  182. const char *name,
  183. size_t object_size,
  184. unsigned int *num)
  185. {
  186. unsigned int sec = find_sec(info, name);
  187. /* Section 0 has sh_addr 0 and sh_size 0. */
  188. *num = info->sechdrs[sec].sh_size / object_size;
  189. return (void *)info->sechdrs[sec].sh_addr;
  190. }
  191. /* Find a module section: 0 means not found. Ignores SHF_ALLOC flag. */
  192. static unsigned int find_any_sec(const struct load_info *info, const char *name)
  193. {
  194. unsigned int i;
  195. for (i = 1; i < info->hdr->e_shnum; i++) {
  196. Elf_Shdr *shdr = &info->sechdrs[i];
  197. if (strcmp(info->secstrings + shdr->sh_name, name) == 0)
  198. return i;
  199. }
  200. return 0;
  201. }
  202. /*
  203. * Find a module section, or NULL. Fill in number of "objects" in section.
  204. * Ignores SHF_ALLOC flag.
  205. */
  206. static __maybe_unused void *any_section_objs(const struct load_info *info,
  207. const char *name,
  208. size_t object_size,
  209. unsigned int *num)
  210. {
  211. unsigned int sec = find_any_sec(info, name);
  212. /* Section 0 has sh_addr 0 and sh_size 0. */
  213. *num = info->sechdrs[sec].sh_size / object_size;
  214. return (void *)info->sechdrs[sec].sh_addr;
  215. }
  216. #ifndef CONFIG_MODVERSIONS
  217. #define symversion(base, idx) NULL
  218. #else
  219. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  220. #endif
  221. static const char *kernel_symbol_name(const struct kernel_symbol *sym)
  222. {
  223. #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  224. return offset_to_ptr(&sym->name_offset);
  225. #else
  226. return sym->name;
  227. #endif
  228. }
  229. static const char *kernel_symbol_namespace(const struct kernel_symbol *sym)
  230. {
  231. #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  232. if (!sym->namespace_offset)
  233. return NULL;
  234. return offset_to_ptr(&sym->namespace_offset);
  235. #else
  236. return sym->namespace;
  237. #endif
  238. }
  239. int cmp_name(const void *name, const void *sym)
  240. {
  241. return strcmp(name, kernel_symbol_name(sym));
  242. }
  243. static bool find_exported_symbol_in_section(const struct symsearch *syms,
  244. struct module *owner,
  245. struct find_symbol_arg *fsa)
  246. {
  247. struct kernel_symbol *sym;
  248. if (!fsa->gplok && syms->license == GPL_ONLY)
  249. return false;
  250. sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
  251. sizeof(struct kernel_symbol), cmp_name);
  252. if (!sym)
  253. return false;
  254. fsa->owner = owner;
  255. fsa->crc = symversion(syms->crcs, sym - syms->start);
  256. fsa->sym = sym;
  257. fsa->license = syms->license;
  258. return true;
  259. }
  260. /*
  261. * Find an exported symbol and return it, along with, (optional) crc and
  262. * (optional) module which owns it. Needs preempt disabled or module_mutex.
  263. */
  264. bool find_symbol(struct find_symbol_arg *fsa)
  265. {
  266. static const struct symsearch arr[] = {
  267. { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
  268. NOT_GPL_ONLY },
  269. { __start___ksymtab_gpl, __stop___ksymtab_gpl,
  270. __start___kcrctab_gpl,
  271. GPL_ONLY },
  272. };
  273. struct module *mod;
  274. unsigned int i;
  275. module_assert_mutex_or_preempt();
  276. for (i = 0; i < ARRAY_SIZE(arr); i++)
  277. if (find_exported_symbol_in_section(&arr[i], NULL, fsa))
  278. return true;
  279. list_for_each_entry_rcu(mod, &modules, list,
  280. lockdep_is_held(&module_mutex)) {
  281. struct symsearch arr[] = {
  282. { mod->syms, mod->syms + mod->num_syms, mod->crcs,
  283. NOT_GPL_ONLY },
  284. { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
  285. mod->gpl_crcs,
  286. GPL_ONLY },
  287. };
  288. if (mod->state == MODULE_STATE_UNFORMED)
  289. continue;
  290. for (i = 0; i < ARRAY_SIZE(arr); i++)
  291. if (find_exported_symbol_in_section(&arr[i], mod, fsa))
  292. return true;
  293. }
  294. pr_debug("Failed to find symbol %s\n", fsa->name);
  295. return false;
  296. }
  297. /*
  298. * Search for module by name: must hold module_mutex (or preempt disabled
  299. * for read-only access).
  300. */
  301. struct module *find_module_all(const char *name, size_t len,
  302. bool even_unformed)
  303. {
  304. struct module *mod;
  305. module_assert_mutex_or_preempt();
  306. list_for_each_entry_rcu(mod, &modules, list,
  307. lockdep_is_held(&module_mutex)) {
  308. if (!even_unformed && mod->state == MODULE_STATE_UNFORMED)
  309. continue;
  310. if (strlen(mod->name) == len && !memcmp(mod->name, name, len))
  311. return mod;
  312. }
  313. return NULL;
  314. }
  315. struct module *find_module(const char *name)
  316. {
  317. return find_module_all(name, strlen(name), false);
  318. }
  319. #ifdef CONFIG_SMP
  320. static inline void __percpu *mod_percpu(struct module *mod)
  321. {
  322. return mod->percpu;
  323. }
  324. static int percpu_modalloc(struct module *mod, struct load_info *info)
  325. {
  326. Elf_Shdr *pcpusec = &info->sechdrs[info->index.pcpu];
  327. unsigned long align = pcpusec->sh_addralign;
  328. if (!pcpusec->sh_size)
  329. return 0;
  330. if (align > PAGE_SIZE) {
  331. pr_warn("%s: per-cpu alignment %li > %li\n",
  332. mod->name, align, PAGE_SIZE);
  333. align = PAGE_SIZE;
  334. }
  335. mod->percpu = __alloc_reserved_percpu(pcpusec->sh_size, align);
  336. if (!mod->percpu) {
  337. pr_warn("%s: Could not allocate %lu bytes percpu data\n",
  338. mod->name, (unsigned long)pcpusec->sh_size);
  339. return -ENOMEM;
  340. }
  341. mod->percpu_size = pcpusec->sh_size;
  342. return 0;
  343. }
  344. static void percpu_modfree(struct module *mod)
  345. {
  346. free_percpu(mod->percpu);
  347. }
  348. static unsigned int find_pcpusec(struct load_info *info)
  349. {
  350. return find_sec(info, ".data..percpu");
  351. }
  352. static void percpu_modcopy(struct module *mod,
  353. const void *from, unsigned long size)
  354. {
  355. int cpu;
  356. for_each_possible_cpu(cpu)
  357. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  358. }
  359. bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  360. {
  361. struct module *mod;
  362. unsigned int cpu;
  363. preempt_disable();
  364. list_for_each_entry_rcu(mod, &modules, list) {
  365. if (mod->state == MODULE_STATE_UNFORMED)
  366. continue;
  367. if (!mod->percpu_size)
  368. continue;
  369. for_each_possible_cpu(cpu) {
  370. void *start = per_cpu_ptr(mod->percpu, cpu);
  371. void *va = (void *)addr;
  372. if (va >= start && va < start + mod->percpu_size) {
  373. if (can_addr) {
  374. *can_addr = (unsigned long) (va - start);
  375. *can_addr += (unsigned long)
  376. per_cpu_ptr(mod->percpu,
  377. get_boot_cpu_id());
  378. }
  379. preempt_enable();
  380. return true;
  381. }
  382. }
  383. }
  384. preempt_enable();
  385. return false;
  386. }
  387. /**
  388. * is_module_percpu_address() - test whether address is from module static percpu
  389. * @addr: address to test
  390. *
  391. * Test whether @addr belongs to module static percpu area.
  392. *
  393. * Return: %true if @addr is from module static percpu area
  394. */
  395. bool is_module_percpu_address(unsigned long addr)
  396. {
  397. return __is_module_percpu_address(addr, NULL);
  398. }
  399. #else /* ... !CONFIG_SMP */
  400. static inline void __percpu *mod_percpu(struct module *mod)
  401. {
  402. return NULL;
  403. }
  404. static int percpu_modalloc(struct module *mod, struct load_info *info)
  405. {
  406. /* UP modules shouldn't have this section: ENOMEM isn't quite right */
  407. if (info->sechdrs[info->index.pcpu].sh_size != 0)
  408. return -ENOMEM;
  409. return 0;
  410. }
  411. static inline void percpu_modfree(struct module *mod)
  412. {
  413. }
  414. static unsigned int find_pcpusec(struct load_info *info)
  415. {
  416. return 0;
  417. }
  418. static inline void percpu_modcopy(struct module *mod,
  419. const void *from, unsigned long size)
  420. {
  421. /* pcpusec should be 0, and size of that section should be 0. */
  422. BUG_ON(size != 0);
  423. }
  424. bool is_module_percpu_address(unsigned long addr)
  425. {
  426. return false;
  427. }
  428. bool __is_module_percpu_address(unsigned long addr, unsigned long *can_addr)
  429. {
  430. return false;
  431. }
  432. #endif /* CONFIG_SMP */
  433. #define MODINFO_ATTR(field) \
  434. static void setup_modinfo_##field(struct module *mod, const char *s) \
  435. { \
  436. mod->field = kstrdup(s, GFP_KERNEL); \
  437. } \
  438. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  439. struct module_kobject *mk, char *buffer) \
  440. { \
  441. return scnprintf(buffer, PAGE_SIZE, "%s\n", mk->mod->field); \
  442. } \
  443. static int modinfo_##field##_exists(struct module *mod) \
  444. { \
  445. return mod->field != NULL; \
  446. } \
  447. static void free_modinfo_##field(struct module *mod) \
  448. { \
  449. kfree(mod->field); \
  450. mod->field = NULL; \
  451. } \
  452. static struct module_attribute modinfo_##field = { \
  453. .attr = { .name = __stringify(field), .mode = 0444 }, \
  454. .show = show_modinfo_##field, \
  455. .setup = setup_modinfo_##field, \
  456. .test = modinfo_##field##_exists, \
  457. .free = free_modinfo_##field, \
  458. };
  459. MODINFO_ATTR(version);
  460. MODINFO_ATTR(srcversion);
  461. MODINFO_ATTR(scmversion);
  462. static struct {
  463. char name[MODULE_NAME_LEN + 1];
  464. char taints[MODULE_FLAGS_BUF_SIZE];
  465. } last_unloaded_module;
  466. #ifdef CONFIG_MODULE_UNLOAD
  467. EXPORT_TRACEPOINT_SYMBOL(module_get);
  468. /* MODULE_REF_BASE is the base reference count by kmodule loader. */
  469. #define MODULE_REF_BASE 1
  470. /* Init the unload section of the module. */
  471. static int module_unload_init(struct module *mod)
  472. {
  473. /*
  474. * Initialize reference counter to MODULE_REF_BASE.
  475. * refcnt == 0 means module is going.
  476. */
  477. atomic_set(&mod->refcnt, MODULE_REF_BASE);
  478. INIT_LIST_HEAD(&mod->source_list);
  479. INIT_LIST_HEAD(&mod->target_list);
  480. /* Hold reference count during initialization. */
  481. atomic_inc(&mod->refcnt);
  482. return 0;
  483. }
  484. /* Does a already use b? */
  485. static int already_uses(struct module *a, struct module *b)
  486. {
  487. struct module_use *use;
  488. list_for_each_entry(use, &b->source_list, source_list) {
  489. if (use->source == a) {
  490. pr_debug("%s uses %s!\n", a->name, b->name);
  491. return 1;
  492. }
  493. }
  494. pr_debug("%s does not use %s!\n", a->name, b->name);
  495. return 0;
  496. }
  497. /*
  498. * Module a uses b
  499. * - we add 'a' as a "source", 'b' as a "target" of module use
  500. * - the module_use is added to the list of 'b' sources (so
  501. * 'b' can walk the list to see who sourced them), and of 'a'
  502. * targets (so 'a' can see what modules it targets).
  503. */
  504. static int add_module_usage(struct module *a, struct module *b)
  505. {
  506. struct module_use *use;
  507. pr_debug("Allocating new usage for %s.\n", a->name);
  508. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  509. if (!use)
  510. return -ENOMEM;
  511. use->source = a;
  512. use->target = b;
  513. list_add(&use->source_list, &b->source_list);
  514. list_add(&use->target_list, &a->target_list);
  515. return 0;
  516. }
  517. /* Module a uses b: caller needs module_mutex() */
  518. static int ref_module(struct module *a, struct module *b)
  519. {
  520. int err;
  521. if (b == NULL || already_uses(a, b))
  522. return 0;
  523. /* If module isn't available, we fail. */
  524. err = strong_try_module_get(b);
  525. if (err)
  526. return err;
  527. err = add_module_usage(a, b);
  528. if (err) {
  529. module_put(b);
  530. return err;
  531. }
  532. return 0;
  533. }
  534. /* Clear the unload stuff of the module. */
  535. static void module_unload_free(struct module *mod)
  536. {
  537. struct module_use *use, *tmp;
  538. mutex_lock(&module_mutex);
  539. list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
  540. struct module *i = use->target;
  541. pr_debug("%s unusing %s\n", mod->name, i->name);
  542. module_put(i);
  543. list_del(&use->source_list);
  544. list_del(&use->target_list);
  545. kfree(use);
  546. }
  547. mutex_unlock(&module_mutex);
  548. }
  549. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  550. static inline int try_force_unload(unsigned int flags)
  551. {
  552. int ret = (flags & O_TRUNC);
  553. if (ret)
  554. add_taint(TAINT_FORCED_RMMOD, LOCKDEP_NOW_UNRELIABLE);
  555. return ret;
  556. }
  557. #else
  558. static inline int try_force_unload(unsigned int flags)
  559. {
  560. return 0;
  561. }
  562. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  563. /* Try to release refcount of module, 0 means success. */
  564. static int try_release_module_ref(struct module *mod)
  565. {
  566. int ret;
  567. /* Try to decrement refcnt which we set at loading */
  568. ret = atomic_sub_return(MODULE_REF_BASE, &mod->refcnt);
  569. BUG_ON(ret < 0);
  570. if (ret)
  571. /* Someone can put this right now, recover with checking */
  572. ret = atomic_add_unless(&mod->refcnt, MODULE_REF_BASE, 0);
  573. return ret;
  574. }
  575. static int try_stop_module(struct module *mod, int flags, int *forced)
  576. {
  577. /* If it's not unused, quit unless we're forcing. */
  578. if (try_release_module_ref(mod) != 0) {
  579. *forced = try_force_unload(flags);
  580. if (!(*forced))
  581. return -EWOULDBLOCK;
  582. }
  583. /* Mark it as dying. */
  584. mod->state = MODULE_STATE_GOING;
  585. return 0;
  586. }
  587. /**
  588. * module_refcount() - return the refcount or -1 if unloading
  589. * @mod: the module we're checking
  590. *
  591. * Return:
  592. * -1 if the module is in the process of unloading
  593. * otherwise the number of references in the kernel to the module
  594. */
  595. int module_refcount(struct module *mod)
  596. {
  597. return atomic_read(&mod->refcnt) - MODULE_REF_BASE;
  598. }
  599. EXPORT_SYMBOL(module_refcount);
  600. /* This exists whether we can unload or not */
  601. static void free_module(struct module *mod);
  602. SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
  603. unsigned int, flags)
  604. {
  605. struct module *mod;
  606. char name[MODULE_NAME_LEN];
  607. char buf[MODULE_FLAGS_BUF_SIZE];
  608. int ret, forced = 0;
  609. #ifdef CONFIG_RKP
  610. struct module_info rkp_mod_info;
  611. #endif
  612. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  613. return -EPERM;
  614. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  615. return -EFAULT;
  616. name[MODULE_NAME_LEN-1] = '\0';
  617. audit_log_kern_module(name);
  618. if (mutex_lock_interruptible(&module_mutex) != 0)
  619. return -EINTR;
  620. mod = find_module(name);
  621. if (!mod) {
  622. ret = -ENOENT;
  623. goto out;
  624. }
  625. if (!list_empty(&mod->source_list)) {
  626. /* Other modules depend on us: get rid of them first. */
  627. ret = -EWOULDBLOCK;
  628. goto out;
  629. }
  630. /* Doing init or already dying? */
  631. if (mod->state != MODULE_STATE_LIVE) {
  632. /* FIXME: if (force), slam module count damn the torpedoes */
  633. pr_debug("%s already dying\n", mod->name);
  634. ret = -EBUSY;
  635. goto out;
  636. }
  637. /* If it has an init func, it must have an exit func to unload */
  638. if (mod->init && !mod->exit) {
  639. forced = try_force_unload(flags);
  640. if (!forced) {
  641. /* This module can't be removed */
  642. ret = -EBUSY;
  643. goto out;
  644. }
  645. }
  646. ret = try_stop_module(mod, flags, &forced);
  647. if (ret != 0)
  648. goto out;
  649. mutex_unlock(&module_mutex);
  650. /* Final destruction now no one is using it. */
  651. if (mod->exit != NULL)
  652. mod->exit();
  653. blocking_notifier_call_chain(&module_notify_list,
  654. MODULE_STATE_GOING, mod);
  655. klp_module_going(mod);
  656. ftrace_release_mod(mod);
  657. async_synchronize_full();
  658. /* Store the name and taints of the last unloaded module for diagnostic purposes */
  659. strscpy(last_unloaded_module.name, mod->name, sizeof(last_unloaded_module.name));
  660. strscpy(last_unloaded_module.taints, module_flags(mod, buf, false), sizeof(last_unloaded_module.taints));
  661. #ifdef CONFIG_RKP
  662. rkp_mod_info.base_va = 0;
  663. rkp_mod_info.vm_size = 0;
  664. rkp_mod_info.core_base_va = (u64)mod->core_layout.base;
  665. rkp_mod_info.core_text_size = (u64)mod->core_layout.text_size;
  666. rkp_mod_info.core_ro_size = (u64)mod->core_layout.ro_size;
  667. rkp_mod_info.init_base_va = 0;
  668. rkp_mod_info.init_text_size = 0;
  669. uh_call(UH_APP_RKP, RKP_MODULE_LOAD, RKP_MODULE_PXN_SET, (u64)&rkp_mod_info, 0, 0);
  670. #endif
  671. free_module(mod);
  672. /* someone could wait for the module in add_unformed_module() */
  673. wake_up_all(&module_wq);
  674. return 0;
  675. out:
  676. mutex_unlock(&module_mutex);
  677. return ret;
  678. }
  679. void __symbol_put(const char *symbol)
  680. {
  681. struct find_symbol_arg fsa = {
  682. .name = symbol,
  683. .gplok = true,
  684. };
  685. preempt_disable();
  686. BUG_ON(!find_symbol(&fsa));
  687. module_put(fsa.owner);
  688. preempt_enable();
  689. }
  690. EXPORT_SYMBOL(__symbol_put);
  691. /* Note this assumes addr is a function, which it currently always is. */
  692. void symbol_put_addr(void *addr)
  693. {
  694. struct module *modaddr;
  695. unsigned long a = (unsigned long)dereference_function_descriptor(addr);
  696. if (core_kernel_text(a))
  697. return;
  698. /*
  699. * Even though we hold a reference on the module; we still need to
  700. * disable preemption in order to safely traverse the data structure.
  701. */
  702. preempt_disable();
  703. modaddr = __module_text_address(a);
  704. BUG_ON(!modaddr);
  705. module_put(modaddr);
  706. preempt_enable();
  707. }
  708. EXPORT_SYMBOL_GPL(symbol_put_addr);
  709. static ssize_t show_refcnt(struct module_attribute *mattr,
  710. struct module_kobject *mk, char *buffer)
  711. {
  712. return sprintf(buffer, "%i\n", module_refcount(mk->mod));
  713. }
  714. static struct module_attribute modinfo_refcnt =
  715. __ATTR(refcnt, 0444, show_refcnt, NULL);
  716. void __module_get(struct module *module)
  717. {
  718. if (module) {
  719. preempt_disable();
  720. atomic_inc(&module->refcnt);
  721. trace_module_get(module, _RET_IP_);
  722. preempt_enable();
  723. }
  724. }
  725. EXPORT_SYMBOL(__module_get);
  726. bool try_module_get(struct module *module)
  727. {
  728. bool ret = true;
  729. if (module) {
  730. preempt_disable();
  731. /* Note: here, we can fail to get a reference */
  732. if (likely(module_is_live(module) &&
  733. atomic_inc_not_zero(&module->refcnt) != 0))
  734. trace_module_get(module, _RET_IP_);
  735. else
  736. ret = false;
  737. preempt_enable();
  738. }
  739. return ret;
  740. }
  741. EXPORT_SYMBOL(try_module_get);
  742. void module_put(struct module *module)
  743. {
  744. int ret;
  745. if (module) {
  746. preempt_disable();
  747. ret = atomic_dec_if_positive(&module->refcnt);
  748. WARN_ON(ret < 0); /* Failed to put refcount */
  749. trace_module_put(module, _RET_IP_);
  750. preempt_enable();
  751. }
  752. }
  753. EXPORT_SYMBOL(module_put);
  754. #else /* !CONFIG_MODULE_UNLOAD */
  755. static inline void module_unload_free(struct module *mod)
  756. {
  757. }
  758. static int ref_module(struct module *a, struct module *b)
  759. {
  760. return strong_try_module_get(b);
  761. }
  762. static inline int module_unload_init(struct module *mod)
  763. {
  764. return 0;
  765. }
  766. #endif /* CONFIG_MODULE_UNLOAD */
  767. size_t module_flags_taint(unsigned long taints, char *buf)
  768. {
  769. size_t l = 0;
  770. int i;
  771. for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
  772. if (taint_flags[i].module && test_bit(i, &taints))
  773. buf[l++] = taint_flags[i].c_true;
  774. }
  775. return l;
  776. }
  777. static ssize_t show_initstate(struct module_attribute *mattr,
  778. struct module_kobject *mk, char *buffer)
  779. {
  780. const char *state = "unknown";
  781. switch (mk->mod->state) {
  782. case MODULE_STATE_LIVE:
  783. state = "live";
  784. break;
  785. case MODULE_STATE_COMING:
  786. state = "coming";
  787. break;
  788. case MODULE_STATE_GOING:
  789. state = "going";
  790. break;
  791. default:
  792. BUG();
  793. }
  794. return sprintf(buffer, "%s\n", state);
  795. }
  796. static struct module_attribute modinfo_initstate =
  797. __ATTR(initstate, 0444, show_initstate, NULL);
  798. static ssize_t store_uevent(struct module_attribute *mattr,
  799. struct module_kobject *mk,
  800. const char *buffer, size_t count)
  801. {
  802. int rc;
  803. rc = kobject_synth_uevent(&mk->kobj, buffer, count);
  804. return rc ? rc : count;
  805. }
  806. struct module_attribute module_uevent =
  807. __ATTR(uevent, 0200, NULL, store_uevent);
  808. static ssize_t show_coresize(struct module_attribute *mattr,
  809. struct module_kobject *mk, char *buffer)
  810. {
  811. return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
  812. }
  813. static struct module_attribute modinfo_coresize =
  814. __ATTR(coresize, 0444, show_coresize, NULL);
  815. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  816. static ssize_t show_datasize(struct module_attribute *mattr,
  817. struct module_kobject *mk, char *buffer)
  818. {
  819. return sprintf(buffer, "%u\n", mk->mod->data_layout.size);
  820. }
  821. static struct module_attribute modinfo_datasize =
  822. __ATTR(datasize, 0444, show_datasize, NULL);
  823. #endif
  824. static ssize_t show_initsize(struct module_attribute *mattr,
  825. struct module_kobject *mk, char *buffer)
  826. {
  827. return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
  828. }
  829. static struct module_attribute modinfo_initsize =
  830. __ATTR(initsize, 0444, show_initsize, NULL);
  831. static ssize_t show_taint(struct module_attribute *mattr,
  832. struct module_kobject *mk, char *buffer)
  833. {
  834. size_t l;
  835. l = module_flags_taint(mk->mod->taints, buffer);
  836. buffer[l++] = '\n';
  837. return l;
  838. }
  839. static struct module_attribute modinfo_taint =
  840. __ATTR(taint, 0444, show_taint, NULL);
  841. struct module_attribute *modinfo_attrs[] = {
  842. &module_uevent,
  843. &modinfo_version,
  844. &modinfo_srcversion,
  845. &modinfo_scmversion,
  846. &modinfo_initstate,
  847. &modinfo_coresize,
  848. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  849. &modinfo_datasize,
  850. #endif
  851. &modinfo_initsize,
  852. &modinfo_taint,
  853. #ifdef CONFIG_MODULE_UNLOAD
  854. &modinfo_refcnt,
  855. #endif
  856. NULL,
  857. };
  858. size_t modinfo_attrs_count = ARRAY_SIZE(modinfo_attrs);
  859. static const char vermagic[] = VERMAGIC_STRING;
  860. int try_to_force_load(struct module *mod, const char *reason)
  861. {
  862. #ifdef CONFIG_MODULE_FORCE_LOAD
  863. if (!test_taint(TAINT_FORCED_MODULE))
  864. pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
  865. add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
  866. return 0;
  867. #else
  868. return -ENOEXEC;
  869. #endif
  870. }
  871. static char *get_modinfo(const struct load_info *info, const char *tag);
  872. static char *get_next_modinfo(const struct load_info *info, const char *tag,
  873. char *prev);
  874. static int verify_namespace_is_imported(const struct load_info *info,
  875. const struct kernel_symbol *sym,
  876. struct module *mod)
  877. {
  878. const char *namespace;
  879. char *imported_namespace;
  880. namespace = kernel_symbol_namespace(sym);
  881. if (namespace && namespace[0]) {
  882. imported_namespace = get_modinfo(info, "import_ns");
  883. while (imported_namespace) {
  884. if (strcmp(namespace, imported_namespace) == 0)
  885. return 0;
  886. imported_namespace = get_next_modinfo(
  887. info, "import_ns", imported_namespace);
  888. }
  889. #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  890. pr_warn(
  891. #else
  892. pr_err(
  893. #endif
  894. "%s: module uses symbol (%s) from namespace %s, but does not import it.\n",
  895. mod->name, kernel_symbol_name(sym), namespace);
  896. #ifndef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS
  897. return -EINVAL;
  898. #endif
  899. }
  900. return 0;
  901. }
  902. static bool inherit_taint(struct module *mod, struct module *owner, const char *name)
  903. {
  904. if (!owner || !test_bit(TAINT_PROPRIETARY_MODULE, &owner->taints))
  905. return true;
  906. if (mod->using_gplonly_symbols) {
  907. pr_err("%s: module using GPL-only symbols uses symbols %s from proprietary module %s.\n",
  908. mod->name, name, owner->name);
  909. return false;
  910. }
  911. if (!test_bit(TAINT_PROPRIETARY_MODULE, &mod->taints)) {
  912. pr_warn("%s: module uses symbols %s from proprietary module %s, inheriting taint.\n",
  913. mod->name, name, owner->name);
  914. set_bit(TAINT_PROPRIETARY_MODULE, &mod->taints);
  915. }
  916. return true;
  917. }
  918. /* Resolve a symbol for this module. I.e. if we find one, record usage. */
  919. static const struct kernel_symbol *resolve_symbol(struct module *mod,
  920. const struct load_info *info,
  921. const char *name,
  922. char ownername[])
  923. {
  924. struct find_symbol_arg fsa = {
  925. .name = name,
  926. .gplok = !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)),
  927. .warn = true,
  928. };
  929. int err;
  930. /*
  931. * The module_mutex should not be a heavily contended lock;
  932. * if we get the occasional sleep here, we'll go an extra iteration
  933. * in the wait_event_interruptible(), which is harmless.
  934. */
  935. sched_annotate_sleep();
  936. mutex_lock(&module_mutex);
  937. if (!find_symbol(&fsa))
  938. goto unlock;
  939. if (fsa.license == GPL_ONLY)
  940. mod->using_gplonly_symbols = true;
  941. if (!inherit_taint(mod, fsa.owner, name)) {
  942. fsa.sym = NULL;
  943. goto getname;
  944. }
  945. if (!check_version(info, name, mod, fsa.crc)) {
  946. fsa.sym = ERR_PTR(-EINVAL);
  947. goto getname;
  948. }
  949. err = verify_namespace_is_imported(info, fsa.sym, mod);
  950. if (err) {
  951. fsa.sym = ERR_PTR(err);
  952. goto getname;
  953. }
  954. /*
  955. * ANDROID: GKI:
  956. * In case of an unsigned module symbol resolves only if:
  957. * 1. Symbol is in the list of unprotected symbol list OR
  958. * 2. If symbol owner is not NULL i.e. owner is another module;
  959. * it has to be an unsigned module and not signed GKI module
  960. * to protect symbols exported by signed GKI modules.
  961. */
  962. if (!mod->sig_ok &&
  963. !gki_is_module_unprotected_symbol(name) &&
  964. fsa.owner && fsa.owner->sig_ok) {
  965. fsa.sym = ERR_PTR(-EACCES);
  966. goto getname;
  967. }
  968. err = ref_module(mod, fsa.owner);
  969. if (err) {
  970. fsa.sym = ERR_PTR(err);
  971. goto getname;
  972. }
  973. getname:
  974. /* We must make copy under the lock if we failed to get ref. */
  975. strncpy(ownername, module_name(fsa.owner), MODULE_NAME_LEN);
  976. unlock:
  977. mutex_unlock(&module_mutex);
  978. return fsa.sym;
  979. }
  980. static const struct kernel_symbol *
  981. resolve_symbol_wait(struct module *mod,
  982. const struct load_info *info,
  983. const char *name)
  984. {
  985. const struct kernel_symbol *ksym;
  986. char owner[MODULE_NAME_LEN];
  987. if (wait_event_interruptible_timeout(module_wq,
  988. !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
  989. || PTR_ERR(ksym) != -EBUSY,
  990. 30 * HZ) <= 0) {
  991. pr_warn("%s: gave up waiting for init of module %s.\n",
  992. mod->name, owner);
  993. }
  994. return ksym;
  995. }
  996. void __weak module_memfree(void *module_region)
  997. {
  998. /*
  999. * This memory may be RO, and freeing RO memory in an interrupt is not
  1000. * supported by vmalloc.
  1001. */
  1002. WARN_ON(in_interrupt());
  1003. vfree(module_region);
  1004. }
  1005. void __weak module_arch_cleanup(struct module *mod)
  1006. {
  1007. }
  1008. void __weak module_arch_freeing_init(struct module *mod)
  1009. {
  1010. }
  1011. /* Free a module, remove from lists, etc. */
  1012. static void free_module(struct module *mod)
  1013. {
  1014. trace_module_free(mod);
  1015. mod_sysfs_teardown(mod);
  1016. /*
  1017. * We leave it in list to prevent duplicate loads, but make sure
  1018. * that noone uses it while it's being deconstructed.
  1019. */
  1020. mutex_lock(&module_mutex);
  1021. mod->state = MODULE_STATE_UNFORMED;
  1022. mutex_unlock(&module_mutex);
  1023. /* Remove dynamic debug info */
  1024. ddebug_remove_module(mod->name);
  1025. /* Arch-specific cleanup. */
  1026. module_arch_cleanup(mod);
  1027. /* Module unload stuff */
  1028. module_unload_free(mod);
  1029. /* Free any allocated parameters. */
  1030. destroy_params(mod->kp, mod->num_kp);
  1031. if (is_livepatch_module(mod))
  1032. free_module_elf(mod);
  1033. /* Now we can delete it from the lists */
  1034. mutex_lock(&module_mutex);
  1035. /* Unlink carefully: kallsyms could be walking list. */
  1036. list_del_rcu(&mod->list);
  1037. mod_tree_remove(mod);
  1038. /* Remove this module from bug list, this uses list_del_rcu */
  1039. module_bug_cleanup(mod);
  1040. /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
  1041. synchronize_rcu();
  1042. if (try_add_tainted_module(mod))
  1043. pr_err("%s: adding tainted module to the unloaded tainted modules list failed.\n",
  1044. mod->name);
  1045. mutex_unlock(&module_mutex);
  1046. /* This may be empty, but that's OK */
  1047. module_arch_freeing_init(mod);
  1048. trace_android_rvh_set_module_init_rw_nx(mod);
  1049. module_memfree(mod->init_layout.base);
  1050. kfree(mod->args);
  1051. percpu_modfree(mod);
  1052. /* Free lock-classes; relies on the preceding sync_rcu(). */
  1053. lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
  1054. /* Finally, free the core (containing the module structure) */
  1055. trace_android_rvh_set_module_core_rw_nx(mod);
  1056. module_memfree(mod->core_layout.base);
  1057. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  1058. vfree(mod->data_layout.base);
  1059. #endif
  1060. }
  1061. void *__symbol_get(const char *symbol)
  1062. {
  1063. struct find_symbol_arg fsa = {
  1064. .name = symbol,
  1065. .gplok = true,
  1066. .warn = true,
  1067. };
  1068. preempt_disable();
  1069. if (!find_symbol(&fsa))
  1070. goto fail;
  1071. if (fsa.license != GPL_ONLY) {
  1072. pr_warn("failing symbol_get of non-GPLONLY symbol %s.\n",
  1073. symbol);
  1074. goto fail;
  1075. }
  1076. if (strong_try_module_get(fsa.owner))
  1077. goto fail;
  1078. preempt_enable();
  1079. return (void *)kernel_symbol_value(fsa.sym);
  1080. fail:
  1081. preempt_enable();
  1082. return NULL;
  1083. }
  1084. EXPORT_SYMBOL_GPL(__symbol_get);
  1085. /*
  1086. * Ensure that an exported symbol [global namespace] does not already exist
  1087. * in the kernel or in some other module's exported symbol table.
  1088. *
  1089. * You must hold the module_mutex.
  1090. */
  1091. static int verify_exported_symbols(struct module *mod)
  1092. {
  1093. unsigned int i;
  1094. const struct kernel_symbol *s;
  1095. struct {
  1096. const struct kernel_symbol *sym;
  1097. unsigned int num;
  1098. } arr[] = {
  1099. { mod->syms, mod->num_syms },
  1100. { mod->gpl_syms, mod->num_gpl_syms },
  1101. };
  1102. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1103. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1104. struct find_symbol_arg fsa = {
  1105. .name = kernel_symbol_name(s),
  1106. .gplok = true,
  1107. };
  1108. if (!mod->sig_ok && gki_is_module_protected_export(
  1109. kernel_symbol_name(s))) {
  1110. pr_err("%s: exports protected symbol %s\n",
  1111. mod->name, kernel_symbol_name(s));
  1112. return -EACCES;
  1113. }
  1114. if (find_symbol(&fsa)) {
  1115. pr_err("%s: exports duplicate symbol %s"
  1116. " (owned by %s)\n",
  1117. mod->name, kernel_symbol_name(s),
  1118. module_name(fsa.owner));
  1119. return -ENOEXEC;
  1120. }
  1121. }
  1122. }
  1123. return 0;
  1124. }
  1125. static bool ignore_undef_symbol(Elf_Half emachine, const char *name)
  1126. {
  1127. /*
  1128. * On x86, PIC code and Clang non-PIC code may have call foo@PLT. GNU as
  1129. * before 2.37 produces an unreferenced _GLOBAL_OFFSET_TABLE_ on x86-64.
  1130. * i386 has a similar problem but may not deserve a fix.
  1131. *
  1132. * If we ever have to ignore many symbols, consider refactoring the code to
  1133. * only warn if referenced by a relocation.
  1134. */
  1135. if (emachine == EM_386 || emachine == EM_X86_64)
  1136. return !strcmp(name, "_GLOBAL_OFFSET_TABLE_");
  1137. return false;
  1138. }
  1139. /* Change all symbols so that st_value encodes the pointer directly. */
  1140. static int simplify_symbols(struct module *mod, const struct load_info *info)
  1141. {
  1142. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1143. Elf_Sym *sym = (void *)symsec->sh_addr;
  1144. unsigned long secbase;
  1145. unsigned int i;
  1146. int ret = 0;
  1147. const struct kernel_symbol *ksym;
  1148. for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
  1149. const char *name = info->strtab + sym[i].st_name;
  1150. switch (sym[i].st_shndx) {
  1151. case SHN_COMMON:
  1152. /* Ignore common symbols */
  1153. if (!strncmp(name, "__gnu_lto", 9))
  1154. break;
  1155. /*
  1156. * We compiled with -fno-common. These are not
  1157. * supposed to happen.
  1158. */
  1159. pr_debug("Common symbol: %s\n", name);
  1160. pr_warn("%s: please compile with -fno-common\n",
  1161. mod->name);
  1162. ret = -ENOEXEC;
  1163. break;
  1164. case SHN_ABS:
  1165. /* Don't need to do anything */
  1166. pr_debug("Absolute symbol: 0x%08lx\n",
  1167. (long)sym[i].st_value);
  1168. break;
  1169. case SHN_LIVEPATCH:
  1170. /* Livepatch symbols are resolved by livepatch */
  1171. break;
  1172. case SHN_UNDEF:
  1173. ksym = resolve_symbol_wait(mod, info, name);
  1174. /* Ok if resolved. */
  1175. if (ksym && !IS_ERR(ksym)) {
  1176. sym[i].st_value = kernel_symbol_value(ksym);
  1177. break;
  1178. }
  1179. /* Ok if weak or ignored. */
  1180. if (!ksym &&
  1181. (ELF_ST_BIND(sym[i].st_info) == STB_WEAK ||
  1182. ignore_undef_symbol(info->hdr->e_machine, name)))
  1183. break;
  1184. if (PTR_ERR(ksym) == -EACCES) {
  1185. ret = -EACCES;
  1186. pr_warn("%s: Protected symbol: %s (err %d)\n",
  1187. mod->name, name, ret);
  1188. } else {
  1189. ret = PTR_ERR(ksym) ?: -ENOENT;
  1190. pr_warn("%s: Unknown symbol %s (err %d)\n",
  1191. mod->name, name, ret);
  1192. }
  1193. break;
  1194. default:
  1195. /* Divert to percpu allocation if a percpu var. */
  1196. if (sym[i].st_shndx == info->index.pcpu)
  1197. secbase = (unsigned long)mod_percpu(mod);
  1198. else
  1199. secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
  1200. sym[i].st_value += secbase;
  1201. break;
  1202. }
  1203. }
  1204. return ret;
  1205. }
  1206. static int apply_relocations(struct module *mod, const struct load_info *info)
  1207. {
  1208. unsigned int i;
  1209. int err = 0;
  1210. /* Now do relocations. */
  1211. for (i = 1; i < info->hdr->e_shnum; i++) {
  1212. unsigned int infosec = info->sechdrs[i].sh_info;
  1213. /* Not a valid relocation section? */
  1214. if (infosec >= info->hdr->e_shnum)
  1215. continue;
  1216. /* Don't bother with non-allocated sections */
  1217. if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
  1218. continue;
  1219. if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
  1220. err = klp_apply_section_relocs(mod, info->sechdrs,
  1221. info->secstrings,
  1222. info->strtab,
  1223. info->index.sym, i,
  1224. NULL);
  1225. else if (info->sechdrs[i].sh_type == SHT_REL)
  1226. err = apply_relocate(info->sechdrs, info->strtab,
  1227. info->index.sym, i, mod);
  1228. else if (info->sechdrs[i].sh_type == SHT_RELA)
  1229. err = apply_relocate_add(info->sechdrs, info->strtab,
  1230. info->index.sym, i, mod);
  1231. if (err < 0)
  1232. break;
  1233. }
  1234. return err;
  1235. }
  1236. /* Additional bytes needed by arch in front of individual sections */
  1237. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1238. unsigned int section)
  1239. {
  1240. /* default implementation just returns zero */
  1241. return 0;
  1242. }
  1243. /* Update size with this section: return offset. */
  1244. long module_get_offset(struct module *mod, unsigned int *size,
  1245. Elf_Shdr *sechdr, unsigned int section)
  1246. {
  1247. long ret;
  1248. *size += arch_mod_section_prepend(mod, section);
  1249. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1250. *size = ret + sechdr->sh_size;
  1251. return ret;
  1252. }
  1253. bool module_init_layout_section(const char *sname)
  1254. {
  1255. #ifndef CONFIG_MODULE_UNLOAD
  1256. if (module_exit_section(sname))
  1257. return true;
  1258. #endif
  1259. return module_init_section(sname);
  1260. }
  1261. /*
  1262. * Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1263. * might -- code, read-only data, read-write data, small data. Tally
  1264. * sizes, and place the offsets into sh_entsize fields: high bit means it
  1265. * belongs in init.
  1266. */
  1267. static void layout_sections(struct module *mod, struct load_info *info)
  1268. {
  1269. static unsigned long const masks[][2] = {
  1270. /*
  1271. * NOTE: all executable code must be the first section
  1272. * in this array; otherwise modify the text_size
  1273. * finder in the two loops below
  1274. */
  1275. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1276. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1277. { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
  1278. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1279. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1280. };
  1281. unsigned int m, i;
  1282. for (i = 0; i < info->hdr->e_shnum; i++)
  1283. info->sechdrs[i].sh_entsize = ~0UL;
  1284. pr_debug("Core section allocation order:\n");
  1285. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1286. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1287. Elf_Shdr *s = &info->sechdrs[i];
  1288. const char *sname = info->secstrings + s->sh_name;
  1289. unsigned int *sizep;
  1290. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1291. || (s->sh_flags & masks[m][1])
  1292. || s->sh_entsize != ~0UL
  1293. || module_init_layout_section(sname))
  1294. continue;
  1295. sizep = m ? &mod->data_layout.size : &mod->core_layout.size;
  1296. s->sh_entsize = module_get_offset(mod, sizep, s, i);
  1297. pr_debug("\t%s\n", sname);
  1298. }
  1299. switch (m) {
  1300. case 0: /* executable */
  1301. mod->core_layout.size = strict_align(mod->core_layout.size);
  1302. mod->core_layout.text_size = mod->core_layout.size;
  1303. break;
  1304. case 1: /* RO: text and ro-data */
  1305. mod->data_layout.size = strict_align(mod->data_layout.size);
  1306. mod->data_layout.ro_size = mod->data_layout.size;
  1307. break;
  1308. case 2: /* RO after init */
  1309. mod->data_layout.size = strict_align(mod->data_layout.size);
  1310. mod->data_layout.ro_after_init_size = mod->data_layout.size;
  1311. break;
  1312. case 4: /* whole core */
  1313. mod->data_layout.size = strict_align(mod->data_layout.size);
  1314. break;
  1315. }
  1316. }
  1317. pr_debug("Init section allocation order:\n");
  1318. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1319. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1320. Elf_Shdr *s = &info->sechdrs[i];
  1321. const char *sname = info->secstrings + s->sh_name;
  1322. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1323. || (s->sh_flags & masks[m][1])
  1324. || s->sh_entsize != ~0UL
  1325. || !module_init_layout_section(sname))
  1326. continue;
  1327. s->sh_entsize = (module_get_offset(mod, &mod->init_layout.size, s, i)
  1328. | INIT_OFFSET_MASK);
  1329. pr_debug("\t%s\n", sname);
  1330. }
  1331. switch (m) {
  1332. case 0: /* executable */
  1333. mod->init_layout.size = strict_align(mod->init_layout.size);
  1334. mod->init_layout.text_size = mod->init_layout.size;
  1335. break;
  1336. case 1: /* RO: text and ro-data */
  1337. mod->init_layout.size = strict_align(mod->init_layout.size);
  1338. mod->init_layout.ro_size = mod->init_layout.size;
  1339. break;
  1340. case 2:
  1341. /*
  1342. * RO after init doesn't apply to init_layout (only
  1343. * core_layout), so it just takes the value of ro_size.
  1344. */
  1345. mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
  1346. break;
  1347. case 4: /* whole init */
  1348. mod->init_layout.size = strict_align(mod->init_layout.size);
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. static void set_license(struct module *mod, const char *license)
  1354. {
  1355. if (!license)
  1356. license = "unspecified";
  1357. if (!license_is_gpl_compatible(license)) {
  1358. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1359. pr_warn("%s: module license '%s' taints kernel.\n",
  1360. mod->name, license);
  1361. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  1362. LOCKDEP_NOW_UNRELIABLE);
  1363. }
  1364. }
  1365. /* Parse tag=value strings from .modinfo section */
  1366. static char *next_string(char *string, unsigned long *secsize)
  1367. {
  1368. /* Skip non-zero chars */
  1369. while (string[0]) {
  1370. string++;
  1371. if ((*secsize)-- <= 1)
  1372. return NULL;
  1373. }
  1374. /* Skip any zero padding. */
  1375. while (!string[0]) {
  1376. string++;
  1377. if ((*secsize)-- <= 1)
  1378. return NULL;
  1379. }
  1380. return string;
  1381. }
  1382. static char *get_next_modinfo(const struct load_info *info, const char *tag,
  1383. char *prev)
  1384. {
  1385. char *p;
  1386. unsigned int taglen = strlen(tag);
  1387. Elf_Shdr *infosec = &info->sechdrs[info->index.info];
  1388. unsigned long size = infosec->sh_size;
  1389. /*
  1390. * get_modinfo() calls made before rewrite_section_headers()
  1391. * must use sh_offset, as sh_addr isn't set!
  1392. */
  1393. char *modinfo = (char *)info->hdr + infosec->sh_offset;
  1394. if (prev) {
  1395. size -= prev - modinfo;
  1396. modinfo = next_string(prev, &size);
  1397. }
  1398. for (p = modinfo; p; p = next_string(p, &size)) {
  1399. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1400. return p + taglen + 1;
  1401. }
  1402. return NULL;
  1403. }
  1404. static char *get_modinfo(const struct load_info *info, const char *tag)
  1405. {
  1406. return get_next_modinfo(info, tag, NULL);
  1407. }
  1408. static void setup_modinfo(struct module *mod, struct load_info *info)
  1409. {
  1410. struct module_attribute *attr;
  1411. int i;
  1412. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1413. if (attr->setup)
  1414. attr->setup(mod, get_modinfo(info, attr->attr.name));
  1415. }
  1416. }
  1417. static void free_modinfo(struct module *mod)
  1418. {
  1419. struct module_attribute *attr;
  1420. int i;
  1421. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1422. if (attr->free)
  1423. attr->free(mod);
  1424. }
  1425. }
  1426. static void dynamic_debug_setup(struct module *mod, struct _ddebug_info *dyndbg)
  1427. {
  1428. if (!dyndbg->num_descs)
  1429. return;
  1430. ddebug_add_module(dyndbg, mod->name);
  1431. }
  1432. static void dynamic_debug_remove(struct module *mod, struct _ddebug_info *dyndbg)
  1433. {
  1434. if (dyndbg->num_descs)
  1435. ddebug_remove_module(mod->name);
  1436. }
  1437. void * __weak module_alloc(unsigned long size)
  1438. {
  1439. return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
  1440. GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
  1441. NUMA_NO_NODE, __builtin_return_address(0));
  1442. }
  1443. bool __weak module_init_section(const char *name)
  1444. {
  1445. return strstarts(name, ".init");
  1446. }
  1447. bool __weak module_exit_section(const char *name)
  1448. {
  1449. return strstarts(name, ".exit");
  1450. }
  1451. static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr)
  1452. {
  1453. #if defined(CONFIG_64BIT)
  1454. unsigned long long secend;
  1455. #else
  1456. unsigned long secend;
  1457. #endif
  1458. /*
  1459. * Check for both overflow and offset/size being
  1460. * too large.
  1461. */
  1462. secend = shdr->sh_offset + shdr->sh_size;
  1463. if (secend < shdr->sh_offset || secend > info->len)
  1464. return -ENOEXEC;
  1465. return 0;
  1466. }
  1467. /*
  1468. * Sanity checks against invalid binaries, wrong arch, weird elf version.
  1469. *
  1470. * Also do basic validity checks against section offsets and sizes, the
  1471. * section name string table, and the indices used for it (sh_name).
  1472. */
  1473. static int elf_validity_check(struct load_info *info)
  1474. {
  1475. unsigned int i;
  1476. Elf_Shdr *shdr, *strhdr;
  1477. int err;
  1478. if (info->len < sizeof(*(info->hdr))) {
  1479. pr_err("Invalid ELF header len %lu\n", info->len);
  1480. goto no_exec;
  1481. }
  1482. if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0) {
  1483. pr_err("Invalid ELF header magic: != %s\n", ELFMAG);
  1484. goto no_exec;
  1485. }
  1486. if (info->hdr->e_type != ET_REL) {
  1487. pr_err("Invalid ELF header type: %u != %u\n",
  1488. info->hdr->e_type, ET_REL);
  1489. goto no_exec;
  1490. }
  1491. if (!elf_check_arch(info->hdr)) {
  1492. pr_err("Invalid architecture in ELF header: %u\n",
  1493. info->hdr->e_machine);
  1494. goto no_exec;
  1495. }
  1496. if (info->hdr->e_shentsize != sizeof(Elf_Shdr)) {
  1497. pr_err("Invalid ELF section header size\n");
  1498. goto no_exec;
  1499. }
  1500. /*
  1501. * e_shnum is 16 bits, and sizeof(Elf_Shdr) is
  1502. * known and small. So e_shnum * sizeof(Elf_Shdr)
  1503. * will not overflow unsigned long on any platform.
  1504. */
  1505. if (info->hdr->e_shoff >= info->len
  1506. || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
  1507. info->len - info->hdr->e_shoff)) {
  1508. pr_err("Invalid ELF section header overflow\n");
  1509. goto no_exec;
  1510. }
  1511. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  1512. /*
  1513. * Verify if the section name table index is valid.
  1514. */
  1515. if (info->hdr->e_shstrndx == SHN_UNDEF
  1516. || info->hdr->e_shstrndx >= info->hdr->e_shnum) {
  1517. pr_err("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n",
  1518. info->hdr->e_shstrndx, info->hdr->e_shstrndx,
  1519. info->hdr->e_shnum);
  1520. goto no_exec;
  1521. }
  1522. strhdr = &info->sechdrs[info->hdr->e_shstrndx];
  1523. err = validate_section_offset(info, strhdr);
  1524. if (err < 0) {
  1525. pr_err("Invalid ELF section hdr(type %u)\n", strhdr->sh_type);
  1526. return err;
  1527. }
  1528. /*
  1529. * The section name table must be NUL-terminated, as required
  1530. * by the spec. This makes strcmp and pr_* calls that access
  1531. * strings in the section safe.
  1532. */
  1533. info->secstrings = (void *)info->hdr + strhdr->sh_offset;
  1534. if (strhdr->sh_size == 0) {
  1535. pr_err("empty section name table\n");
  1536. goto no_exec;
  1537. }
  1538. if (info->secstrings[strhdr->sh_size - 1] != '\0') {
  1539. pr_err("ELF Spec violation: section name table isn't null terminated\n");
  1540. goto no_exec;
  1541. }
  1542. /*
  1543. * The code assumes that section 0 has a length of zero and
  1544. * an addr of zero, so check for it.
  1545. */
  1546. if (info->sechdrs[0].sh_type != SHT_NULL
  1547. || info->sechdrs[0].sh_size != 0
  1548. || info->sechdrs[0].sh_addr != 0) {
  1549. pr_err("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n",
  1550. info->sechdrs[0].sh_type);
  1551. goto no_exec;
  1552. }
  1553. for (i = 1; i < info->hdr->e_shnum; i++) {
  1554. shdr = &info->sechdrs[i];
  1555. switch (shdr->sh_type) {
  1556. case SHT_NULL:
  1557. case SHT_NOBITS:
  1558. continue;
  1559. case SHT_SYMTAB:
  1560. if (shdr->sh_link == SHN_UNDEF
  1561. || shdr->sh_link >= info->hdr->e_shnum) {
  1562. pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n",
  1563. shdr->sh_link, shdr->sh_link,
  1564. info->hdr->e_shnum);
  1565. goto no_exec;
  1566. }
  1567. fallthrough;
  1568. default:
  1569. err = validate_section_offset(info, shdr);
  1570. if (err < 0) {
  1571. pr_err("Invalid ELF section in module (section %u type %u)\n",
  1572. i, shdr->sh_type);
  1573. return err;
  1574. }
  1575. if (shdr->sh_flags & SHF_ALLOC) {
  1576. if (shdr->sh_name >= strhdr->sh_size) {
  1577. pr_err("Invalid ELF section name in module (section %u type %u)\n",
  1578. i, shdr->sh_type);
  1579. return -ENOEXEC;
  1580. }
  1581. }
  1582. break;
  1583. }
  1584. }
  1585. return 0;
  1586. no_exec:
  1587. return -ENOEXEC;
  1588. }
  1589. #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
  1590. static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
  1591. {
  1592. do {
  1593. unsigned long n = min(len, COPY_CHUNK_SIZE);
  1594. if (copy_from_user(dst, usrc, n) != 0)
  1595. return -EFAULT;
  1596. cond_resched();
  1597. dst += n;
  1598. usrc += n;
  1599. len -= n;
  1600. } while (len);
  1601. return 0;
  1602. }
  1603. static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
  1604. {
  1605. if (!get_modinfo(info, "livepatch"))
  1606. /* Nothing more to do */
  1607. return 0;
  1608. if (set_livepatch_module(mod)) {
  1609. add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
  1610. pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n",
  1611. mod->name);
  1612. return 0;
  1613. }
  1614. pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
  1615. mod->name);
  1616. return -ENOEXEC;
  1617. }
  1618. static void check_modinfo_retpoline(struct module *mod, struct load_info *info)
  1619. {
  1620. if (retpoline_module_ok(get_modinfo(info, "retpoline")))
  1621. return;
  1622. pr_warn("%s: loading module not compiled with retpoline compiler.\n",
  1623. mod->name);
  1624. }
  1625. /* Sets info->hdr and info->len. */
  1626. static int copy_module_from_user(const void __user *umod, unsigned long len,
  1627. struct load_info *info)
  1628. {
  1629. int err;
  1630. info->len = len;
  1631. if (info->len < sizeof(*(info->hdr)))
  1632. return -ENOEXEC;
  1633. err = security_kernel_load_data(LOADING_MODULE, true);
  1634. if (err)
  1635. return err;
  1636. /* Suck in entire file: we'll want most of it. */
  1637. info->hdr = __vmalloc(info->len, GFP_KERNEL | __GFP_NOWARN);
  1638. if (!info->hdr)
  1639. return -ENOMEM;
  1640. if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
  1641. err = -EFAULT;
  1642. goto out;
  1643. }
  1644. err = security_kernel_post_load_data((char *)info->hdr, info->len,
  1645. LOADING_MODULE, "init_module");
  1646. out:
  1647. if (err)
  1648. vfree(info->hdr);
  1649. return err;
  1650. }
  1651. static void free_copy(struct load_info *info, int flags)
  1652. {
  1653. if (flags & MODULE_INIT_COMPRESSED_FILE)
  1654. module_decompress_cleanup(info);
  1655. else
  1656. vfree(info->hdr);
  1657. }
  1658. static int rewrite_section_headers(struct load_info *info, int flags)
  1659. {
  1660. unsigned int i;
  1661. /* This should always be true, but let's be sure. */
  1662. info->sechdrs[0].sh_addr = 0;
  1663. for (i = 1; i < info->hdr->e_shnum; i++) {
  1664. Elf_Shdr *shdr = &info->sechdrs[i];
  1665. /*
  1666. * Mark all sections sh_addr with their address in the
  1667. * temporary image.
  1668. */
  1669. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  1670. }
  1671. /* Track but don't keep modinfo and version sections. */
  1672. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1673. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1674. return 0;
  1675. }
  1676. /*
  1677. * Set up our basic convenience variables (pointers to section headers,
  1678. * search for module section index etc), and do some basic section
  1679. * verification.
  1680. *
  1681. * Set info->mod to the temporary copy of the module in info->hdr. The final one
  1682. * will be allocated in move_module().
  1683. */
  1684. static int setup_load_info(struct load_info *info, int flags)
  1685. {
  1686. unsigned int i;
  1687. /* Try to find a name early so we can log errors with a module name */
  1688. info->index.info = find_sec(info, ".modinfo");
  1689. if (info->index.info)
  1690. info->name = get_modinfo(info, "name");
  1691. /* Find internal symbols and strings. */
  1692. for (i = 1; i < info->hdr->e_shnum; i++) {
  1693. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  1694. info->index.sym = i;
  1695. info->index.str = info->sechdrs[i].sh_link;
  1696. info->strtab = (char *)info->hdr
  1697. + info->sechdrs[info->index.str].sh_offset;
  1698. break;
  1699. }
  1700. }
  1701. if (info->index.sym == 0) {
  1702. pr_warn("%s: module has no symbols (stripped?)\n",
  1703. info->name ?: "(missing .modinfo section or name field)");
  1704. return -ENOEXEC;
  1705. }
  1706. info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
  1707. if (!info->index.mod) {
  1708. pr_warn("%s: No module found in object\n",
  1709. info->name ?: "(missing .modinfo section or name field)");
  1710. return -ENOEXEC;
  1711. }
  1712. /* This is temporary: point mod into copy of data. */
  1713. info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
  1714. /*
  1715. * If we didn't load the .modinfo 'name' field earlier, fall back to
  1716. * on-disk struct mod 'name' field.
  1717. */
  1718. if (!info->name)
  1719. info->name = info->mod->name;
  1720. if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
  1721. info->index.vers = 0; /* Pretend no __versions section! */
  1722. else
  1723. info->index.vers = find_sec(info, "__versions");
  1724. info->index.pcpu = find_pcpusec(info);
  1725. return 0;
  1726. }
  1727. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  1728. {
  1729. const char *modmagic = get_modinfo(info, "vermagic");
  1730. int err;
  1731. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  1732. modmagic = NULL;
  1733. /* This is allowed: modprobe --force will invalidate it. */
  1734. if (!modmagic) {
  1735. err = try_to_force_load(mod, "bad vermagic");
  1736. if (err)
  1737. return err;
  1738. } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
  1739. pr_err("%s: version magic '%s' should be '%s'\n",
  1740. info->name, modmagic, vermagic);
  1741. return -ENOEXEC;
  1742. }
  1743. if (!get_modinfo(info, "intree")) {
  1744. if (!test_taint(TAINT_OOT_MODULE))
  1745. pr_warn("%s: loading out-of-tree module taints kernel.\n",
  1746. mod->name);
  1747. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  1748. }
  1749. check_modinfo_retpoline(mod, info);
  1750. if (get_modinfo(info, "staging")) {
  1751. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  1752. pr_warn("%s: module is from the staging directory, the quality "
  1753. "is unknown, you have been warned.\n", mod->name);
  1754. }
  1755. err = check_modinfo_livepatch(mod, info);
  1756. if (err)
  1757. return err;
  1758. /* Set up license info based on the info section */
  1759. set_license(mod, get_modinfo(info, "license"));
  1760. if (get_modinfo(info, "test")) {
  1761. if (!test_taint(TAINT_TEST))
  1762. pr_warn("%s: loading test module taints kernel.\n",
  1763. mod->name);
  1764. add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK);
  1765. }
  1766. return 0;
  1767. }
  1768. static int find_module_sections(struct module *mod, struct load_info *info)
  1769. {
  1770. mod->kp = section_objs(info, "__param",
  1771. sizeof(*mod->kp), &mod->num_kp);
  1772. mod->syms = section_objs(info, "__ksymtab",
  1773. sizeof(*mod->syms), &mod->num_syms);
  1774. mod->crcs = section_addr(info, "__kcrctab");
  1775. mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
  1776. sizeof(*mod->gpl_syms),
  1777. &mod->num_gpl_syms);
  1778. mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
  1779. #ifdef CONFIG_CONSTRUCTORS
  1780. mod->ctors = section_objs(info, ".ctors",
  1781. sizeof(*mod->ctors), &mod->num_ctors);
  1782. if (!mod->ctors)
  1783. mod->ctors = section_objs(info, ".init_array",
  1784. sizeof(*mod->ctors), &mod->num_ctors);
  1785. else if (find_sec(info, ".init_array")) {
  1786. /*
  1787. * This shouldn't happen with same compiler and binutils
  1788. * building all parts of the module.
  1789. */
  1790. pr_warn("%s: has both .ctors and .init_array.\n",
  1791. mod->name);
  1792. return -EINVAL;
  1793. }
  1794. #endif
  1795. mod->noinstr_text_start = section_objs(info, ".noinstr.text", 1,
  1796. &mod->noinstr_text_size);
  1797. #ifdef CONFIG_TRACEPOINTS
  1798. mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
  1799. sizeof(*mod->tracepoints_ptrs),
  1800. &mod->num_tracepoints);
  1801. #endif
  1802. #ifdef CONFIG_TREE_SRCU
  1803. mod->srcu_struct_ptrs = section_objs(info, "___srcu_struct_ptrs",
  1804. sizeof(*mod->srcu_struct_ptrs),
  1805. &mod->num_srcu_structs);
  1806. #endif
  1807. #ifdef CONFIG_BPF_EVENTS
  1808. mod->bpf_raw_events = section_objs(info, "__bpf_raw_tp_map",
  1809. sizeof(*mod->bpf_raw_events),
  1810. &mod->num_bpf_raw_events);
  1811. #endif
  1812. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  1813. mod->btf_data = any_section_objs(info, ".BTF", 1, &mod->btf_data_size);
  1814. #endif
  1815. #ifdef CONFIG_JUMP_LABEL
  1816. mod->jump_entries = section_objs(info, "__jump_table",
  1817. sizeof(*mod->jump_entries),
  1818. &mod->num_jump_entries);
  1819. #endif
  1820. #ifdef CONFIG_EVENT_TRACING
  1821. mod->trace_events = section_objs(info, "_ftrace_events",
  1822. sizeof(*mod->trace_events),
  1823. &mod->num_trace_events);
  1824. mod->trace_evals = section_objs(info, "_ftrace_eval_map",
  1825. sizeof(*mod->trace_evals),
  1826. &mod->num_trace_evals);
  1827. #endif
  1828. #ifdef CONFIG_TRACING
  1829. mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
  1830. sizeof(*mod->trace_bprintk_fmt_start),
  1831. &mod->num_trace_bprintk_fmt);
  1832. #endif
  1833. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  1834. /* sechdrs[0].sh_size is always zero */
  1835. mod->ftrace_callsites = section_objs(info, FTRACE_CALLSITE_SECTION,
  1836. sizeof(*mod->ftrace_callsites),
  1837. &mod->num_ftrace_callsites);
  1838. #endif
  1839. #ifdef CONFIG_FUNCTION_ERROR_INJECTION
  1840. mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
  1841. sizeof(*mod->ei_funcs),
  1842. &mod->num_ei_funcs);
  1843. #endif
  1844. #ifdef CONFIG_KPROBES
  1845. mod->kprobes_text_start = section_objs(info, ".kprobes.text", 1,
  1846. &mod->kprobes_text_size);
  1847. mod->kprobe_blacklist = section_objs(info, "_kprobe_blacklist",
  1848. sizeof(unsigned long),
  1849. &mod->num_kprobe_blacklist);
  1850. #endif
  1851. #ifdef CONFIG_PRINTK_INDEX
  1852. mod->printk_index_start = section_objs(info, ".printk_index",
  1853. sizeof(*mod->printk_index_start),
  1854. &mod->printk_index_size);
  1855. #endif
  1856. #ifdef CONFIG_HAVE_STATIC_CALL_INLINE
  1857. mod->static_call_sites = section_objs(info, ".static_call_sites",
  1858. sizeof(*mod->static_call_sites),
  1859. &mod->num_static_call_sites);
  1860. #endif
  1861. #if IS_ENABLED(CONFIG_KUNIT)
  1862. mod->kunit_suites = section_objs(info, ".kunit_test_suites",
  1863. sizeof(*mod->kunit_suites),
  1864. &mod->num_kunit_suites);
  1865. #endif
  1866. mod->extable = section_objs(info, "__ex_table",
  1867. sizeof(*mod->extable), &mod->num_exentries);
  1868. if (section_addr(info, "__obsparm"))
  1869. pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
  1870. info->dyndbg.descs = section_objs(info, "__dyndbg",
  1871. sizeof(*info->dyndbg.descs), &info->dyndbg.num_descs);
  1872. info->dyndbg.classes = section_objs(info, "__dyndbg_classes",
  1873. sizeof(*info->dyndbg.classes), &info->dyndbg.num_classes);
  1874. return 0;
  1875. }
  1876. static int move_module(struct module *mod, struct load_info *info)
  1877. {
  1878. int i;
  1879. void *ptr;
  1880. /* Do the allocs. */
  1881. #ifdef CONFIG_RKP
  1882. ptr = module_alloc_by_rkp(mod->core_layout.size, mod->core_layout.text_size);
  1883. #else
  1884. ptr = module_alloc(mod->core_layout.size);
  1885. #endif
  1886. /*
  1887. * The pointer to this block is stored in the module structure
  1888. * which is inside the block. Just mark it as not being a
  1889. * leak.
  1890. */
  1891. kmemleak_not_leak(ptr);
  1892. if (!ptr)
  1893. return -ENOMEM;
  1894. memset(ptr, 0, mod->core_layout.size);
  1895. mod->core_layout.base = ptr;
  1896. if (mod->init_layout.size) {
  1897. ptr = module_alloc(mod->init_layout.size);
  1898. /*
  1899. * The pointer to this block is stored in the module structure
  1900. * which is inside the block. This block doesn't need to be
  1901. * scanned as it contains data and code that will be freed
  1902. * after the module is initialized.
  1903. */
  1904. kmemleak_ignore(ptr);
  1905. if (!ptr) {
  1906. module_memfree(mod->core_layout.base);
  1907. return -ENOMEM;
  1908. }
  1909. memset(ptr, 0, mod->init_layout.size);
  1910. mod->init_layout.base = ptr;
  1911. } else
  1912. mod->init_layout.base = NULL;
  1913. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  1914. /* Do the allocs. */
  1915. ptr = vzalloc(mod->data_layout.size);
  1916. /*
  1917. * The pointer to this block is stored in the module structure
  1918. * which is inside the block. Just mark it as not being a
  1919. * leak.
  1920. */
  1921. kmemleak_not_leak(ptr);
  1922. if (!ptr) {
  1923. module_memfree(mod->core_layout.base);
  1924. module_memfree(mod->init_layout.base);
  1925. return -ENOMEM;
  1926. }
  1927. mod->data_layout.base = ptr;
  1928. #endif
  1929. /* Transfer each section which specifies SHF_ALLOC */
  1930. pr_debug("final section addresses:\n");
  1931. for (i = 0; i < info->hdr->e_shnum; i++) {
  1932. void *dest;
  1933. Elf_Shdr *shdr = &info->sechdrs[i];
  1934. if (!(shdr->sh_flags & SHF_ALLOC))
  1935. continue;
  1936. if (shdr->sh_entsize & INIT_OFFSET_MASK)
  1937. dest = mod->init_layout.base
  1938. + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
  1939. else if (!(shdr->sh_flags & SHF_EXECINSTR))
  1940. dest = mod->data_layout.base + shdr->sh_entsize;
  1941. else
  1942. dest = mod->core_layout.base + shdr->sh_entsize;
  1943. if (shdr->sh_type != SHT_NOBITS)
  1944. memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
  1945. /* Update sh_addr to point to copy in image. */
  1946. shdr->sh_addr = (unsigned long)dest;
  1947. pr_debug("\t0x%lx %s\n",
  1948. (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
  1949. }
  1950. return 0;
  1951. }
  1952. static int check_module_license_and_versions(struct module *mod)
  1953. {
  1954. int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
  1955. /*
  1956. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1957. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1958. * using GPL-only symbols it needs.
  1959. */
  1960. if (strcmp(mod->name, "ndiswrapper") == 0)
  1961. add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
  1962. /* driverloader was caught wrongly pretending to be under GPL */
  1963. if (strcmp(mod->name, "driverloader") == 0)
  1964. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  1965. LOCKDEP_NOW_UNRELIABLE);
  1966. /* lve claims to be GPL but upstream won't provide source */
  1967. if (strcmp(mod->name, "lve") == 0)
  1968. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  1969. LOCKDEP_NOW_UNRELIABLE);
  1970. if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
  1971. pr_warn("%s: module license taints kernel.\n", mod->name);
  1972. #ifdef CONFIG_MODVERSIONS
  1973. if ((mod->num_syms && !mod->crcs) ||
  1974. (mod->num_gpl_syms && !mod->gpl_crcs)) {
  1975. return try_to_force_load(mod,
  1976. "no versions for exported symbols");
  1977. }
  1978. #endif
  1979. return 0;
  1980. }
  1981. static void flush_module_icache(const struct module *mod)
  1982. {
  1983. /*
  1984. * Flush the instruction cache, since we've played with text.
  1985. * Do it before processing of module parameters, so the module
  1986. * can provide parameter accessor functions of its own.
  1987. */
  1988. if (mod->init_layout.base)
  1989. flush_icache_range((unsigned long)mod->init_layout.base,
  1990. (unsigned long)mod->init_layout.base
  1991. + mod->init_layout.size);
  1992. flush_icache_range((unsigned long)mod->core_layout.base,
  1993. (unsigned long)mod->core_layout.base + mod->core_layout.size);
  1994. }
  1995. int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
  1996. Elf_Shdr *sechdrs,
  1997. char *secstrings,
  1998. struct module *mod)
  1999. {
  2000. return 0;
  2001. }
  2002. /* module_blacklist is a comma-separated list of module names */
  2003. static char *module_blacklist;
  2004. static bool blacklisted(const char *module_name)
  2005. {
  2006. const char *p;
  2007. size_t len;
  2008. if (!module_blacklist)
  2009. return false;
  2010. for (p = module_blacklist; *p; p += len) {
  2011. len = strcspn(p, ",");
  2012. if (strlen(module_name) == len && !memcmp(module_name, p, len))
  2013. return true;
  2014. if (p[len] == ',')
  2015. len++;
  2016. }
  2017. return false;
  2018. }
  2019. core_param(module_blacklist, module_blacklist, charp, 0400);
  2020. static struct module *layout_and_allocate(struct load_info *info, int flags)
  2021. {
  2022. struct module *mod;
  2023. unsigned int ndx;
  2024. int err;
  2025. err = check_modinfo(info->mod, info, flags);
  2026. if (err)
  2027. return ERR_PTR(err);
  2028. /* Allow arches to frob section contents and sizes. */
  2029. err = module_frob_arch_sections(info->hdr, info->sechdrs,
  2030. info->secstrings, info->mod);
  2031. if (err < 0)
  2032. return ERR_PTR(err);
  2033. err = module_enforce_rwx_sections(info->hdr, info->sechdrs,
  2034. info->secstrings, info->mod);
  2035. if (err < 0)
  2036. return ERR_PTR(err);
  2037. /* We will do a special allocation for per-cpu sections later. */
  2038. info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2039. /*
  2040. * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
  2041. * layout_sections() can put it in the right place.
  2042. * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
  2043. */
  2044. ndx = find_sec(info, ".data..ro_after_init");
  2045. if (ndx)
  2046. info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
  2047. /*
  2048. * Mark the __jump_table section as ro_after_init as well: these data
  2049. * structures are never modified, with the exception of entries that
  2050. * refer to code in the __init section, which are annotated as such
  2051. * at module load time.
  2052. */
  2053. ndx = find_sec(info, "__jump_table");
  2054. if (ndx)
  2055. info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
  2056. /*
  2057. * Determine total sizes, and put offsets in sh_entsize. For now
  2058. * this is done generically; there doesn't appear to be any
  2059. * special cases for the architectures.
  2060. */
  2061. layout_sections(info->mod, info);
  2062. layout_symtab(info->mod, info);
  2063. /* Allocate and move to the final place */
  2064. err = move_module(info->mod, info);
  2065. if (err)
  2066. return ERR_PTR(err);
  2067. /* Module has been copied to its final place now: return it. */
  2068. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2069. kmemleak_load_module(mod, info);
  2070. return mod;
  2071. }
  2072. /* mod is no longer valid after this! */
  2073. static void module_deallocate(struct module *mod, struct load_info *info)
  2074. {
  2075. percpu_modfree(mod);
  2076. module_arch_freeing_init(mod);
  2077. trace_android_rvh_set_module_init_rw_nx(mod);
  2078. module_memfree(mod->init_layout.base);
  2079. trace_android_rvh_set_module_core_rw_nx(mod);
  2080. module_memfree(mod->core_layout.base);
  2081. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  2082. vfree(mod->data_layout.base);
  2083. #endif
  2084. }
  2085. int __weak module_finalize(const Elf_Ehdr *hdr,
  2086. const Elf_Shdr *sechdrs,
  2087. struct module *me)
  2088. {
  2089. return 0;
  2090. }
  2091. static int post_relocation(struct module *mod, const struct load_info *info)
  2092. {
  2093. /* Sort exception table now relocations are done. */
  2094. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2095. /* Copy relocated percpu area over. */
  2096. percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
  2097. info->sechdrs[info->index.pcpu].sh_size);
  2098. /* Setup kallsyms-specific fields. */
  2099. add_kallsyms(mod, info);
  2100. /* Arch-specific module finalizing. */
  2101. return module_finalize(info->hdr, info->sechdrs, mod);
  2102. }
  2103. /* Is this module of this name done loading? No locks held. */
  2104. static bool finished_loading(const char *name)
  2105. {
  2106. struct module *mod;
  2107. bool ret;
  2108. /*
  2109. * The module_mutex should not be a heavily contended lock;
  2110. * if we get the occasional sleep here, we'll go an extra iteration
  2111. * in the wait_event_interruptible(), which is harmless.
  2112. */
  2113. sched_annotate_sleep();
  2114. mutex_lock(&module_mutex);
  2115. mod = find_module_all(name, strlen(name), true);
  2116. ret = !mod || mod->state == MODULE_STATE_LIVE
  2117. || mod->state == MODULE_STATE_GOING;
  2118. mutex_unlock(&module_mutex);
  2119. return ret;
  2120. }
  2121. /* Call module constructors. */
  2122. static void do_mod_ctors(struct module *mod)
  2123. {
  2124. #ifdef CONFIG_CONSTRUCTORS
  2125. unsigned long i;
  2126. for (i = 0; i < mod->num_ctors; i++)
  2127. mod->ctors[i]();
  2128. #endif
  2129. }
  2130. /* For freeing module_init on success, in case kallsyms traversing */
  2131. struct mod_initfree {
  2132. struct llist_node node;
  2133. void *module_init;
  2134. };
  2135. static void do_free_init(struct work_struct *w)
  2136. {
  2137. struct llist_node *pos, *n, *list;
  2138. struct mod_initfree *initfree;
  2139. list = llist_del_all(&init_free_list);
  2140. synchronize_rcu();
  2141. llist_for_each_safe(pos, n, list) {
  2142. initfree = container_of(pos, struct mod_initfree, node);
  2143. module_memfree(initfree->module_init);
  2144. kfree(initfree);
  2145. }
  2146. }
  2147. #undef MODULE_PARAM_PREFIX
  2148. #define MODULE_PARAM_PREFIX "module."
  2149. /* Default value for module->async_probe_requested */
  2150. static bool async_probe;
  2151. module_param(async_probe, bool, 0644);
  2152. /*
  2153. * This is where the real work happens.
  2154. *
  2155. * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
  2156. * helper command 'lx-symbols'.
  2157. */
  2158. static noinline int do_init_module(struct module *mod)
  2159. {
  2160. int ret = 0;
  2161. struct mod_initfree *freeinit;
  2162. #ifdef CONFIG_RKP
  2163. struct module_info rkp_mod_info;
  2164. #endif
  2165. freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
  2166. if (!freeinit) {
  2167. ret = -ENOMEM;
  2168. goto fail;
  2169. }
  2170. freeinit->module_init = mod->init_layout.base;
  2171. do_mod_ctors(mod);
  2172. /* Start the module */
  2173. if (mod->init != NULL)
  2174. ret = do_one_initcall(mod->init);
  2175. if (ret < 0) {
  2176. goto fail_free_freeinit;
  2177. }
  2178. if (ret > 0) {
  2179. pr_warn("%s: '%s'->init suspiciously returned %d, it should "
  2180. "follow 0/-E convention\n"
  2181. "%s: loading module anyway...\n",
  2182. __func__, mod->name, ret, __func__);
  2183. dump_stack();
  2184. }
  2185. /* Now it's a first class citizen! */
  2186. mod->state = MODULE_STATE_LIVE;
  2187. blocking_notifier_call_chain(&module_notify_list,
  2188. MODULE_STATE_LIVE, mod);
  2189. /* Delay uevent until module has finished its init routine */
  2190. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  2191. /*
  2192. * We need to finish all async code before the module init sequence
  2193. * is done. This has potential to deadlock if synchronous module
  2194. * loading is requested from async (which is not allowed!).
  2195. *
  2196. * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous
  2197. * request_module() from async workers") for more details.
  2198. */
  2199. if (!mod->async_probe_requested)
  2200. async_synchronize_full();
  2201. ftrace_free_mem(mod, mod->init_layout.base, mod->init_layout.base +
  2202. mod->init_layout.size);
  2203. mutex_lock(&module_mutex);
  2204. /* Drop initial reference. */
  2205. module_put(mod);
  2206. trim_init_extable(mod);
  2207. #ifdef CONFIG_KALLSYMS
  2208. /* Switch to core kallsyms now init is done: kallsyms may be walking! */
  2209. rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
  2210. #endif
  2211. module_enable_ro(mod, true);
  2212. trace_android_rvh_set_module_permit_after_init(mod);
  2213. mod_tree_remove_init(mod);
  2214. module_arch_freeing_init(mod);
  2215. trace_android_rvh_set_module_init_rw_nx(mod);
  2216. #ifdef CONFIG_RKP
  2217. rkp_mod_info.base_va = 0;
  2218. rkp_mod_info.vm_size = 0;
  2219. rkp_mod_info.core_base_va = 0;
  2220. rkp_mod_info.core_text_size = 0;
  2221. rkp_mod_info.core_ro_size = 0;
  2222. rkp_mod_info.init_base_va = (u64)mod->init_layout.base;
  2223. rkp_mod_info.init_text_size = (u64)mod->init_layout.text_size;
  2224. uh_call(UH_APP_RKP, RKP_MODULE_LOAD, RKP_MODULE_PXN_SET, (u64)&rkp_mod_info, 0, 0);
  2225. #endif
  2226. mod->init_layout.base = NULL;
  2227. mod->init_layout.size = 0;
  2228. mod->init_layout.ro_size = 0;
  2229. mod->init_layout.ro_after_init_size = 0;
  2230. mod->init_layout.text_size = 0;
  2231. #ifdef CONFIG_DEBUG_INFO_BTF_MODULES
  2232. /* .BTF is not SHF_ALLOC and will get removed, so sanitize pointer */
  2233. mod->btf_data = NULL;
  2234. #endif
  2235. /*
  2236. * We want to free module_init, but be aware that kallsyms may be
  2237. * walking this with preempt disabled. In all the failure paths, we
  2238. * call synchronize_rcu(), but we don't want to slow down the success
  2239. * path. module_memfree() cannot be called in an interrupt, so do the
  2240. * work and call synchronize_rcu() in a work queue.
  2241. *
  2242. * Note that module_alloc() on most architectures creates W+X page
  2243. * mappings which won't be cleaned up until do_free_init() runs. Any
  2244. * code such as mark_rodata_ro() which depends on those mappings to
  2245. * be cleaned up needs to sync with the queued work - ie
  2246. * rcu_barrier()
  2247. */
  2248. if (llist_add(&freeinit->node, &init_free_list))
  2249. schedule_work(&init_free_wq);
  2250. mutex_unlock(&module_mutex);
  2251. wake_up_all(&module_wq);
  2252. return 0;
  2253. fail_free_freeinit:
  2254. kfree(freeinit);
  2255. fail:
  2256. /* Try to protect us from buggy refcounters. */
  2257. mod->state = MODULE_STATE_GOING;
  2258. synchronize_rcu();
  2259. module_put(mod);
  2260. blocking_notifier_call_chain(&module_notify_list,
  2261. MODULE_STATE_GOING, mod);
  2262. klp_module_going(mod);
  2263. ftrace_release_mod(mod);
  2264. #ifdef CONFIG_RKP
  2265. rkp_mod_info.base_va = 0;
  2266. rkp_mod_info.vm_size = 0;
  2267. rkp_mod_info.core_base_va = (u64)mod->core_layout.base;
  2268. rkp_mod_info.core_text_size = (u64)mod->core_layout.text_size;
  2269. rkp_mod_info.core_ro_size = (u64)mod->core_layout.ro_size;
  2270. rkp_mod_info.init_base_va = (u64)mod->init_layout.base;
  2271. rkp_mod_info.init_text_size = (u64)mod->init_layout.text_size;
  2272. uh_call(UH_APP_RKP, RKP_MODULE_LOAD, RKP_MODULE_PXN_SET, (u64)&rkp_mod_info, 0, 0);
  2273. #endif
  2274. free_module(mod);
  2275. wake_up_all(&module_wq);
  2276. return ret;
  2277. }
  2278. static int may_init_module(void)
  2279. {
  2280. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2281. return -EPERM;
  2282. return 0;
  2283. }
  2284. /*
  2285. * We try to place it in the list now to make sure it's unique before
  2286. * we dedicate too many resources. In particular, temporary percpu
  2287. * memory exhaustion.
  2288. */
  2289. static int add_unformed_module(struct module *mod)
  2290. {
  2291. int err;
  2292. struct module *old;
  2293. mod->state = MODULE_STATE_UNFORMED;
  2294. mutex_lock(&module_mutex);
  2295. old = find_module_all(mod->name, strlen(mod->name), true);
  2296. if (old != NULL) {
  2297. if (old->state == MODULE_STATE_COMING
  2298. || old->state == MODULE_STATE_UNFORMED) {
  2299. /* Wait in case it fails to load. */
  2300. mutex_unlock(&module_mutex);
  2301. err = wait_event_interruptible(module_wq,
  2302. finished_loading(mod->name));
  2303. if (err)
  2304. goto out_unlocked;
  2305. /* The module might have gone in the meantime. */
  2306. mutex_lock(&module_mutex);
  2307. old = find_module_all(mod->name, strlen(mod->name),
  2308. true);
  2309. }
  2310. /*
  2311. * We are here only when the same module was being loaded. Do
  2312. * not try to load it again right now. It prevents long delays
  2313. * caused by serialized module load failures. It might happen
  2314. * when more devices of the same type trigger load of
  2315. * a particular module.
  2316. */
  2317. if (old && old->state == MODULE_STATE_LIVE)
  2318. err = -EEXIST;
  2319. else
  2320. err = -EBUSY;
  2321. goto out;
  2322. }
  2323. mod_update_bounds(mod);
  2324. list_add_rcu(&mod->list, &modules);
  2325. mod_tree_insert(mod);
  2326. err = 0;
  2327. out:
  2328. mutex_unlock(&module_mutex);
  2329. out_unlocked:
  2330. return err;
  2331. }
  2332. static int complete_formation(struct module *mod, struct load_info *info)
  2333. {
  2334. int err;
  2335. #ifdef CONFIG_RKP
  2336. struct module_info rkp_mod_info;
  2337. #endif
  2338. mutex_lock(&module_mutex);
  2339. /* Find duplicate symbols (must be called under lock). */
  2340. err = verify_exported_symbols(mod);
  2341. if (err < 0)
  2342. goto out;
  2343. /* These rely on module_mutex for list integrity. */
  2344. module_bug_finalize(info->hdr, info->sechdrs, mod);
  2345. module_cfi_finalize(info->hdr, info->sechdrs, mod);
  2346. if (module_check_misalignment(mod))
  2347. goto out_misaligned;
  2348. module_enable_ro(mod, false);
  2349. module_enable_nx(mod);
  2350. module_enable_x(mod);
  2351. trace_android_rvh_set_module_permit_before_init(mod);
  2352. /*
  2353. * Mark state as coming so strong_try_module_get() ignores us,
  2354. * but kallsyms etc. can see us.
  2355. */
  2356. mod->state = MODULE_STATE_COMING;
  2357. #ifdef CONFIG_RKP
  2358. rkp_mod_info.base_va = 0;
  2359. rkp_mod_info.vm_size = 0;
  2360. rkp_mod_info.core_base_va = (u64)mod->core_layout.base;
  2361. rkp_mod_info.core_text_size = (u64)mod->core_layout.text_size;
  2362. rkp_mod_info.core_ro_size = (u64)mod->core_layout.ro_size;
  2363. rkp_mod_info.init_base_va = (u64)mod->init_layout.base;
  2364. rkp_mod_info.init_text_size = (u64)mod->init_layout.text_size;
  2365. uh_call(UH_APP_RKP, RKP_MODULE_LOAD, RKP_MODULE_PXN_CLEAR, (u64)&rkp_mod_info, 0, 0);
  2366. #endif
  2367. mutex_unlock(&module_mutex);
  2368. return 0;
  2369. out_misaligned:
  2370. err = -EINVAL;
  2371. out:
  2372. mutex_unlock(&module_mutex);
  2373. return err;
  2374. }
  2375. static int prepare_coming_module(struct module *mod)
  2376. {
  2377. int err;
  2378. ftrace_module_enable(mod);
  2379. err = klp_module_coming(mod);
  2380. if (err)
  2381. return err;
  2382. err = blocking_notifier_call_chain_robust(&module_notify_list,
  2383. MODULE_STATE_COMING, MODULE_STATE_GOING, mod);
  2384. err = notifier_to_errno(err);
  2385. if (err)
  2386. klp_module_going(mod);
  2387. return err;
  2388. }
  2389. static int unknown_module_param_cb(char *param, char *val, const char *modname,
  2390. void *arg)
  2391. {
  2392. struct module *mod = arg;
  2393. int ret;
  2394. if (strcmp(param, "async_probe") == 0) {
  2395. if (strtobool(val, &mod->async_probe_requested))
  2396. mod->async_probe_requested = true;
  2397. return 0;
  2398. }
  2399. /* Check for magic 'dyndbg' arg */
  2400. ret = ddebug_dyndbg_module_param_cb(param, val, modname);
  2401. if (ret != 0)
  2402. pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
  2403. return 0;
  2404. }
  2405. /*
  2406. * Allocate and load the module: note that size of section 0 is always
  2407. * zero, and we rely on this for optional sections.
  2408. */
  2409. static int load_module(struct load_info *info, const char __user *uargs,
  2410. int flags)
  2411. {
  2412. struct module *mod;
  2413. long err = 0;
  2414. char *after_dashes;
  2415. #ifdef CONFIG_RKP
  2416. struct module_info rkp_mod_info;
  2417. #endif
  2418. /*
  2419. * Do the signature check (if any) first. All that
  2420. * the signature check needs is info->len, it does
  2421. * not need any of the section info. That can be
  2422. * set up later. This will minimize the chances
  2423. * of a corrupt module causing problems before
  2424. * we even get to the signature check.
  2425. *
  2426. * The check will also adjust info->len by stripping
  2427. * off the sig length at the end of the module, making
  2428. * checks against info->len more correct.
  2429. */
  2430. err = module_sig_check(info, flags);
  2431. if (err)
  2432. goto free_copy;
  2433. /*
  2434. * Do basic sanity checks against the ELF header and
  2435. * sections.
  2436. */
  2437. err = elf_validity_check(info);
  2438. if (err)
  2439. goto free_copy;
  2440. /*
  2441. * Everything checks out, so set up the section info
  2442. * in the info structure.
  2443. */
  2444. err = setup_load_info(info, flags);
  2445. if (err)
  2446. goto free_copy;
  2447. /*
  2448. * Now that we know we have the correct module name, check
  2449. * if it's blacklisted.
  2450. */
  2451. if (blacklisted(info->name)) {
  2452. err = -EPERM;
  2453. pr_err("Module %s is blacklisted\n", info->name);
  2454. goto free_copy;
  2455. }
  2456. err = rewrite_section_headers(info, flags);
  2457. if (err)
  2458. goto free_copy;
  2459. /* Check module struct version now, before we try to use module. */
  2460. if (!check_modstruct_version(info, info->mod)) {
  2461. err = -ENOEXEC;
  2462. goto free_copy;
  2463. }
  2464. /* Figure out module layout, and allocate all the memory. */
  2465. mod = layout_and_allocate(info, flags);
  2466. if (IS_ERR(mod)) {
  2467. err = PTR_ERR(mod);
  2468. goto free_copy;
  2469. }
  2470. audit_log_kern_module(mod->name);
  2471. /* Reserve our place in the list. */
  2472. err = add_unformed_module(mod);
  2473. if (err)
  2474. goto free_module;
  2475. #ifdef CONFIG_MODULE_SIG
  2476. mod->sig_ok = info->sig_ok;
  2477. if (!mod->sig_ok) {
  2478. pr_notice_once("%s: module verification failed: signature "
  2479. "and/or required key missing - tainting "
  2480. "kernel\n", mod->name);
  2481. add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
  2482. }
  2483. #else
  2484. mod->sig_ok = 0;
  2485. #endif
  2486. /* To avoid stressing percpu allocator, do this once we're unique. */
  2487. err = percpu_modalloc(mod, info);
  2488. if (err)
  2489. goto unlink_mod;
  2490. /* Now module is in final location, initialize linked lists, etc. */
  2491. err = module_unload_init(mod);
  2492. if (err)
  2493. goto unlink_mod;
  2494. init_param_lock(mod);
  2495. /*
  2496. * Now we've got everything in the final locations, we can
  2497. * find optional sections.
  2498. */
  2499. err = find_module_sections(mod, info);
  2500. if (err)
  2501. goto free_unload;
  2502. err = check_module_license_and_versions(mod);
  2503. if (err)
  2504. goto free_unload;
  2505. /* Set up MODINFO_ATTR fields */
  2506. setup_modinfo(mod, info);
  2507. /* Fix up syms, so that st_value is a pointer to location. */
  2508. err = simplify_symbols(mod, info);
  2509. if (err < 0)
  2510. goto free_modinfo;
  2511. err = apply_relocations(mod, info);
  2512. if (err < 0)
  2513. goto free_modinfo;
  2514. err = post_relocation(mod, info);
  2515. if (err < 0)
  2516. goto free_modinfo;
  2517. flush_module_icache(mod);
  2518. /* Now copy in args */
  2519. mod->args = strndup_user(uargs, ~0UL >> 1);
  2520. if (IS_ERR(mod->args)) {
  2521. err = PTR_ERR(mod->args);
  2522. goto free_arch_cleanup;
  2523. }
  2524. init_build_id(mod, info);
  2525. dynamic_debug_setup(mod, &info->dyndbg);
  2526. /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
  2527. ftrace_module_init(mod);
  2528. /* Finally it's fully formed, ready to start executing. */
  2529. err = complete_formation(mod, info);
  2530. if (err)
  2531. goto ddebug_cleanup;
  2532. err = prepare_coming_module(mod);
  2533. if (err)
  2534. goto bug_cleanup;
  2535. mod->async_probe_requested = async_probe;
  2536. /* Module is ready to execute: parsing args may do that. */
  2537. after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
  2538. -32768, 32767, mod,
  2539. unknown_module_param_cb);
  2540. if (IS_ERR(after_dashes)) {
  2541. err = PTR_ERR(after_dashes);
  2542. goto coming_cleanup;
  2543. } else if (after_dashes) {
  2544. pr_warn("%s: parameters '%s' after `--' ignored\n",
  2545. mod->name, after_dashes);
  2546. }
  2547. /* Link in to sysfs. */
  2548. err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
  2549. if (err < 0)
  2550. goto coming_cleanup;
  2551. if (is_livepatch_module(mod)) {
  2552. err = copy_module_elf(mod, info);
  2553. if (err < 0)
  2554. goto sysfs_cleanup;
  2555. }
  2556. /* Get rid of temporary copy. */
  2557. free_copy(info, flags);
  2558. /* Done! */
  2559. trace_module_load(mod);
  2560. return do_init_module(mod);
  2561. sysfs_cleanup:
  2562. mod_sysfs_teardown(mod);
  2563. coming_cleanup:
  2564. mod->state = MODULE_STATE_GOING;
  2565. destroy_params(mod->kp, mod->num_kp);
  2566. blocking_notifier_call_chain(&module_notify_list,
  2567. MODULE_STATE_GOING, mod);
  2568. klp_module_going(mod);
  2569. bug_cleanup:
  2570. mod->state = MODULE_STATE_GOING;
  2571. /* module_bug_cleanup needs module_mutex protection */
  2572. mutex_lock(&module_mutex);
  2573. module_bug_cleanup(mod);
  2574. mutex_unlock(&module_mutex);
  2575. #ifdef CONFIG_RKP
  2576. rkp_mod_info.base_va = 0;
  2577. rkp_mod_info.vm_size = 0;
  2578. rkp_mod_info.core_base_va = (u64)mod->core_layout.base;
  2579. rkp_mod_info.core_text_size = (u64)mod->core_layout.text_size;
  2580. rkp_mod_info.core_ro_size = (u64)mod->core_layout.ro_size;
  2581. rkp_mod_info.init_base_va = (u64)mod->init_layout.base;
  2582. rkp_mod_info.init_text_size = (u64)mod->init_layout.text_size;
  2583. uh_call(UH_APP_RKP, RKP_MODULE_LOAD, RKP_MODULE_PXN_SET, (u64)&rkp_mod_info, 0, 0);
  2584. #endif
  2585. ddebug_cleanup:
  2586. ftrace_release_mod(mod);
  2587. dynamic_debug_remove(mod, &info->dyndbg);
  2588. synchronize_rcu();
  2589. kfree(mod->args);
  2590. free_arch_cleanup:
  2591. module_arch_cleanup(mod);
  2592. free_modinfo:
  2593. free_modinfo(mod);
  2594. free_unload:
  2595. module_unload_free(mod);
  2596. unlink_mod:
  2597. mutex_lock(&module_mutex);
  2598. /* Unlink carefully: kallsyms could be walking list. */
  2599. list_del_rcu(&mod->list);
  2600. mod_tree_remove(mod);
  2601. wake_up_all(&module_wq);
  2602. /* Wait for RCU-sched synchronizing before releasing mod->list. */
  2603. synchronize_rcu();
  2604. mutex_unlock(&module_mutex);
  2605. free_module:
  2606. /* Free lock-classes; relies on the preceding sync_rcu() */
  2607. lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
  2608. module_deallocate(mod, info);
  2609. free_copy:
  2610. free_copy(info, flags);
  2611. return err;
  2612. }
  2613. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2614. unsigned long, len, const char __user *, uargs)
  2615. {
  2616. int err;
  2617. struct load_info info = { };
  2618. err = may_init_module();
  2619. if (err)
  2620. return err;
  2621. pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
  2622. umod, len, uargs);
  2623. err = copy_module_from_user(umod, len, &info);
  2624. if (err)
  2625. return err;
  2626. return load_module(&info, uargs, 0);
  2627. }
  2628. SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
  2629. {
  2630. struct load_info info = { };
  2631. void *buf = NULL;
  2632. int len;
  2633. int err;
  2634. err = may_init_module();
  2635. if (err)
  2636. return err;
  2637. pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
  2638. if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
  2639. |MODULE_INIT_IGNORE_VERMAGIC
  2640. |MODULE_INIT_COMPRESSED_FILE))
  2641. return -EINVAL;
  2642. len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
  2643. READING_MODULE);
  2644. if (len < 0)
  2645. return len;
  2646. if (flags & MODULE_INIT_COMPRESSED_FILE) {
  2647. err = module_decompress(&info, buf, len);
  2648. vfree(buf); /* compressed data is no longer needed */
  2649. if (err)
  2650. return err;
  2651. } else {
  2652. info.hdr = buf;
  2653. info.len = len;
  2654. }
  2655. return load_module(&info, uargs, flags);
  2656. }
  2657. static inline int within(unsigned long addr, void *start, unsigned long size)
  2658. {
  2659. return ((void *)addr >= start && (void *)addr < start + size);
  2660. }
  2661. /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
  2662. char *module_flags(struct module *mod, char *buf, bool show_state)
  2663. {
  2664. int bx = 0;
  2665. BUG_ON(mod->state == MODULE_STATE_UNFORMED);
  2666. if (!mod->taints && !show_state)
  2667. goto out;
  2668. if (mod->taints ||
  2669. mod->state == MODULE_STATE_GOING ||
  2670. mod->state == MODULE_STATE_COMING) {
  2671. buf[bx++] = '(';
  2672. bx += module_flags_taint(mod->taints, buf + bx);
  2673. /* Show a - for module-is-being-unloaded */
  2674. if (mod->state == MODULE_STATE_GOING && show_state)
  2675. buf[bx++] = '-';
  2676. /* Show a + for module-is-being-loaded */
  2677. if (mod->state == MODULE_STATE_COMING && show_state)
  2678. buf[bx++] = '+';
  2679. buf[bx++] = ')';
  2680. }
  2681. out:
  2682. buf[bx] = '\0';
  2683. return buf;
  2684. }
  2685. /* Given an address, look for it in the module exception tables. */
  2686. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2687. {
  2688. const struct exception_table_entry *e = NULL;
  2689. struct module *mod;
  2690. preempt_disable();
  2691. mod = __module_address(addr);
  2692. if (!mod)
  2693. goto out;
  2694. if (!mod->num_exentries)
  2695. goto out;
  2696. e = search_extable(mod->extable,
  2697. mod->num_exentries,
  2698. addr);
  2699. out:
  2700. preempt_enable();
  2701. /*
  2702. * Now, if we found one, we are running inside it now, hence
  2703. * we cannot unload the module, hence no refcnt needed.
  2704. */
  2705. return e;
  2706. }
  2707. /**
  2708. * is_module_address() - is this address inside a module?
  2709. * @addr: the address to check.
  2710. *
  2711. * See is_module_text_address() if you simply want to see if the address
  2712. * is code (not data).
  2713. */
  2714. bool is_module_address(unsigned long addr)
  2715. {
  2716. bool ret;
  2717. preempt_disable();
  2718. ret = __module_address(addr) != NULL;
  2719. preempt_enable();
  2720. return ret;
  2721. }
  2722. /**
  2723. * __module_address() - get the module which contains an address.
  2724. * @addr: the address.
  2725. *
  2726. * Must be called with preempt disabled or module mutex held so that
  2727. * module doesn't get freed during this.
  2728. */
  2729. struct module *__module_address(unsigned long addr)
  2730. {
  2731. struct module *mod;
  2732. struct mod_tree_root *tree;
  2733. if (addr >= mod_tree.addr_min && addr <= mod_tree.addr_max)
  2734. tree = &mod_tree;
  2735. #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
  2736. else if (addr >= mod_data_tree.addr_min && addr <= mod_data_tree.addr_max)
  2737. tree = &mod_data_tree;
  2738. #endif
  2739. else
  2740. return NULL;
  2741. module_assert_mutex_or_preempt();
  2742. mod = mod_find(addr, tree);
  2743. if (mod) {
  2744. BUG_ON(!within_module(addr, mod));
  2745. if (mod->state == MODULE_STATE_UNFORMED)
  2746. mod = NULL;
  2747. }
  2748. return mod;
  2749. }
  2750. /**
  2751. * is_module_text_address() - is this address inside module code?
  2752. * @addr: the address to check.
  2753. *
  2754. * See is_module_address() if you simply want to see if the address is
  2755. * anywhere in a module. See kernel_text_address() for testing if an
  2756. * address corresponds to kernel or module code.
  2757. */
  2758. bool is_module_text_address(unsigned long addr)
  2759. {
  2760. bool ret;
  2761. preempt_disable();
  2762. ret = __module_text_address(addr) != NULL;
  2763. preempt_enable();
  2764. return ret;
  2765. }
  2766. /**
  2767. * __module_text_address() - get the module whose code contains an address.
  2768. * @addr: the address.
  2769. *
  2770. * Must be called with preempt disabled or module mutex held so that
  2771. * module doesn't get freed during this.
  2772. */
  2773. struct module *__module_text_address(unsigned long addr)
  2774. {
  2775. struct module *mod = __module_address(addr);
  2776. if (mod) {
  2777. /* Make sure it's within the text section. */
  2778. if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
  2779. && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
  2780. mod = NULL;
  2781. }
  2782. return mod;
  2783. }
  2784. /* Don't grab lock, we're oopsing. */
  2785. void print_modules(void)
  2786. {
  2787. struct module *mod;
  2788. char buf[MODULE_FLAGS_BUF_SIZE];
  2789. printk(KERN_DEFAULT "Modules linked in:");
  2790. /* Most callers should already have preempt disabled, but make sure */
  2791. preempt_disable();
  2792. list_for_each_entry_rcu(mod, &modules, list) {
  2793. if (mod->state == MODULE_STATE_UNFORMED)
  2794. continue;
  2795. pr_cont(" %s%s", mod->name, module_flags(mod, buf, true));
  2796. }
  2797. print_unloaded_tainted_modules();
  2798. preempt_enable();
  2799. if (last_unloaded_module.name[0])
  2800. pr_cont(" [last unloaded: %s%s]", last_unloaded_module.name,
  2801. last_unloaded_module.taints);
  2802. pr_cont("\n");
  2803. }