module.c 89 KB

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