module.c 84 KB

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