module.c 97 KB

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