module.c 110 KB

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