module.c 88 KB

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