module.c 113 KB

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