module.c 112 KB

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