module.c 88 KB

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