module.c 88 KB

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