module.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. /*
  2. Copyright (C) 2002 Richard Henderson
  3. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/ftrace_event.h>
  19. #include <linux/init.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/fs.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/elf.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/capability.h>
  33. #include <linux/cpu.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/errno.h>
  36. #include <linux/err.h>
  37. #include <linux/vermagic.h>
  38. #include <linux/notifier.h>
  39. #include <linux/sched.h>
  40. #include <linux/stop_machine.h>
  41. #include <linux/device.h>
  42. #include <linux/string.h>
  43. #include <linux/mutex.h>
  44. #include <linux/rculist.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/mmu_context.h>
  48. #include <linux/license.h>
  49. #include <asm/sections.h>
  50. #include <linux/tracepoint.h>
  51. #include <linux/ftrace.h>
  52. #include <linux/async.h>
  53. #include <linux/percpu.h>
  54. #include <linux/kmemleak.h>
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/module.h>
  57. #if 0
  58. #define DEBUGP printk
  59. #else
  60. #define DEBUGP(fmt , a...)
  61. #endif
  62. #ifndef ARCH_SHF_SMALL
  63. #define ARCH_SHF_SMALL 0
  64. #endif
  65. /* If this is set, the section belongs in the init part of the module */
  66. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  67. /*
  68. * Mutex protects:
  69. * 1) List of modules (also safely readable with preempt_disable),
  70. * 2) module_use links,
  71. * 3) module_addr_min/module_addr_max.
  72. * (delete uses stop_machine/add uses RCU list operations). */
  73. DEFINE_MUTEX(module_mutex);
  74. EXPORT_SYMBOL_GPL(module_mutex);
  75. static LIST_HEAD(modules);
  76. #ifdef CONFIG_KGDB_KDB
  77. struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
  78. #endif /* CONFIG_KGDB_KDB */
  79. /* Block module loading/unloading? */
  80. int modules_disabled = 0;
  81. /* Waiting for a module to finish initializing? */
  82. static DECLARE_WAIT_QUEUE_HEAD(module_wq);
  83. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  84. /* Bounds of module allocation, for speeding __module_address.
  85. * Protected by module_mutex. */
  86. static unsigned long module_addr_min = -1UL, module_addr_max = 0;
  87. int register_module_notifier(struct notifier_block * nb)
  88. {
  89. return blocking_notifier_chain_register(&module_notify_list, nb);
  90. }
  91. EXPORT_SYMBOL(register_module_notifier);
  92. int unregister_module_notifier(struct notifier_block * nb)
  93. {
  94. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  95. }
  96. EXPORT_SYMBOL(unregister_module_notifier);
  97. struct load_info {
  98. Elf_Ehdr *hdr;
  99. unsigned long len;
  100. Elf_Shdr *sechdrs;
  101. char *secstrings, *args, *strtab;
  102. struct {
  103. unsigned int sym, str, mod, vers, info, pcpu;
  104. } index;
  105. };
  106. /* We require a truly strong try_module_get(): 0 means failure due to
  107. ongoing or failed initialization etc. */
  108. static inline int strong_try_module_get(struct module *mod)
  109. {
  110. if (mod && mod->state == MODULE_STATE_COMING)
  111. return -EBUSY;
  112. if (try_module_get(mod))
  113. return 0;
  114. else
  115. return -ENOENT;
  116. }
  117. static inline void add_taint_module(struct module *mod, unsigned flag)
  118. {
  119. add_taint(flag);
  120. mod->taints |= (1U << flag);
  121. }
  122. /*
  123. * A thread that wants to hold a reference to a module only while it
  124. * is running can call this to safely exit. nfsd and lockd use this.
  125. */
  126. void __module_put_and_exit(struct module *mod, long code)
  127. {
  128. module_put(mod);
  129. do_exit(code);
  130. }
  131. EXPORT_SYMBOL(__module_put_and_exit);
  132. /* Find a module section: 0 means not found. */
  133. static unsigned int find_sec(Elf_Ehdr *hdr,
  134. Elf_Shdr *sechdrs,
  135. const char *secstrings,
  136. const char *name)
  137. {
  138. unsigned int i;
  139. for (i = 1; i < hdr->e_shnum; i++)
  140. /* Alloc bit cleared means "ignore it." */
  141. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  142. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  143. return i;
  144. return 0;
  145. }
  146. /* Find a module section, or NULL. */
  147. static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
  148. const char *secstrings, const char *name)
  149. {
  150. /* Section 0 has sh_addr 0. */
  151. return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
  152. }
  153. /* Find a module section, or NULL. Fill in number of "objects" in section. */
  154. static void *section_objs(Elf_Ehdr *hdr,
  155. Elf_Shdr *sechdrs,
  156. const char *secstrings,
  157. const char *name,
  158. size_t object_size,
  159. unsigned int *num)
  160. {
  161. unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
  162. /* Section 0 has sh_addr 0 and sh_size 0. */
  163. *num = sechdrs[sec].sh_size / object_size;
  164. return (void *)sechdrs[sec].sh_addr;
  165. }
  166. /* Provided by the linker */
  167. extern const struct kernel_symbol __start___ksymtab[];
  168. extern const struct kernel_symbol __stop___ksymtab[];
  169. extern const struct kernel_symbol __start___ksymtab_gpl[];
  170. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  171. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  172. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  173. extern const unsigned long __start___kcrctab[];
  174. extern const unsigned long __start___kcrctab_gpl[];
  175. extern const unsigned long __start___kcrctab_gpl_future[];
  176. #ifdef CONFIG_UNUSED_SYMBOLS
  177. extern const struct kernel_symbol __start___ksymtab_unused[];
  178. extern const struct kernel_symbol __stop___ksymtab_unused[];
  179. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  180. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  181. extern const unsigned long __start___kcrctab_unused[];
  182. extern const unsigned long __start___kcrctab_unused_gpl[];
  183. #endif
  184. #ifndef CONFIG_MODVERSIONS
  185. #define symversion(base, idx) NULL
  186. #else
  187. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  188. #endif
  189. static bool each_symbol_in_section(const struct symsearch *arr,
  190. unsigned int arrsize,
  191. struct module *owner,
  192. bool (*fn)(const struct symsearch *syms,
  193. struct module *owner,
  194. unsigned int symnum, void *data),
  195. void *data)
  196. {
  197. unsigned int i, j;
  198. for (j = 0; j < arrsize; j++) {
  199. for (i = 0; i < arr[j].stop - arr[j].start; i++)
  200. if (fn(&arr[j], owner, i, data))
  201. return true;
  202. }
  203. return false;
  204. }
  205. /* Returns true as soon as fn returns true, otherwise false. */
  206. bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
  207. unsigned int symnum, void *data), void *data)
  208. {
  209. struct module *mod;
  210. static const struct symsearch arr[] = {
  211. { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
  212. NOT_GPL_ONLY, false },
  213. { __start___ksymtab_gpl, __stop___ksymtab_gpl,
  214. __start___kcrctab_gpl,
  215. GPL_ONLY, false },
  216. { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
  217. __start___kcrctab_gpl_future,
  218. WILL_BE_GPL_ONLY, false },
  219. #ifdef CONFIG_UNUSED_SYMBOLS
  220. { __start___ksymtab_unused, __stop___ksymtab_unused,
  221. __start___kcrctab_unused,
  222. NOT_GPL_ONLY, true },
  223. { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
  224. __start___kcrctab_unused_gpl,
  225. GPL_ONLY, true },
  226. #endif
  227. };
  228. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
  229. return true;
  230. list_for_each_entry_rcu(mod, &modules, list) {
  231. struct symsearch arr[] = {
  232. { mod->syms, mod->syms + mod->num_syms, mod->crcs,
  233. NOT_GPL_ONLY, false },
  234. { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
  235. mod->gpl_crcs,
  236. GPL_ONLY, false },
  237. { mod->gpl_future_syms,
  238. mod->gpl_future_syms + mod->num_gpl_future_syms,
  239. mod->gpl_future_crcs,
  240. WILL_BE_GPL_ONLY, false },
  241. #ifdef CONFIG_UNUSED_SYMBOLS
  242. { mod->unused_syms,
  243. mod->unused_syms + mod->num_unused_syms,
  244. mod->unused_crcs,
  245. NOT_GPL_ONLY, true },
  246. { mod->unused_gpl_syms,
  247. mod->unused_gpl_syms + mod->num_unused_gpl_syms,
  248. mod->unused_gpl_crcs,
  249. GPL_ONLY, true },
  250. #endif
  251. };
  252. if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
  253. return true;
  254. }
  255. return false;
  256. }
  257. EXPORT_SYMBOL_GPL(each_symbol);
  258. struct find_symbol_arg {
  259. /* Input */
  260. const char *name;
  261. bool gplok;
  262. bool warn;
  263. /* Output */
  264. struct module *owner;
  265. const unsigned long *crc;
  266. const struct kernel_symbol *sym;
  267. };
  268. static bool find_symbol_in_section(const struct symsearch *syms,
  269. struct module *owner,
  270. unsigned int symnum, void *data)
  271. {
  272. struct find_symbol_arg *fsa = data;
  273. if (strcmp(syms->start[symnum].name, fsa->name) != 0)
  274. return false;
  275. if (!fsa->gplok) {
  276. if (syms->licence == GPL_ONLY)
  277. return false;
  278. if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
  279. printk(KERN_WARNING "Symbol %s is being used "
  280. "by a non-GPL module, which will not "
  281. "be allowed in the future\n", fsa->name);
  282. printk(KERN_WARNING "Please see the file "
  283. "Documentation/feature-removal-schedule.txt "
  284. "in the kernel source tree for more details.\n");
  285. }
  286. }
  287. #ifdef CONFIG_UNUSED_SYMBOLS
  288. if (syms->unused && fsa->warn) {
  289. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  290. "however this module is using it.\n", fsa->name);
  291. printk(KERN_WARNING
  292. "This symbol will go away in the future.\n");
  293. printk(KERN_WARNING
  294. "Please evalute if this is the right api to use and if "
  295. "it really is, submit a report the linux kernel "
  296. "mailinglist together with submitting your code for "
  297. "inclusion.\n");
  298. }
  299. #endif
  300. fsa->owner = owner;
  301. fsa->crc = symversion(syms->crcs, symnum);
  302. fsa->sym = &syms->start[symnum];
  303. return true;
  304. }
  305. /* Find a symbol and return it, along with, (optional) crc and
  306. * (optional) module which owns it. Needs preempt disabled or module_mutex. */
  307. const struct kernel_symbol *find_symbol(const char *name,
  308. struct module **owner,
  309. const unsigned long **crc,
  310. bool gplok,
  311. bool warn)
  312. {
  313. struct find_symbol_arg fsa;
  314. fsa.name = name;
  315. fsa.gplok = gplok;
  316. fsa.warn = warn;
  317. if (each_symbol(find_symbol_in_section, &fsa)) {
  318. if (owner)
  319. *owner = fsa.owner;
  320. if (crc)
  321. *crc = fsa.crc;
  322. return fsa.sym;
  323. }
  324. DEBUGP("Failed to find symbol %s\n", name);
  325. return NULL;
  326. }
  327. EXPORT_SYMBOL_GPL(find_symbol);
  328. /* Search for module by name: must hold module_mutex. */
  329. struct module *find_module(const char *name)
  330. {
  331. struct module *mod;
  332. list_for_each_entry(mod, &modules, list) {
  333. if (strcmp(mod->name, name) == 0)
  334. return mod;
  335. }
  336. return NULL;
  337. }
  338. EXPORT_SYMBOL_GPL(find_module);
  339. #ifdef CONFIG_SMP
  340. static inline void __percpu *mod_percpu(struct module *mod)
  341. {
  342. return mod->percpu;
  343. }
  344. static int percpu_modalloc(struct module *mod,
  345. unsigned long size, unsigned long align)
  346. {
  347. if (align > PAGE_SIZE) {
  348. printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
  349. mod->name, align, PAGE_SIZE);
  350. align = PAGE_SIZE;
  351. }
  352. mod->percpu = __alloc_reserved_percpu(size, align);
  353. if (!mod->percpu) {
  354. printk(KERN_WARNING
  355. "Could not allocate %lu bytes percpu data\n", size);
  356. return -ENOMEM;
  357. }
  358. mod->percpu_size = size;
  359. return 0;
  360. }
  361. static void percpu_modfree(struct module *mod)
  362. {
  363. free_percpu(mod->percpu);
  364. }
  365. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  366. Elf_Shdr *sechdrs,
  367. const char *secstrings)
  368. {
  369. return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
  370. }
  371. static void percpu_modcopy(struct module *mod,
  372. const void *from, unsigned long size)
  373. {
  374. int cpu;
  375. for_each_possible_cpu(cpu)
  376. memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
  377. }
  378. /**
  379. * is_module_percpu_address - test whether address is from module static percpu
  380. * @addr: address to test
  381. *
  382. * Test whether @addr belongs to module static percpu area.
  383. *
  384. * RETURNS:
  385. * %true if @addr is from module static percpu area
  386. */
  387. bool is_module_percpu_address(unsigned long addr)
  388. {
  389. struct module *mod;
  390. unsigned int cpu;
  391. preempt_disable();
  392. list_for_each_entry_rcu(mod, &modules, list) {
  393. if (!mod->percpu_size)
  394. continue;
  395. for_each_possible_cpu(cpu) {
  396. void *start = per_cpu_ptr(mod->percpu, cpu);
  397. if ((void *)addr >= start &&
  398. (void *)addr < start + mod->percpu_size) {
  399. preempt_enable();
  400. return true;
  401. }
  402. }
  403. }
  404. preempt_enable();
  405. return false;
  406. }
  407. #else /* ... !CONFIG_SMP */
  408. static inline void __percpu *mod_percpu(struct module *mod)
  409. {
  410. return NULL;
  411. }
  412. static inline int percpu_modalloc(struct module *mod,
  413. unsigned long size, unsigned long align)
  414. {
  415. return -ENOMEM;
  416. }
  417. static inline void percpu_modfree(struct module *mod)
  418. {
  419. }
  420. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  421. Elf_Shdr *sechdrs,
  422. const char *secstrings)
  423. {
  424. return 0;
  425. }
  426. static inline void percpu_modcopy(struct module *mod,
  427. const void *from, unsigned long size)
  428. {
  429. /* pcpusec should be 0, and size of that section should be 0. */
  430. BUG_ON(size != 0);
  431. }
  432. bool is_module_percpu_address(unsigned long addr)
  433. {
  434. return false;
  435. }
  436. #endif /* CONFIG_SMP */
  437. #define MODINFO_ATTR(field) \
  438. static void setup_modinfo_##field(struct module *mod, const char *s) \
  439. { \
  440. mod->field = kstrdup(s, GFP_KERNEL); \
  441. } \
  442. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  443. struct module *mod, char *buffer) \
  444. { \
  445. return sprintf(buffer, "%s\n", mod->field); \
  446. } \
  447. static int modinfo_##field##_exists(struct module *mod) \
  448. { \
  449. return mod->field != NULL; \
  450. } \
  451. static void free_modinfo_##field(struct module *mod) \
  452. { \
  453. kfree(mod->field); \
  454. mod->field = NULL; \
  455. } \
  456. static struct module_attribute modinfo_##field = { \
  457. .attr = { .name = __stringify(field), .mode = 0444 }, \
  458. .show = show_modinfo_##field, \
  459. .setup = setup_modinfo_##field, \
  460. .test = modinfo_##field##_exists, \
  461. .free = free_modinfo_##field, \
  462. };
  463. MODINFO_ATTR(version);
  464. MODINFO_ATTR(srcversion);
  465. static char last_unloaded_module[MODULE_NAME_LEN+1];
  466. #ifdef CONFIG_MODULE_UNLOAD
  467. EXPORT_TRACEPOINT_SYMBOL(module_get);
  468. /* Init the unload section of the module. */
  469. static int module_unload_init(struct module *mod)
  470. {
  471. mod->refptr = alloc_percpu(struct module_ref);
  472. if (!mod->refptr)
  473. return -ENOMEM;
  474. INIT_LIST_HEAD(&mod->source_list);
  475. INIT_LIST_HEAD(&mod->target_list);
  476. /* Hold reference count during initialization. */
  477. __this_cpu_write(mod->refptr->incs, 1);
  478. /* Backwards compatibility macros put refcount during init. */
  479. mod->waiter = current;
  480. return 0;
  481. }
  482. /* Does a already use b? */
  483. static int already_uses(struct module *a, struct module *b)
  484. {
  485. struct module_use *use;
  486. list_for_each_entry(use, &b->source_list, source_list) {
  487. if (use->source == a) {
  488. DEBUGP("%s uses %s!\n", a->name, b->name);
  489. return 1;
  490. }
  491. }
  492. DEBUGP("%s does not use %s!\n", a->name, b->name);
  493. return 0;
  494. }
  495. /*
  496. * Module a uses b
  497. * - we add 'a' as a "source", 'b' as a "target" of module use
  498. * - the module_use is added to the list of 'b' sources (so
  499. * 'b' can walk the list to see who sourced them), and of 'a'
  500. * targets (so 'a' can see what modules it targets).
  501. */
  502. static int add_module_usage(struct module *a, struct module *b)
  503. {
  504. struct module_use *use;
  505. DEBUGP("Allocating new usage for %s.\n", a->name);
  506. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  507. if (!use) {
  508. printk(KERN_WARNING "%s: out of memory loading\n", a->name);
  509. return -ENOMEM;
  510. }
  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. 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. EXPORT_SYMBOL_GPL(ref_module);
  535. /* Clear the unload stuff of the module. */
  536. static void module_unload_free(struct module *mod)
  537. {
  538. struct module_use *use, *tmp;
  539. mutex_lock(&module_mutex);
  540. list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
  541. struct module *i = use->target;
  542. DEBUGP("%s unusing %s\n", mod->name, i->name);
  543. module_put(i);
  544. list_del(&use->source_list);
  545. list_del(&use->target_list);
  546. kfree(use);
  547. }
  548. mutex_unlock(&module_mutex);
  549. free_percpu(mod->refptr);
  550. }
  551. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  552. static inline int try_force_unload(unsigned int flags)
  553. {
  554. int ret = (flags & O_TRUNC);
  555. if (ret)
  556. add_taint(TAINT_FORCED_RMMOD);
  557. return ret;
  558. }
  559. #else
  560. static inline int try_force_unload(unsigned int flags)
  561. {
  562. return 0;
  563. }
  564. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  565. struct stopref
  566. {
  567. struct module *mod;
  568. int flags;
  569. int *forced;
  570. };
  571. /* Whole machine is stopped with interrupts off when this runs. */
  572. static int __try_stop_module(void *_sref)
  573. {
  574. struct stopref *sref = _sref;
  575. /* If it's not unused, quit unless we're forcing. */
  576. if (module_refcount(sref->mod) != 0) {
  577. if (!(*sref->forced = try_force_unload(sref->flags)))
  578. return -EWOULDBLOCK;
  579. }
  580. /* Mark it as dying. */
  581. sref->mod->state = MODULE_STATE_GOING;
  582. return 0;
  583. }
  584. static int try_stop_module(struct module *mod, int flags, int *forced)
  585. {
  586. if (flags & O_NONBLOCK) {
  587. struct stopref sref = { mod, flags, forced };
  588. return stop_machine(__try_stop_module, &sref, NULL);
  589. } else {
  590. /* We don't need to stop the machine for this. */
  591. mod->state = MODULE_STATE_GOING;
  592. synchronize_sched();
  593. return 0;
  594. }
  595. }
  596. unsigned int module_refcount(struct module *mod)
  597. {
  598. unsigned int incs = 0, decs = 0;
  599. int cpu;
  600. for_each_possible_cpu(cpu)
  601. decs += per_cpu_ptr(mod->refptr, cpu)->decs;
  602. /*
  603. * ensure the incs are added up after the decs.
  604. * module_put ensures incs are visible before decs with smp_wmb.
  605. *
  606. * This 2-count scheme avoids the situation where the refcount
  607. * for CPU0 is read, then CPU0 increments the module refcount,
  608. * then CPU1 drops that refcount, then the refcount for CPU1 is
  609. * read. We would record a decrement but not its corresponding
  610. * increment so we would see a low count (disaster).
  611. *
  612. * Rare situation? But module_refcount can be preempted, and we
  613. * might be tallying up 4096+ CPUs. So it is not impossible.
  614. */
  615. smp_rmb();
  616. for_each_possible_cpu(cpu)
  617. incs += per_cpu_ptr(mod->refptr, cpu)->incs;
  618. return incs - decs;
  619. }
  620. EXPORT_SYMBOL(module_refcount);
  621. /* This exists whether we can unload or not */
  622. static void free_module(struct module *mod);
  623. static void wait_for_zero_refcount(struct module *mod)
  624. {
  625. /* Since we might sleep for some time, release the mutex first */
  626. mutex_unlock(&module_mutex);
  627. for (;;) {
  628. DEBUGP("Looking at refcount...\n");
  629. set_current_state(TASK_UNINTERRUPTIBLE);
  630. if (module_refcount(mod) == 0)
  631. break;
  632. schedule();
  633. }
  634. current->state = TASK_RUNNING;
  635. mutex_lock(&module_mutex);
  636. }
  637. SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
  638. unsigned int, flags)
  639. {
  640. struct module *mod;
  641. char name[MODULE_NAME_LEN];
  642. int ret, forced = 0;
  643. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  644. return -EPERM;
  645. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  646. return -EFAULT;
  647. name[MODULE_NAME_LEN-1] = '\0';
  648. if (mutex_lock_interruptible(&module_mutex) != 0)
  649. return -EINTR;
  650. mod = find_module(name);
  651. if (!mod) {
  652. ret = -ENOENT;
  653. goto out;
  654. }
  655. if (!list_empty(&mod->source_list)) {
  656. /* Other modules depend on us: get rid of them first. */
  657. ret = -EWOULDBLOCK;
  658. goto out;
  659. }
  660. /* Doing init or already dying? */
  661. if (mod->state != MODULE_STATE_LIVE) {
  662. /* FIXME: if (force), slam module count and wake up
  663. waiter --RR */
  664. DEBUGP("%s already dying\n", mod->name);
  665. ret = -EBUSY;
  666. goto out;
  667. }
  668. /* If it has an init func, it must have an exit func to unload */
  669. if (mod->init && !mod->exit) {
  670. forced = try_force_unload(flags);
  671. if (!forced) {
  672. /* This module can't be removed */
  673. ret = -EBUSY;
  674. goto out;
  675. }
  676. }
  677. /* Set this up before setting mod->state */
  678. mod->waiter = current;
  679. /* Stop the machine so refcounts can't move and disable module. */
  680. ret = try_stop_module(mod, flags, &forced);
  681. if (ret != 0)
  682. goto out;
  683. /* Never wait if forced. */
  684. if (!forced && module_refcount(mod) != 0)
  685. wait_for_zero_refcount(mod);
  686. mutex_unlock(&module_mutex);
  687. /* Final destruction now noone is using it. */
  688. if (mod->exit != NULL)
  689. mod->exit();
  690. blocking_notifier_call_chain(&module_notify_list,
  691. MODULE_STATE_GOING, mod);
  692. async_synchronize_full();
  693. /* Store the name of the last unloaded module for diagnostic purposes */
  694. strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
  695. free_module(mod);
  696. return 0;
  697. out:
  698. mutex_unlock(&module_mutex);
  699. return ret;
  700. }
  701. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  702. {
  703. struct module_use *use;
  704. int printed_something = 0;
  705. seq_printf(m, " %u ", module_refcount(mod));
  706. /* Always include a trailing , so userspace can differentiate
  707. between this and the old multi-field proc format. */
  708. list_for_each_entry(use, &mod->source_list, source_list) {
  709. printed_something = 1;
  710. seq_printf(m, "%s,", use->source->name);
  711. }
  712. if (mod->init != NULL && mod->exit == NULL) {
  713. printed_something = 1;
  714. seq_printf(m, "[permanent],");
  715. }
  716. if (!printed_something)
  717. seq_printf(m, "-");
  718. }
  719. void __symbol_put(const char *symbol)
  720. {
  721. struct module *owner;
  722. preempt_disable();
  723. if (!find_symbol(symbol, &owner, NULL, true, false))
  724. BUG();
  725. module_put(owner);
  726. preempt_enable();
  727. }
  728. EXPORT_SYMBOL(__symbol_put);
  729. /* Note this assumes addr is a function, which it currently always is. */
  730. void symbol_put_addr(void *addr)
  731. {
  732. struct module *modaddr;
  733. unsigned long a = (unsigned long)dereference_function_descriptor(addr);
  734. if (core_kernel_text(a))
  735. return;
  736. /* module_text_address is safe here: we're supposed to have reference
  737. * to module from symbol_get, so it can't go away. */
  738. modaddr = __module_text_address(a);
  739. BUG_ON(!modaddr);
  740. module_put(modaddr);
  741. }
  742. EXPORT_SYMBOL_GPL(symbol_put_addr);
  743. static ssize_t show_refcnt(struct module_attribute *mattr,
  744. struct module *mod, char *buffer)
  745. {
  746. return sprintf(buffer, "%u\n", module_refcount(mod));
  747. }
  748. static struct module_attribute refcnt = {
  749. .attr = { .name = "refcnt", .mode = 0444 },
  750. .show = show_refcnt,
  751. };
  752. void module_put(struct module *module)
  753. {
  754. if (module) {
  755. preempt_disable();
  756. smp_wmb(); /* see comment in module_refcount */
  757. __this_cpu_inc(module->refptr->decs);
  758. trace_module_put(module, _RET_IP_);
  759. /* Maybe they're waiting for us to drop reference? */
  760. if (unlikely(!module_is_live(module)))
  761. wake_up_process(module->waiter);
  762. preempt_enable();
  763. }
  764. }
  765. EXPORT_SYMBOL(module_put);
  766. #else /* !CONFIG_MODULE_UNLOAD */
  767. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  768. {
  769. /* We don't know the usage count, or what modules are using. */
  770. seq_printf(m, " - -");
  771. }
  772. static inline void module_unload_free(struct module *mod)
  773. {
  774. }
  775. int ref_module(struct module *a, struct module *b)
  776. {
  777. return strong_try_module_get(b);
  778. }
  779. EXPORT_SYMBOL_GPL(ref_module);
  780. static inline int module_unload_init(struct module *mod)
  781. {
  782. return 0;
  783. }
  784. #endif /* CONFIG_MODULE_UNLOAD */
  785. static ssize_t show_initstate(struct module_attribute *mattr,
  786. struct module *mod, char *buffer)
  787. {
  788. const char *state = "unknown";
  789. switch (mod->state) {
  790. case MODULE_STATE_LIVE:
  791. state = "live";
  792. break;
  793. case MODULE_STATE_COMING:
  794. state = "coming";
  795. break;
  796. case MODULE_STATE_GOING:
  797. state = "going";
  798. break;
  799. }
  800. return sprintf(buffer, "%s\n", state);
  801. }
  802. static struct module_attribute initstate = {
  803. .attr = { .name = "initstate", .mode = 0444 },
  804. .show = show_initstate,
  805. };
  806. static struct module_attribute *modinfo_attrs[] = {
  807. &modinfo_version,
  808. &modinfo_srcversion,
  809. &initstate,
  810. #ifdef CONFIG_MODULE_UNLOAD
  811. &refcnt,
  812. #endif
  813. NULL,
  814. };
  815. static const char vermagic[] = VERMAGIC_STRING;
  816. static int try_to_force_load(struct module *mod, const char *reason)
  817. {
  818. #ifdef CONFIG_MODULE_FORCE_LOAD
  819. if (!test_taint(TAINT_FORCED_MODULE))
  820. printk(KERN_WARNING "%s: %s: kernel tainted.\n",
  821. mod->name, reason);
  822. add_taint_module(mod, TAINT_FORCED_MODULE);
  823. return 0;
  824. #else
  825. return -ENOEXEC;
  826. #endif
  827. }
  828. #ifdef CONFIG_MODVERSIONS
  829. /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
  830. static unsigned long maybe_relocated(unsigned long crc,
  831. const struct module *crc_owner)
  832. {
  833. #ifdef ARCH_RELOCATES_KCRCTAB
  834. if (crc_owner == NULL)
  835. return crc - (unsigned long)reloc_start;
  836. #endif
  837. return crc;
  838. }
  839. static int check_version(Elf_Shdr *sechdrs,
  840. unsigned int versindex,
  841. const char *symname,
  842. struct module *mod,
  843. const unsigned long *crc,
  844. const struct module *crc_owner)
  845. {
  846. unsigned int i, num_versions;
  847. struct modversion_info *versions;
  848. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  849. if (!crc)
  850. return 1;
  851. /* No versions at all? modprobe --force does this. */
  852. if (versindex == 0)
  853. return try_to_force_load(mod, symname) == 0;
  854. versions = (void *) sechdrs[versindex].sh_addr;
  855. num_versions = sechdrs[versindex].sh_size
  856. / sizeof(struct modversion_info);
  857. for (i = 0; i < num_versions; i++) {
  858. if (strcmp(versions[i].name, symname) != 0)
  859. continue;
  860. if (versions[i].crc == maybe_relocated(*crc, crc_owner))
  861. return 1;
  862. DEBUGP("Found checksum %lX vs module %lX\n",
  863. maybe_relocated(*crc, crc_owner), versions[i].crc);
  864. goto bad_version;
  865. }
  866. printk(KERN_WARNING "%s: no symbol version for %s\n",
  867. mod->name, symname);
  868. return 0;
  869. bad_version:
  870. printk("%s: disagrees about version of symbol %s\n",
  871. mod->name, symname);
  872. return 0;
  873. }
  874. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  875. unsigned int versindex,
  876. struct module *mod)
  877. {
  878. const unsigned long *crc;
  879. /* Since this should be found in kernel (which can't be removed),
  880. * no locking is necessary. */
  881. if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
  882. &crc, true, false))
  883. BUG();
  884. return check_version(sechdrs, versindex, "module_layout", mod, crc,
  885. NULL);
  886. }
  887. /* First part is kernel version, which we ignore if module has crcs. */
  888. static inline int same_magic(const char *amagic, const char *bmagic,
  889. bool has_crcs)
  890. {
  891. if (has_crcs) {
  892. amagic += strcspn(amagic, " ");
  893. bmagic += strcspn(bmagic, " ");
  894. }
  895. return strcmp(amagic, bmagic) == 0;
  896. }
  897. #else
  898. static inline int check_version(Elf_Shdr *sechdrs,
  899. unsigned int versindex,
  900. const char *symname,
  901. struct module *mod,
  902. const unsigned long *crc,
  903. const struct module *crc_owner)
  904. {
  905. return 1;
  906. }
  907. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  908. unsigned int versindex,
  909. struct module *mod)
  910. {
  911. return 1;
  912. }
  913. static inline int same_magic(const char *amagic, const char *bmagic,
  914. bool has_crcs)
  915. {
  916. return strcmp(amagic, bmagic) == 0;
  917. }
  918. #endif /* CONFIG_MODVERSIONS */
  919. /* Resolve a symbol for this module. I.e. if we find one, record usage. */
  920. static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
  921. unsigned int versindex,
  922. const char *name,
  923. struct module *mod,
  924. char ownername[])
  925. {
  926. struct module *owner;
  927. const struct kernel_symbol *sym;
  928. const unsigned long *crc;
  929. int err;
  930. mutex_lock(&module_mutex);
  931. sym = find_symbol(name, &owner, &crc,
  932. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  933. if (!sym)
  934. goto unlock;
  935. if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
  936. sym = ERR_PTR(-EINVAL);
  937. goto getname;
  938. }
  939. err = ref_module(mod, owner);
  940. if (err) {
  941. sym = ERR_PTR(err);
  942. goto getname;
  943. }
  944. getname:
  945. /* We must make copy under the lock if we failed to get ref. */
  946. strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
  947. unlock:
  948. mutex_unlock(&module_mutex);
  949. return sym;
  950. }
  951. static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
  952. unsigned int versindex,
  953. const char *name,
  954. struct module *mod)
  955. {
  956. const struct kernel_symbol *ksym;
  957. char ownername[MODULE_NAME_LEN];
  958. if (wait_event_interruptible_timeout(module_wq,
  959. !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
  960. mod, ownername)) ||
  961. PTR_ERR(ksym) != -EBUSY,
  962. 30 * HZ) <= 0) {
  963. printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
  964. mod->name, ownername);
  965. }
  966. return ksym;
  967. }
  968. /*
  969. * /sys/module/foo/sections stuff
  970. * J. Corbet <corbet@lwn.net>
  971. */
  972. #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
  973. static inline bool sect_empty(const Elf_Shdr *sect)
  974. {
  975. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  976. }
  977. struct module_sect_attr
  978. {
  979. struct module_attribute mattr;
  980. char *name;
  981. unsigned long address;
  982. };
  983. struct module_sect_attrs
  984. {
  985. struct attribute_group grp;
  986. unsigned int nsections;
  987. struct module_sect_attr attrs[0];
  988. };
  989. static ssize_t module_sect_show(struct module_attribute *mattr,
  990. struct module *mod, char *buf)
  991. {
  992. struct module_sect_attr *sattr =
  993. container_of(mattr, struct module_sect_attr, mattr);
  994. return sprintf(buf, "0x%lx\n", sattr->address);
  995. }
  996. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  997. {
  998. unsigned int section;
  999. for (section = 0; section < sect_attrs->nsections; section++)
  1000. kfree(sect_attrs->attrs[section].name);
  1001. kfree(sect_attrs);
  1002. }
  1003. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  1004. char *secstrings, Elf_Shdr *sechdrs)
  1005. {
  1006. unsigned int nloaded = 0, i, size[2];
  1007. struct module_sect_attrs *sect_attrs;
  1008. struct module_sect_attr *sattr;
  1009. struct attribute **gattr;
  1010. /* Count loaded sections and allocate structures */
  1011. for (i = 0; i < nsect; i++)
  1012. if (!sect_empty(&sechdrs[i]))
  1013. nloaded++;
  1014. size[0] = ALIGN(sizeof(*sect_attrs)
  1015. + nloaded * sizeof(sect_attrs->attrs[0]),
  1016. sizeof(sect_attrs->grp.attrs[0]));
  1017. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  1018. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  1019. if (sect_attrs == NULL)
  1020. return;
  1021. /* Setup section attributes. */
  1022. sect_attrs->grp.name = "sections";
  1023. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  1024. sect_attrs->nsections = 0;
  1025. sattr = &sect_attrs->attrs[0];
  1026. gattr = &sect_attrs->grp.attrs[0];
  1027. for (i = 0; i < nsect; i++) {
  1028. if (sect_empty(&sechdrs[i]))
  1029. continue;
  1030. sattr->address = sechdrs[i].sh_addr;
  1031. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  1032. GFP_KERNEL);
  1033. if (sattr->name == NULL)
  1034. goto out;
  1035. sect_attrs->nsections++;
  1036. sysfs_attr_init(&sattr->mattr.attr);
  1037. sattr->mattr.show = module_sect_show;
  1038. sattr->mattr.store = NULL;
  1039. sattr->mattr.attr.name = sattr->name;
  1040. sattr->mattr.attr.mode = S_IRUGO;
  1041. *(gattr++) = &(sattr++)->mattr.attr;
  1042. }
  1043. *gattr = NULL;
  1044. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1045. goto out;
  1046. mod->sect_attrs = sect_attrs;
  1047. return;
  1048. out:
  1049. free_sect_attrs(sect_attrs);
  1050. }
  1051. static void remove_sect_attrs(struct module *mod)
  1052. {
  1053. if (mod->sect_attrs) {
  1054. sysfs_remove_group(&mod->mkobj.kobj,
  1055. &mod->sect_attrs->grp);
  1056. /* We are positive that no one is using any sect attrs
  1057. * at this point. Deallocate immediately. */
  1058. free_sect_attrs(mod->sect_attrs);
  1059. mod->sect_attrs = NULL;
  1060. }
  1061. }
  1062. /*
  1063. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1064. */
  1065. struct module_notes_attrs {
  1066. struct kobject *dir;
  1067. unsigned int notes;
  1068. struct bin_attribute attrs[0];
  1069. };
  1070. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  1071. struct bin_attribute *bin_attr,
  1072. char *buf, loff_t pos, size_t count)
  1073. {
  1074. /*
  1075. * The caller checked the pos and count against our size.
  1076. */
  1077. memcpy(buf, bin_attr->private + pos, count);
  1078. return count;
  1079. }
  1080. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1081. unsigned int i)
  1082. {
  1083. if (notes_attrs->dir) {
  1084. while (i-- > 0)
  1085. sysfs_remove_bin_file(notes_attrs->dir,
  1086. &notes_attrs->attrs[i]);
  1087. kobject_put(notes_attrs->dir);
  1088. }
  1089. kfree(notes_attrs);
  1090. }
  1091. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  1092. char *secstrings, Elf_Shdr *sechdrs)
  1093. {
  1094. unsigned int notes, loaded, i;
  1095. struct module_notes_attrs *notes_attrs;
  1096. struct bin_attribute *nattr;
  1097. /* failed to create section attributes, so can't create notes */
  1098. if (!mod->sect_attrs)
  1099. return;
  1100. /* Count notes sections and allocate structures. */
  1101. notes = 0;
  1102. for (i = 0; i < nsect; i++)
  1103. if (!sect_empty(&sechdrs[i]) &&
  1104. (sechdrs[i].sh_type == SHT_NOTE))
  1105. ++notes;
  1106. if (notes == 0)
  1107. return;
  1108. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1109. + notes * sizeof(notes_attrs->attrs[0]),
  1110. GFP_KERNEL);
  1111. if (notes_attrs == NULL)
  1112. return;
  1113. notes_attrs->notes = notes;
  1114. nattr = &notes_attrs->attrs[0];
  1115. for (loaded = i = 0; i < nsect; ++i) {
  1116. if (sect_empty(&sechdrs[i]))
  1117. continue;
  1118. if (sechdrs[i].sh_type == SHT_NOTE) {
  1119. sysfs_bin_attr_init(nattr);
  1120. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1121. nattr->attr.mode = S_IRUGO;
  1122. nattr->size = sechdrs[i].sh_size;
  1123. nattr->private = (void *) sechdrs[i].sh_addr;
  1124. nattr->read = module_notes_read;
  1125. ++nattr;
  1126. }
  1127. ++loaded;
  1128. }
  1129. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1130. if (!notes_attrs->dir)
  1131. goto out;
  1132. for (i = 0; i < notes; ++i)
  1133. if (sysfs_create_bin_file(notes_attrs->dir,
  1134. &notes_attrs->attrs[i]))
  1135. goto out;
  1136. mod->notes_attrs = notes_attrs;
  1137. return;
  1138. out:
  1139. free_notes_attrs(notes_attrs, i);
  1140. }
  1141. static void remove_notes_attrs(struct module *mod)
  1142. {
  1143. if (mod->notes_attrs)
  1144. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1145. }
  1146. #else
  1147. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1148. char *sectstrings, Elf_Shdr *sechdrs)
  1149. {
  1150. }
  1151. static inline void remove_sect_attrs(struct module *mod)
  1152. {
  1153. }
  1154. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1155. char *sectstrings, Elf_Shdr *sechdrs)
  1156. {
  1157. }
  1158. static inline void remove_notes_attrs(struct module *mod)
  1159. {
  1160. }
  1161. #endif
  1162. #ifdef CONFIG_SYSFS
  1163. static void add_usage_links(struct module *mod)
  1164. {
  1165. #ifdef CONFIG_MODULE_UNLOAD
  1166. struct module_use *use;
  1167. int nowarn;
  1168. mutex_lock(&module_mutex);
  1169. list_for_each_entry(use, &mod->target_list, target_list) {
  1170. nowarn = sysfs_create_link(use->target->holders_dir,
  1171. &mod->mkobj.kobj, mod->name);
  1172. }
  1173. mutex_unlock(&module_mutex);
  1174. #endif
  1175. }
  1176. static void del_usage_links(struct module *mod)
  1177. {
  1178. #ifdef CONFIG_MODULE_UNLOAD
  1179. struct module_use *use;
  1180. mutex_lock(&module_mutex);
  1181. list_for_each_entry(use, &mod->target_list, target_list)
  1182. sysfs_remove_link(use->target->holders_dir, mod->name);
  1183. mutex_unlock(&module_mutex);
  1184. #endif
  1185. }
  1186. static int module_add_modinfo_attrs(struct module *mod)
  1187. {
  1188. struct module_attribute *attr;
  1189. struct module_attribute *temp_attr;
  1190. int error = 0;
  1191. int i;
  1192. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1193. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1194. GFP_KERNEL);
  1195. if (!mod->modinfo_attrs)
  1196. return -ENOMEM;
  1197. temp_attr = mod->modinfo_attrs;
  1198. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1199. if (!attr->test ||
  1200. (attr->test && attr->test(mod))) {
  1201. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1202. sysfs_attr_init(&temp_attr->attr);
  1203. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1204. ++temp_attr;
  1205. }
  1206. }
  1207. return error;
  1208. }
  1209. static void module_remove_modinfo_attrs(struct module *mod)
  1210. {
  1211. struct module_attribute *attr;
  1212. int i;
  1213. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1214. /* pick a field to test for end of list */
  1215. if (!attr->attr.name)
  1216. break;
  1217. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1218. if (attr->free)
  1219. attr->free(mod);
  1220. }
  1221. kfree(mod->modinfo_attrs);
  1222. }
  1223. static int mod_sysfs_init(struct module *mod)
  1224. {
  1225. int err;
  1226. struct kobject *kobj;
  1227. if (!module_sysfs_initialized) {
  1228. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1229. mod->name);
  1230. err = -EINVAL;
  1231. goto out;
  1232. }
  1233. kobj = kset_find_obj(module_kset, mod->name);
  1234. if (kobj) {
  1235. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1236. kobject_put(kobj);
  1237. err = -EINVAL;
  1238. goto out;
  1239. }
  1240. mod->mkobj.mod = mod;
  1241. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1242. mod->mkobj.kobj.kset = module_kset;
  1243. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1244. "%s", mod->name);
  1245. if (err)
  1246. kobject_put(&mod->mkobj.kobj);
  1247. /* delay uevent until full sysfs population */
  1248. out:
  1249. return err;
  1250. }
  1251. static int mod_sysfs_setup(struct module *mod,
  1252. struct kernel_param *kparam,
  1253. unsigned int num_params)
  1254. {
  1255. int err;
  1256. err = mod_sysfs_init(mod);
  1257. if (err)
  1258. goto out;
  1259. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1260. if (!mod->holders_dir) {
  1261. err = -ENOMEM;
  1262. goto out_unreg;
  1263. }
  1264. err = module_param_sysfs_setup(mod, kparam, num_params);
  1265. if (err)
  1266. goto out_unreg_holders;
  1267. err = module_add_modinfo_attrs(mod);
  1268. if (err)
  1269. goto out_unreg_param;
  1270. add_usage_links(mod);
  1271. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1272. return 0;
  1273. out_unreg_param:
  1274. module_param_sysfs_remove(mod);
  1275. out_unreg_holders:
  1276. kobject_put(mod->holders_dir);
  1277. out_unreg:
  1278. kobject_put(&mod->mkobj.kobj);
  1279. out:
  1280. return err;
  1281. }
  1282. static void mod_sysfs_fini(struct module *mod)
  1283. {
  1284. kobject_put(&mod->mkobj.kobj);
  1285. }
  1286. #else /* CONFIG_SYSFS */
  1287. static inline int mod_sysfs_init(struct module *mod)
  1288. {
  1289. return 0;
  1290. }
  1291. static inline int mod_sysfs_setup(struct module *mod,
  1292. struct kernel_param *kparam,
  1293. unsigned int num_params)
  1294. {
  1295. return 0;
  1296. }
  1297. static inline int module_add_modinfo_attrs(struct module *mod)
  1298. {
  1299. return 0;
  1300. }
  1301. static inline void module_remove_modinfo_attrs(struct module *mod)
  1302. {
  1303. }
  1304. static void mod_sysfs_fini(struct module *mod)
  1305. {
  1306. }
  1307. static void del_usage_links(struct module *mod)
  1308. {
  1309. }
  1310. #endif /* CONFIG_SYSFS */
  1311. static void mod_kobject_remove(struct module *mod)
  1312. {
  1313. del_usage_links(mod);
  1314. module_remove_modinfo_attrs(mod);
  1315. module_param_sysfs_remove(mod);
  1316. kobject_put(mod->mkobj.drivers_dir);
  1317. kobject_put(mod->holders_dir);
  1318. mod_sysfs_fini(mod);
  1319. }
  1320. /*
  1321. * unlink the module with the whole machine is stopped with interrupts off
  1322. * - this defends against kallsyms not taking locks
  1323. */
  1324. static int __unlink_module(void *_mod)
  1325. {
  1326. struct module *mod = _mod;
  1327. list_del(&mod->list);
  1328. return 0;
  1329. }
  1330. /* Free a module, remove from lists, etc. */
  1331. static void free_module(struct module *mod)
  1332. {
  1333. trace_module_free(mod);
  1334. /* Delete from various lists */
  1335. mutex_lock(&module_mutex);
  1336. stop_machine(__unlink_module, mod, NULL);
  1337. mutex_unlock(&module_mutex);
  1338. remove_notes_attrs(mod);
  1339. remove_sect_attrs(mod);
  1340. mod_kobject_remove(mod);
  1341. /* Remove dynamic debug info */
  1342. ddebug_remove_module(mod->name);
  1343. /* Arch-specific cleanup. */
  1344. module_arch_cleanup(mod);
  1345. /* Module unload stuff */
  1346. module_unload_free(mod);
  1347. /* Free any allocated parameters. */
  1348. destroy_params(mod->kp, mod->num_kp);
  1349. /* This may be NULL, but that's OK */
  1350. module_free(mod, mod->module_init);
  1351. kfree(mod->args);
  1352. percpu_modfree(mod);
  1353. /* Free lock-classes: */
  1354. lockdep_free_key_range(mod->module_core, mod->core_size);
  1355. /* Finally, free the core (containing the module structure) */
  1356. module_free(mod, mod->module_core);
  1357. #ifdef CONFIG_MPU
  1358. update_protections(current->mm);
  1359. #endif
  1360. }
  1361. void *__symbol_get(const char *symbol)
  1362. {
  1363. struct module *owner;
  1364. const struct kernel_symbol *sym;
  1365. preempt_disable();
  1366. sym = find_symbol(symbol, &owner, NULL, true, true);
  1367. if (sym && strong_try_module_get(owner))
  1368. sym = NULL;
  1369. preempt_enable();
  1370. return sym ? (void *)sym->value : NULL;
  1371. }
  1372. EXPORT_SYMBOL_GPL(__symbol_get);
  1373. /*
  1374. * Ensure that an exported symbol [global namespace] does not already exist
  1375. * in the kernel or in some other module's exported symbol table.
  1376. *
  1377. * You must hold the module_mutex.
  1378. */
  1379. static int verify_export_symbols(struct module *mod)
  1380. {
  1381. unsigned int i;
  1382. struct module *owner;
  1383. const struct kernel_symbol *s;
  1384. struct {
  1385. const struct kernel_symbol *sym;
  1386. unsigned int num;
  1387. } arr[] = {
  1388. { mod->syms, mod->num_syms },
  1389. { mod->gpl_syms, mod->num_gpl_syms },
  1390. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1391. #ifdef CONFIG_UNUSED_SYMBOLS
  1392. { mod->unused_syms, mod->num_unused_syms },
  1393. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1394. #endif
  1395. };
  1396. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1397. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1398. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1399. printk(KERN_ERR
  1400. "%s: exports duplicate symbol %s"
  1401. " (owned by %s)\n",
  1402. mod->name, s->name, module_name(owner));
  1403. return -ENOEXEC;
  1404. }
  1405. }
  1406. }
  1407. return 0;
  1408. }
  1409. /* Change all symbols so that st_value encodes the pointer directly. */
  1410. static int simplify_symbols(Elf_Shdr *sechdrs,
  1411. unsigned int symindex,
  1412. const char *strtab,
  1413. unsigned int versindex,
  1414. unsigned int pcpuindex,
  1415. struct module *mod)
  1416. {
  1417. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1418. unsigned long secbase;
  1419. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1420. int ret = 0;
  1421. const struct kernel_symbol *ksym;
  1422. for (i = 1; i < n; i++) {
  1423. switch (sym[i].st_shndx) {
  1424. case SHN_COMMON:
  1425. /* We compiled with -fno-common. These are not
  1426. supposed to happen. */
  1427. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1428. printk("%s: please compile with -fno-common\n",
  1429. mod->name);
  1430. ret = -ENOEXEC;
  1431. break;
  1432. case SHN_ABS:
  1433. /* Don't need to do anything */
  1434. DEBUGP("Absolute symbol: 0x%08lx\n",
  1435. (long)sym[i].st_value);
  1436. break;
  1437. case SHN_UNDEF:
  1438. ksym = resolve_symbol_wait(sechdrs, versindex,
  1439. strtab + sym[i].st_name,
  1440. mod);
  1441. /* Ok if resolved. */
  1442. if (ksym && !IS_ERR(ksym)) {
  1443. sym[i].st_value = ksym->value;
  1444. break;
  1445. }
  1446. /* Ok if weak. */
  1447. if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1448. break;
  1449. printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
  1450. mod->name, strtab + sym[i].st_name,
  1451. PTR_ERR(ksym));
  1452. ret = PTR_ERR(ksym) ?: -ENOENT;
  1453. break;
  1454. default:
  1455. /* Divert to percpu allocation if a percpu var. */
  1456. if (sym[i].st_shndx == pcpuindex)
  1457. secbase = (unsigned long)mod_percpu(mod);
  1458. else
  1459. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1460. sym[i].st_value += secbase;
  1461. break;
  1462. }
  1463. }
  1464. return ret;
  1465. }
  1466. static int apply_relocations(struct module *mod,
  1467. Elf_Ehdr *hdr,
  1468. Elf_Shdr *sechdrs,
  1469. unsigned int symindex,
  1470. unsigned int strindex)
  1471. {
  1472. unsigned int i;
  1473. int err = 0;
  1474. /* Now do relocations. */
  1475. for (i = 1; i < hdr->e_shnum; i++) {
  1476. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1477. unsigned int info = sechdrs[i].sh_info;
  1478. /* Not a valid relocation section? */
  1479. if (info >= hdr->e_shnum)
  1480. continue;
  1481. /* Don't bother with non-allocated sections */
  1482. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1483. continue;
  1484. if (sechdrs[i].sh_type == SHT_REL)
  1485. err = apply_relocate(sechdrs, strtab, symindex, i, mod);
  1486. else if (sechdrs[i].sh_type == SHT_RELA)
  1487. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1488. mod);
  1489. if (err < 0)
  1490. break;
  1491. }
  1492. return err;
  1493. }
  1494. /* Additional bytes needed by arch in front of individual sections */
  1495. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1496. unsigned int section)
  1497. {
  1498. /* default implementation just returns zero */
  1499. return 0;
  1500. }
  1501. /* Update size with this section: return offset. */
  1502. static long get_offset(struct module *mod, unsigned int *size,
  1503. Elf_Shdr *sechdr, unsigned int section)
  1504. {
  1505. long ret;
  1506. *size += arch_mod_section_prepend(mod, section);
  1507. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1508. *size = ret + sechdr->sh_size;
  1509. return ret;
  1510. }
  1511. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1512. might -- code, read-only data, read-write data, small data. Tally
  1513. sizes, and place the offsets into sh_entsize fields: high bit means it
  1514. belongs in init. */
  1515. static void layout_sections(struct module *mod,
  1516. const Elf_Ehdr *hdr,
  1517. Elf_Shdr *sechdrs,
  1518. const char *secstrings)
  1519. {
  1520. static unsigned long const masks[][2] = {
  1521. /* NOTE: all executable code must be the first section
  1522. * in this array; otherwise modify the text_size
  1523. * finder in the two loops below */
  1524. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1525. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1526. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1527. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1528. };
  1529. unsigned int m, i;
  1530. for (i = 0; i < hdr->e_shnum; i++)
  1531. sechdrs[i].sh_entsize = ~0UL;
  1532. DEBUGP("Core section allocation order:\n");
  1533. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1534. for (i = 0; i < hdr->e_shnum; ++i) {
  1535. Elf_Shdr *s = &sechdrs[i];
  1536. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1537. || (s->sh_flags & masks[m][1])
  1538. || s->sh_entsize != ~0UL
  1539. || strstarts(secstrings + s->sh_name, ".init"))
  1540. continue;
  1541. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1542. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1543. }
  1544. if (m == 0)
  1545. mod->core_text_size = mod->core_size;
  1546. }
  1547. DEBUGP("Init section allocation order:\n");
  1548. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1549. for (i = 0; i < hdr->e_shnum; ++i) {
  1550. Elf_Shdr *s = &sechdrs[i];
  1551. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1552. || (s->sh_flags & masks[m][1])
  1553. || s->sh_entsize != ~0UL
  1554. || !strstarts(secstrings + s->sh_name, ".init"))
  1555. continue;
  1556. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1557. | INIT_OFFSET_MASK);
  1558. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1559. }
  1560. if (m == 0)
  1561. mod->init_text_size = mod->init_size;
  1562. }
  1563. }
  1564. static void set_license(struct module *mod, const char *license)
  1565. {
  1566. if (!license)
  1567. license = "unspecified";
  1568. if (!license_is_gpl_compatible(license)) {
  1569. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1570. printk(KERN_WARNING "%s: module license '%s' taints "
  1571. "kernel.\n", mod->name, license);
  1572. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1573. }
  1574. }
  1575. /* Parse tag=value strings from .modinfo section */
  1576. static char *next_string(char *string, unsigned long *secsize)
  1577. {
  1578. /* Skip non-zero chars */
  1579. while (string[0]) {
  1580. string++;
  1581. if ((*secsize)-- <= 1)
  1582. return NULL;
  1583. }
  1584. /* Skip any zero padding. */
  1585. while (!string[0]) {
  1586. string++;
  1587. if ((*secsize)-- <= 1)
  1588. return NULL;
  1589. }
  1590. return string;
  1591. }
  1592. static char *get_modinfo(const Elf_Shdr *sechdrs,
  1593. unsigned int info,
  1594. const char *tag)
  1595. {
  1596. char *p;
  1597. unsigned int taglen = strlen(tag);
  1598. unsigned long size = sechdrs[info].sh_size;
  1599. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1600. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1601. return p + taglen + 1;
  1602. }
  1603. return NULL;
  1604. }
  1605. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1606. unsigned int infoindex)
  1607. {
  1608. struct module_attribute *attr;
  1609. int i;
  1610. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1611. if (attr->setup)
  1612. attr->setup(mod,
  1613. get_modinfo(sechdrs,
  1614. infoindex,
  1615. attr->attr.name));
  1616. }
  1617. }
  1618. static void free_modinfo(struct module *mod)
  1619. {
  1620. struct module_attribute *attr;
  1621. int i;
  1622. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1623. if (attr->free)
  1624. attr->free(mod);
  1625. }
  1626. }
  1627. #ifdef CONFIG_KALLSYMS
  1628. /* lookup symbol in given range of kernel_symbols */
  1629. static const struct kernel_symbol *lookup_symbol(const char *name,
  1630. const struct kernel_symbol *start,
  1631. const struct kernel_symbol *stop)
  1632. {
  1633. const struct kernel_symbol *ks = start;
  1634. for (; ks < stop; ks++)
  1635. if (strcmp(ks->name, name) == 0)
  1636. return ks;
  1637. return NULL;
  1638. }
  1639. static int is_exported(const char *name, unsigned long value,
  1640. const struct module *mod)
  1641. {
  1642. const struct kernel_symbol *ks;
  1643. if (!mod)
  1644. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  1645. else
  1646. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  1647. return ks != NULL && ks->value == value;
  1648. }
  1649. /* As per nm */
  1650. static char elf_type(const Elf_Sym *sym, const struct load_info *info)
  1651. {
  1652. const Elf_Shdr *sechdrs = info->sechdrs;
  1653. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1654. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1655. return 'v';
  1656. else
  1657. return 'w';
  1658. }
  1659. if (sym->st_shndx == SHN_UNDEF)
  1660. return 'U';
  1661. if (sym->st_shndx == SHN_ABS)
  1662. return 'a';
  1663. if (sym->st_shndx >= SHN_LORESERVE)
  1664. return '?';
  1665. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1666. return 't';
  1667. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1668. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1669. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1670. return 'r';
  1671. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1672. return 'g';
  1673. else
  1674. return 'd';
  1675. }
  1676. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1677. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1678. return 's';
  1679. else
  1680. return 'b';
  1681. }
  1682. if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
  1683. ".debug")) {
  1684. return 'n';
  1685. }
  1686. return '?';
  1687. }
  1688. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  1689. unsigned int shnum)
  1690. {
  1691. const Elf_Shdr *sec;
  1692. if (src->st_shndx == SHN_UNDEF
  1693. || src->st_shndx >= shnum
  1694. || !src->st_name)
  1695. return false;
  1696. sec = sechdrs + src->st_shndx;
  1697. if (!(sec->sh_flags & SHF_ALLOC)
  1698. #ifndef CONFIG_KALLSYMS_ALL
  1699. || !(sec->sh_flags & SHF_EXECINSTR)
  1700. #endif
  1701. || (sec->sh_entsize & INIT_OFFSET_MASK))
  1702. return false;
  1703. return true;
  1704. }
  1705. static unsigned long layout_symtab(struct module *mod,
  1706. Elf_Shdr *sechdrs,
  1707. unsigned int symindex,
  1708. unsigned int strindex,
  1709. const Elf_Ehdr *hdr,
  1710. const char *secstrings,
  1711. unsigned long *pstroffs,
  1712. unsigned long *strmap)
  1713. {
  1714. unsigned long symoffs;
  1715. Elf_Shdr *symsect = sechdrs + symindex;
  1716. Elf_Shdr *strsect = sechdrs + strindex;
  1717. const Elf_Sym *src;
  1718. const char *strtab;
  1719. unsigned int i, nsrc, ndst;
  1720. /* Put symbol section at end of init part of module. */
  1721. symsect->sh_flags |= SHF_ALLOC;
  1722. symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
  1723. symindex) | INIT_OFFSET_MASK;
  1724. DEBUGP("\t%s\n", secstrings + symsect->sh_name);
  1725. src = (void *)hdr + symsect->sh_offset;
  1726. nsrc = symsect->sh_size / sizeof(*src);
  1727. strtab = (void *)hdr + strsect->sh_offset;
  1728. for (ndst = i = 1; i < nsrc; ++i, ++src)
  1729. if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
  1730. unsigned int j = src->st_name;
  1731. while(!__test_and_set_bit(j, strmap) && strtab[j])
  1732. ++j;
  1733. ++ndst;
  1734. }
  1735. /* Append room for core symbols at end of core part. */
  1736. symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
  1737. mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
  1738. /* Put string table section at end of init part of module. */
  1739. strsect->sh_flags |= SHF_ALLOC;
  1740. strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
  1741. strindex) | INIT_OFFSET_MASK;
  1742. DEBUGP("\t%s\n", secstrings + strsect->sh_name);
  1743. /* Append room for core symbols' strings at end of core part. */
  1744. *pstroffs = mod->core_size;
  1745. __set_bit(0, strmap);
  1746. mod->core_size += bitmap_weight(strmap, strsect->sh_size);
  1747. return symoffs;
  1748. }
  1749. static void add_kallsyms(struct module *mod, struct load_info *info,
  1750. unsigned long symoffs,
  1751. unsigned long stroffs,
  1752. unsigned long *strmap)
  1753. {
  1754. unsigned int i, ndst;
  1755. const Elf_Sym *src;
  1756. Elf_Sym *dst;
  1757. char *s;
  1758. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1759. mod->symtab = (void *)symsec->sh_addr;
  1760. mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
  1761. mod->strtab = info->strtab;
  1762. /* Set types up while we still have access to sections. */
  1763. for (i = 0; i < mod->num_symtab; i++)
  1764. mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
  1765. mod->core_symtab = dst = mod->module_core + symoffs;
  1766. src = mod->symtab;
  1767. *dst = *src;
  1768. for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
  1769. if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
  1770. continue;
  1771. dst[ndst] = *src;
  1772. dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
  1773. ++ndst;
  1774. }
  1775. mod->core_num_syms = ndst;
  1776. mod->core_strtab = s = mod->module_core + stroffs;
  1777. for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
  1778. if (test_bit(i, strmap))
  1779. *++s = mod->strtab[i];
  1780. }
  1781. #else
  1782. static inline unsigned long layout_symtab(struct module *mod,
  1783. Elf_Shdr *sechdrs,
  1784. unsigned int symindex,
  1785. unsigned int strindex,
  1786. const Elf_Ehdr *hdr,
  1787. const char *secstrings,
  1788. unsigned long *pstroffs,
  1789. unsigned long *strmap)
  1790. {
  1791. return 0;
  1792. }
  1793. static void add_kallsyms(struct module *mod, struct load_info *info,
  1794. unsigned long symoffs,
  1795. unsigned long stroffs,
  1796. unsigned long *strmap)
  1797. {
  1798. }
  1799. #endif /* CONFIG_KALLSYMS */
  1800. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  1801. {
  1802. #ifdef CONFIG_DYNAMIC_DEBUG
  1803. if (ddebug_add_module(debug, num, debug->modname))
  1804. printk(KERN_ERR "dynamic debug error adding module: %s\n",
  1805. debug->modname);
  1806. #endif
  1807. }
  1808. static void dynamic_debug_remove(struct _ddebug *debug)
  1809. {
  1810. if (debug)
  1811. ddebug_remove_module(debug->modname);
  1812. }
  1813. static void *module_alloc_update_bounds(unsigned long size)
  1814. {
  1815. void *ret = module_alloc(size);
  1816. if (ret) {
  1817. mutex_lock(&module_mutex);
  1818. /* Update module bounds. */
  1819. if ((unsigned long)ret < module_addr_min)
  1820. module_addr_min = (unsigned long)ret;
  1821. if ((unsigned long)ret + size > module_addr_max)
  1822. module_addr_max = (unsigned long)ret + size;
  1823. mutex_unlock(&module_mutex);
  1824. }
  1825. return ret;
  1826. }
  1827. #ifdef CONFIG_DEBUG_KMEMLEAK
  1828. static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1829. const Elf_Shdr *sechdrs,
  1830. const char *secstrings)
  1831. {
  1832. unsigned int i;
  1833. /* only scan the sections containing data */
  1834. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  1835. for (i = 1; i < hdr->e_shnum; i++) {
  1836. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1837. continue;
  1838. if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
  1839. && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
  1840. continue;
  1841. kmemleak_scan_area((void *)sechdrs[i].sh_addr,
  1842. sechdrs[i].sh_size, GFP_KERNEL);
  1843. }
  1844. }
  1845. #else
  1846. static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1847. Elf_Shdr *sechdrs,
  1848. const char *secstrings)
  1849. {
  1850. }
  1851. #endif
  1852. /* Sets info->hdr and info->len. */
  1853. static int copy_and_check(struct load_info *info, const void __user *umod, unsigned long len)
  1854. {
  1855. int err;
  1856. Elf_Ehdr *hdr;
  1857. if (len < sizeof(*hdr))
  1858. return -ENOEXEC;
  1859. /* Suck in entire file: we'll want most of it. */
  1860. /* vmalloc barfs on "unusual" numbers. Check here */
  1861. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1862. return -ENOMEM;
  1863. if (copy_from_user(hdr, umod, len) != 0) {
  1864. err = -EFAULT;
  1865. goto free_hdr;
  1866. }
  1867. /* Sanity checks against insmoding binaries or wrong arch,
  1868. weird elf version */
  1869. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1870. || hdr->e_type != ET_REL
  1871. || !elf_check_arch(hdr)
  1872. || hdr->e_shentsize != sizeof(Elf_Shdr)) {
  1873. err = -ENOEXEC;
  1874. goto free_hdr;
  1875. }
  1876. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  1877. err = -ENOEXEC;
  1878. goto free_hdr;
  1879. }
  1880. info->hdr = hdr;
  1881. info->len = len;
  1882. return 0;
  1883. free_hdr:
  1884. vfree(hdr);
  1885. return err;
  1886. }
  1887. static int rewrite_section_headers(struct load_info *info)
  1888. {
  1889. unsigned int i;
  1890. /* This should always be true, but let's be sure. */
  1891. info->sechdrs[0].sh_addr = 0;
  1892. for (i = 1; i < info->hdr->e_shnum; i++) {
  1893. Elf_Shdr *shdr = &info->sechdrs[i];
  1894. if (shdr->sh_type != SHT_NOBITS
  1895. && info->len < shdr->sh_offset + shdr->sh_size) {
  1896. printk(KERN_ERR "Module len %lu truncated\n",
  1897. info->len);
  1898. return -ENOEXEC;
  1899. }
  1900. /* Mark all sections sh_addr with their address in the
  1901. temporary image. */
  1902. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  1903. #ifndef CONFIG_MODULE_UNLOAD
  1904. /* Don't load .exit sections */
  1905. if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
  1906. shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
  1907. #endif
  1908. }
  1909. /* Track but don't keep modinfo and version sections. */
  1910. info->index.vers = find_sec(info->hdr, info->sechdrs, info->secstrings, "__versions");
  1911. info->index.info = find_sec(info->hdr, info->sechdrs, info->secstrings, ".modinfo");
  1912. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1913. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1914. return 0;
  1915. }
  1916. /*
  1917. * Set up our basic convenience variables (pointers to section headers,
  1918. * search for module section index etc), and do some basic section
  1919. * verification.
  1920. *
  1921. * Return the temporary module pointer (we'll replace it with the final
  1922. * one when we move the module sections around).
  1923. */
  1924. static struct module *setup_load_info(struct load_info *info)
  1925. {
  1926. unsigned int i;
  1927. int err;
  1928. struct module *mod;
  1929. /* Set up the convenience variables */
  1930. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  1931. info->secstrings = (void *)info->hdr
  1932. + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
  1933. err = rewrite_section_headers(info);
  1934. if (err)
  1935. return ERR_PTR(err);
  1936. /* Find internal symbols and strings. */
  1937. for (i = 1; i < info->hdr->e_shnum; i++) {
  1938. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  1939. info->index.sym = i;
  1940. info->index.str = info->sechdrs[i].sh_link;
  1941. info->strtab = (char *)info->hdr
  1942. + info->sechdrs[info->index.str].sh_offset;
  1943. break;
  1944. }
  1945. }
  1946. info->index.mod = find_sec(info->hdr, info->sechdrs, info->secstrings,
  1947. ".gnu.linkonce.this_module");
  1948. if (!info->index.mod) {
  1949. printk(KERN_WARNING "No module found in object\n");
  1950. return ERR_PTR(-ENOEXEC);
  1951. }
  1952. /* This is temporary: point mod into copy of data. */
  1953. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  1954. if (info->index.sym == 0) {
  1955. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1956. mod->name);
  1957. return ERR_PTR(-ENOEXEC);
  1958. }
  1959. info->index.pcpu = find_pcpusec(info->hdr, info->sechdrs, info->secstrings);
  1960. /* Check module struct version now, before we try to use module. */
  1961. if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
  1962. return ERR_PTR(-ENOEXEC);
  1963. return mod;
  1964. }
  1965. static int check_modinfo(struct module *mod,
  1966. const Elf_Shdr *sechdrs,
  1967. unsigned int infoindex, unsigned int versindex)
  1968. {
  1969. const char *modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1970. int err;
  1971. /* This is allowed: modprobe --force will invalidate it. */
  1972. if (!modmagic) {
  1973. err = try_to_force_load(mod, "bad vermagic");
  1974. if (err)
  1975. return err;
  1976. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1977. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1978. mod->name, modmagic, vermagic);
  1979. return -ENOEXEC;
  1980. }
  1981. if (get_modinfo(sechdrs, infoindex, "staging")) {
  1982. add_taint_module(mod, TAINT_CRAP);
  1983. printk(KERN_WARNING "%s: module is from the staging directory,"
  1984. " the quality is unknown, you have been warned.\n",
  1985. mod->name);
  1986. }
  1987. /* Set up license info based on the info section */
  1988. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1989. return 0;
  1990. }
  1991. static void find_module_sections(struct module *mod, Elf_Ehdr *hdr,
  1992. Elf_Shdr *sechdrs, const char *secstrings)
  1993. {
  1994. mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
  1995. sizeof(*mod->kp), &mod->num_kp);
  1996. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  1997. sizeof(*mod->syms), &mod->num_syms);
  1998. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  1999. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  2000. sizeof(*mod->gpl_syms),
  2001. &mod->num_gpl_syms);
  2002. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  2003. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  2004. "__ksymtab_gpl_future",
  2005. sizeof(*mod->gpl_future_syms),
  2006. &mod->num_gpl_future_syms);
  2007. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  2008. "__kcrctab_gpl_future");
  2009. #ifdef CONFIG_UNUSED_SYMBOLS
  2010. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  2011. "__ksymtab_unused",
  2012. sizeof(*mod->unused_syms),
  2013. &mod->num_unused_syms);
  2014. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  2015. "__kcrctab_unused");
  2016. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  2017. "__ksymtab_unused_gpl",
  2018. sizeof(*mod->unused_gpl_syms),
  2019. &mod->num_unused_gpl_syms);
  2020. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  2021. "__kcrctab_unused_gpl");
  2022. #endif
  2023. #ifdef CONFIG_CONSTRUCTORS
  2024. mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
  2025. sizeof(*mod->ctors), &mod->num_ctors);
  2026. #endif
  2027. #ifdef CONFIG_TRACEPOINTS
  2028. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  2029. "__tracepoints",
  2030. sizeof(*mod->tracepoints),
  2031. &mod->num_tracepoints);
  2032. #endif
  2033. #ifdef CONFIG_EVENT_TRACING
  2034. mod->trace_events = section_objs(hdr, sechdrs, secstrings,
  2035. "_ftrace_events",
  2036. sizeof(*mod->trace_events),
  2037. &mod->num_trace_events);
  2038. /*
  2039. * This section contains pointers to allocated objects in the trace
  2040. * code and not scanning it leads to false positives.
  2041. */
  2042. kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
  2043. mod->num_trace_events, GFP_KERNEL);
  2044. #endif
  2045. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2046. /* sechdrs[0].sh_size is always zero */
  2047. mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
  2048. "__mcount_loc",
  2049. sizeof(*mod->ftrace_callsites),
  2050. &mod->num_ftrace_callsites);
  2051. #endif
  2052. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  2053. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  2054. mod->name);
  2055. }
  2056. static struct module *move_module(struct module *mod,
  2057. Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  2058. const char *secstrings, unsigned modindex)
  2059. {
  2060. int i;
  2061. void *ptr;
  2062. /* Do the allocs. */
  2063. ptr = module_alloc_update_bounds(mod->core_size);
  2064. /*
  2065. * The pointer to this block is stored in the module structure
  2066. * which is inside the block. Just mark it as not being a
  2067. * leak.
  2068. */
  2069. kmemleak_not_leak(ptr);
  2070. if (!ptr)
  2071. return ERR_PTR(-ENOMEM);
  2072. memset(ptr, 0, mod->core_size);
  2073. mod->module_core = ptr;
  2074. ptr = module_alloc_update_bounds(mod->init_size);
  2075. /*
  2076. * The pointer to this block is stored in the module structure
  2077. * which is inside the block. This block doesn't need to be
  2078. * scanned as it contains data and code that will be freed
  2079. * after the module is initialized.
  2080. */
  2081. kmemleak_ignore(ptr);
  2082. if (!ptr && mod->init_size) {
  2083. module_free(mod, mod->module_core);
  2084. return ERR_PTR(-ENOMEM);
  2085. }
  2086. memset(ptr, 0, mod->init_size);
  2087. mod->module_init = ptr;
  2088. /* Transfer each section which specifies SHF_ALLOC */
  2089. DEBUGP("final section addresses:\n");
  2090. for (i = 0; i < hdr->e_shnum; i++) {
  2091. void *dest;
  2092. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  2093. continue;
  2094. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  2095. dest = mod->module_init
  2096. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  2097. else
  2098. dest = mod->module_core + sechdrs[i].sh_entsize;
  2099. if (sechdrs[i].sh_type != SHT_NOBITS)
  2100. memcpy(dest, (void *)sechdrs[i].sh_addr,
  2101. sechdrs[i].sh_size);
  2102. /* Update sh_addr to point to copy in image. */
  2103. sechdrs[i].sh_addr = (unsigned long)dest;
  2104. DEBUGP("\t0x%lx %s\n",
  2105. sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  2106. }
  2107. /* Module has been moved. */
  2108. mod = (void *)sechdrs[modindex].sh_addr;
  2109. kmemleak_load_module(mod, hdr, sechdrs, secstrings);
  2110. return mod;
  2111. }
  2112. static int check_module_license_and_versions(struct module *mod,
  2113. Elf_Shdr *sechdrs)
  2114. {
  2115. /*
  2116. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2117. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2118. * using GPL-only symbols it needs.
  2119. */
  2120. if (strcmp(mod->name, "ndiswrapper") == 0)
  2121. add_taint(TAINT_PROPRIETARY_MODULE);
  2122. /* driverloader was caught wrongly pretending to be under GPL */
  2123. if (strcmp(mod->name, "driverloader") == 0)
  2124. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  2125. #ifdef CONFIG_MODVERSIONS
  2126. if ((mod->num_syms && !mod->crcs)
  2127. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2128. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2129. #ifdef CONFIG_UNUSED_SYMBOLS
  2130. || (mod->num_unused_syms && !mod->unused_crcs)
  2131. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2132. #endif
  2133. ) {
  2134. return try_to_force_load(mod,
  2135. "no versions for exported symbols");
  2136. }
  2137. #endif
  2138. return 0;
  2139. }
  2140. static void flush_module_icache(const struct module *mod)
  2141. {
  2142. mm_segment_t old_fs;
  2143. /* flush the icache in correct context */
  2144. old_fs = get_fs();
  2145. set_fs(KERNEL_DS);
  2146. /*
  2147. * Flush the instruction cache, since we've played with text.
  2148. * Do it before processing of module parameters, so the module
  2149. * can provide parameter accessor functions of its own.
  2150. */
  2151. if (mod->module_init)
  2152. flush_icache_range((unsigned long)mod->module_init,
  2153. (unsigned long)mod->module_init
  2154. + mod->init_size);
  2155. flush_icache_range((unsigned long)mod->module_core,
  2156. (unsigned long)mod->module_core + mod->core_size);
  2157. set_fs(old_fs);
  2158. }
  2159. /* Allocate and load the module: note that size of section 0 is always
  2160. zero, and we rely on this for optional sections. */
  2161. static noinline struct module *load_module(void __user *umod,
  2162. unsigned long len,
  2163. const char __user *uargs)
  2164. {
  2165. struct load_info info = { NULL, };
  2166. struct module *mod;
  2167. long err;
  2168. unsigned long symoffs, stroffs, *strmap;
  2169. void __percpu *percpu;
  2170. struct _ddebug *debug = NULL;
  2171. unsigned int num_debug = 0;
  2172. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  2173. umod, len, uargs);
  2174. err = copy_and_check(&info, umod, len);
  2175. if (err)
  2176. return ERR_PTR(err);
  2177. mod = setup_load_info(&info);
  2178. if (IS_ERR(mod)) {
  2179. err = PTR_ERR(mod);
  2180. goto free_hdr;
  2181. }
  2182. err = check_modinfo(mod, info.sechdrs, info.index.info, info.index.vers);
  2183. if (err)
  2184. goto free_hdr;
  2185. /* Now copy in args */
  2186. info.args = strndup_user(uargs, ~0UL >> 1);
  2187. if (IS_ERR(info.args)) {
  2188. err = PTR_ERR(info.args);
  2189. goto free_hdr;
  2190. }
  2191. strmap = kzalloc(BITS_TO_LONGS(info.sechdrs[info.index.str].sh_size)
  2192. * sizeof(long), GFP_KERNEL);
  2193. if (!strmap) {
  2194. err = -ENOMEM;
  2195. goto free_mod;
  2196. }
  2197. mod->state = MODULE_STATE_COMING;
  2198. /* Allow arches to frob section contents and sizes. */
  2199. err = module_frob_arch_sections(info.hdr, info.sechdrs, info.secstrings, mod);
  2200. if (err < 0)
  2201. goto free_mod;
  2202. if (info.index.pcpu) {
  2203. /* We have a special allocation for this section. */
  2204. err = percpu_modalloc(mod, info.sechdrs[info.index.pcpu].sh_size,
  2205. info.sechdrs[info.index.pcpu].sh_addralign);
  2206. if (err)
  2207. goto free_mod;
  2208. info.sechdrs[info.index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2209. }
  2210. /* Keep this around for failure path. */
  2211. percpu = mod_percpu(mod);
  2212. /* Determine total sizes, and put offsets in sh_entsize. For now
  2213. this is done generically; there doesn't appear to be any
  2214. special cases for the architectures. */
  2215. layout_sections(mod, info.hdr, info.sechdrs, info.secstrings);
  2216. symoffs = layout_symtab(mod, info.sechdrs, info.index.sym, info.index.str, info.hdr,
  2217. info.secstrings, &stroffs, strmap);
  2218. /* Allocate and move to the final place */
  2219. mod = move_module(mod, info.hdr, info.sechdrs, info.secstrings, info.index.mod);
  2220. if (IS_ERR(mod)) {
  2221. err = PTR_ERR(mod);
  2222. goto free_percpu;
  2223. }
  2224. /* Now we've moved module, initialize linked lists, etc. */
  2225. err = module_unload_init(mod);
  2226. if (err)
  2227. goto free_init;
  2228. /* Now we've got everything in the final locations, we can
  2229. * find optional sections. */
  2230. find_module_sections(mod, info.hdr, info.sechdrs, info.secstrings);
  2231. err = check_module_license_and_versions(mod, info.sechdrs);
  2232. if (err)
  2233. goto free_unload;
  2234. /* Set up MODINFO_ATTR fields */
  2235. setup_modinfo(mod, info.sechdrs, info.index.info);
  2236. /* Fix up syms, so that st_value is a pointer to location. */
  2237. err = simplify_symbols(info.sechdrs, info.index.sym, info.strtab, info.index.vers, info.index.pcpu,
  2238. mod);
  2239. if (err < 0)
  2240. goto cleanup;
  2241. err = apply_relocations(mod, info.hdr, info.sechdrs, info.index.sym, info.index.str);
  2242. if (err < 0)
  2243. goto cleanup;
  2244. /* Set up and sort exception table */
  2245. mod->extable = section_objs(info.hdr, info.sechdrs, info.secstrings, "__ex_table",
  2246. sizeof(*mod->extable), &mod->num_exentries);
  2247. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2248. /* Finally, copy percpu area over. */
  2249. percpu_modcopy(mod, (void *)info.sechdrs[info.index.pcpu].sh_addr,
  2250. info.sechdrs[info.index.pcpu].sh_size);
  2251. add_kallsyms(mod, &info, symoffs, stroffs, strmap);
  2252. kfree(strmap);
  2253. strmap = NULL;
  2254. if (!mod->taints)
  2255. debug = section_objs(info.hdr, info.sechdrs, info.secstrings, "__verbose",
  2256. sizeof(*debug), &num_debug);
  2257. err = module_finalize(info.hdr, info.sechdrs, mod);
  2258. if (err < 0)
  2259. goto cleanup;
  2260. flush_module_icache(mod);
  2261. mod->args = info.args;
  2262. /* Now sew it into the lists so we can get lockdep and oops
  2263. * info during argument parsing. Noone should access us, since
  2264. * strong_try_module_get() will fail.
  2265. * lockdep/oops can run asynchronous, so use the RCU list insertion
  2266. * function to insert in a way safe to concurrent readers.
  2267. * The mutex protects against concurrent writers.
  2268. */
  2269. mutex_lock(&module_mutex);
  2270. if (find_module(mod->name)) {
  2271. err = -EEXIST;
  2272. goto unlock;
  2273. }
  2274. if (debug)
  2275. dynamic_debug_setup(debug, num_debug);
  2276. /* Find duplicate symbols */
  2277. err = verify_export_symbols(mod);
  2278. if (err < 0)
  2279. goto ddebug;
  2280. list_add_rcu(&mod->list, &modules);
  2281. mutex_unlock(&module_mutex);
  2282. err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
  2283. if (err < 0)
  2284. goto unlink;
  2285. err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
  2286. if (err < 0)
  2287. goto unlink;
  2288. add_sect_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
  2289. add_notes_attrs(mod, info.hdr->e_shnum, info.secstrings, info.sechdrs);
  2290. /* Get rid of temporary copy */
  2291. vfree(info.hdr);
  2292. trace_module_load(mod);
  2293. /* Done! */
  2294. return mod;
  2295. unlink:
  2296. mutex_lock(&module_mutex);
  2297. /* Unlink carefully: kallsyms could be walking list. */
  2298. list_del_rcu(&mod->list);
  2299. ddebug:
  2300. dynamic_debug_remove(debug);
  2301. unlock:
  2302. mutex_unlock(&module_mutex);
  2303. synchronize_sched();
  2304. module_arch_cleanup(mod);
  2305. cleanup:
  2306. free_modinfo(mod);
  2307. free_unload:
  2308. module_unload_free(mod);
  2309. free_init:
  2310. module_free(mod, mod->module_init);
  2311. module_free(mod, mod->module_core);
  2312. /* mod will be freed with core. Don't access it beyond this line! */
  2313. free_percpu:
  2314. free_percpu(percpu);
  2315. free_mod:
  2316. kfree(info.args);
  2317. kfree(strmap);
  2318. free_hdr:
  2319. vfree(info.hdr);
  2320. return ERR_PTR(err);
  2321. }
  2322. /* Call module constructors. */
  2323. static void do_mod_ctors(struct module *mod)
  2324. {
  2325. #ifdef CONFIG_CONSTRUCTORS
  2326. unsigned long i;
  2327. for (i = 0; i < mod->num_ctors; i++)
  2328. mod->ctors[i]();
  2329. #endif
  2330. }
  2331. /* This is where the real work happens */
  2332. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2333. unsigned long, len, const char __user *, uargs)
  2334. {
  2335. struct module *mod;
  2336. int ret = 0;
  2337. /* Must have permission */
  2338. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2339. return -EPERM;
  2340. /* Do all the hard work */
  2341. mod = load_module(umod, len, uargs);
  2342. if (IS_ERR(mod))
  2343. return PTR_ERR(mod);
  2344. blocking_notifier_call_chain(&module_notify_list,
  2345. MODULE_STATE_COMING, mod);
  2346. do_mod_ctors(mod);
  2347. /* Start the module */
  2348. if (mod->init != NULL)
  2349. ret = do_one_initcall(mod->init);
  2350. if (ret < 0) {
  2351. /* Init routine failed: abort. Try to protect us from
  2352. buggy refcounters. */
  2353. mod->state = MODULE_STATE_GOING;
  2354. synchronize_sched();
  2355. module_put(mod);
  2356. blocking_notifier_call_chain(&module_notify_list,
  2357. MODULE_STATE_GOING, mod);
  2358. free_module(mod);
  2359. wake_up(&module_wq);
  2360. return ret;
  2361. }
  2362. if (ret > 0) {
  2363. printk(KERN_WARNING
  2364. "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
  2365. "%s: loading module anyway...\n",
  2366. __func__, mod->name, ret,
  2367. __func__);
  2368. dump_stack();
  2369. }
  2370. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2371. mod->state = MODULE_STATE_LIVE;
  2372. wake_up(&module_wq);
  2373. blocking_notifier_call_chain(&module_notify_list,
  2374. MODULE_STATE_LIVE, mod);
  2375. /* We need to finish all async code before the module init sequence is done */
  2376. async_synchronize_full();
  2377. mutex_lock(&module_mutex);
  2378. /* Drop initial reference. */
  2379. module_put(mod);
  2380. trim_init_extable(mod);
  2381. #ifdef CONFIG_KALLSYMS
  2382. mod->num_symtab = mod->core_num_syms;
  2383. mod->symtab = mod->core_symtab;
  2384. mod->strtab = mod->core_strtab;
  2385. #endif
  2386. module_free(mod, mod->module_init);
  2387. mod->module_init = NULL;
  2388. mod->init_size = 0;
  2389. mod->init_text_size = 0;
  2390. mutex_unlock(&module_mutex);
  2391. return 0;
  2392. }
  2393. static inline int within(unsigned long addr, void *start, unsigned long size)
  2394. {
  2395. return ((void *)addr >= start && (void *)addr < start + size);
  2396. }
  2397. #ifdef CONFIG_KALLSYMS
  2398. /*
  2399. * This ignores the intensely annoying "mapping symbols" found
  2400. * in ARM ELF files: $a, $t and $d.
  2401. */
  2402. static inline int is_arm_mapping_symbol(const char *str)
  2403. {
  2404. return str[0] == '$' && strchr("atd", str[1])
  2405. && (str[2] == '\0' || str[2] == '.');
  2406. }
  2407. static const char *get_ksymbol(struct module *mod,
  2408. unsigned long addr,
  2409. unsigned long *size,
  2410. unsigned long *offset)
  2411. {
  2412. unsigned int i, best = 0;
  2413. unsigned long nextval;
  2414. /* At worse, next value is at end of module */
  2415. if (within_module_init(addr, mod))
  2416. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2417. else
  2418. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2419. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2420. starts real symbols at 1). */
  2421. for (i = 1; i < mod->num_symtab; i++) {
  2422. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2423. continue;
  2424. /* We ignore unnamed symbols: they're uninformative
  2425. * and inserted at a whim. */
  2426. if (mod->symtab[i].st_value <= addr
  2427. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2428. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2429. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2430. best = i;
  2431. if (mod->symtab[i].st_value > addr
  2432. && mod->symtab[i].st_value < nextval
  2433. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2434. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2435. nextval = mod->symtab[i].st_value;
  2436. }
  2437. if (!best)
  2438. return NULL;
  2439. if (size)
  2440. *size = nextval - mod->symtab[best].st_value;
  2441. if (offset)
  2442. *offset = addr - mod->symtab[best].st_value;
  2443. return mod->strtab + mod->symtab[best].st_name;
  2444. }
  2445. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2446. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2447. const char *module_address_lookup(unsigned long addr,
  2448. unsigned long *size,
  2449. unsigned long *offset,
  2450. char **modname,
  2451. char *namebuf)
  2452. {
  2453. struct module *mod;
  2454. const char *ret = NULL;
  2455. preempt_disable();
  2456. list_for_each_entry_rcu(mod, &modules, list) {
  2457. if (within_module_init(addr, mod) ||
  2458. within_module_core(addr, mod)) {
  2459. if (modname)
  2460. *modname = mod->name;
  2461. ret = get_ksymbol(mod, addr, size, offset);
  2462. break;
  2463. }
  2464. }
  2465. /* Make a copy in here where it's safe */
  2466. if (ret) {
  2467. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2468. ret = namebuf;
  2469. }
  2470. preempt_enable();
  2471. return ret;
  2472. }
  2473. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2474. {
  2475. struct module *mod;
  2476. preempt_disable();
  2477. list_for_each_entry_rcu(mod, &modules, list) {
  2478. if (within_module_init(addr, mod) ||
  2479. within_module_core(addr, mod)) {
  2480. const char *sym;
  2481. sym = get_ksymbol(mod, addr, NULL, NULL);
  2482. if (!sym)
  2483. goto out;
  2484. strlcpy(symname, sym, KSYM_NAME_LEN);
  2485. preempt_enable();
  2486. return 0;
  2487. }
  2488. }
  2489. out:
  2490. preempt_enable();
  2491. return -ERANGE;
  2492. }
  2493. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2494. unsigned long *offset, char *modname, char *name)
  2495. {
  2496. struct module *mod;
  2497. preempt_disable();
  2498. list_for_each_entry_rcu(mod, &modules, list) {
  2499. if (within_module_init(addr, mod) ||
  2500. within_module_core(addr, mod)) {
  2501. const char *sym;
  2502. sym = get_ksymbol(mod, addr, size, offset);
  2503. if (!sym)
  2504. goto out;
  2505. if (modname)
  2506. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2507. if (name)
  2508. strlcpy(name, sym, KSYM_NAME_LEN);
  2509. preempt_enable();
  2510. return 0;
  2511. }
  2512. }
  2513. out:
  2514. preempt_enable();
  2515. return -ERANGE;
  2516. }
  2517. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2518. char *name, char *module_name, int *exported)
  2519. {
  2520. struct module *mod;
  2521. preempt_disable();
  2522. list_for_each_entry_rcu(mod, &modules, list) {
  2523. if (symnum < mod->num_symtab) {
  2524. *value = mod->symtab[symnum].st_value;
  2525. *type = mod->symtab[symnum].st_info;
  2526. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2527. KSYM_NAME_LEN);
  2528. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2529. *exported = is_exported(name, *value, mod);
  2530. preempt_enable();
  2531. return 0;
  2532. }
  2533. symnum -= mod->num_symtab;
  2534. }
  2535. preempt_enable();
  2536. return -ERANGE;
  2537. }
  2538. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2539. {
  2540. unsigned int i;
  2541. for (i = 0; i < mod->num_symtab; i++)
  2542. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2543. mod->symtab[i].st_info != 'U')
  2544. return mod->symtab[i].st_value;
  2545. return 0;
  2546. }
  2547. /* Look for this name: can be of form module:name. */
  2548. unsigned long module_kallsyms_lookup_name(const char *name)
  2549. {
  2550. struct module *mod;
  2551. char *colon;
  2552. unsigned long ret = 0;
  2553. /* Don't lock: we're in enough trouble already. */
  2554. preempt_disable();
  2555. if ((colon = strchr(name, ':')) != NULL) {
  2556. *colon = '\0';
  2557. if ((mod = find_module(name)) != NULL)
  2558. ret = mod_find_symname(mod, colon+1);
  2559. *colon = ':';
  2560. } else {
  2561. list_for_each_entry_rcu(mod, &modules, list)
  2562. if ((ret = mod_find_symname(mod, name)) != 0)
  2563. break;
  2564. }
  2565. preempt_enable();
  2566. return ret;
  2567. }
  2568. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  2569. struct module *, unsigned long),
  2570. void *data)
  2571. {
  2572. struct module *mod;
  2573. unsigned int i;
  2574. int ret;
  2575. list_for_each_entry(mod, &modules, list) {
  2576. for (i = 0; i < mod->num_symtab; i++) {
  2577. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  2578. mod, mod->symtab[i].st_value);
  2579. if (ret != 0)
  2580. return ret;
  2581. }
  2582. }
  2583. return 0;
  2584. }
  2585. #endif /* CONFIG_KALLSYMS */
  2586. static char *module_flags(struct module *mod, char *buf)
  2587. {
  2588. int bx = 0;
  2589. if (mod->taints ||
  2590. mod->state == MODULE_STATE_GOING ||
  2591. mod->state == MODULE_STATE_COMING) {
  2592. buf[bx++] = '(';
  2593. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2594. buf[bx++] = 'P';
  2595. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2596. buf[bx++] = 'F';
  2597. if (mod->taints & (1 << TAINT_CRAP))
  2598. buf[bx++] = 'C';
  2599. /*
  2600. * TAINT_FORCED_RMMOD: could be added.
  2601. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2602. * apply to modules.
  2603. */
  2604. /* Show a - for module-is-being-unloaded */
  2605. if (mod->state == MODULE_STATE_GOING)
  2606. buf[bx++] = '-';
  2607. /* Show a + for module-is-being-loaded */
  2608. if (mod->state == MODULE_STATE_COMING)
  2609. buf[bx++] = '+';
  2610. buf[bx++] = ')';
  2611. }
  2612. buf[bx] = '\0';
  2613. return buf;
  2614. }
  2615. #ifdef CONFIG_PROC_FS
  2616. /* Called by the /proc file system to return a list of modules. */
  2617. static void *m_start(struct seq_file *m, loff_t *pos)
  2618. {
  2619. mutex_lock(&module_mutex);
  2620. return seq_list_start(&modules, *pos);
  2621. }
  2622. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2623. {
  2624. return seq_list_next(p, &modules, pos);
  2625. }
  2626. static void m_stop(struct seq_file *m, void *p)
  2627. {
  2628. mutex_unlock(&module_mutex);
  2629. }
  2630. static int m_show(struct seq_file *m, void *p)
  2631. {
  2632. struct module *mod = list_entry(p, struct module, list);
  2633. char buf[8];
  2634. seq_printf(m, "%s %u",
  2635. mod->name, mod->init_size + mod->core_size);
  2636. print_unload_info(m, mod);
  2637. /* Informative for users. */
  2638. seq_printf(m, " %s",
  2639. mod->state == MODULE_STATE_GOING ? "Unloading":
  2640. mod->state == MODULE_STATE_COMING ? "Loading":
  2641. "Live");
  2642. /* Used by oprofile and other similar tools. */
  2643. seq_printf(m, " 0x%p", mod->module_core);
  2644. /* Taints info */
  2645. if (mod->taints)
  2646. seq_printf(m, " %s", module_flags(mod, buf));
  2647. seq_printf(m, "\n");
  2648. return 0;
  2649. }
  2650. /* Format: modulename size refcount deps address
  2651. Where refcount is a number or -, and deps is a comma-separated list
  2652. of depends or -.
  2653. */
  2654. static const struct seq_operations modules_op = {
  2655. .start = m_start,
  2656. .next = m_next,
  2657. .stop = m_stop,
  2658. .show = m_show
  2659. };
  2660. static int modules_open(struct inode *inode, struct file *file)
  2661. {
  2662. return seq_open(file, &modules_op);
  2663. }
  2664. static const struct file_operations proc_modules_operations = {
  2665. .open = modules_open,
  2666. .read = seq_read,
  2667. .llseek = seq_lseek,
  2668. .release = seq_release,
  2669. };
  2670. static int __init proc_modules_init(void)
  2671. {
  2672. proc_create("modules", 0, NULL, &proc_modules_operations);
  2673. return 0;
  2674. }
  2675. module_init(proc_modules_init);
  2676. #endif
  2677. /* Given an address, look for it in the module exception tables. */
  2678. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2679. {
  2680. const struct exception_table_entry *e = NULL;
  2681. struct module *mod;
  2682. preempt_disable();
  2683. list_for_each_entry_rcu(mod, &modules, list) {
  2684. if (mod->num_exentries == 0)
  2685. continue;
  2686. e = search_extable(mod->extable,
  2687. mod->extable + mod->num_exentries - 1,
  2688. addr);
  2689. if (e)
  2690. break;
  2691. }
  2692. preempt_enable();
  2693. /* Now, if we found one, we are running inside it now, hence
  2694. we cannot unload the module, hence no refcnt needed. */
  2695. return e;
  2696. }
  2697. /*
  2698. * is_module_address - is this address inside a module?
  2699. * @addr: the address to check.
  2700. *
  2701. * See is_module_text_address() if you simply want to see if the address
  2702. * is code (not data).
  2703. */
  2704. bool is_module_address(unsigned long addr)
  2705. {
  2706. bool ret;
  2707. preempt_disable();
  2708. ret = __module_address(addr) != NULL;
  2709. preempt_enable();
  2710. return ret;
  2711. }
  2712. /*
  2713. * __module_address - get the module which contains an address.
  2714. * @addr: the address.
  2715. *
  2716. * Must be called with preempt disabled or module mutex held so that
  2717. * module doesn't get freed during this.
  2718. */
  2719. struct module *__module_address(unsigned long addr)
  2720. {
  2721. struct module *mod;
  2722. if (addr < module_addr_min || addr > module_addr_max)
  2723. return NULL;
  2724. list_for_each_entry_rcu(mod, &modules, list)
  2725. if (within_module_core(addr, mod)
  2726. || within_module_init(addr, mod))
  2727. return mod;
  2728. return NULL;
  2729. }
  2730. EXPORT_SYMBOL_GPL(__module_address);
  2731. /*
  2732. * is_module_text_address - is this address inside module code?
  2733. * @addr: the address to check.
  2734. *
  2735. * See is_module_address() if you simply want to see if the address is
  2736. * anywhere in a module. See kernel_text_address() for testing if an
  2737. * address corresponds to kernel or module code.
  2738. */
  2739. bool is_module_text_address(unsigned long addr)
  2740. {
  2741. bool ret;
  2742. preempt_disable();
  2743. ret = __module_text_address(addr) != NULL;
  2744. preempt_enable();
  2745. return ret;
  2746. }
  2747. /*
  2748. * __module_text_address - get the module whose code contains an address.
  2749. * @addr: the address.
  2750. *
  2751. * Must be called with preempt disabled or module mutex held so that
  2752. * module doesn't get freed during this.
  2753. */
  2754. struct module *__module_text_address(unsigned long addr)
  2755. {
  2756. struct module *mod = __module_address(addr);
  2757. if (mod) {
  2758. /* Make sure it's within the text section. */
  2759. if (!within(addr, mod->module_init, mod->init_text_size)
  2760. && !within(addr, mod->module_core, mod->core_text_size))
  2761. mod = NULL;
  2762. }
  2763. return mod;
  2764. }
  2765. EXPORT_SYMBOL_GPL(__module_text_address);
  2766. /* Don't grab lock, we're oopsing. */
  2767. void print_modules(void)
  2768. {
  2769. struct module *mod;
  2770. char buf[8];
  2771. printk(KERN_DEFAULT "Modules linked in:");
  2772. /* Most callers should already have preempt disabled, but make sure */
  2773. preempt_disable();
  2774. list_for_each_entry_rcu(mod, &modules, list)
  2775. printk(" %s%s", mod->name, module_flags(mod, buf));
  2776. preempt_enable();
  2777. if (last_unloaded_module[0])
  2778. printk(" [last unloaded: %s]", last_unloaded_module);
  2779. printk("\n");
  2780. }
  2781. #ifdef CONFIG_MODVERSIONS
  2782. /* Generate the signature for all relevant module structures here.
  2783. * If these change, we don't want to try to parse the module. */
  2784. void module_layout(struct module *mod,
  2785. struct modversion_info *ver,
  2786. struct kernel_param *kp,
  2787. struct kernel_symbol *ks,
  2788. struct tracepoint *tp)
  2789. {
  2790. }
  2791. EXPORT_SYMBOL(module_layout);
  2792. #endif
  2793. #ifdef CONFIG_TRACEPOINTS
  2794. void module_update_tracepoints(void)
  2795. {
  2796. struct module *mod;
  2797. mutex_lock(&module_mutex);
  2798. list_for_each_entry(mod, &modules, list)
  2799. if (!mod->taints)
  2800. tracepoint_update_probe_range(mod->tracepoints,
  2801. mod->tracepoints + mod->num_tracepoints);
  2802. mutex_unlock(&module_mutex);
  2803. }
  2804. /*
  2805. * Returns 0 if current not found.
  2806. * Returns 1 if current found.
  2807. */
  2808. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2809. {
  2810. struct module *iter_mod;
  2811. int found = 0;
  2812. mutex_lock(&module_mutex);
  2813. list_for_each_entry(iter_mod, &modules, list) {
  2814. if (!iter_mod->taints) {
  2815. /*
  2816. * Sorted module list
  2817. */
  2818. if (iter_mod < iter->module)
  2819. continue;
  2820. else if (iter_mod > iter->module)
  2821. iter->tracepoint = NULL;
  2822. found = tracepoint_get_iter_range(&iter->tracepoint,
  2823. iter_mod->tracepoints,
  2824. iter_mod->tracepoints
  2825. + iter_mod->num_tracepoints);
  2826. if (found) {
  2827. iter->module = iter_mod;
  2828. break;
  2829. }
  2830. }
  2831. }
  2832. mutex_unlock(&module_mutex);
  2833. return found;
  2834. }
  2835. #endif