module.c 88 KB

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