module.c 97 KB

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