module.c 90 KB

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