module.c 110 KB

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