module.c 89 KB

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