module.c 112 KB

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