module.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508
  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_put(struct module *module)
  782. {
  783. if (module) {
  784. preempt_disable();
  785. smp_wmb(); /* see comment in module_refcount */
  786. __this_cpu_inc(module->refptr->decs);
  787. trace_module_put(module, _RET_IP_);
  788. /* Maybe they're waiting for us to drop reference? */
  789. if (unlikely(!module_is_live(module)))
  790. wake_up_process(module->waiter);
  791. preempt_enable();
  792. }
  793. }
  794. EXPORT_SYMBOL(module_put);
  795. #else /* !CONFIG_MODULE_UNLOAD */
  796. static inline void print_unload_info(struct seq_file *m, struct module *mod)
  797. {
  798. /* We don't know the usage count, or what modules are using. */
  799. seq_printf(m, " - -");
  800. }
  801. static inline void module_unload_free(struct module *mod)
  802. {
  803. }
  804. int ref_module(struct module *a, struct module *b)
  805. {
  806. return strong_try_module_get(b);
  807. }
  808. EXPORT_SYMBOL_GPL(ref_module);
  809. static inline int module_unload_init(struct module *mod)
  810. {
  811. return 0;
  812. }
  813. #endif /* CONFIG_MODULE_UNLOAD */
  814. static size_t module_flags_taint(struct module *mod, char *buf)
  815. {
  816. size_t l = 0;
  817. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  818. buf[l++] = 'P';
  819. if (mod->taints & (1 << TAINT_OOT_MODULE))
  820. buf[l++] = 'O';
  821. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  822. buf[l++] = 'F';
  823. if (mod->taints & (1 << TAINT_CRAP))
  824. buf[l++] = 'C';
  825. /*
  826. * TAINT_FORCED_RMMOD: could be added.
  827. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  828. * apply to modules.
  829. */
  830. return l;
  831. }
  832. static ssize_t show_initstate(struct module_attribute *mattr,
  833. struct module_kobject *mk, char *buffer)
  834. {
  835. const char *state = "unknown";
  836. switch (mk->mod->state) {
  837. case MODULE_STATE_LIVE:
  838. state = "live";
  839. break;
  840. case MODULE_STATE_COMING:
  841. state = "coming";
  842. break;
  843. case MODULE_STATE_GOING:
  844. state = "going";
  845. break;
  846. }
  847. return sprintf(buffer, "%s\n", state);
  848. }
  849. static struct module_attribute modinfo_initstate =
  850. __ATTR(initstate, 0444, show_initstate, NULL);
  851. static ssize_t store_uevent(struct module_attribute *mattr,
  852. struct module_kobject *mk,
  853. const char *buffer, size_t count)
  854. {
  855. enum kobject_action action;
  856. if (kobject_action_type(buffer, count, &action) == 0)
  857. kobject_uevent(&mk->kobj, action);
  858. return count;
  859. }
  860. struct module_attribute module_uevent =
  861. __ATTR(uevent, 0200, NULL, store_uevent);
  862. static ssize_t show_coresize(struct module_attribute *mattr,
  863. struct module_kobject *mk, char *buffer)
  864. {
  865. return sprintf(buffer, "%u\n", mk->mod->core_size);
  866. }
  867. static struct module_attribute modinfo_coresize =
  868. __ATTR(coresize, 0444, show_coresize, NULL);
  869. static ssize_t show_initsize(struct module_attribute *mattr,
  870. struct module_kobject *mk, char *buffer)
  871. {
  872. return sprintf(buffer, "%u\n", mk->mod->init_size);
  873. }
  874. static struct module_attribute modinfo_initsize =
  875. __ATTR(initsize, 0444, show_initsize, NULL);
  876. static ssize_t show_taint(struct module_attribute *mattr,
  877. struct module_kobject *mk, char *buffer)
  878. {
  879. size_t l;
  880. l = module_flags_taint(mk->mod, buffer);
  881. buffer[l++] = '\n';
  882. return l;
  883. }
  884. static struct module_attribute modinfo_taint =
  885. __ATTR(taint, 0444, show_taint, NULL);
  886. static struct module_attribute *modinfo_attrs[] = {
  887. &module_uevent,
  888. &modinfo_version,
  889. &modinfo_srcversion,
  890. &modinfo_initstate,
  891. &modinfo_coresize,
  892. &modinfo_initsize,
  893. &modinfo_taint,
  894. #ifdef CONFIG_MODULE_UNLOAD
  895. &modinfo_refcnt,
  896. #endif
  897. NULL,
  898. };
  899. static const char vermagic[] = VERMAGIC_STRING;
  900. static int try_to_force_load(struct module *mod, const char *reason)
  901. {
  902. #ifdef CONFIG_MODULE_FORCE_LOAD
  903. if (!test_taint(TAINT_FORCED_MODULE))
  904. printk(KERN_WARNING "%s: %s: kernel tainted.\n",
  905. mod->name, reason);
  906. add_taint_module(mod, TAINT_FORCED_MODULE);
  907. return 0;
  908. #else
  909. return -ENOEXEC;
  910. #endif
  911. }
  912. #ifdef CONFIG_MODVERSIONS
  913. /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
  914. static unsigned long maybe_relocated(unsigned long crc,
  915. const struct module *crc_owner)
  916. {
  917. #ifdef ARCH_RELOCATES_KCRCTAB
  918. if (crc_owner == NULL)
  919. return crc - (unsigned long)reloc_start;
  920. #endif
  921. return crc;
  922. }
  923. static int check_version(Elf_Shdr *sechdrs,
  924. unsigned int versindex,
  925. const char *symname,
  926. struct module *mod,
  927. const unsigned long *crc,
  928. const struct module *crc_owner)
  929. {
  930. unsigned int i, num_versions;
  931. struct modversion_info *versions;
  932. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  933. if (!crc)
  934. return 1;
  935. /* No versions at all? modprobe --force does this. */
  936. if (versindex == 0)
  937. return try_to_force_load(mod, symname) == 0;
  938. versions = (void *) sechdrs[versindex].sh_addr;
  939. num_versions = sechdrs[versindex].sh_size
  940. / sizeof(struct modversion_info);
  941. for (i = 0; i < num_versions; i++) {
  942. if (strcmp(versions[i].name, symname) != 0)
  943. continue;
  944. if (versions[i].crc == maybe_relocated(*crc, crc_owner))
  945. return 1;
  946. pr_debug("Found checksum %lX vs module %lX\n",
  947. maybe_relocated(*crc, crc_owner), versions[i].crc);
  948. goto bad_version;
  949. }
  950. printk(KERN_WARNING "%s: no symbol version for %s\n",
  951. mod->name, symname);
  952. return 0;
  953. bad_version:
  954. printk("%s: disagrees about version of symbol %s\n",
  955. mod->name, symname);
  956. return 0;
  957. }
  958. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  959. unsigned int versindex,
  960. struct module *mod)
  961. {
  962. const unsigned long *crc;
  963. /* Since this should be found in kernel (which can't be removed),
  964. * no locking is necessary. */
  965. if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
  966. &crc, true, false))
  967. BUG();
  968. return check_version(sechdrs, versindex, "module_layout", mod, crc,
  969. NULL);
  970. }
  971. /* First part is kernel version, which we ignore if module has crcs. */
  972. static inline int same_magic(const char *amagic, const char *bmagic,
  973. bool has_crcs)
  974. {
  975. if (has_crcs) {
  976. amagic += strcspn(amagic, " ");
  977. bmagic += strcspn(bmagic, " ");
  978. }
  979. return strcmp(amagic, bmagic) == 0;
  980. }
  981. #else
  982. static inline int check_version(Elf_Shdr *sechdrs,
  983. unsigned int versindex,
  984. const char *symname,
  985. struct module *mod,
  986. const unsigned long *crc,
  987. const struct module *crc_owner)
  988. {
  989. return 1;
  990. }
  991. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  992. unsigned int versindex,
  993. struct module *mod)
  994. {
  995. return 1;
  996. }
  997. static inline int same_magic(const char *amagic, const char *bmagic,
  998. bool has_crcs)
  999. {
  1000. return strcmp(amagic, bmagic) == 0;
  1001. }
  1002. #endif /* CONFIG_MODVERSIONS */
  1003. /* Resolve a symbol for this module. I.e. if we find one, record usage. */
  1004. static const struct kernel_symbol *resolve_symbol(struct module *mod,
  1005. const struct load_info *info,
  1006. const char *name,
  1007. char ownername[])
  1008. {
  1009. struct module *owner;
  1010. const struct kernel_symbol *sym;
  1011. const unsigned long *crc;
  1012. int err;
  1013. mutex_lock(&module_mutex);
  1014. sym = find_symbol(name, &owner, &crc,
  1015. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  1016. if (!sym)
  1017. goto unlock;
  1018. if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
  1019. owner)) {
  1020. sym = ERR_PTR(-EINVAL);
  1021. goto getname;
  1022. }
  1023. err = ref_module(mod, owner);
  1024. if (err) {
  1025. sym = ERR_PTR(err);
  1026. goto getname;
  1027. }
  1028. getname:
  1029. /* We must make copy under the lock if we failed to get ref. */
  1030. strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
  1031. unlock:
  1032. mutex_unlock(&module_mutex);
  1033. return sym;
  1034. }
  1035. static const struct kernel_symbol *
  1036. resolve_symbol_wait(struct module *mod,
  1037. const struct load_info *info,
  1038. const char *name)
  1039. {
  1040. const struct kernel_symbol *ksym;
  1041. char owner[MODULE_NAME_LEN];
  1042. if (wait_event_interruptible_timeout(module_wq,
  1043. !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
  1044. || PTR_ERR(ksym) != -EBUSY,
  1045. 30 * HZ) <= 0) {
  1046. printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
  1047. mod->name, owner);
  1048. }
  1049. return ksym;
  1050. }
  1051. /*
  1052. * /sys/module/foo/sections stuff
  1053. * J. Corbet <corbet@lwn.net>
  1054. */
  1055. #ifdef CONFIG_SYSFS
  1056. #ifdef CONFIG_KALLSYMS
  1057. static inline bool sect_empty(const Elf_Shdr *sect)
  1058. {
  1059. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  1060. }
  1061. struct module_sect_attr
  1062. {
  1063. struct module_attribute mattr;
  1064. char *name;
  1065. unsigned long address;
  1066. };
  1067. struct module_sect_attrs
  1068. {
  1069. struct attribute_group grp;
  1070. unsigned int nsections;
  1071. struct module_sect_attr attrs[0];
  1072. };
  1073. static ssize_t module_sect_show(struct module_attribute *mattr,
  1074. struct module_kobject *mk, char *buf)
  1075. {
  1076. struct module_sect_attr *sattr =
  1077. container_of(mattr, struct module_sect_attr, mattr);
  1078. return sprintf(buf, "0x%pK\n", (void *)sattr->address);
  1079. }
  1080. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  1081. {
  1082. unsigned int section;
  1083. for (section = 0; section < sect_attrs->nsections; section++)
  1084. kfree(sect_attrs->attrs[section].name);
  1085. kfree(sect_attrs);
  1086. }
  1087. static void add_sect_attrs(struct module *mod, const struct load_info *info)
  1088. {
  1089. unsigned int nloaded = 0, i, size[2];
  1090. struct module_sect_attrs *sect_attrs;
  1091. struct module_sect_attr *sattr;
  1092. struct attribute **gattr;
  1093. /* Count loaded sections and allocate structures */
  1094. for (i = 0; i < info->hdr->e_shnum; i++)
  1095. if (!sect_empty(&info->sechdrs[i]))
  1096. nloaded++;
  1097. size[0] = ALIGN(sizeof(*sect_attrs)
  1098. + nloaded * sizeof(sect_attrs->attrs[0]),
  1099. sizeof(sect_attrs->grp.attrs[0]));
  1100. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  1101. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  1102. if (sect_attrs == NULL)
  1103. return;
  1104. /* Setup section attributes. */
  1105. sect_attrs->grp.name = "sections";
  1106. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  1107. sect_attrs->nsections = 0;
  1108. sattr = &sect_attrs->attrs[0];
  1109. gattr = &sect_attrs->grp.attrs[0];
  1110. for (i = 0; i < info->hdr->e_shnum; i++) {
  1111. Elf_Shdr *sec = &info->sechdrs[i];
  1112. if (sect_empty(sec))
  1113. continue;
  1114. sattr->address = sec->sh_addr;
  1115. sattr->name = kstrdup(info->secstrings + sec->sh_name,
  1116. GFP_KERNEL);
  1117. if (sattr->name == NULL)
  1118. goto out;
  1119. sect_attrs->nsections++;
  1120. sysfs_attr_init(&sattr->mattr.attr);
  1121. sattr->mattr.show = module_sect_show;
  1122. sattr->mattr.store = NULL;
  1123. sattr->mattr.attr.name = sattr->name;
  1124. sattr->mattr.attr.mode = S_IRUGO;
  1125. *(gattr++) = &(sattr++)->mattr.attr;
  1126. }
  1127. *gattr = NULL;
  1128. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1129. goto out;
  1130. mod->sect_attrs = sect_attrs;
  1131. return;
  1132. out:
  1133. free_sect_attrs(sect_attrs);
  1134. }
  1135. static void remove_sect_attrs(struct module *mod)
  1136. {
  1137. if (mod->sect_attrs) {
  1138. sysfs_remove_group(&mod->mkobj.kobj,
  1139. &mod->sect_attrs->grp);
  1140. /* We are positive that no one is using any sect attrs
  1141. * at this point. Deallocate immediately. */
  1142. free_sect_attrs(mod->sect_attrs);
  1143. mod->sect_attrs = NULL;
  1144. }
  1145. }
  1146. /*
  1147. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1148. */
  1149. struct module_notes_attrs {
  1150. struct kobject *dir;
  1151. unsigned int notes;
  1152. struct bin_attribute attrs[0];
  1153. };
  1154. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  1155. struct bin_attribute *bin_attr,
  1156. char *buf, loff_t pos, size_t count)
  1157. {
  1158. /*
  1159. * The caller checked the pos and count against our size.
  1160. */
  1161. memcpy(buf, bin_attr->private + pos, count);
  1162. return count;
  1163. }
  1164. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1165. unsigned int i)
  1166. {
  1167. if (notes_attrs->dir) {
  1168. while (i-- > 0)
  1169. sysfs_remove_bin_file(notes_attrs->dir,
  1170. &notes_attrs->attrs[i]);
  1171. kobject_put(notes_attrs->dir);
  1172. }
  1173. kfree(notes_attrs);
  1174. }
  1175. static void add_notes_attrs(struct module *mod, const struct load_info *info)
  1176. {
  1177. unsigned int notes, loaded, i;
  1178. struct module_notes_attrs *notes_attrs;
  1179. struct bin_attribute *nattr;
  1180. /* failed to create section attributes, so can't create notes */
  1181. if (!mod->sect_attrs)
  1182. return;
  1183. /* Count notes sections and allocate structures. */
  1184. notes = 0;
  1185. for (i = 0; i < info->hdr->e_shnum; i++)
  1186. if (!sect_empty(&info->sechdrs[i]) &&
  1187. (info->sechdrs[i].sh_type == SHT_NOTE))
  1188. ++notes;
  1189. if (notes == 0)
  1190. return;
  1191. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1192. + notes * sizeof(notes_attrs->attrs[0]),
  1193. GFP_KERNEL);
  1194. if (notes_attrs == NULL)
  1195. return;
  1196. notes_attrs->notes = notes;
  1197. nattr = &notes_attrs->attrs[0];
  1198. for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
  1199. if (sect_empty(&info->sechdrs[i]))
  1200. continue;
  1201. if (info->sechdrs[i].sh_type == SHT_NOTE) {
  1202. sysfs_bin_attr_init(nattr);
  1203. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1204. nattr->attr.mode = S_IRUGO;
  1205. nattr->size = info->sechdrs[i].sh_size;
  1206. nattr->private = (void *) info->sechdrs[i].sh_addr;
  1207. nattr->read = module_notes_read;
  1208. ++nattr;
  1209. }
  1210. ++loaded;
  1211. }
  1212. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1213. if (!notes_attrs->dir)
  1214. goto out;
  1215. for (i = 0; i < notes; ++i)
  1216. if (sysfs_create_bin_file(notes_attrs->dir,
  1217. &notes_attrs->attrs[i]))
  1218. goto out;
  1219. mod->notes_attrs = notes_attrs;
  1220. return;
  1221. out:
  1222. free_notes_attrs(notes_attrs, i);
  1223. }
  1224. static void remove_notes_attrs(struct module *mod)
  1225. {
  1226. if (mod->notes_attrs)
  1227. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1228. }
  1229. #else
  1230. static inline void add_sect_attrs(struct module *mod,
  1231. const struct load_info *info)
  1232. {
  1233. }
  1234. static inline void remove_sect_attrs(struct module *mod)
  1235. {
  1236. }
  1237. static inline void add_notes_attrs(struct module *mod,
  1238. const struct load_info *info)
  1239. {
  1240. }
  1241. static inline void remove_notes_attrs(struct module *mod)
  1242. {
  1243. }
  1244. #endif /* CONFIG_KALLSYMS */
  1245. static void add_usage_links(struct module *mod)
  1246. {
  1247. #ifdef CONFIG_MODULE_UNLOAD
  1248. struct module_use *use;
  1249. int nowarn;
  1250. mutex_lock(&module_mutex);
  1251. list_for_each_entry(use, &mod->target_list, target_list) {
  1252. nowarn = sysfs_create_link(use->target->holders_dir,
  1253. &mod->mkobj.kobj, mod->name);
  1254. }
  1255. mutex_unlock(&module_mutex);
  1256. #endif
  1257. }
  1258. static void del_usage_links(struct module *mod)
  1259. {
  1260. #ifdef CONFIG_MODULE_UNLOAD
  1261. struct module_use *use;
  1262. mutex_lock(&module_mutex);
  1263. list_for_each_entry(use, &mod->target_list, target_list)
  1264. sysfs_remove_link(use->target->holders_dir, mod->name);
  1265. mutex_unlock(&module_mutex);
  1266. #endif
  1267. }
  1268. static int module_add_modinfo_attrs(struct module *mod)
  1269. {
  1270. struct module_attribute *attr;
  1271. struct module_attribute *temp_attr;
  1272. int error = 0;
  1273. int i;
  1274. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1275. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1276. GFP_KERNEL);
  1277. if (!mod->modinfo_attrs)
  1278. return -ENOMEM;
  1279. temp_attr = mod->modinfo_attrs;
  1280. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1281. if (!attr->test ||
  1282. (attr->test && attr->test(mod))) {
  1283. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1284. sysfs_attr_init(&temp_attr->attr);
  1285. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1286. ++temp_attr;
  1287. }
  1288. }
  1289. return error;
  1290. }
  1291. static void module_remove_modinfo_attrs(struct module *mod)
  1292. {
  1293. struct module_attribute *attr;
  1294. int i;
  1295. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1296. /* pick a field to test for end of list */
  1297. if (!attr->attr.name)
  1298. break;
  1299. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1300. if (attr->free)
  1301. attr->free(mod);
  1302. }
  1303. kfree(mod->modinfo_attrs);
  1304. }
  1305. static int mod_sysfs_init(struct module *mod)
  1306. {
  1307. int err;
  1308. struct kobject *kobj;
  1309. if (!module_sysfs_initialized) {
  1310. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1311. mod->name);
  1312. err = -EINVAL;
  1313. goto out;
  1314. }
  1315. kobj = kset_find_obj(module_kset, mod->name);
  1316. if (kobj) {
  1317. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1318. kobject_put(kobj);
  1319. err = -EINVAL;
  1320. goto out;
  1321. }
  1322. mod->mkobj.mod = mod;
  1323. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1324. mod->mkobj.kobj.kset = module_kset;
  1325. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1326. "%s", mod->name);
  1327. if (err)
  1328. kobject_put(&mod->mkobj.kobj);
  1329. /* delay uevent until full sysfs population */
  1330. out:
  1331. return err;
  1332. }
  1333. static int mod_sysfs_setup(struct module *mod,
  1334. const struct load_info *info,
  1335. struct kernel_param *kparam,
  1336. unsigned int num_params)
  1337. {
  1338. int err;
  1339. err = mod_sysfs_init(mod);
  1340. if (err)
  1341. goto out;
  1342. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1343. if (!mod->holders_dir) {
  1344. err = -ENOMEM;
  1345. goto out_unreg;
  1346. }
  1347. err = module_param_sysfs_setup(mod, kparam, num_params);
  1348. if (err)
  1349. goto out_unreg_holders;
  1350. err = module_add_modinfo_attrs(mod);
  1351. if (err)
  1352. goto out_unreg_param;
  1353. add_usage_links(mod);
  1354. add_sect_attrs(mod, info);
  1355. add_notes_attrs(mod, info);
  1356. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1357. return 0;
  1358. out_unreg_param:
  1359. module_param_sysfs_remove(mod);
  1360. out_unreg_holders:
  1361. kobject_put(mod->holders_dir);
  1362. out_unreg:
  1363. kobject_put(&mod->mkobj.kobj);
  1364. out:
  1365. return err;
  1366. }
  1367. static void mod_sysfs_fini(struct module *mod)
  1368. {
  1369. remove_notes_attrs(mod);
  1370. remove_sect_attrs(mod);
  1371. kobject_put(&mod->mkobj.kobj);
  1372. }
  1373. #else /* !CONFIG_SYSFS */
  1374. static int mod_sysfs_setup(struct module *mod,
  1375. const struct load_info *info,
  1376. struct kernel_param *kparam,
  1377. unsigned int num_params)
  1378. {
  1379. return 0;
  1380. }
  1381. static void mod_sysfs_fini(struct module *mod)
  1382. {
  1383. }
  1384. static void module_remove_modinfo_attrs(struct module *mod)
  1385. {
  1386. }
  1387. static void del_usage_links(struct module *mod)
  1388. {
  1389. }
  1390. #endif /* CONFIG_SYSFS */
  1391. static void mod_sysfs_teardown(struct module *mod)
  1392. {
  1393. del_usage_links(mod);
  1394. module_remove_modinfo_attrs(mod);
  1395. module_param_sysfs_remove(mod);
  1396. kobject_put(mod->mkobj.drivers_dir);
  1397. kobject_put(mod->holders_dir);
  1398. mod_sysfs_fini(mod);
  1399. }
  1400. /*
  1401. * unlink the module with the whole machine is stopped with interrupts off
  1402. * - this defends against kallsyms not taking locks
  1403. */
  1404. static int __unlink_module(void *_mod)
  1405. {
  1406. struct module *mod = _mod;
  1407. list_del(&mod->list);
  1408. module_bug_cleanup(mod);
  1409. return 0;
  1410. }
  1411. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  1412. /*
  1413. * LKM RO/NX protection: protect module's text/ro-data
  1414. * from modification and any data from execution.
  1415. */
  1416. void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
  1417. {
  1418. unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
  1419. unsigned long end_pfn = PFN_DOWN((unsigned long)end);
  1420. if (end_pfn > begin_pfn)
  1421. set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
  1422. }
  1423. static void set_section_ro_nx(void *base,
  1424. unsigned long text_size,
  1425. unsigned long ro_size,
  1426. unsigned long total_size)
  1427. {
  1428. /* begin and end PFNs of the current subsection */
  1429. unsigned long begin_pfn;
  1430. unsigned long end_pfn;
  1431. /*
  1432. * Set RO for module text and RO-data:
  1433. * - Always protect first page.
  1434. * - Do not protect last partial page.
  1435. */
  1436. if (ro_size > 0)
  1437. set_page_attributes(base, base + ro_size, set_memory_ro);
  1438. /*
  1439. * Set NX permissions for module data:
  1440. * - Do not protect first partial page.
  1441. * - Always protect last page.
  1442. */
  1443. if (total_size > text_size) {
  1444. begin_pfn = PFN_UP((unsigned long)base + text_size);
  1445. end_pfn = PFN_UP((unsigned long)base + total_size);
  1446. if (end_pfn > begin_pfn)
  1447. set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
  1448. }
  1449. }
  1450. static void unset_module_core_ro_nx(struct module *mod)
  1451. {
  1452. set_page_attributes(mod->module_core + mod->core_text_size,
  1453. mod->module_core + mod->core_size,
  1454. set_memory_x);
  1455. set_page_attributes(mod->module_core,
  1456. mod->module_core + mod->core_ro_size,
  1457. set_memory_rw);
  1458. }
  1459. static void unset_module_init_ro_nx(struct module *mod)
  1460. {
  1461. set_page_attributes(mod->module_init + mod->init_text_size,
  1462. mod->module_init + mod->init_size,
  1463. set_memory_x);
  1464. set_page_attributes(mod->module_init,
  1465. mod->module_init + mod->init_ro_size,
  1466. set_memory_rw);
  1467. }
  1468. /* Iterate through all modules and set each module's text as RW */
  1469. void set_all_modules_text_rw(void)
  1470. {
  1471. struct module *mod;
  1472. mutex_lock(&module_mutex);
  1473. list_for_each_entry_rcu(mod, &modules, list) {
  1474. if ((mod->module_core) && (mod->core_text_size)) {
  1475. set_page_attributes(mod->module_core,
  1476. mod->module_core + mod->core_text_size,
  1477. set_memory_rw);
  1478. }
  1479. if ((mod->module_init) && (mod->init_text_size)) {
  1480. set_page_attributes(mod->module_init,
  1481. mod->module_init + mod->init_text_size,
  1482. set_memory_rw);
  1483. }
  1484. }
  1485. mutex_unlock(&module_mutex);
  1486. }
  1487. /* Iterate through all modules and set each module's text as RO */
  1488. void set_all_modules_text_ro(void)
  1489. {
  1490. struct module *mod;
  1491. mutex_lock(&module_mutex);
  1492. list_for_each_entry_rcu(mod, &modules, list) {
  1493. if ((mod->module_core) && (mod->core_text_size)) {
  1494. set_page_attributes(mod->module_core,
  1495. mod->module_core + mod->core_text_size,
  1496. set_memory_ro);
  1497. }
  1498. if ((mod->module_init) && (mod->init_text_size)) {
  1499. set_page_attributes(mod->module_init,
  1500. mod->module_init + mod->init_text_size,
  1501. set_memory_ro);
  1502. }
  1503. }
  1504. mutex_unlock(&module_mutex);
  1505. }
  1506. #else
  1507. static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
  1508. static void unset_module_core_ro_nx(struct module *mod) { }
  1509. static void unset_module_init_ro_nx(struct module *mod) { }
  1510. #endif
  1511. void __weak module_free(struct module *mod, void *module_region)
  1512. {
  1513. vfree(module_region);
  1514. }
  1515. void __weak module_arch_cleanup(struct module *mod)
  1516. {
  1517. }
  1518. /* Free a module, remove from lists, etc. */
  1519. static void free_module(struct module *mod)
  1520. {
  1521. trace_module_free(mod);
  1522. /* Delete from various lists */
  1523. mutex_lock(&module_mutex);
  1524. stop_machine(__unlink_module, mod, NULL);
  1525. mutex_unlock(&module_mutex);
  1526. mod_sysfs_teardown(mod);
  1527. /* Remove dynamic debug info */
  1528. ddebug_remove_module(mod->name);
  1529. /* Arch-specific cleanup. */
  1530. module_arch_cleanup(mod);
  1531. /* Module unload stuff */
  1532. module_unload_free(mod);
  1533. /* Free any allocated parameters. */
  1534. destroy_params(mod->kp, mod->num_kp);
  1535. /* This may be NULL, but that's OK */
  1536. unset_module_init_ro_nx(mod);
  1537. module_free(mod, mod->module_init);
  1538. kfree(mod->args);
  1539. percpu_modfree(mod);
  1540. /* Free lock-classes: */
  1541. lockdep_free_key_range(mod->module_core, mod->core_size);
  1542. /* Finally, free the core (containing the module structure) */
  1543. unset_module_core_ro_nx(mod);
  1544. module_free(mod, mod->module_core);
  1545. #ifdef CONFIG_MPU
  1546. update_protections(current->mm);
  1547. #endif
  1548. }
  1549. void *__symbol_get(const char *symbol)
  1550. {
  1551. struct module *owner;
  1552. const struct kernel_symbol *sym;
  1553. preempt_disable();
  1554. sym = find_symbol(symbol, &owner, NULL, true, true);
  1555. if (sym && strong_try_module_get(owner))
  1556. sym = NULL;
  1557. preempt_enable();
  1558. return sym ? (void *)sym->value : NULL;
  1559. }
  1560. EXPORT_SYMBOL_GPL(__symbol_get);
  1561. /*
  1562. * Ensure that an exported symbol [global namespace] does not already exist
  1563. * in the kernel or in some other module's exported symbol table.
  1564. *
  1565. * You must hold the module_mutex.
  1566. */
  1567. static int verify_export_symbols(struct module *mod)
  1568. {
  1569. unsigned int i;
  1570. struct module *owner;
  1571. const struct kernel_symbol *s;
  1572. struct {
  1573. const struct kernel_symbol *sym;
  1574. unsigned int num;
  1575. } arr[] = {
  1576. { mod->syms, mod->num_syms },
  1577. { mod->gpl_syms, mod->num_gpl_syms },
  1578. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1579. #ifdef CONFIG_UNUSED_SYMBOLS
  1580. { mod->unused_syms, mod->num_unused_syms },
  1581. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1582. #endif
  1583. };
  1584. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1585. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1586. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1587. printk(KERN_ERR
  1588. "%s: exports duplicate symbol %s"
  1589. " (owned by %s)\n",
  1590. mod->name, s->name, module_name(owner));
  1591. return -ENOEXEC;
  1592. }
  1593. }
  1594. }
  1595. return 0;
  1596. }
  1597. /* Change all symbols so that st_value encodes the pointer directly. */
  1598. static int simplify_symbols(struct module *mod, const struct load_info *info)
  1599. {
  1600. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1601. Elf_Sym *sym = (void *)symsec->sh_addr;
  1602. unsigned long secbase;
  1603. unsigned int i;
  1604. int ret = 0;
  1605. const struct kernel_symbol *ksym;
  1606. for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
  1607. const char *name = info->strtab + sym[i].st_name;
  1608. switch (sym[i].st_shndx) {
  1609. case SHN_COMMON:
  1610. /* We compiled with -fno-common. These are not
  1611. supposed to happen. */
  1612. pr_debug("Common symbol: %s\n", name);
  1613. printk("%s: please compile with -fno-common\n",
  1614. mod->name);
  1615. ret = -ENOEXEC;
  1616. break;
  1617. case SHN_ABS:
  1618. /* Don't need to do anything */
  1619. pr_debug("Absolute symbol: 0x%08lx\n",
  1620. (long)sym[i].st_value);
  1621. break;
  1622. case SHN_UNDEF:
  1623. ksym = resolve_symbol_wait(mod, info, name);
  1624. /* Ok if resolved. */
  1625. if (ksym && !IS_ERR(ksym)) {
  1626. sym[i].st_value = ksym->value;
  1627. break;
  1628. }
  1629. /* Ok if weak. */
  1630. if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1631. break;
  1632. printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
  1633. mod->name, name, PTR_ERR(ksym));
  1634. ret = PTR_ERR(ksym) ?: -ENOENT;
  1635. break;
  1636. default:
  1637. /* Divert to percpu allocation if a percpu var. */
  1638. if (sym[i].st_shndx == info->index.pcpu)
  1639. secbase = (unsigned long)mod_percpu(mod);
  1640. else
  1641. secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
  1642. sym[i].st_value += secbase;
  1643. break;
  1644. }
  1645. }
  1646. return ret;
  1647. }
  1648. int __weak apply_relocate(Elf_Shdr *sechdrs,
  1649. const char *strtab,
  1650. unsigned int symindex,
  1651. unsigned int relsec,
  1652. struct module *me)
  1653. {
  1654. pr_err("module %s: REL relocation unsupported\n", me->name);
  1655. return -ENOEXEC;
  1656. }
  1657. int __weak apply_relocate_add(Elf_Shdr *sechdrs,
  1658. const char *strtab,
  1659. unsigned int symindex,
  1660. unsigned int relsec,
  1661. struct module *me)
  1662. {
  1663. pr_err("module %s: RELA relocation unsupported\n", me->name);
  1664. return -ENOEXEC;
  1665. }
  1666. static int apply_relocations(struct module *mod, const struct load_info *info)
  1667. {
  1668. unsigned int i;
  1669. int err = 0;
  1670. /* Now do relocations. */
  1671. for (i = 1; i < info->hdr->e_shnum; i++) {
  1672. unsigned int infosec = info->sechdrs[i].sh_info;
  1673. /* Not a valid relocation section? */
  1674. if (infosec >= info->hdr->e_shnum)
  1675. continue;
  1676. /* Don't bother with non-allocated sections */
  1677. if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
  1678. continue;
  1679. if (info->sechdrs[i].sh_type == SHT_REL)
  1680. err = apply_relocate(info->sechdrs, info->strtab,
  1681. info->index.sym, i, mod);
  1682. else if (info->sechdrs[i].sh_type == SHT_RELA)
  1683. err = apply_relocate_add(info->sechdrs, info->strtab,
  1684. info->index.sym, i, mod);
  1685. if (err < 0)
  1686. break;
  1687. }
  1688. return err;
  1689. }
  1690. /* Additional bytes needed by arch in front of individual sections */
  1691. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1692. unsigned int section)
  1693. {
  1694. /* default implementation just returns zero */
  1695. return 0;
  1696. }
  1697. /* Update size with this section: return offset. */
  1698. static long get_offset(struct module *mod, unsigned int *size,
  1699. Elf_Shdr *sechdr, unsigned int section)
  1700. {
  1701. long ret;
  1702. *size += arch_mod_section_prepend(mod, section);
  1703. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1704. *size = ret + sechdr->sh_size;
  1705. return ret;
  1706. }
  1707. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1708. might -- code, read-only data, read-write data, small data. Tally
  1709. sizes, and place the offsets into sh_entsize fields: high bit means it
  1710. belongs in init. */
  1711. static void layout_sections(struct module *mod, struct load_info *info)
  1712. {
  1713. static unsigned long const masks[][2] = {
  1714. /* NOTE: all executable code must be the first section
  1715. * in this array; otherwise modify the text_size
  1716. * finder in the two loops below */
  1717. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1718. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1719. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1720. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1721. };
  1722. unsigned int m, i;
  1723. for (i = 0; i < info->hdr->e_shnum; i++)
  1724. info->sechdrs[i].sh_entsize = ~0UL;
  1725. pr_debug("Core section allocation order:\n");
  1726. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1727. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1728. Elf_Shdr *s = &info->sechdrs[i];
  1729. const char *sname = info->secstrings + s->sh_name;
  1730. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1731. || (s->sh_flags & masks[m][1])
  1732. || s->sh_entsize != ~0UL
  1733. || strstarts(sname, ".init"))
  1734. continue;
  1735. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1736. pr_debug("\t%s\n", sname);
  1737. }
  1738. switch (m) {
  1739. case 0: /* executable */
  1740. mod->core_size = debug_align(mod->core_size);
  1741. mod->core_text_size = mod->core_size;
  1742. break;
  1743. case 1: /* RO: text and ro-data */
  1744. mod->core_size = debug_align(mod->core_size);
  1745. mod->core_ro_size = mod->core_size;
  1746. break;
  1747. case 3: /* whole core */
  1748. mod->core_size = debug_align(mod->core_size);
  1749. break;
  1750. }
  1751. }
  1752. pr_debug("Init section allocation order:\n");
  1753. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1754. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1755. Elf_Shdr *s = &info->sechdrs[i];
  1756. const char *sname = info->secstrings + s->sh_name;
  1757. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1758. || (s->sh_flags & masks[m][1])
  1759. || s->sh_entsize != ~0UL
  1760. || !strstarts(sname, ".init"))
  1761. continue;
  1762. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1763. | INIT_OFFSET_MASK);
  1764. pr_debug("\t%s\n", sname);
  1765. }
  1766. switch (m) {
  1767. case 0: /* executable */
  1768. mod->init_size = debug_align(mod->init_size);
  1769. mod->init_text_size = mod->init_size;
  1770. break;
  1771. case 1: /* RO: text and ro-data */
  1772. mod->init_size = debug_align(mod->init_size);
  1773. mod->init_ro_size = mod->init_size;
  1774. break;
  1775. case 3: /* whole init */
  1776. mod->init_size = debug_align(mod->init_size);
  1777. break;
  1778. }
  1779. }
  1780. }
  1781. static void set_license(struct module *mod, const char *license)
  1782. {
  1783. if (!license)
  1784. license = "unspecified";
  1785. if (!license_is_gpl_compatible(license)) {
  1786. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1787. printk(KERN_WARNING "%s: module license '%s' taints "
  1788. "kernel.\n", mod->name, license);
  1789. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1790. }
  1791. }
  1792. /* Parse tag=value strings from .modinfo section */
  1793. static char *next_string(char *string, unsigned long *secsize)
  1794. {
  1795. /* Skip non-zero chars */
  1796. while (string[0]) {
  1797. string++;
  1798. if ((*secsize)-- <= 1)
  1799. return NULL;
  1800. }
  1801. /* Skip any zero padding. */
  1802. while (!string[0]) {
  1803. string++;
  1804. if ((*secsize)-- <= 1)
  1805. return NULL;
  1806. }
  1807. return string;
  1808. }
  1809. static char *get_modinfo(struct load_info *info, const char *tag)
  1810. {
  1811. char *p;
  1812. unsigned int taglen = strlen(tag);
  1813. Elf_Shdr *infosec = &info->sechdrs[info->index.info];
  1814. unsigned long size = infosec->sh_size;
  1815. for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
  1816. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1817. return p + taglen + 1;
  1818. }
  1819. return NULL;
  1820. }
  1821. static void setup_modinfo(struct module *mod, struct load_info *info)
  1822. {
  1823. struct module_attribute *attr;
  1824. int i;
  1825. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1826. if (attr->setup)
  1827. attr->setup(mod, get_modinfo(info, attr->attr.name));
  1828. }
  1829. }
  1830. static void free_modinfo(struct module *mod)
  1831. {
  1832. struct module_attribute *attr;
  1833. int i;
  1834. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1835. if (attr->free)
  1836. attr->free(mod);
  1837. }
  1838. }
  1839. #ifdef CONFIG_KALLSYMS
  1840. /* lookup symbol in given range of kernel_symbols */
  1841. static const struct kernel_symbol *lookup_symbol(const char *name,
  1842. const struct kernel_symbol *start,
  1843. const struct kernel_symbol *stop)
  1844. {
  1845. return bsearch(name, start, stop - start,
  1846. sizeof(struct kernel_symbol), cmp_name);
  1847. }
  1848. static int is_exported(const char *name, unsigned long value,
  1849. const struct module *mod)
  1850. {
  1851. const struct kernel_symbol *ks;
  1852. if (!mod)
  1853. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  1854. else
  1855. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  1856. return ks != NULL && ks->value == value;
  1857. }
  1858. /* As per nm */
  1859. static char elf_type(const Elf_Sym *sym, const struct load_info *info)
  1860. {
  1861. const Elf_Shdr *sechdrs = info->sechdrs;
  1862. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1863. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1864. return 'v';
  1865. else
  1866. return 'w';
  1867. }
  1868. if (sym->st_shndx == SHN_UNDEF)
  1869. return 'U';
  1870. if (sym->st_shndx == SHN_ABS)
  1871. return 'a';
  1872. if (sym->st_shndx >= SHN_LORESERVE)
  1873. return '?';
  1874. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1875. return 't';
  1876. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1877. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1878. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1879. return 'r';
  1880. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1881. return 'g';
  1882. else
  1883. return 'd';
  1884. }
  1885. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1886. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1887. return 's';
  1888. else
  1889. return 'b';
  1890. }
  1891. if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
  1892. ".debug")) {
  1893. return 'n';
  1894. }
  1895. return '?';
  1896. }
  1897. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  1898. unsigned int shnum)
  1899. {
  1900. const Elf_Shdr *sec;
  1901. if (src->st_shndx == SHN_UNDEF
  1902. || src->st_shndx >= shnum
  1903. || !src->st_name)
  1904. return false;
  1905. sec = sechdrs + src->st_shndx;
  1906. if (!(sec->sh_flags & SHF_ALLOC)
  1907. #ifndef CONFIG_KALLSYMS_ALL
  1908. || !(sec->sh_flags & SHF_EXECINSTR)
  1909. #endif
  1910. || (sec->sh_entsize & INIT_OFFSET_MASK))
  1911. return false;
  1912. return true;
  1913. }
  1914. /*
  1915. * We only allocate and copy the strings needed by the parts of symtab
  1916. * we keep. This is simple, but has the effect of making multiple
  1917. * copies of duplicates. We could be more sophisticated, see
  1918. * linux-kernel thread starting with
  1919. * <73defb5e4bca04a6431392cc341112b1@localhost>.
  1920. */
  1921. static void layout_symtab(struct module *mod, struct load_info *info)
  1922. {
  1923. Elf_Shdr *symsect = info->sechdrs + info->index.sym;
  1924. Elf_Shdr *strsect = info->sechdrs + info->index.str;
  1925. const Elf_Sym *src;
  1926. unsigned int i, nsrc, ndst, strtab_size;
  1927. /* Put symbol section at end of init part of module. */
  1928. symsect->sh_flags |= SHF_ALLOC;
  1929. symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
  1930. info->index.sym) | INIT_OFFSET_MASK;
  1931. pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
  1932. src = (void *)info->hdr + symsect->sh_offset;
  1933. nsrc = symsect->sh_size / sizeof(*src);
  1934. /* Compute total space required for the core symbols' strtab. */
  1935. for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
  1936. if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
  1937. strtab_size += strlen(&info->strtab[src->st_name]) + 1;
  1938. ndst++;
  1939. }
  1940. /* Append room for core symbols at end of core part. */
  1941. info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
  1942. info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
  1943. mod->core_size += strtab_size;
  1944. /* Put string table section at end of init part of module. */
  1945. strsect->sh_flags |= SHF_ALLOC;
  1946. strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
  1947. info->index.str) | INIT_OFFSET_MASK;
  1948. pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
  1949. }
  1950. static void add_kallsyms(struct module *mod, const struct load_info *info)
  1951. {
  1952. unsigned int i, ndst;
  1953. const Elf_Sym *src;
  1954. Elf_Sym *dst;
  1955. char *s;
  1956. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1957. mod->symtab = (void *)symsec->sh_addr;
  1958. mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
  1959. /* Make sure we get permanent strtab: don't use info->strtab. */
  1960. mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
  1961. /* Set types up while we still have access to sections. */
  1962. for (i = 0; i < mod->num_symtab; i++)
  1963. mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
  1964. mod->core_symtab = dst = mod->module_core + info->symoffs;
  1965. mod->core_strtab = s = mod->module_core + info->stroffs;
  1966. src = mod->symtab;
  1967. *dst = *src;
  1968. *s++ = 0;
  1969. for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
  1970. if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
  1971. continue;
  1972. dst[ndst] = *src;
  1973. dst[ndst++].st_name = s - mod->core_strtab;
  1974. s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
  1975. }
  1976. mod->core_num_syms = ndst;
  1977. }
  1978. #else
  1979. static inline void layout_symtab(struct module *mod, struct load_info *info)
  1980. {
  1981. }
  1982. static void add_kallsyms(struct module *mod, const struct load_info *info)
  1983. {
  1984. }
  1985. #endif /* CONFIG_KALLSYMS */
  1986. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  1987. {
  1988. if (!debug)
  1989. return;
  1990. #ifdef CONFIG_DYNAMIC_DEBUG
  1991. if (ddebug_add_module(debug, num, debug->modname))
  1992. printk(KERN_ERR "dynamic debug error adding module: %s\n",
  1993. debug->modname);
  1994. #endif
  1995. }
  1996. static void dynamic_debug_remove(struct _ddebug *debug)
  1997. {
  1998. if (debug)
  1999. ddebug_remove_module(debug->modname);
  2000. }
  2001. void * __weak module_alloc(unsigned long size)
  2002. {
  2003. return size == 0 ? NULL : vmalloc_exec(size);
  2004. }
  2005. static void *module_alloc_update_bounds(unsigned long size)
  2006. {
  2007. void *ret = module_alloc(size);
  2008. if (ret) {
  2009. mutex_lock(&module_mutex);
  2010. /* Update module bounds. */
  2011. if ((unsigned long)ret < module_addr_min)
  2012. module_addr_min = (unsigned long)ret;
  2013. if ((unsigned long)ret + size > module_addr_max)
  2014. module_addr_max = (unsigned long)ret + size;
  2015. mutex_unlock(&module_mutex);
  2016. }
  2017. return ret;
  2018. }
  2019. #ifdef CONFIG_DEBUG_KMEMLEAK
  2020. static void kmemleak_load_module(const struct module *mod,
  2021. const struct load_info *info)
  2022. {
  2023. unsigned int i;
  2024. /* only scan the sections containing data */
  2025. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  2026. for (i = 1; i < info->hdr->e_shnum; i++) {
  2027. const char *name = info->secstrings + info->sechdrs[i].sh_name;
  2028. if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
  2029. continue;
  2030. if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
  2031. continue;
  2032. kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
  2033. info->sechdrs[i].sh_size, GFP_KERNEL);
  2034. }
  2035. }
  2036. #else
  2037. static inline void kmemleak_load_module(const struct module *mod,
  2038. const struct load_info *info)
  2039. {
  2040. }
  2041. #endif
  2042. /* Sets info->hdr and info->len. */
  2043. static int copy_and_check(struct load_info *info,
  2044. const void __user *umod, unsigned long len,
  2045. const char __user *uargs)
  2046. {
  2047. int err;
  2048. Elf_Ehdr *hdr;
  2049. if (len < sizeof(*hdr))
  2050. return -ENOEXEC;
  2051. /* Suck in entire file: we'll want most of it. */
  2052. /* vmalloc barfs on "unusual" numbers. Check here */
  2053. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  2054. return -ENOMEM;
  2055. if (copy_from_user(hdr, umod, len) != 0) {
  2056. err = -EFAULT;
  2057. goto free_hdr;
  2058. }
  2059. /* Sanity checks against insmoding binaries or wrong arch,
  2060. weird elf version */
  2061. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  2062. || hdr->e_type != ET_REL
  2063. || !elf_check_arch(hdr)
  2064. || hdr->e_shentsize != sizeof(Elf_Shdr)) {
  2065. err = -ENOEXEC;
  2066. goto free_hdr;
  2067. }
  2068. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  2069. err = -ENOEXEC;
  2070. goto free_hdr;
  2071. }
  2072. info->hdr = hdr;
  2073. info->len = len;
  2074. return 0;
  2075. free_hdr:
  2076. vfree(hdr);
  2077. return err;
  2078. }
  2079. static void free_copy(struct load_info *info)
  2080. {
  2081. vfree(info->hdr);
  2082. }
  2083. static int rewrite_section_headers(struct load_info *info)
  2084. {
  2085. unsigned int i;
  2086. /* This should always be true, but let's be sure. */
  2087. info->sechdrs[0].sh_addr = 0;
  2088. for (i = 1; i < info->hdr->e_shnum; i++) {
  2089. Elf_Shdr *shdr = &info->sechdrs[i];
  2090. if (shdr->sh_type != SHT_NOBITS
  2091. && info->len < shdr->sh_offset + shdr->sh_size) {
  2092. printk(KERN_ERR "Module len %lu truncated\n",
  2093. info->len);
  2094. return -ENOEXEC;
  2095. }
  2096. /* Mark all sections sh_addr with their address in the
  2097. temporary image. */
  2098. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  2099. #ifndef CONFIG_MODULE_UNLOAD
  2100. /* Don't load .exit sections */
  2101. if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
  2102. shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
  2103. #endif
  2104. }
  2105. /* Track but don't keep modinfo and version sections. */
  2106. info->index.vers = find_sec(info, "__versions");
  2107. info->index.info = find_sec(info, ".modinfo");
  2108. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2109. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2110. return 0;
  2111. }
  2112. /*
  2113. * Set up our basic convenience variables (pointers to section headers,
  2114. * search for module section index etc), and do some basic section
  2115. * verification.
  2116. *
  2117. * Return the temporary module pointer (we'll replace it with the final
  2118. * one when we move the module sections around).
  2119. */
  2120. static struct module *setup_load_info(struct load_info *info)
  2121. {
  2122. unsigned int i;
  2123. int err;
  2124. struct module *mod;
  2125. /* Set up the convenience variables */
  2126. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  2127. info->secstrings = (void *)info->hdr
  2128. + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
  2129. err = rewrite_section_headers(info);
  2130. if (err)
  2131. return ERR_PTR(err);
  2132. /* Find internal symbols and strings. */
  2133. for (i = 1; i < info->hdr->e_shnum; i++) {
  2134. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  2135. info->index.sym = i;
  2136. info->index.str = info->sechdrs[i].sh_link;
  2137. info->strtab = (char *)info->hdr
  2138. + info->sechdrs[info->index.str].sh_offset;
  2139. break;
  2140. }
  2141. }
  2142. info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
  2143. if (!info->index.mod) {
  2144. printk(KERN_WARNING "No module found in object\n");
  2145. return ERR_PTR(-ENOEXEC);
  2146. }
  2147. /* This is temporary: point mod into copy of data. */
  2148. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2149. if (info->index.sym == 0) {
  2150. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  2151. mod->name);
  2152. return ERR_PTR(-ENOEXEC);
  2153. }
  2154. info->index.pcpu = find_pcpusec(info);
  2155. /* Check module struct version now, before we try to use module. */
  2156. if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
  2157. return ERR_PTR(-ENOEXEC);
  2158. return mod;
  2159. }
  2160. static int check_modinfo(struct module *mod, struct load_info *info)
  2161. {
  2162. const char *modmagic = get_modinfo(info, "vermagic");
  2163. int err;
  2164. /* This is allowed: modprobe --force will invalidate it. */
  2165. if (!modmagic) {
  2166. err = try_to_force_load(mod, "bad vermagic");
  2167. if (err)
  2168. return err;
  2169. } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
  2170. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  2171. mod->name, modmagic, vermagic);
  2172. return -ENOEXEC;
  2173. }
  2174. if (!get_modinfo(info, "intree"))
  2175. add_taint_module(mod, TAINT_OOT_MODULE);
  2176. if (get_modinfo(info, "staging")) {
  2177. add_taint_module(mod, TAINT_CRAP);
  2178. printk(KERN_WARNING "%s: module is from the staging directory,"
  2179. " the quality is unknown, you have been warned.\n",
  2180. mod->name);
  2181. }
  2182. /* Set up license info based on the info section */
  2183. set_license(mod, get_modinfo(info, "license"));
  2184. return 0;
  2185. }
  2186. static void find_module_sections(struct module *mod, struct load_info *info)
  2187. {
  2188. mod->kp = section_objs(info, "__param",
  2189. sizeof(*mod->kp), &mod->num_kp);
  2190. mod->syms = section_objs(info, "__ksymtab",
  2191. sizeof(*mod->syms), &mod->num_syms);
  2192. mod->crcs = section_addr(info, "__kcrctab");
  2193. mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
  2194. sizeof(*mod->gpl_syms),
  2195. &mod->num_gpl_syms);
  2196. mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
  2197. mod->gpl_future_syms = section_objs(info,
  2198. "__ksymtab_gpl_future",
  2199. sizeof(*mod->gpl_future_syms),
  2200. &mod->num_gpl_future_syms);
  2201. mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
  2202. #ifdef CONFIG_UNUSED_SYMBOLS
  2203. mod->unused_syms = section_objs(info, "__ksymtab_unused",
  2204. sizeof(*mod->unused_syms),
  2205. &mod->num_unused_syms);
  2206. mod->unused_crcs = section_addr(info, "__kcrctab_unused");
  2207. mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
  2208. sizeof(*mod->unused_gpl_syms),
  2209. &mod->num_unused_gpl_syms);
  2210. mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
  2211. #endif
  2212. #ifdef CONFIG_CONSTRUCTORS
  2213. mod->ctors = section_objs(info, ".ctors",
  2214. sizeof(*mod->ctors), &mod->num_ctors);
  2215. #endif
  2216. #ifdef CONFIG_TRACEPOINTS
  2217. mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
  2218. sizeof(*mod->tracepoints_ptrs),
  2219. &mod->num_tracepoints);
  2220. #endif
  2221. #ifdef HAVE_JUMP_LABEL
  2222. mod->jump_entries = section_objs(info, "__jump_table",
  2223. sizeof(*mod->jump_entries),
  2224. &mod->num_jump_entries);
  2225. #endif
  2226. #ifdef CONFIG_EVENT_TRACING
  2227. mod->trace_events = section_objs(info, "_ftrace_events",
  2228. sizeof(*mod->trace_events),
  2229. &mod->num_trace_events);
  2230. /*
  2231. * This section contains pointers to allocated objects in the trace
  2232. * code and not scanning it leads to false positives.
  2233. */
  2234. kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
  2235. mod->num_trace_events, GFP_KERNEL);
  2236. #endif
  2237. #ifdef CONFIG_TRACING
  2238. mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
  2239. sizeof(*mod->trace_bprintk_fmt_start),
  2240. &mod->num_trace_bprintk_fmt);
  2241. /*
  2242. * This section contains pointers to allocated objects in the trace
  2243. * code and not scanning it leads to false positives.
  2244. */
  2245. kmemleak_scan_area(mod->trace_bprintk_fmt_start,
  2246. sizeof(*mod->trace_bprintk_fmt_start) *
  2247. mod->num_trace_bprintk_fmt, GFP_KERNEL);
  2248. #endif
  2249. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2250. /* sechdrs[0].sh_size is always zero */
  2251. mod->ftrace_callsites = section_objs(info, "__mcount_loc",
  2252. sizeof(*mod->ftrace_callsites),
  2253. &mod->num_ftrace_callsites);
  2254. #endif
  2255. mod->extable = section_objs(info, "__ex_table",
  2256. sizeof(*mod->extable), &mod->num_exentries);
  2257. if (section_addr(info, "__obsparm"))
  2258. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  2259. mod->name);
  2260. info->debug = section_objs(info, "__verbose",
  2261. sizeof(*info->debug), &info->num_debug);
  2262. }
  2263. static int move_module(struct module *mod, struct load_info *info)
  2264. {
  2265. int i;
  2266. void *ptr;
  2267. /* Do the allocs. */
  2268. ptr = module_alloc_update_bounds(mod->core_size);
  2269. /*
  2270. * The pointer to this block is stored in the module structure
  2271. * which is inside the block. Just mark it as not being a
  2272. * leak.
  2273. */
  2274. kmemleak_not_leak(ptr);
  2275. if (!ptr)
  2276. return -ENOMEM;
  2277. memset(ptr, 0, mod->core_size);
  2278. mod->module_core = ptr;
  2279. ptr = module_alloc_update_bounds(mod->init_size);
  2280. /*
  2281. * The pointer to this block is stored in the module structure
  2282. * which is inside the block. This block doesn't need to be
  2283. * scanned as it contains data and code that will be freed
  2284. * after the module is initialized.
  2285. */
  2286. kmemleak_ignore(ptr);
  2287. if (!ptr && mod->init_size) {
  2288. module_free(mod, mod->module_core);
  2289. return -ENOMEM;
  2290. }
  2291. memset(ptr, 0, mod->init_size);
  2292. mod->module_init = ptr;
  2293. /* Transfer each section which specifies SHF_ALLOC */
  2294. pr_debug("final section addresses:\n");
  2295. for (i = 0; i < info->hdr->e_shnum; i++) {
  2296. void *dest;
  2297. Elf_Shdr *shdr = &info->sechdrs[i];
  2298. if (!(shdr->sh_flags & SHF_ALLOC))
  2299. continue;
  2300. if (shdr->sh_entsize & INIT_OFFSET_MASK)
  2301. dest = mod->module_init
  2302. + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
  2303. else
  2304. dest = mod->module_core + shdr->sh_entsize;
  2305. if (shdr->sh_type != SHT_NOBITS)
  2306. memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
  2307. /* Update sh_addr to point to copy in image. */
  2308. shdr->sh_addr = (unsigned long)dest;
  2309. pr_debug("\t0x%lx %s\n",
  2310. (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
  2311. }
  2312. return 0;
  2313. }
  2314. static int check_module_license_and_versions(struct module *mod)
  2315. {
  2316. /*
  2317. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2318. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2319. * using GPL-only symbols it needs.
  2320. */
  2321. if (strcmp(mod->name, "ndiswrapper") == 0)
  2322. add_taint(TAINT_PROPRIETARY_MODULE);
  2323. /* driverloader was caught wrongly pretending to be under GPL */
  2324. if (strcmp(mod->name, "driverloader") == 0)
  2325. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  2326. #ifdef CONFIG_MODVERSIONS
  2327. if ((mod->num_syms && !mod->crcs)
  2328. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2329. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2330. #ifdef CONFIG_UNUSED_SYMBOLS
  2331. || (mod->num_unused_syms && !mod->unused_crcs)
  2332. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2333. #endif
  2334. ) {
  2335. return try_to_force_load(mod,
  2336. "no versions for exported symbols");
  2337. }
  2338. #endif
  2339. return 0;
  2340. }
  2341. static void flush_module_icache(const struct module *mod)
  2342. {
  2343. mm_segment_t old_fs;
  2344. /* flush the icache in correct context */
  2345. old_fs = get_fs();
  2346. set_fs(KERNEL_DS);
  2347. /*
  2348. * Flush the instruction cache, since we've played with text.
  2349. * Do it before processing of module parameters, so the module
  2350. * can provide parameter accessor functions of its own.
  2351. */
  2352. if (mod->module_init)
  2353. flush_icache_range((unsigned long)mod->module_init,
  2354. (unsigned long)mod->module_init
  2355. + mod->init_size);
  2356. flush_icache_range((unsigned long)mod->module_core,
  2357. (unsigned long)mod->module_core + mod->core_size);
  2358. set_fs(old_fs);
  2359. }
  2360. int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
  2361. Elf_Shdr *sechdrs,
  2362. char *secstrings,
  2363. struct module *mod)
  2364. {
  2365. return 0;
  2366. }
  2367. static struct module *layout_and_allocate(struct load_info *info)
  2368. {
  2369. /* Module within temporary copy. */
  2370. struct module *mod;
  2371. Elf_Shdr *pcpusec;
  2372. int err;
  2373. mod = setup_load_info(info);
  2374. if (IS_ERR(mod))
  2375. return mod;
  2376. err = check_modinfo(mod, info);
  2377. if (err)
  2378. return ERR_PTR(err);
  2379. /* Allow arches to frob section contents and sizes. */
  2380. err = module_frob_arch_sections(info->hdr, info->sechdrs,
  2381. info->secstrings, mod);
  2382. if (err < 0)
  2383. goto out;
  2384. pcpusec = &info->sechdrs[info->index.pcpu];
  2385. if (pcpusec->sh_size) {
  2386. /* We have a special allocation for this section. */
  2387. err = percpu_modalloc(mod,
  2388. pcpusec->sh_size, pcpusec->sh_addralign);
  2389. if (err)
  2390. goto out;
  2391. pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
  2392. }
  2393. /* Determine total sizes, and put offsets in sh_entsize. For now
  2394. this is done generically; there doesn't appear to be any
  2395. special cases for the architectures. */
  2396. layout_sections(mod, info);
  2397. layout_symtab(mod, info);
  2398. /* Allocate and move to the final place */
  2399. err = move_module(mod, info);
  2400. if (err)
  2401. goto free_percpu;
  2402. /* Module has been copied to its final place now: return it. */
  2403. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2404. kmemleak_load_module(mod, info);
  2405. return mod;
  2406. free_percpu:
  2407. percpu_modfree(mod);
  2408. out:
  2409. return ERR_PTR(err);
  2410. }
  2411. /* mod is no longer valid after this! */
  2412. static void module_deallocate(struct module *mod, struct load_info *info)
  2413. {
  2414. percpu_modfree(mod);
  2415. module_free(mod, mod->module_init);
  2416. module_free(mod, mod->module_core);
  2417. }
  2418. int __weak module_finalize(const Elf_Ehdr *hdr,
  2419. const Elf_Shdr *sechdrs,
  2420. struct module *me)
  2421. {
  2422. return 0;
  2423. }
  2424. static int post_relocation(struct module *mod, const struct load_info *info)
  2425. {
  2426. /* Sort exception table now relocations are done. */
  2427. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2428. /* Copy relocated percpu area over. */
  2429. percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
  2430. info->sechdrs[info->index.pcpu].sh_size);
  2431. /* Setup kallsyms-specific fields. */
  2432. add_kallsyms(mod, info);
  2433. /* Arch-specific module finalizing. */
  2434. return module_finalize(info->hdr, info->sechdrs, mod);
  2435. }
  2436. /* Allocate and load the module: note that size of section 0 is always
  2437. zero, and we rely on this for optional sections. */
  2438. static struct module *load_module(void __user *umod,
  2439. unsigned long len,
  2440. const char __user *uargs)
  2441. {
  2442. struct load_info info = { NULL, };
  2443. struct module *mod;
  2444. long err;
  2445. pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
  2446. umod, len, uargs);
  2447. /* Copy in the blobs from userspace, check they are vaguely sane. */
  2448. err = copy_and_check(&info, umod, len, uargs);
  2449. if (err)
  2450. return ERR_PTR(err);
  2451. /* Figure out module layout, and allocate all the memory. */
  2452. mod = layout_and_allocate(&info);
  2453. if (IS_ERR(mod)) {
  2454. err = PTR_ERR(mod);
  2455. goto free_copy;
  2456. }
  2457. /* Now module is in final location, initialize linked lists, etc. */
  2458. err = module_unload_init(mod);
  2459. if (err)
  2460. goto free_module;
  2461. /* Now we've got everything in the final locations, we can
  2462. * find optional sections. */
  2463. find_module_sections(mod, &info);
  2464. err = check_module_license_and_versions(mod);
  2465. if (err)
  2466. goto free_unload;
  2467. /* Set up MODINFO_ATTR fields */
  2468. setup_modinfo(mod, &info);
  2469. /* Fix up syms, so that st_value is a pointer to location. */
  2470. err = simplify_symbols(mod, &info);
  2471. if (err < 0)
  2472. goto free_modinfo;
  2473. err = apply_relocations(mod, &info);
  2474. if (err < 0)
  2475. goto free_modinfo;
  2476. err = post_relocation(mod, &info);
  2477. if (err < 0)
  2478. goto free_modinfo;
  2479. flush_module_icache(mod);
  2480. /* Now copy in args */
  2481. mod->args = strndup_user(uargs, ~0UL >> 1);
  2482. if (IS_ERR(mod->args)) {
  2483. err = PTR_ERR(mod->args);
  2484. goto free_arch_cleanup;
  2485. }
  2486. /* Mark state as coming so strong_try_module_get() ignores us. */
  2487. mod->state = MODULE_STATE_COMING;
  2488. /* Now sew it into the lists so we can get lockdep and oops
  2489. * info during argument parsing. No one should access us, since
  2490. * strong_try_module_get() will fail.
  2491. * lockdep/oops can run asynchronous, so use the RCU list insertion
  2492. * function to insert in a way safe to concurrent readers.
  2493. * The mutex protects against concurrent writers.
  2494. */
  2495. mutex_lock(&module_mutex);
  2496. if (find_module(mod->name)) {
  2497. err = -EEXIST;
  2498. goto unlock;
  2499. }
  2500. /* This has to be done once we're sure module name is unique. */
  2501. dynamic_debug_setup(info.debug, info.num_debug);
  2502. /* Find duplicate symbols */
  2503. err = verify_export_symbols(mod);
  2504. if (err < 0)
  2505. goto ddebug;
  2506. module_bug_finalize(info.hdr, info.sechdrs, mod);
  2507. list_add_rcu(&mod->list, &modules);
  2508. mutex_unlock(&module_mutex);
  2509. /* Module is ready to execute: parsing args may do that. */
  2510. err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
  2511. if (err < 0)
  2512. goto unlink;
  2513. /* Link in to syfs. */
  2514. err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
  2515. if (err < 0)
  2516. goto unlink;
  2517. /* Get rid of temporary copy. */
  2518. free_copy(&info);
  2519. /* Done! */
  2520. trace_module_load(mod);
  2521. return mod;
  2522. unlink:
  2523. mutex_lock(&module_mutex);
  2524. /* Unlink carefully: kallsyms could be walking list. */
  2525. list_del_rcu(&mod->list);
  2526. module_bug_cleanup(mod);
  2527. ddebug:
  2528. dynamic_debug_remove(info.debug);
  2529. unlock:
  2530. mutex_unlock(&module_mutex);
  2531. synchronize_sched();
  2532. kfree(mod->args);
  2533. free_arch_cleanup:
  2534. module_arch_cleanup(mod);
  2535. free_modinfo:
  2536. free_modinfo(mod);
  2537. free_unload:
  2538. module_unload_free(mod);
  2539. free_module:
  2540. module_deallocate(mod, &info);
  2541. free_copy:
  2542. free_copy(&info);
  2543. return ERR_PTR(err);
  2544. }
  2545. /* Call module constructors. */
  2546. static void do_mod_ctors(struct module *mod)
  2547. {
  2548. #ifdef CONFIG_CONSTRUCTORS
  2549. unsigned long i;
  2550. for (i = 0; i < mod->num_ctors; i++)
  2551. mod->ctors[i]();
  2552. #endif
  2553. }
  2554. /* This is where the real work happens */
  2555. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2556. unsigned long, len, const char __user *, uargs)
  2557. {
  2558. struct module *mod;
  2559. int ret = 0;
  2560. /* Must have permission */
  2561. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2562. return -EPERM;
  2563. /* Do all the hard work */
  2564. mod = load_module(umod, len, uargs);
  2565. if (IS_ERR(mod))
  2566. return PTR_ERR(mod);
  2567. blocking_notifier_call_chain(&module_notify_list,
  2568. MODULE_STATE_COMING, mod);
  2569. /* Set RO and NX regions for core */
  2570. set_section_ro_nx(mod->module_core,
  2571. mod->core_text_size,
  2572. mod->core_ro_size,
  2573. mod->core_size);
  2574. /* Set RO and NX regions for init */
  2575. set_section_ro_nx(mod->module_init,
  2576. mod->init_text_size,
  2577. mod->init_ro_size,
  2578. mod->init_size);
  2579. do_mod_ctors(mod);
  2580. /* Start the module */
  2581. if (mod->init != NULL)
  2582. ret = do_one_initcall(mod->init);
  2583. if (ret < 0) {
  2584. /* Init routine failed: abort. Try to protect us from
  2585. buggy refcounters. */
  2586. mod->state = MODULE_STATE_GOING;
  2587. synchronize_sched();
  2588. module_put(mod);
  2589. blocking_notifier_call_chain(&module_notify_list,
  2590. MODULE_STATE_GOING, mod);
  2591. free_module(mod);
  2592. wake_up(&module_wq);
  2593. return ret;
  2594. }
  2595. if (ret > 0) {
  2596. printk(KERN_WARNING
  2597. "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
  2598. "%s: loading module anyway...\n",
  2599. __func__, mod->name, ret,
  2600. __func__);
  2601. dump_stack();
  2602. }
  2603. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2604. mod->state = MODULE_STATE_LIVE;
  2605. wake_up(&module_wq);
  2606. blocking_notifier_call_chain(&module_notify_list,
  2607. MODULE_STATE_LIVE, mod);
  2608. /* We need to finish all async code before the module init sequence is done */
  2609. async_synchronize_full();
  2610. mutex_lock(&module_mutex);
  2611. /* Drop initial reference. */
  2612. module_put(mod);
  2613. trim_init_extable(mod);
  2614. #ifdef CONFIG_KALLSYMS
  2615. mod->num_symtab = mod->core_num_syms;
  2616. mod->symtab = mod->core_symtab;
  2617. mod->strtab = mod->core_strtab;
  2618. #endif
  2619. unset_module_init_ro_nx(mod);
  2620. module_free(mod, mod->module_init);
  2621. mod->module_init = NULL;
  2622. mod->init_size = 0;
  2623. mod->init_ro_size = 0;
  2624. mod->init_text_size = 0;
  2625. mutex_unlock(&module_mutex);
  2626. return 0;
  2627. }
  2628. static inline int within(unsigned long addr, void *start, unsigned long size)
  2629. {
  2630. return ((void *)addr >= start && (void *)addr < start + size);
  2631. }
  2632. #ifdef CONFIG_KALLSYMS
  2633. /*
  2634. * This ignores the intensely annoying "mapping symbols" found
  2635. * in ARM ELF files: $a, $t and $d.
  2636. */
  2637. static inline int is_arm_mapping_symbol(const char *str)
  2638. {
  2639. return str[0] == '$' && strchr("atd", str[1])
  2640. && (str[2] == '\0' || str[2] == '.');
  2641. }
  2642. static const char *get_ksymbol(struct module *mod,
  2643. unsigned long addr,
  2644. unsigned long *size,
  2645. unsigned long *offset)
  2646. {
  2647. unsigned int i, best = 0;
  2648. unsigned long nextval;
  2649. /* At worse, next value is at end of module */
  2650. if (within_module_init(addr, mod))
  2651. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2652. else
  2653. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2654. /* Scan for closest preceding symbol, and next symbol. (ELF
  2655. starts real symbols at 1). */
  2656. for (i = 1; i < mod->num_symtab; i++) {
  2657. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2658. continue;
  2659. /* We ignore unnamed symbols: they're uninformative
  2660. * and inserted at a whim. */
  2661. if (mod->symtab[i].st_value <= addr
  2662. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2663. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2664. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2665. best = i;
  2666. if (mod->symtab[i].st_value > addr
  2667. && mod->symtab[i].st_value < nextval
  2668. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2669. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2670. nextval = mod->symtab[i].st_value;
  2671. }
  2672. if (!best)
  2673. return NULL;
  2674. if (size)
  2675. *size = nextval - mod->symtab[best].st_value;
  2676. if (offset)
  2677. *offset = addr - mod->symtab[best].st_value;
  2678. return mod->strtab + mod->symtab[best].st_name;
  2679. }
  2680. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2681. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2682. const char *module_address_lookup(unsigned long addr,
  2683. unsigned long *size,
  2684. unsigned long *offset,
  2685. char **modname,
  2686. char *namebuf)
  2687. {
  2688. struct module *mod;
  2689. const char *ret = NULL;
  2690. preempt_disable();
  2691. list_for_each_entry_rcu(mod, &modules, list) {
  2692. if (within_module_init(addr, mod) ||
  2693. within_module_core(addr, mod)) {
  2694. if (modname)
  2695. *modname = mod->name;
  2696. ret = get_ksymbol(mod, addr, size, offset);
  2697. break;
  2698. }
  2699. }
  2700. /* Make a copy in here where it's safe */
  2701. if (ret) {
  2702. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2703. ret = namebuf;
  2704. }
  2705. preempt_enable();
  2706. return ret;
  2707. }
  2708. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2709. {
  2710. struct module *mod;
  2711. preempt_disable();
  2712. list_for_each_entry_rcu(mod, &modules, list) {
  2713. if (within_module_init(addr, mod) ||
  2714. within_module_core(addr, mod)) {
  2715. const char *sym;
  2716. sym = get_ksymbol(mod, addr, NULL, NULL);
  2717. if (!sym)
  2718. goto out;
  2719. strlcpy(symname, sym, KSYM_NAME_LEN);
  2720. preempt_enable();
  2721. return 0;
  2722. }
  2723. }
  2724. out:
  2725. preempt_enable();
  2726. return -ERANGE;
  2727. }
  2728. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2729. unsigned long *offset, char *modname, char *name)
  2730. {
  2731. struct module *mod;
  2732. preempt_disable();
  2733. list_for_each_entry_rcu(mod, &modules, list) {
  2734. if (within_module_init(addr, mod) ||
  2735. within_module_core(addr, mod)) {
  2736. const char *sym;
  2737. sym = get_ksymbol(mod, addr, size, offset);
  2738. if (!sym)
  2739. goto out;
  2740. if (modname)
  2741. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2742. if (name)
  2743. strlcpy(name, sym, KSYM_NAME_LEN);
  2744. preempt_enable();
  2745. return 0;
  2746. }
  2747. }
  2748. out:
  2749. preempt_enable();
  2750. return -ERANGE;
  2751. }
  2752. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2753. char *name, char *module_name, int *exported)
  2754. {
  2755. struct module *mod;
  2756. preempt_disable();
  2757. list_for_each_entry_rcu(mod, &modules, list) {
  2758. if (symnum < mod->num_symtab) {
  2759. *value = mod->symtab[symnum].st_value;
  2760. *type = mod->symtab[symnum].st_info;
  2761. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2762. KSYM_NAME_LEN);
  2763. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2764. *exported = is_exported(name, *value, mod);
  2765. preempt_enable();
  2766. return 0;
  2767. }
  2768. symnum -= mod->num_symtab;
  2769. }
  2770. preempt_enable();
  2771. return -ERANGE;
  2772. }
  2773. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2774. {
  2775. unsigned int i;
  2776. for (i = 0; i < mod->num_symtab; i++)
  2777. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2778. mod->symtab[i].st_info != 'U')
  2779. return mod->symtab[i].st_value;
  2780. return 0;
  2781. }
  2782. /* Look for this name: can be of form module:name. */
  2783. unsigned long module_kallsyms_lookup_name(const char *name)
  2784. {
  2785. struct module *mod;
  2786. char *colon;
  2787. unsigned long ret = 0;
  2788. /* Don't lock: we're in enough trouble already. */
  2789. preempt_disable();
  2790. if ((colon = strchr(name, ':')) != NULL) {
  2791. *colon = '\0';
  2792. if ((mod = find_module(name)) != NULL)
  2793. ret = mod_find_symname(mod, colon+1);
  2794. *colon = ':';
  2795. } else {
  2796. list_for_each_entry_rcu(mod, &modules, list)
  2797. if ((ret = mod_find_symname(mod, name)) != 0)
  2798. break;
  2799. }
  2800. preempt_enable();
  2801. return ret;
  2802. }
  2803. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  2804. struct module *, unsigned long),
  2805. void *data)
  2806. {
  2807. struct module *mod;
  2808. unsigned int i;
  2809. int ret;
  2810. list_for_each_entry(mod, &modules, list) {
  2811. for (i = 0; i < mod->num_symtab; i++) {
  2812. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  2813. mod, mod->symtab[i].st_value);
  2814. if (ret != 0)
  2815. return ret;
  2816. }
  2817. }
  2818. return 0;
  2819. }
  2820. #endif /* CONFIG_KALLSYMS */
  2821. static char *module_flags(struct module *mod, char *buf)
  2822. {
  2823. int bx = 0;
  2824. if (mod->taints ||
  2825. mod->state == MODULE_STATE_GOING ||
  2826. mod->state == MODULE_STATE_COMING) {
  2827. buf[bx++] = '(';
  2828. bx += module_flags_taint(mod, buf + bx);
  2829. /* Show a - for module-is-being-unloaded */
  2830. if (mod->state == MODULE_STATE_GOING)
  2831. buf[bx++] = '-';
  2832. /* Show a + for module-is-being-loaded */
  2833. if (mod->state == MODULE_STATE_COMING)
  2834. buf[bx++] = '+';
  2835. buf[bx++] = ')';
  2836. }
  2837. buf[bx] = '\0';
  2838. return buf;
  2839. }
  2840. #ifdef CONFIG_PROC_FS
  2841. /* Called by the /proc file system to return a list of modules. */
  2842. static void *m_start(struct seq_file *m, loff_t *pos)
  2843. {
  2844. mutex_lock(&module_mutex);
  2845. return seq_list_start(&modules, *pos);
  2846. }
  2847. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2848. {
  2849. return seq_list_next(p, &modules, pos);
  2850. }
  2851. static void m_stop(struct seq_file *m, void *p)
  2852. {
  2853. mutex_unlock(&module_mutex);
  2854. }
  2855. static int m_show(struct seq_file *m, void *p)
  2856. {
  2857. struct module *mod = list_entry(p, struct module, list);
  2858. char buf[8];
  2859. seq_printf(m, "%s %u",
  2860. mod->name, mod->init_size + mod->core_size);
  2861. print_unload_info(m, mod);
  2862. /* Informative for users. */
  2863. seq_printf(m, " %s",
  2864. mod->state == MODULE_STATE_GOING ? "Unloading":
  2865. mod->state == MODULE_STATE_COMING ? "Loading":
  2866. "Live");
  2867. /* Used by oprofile and other similar tools. */
  2868. seq_printf(m, " 0x%pK", mod->module_core);
  2869. /* Taints info */
  2870. if (mod->taints)
  2871. seq_printf(m, " %s", module_flags(mod, buf));
  2872. seq_printf(m, "\n");
  2873. return 0;
  2874. }
  2875. /* Format: modulename size refcount deps address
  2876. Where refcount is a number or -, and deps is a comma-separated list
  2877. of depends or -.
  2878. */
  2879. static const struct seq_operations modules_op = {
  2880. .start = m_start,
  2881. .next = m_next,
  2882. .stop = m_stop,
  2883. .show = m_show
  2884. };
  2885. static int modules_open(struct inode *inode, struct file *file)
  2886. {
  2887. return seq_open(file, &modules_op);
  2888. }
  2889. static const struct file_operations proc_modules_operations = {
  2890. .open = modules_open,
  2891. .read = seq_read,
  2892. .llseek = seq_lseek,
  2893. .release = seq_release,
  2894. };
  2895. static int __init proc_modules_init(void)
  2896. {
  2897. proc_create("modules", 0, NULL, &proc_modules_operations);
  2898. return 0;
  2899. }
  2900. module_init(proc_modules_init);
  2901. #endif
  2902. /* Given an address, look for it in the module exception tables. */
  2903. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2904. {
  2905. const struct exception_table_entry *e = NULL;
  2906. struct module *mod;
  2907. preempt_disable();
  2908. list_for_each_entry_rcu(mod, &modules, list) {
  2909. if (mod->num_exentries == 0)
  2910. continue;
  2911. e = search_extable(mod->extable,
  2912. mod->extable + mod->num_exentries - 1,
  2913. addr);
  2914. if (e)
  2915. break;
  2916. }
  2917. preempt_enable();
  2918. /* Now, if we found one, we are running inside it now, hence
  2919. we cannot unload the module, hence no refcnt needed. */
  2920. return e;
  2921. }
  2922. /*
  2923. * is_module_address - is this address inside a module?
  2924. * @addr: the address to check.
  2925. *
  2926. * See is_module_text_address() if you simply want to see if the address
  2927. * is code (not data).
  2928. */
  2929. bool is_module_address(unsigned long addr)
  2930. {
  2931. bool ret;
  2932. preempt_disable();
  2933. ret = __module_address(addr) != NULL;
  2934. preempt_enable();
  2935. return ret;
  2936. }
  2937. /*
  2938. * __module_address - get the module which contains an address.
  2939. * @addr: the address.
  2940. *
  2941. * Must be called with preempt disabled or module mutex held so that
  2942. * module doesn't get freed during this.
  2943. */
  2944. struct module *__module_address(unsigned long addr)
  2945. {
  2946. struct module *mod;
  2947. if (addr < module_addr_min || addr > module_addr_max)
  2948. return NULL;
  2949. list_for_each_entry_rcu(mod, &modules, list)
  2950. if (within_module_core(addr, mod)
  2951. || within_module_init(addr, mod))
  2952. return mod;
  2953. return NULL;
  2954. }
  2955. EXPORT_SYMBOL_GPL(__module_address);
  2956. /*
  2957. * is_module_text_address - is this address inside module code?
  2958. * @addr: the address to check.
  2959. *
  2960. * See is_module_address() if you simply want to see if the address is
  2961. * anywhere in a module. See kernel_text_address() for testing if an
  2962. * address corresponds to kernel or module code.
  2963. */
  2964. bool is_module_text_address(unsigned long addr)
  2965. {
  2966. bool ret;
  2967. preempt_disable();
  2968. ret = __module_text_address(addr) != NULL;
  2969. preempt_enable();
  2970. return ret;
  2971. }
  2972. /*
  2973. * __module_text_address - get the module whose code contains an address.
  2974. * @addr: the address.
  2975. *
  2976. * Must be called with preempt disabled or module mutex held so that
  2977. * module doesn't get freed during this.
  2978. */
  2979. struct module *__module_text_address(unsigned long addr)
  2980. {
  2981. struct module *mod = __module_address(addr);
  2982. if (mod) {
  2983. /* Make sure it's within the text section. */
  2984. if (!within(addr, mod->module_init, mod->init_text_size)
  2985. && !within(addr, mod->module_core, mod->core_text_size))
  2986. mod = NULL;
  2987. }
  2988. return mod;
  2989. }
  2990. EXPORT_SYMBOL_GPL(__module_text_address);
  2991. /* Don't grab lock, we're oopsing. */
  2992. void print_modules(void)
  2993. {
  2994. struct module *mod;
  2995. char buf[8];
  2996. printk(KERN_DEFAULT "Modules linked in:");
  2997. /* Most callers should already have preempt disabled, but make sure */
  2998. preempt_disable();
  2999. list_for_each_entry_rcu(mod, &modules, list)
  3000. printk(" %s%s", mod->name, module_flags(mod, buf));
  3001. preempt_enable();
  3002. if (last_unloaded_module[0])
  3003. printk(" [last unloaded: %s]", last_unloaded_module);
  3004. printk("\n");
  3005. }
  3006. #ifdef CONFIG_MODVERSIONS
  3007. /* Generate the signature for all relevant module structures here.
  3008. * If these change, we don't want to try to parse the module. */
  3009. void module_layout(struct module *mod,
  3010. struct modversion_info *ver,
  3011. struct kernel_param *kp,
  3012. struct kernel_symbol *ks,
  3013. struct tracepoint * const *tp)
  3014. {
  3015. }
  3016. EXPORT_SYMBOL(module_layout);
  3017. #endif