module.c 84 KB

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