module.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281
  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. /*
  978. * TAINT_FORCED_RMMOD: could be added.
  979. * TAINT_CPU_OUT_OF_SPEC, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  980. * apply to modules.
  981. */
  982. return l;
  983. }
  984. static ssize_t show_initstate(struct module_attribute *mattr,
  985. struct module_kobject *mk, char *buffer)
  986. {
  987. const char *state = "unknown";
  988. switch (mk->mod->state) {
  989. case MODULE_STATE_LIVE:
  990. state = "live";
  991. break;
  992. case MODULE_STATE_COMING:
  993. state = "coming";
  994. break;
  995. case MODULE_STATE_GOING:
  996. state = "going";
  997. break;
  998. default:
  999. BUG();
  1000. }
  1001. return sprintf(buffer, "%s\n", state);
  1002. }
  1003. static struct module_attribute modinfo_initstate =
  1004. __ATTR(initstate, 0444, show_initstate, NULL);
  1005. static ssize_t store_uevent(struct module_attribute *mattr,
  1006. struct module_kobject *mk,
  1007. const char *buffer, size_t count)
  1008. {
  1009. enum kobject_action action;
  1010. if (kobject_action_type(buffer, count, &action) == 0)
  1011. kobject_uevent(&mk->kobj, action);
  1012. return count;
  1013. }
  1014. struct module_attribute module_uevent =
  1015. __ATTR(uevent, 0200, NULL, store_uevent);
  1016. static ssize_t show_coresize(struct module_attribute *mattr,
  1017. struct module_kobject *mk, char *buffer)
  1018. {
  1019. return sprintf(buffer, "%u\n", mk->mod->core_layout.size);
  1020. }
  1021. static struct module_attribute modinfo_coresize =
  1022. __ATTR(coresize, 0444, show_coresize, NULL);
  1023. static ssize_t show_initsize(struct module_attribute *mattr,
  1024. struct module_kobject *mk, char *buffer)
  1025. {
  1026. return sprintf(buffer, "%u\n", mk->mod->init_layout.size);
  1027. }
  1028. static struct module_attribute modinfo_initsize =
  1029. __ATTR(initsize, 0444, show_initsize, NULL);
  1030. static ssize_t show_taint(struct module_attribute *mattr,
  1031. struct module_kobject *mk, char *buffer)
  1032. {
  1033. size_t l;
  1034. l = module_flags_taint(mk->mod, buffer);
  1035. buffer[l++] = '\n';
  1036. return l;
  1037. }
  1038. static struct module_attribute modinfo_taint =
  1039. __ATTR(taint, 0444, show_taint, NULL);
  1040. static struct module_attribute *modinfo_attrs[] = {
  1041. &module_uevent,
  1042. &modinfo_version,
  1043. &modinfo_srcversion,
  1044. &modinfo_initstate,
  1045. &modinfo_coresize,
  1046. &modinfo_initsize,
  1047. &modinfo_taint,
  1048. #ifdef CONFIG_MODULE_UNLOAD
  1049. &modinfo_refcnt,
  1050. #endif
  1051. NULL,
  1052. };
  1053. static const char vermagic[] = VERMAGIC_STRING;
  1054. static int try_to_force_load(struct module *mod, const char *reason)
  1055. {
  1056. #ifdef CONFIG_MODULE_FORCE_LOAD
  1057. if (!test_taint(TAINT_FORCED_MODULE))
  1058. pr_warn("%s: %s: kernel tainted.\n", mod->name, reason);
  1059. add_taint_module(mod, TAINT_FORCED_MODULE, LOCKDEP_NOW_UNRELIABLE);
  1060. return 0;
  1061. #else
  1062. return -ENOEXEC;
  1063. #endif
  1064. }
  1065. #ifdef CONFIG_MODVERSIONS
  1066. /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
  1067. static unsigned long maybe_relocated(unsigned long crc,
  1068. const struct module *crc_owner)
  1069. {
  1070. #ifdef ARCH_RELOCATES_KCRCTAB
  1071. if (crc_owner == NULL)
  1072. return crc - (unsigned long)reloc_start;
  1073. #endif
  1074. return crc;
  1075. }
  1076. static int check_version(Elf_Shdr *sechdrs,
  1077. unsigned int versindex,
  1078. const char *symname,
  1079. struct module *mod,
  1080. const unsigned long *crc,
  1081. const struct module *crc_owner)
  1082. {
  1083. unsigned int i, num_versions;
  1084. struct modversion_info *versions;
  1085. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  1086. if (!crc)
  1087. return 1;
  1088. /* No versions at all? modprobe --force does this. */
  1089. if (versindex == 0)
  1090. return try_to_force_load(mod, symname) == 0;
  1091. versions = (void *) sechdrs[versindex].sh_addr;
  1092. num_versions = sechdrs[versindex].sh_size
  1093. / sizeof(struct modversion_info);
  1094. for (i = 0; i < num_versions; i++) {
  1095. if (strcmp(versions[i].name, symname) != 0)
  1096. continue;
  1097. if (versions[i].crc == maybe_relocated(*crc, crc_owner))
  1098. return 1;
  1099. pr_debug("Found checksum %lX vs module %lX\n",
  1100. maybe_relocated(*crc, crc_owner), versions[i].crc);
  1101. goto bad_version;
  1102. }
  1103. pr_warn("%s: no symbol version for %s\n", mod->name, symname);
  1104. return 0;
  1105. bad_version:
  1106. pr_warn("%s: disagrees about version of symbol %s\n",
  1107. mod->name, symname);
  1108. return 0;
  1109. }
  1110. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  1111. unsigned int versindex,
  1112. struct module *mod)
  1113. {
  1114. const unsigned long *crc;
  1115. /*
  1116. * Since this should be found in kernel (which can't be removed), no
  1117. * locking is necessary -- use preempt_disable() to placate lockdep.
  1118. */
  1119. preempt_disable();
  1120. if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
  1121. &crc, true, false)) {
  1122. preempt_enable();
  1123. BUG();
  1124. }
  1125. preempt_enable();
  1126. return check_version(sechdrs, versindex,
  1127. VMLINUX_SYMBOL_STR(module_layout), mod, crc,
  1128. NULL);
  1129. }
  1130. /* First part is kernel version, which we ignore if module has crcs. */
  1131. static inline int same_magic(const char *amagic, const char *bmagic,
  1132. bool has_crcs)
  1133. {
  1134. if (has_crcs) {
  1135. amagic += strcspn(amagic, " ");
  1136. bmagic += strcspn(bmagic, " ");
  1137. }
  1138. return strcmp(amagic, bmagic) == 0;
  1139. }
  1140. #else
  1141. static inline int check_version(Elf_Shdr *sechdrs,
  1142. unsigned int versindex,
  1143. const char *symname,
  1144. struct module *mod,
  1145. const unsigned long *crc,
  1146. const struct module *crc_owner)
  1147. {
  1148. return 1;
  1149. }
  1150. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  1151. unsigned int versindex,
  1152. struct module *mod)
  1153. {
  1154. return 1;
  1155. }
  1156. static inline int same_magic(const char *amagic, const char *bmagic,
  1157. bool has_crcs)
  1158. {
  1159. return strcmp(amagic, bmagic) == 0;
  1160. }
  1161. #endif /* CONFIG_MODVERSIONS */
  1162. /* Resolve a symbol for this module. I.e. if we find one, record usage. */
  1163. static const struct kernel_symbol *resolve_symbol(struct module *mod,
  1164. const struct load_info *info,
  1165. const char *name,
  1166. char ownername[])
  1167. {
  1168. struct module *owner;
  1169. const struct kernel_symbol *sym;
  1170. const unsigned long *crc;
  1171. int err;
  1172. /*
  1173. * The module_mutex should not be a heavily contended lock;
  1174. * if we get the occasional sleep here, we'll go an extra iteration
  1175. * in the wait_event_interruptible(), which is harmless.
  1176. */
  1177. sched_annotate_sleep();
  1178. mutex_lock(&module_mutex);
  1179. sym = find_symbol(name, &owner, &crc,
  1180. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  1181. if (!sym)
  1182. goto unlock;
  1183. if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
  1184. owner)) {
  1185. sym = ERR_PTR(-EINVAL);
  1186. goto getname;
  1187. }
  1188. err = ref_module(mod, owner);
  1189. if (err) {
  1190. sym = ERR_PTR(err);
  1191. goto getname;
  1192. }
  1193. getname:
  1194. /* We must make copy under the lock if we failed to get ref. */
  1195. strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
  1196. unlock:
  1197. mutex_unlock(&module_mutex);
  1198. return sym;
  1199. }
  1200. static const struct kernel_symbol *
  1201. resolve_symbol_wait(struct module *mod,
  1202. const struct load_info *info,
  1203. const char *name)
  1204. {
  1205. const struct kernel_symbol *ksym;
  1206. char owner[MODULE_NAME_LEN];
  1207. if (wait_event_interruptible_timeout(module_wq,
  1208. !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
  1209. || PTR_ERR(ksym) != -EBUSY,
  1210. 30 * HZ) <= 0) {
  1211. pr_warn("%s: gave up waiting for init of module %s.\n",
  1212. mod->name, owner);
  1213. }
  1214. return ksym;
  1215. }
  1216. /*
  1217. * /sys/module/foo/sections stuff
  1218. * J. Corbet <corbet@lwn.net>
  1219. */
  1220. #ifdef CONFIG_SYSFS
  1221. #ifdef CONFIG_KALLSYMS
  1222. static inline bool sect_empty(const Elf_Shdr *sect)
  1223. {
  1224. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  1225. }
  1226. struct module_sect_attr {
  1227. struct module_attribute mattr;
  1228. char *name;
  1229. unsigned long address;
  1230. };
  1231. struct module_sect_attrs {
  1232. struct attribute_group grp;
  1233. unsigned int nsections;
  1234. struct module_sect_attr attrs[0];
  1235. };
  1236. static ssize_t module_sect_show(struct module_attribute *mattr,
  1237. struct module_kobject *mk, char *buf)
  1238. {
  1239. struct module_sect_attr *sattr =
  1240. container_of(mattr, struct module_sect_attr, mattr);
  1241. return sprintf(buf, "0x%pK\n", (void *)sattr->address);
  1242. }
  1243. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  1244. {
  1245. unsigned int section;
  1246. for (section = 0; section < sect_attrs->nsections; section++)
  1247. kfree(sect_attrs->attrs[section].name);
  1248. kfree(sect_attrs);
  1249. }
  1250. static void add_sect_attrs(struct module *mod, const struct load_info *info)
  1251. {
  1252. unsigned int nloaded = 0, i, size[2];
  1253. struct module_sect_attrs *sect_attrs;
  1254. struct module_sect_attr *sattr;
  1255. struct attribute **gattr;
  1256. /* Count loaded sections and allocate structures */
  1257. for (i = 0; i < info->hdr->e_shnum; i++)
  1258. if (!sect_empty(&info->sechdrs[i]))
  1259. nloaded++;
  1260. size[0] = ALIGN(sizeof(*sect_attrs)
  1261. + nloaded * sizeof(sect_attrs->attrs[0]),
  1262. sizeof(sect_attrs->grp.attrs[0]));
  1263. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  1264. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  1265. if (sect_attrs == NULL)
  1266. return;
  1267. /* Setup section attributes. */
  1268. sect_attrs->grp.name = "sections";
  1269. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  1270. sect_attrs->nsections = 0;
  1271. sattr = &sect_attrs->attrs[0];
  1272. gattr = &sect_attrs->grp.attrs[0];
  1273. for (i = 0; i < info->hdr->e_shnum; i++) {
  1274. Elf_Shdr *sec = &info->sechdrs[i];
  1275. if (sect_empty(sec))
  1276. continue;
  1277. sattr->address = sec->sh_addr;
  1278. sattr->name = kstrdup(info->secstrings + sec->sh_name,
  1279. GFP_KERNEL);
  1280. if (sattr->name == NULL)
  1281. goto out;
  1282. sect_attrs->nsections++;
  1283. sysfs_attr_init(&sattr->mattr.attr);
  1284. sattr->mattr.show = module_sect_show;
  1285. sattr->mattr.store = NULL;
  1286. sattr->mattr.attr.name = sattr->name;
  1287. sattr->mattr.attr.mode = S_IRUGO;
  1288. *(gattr++) = &(sattr++)->mattr.attr;
  1289. }
  1290. *gattr = NULL;
  1291. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1292. goto out;
  1293. mod->sect_attrs = sect_attrs;
  1294. return;
  1295. out:
  1296. free_sect_attrs(sect_attrs);
  1297. }
  1298. static void remove_sect_attrs(struct module *mod)
  1299. {
  1300. if (mod->sect_attrs) {
  1301. sysfs_remove_group(&mod->mkobj.kobj,
  1302. &mod->sect_attrs->grp);
  1303. /* We are positive that no one is using any sect attrs
  1304. * at this point. Deallocate immediately. */
  1305. free_sect_attrs(mod->sect_attrs);
  1306. mod->sect_attrs = NULL;
  1307. }
  1308. }
  1309. /*
  1310. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1311. */
  1312. struct module_notes_attrs {
  1313. struct kobject *dir;
  1314. unsigned int notes;
  1315. struct bin_attribute attrs[0];
  1316. };
  1317. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  1318. struct bin_attribute *bin_attr,
  1319. char *buf, loff_t pos, size_t count)
  1320. {
  1321. /*
  1322. * The caller checked the pos and count against our size.
  1323. */
  1324. memcpy(buf, bin_attr->private + pos, count);
  1325. return count;
  1326. }
  1327. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1328. unsigned int i)
  1329. {
  1330. if (notes_attrs->dir) {
  1331. while (i-- > 0)
  1332. sysfs_remove_bin_file(notes_attrs->dir,
  1333. &notes_attrs->attrs[i]);
  1334. kobject_put(notes_attrs->dir);
  1335. }
  1336. kfree(notes_attrs);
  1337. }
  1338. static void add_notes_attrs(struct module *mod, const struct load_info *info)
  1339. {
  1340. unsigned int notes, loaded, i;
  1341. struct module_notes_attrs *notes_attrs;
  1342. struct bin_attribute *nattr;
  1343. /* failed to create section attributes, so can't create notes */
  1344. if (!mod->sect_attrs)
  1345. return;
  1346. /* Count notes sections and allocate structures. */
  1347. notes = 0;
  1348. for (i = 0; i < info->hdr->e_shnum; i++)
  1349. if (!sect_empty(&info->sechdrs[i]) &&
  1350. (info->sechdrs[i].sh_type == SHT_NOTE))
  1351. ++notes;
  1352. if (notes == 0)
  1353. return;
  1354. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1355. + notes * sizeof(notes_attrs->attrs[0]),
  1356. GFP_KERNEL);
  1357. if (notes_attrs == NULL)
  1358. return;
  1359. notes_attrs->notes = notes;
  1360. nattr = &notes_attrs->attrs[0];
  1361. for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
  1362. if (sect_empty(&info->sechdrs[i]))
  1363. continue;
  1364. if (info->sechdrs[i].sh_type == SHT_NOTE) {
  1365. sysfs_bin_attr_init(nattr);
  1366. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1367. nattr->attr.mode = S_IRUGO;
  1368. nattr->size = info->sechdrs[i].sh_size;
  1369. nattr->private = (void *) info->sechdrs[i].sh_addr;
  1370. nattr->read = module_notes_read;
  1371. ++nattr;
  1372. }
  1373. ++loaded;
  1374. }
  1375. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1376. if (!notes_attrs->dir)
  1377. goto out;
  1378. for (i = 0; i < notes; ++i)
  1379. if (sysfs_create_bin_file(notes_attrs->dir,
  1380. &notes_attrs->attrs[i]))
  1381. goto out;
  1382. mod->notes_attrs = notes_attrs;
  1383. return;
  1384. out:
  1385. free_notes_attrs(notes_attrs, i);
  1386. }
  1387. static void remove_notes_attrs(struct module *mod)
  1388. {
  1389. if (mod->notes_attrs)
  1390. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1391. }
  1392. #else
  1393. static inline void add_sect_attrs(struct module *mod,
  1394. const struct load_info *info)
  1395. {
  1396. }
  1397. static inline void remove_sect_attrs(struct module *mod)
  1398. {
  1399. }
  1400. static inline void add_notes_attrs(struct module *mod,
  1401. const struct load_info *info)
  1402. {
  1403. }
  1404. static inline void remove_notes_attrs(struct module *mod)
  1405. {
  1406. }
  1407. #endif /* CONFIG_KALLSYMS */
  1408. static void add_usage_links(struct module *mod)
  1409. {
  1410. #ifdef CONFIG_MODULE_UNLOAD
  1411. struct module_use *use;
  1412. int nowarn;
  1413. mutex_lock(&module_mutex);
  1414. list_for_each_entry(use, &mod->target_list, target_list) {
  1415. nowarn = sysfs_create_link(use->target->holders_dir,
  1416. &mod->mkobj.kobj, mod->name);
  1417. }
  1418. mutex_unlock(&module_mutex);
  1419. #endif
  1420. }
  1421. static void del_usage_links(struct module *mod)
  1422. {
  1423. #ifdef CONFIG_MODULE_UNLOAD
  1424. struct module_use *use;
  1425. mutex_lock(&module_mutex);
  1426. list_for_each_entry(use, &mod->target_list, target_list)
  1427. sysfs_remove_link(use->target->holders_dir, mod->name);
  1428. mutex_unlock(&module_mutex);
  1429. #endif
  1430. }
  1431. static int module_add_modinfo_attrs(struct module *mod)
  1432. {
  1433. struct module_attribute *attr;
  1434. struct module_attribute *temp_attr;
  1435. int error = 0;
  1436. int i;
  1437. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1438. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1439. GFP_KERNEL);
  1440. if (!mod->modinfo_attrs)
  1441. return -ENOMEM;
  1442. temp_attr = mod->modinfo_attrs;
  1443. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1444. if (!attr->test || attr->test(mod)) {
  1445. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1446. sysfs_attr_init(&temp_attr->attr);
  1447. error = sysfs_create_file(&mod->mkobj.kobj,
  1448. &temp_attr->attr);
  1449. ++temp_attr;
  1450. }
  1451. }
  1452. return error;
  1453. }
  1454. static void module_remove_modinfo_attrs(struct module *mod)
  1455. {
  1456. struct module_attribute *attr;
  1457. int i;
  1458. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1459. /* pick a field to test for end of list */
  1460. if (!attr->attr.name)
  1461. break;
  1462. sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
  1463. if (attr->free)
  1464. attr->free(mod);
  1465. }
  1466. kfree(mod->modinfo_attrs);
  1467. }
  1468. static void mod_kobject_put(struct module *mod)
  1469. {
  1470. DECLARE_COMPLETION_ONSTACK(c);
  1471. mod->mkobj.kobj_completion = &c;
  1472. kobject_put(&mod->mkobj.kobj);
  1473. wait_for_completion(&c);
  1474. }
  1475. static int mod_sysfs_init(struct module *mod)
  1476. {
  1477. int err;
  1478. struct kobject *kobj;
  1479. if (!module_sysfs_initialized) {
  1480. pr_err("%s: module sysfs not initialized\n", mod->name);
  1481. err = -EINVAL;
  1482. goto out;
  1483. }
  1484. kobj = kset_find_obj(module_kset, mod->name);
  1485. if (kobj) {
  1486. pr_err("%s: module is already loaded\n", mod->name);
  1487. kobject_put(kobj);
  1488. err = -EINVAL;
  1489. goto out;
  1490. }
  1491. mod->mkobj.mod = mod;
  1492. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1493. mod->mkobj.kobj.kset = module_kset;
  1494. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1495. "%s", mod->name);
  1496. if (err)
  1497. mod_kobject_put(mod);
  1498. /* delay uevent until full sysfs population */
  1499. out:
  1500. return err;
  1501. }
  1502. static int mod_sysfs_setup(struct module *mod,
  1503. const struct load_info *info,
  1504. struct kernel_param *kparam,
  1505. unsigned int num_params)
  1506. {
  1507. int err;
  1508. err = mod_sysfs_init(mod);
  1509. if (err)
  1510. goto out;
  1511. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1512. if (!mod->holders_dir) {
  1513. err = -ENOMEM;
  1514. goto out_unreg;
  1515. }
  1516. err = module_param_sysfs_setup(mod, kparam, num_params);
  1517. if (err)
  1518. goto out_unreg_holders;
  1519. err = module_add_modinfo_attrs(mod);
  1520. if (err)
  1521. goto out_unreg_param;
  1522. add_usage_links(mod);
  1523. add_sect_attrs(mod, info);
  1524. add_notes_attrs(mod, info);
  1525. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1526. return 0;
  1527. out_unreg_param:
  1528. module_param_sysfs_remove(mod);
  1529. out_unreg_holders:
  1530. kobject_put(mod->holders_dir);
  1531. out_unreg:
  1532. mod_kobject_put(mod);
  1533. out:
  1534. return err;
  1535. }
  1536. static void mod_sysfs_fini(struct module *mod)
  1537. {
  1538. remove_notes_attrs(mod);
  1539. remove_sect_attrs(mod);
  1540. mod_kobject_put(mod);
  1541. }
  1542. static void init_param_lock(struct module *mod)
  1543. {
  1544. mutex_init(&mod->param_lock);
  1545. }
  1546. #else /* !CONFIG_SYSFS */
  1547. static int mod_sysfs_setup(struct module *mod,
  1548. const struct load_info *info,
  1549. struct kernel_param *kparam,
  1550. unsigned int num_params)
  1551. {
  1552. return 0;
  1553. }
  1554. static void mod_sysfs_fini(struct module *mod)
  1555. {
  1556. }
  1557. static void module_remove_modinfo_attrs(struct module *mod)
  1558. {
  1559. }
  1560. static void del_usage_links(struct module *mod)
  1561. {
  1562. }
  1563. static void init_param_lock(struct module *mod)
  1564. {
  1565. }
  1566. #endif /* CONFIG_SYSFS */
  1567. static void mod_sysfs_teardown(struct module *mod)
  1568. {
  1569. del_usage_links(mod);
  1570. module_remove_modinfo_attrs(mod);
  1571. module_param_sysfs_remove(mod);
  1572. kobject_put(mod->mkobj.drivers_dir);
  1573. kobject_put(mod->holders_dir);
  1574. mod_sysfs_fini(mod);
  1575. }
  1576. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  1577. /*
  1578. * LKM RO/NX protection: protect module's text/ro-data
  1579. * from modification and any data from execution.
  1580. *
  1581. * General layout of module is:
  1582. * [text] [read-only-data] [ro-after-init] [writable data]
  1583. * text_size -----^ ^ ^ ^
  1584. * ro_size ------------------------| | |
  1585. * ro_after_init_size -----------------------------| |
  1586. * size -----------------------------------------------------------|
  1587. *
  1588. * These values are always page-aligned (as is base)
  1589. */
  1590. static void frob_text(const struct module_layout *layout,
  1591. int (*set_memory)(unsigned long start, int num_pages))
  1592. {
  1593. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1594. BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
  1595. set_memory((unsigned long)layout->base,
  1596. layout->text_size >> PAGE_SHIFT);
  1597. }
  1598. static void frob_rodata(const struct module_layout *layout,
  1599. int (*set_memory)(unsigned long start, int num_pages))
  1600. {
  1601. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1602. BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
  1603. BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
  1604. set_memory((unsigned long)layout->base + layout->text_size,
  1605. (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
  1606. }
  1607. static void frob_ro_after_init(const struct module_layout *layout,
  1608. int (*set_memory)(unsigned long start, int num_pages))
  1609. {
  1610. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1611. BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
  1612. BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
  1613. set_memory((unsigned long)layout->base + layout->ro_size,
  1614. (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
  1615. }
  1616. static void frob_writable_data(const struct module_layout *layout,
  1617. int (*set_memory)(unsigned long start, int num_pages))
  1618. {
  1619. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1620. BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
  1621. BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
  1622. set_memory((unsigned long)layout->base + layout->ro_after_init_size,
  1623. (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
  1624. }
  1625. /* livepatching wants to disable read-only so it can frob module. */
  1626. void module_disable_ro(const struct module *mod)
  1627. {
  1628. frob_text(&mod->core_layout, set_memory_rw);
  1629. frob_rodata(&mod->core_layout, set_memory_rw);
  1630. frob_ro_after_init(&mod->core_layout, set_memory_rw);
  1631. frob_text(&mod->init_layout, set_memory_rw);
  1632. frob_rodata(&mod->init_layout, set_memory_rw);
  1633. }
  1634. void module_enable_ro(const struct module *mod, bool after_init)
  1635. {
  1636. frob_text(&mod->core_layout, set_memory_ro);
  1637. frob_rodata(&mod->core_layout, set_memory_ro);
  1638. frob_text(&mod->init_layout, set_memory_ro);
  1639. frob_rodata(&mod->init_layout, set_memory_ro);
  1640. if (after_init)
  1641. frob_ro_after_init(&mod->core_layout, set_memory_ro);
  1642. }
  1643. static void module_enable_nx(const struct module *mod)
  1644. {
  1645. frob_rodata(&mod->core_layout, set_memory_nx);
  1646. frob_ro_after_init(&mod->core_layout, set_memory_nx);
  1647. frob_writable_data(&mod->core_layout, set_memory_nx);
  1648. frob_rodata(&mod->init_layout, set_memory_nx);
  1649. frob_writable_data(&mod->init_layout, set_memory_nx);
  1650. }
  1651. static void module_disable_nx(const struct module *mod)
  1652. {
  1653. frob_rodata(&mod->core_layout, set_memory_x);
  1654. frob_ro_after_init(&mod->core_layout, set_memory_x);
  1655. frob_writable_data(&mod->core_layout, set_memory_x);
  1656. frob_rodata(&mod->init_layout, set_memory_x);
  1657. frob_writable_data(&mod->init_layout, set_memory_x);
  1658. }
  1659. /* Iterate through all modules and set each module's text as RW */
  1660. void set_all_modules_text_rw(void)
  1661. {
  1662. struct module *mod;
  1663. mutex_lock(&module_mutex);
  1664. list_for_each_entry_rcu(mod, &modules, list) {
  1665. if (mod->state == MODULE_STATE_UNFORMED)
  1666. continue;
  1667. frob_text(&mod->core_layout, set_memory_rw);
  1668. frob_text(&mod->init_layout, set_memory_rw);
  1669. }
  1670. mutex_unlock(&module_mutex);
  1671. }
  1672. /* Iterate through all modules and set each module's text as RO */
  1673. void set_all_modules_text_ro(void)
  1674. {
  1675. struct module *mod;
  1676. mutex_lock(&module_mutex);
  1677. list_for_each_entry_rcu(mod, &modules, list) {
  1678. if (mod->state == MODULE_STATE_UNFORMED)
  1679. continue;
  1680. frob_text(&mod->core_layout, set_memory_ro);
  1681. frob_text(&mod->init_layout, set_memory_ro);
  1682. }
  1683. mutex_unlock(&module_mutex);
  1684. }
  1685. static void disable_ro_nx(const struct module_layout *layout)
  1686. {
  1687. frob_text(layout, set_memory_rw);
  1688. frob_rodata(layout, set_memory_rw);
  1689. frob_rodata(layout, set_memory_x);
  1690. frob_ro_after_init(layout, set_memory_rw);
  1691. frob_ro_after_init(layout, set_memory_x);
  1692. frob_writable_data(layout, set_memory_x);
  1693. }
  1694. #else
  1695. static void disable_ro_nx(const struct module_layout *layout) { }
  1696. static void module_enable_nx(const struct module *mod) { }
  1697. static void module_disable_nx(const struct module *mod) { }
  1698. #endif
  1699. #ifdef CONFIG_LIVEPATCH
  1700. /*
  1701. * Persist Elf information about a module. Copy the Elf header,
  1702. * section header table, section string table, and symtab section
  1703. * index from info to mod->klp_info.
  1704. */
  1705. static int copy_module_elf(struct module *mod, struct load_info *info)
  1706. {
  1707. unsigned int size, symndx;
  1708. int ret;
  1709. size = sizeof(*mod->klp_info);
  1710. mod->klp_info = kmalloc(size, GFP_KERNEL);
  1711. if (mod->klp_info == NULL)
  1712. return -ENOMEM;
  1713. /* Elf header */
  1714. size = sizeof(mod->klp_info->hdr);
  1715. memcpy(&mod->klp_info->hdr, info->hdr, size);
  1716. /* Elf section header table */
  1717. size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
  1718. mod->klp_info->sechdrs = kmalloc(size, GFP_KERNEL);
  1719. if (mod->klp_info->sechdrs == NULL) {
  1720. ret = -ENOMEM;
  1721. goto free_info;
  1722. }
  1723. memcpy(mod->klp_info->sechdrs, info->sechdrs, size);
  1724. /* Elf section name string table */
  1725. size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
  1726. mod->klp_info->secstrings = kmalloc(size, GFP_KERNEL);
  1727. if (mod->klp_info->secstrings == NULL) {
  1728. ret = -ENOMEM;
  1729. goto free_sechdrs;
  1730. }
  1731. memcpy(mod->klp_info->secstrings, info->secstrings, size);
  1732. /* Elf symbol section index */
  1733. symndx = info->index.sym;
  1734. mod->klp_info->symndx = symndx;
  1735. /*
  1736. * For livepatch modules, core_kallsyms.symtab is a complete
  1737. * copy of the original symbol table. Adjust sh_addr to point
  1738. * to core_kallsyms.symtab since the copy of the symtab in module
  1739. * init memory is freed at the end of do_init_module().
  1740. */
  1741. mod->klp_info->sechdrs[symndx].sh_addr = \
  1742. (unsigned long) mod->core_kallsyms.symtab;
  1743. return 0;
  1744. free_sechdrs:
  1745. kfree(mod->klp_info->sechdrs);
  1746. free_info:
  1747. kfree(mod->klp_info);
  1748. return ret;
  1749. }
  1750. static void free_module_elf(struct module *mod)
  1751. {
  1752. kfree(mod->klp_info->sechdrs);
  1753. kfree(mod->klp_info->secstrings);
  1754. kfree(mod->klp_info);
  1755. }
  1756. #else /* !CONFIG_LIVEPATCH */
  1757. static int copy_module_elf(struct module *mod, struct load_info *info)
  1758. {
  1759. return 0;
  1760. }
  1761. static void free_module_elf(struct module *mod)
  1762. {
  1763. }
  1764. #endif /* CONFIG_LIVEPATCH */
  1765. void __weak module_memfree(void *module_region)
  1766. {
  1767. vfree(module_region);
  1768. }
  1769. void __weak module_arch_cleanup(struct module *mod)
  1770. {
  1771. }
  1772. void __weak module_arch_freeing_init(struct module *mod)
  1773. {
  1774. }
  1775. /* Free a module, remove from lists, etc. */
  1776. static void free_module(struct module *mod)
  1777. {
  1778. trace_module_free(mod);
  1779. mod_sysfs_teardown(mod);
  1780. /* We leave it in list to prevent duplicate loads, but make sure
  1781. * that noone uses it while it's being deconstructed. */
  1782. mutex_lock(&module_mutex);
  1783. mod->state = MODULE_STATE_UNFORMED;
  1784. mutex_unlock(&module_mutex);
  1785. /* Remove dynamic debug info */
  1786. ddebug_remove_module(mod->name);
  1787. /* Arch-specific cleanup. */
  1788. module_arch_cleanup(mod);
  1789. /* Module unload stuff */
  1790. module_unload_free(mod);
  1791. /* Free any allocated parameters. */
  1792. destroy_params(mod->kp, mod->num_kp);
  1793. if (is_livepatch_module(mod))
  1794. free_module_elf(mod);
  1795. /* Now we can delete it from the lists */
  1796. mutex_lock(&module_mutex);
  1797. /* Unlink carefully: kallsyms could be walking list. */
  1798. list_del_rcu(&mod->list);
  1799. mod_tree_remove(mod);
  1800. /* Remove this module from bug list, this uses list_del_rcu */
  1801. module_bug_cleanup(mod);
  1802. /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
  1803. synchronize_sched();
  1804. mutex_unlock(&module_mutex);
  1805. /* This may be empty, but that's OK */
  1806. disable_ro_nx(&mod->init_layout);
  1807. module_arch_freeing_init(mod);
  1808. module_memfree(mod->init_layout.base);
  1809. kfree(mod->args);
  1810. percpu_modfree(mod);
  1811. /* Free lock-classes; relies on the preceding sync_rcu(). */
  1812. lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
  1813. /* Finally, free the core (containing the module structure) */
  1814. disable_ro_nx(&mod->core_layout);
  1815. module_memfree(mod->core_layout.base);
  1816. #ifdef CONFIG_MPU
  1817. update_protections(current->mm);
  1818. #endif
  1819. }
  1820. void *__symbol_get(const char *symbol)
  1821. {
  1822. struct module *owner;
  1823. const struct kernel_symbol *sym;
  1824. preempt_disable();
  1825. sym = find_symbol(symbol, &owner, NULL, true, true);
  1826. if (sym && strong_try_module_get(owner))
  1827. sym = NULL;
  1828. preempt_enable();
  1829. return sym ? (void *)sym->value : NULL;
  1830. }
  1831. EXPORT_SYMBOL_GPL(__symbol_get);
  1832. /*
  1833. * Ensure that an exported symbol [global namespace] does not already exist
  1834. * in the kernel or in some other module's exported symbol table.
  1835. *
  1836. * You must hold the module_mutex.
  1837. */
  1838. static int verify_export_symbols(struct module *mod)
  1839. {
  1840. unsigned int i;
  1841. struct module *owner;
  1842. const struct kernel_symbol *s;
  1843. struct {
  1844. const struct kernel_symbol *sym;
  1845. unsigned int num;
  1846. } arr[] = {
  1847. { mod->syms, mod->num_syms },
  1848. { mod->gpl_syms, mod->num_gpl_syms },
  1849. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1850. #ifdef CONFIG_UNUSED_SYMBOLS
  1851. { mod->unused_syms, mod->num_unused_syms },
  1852. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1853. #endif
  1854. };
  1855. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1856. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1857. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1858. pr_err("%s: exports duplicate symbol %s"
  1859. " (owned by %s)\n",
  1860. mod->name, s->name, module_name(owner));
  1861. return -ENOEXEC;
  1862. }
  1863. }
  1864. }
  1865. return 0;
  1866. }
  1867. /* Change all symbols so that st_value encodes the pointer directly. */
  1868. static int simplify_symbols(struct module *mod, const struct load_info *info)
  1869. {
  1870. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1871. Elf_Sym *sym = (void *)symsec->sh_addr;
  1872. unsigned long secbase;
  1873. unsigned int i;
  1874. int ret = 0;
  1875. const struct kernel_symbol *ksym;
  1876. for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
  1877. const char *name = info->strtab + sym[i].st_name;
  1878. switch (sym[i].st_shndx) {
  1879. case SHN_COMMON:
  1880. /* Ignore common symbols */
  1881. if (!strncmp(name, "__gnu_lto", 9))
  1882. break;
  1883. /* We compiled with -fno-common. These are not
  1884. supposed to happen. */
  1885. pr_debug("Common symbol: %s\n", name);
  1886. pr_warn("%s: please compile with -fno-common\n",
  1887. mod->name);
  1888. ret = -ENOEXEC;
  1889. break;
  1890. case SHN_ABS:
  1891. /* Don't need to do anything */
  1892. pr_debug("Absolute symbol: 0x%08lx\n",
  1893. (long)sym[i].st_value);
  1894. break;
  1895. case SHN_LIVEPATCH:
  1896. /* Livepatch symbols are resolved by livepatch */
  1897. break;
  1898. case SHN_UNDEF:
  1899. ksym = resolve_symbol_wait(mod, info, name);
  1900. /* Ok if resolved. */
  1901. if (ksym && !IS_ERR(ksym)) {
  1902. sym[i].st_value = ksym->value;
  1903. break;
  1904. }
  1905. /* Ok if weak. */
  1906. if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1907. break;
  1908. pr_warn("%s: Unknown symbol %s (err %li)\n",
  1909. mod->name, name, PTR_ERR(ksym));
  1910. ret = PTR_ERR(ksym) ?: -ENOENT;
  1911. break;
  1912. default:
  1913. /* Divert to percpu allocation if a percpu var. */
  1914. if (sym[i].st_shndx == info->index.pcpu)
  1915. secbase = (unsigned long)mod_percpu(mod);
  1916. else
  1917. secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
  1918. sym[i].st_value += secbase;
  1919. break;
  1920. }
  1921. }
  1922. return ret;
  1923. }
  1924. static int apply_relocations(struct module *mod, const struct load_info *info)
  1925. {
  1926. unsigned int i;
  1927. int err = 0;
  1928. /* Now do relocations. */
  1929. for (i = 1; i < info->hdr->e_shnum; i++) {
  1930. unsigned int infosec = info->sechdrs[i].sh_info;
  1931. /* Not a valid relocation section? */
  1932. if (infosec >= info->hdr->e_shnum)
  1933. continue;
  1934. /* Don't bother with non-allocated sections */
  1935. if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
  1936. continue;
  1937. /* Livepatch relocation sections are applied by livepatch */
  1938. if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
  1939. continue;
  1940. if (info->sechdrs[i].sh_type == SHT_REL)
  1941. err = apply_relocate(info->sechdrs, info->strtab,
  1942. info->index.sym, i, mod);
  1943. else if (info->sechdrs[i].sh_type == SHT_RELA)
  1944. err = apply_relocate_add(info->sechdrs, info->strtab,
  1945. info->index.sym, i, mod);
  1946. if (err < 0)
  1947. break;
  1948. }
  1949. return err;
  1950. }
  1951. /* Additional bytes needed by arch in front of individual sections */
  1952. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1953. unsigned int section)
  1954. {
  1955. /* default implementation just returns zero */
  1956. return 0;
  1957. }
  1958. /* Update size with this section: return offset. */
  1959. static long get_offset(struct module *mod, unsigned int *size,
  1960. Elf_Shdr *sechdr, unsigned int section)
  1961. {
  1962. long ret;
  1963. *size += arch_mod_section_prepend(mod, section);
  1964. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1965. *size = ret + sechdr->sh_size;
  1966. return ret;
  1967. }
  1968. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1969. might -- code, read-only data, read-write data, small data. Tally
  1970. sizes, and place the offsets into sh_entsize fields: high bit means it
  1971. belongs in init. */
  1972. static void layout_sections(struct module *mod, struct load_info *info)
  1973. {
  1974. static unsigned long const masks[][2] = {
  1975. /* NOTE: all executable code must be the first section
  1976. * in this array; otherwise modify the text_size
  1977. * finder in the two loops below */
  1978. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1979. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1980. { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
  1981. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1982. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1983. };
  1984. unsigned int m, i;
  1985. for (i = 0; i < info->hdr->e_shnum; i++)
  1986. info->sechdrs[i].sh_entsize = ~0UL;
  1987. pr_debug("Core section allocation order:\n");
  1988. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1989. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1990. Elf_Shdr *s = &info->sechdrs[i];
  1991. const char *sname = info->secstrings + s->sh_name;
  1992. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1993. || (s->sh_flags & masks[m][1])
  1994. || s->sh_entsize != ~0UL
  1995. || strstarts(sname, ".init"))
  1996. continue;
  1997. s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
  1998. pr_debug("\t%s\n", sname);
  1999. }
  2000. switch (m) {
  2001. case 0: /* executable */
  2002. mod->core_layout.size = debug_align(mod->core_layout.size);
  2003. mod->core_layout.text_size = mod->core_layout.size;
  2004. break;
  2005. case 1: /* RO: text and ro-data */
  2006. mod->core_layout.size = debug_align(mod->core_layout.size);
  2007. mod->core_layout.ro_size = mod->core_layout.size;
  2008. break;
  2009. case 2: /* RO after init */
  2010. mod->core_layout.size = debug_align(mod->core_layout.size);
  2011. mod->core_layout.ro_after_init_size = mod->core_layout.size;
  2012. break;
  2013. case 4: /* whole core */
  2014. mod->core_layout.size = debug_align(mod->core_layout.size);
  2015. break;
  2016. }
  2017. }
  2018. pr_debug("Init section allocation order:\n");
  2019. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  2020. for (i = 0; i < info->hdr->e_shnum; ++i) {
  2021. Elf_Shdr *s = &info->sechdrs[i];
  2022. const char *sname = info->secstrings + s->sh_name;
  2023. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  2024. || (s->sh_flags & masks[m][1])
  2025. || s->sh_entsize != ~0UL
  2026. || !strstarts(sname, ".init"))
  2027. continue;
  2028. s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
  2029. | INIT_OFFSET_MASK);
  2030. pr_debug("\t%s\n", sname);
  2031. }
  2032. switch (m) {
  2033. case 0: /* executable */
  2034. mod->init_layout.size = debug_align(mod->init_layout.size);
  2035. mod->init_layout.text_size = mod->init_layout.size;
  2036. break;
  2037. case 1: /* RO: text and ro-data */
  2038. mod->init_layout.size = debug_align(mod->init_layout.size);
  2039. mod->init_layout.ro_size = mod->init_layout.size;
  2040. break;
  2041. case 2:
  2042. /*
  2043. * RO after init doesn't apply to init_layout (only
  2044. * core_layout), so it just takes the value of ro_size.
  2045. */
  2046. mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
  2047. break;
  2048. case 4: /* whole init */
  2049. mod->init_layout.size = debug_align(mod->init_layout.size);
  2050. break;
  2051. }
  2052. }
  2053. }
  2054. static void set_license(struct module *mod, const char *license)
  2055. {
  2056. if (!license)
  2057. license = "unspecified";
  2058. if (!license_is_gpl_compatible(license)) {
  2059. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  2060. pr_warn("%s: module license '%s' taints kernel.\n",
  2061. mod->name, license);
  2062. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2063. LOCKDEP_NOW_UNRELIABLE);
  2064. }
  2065. }
  2066. /* Parse tag=value strings from .modinfo section */
  2067. static char *next_string(char *string, unsigned long *secsize)
  2068. {
  2069. /* Skip non-zero chars */
  2070. while (string[0]) {
  2071. string++;
  2072. if ((*secsize)-- <= 1)
  2073. return NULL;
  2074. }
  2075. /* Skip any zero padding. */
  2076. while (!string[0]) {
  2077. string++;
  2078. if ((*secsize)-- <= 1)
  2079. return NULL;
  2080. }
  2081. return string;
  2082. }
  2083. static char *get_modinfo(struct load_info *info, const char *tag)
  2084. {
  2085. char *p;
  2086. unsigned int taglen = strlen(tag);
  2087. Elf_Shdr *infosec = &info->sechdrs[info->index.info];
  2088. unsigned long size = infosec->sh_size;
  2089. for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
  2090. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  2091. return p + taglen + 1;
  2092. }
  2093. return NULL;
  2094. }
  2095. static void setup_modinfo(struct module *mod, struct load_info *info)
  2096. {
  2097. struct module_attribute *attr;
  2098. int i;
  2099. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  2100. if (attr->setup)
  2101. attr->setup(mod, get_modinfo(info, attr->attr.name));
  2102. }
  2103. }
  2104. static void free_modinfo(struct module *mod)
  2105. {
  2106. struct module_attribute *attr;
  2107. int i;
  2108. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  2109. if (attr->free)
  2110. attr->free(mod);
  2111. }
  2112. }
  2113. #ifdef CONFIG_KALLSYMS
  2114. /* lookup symbol in given range of kernel_symbols */
  2115. static const struct kernel_symbol *lookup_symbol(const char *name,
  2116. const struct kernel_symbol *start,
  2117. const struct kernel_symbol *stop)
  2118. {
  2119. return bsearch(name, start, stop - start,
  2120. sizeof(struct kernel_symbol), cmp_name);
  2121. }
  2122. static int is_exported(const char *name, unsigned long value,
  2123. const struct module *mod)
  2124. {
  2125. const struct kernel_symbol *ks;
  2126. if (!mod)
  2127. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  2128. else
  2129. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  2130. return ks != NULL && ks->value == value;
  2131. }
  2132. /* As per nm */
  2133. static char elf_type(const Elf_Sym *sym, const struct load_info *info)
  2134. {
  2135. const Elf_Shdr *sechdrs = info->sechdrs;
  2136. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  2137. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  2138. return 'v';
  2139. else
  2140. return 'w';
  2141. }
  2142. if (sym->st_shndx == SHN_UNDEF)
  2143. return 'U';
  2144. if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
  2145. return 'a';
  2146. if (sym->st_shndx >= SHN_LORESERVE)
  2147. return '?';
  2148. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  2149. return 't';
  2150. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  2151. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  2152. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  2153. return 'r';
  2154. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2155. return 'g';
  2156. else
  2157. return 'd';
  2158. }
  2159. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  2160. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2161. return 's';
  2162. else
  2163. return 'b';
  2164. }
  2165. if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
  2166. ".debug")) {
  2167. return 'n';
  2168. }
  2169. return '?';
  2170. }
  2171. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  2172. unsigned int shnum, unsigned int pcpundx)
  2173. {
  2174. const Elf_Shdr *sec;
  2175. if (src->st_shndx == SHN_UNDEF
  2176. || src->st_shndx >= shnum
  2177. || !src->st_name)
  2178. return false;
  2179. #ifdef CONFIG_KALLSYMS_ALL
  2180. if (src->st_shndx == pcpundx)
  2181. return true;
  2182. #endif
  2183. sec = sechdrs + src->st_shndx;
  2184. if (!(sec->sh_flags & SHF_ALLOC)
  2185. #ifndef CONFIG_KALLSYMS_ALL
  2186. || !(sec->sh_flags & SHF_EXECINSTR)
  2187. #endif
  2188. || (sec->sh_entsize & INIT_OFFSET_MASK))
  2189. return false;
  2190. return true;
  2191. }
  2192. /*
  2193. * We only allocate and copy the strings needed by the parts of symtab
  2194. * we keep. This is simple, but has the effect of making multiple
  2195. * copies of duplicates. We could be more sophisticated, see
  2196. * linux-kernel thread starting with
  2197. * <73defb5e4bca04a6431392cc341112b1@localhost>.
  2198. */
  2199. static void layout_symtab(struct module *mod, struct load_info *info)
  2200. {
  2201. Elf_Shdr *symsect = info->sechdrs + info->index.sym;
  2202. Elf_Shdr *strsect = info->sechdrs + info->index.str;
  2203. const Elf_Sym *src;
  2204. unsigned int i, nsrc, ndst, strtab_size = 0;
  2205. /* Put symbol section at end of init part of module. */
  2206. symsect->sh_flags |= SHF_ALLOC;
  2207. symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
  2208. info->index.sym) | INIT_OFFSET_MASK;
  2209. pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
  2210. src = (void *)info->hdr + symsect->sh_offset;
  2211. nsrc = symsect->sh_size / sizeof(*src);
  2212. /* Compute total space required for the core symbols' strtab. */
  2213. for (ndst = i = 0; i < nsrc; i++) {
  2214. if (i == 0 || is_livepatch_module(mod) ||
  2215. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
  2216. info->index.pcpu)) {
  2217. strtab_size += strlen(&info->strtab[src[i].st_name])+1;
  2218. ndst++;
  2219. }
  2220. }
  2221. /* Append room for core symbols at end of core part. */
  2222. info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
  2223. info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
  2224. mod->core_layout.size += strtab_size;
  2225. mod->core_layout.size = debug_align(mod->core_layout.size);
  2226. /* Put string table section at end of init part of module. */
  2227. strsect->sh_flags |= SHF_ALLOC;
  2228. strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
  2229. info->index.str) | INIT_OFFSET_MASK;
  2230. pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
  2231. /* We'll tack temporary mod_kallsyms on the end. */
  2232. mod->init_layout.size = ALIGN(mod->init_layout.size,
  2233. __alignof__(struct mod_kallsyms));
  2234. info->mod_kallsyms_init_off = mod->init_layout.size;
  2235. mod->init_layout.size += sizeof(struct mod_kallsyms);
  2236. mod->init_layout.size = debug_align(mod->init_layout.size);
  2237. }
  2238. /*
  2239. * We use the full symtab and strtab which layout_symtab arranged to
  2240. * be appended to the init section. Later we switch to the cut-down
  2241. * core-only ones.
  2242. */
  2243. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2244. {
  2245. unsigned int i, ndst;
  2246. const Elf_Sym *src;
  2247. Elf_Sym *dst;
  2248. char *s;
  2249. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  2250. /* Set up to point into init section. */
  2251. mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
  2252. mod->kallsyms->symtab = (void *)symsec->sh_addr;
  2253. mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
  2254. /* Make sure we get permanent strtab: don't use info->strtab. */
  2255. mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
  2256. /* Set types up while we still have access to sections. */
  2257. for (i = 0; i < mod->kallsyms->num_symtab; i++)
  2258. mod->kallsyms->symtab[i].st_info
  2259. = elf_type(&mod->kallsyms->symtab[i], info);
  2260. /* Now populate the cut down core kallsyms for after init. */
  2261. mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
  2262. mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
  2263. src = mod->kallsyms->symtab;
  2264. for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
  2265. if (i == 0 || is_livepatch_module(mod) ||
  2266. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
  2267. info->index.pcpu)) {
  2268. dst[ndst] = src[i];
  2269. dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
  2270. s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
  2271. KSYM_NAME_LEN) + 1;
  2272. }
  2273. }
  2274. mod->core_kallsyms.num_symtab = ndst;
  2275. }
  2276. #else
  2277. static inline void layout_symtab(struct module *mod, struct load_info *info)
  2278. {
  2279. }
  2280. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2281. {
  2282. }
  2283. #endif /* CONFIG_KALLSYMS */
  2284. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  2285. {
  2286. if (!debug)
  2287. return;
  2288. #ifdef CONFIG_DYNAMIC_DEBUG
  2289. if (ddebug_add_module(debug, num, debug->modname))
  2290. pr_err("dynamic debug error adding module: %s\n",
  2291. debug->modname);
  2292. #endif
  2293. }
  2294. static void dynamic_debug_remove(struct _ddebug *debug)
  2295. {
  2296. if (debug)
  2297. ddebug_remove_module(debug->modname);
  2298. }
  2299. void * __weak module_alloc(unsigned long size)
  2300. {
  2301. return vmalloc_exec(size);
  2302. }
  2303. #ifdef CONFIG_DEBUG_KMEMLEAK
  2304. static void kmemleak_load_module(const struct module *mod,
  2305. const struct load_info *info)
  2306. {
  2307. unsigned int i;
  2308. /* only scan the sections containing data */
  2309. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  2310. for (i = 1; i < info->hdr->e_shnum; i++) {
  2311. /* Scan all writable sections that's not executable */
  2312. if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
  2313. !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
  2314. (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
  2315. continue;
  2316. kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
  2317. info->sechdrs[i].sh_size, GFP_KERNEL);
  2318. }
  2319. }
  2320. #else
  2321. static inline void kmemleak_load_module(const struct module *mod,
  2322. const struct load_info *info)
  2323. {
  2324. }
  2325. #endif
  2326. #ifdef CONFIG_MODULE_SIG
  2327. static int module_sig_check(struct load_info *info, int flags)
  2328. {
  2329. int err = -ENOKEY;
  2330. const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
  2331. const void *mod = info->hdr;
  2332. /*
  2333. * Require flags == 0, as a module with version information
  2334. * removed is no longer the module that was signed
  2335. */
  2336. if (flags == 0 &&
  2337. info->len > markerlen &&
  2338. memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
  2339. /* We truncate the module to discard the signature */
  2340. info->len -= markerlen;
  2341. err = mod_verify_sig(mod, &info->len);
  2342. }
  2343. if (!err) {
  2344. info->sig_ok = true;
  2345. return 0;
  2346. }
  2347. /* Not having a signature is only an error if we're strict. */
  2348. if (err == -ENOKEY && !sig_enforce)
  2349. err = 0;
  2350. return err;
  2351. }
  2352. #else /* !CONFIG_MODULE_SIG */
  2353. static int module_sig_check(struct load_info *info, int flags)
  2354. {
  2355. return 0;
  2356. }
  2357. #endif /* !CONFIG_MODULE_SIG */
  2358. /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
  2359. static int elf_header_check(struct load_info *info)
  2360. {
  2361. if (info->len < sizeof(*(info->hdr)))
  2362. return -ENOEXEC;
  2363. if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
  2364. || info->hdr->e_type != ET_REL
  2365. || !elf_check_arch(info->hdr)
  2366. || info->hdr->e_shentsize != sizeof(Elf_Shdr))
  2367. return -ENOEXEC;
  2368. if (info->hdr->e_shoff >= info->len
  2369. || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
  2370. info->len - info->hdr->e_shoff))
  2371. return -ENOEXEC;
  2372. return 0;
  2373. }
  2374. #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
  2375. static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
  2376. {
  2377. do {
  2378. unsigned long n = min(len, COPY_CHUNK_SIZE);
  2379. if (copy_from_user(dst, usrc, n) != 0)
  2380. return -EFAULT;
  2381. cond_resched();
  2382. dst += n;
  2383. usrc += n;
  2384. len -= n;
  2385. } while (len);
  2386. return 0;
  2387. }
  2388. #ifdef CONFIG_LIVEPATCH
  2389. static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
  2390. {
  2391. mod->klp = get_modinfo(info, "livepatch") ? true : false;
  2392. return 0;
  2393. }
  2394. #else /* !CONFIG_LIVEPATCH */
  2395. static int find_livepatch_modinfo(struct module *mod, struct load_info *info)
  2396. {
  2397. if (get_modinfo(info, "livepatch")) {
  2398. pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
  2399. mod->name);
  2400. return -ENOEXEC;
  2401. }
  2402. return 0;
  2403. }
  2404. #endif /* CONFIG_LIVEPATCH */
  2405. /* Sets info->hdr and info->len. */
  2406. static int copy_module_from_user(const void __user *umod, unsigned long len,
  2407. struct load_info *info)
  2408. {
  2409. int err;
  2410. info->len = len;
  2411. if (info->len < sizeof(*(info->hdr)))
  2412. return -ENOEXEC;
  2413. err = security_kernel_read_file(NULL, READING_MODULE);
  2414. if (err)
  2415. return err;
  2416. /* Suck in entire file: we'll want most of it. */
  2417. info->hdr = __vmalloc(info->len,
  2418. GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
  2419. if (!info->hdr)
  2420. return -ENOMEM;
  2421. if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
  2422. vfree(info->hdr);
  2423. return -EFAULT;
  2424. }
  2425. return 0;
  2426. }
  2427. static void free_copy(struct load_info *info)
  2428. {
  2429. vfree(info->hdr);
  2430. }
  2431. static int rewrite_section_headers(struct load_info *info, int flags)
  2432. {
  2433. unsigned int i;
  2434. /* This should always be true, but let's be sure. */
  2435. info->sechdrs[0].sh_addr = 0;
  2436. for (i = 1; i < info->hdr->e_shnum; i++) {
  2437. Elf_Shdr *shdr = &info->sechdrs[i];
  2438. if (shdr->sh_type != SHT_NOBITS
  2439. && info->len < shdr->sh_offset + shdr->sh_size) {
  2440. pr_err("Module len %lu truncated\n", info->len);
  2441. return -ENOEXEC;
  2442. }
  2443. /* Mark all sections sh_addr with their address in the
  2444. temporary image. */
  2445. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  2446. #ifndef CONFIG_MODULE_UNLOAD
  2447. /* Don't load .exit sections */
  2448. if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
  2449. shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
  2450. #endif
  2451. }
  2452. /* Track but don't keep modinfo and version sections. */
  2453. if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
  2454. info->index.vers = 0; /* Pretend no __versions section! */
  2455. else
  2456. info->index.vers = find_sec(info, "__versions");
  2457. info->index.info = find_sec(info, ".modinfo");
  2458. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2459. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2460. return 0;
  2461. }
  2462. /*
  2463. * Set up our basic convenience variables (pointers to section headers,
  2464. * search for module section index etc), and do some basic section
  2465. * verification.
  2466. *
  2467. * Return the temporary module pointer (we'll replace it with the final
  2468. * one when we move the module sections around).
  2469. */
  2470. static struct module *setup_load_info(struct load_info *info, int flags)
  2471. {
  2472. unsigned int i;
  2473. int err;
  2474. struct module *mod;
  2475. /* Set up the convenience variables */
  2476. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  2477. info->secstrings = (void *)info->hdr
  2478. + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
  2479. err = rewrite_section_headers(info, flags);
  2480. if (err)
  2481. return ERR_PTR(err);
  2482. /* Find internal symbols and strings. */
  2483. for (i = 1; i < info->hdr->e_shnum; i++) {
  2484. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  2485. info->index.sym = i;
  2486. info->index.str = info->sechdrs[i].sh_link;
  2487. info->strtab = (char *)info->hdr
  2488. + info->sechdrs[info->index.str].sh_offset;
  2489. break;
  2490. }
  2491. }
  2492. info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
  2493. if (!info->index.mod) {
  2494. pr_warn("No module found in object\n");
  2495. return ERR_PTR(-ENOEXEC);
  2496. }
  2497. /* This is temporary: point mod into copy of data. */
  2498. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2499. if (info->index.sym == 0) {
  2500. pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
  2501. return ERR_PTR(-ENOEXEC);
  2502. }
  2503. info->index.pcpu = find_pcpusec(info);
  2504. /* Check module struct version now, before we try to use module. */
  2505. if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
  2506. return ERR_PTR(-ENOEXEC);
  2507. return mod;
  2508. }
  2509. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  2510. {
  2511. const char *modmagic = get_modinfo(info, "vermagic");
  2512. int err;
  2513. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  2514. modmagic = NULL;
  2515. /* This is allowed: modprobe --force will invalidate it. */
  2516. if (!modmagic) {
  2517. err = try_to_force_load(mod, "bad vermagic");
  2518. if (err)
  2519. return err;
  2520. } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
  2521. pr_err("%s: version magic '%s' should be '%s'\n",
  2522. mod->name, modmagic, vermagic);
  2523. return -ENOEXEC;
  2524. }
  2525. if (!get_modinfo(info, "intree")) {
  2526. if (!test_taint(TAINT_OOT_MODULE))
  2527. pr_warn("%s: loading out-of-tree module taints kernel.\n",
  2528. mod->name);
  2529. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  2530. }
  2531. if (get_modinfo(info, "staging")) {
  2532. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  2533. pr_warn("%s: module is from the staging directory, the quality "
  2534. "is unknown, you have been warned.\n", mod->name);
  2535. }
  2536. err = find_livepatch_modinfo(mod, info);
  2537. if (err)
  2538. return err;
  2539. /* Set up license info based on the info section */
  2540. set_license(mod, get_modinfo(info, "license"));
  2541. return 0;
  2542. }
  2543. static int find_module_sections(struct module *mod, struct load_info *info)
  2544. {
  2545. mod->kp = section_objs(info, "__param",
  2546. sizeof(*mod->kp), &mod->num_kp);
  2547. mod->syms = section_objs(info, "__ksymtab",
  2548. sizeof(*mod->syms), &mod->num_syms);
  2549. mod->crcs = section_addr(info, "__kcrctab");
  2550. mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
  2551. sizeof(*mod->gpl_syms),
  2552. &mod->num_gpl_syms);
  2553. mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
  2554. mod->gpl_future_syms = section_objs(info,
  2555. "__ksymtab_gpl_future",
  2556. sizeof(*mod->gpl_future_syms),
  2557. &mod->num_gpl_future_syms);
  2558. mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
  2559. #ifdef CONFIG_UNUSED_SYMBOLS
  2560. mod->unused_syms = section_objs(info, "__ksymtab_unused",
  2561. sizeof(*mod->unused_syms),
  2562. &mod->num_unused_syms);
  2563. mod->unused_crcs = section_addr(info, "__kcrctab_unused");
  2564. mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
  2565. sizeof(*mod->unused_gpl_syms),
  2566. &mod->num_unused_gpl_syms);
  2567. mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
  2568. #endif
  2569. #ifdef CONFIG_CONSTRUCTORS
  2570. mod->ctors = section_objs(info, ".ctors",
  2571. sizeof(*mod->ctors), &mod->num_ctors);
  2572. if (!mod->ctors)
  2573. mod->ctors = section_objs(info, ".init_array",
  2574. sizeof(*mod->ctors), &mod->num_ctors);
  2575. else if (find_sec(info, ".init_array")) {
  2576. /*
  2577. * This shouldn't happen with same compiler and binutils
  2578. * building all parts of the module.
  2579. */
  2580. pr_warn("%s: has both .ctors and .init_array.\n",
  2581. mod->name);
  2582. return -EINVAL;
  2583. }
  2584. #endif
  2585. #ifdef CONFIG_TRACEPOINTS
  2586. mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
  2587. sizeof(*mod->tracepoints_ptrs),
  2588. &mod->num_tracepoints);
  2589. #endif
  2590. #ifdef HAVE_JUMP_LABEL
  2591. mod->jump_entries = section_objs(info, "__jump_table",
  2592. sizeof(*mod->jump_entries),
  2593. &mod->num_jump_entries);
  2594. #endif
  2595. #ifdef CONFIG_EVENT_TRACING
  2596. mod->trace_events = section_objs(info, "_ftrace_events",
  2597. sizeof(*mod->trace_events),
  2598. &mod->num_trace_events);
  2599. mod->trace_enums = section_objs(info, "_ftrace_enum_map",
  2600. sizeof(*mod->trace_enums),
  2601. &mod->num_trace_enums);
  2602. #endif
  2603. #ifdef CONFIG_TRACING
  2604. mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
  2605. sizeof(*mod->trace_bprintk_fmt_start),
  2606. &mod->num_trace_bprintk_fmt);
  2607. #endif
  2608. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2609. /* sechdrs[0].sh_size is always zero */
  2610. mod->ftrace_callsites = section_objs(info, "__mcount_loc",
  2611. sizeof(*mod->ftrace_callsites),
  2612. &mod->num_ftrace_callsites);
  2613. #endif
  2614. mod->extable = section_objs(info, "__ex_table",
  2615. sizeof(*mod->extable), &mod->num_exentries);
  2616. if (section_addr(info, "__obsparm"))
  2617. pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
  2618. info->debug = section_objs(info, "__verbose",
  2619. sizeof(*info->debug), &info->num_debug);
  2620. return 0;
  2621. }
  2622. static int move_module(struct module *mod, struct load_info *info)
  2623. {
  2624. int i;
  2625. void *ptr;
  2626. /* Do the allocs. */
  2627. ptr = module_alloc(mod->core_layout.size);
  2628. /*
  2629. * The pointer to this block is stored in the module structure
  2630. * which is inside the block. Just mark it as not being a
  2631. * leak.
  2632. */
  2633. kmemleak_not_leak(ptr);
  2634. if (!ptr)
  2635. return -ENOMEM;
  2636. memset(ptr, 0, mod->core_layout.size);
  2637. mod->core_layout.base = ptr;
  2638. if (mod->init_layout.size) {
  2639. ptr = module_alloc(mod->init_layout.size);
  2640. /*
  2641. * The pointer to this block is stored in the module structure
  2642. * which is inside the block. This block doesn't need to be
  2643. * scanned as it contains data and code that will be freed
  2644. * after the module is initialized.
  2645. */
  2646. kmemleak_ignore(ptr);
  2647. if (!ptr) {
  2648. module_memfree(mod->core_layout.base);
  2649. return -ENOMEM;
  2650. }
  2651. memset(ptr, 0, mod->init_layout.size);
  2652. mod->init_layout.base = ptr;
  2653. } else
  2654. mod->init_layout.base = NULL;
  2655. /* Transfer each section which specifies SHF_ALLOC */
  2656. pr_debug("final section addresses:\n");
  2657. for (i = 0; i < info->hdr->e_shnum; i++) {
  2658. void *dest;
  2659. Elf_Shdr *shdr = &info->sechdrs[i];
  2660. if (!(shdr->sh_flags & SHF_ALLOC))
  2661. continue;
  2662. if (shdr->sh_entsize & INIT_OFFSET_MASK)
  2663. dest = mod->init_layout.base
  2664. + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
  2665. else
  2666. dest = mod->core_layout.base + shdr->sh_entsize;
  2667. if (shdr->sh_type != SHT_NOBITS)
  2668. memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
  2669. /* Update sh_addr to point to copy in image. */
  2670. shdr->sh_addr = (unsigned long)dest;
  2671. pr_debug("\t0x%lx %s\n",
  2672. (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
  2673. }
  2674. return 0;
  2675. }
  2676. static int check_module_license_and_versions(struct module *mod)
  2677. {
  2678. int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
  2679. /*
  2680. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2681. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2682. * using GPL-only symbols it needs.
  2683. */
  2684. if (strcmp(mod->name, "ndiswrapper") == 0)
  2685. add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
  2686. /* driverloader was caught wrongly pretending to be under GPL */
  2687. if (strcmp(mod->name, "driverloader") == 0)
  2688. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2689. LOCKDEP_NOW_UNRELIABLE);
  2690. /* lve claims to be GPL but upstream won't provide source */
  2691. if (strcmp(mod->name, "lve") == 0)
  2692. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2693. LOCKDEP_NOW_UNRELIABLE);
  2694. if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
  2695. pr_warn("%s: module license taints kernel.\n", mod->name);
  2696. #ifdef CONFIG_MODVERSIONS
  2697. if ((mod->num_syms && !mod->crcs)
  2698. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2699. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2700. #ifdef CONFIG_UNUSED_SYMBOLS
  2701. || (mod->num_unused_syms && !mod->unused_crcs)
  2702. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2703. #endif
  2704. ) {
  2705. return try_to_force_load(mod,
  2706. "no versions for exported symbols");
  2707. }
  2708. #endif
  2709. return 0;
  2710. }
  2711. static void flush_module_icache(const struct module *mod)
  2712. {
  2713. mm_segment_t old_fs;
  2714. /* flush the icache in correct context */
  2715. old_fs = get_fs();
  2716. set_fs(KERNEL_DS);
  2717. /*
  2718. * Flush the instruction cache, since we've played with text.
  2719. * Do it before processing of module parameters, so the module
  2720. * can provide parameter accessor functions of its own.
  2721. */
  2722. if (mod->init_layout.base)
  2723. flush_icache_range((unsigned long)mod->init_layout.base,
  2724. (unsigned long)mod->init_layout.base
  2725. + mod->init_layout.size);
  2726. flush_icache_range((unsigned long)mod->core_layout.base,
  2727. (unsigned long)mod->core_layout.base + mod->core_layout.size);
  2728. set_fs(old_fs);
  2729. }
  2730. int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
  2731. Elf_Shdr *sechdrs,
  2732. char *secstrings,
  2733. struct module *mod)
  2734. {
  2735. return 0;
  2736. }
  2737. /* module_blacklist is a comma-separated list of module names */
  2738. static char *module_blacklist;
  2739. static bool blacklisted(char *module_name)
  2740. {
  2741. const char *p;
  2742. size_t len;
  2743. if (!module_blacklist)
  2744. return false;
  2745. for (p = module_blacklist; *p; p += len) {
  2746. len = strcspn(p, ",");
  2747. if (strlen(module_name) == len && !memcmp(module_name, p, len))
  2748. return true;
  2749. if (p[len] == ',')
  2750. len++;
  2751. }
  2752. return false;
  2753. }
  2754. core_param(module_blacklist, module_blacklist, charp, 0400);
  2755. static struct module *layout_and_allocate(struct load_info *info, int flags)
  2756. {
  2757. /* Module within temporary copy. */
  2758. struct module *mod;
  2759. unsigned int ndx;
  2760. int err;
  2761. mod = setup_load_info(info, flags);
  2762. if (IS_ERR(mod))
  2763. return mod;
  2764. if (blacklisted(mod->name))
  2765. return ERR_PTR(-EPERM);
  2766. err = check_modinfo(mod, info, flags);
  2767. if (err)
  2768. return ERR_PTR(err);
  2769. /* Allow arches to frob section contents and sizes. */
  2770. err = module_frob_arch_sections(info->hdr, info->sechdrs,
  2771. info->secstrings, mod);
  2772. if (err < 0)
  2773. return ERR_PTR(err);
  2774. /* We will do a special allocation for per-cpu sections later. */
  2775. info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2776. /*
  2777. * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
  2778. * layout_sections() can put it in the right place.
  2779. * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
  2780. */
  2781. ndx = find_sec(info, ".data..ro_after_init");
  2782. if (ndx)
  2783. info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
  2784. /* Determine total sizes, and put offsets in sh_entsize. For now
  2785. this is done generically; there doesn't appear to be any
  2786. special cases for the architectures. */
  2787. layout_sections(mod, info);
  2788. layout_symtab(mod, info);
  2789. /* Allocate and move to the final place */
  2790. err = move_module(mod, info);
  2791. if (err)
  2792. return ERR_PTR(err);
  2793. /* Module has been copied to its final place now: return it. */
  2794. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2795. kmemleak_load_module(mod, info);
  2796. return mod;
  2797. }
  2798. /* mod is no longer valid after this! */
  2799. static void module_deallocate(struct module *mod, struct load_info *info)
  2800. {
  2801. percpu_modfree(mod);
  2802. module_arch_freeing_init(mod);
  2803. module_memfree(mod->init_layout.base);
  2804. module_memfree(mod->core_layout.base);
  2805. }
  2806. int __weak module_finalize(const Elf_Ehdr *hdr,
  2807. const Elf_Shdr *sechdrs,
  2808. struct module *me)
  2809. {
  2810. return 0;
  2811. }
  2812. static int post_relocation(struct module *mod, const struct load_info *info)
  2813. {
  2814. /* Sort exception table now relocations are done. */
  2815. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2816. /* Copy relocated percpu area over. */
  2817. percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
  2818. info->sechdrs[info->index.pcpu].sh_size);
  2819. /* Setup kallsyms-specific fields. */
  2820. add_kallsyms(mod, info);
  2821. /* Arch-specific module finalizing. */
  2822. return module_finalize(info->hdr, info->sechdrs, mod);
  2823. }
  2824. /* Is this module of this name done loading? No locks held. */
  2825. static bool finished_loading(const char *name)
  2826. {
  2827. struct module *mod;
  2828. bool ret;
  2829. /*
  2830. * The module_mutex should not be a heavily contended lock;
  2831. * if we get the occasional sleep here, we'll go an extra iteration
  2832. * in the wait_event_interruptible(), which is harmless.
  2833. */
  2834. sched_annotate_sleep();
  2835. mutex_lock(&module_mutex);
  2836. mod = find_module_all(name, strlen(name), true);
  2837. ret = !mod || mod->state == MODULE_STATE_LIVE
  2838. || mod->state == MODULE_STATE_GOING;
  2839. mutex_unlock(&module_mutex);
  2840. return ret;
  2841. }
  2842. /* Call module constructors. */
  2843. static void do_mod_ctors(struct module *mod)
  2844. {
  2845. #ifdef CONFIG_CONSTRUCTORS
  2846. unsigned long i;
  2847. for (i = 0; i < mod->num_ctors; i++)
  2848. mod->ctors[i]();
  2849. #endif
  2850. }
  2851. /* For freeing module_init on success, in case kallsyms traversing */
  2852. struct mod_initfree {
  2853. struct rcu_head rcu;
  2854. void *module_init;
  2855. };
  2856. static void do_free_init(struct rcu_head *head)
  2857. {
  2858. struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
  2859. module_memfree(m->module_init);
  2860. kfree(m);
  2861. }
  2862. /*
  2863. * This is where the real work happens.
  2864. *
  2865. * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
  2866. * helper command 'lx-symbols'.
  2867. */
  2868. static noinline int do_init_module(struct module *mod)
  2869. {
  2870. int ret = 0;
  2871. struct mod_initfree *freeinit;
  2872. freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
  2873. if (!freeinit) {
  2874. ret = -ENOMEM;
  2875. goto fail;
  2876. }
  2877. freeinit->module_init = mod->init_layout.base;
  2878. /*
  2879. * We want to find out whether @mod uses async during init. Clear
  2880. * PF_USED_ASYNC. async_schedule*() will set it.
  2881. */
  2882. current->flags &= ~PF_USED_ASYNC;
  2883. do_mod_ctors(mod);
  2884. /* Start the module */
  2885. if (mod->init != NULL)
  2886. ret = do_one_initcall(mod->init);
  2887. if (ret < 0) {
  2888. goto fail_free_freeinit;
  2889. }
  2890. if (ret > 0) {
  2891. pr_warn("%s: '%s'->init suspiciously returned %d, it should "
  2892. "follow 0/-E convention\n"
  2893. "%s: loading module anyway...\n",
  2894. __func__, mod->name, ret, __func__);
  2895. dump_stack();
  2896. }
  2897. /* Now it's a first class citizen! */
  2898. mod->state = MODULE_STATE_LIVE;
  2899. blocking_notifier_call_chain(&module_notify_list,
  2900. MODULE_STATE_LIVE, mod);
  2901. /*
  2902. * We need to finish all async code before the module init sequence
  2903. * is done. This has potential to deadlock. For example, a newly
  2904. * detected block device can trigger request_module() of the
  2905. * default iosched from async probing task. Once userland helper
  2906. * reaches here, async_synchronize_full() will wait on the async
  2907. * task waiting on request_module() and deadlock.
  2908. *
  2909. * This deadlock is avoided by perfomring async_synchronize_full()
  2910. * iff module init queued any async jobs. This isn't a full
  2911. * solution as it will deadlock the same if module loading from
  2912. * async jobs nests more than once; however, due to the various
  2913. * constraints, this hack seems to be the best option for now.
  2914. * Please refer to the following thread for details.
  2915. *
  2916. * http://thread.gmane.org/gmane.linux.kernel/1420814
  2917. */
  2918. if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
  2919. async_synchronize_full();
  2920. mutex_lock(&module_mutex);
  2921. /* Drop initial reference. */
  2922. module_put(mod);
  2923. trim_init_extable(mod);
  2924. #ifdef CONFIG_KALLSYMS
  2925. /* Switch to core kallsyms now init is done: kallsyms may be walking! */
  2926. rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
  2927. #endif
  2928. module_enable_ro(mod, true);
  2929. mod_tree_remove_init(mod);
  2930. disable_ro_nx(&mod->init_layout);
  2931. module_arch_freeing_init(mod);
  2932. mod->init_layout.base = NULL;
  2933. mod->init_layout.size = 0;
  2934. mod->init_layout.ro_size = 0;
  2935. mod->init_layout.ro_after_init_size = 0;
  2936. mod->init_layout.text_size = 0;
  2937. /*
  2938. * We want to free module_init, but be aware that kallsyms may be
  2939. * walking this with preempt disabled. In all the failure paths, we
  2940. * call synchronize_sched(), but we don't want to slow down the success
  2941. * path, so use actual RCU here.
  2942. */
  2943. call_rcu_sched(&freeinit->rcu, do_free_init);
  2944. mutex_unlock(&module_mutex);
  2945. wake_up_all(&module_wq);
  2946. return 0;
  2947. fail_free_freeinit:
  2948. kfree(freeinit);
  2949. fail:
  2950. /* Try to protect us from buggy refcounters. */
  2951. mod->state = MODULE_STATE_GOING;
  2952. synchronize_sched();
  2953. module_put(mod);
  2954. blocking_notifier_call_chain(&module_notify_list,
  2955. MODULE_STATE_GOING, mod);
  2956. klp_module_going(mod);
  2957. ftrace_release_mod(mod);
  2958. free_module(mod);
  2959. wake_up_all(&module_wq);
  2960. return ret;
  2961. }
  2962. static int may_init_module(void)
  2963. {
  2964. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2965. return -EPERM;
  2966. return 0;
  2967. }
  2968. /*
  2969. * We try to place it in the list now to make sure it's unique before
  2970. * we dedicate too many resources. In particular, temporary percpu
  2971. * memory exhaustion.
  2972. */
  2973. static int add_unformed_module(struct module *mod)
  2974. {
  2975. int err;
  2976. struct module *old;
  2977. mod->state = MODULE_STATE_UNFORMED;
  2978. again:
  2979. mutex_lock(&module_mutex);
  2980. old = find_module_all(mod->name, strlen(mod->name), true);
  2981. if (old != NULL) {
  2982. if (old->state == MODULE_STATE_COMING
  2983. || old->state == MODULE_STATE_UNFORMED) {
  2984. /* Wait in case it fails to load. */
  2985. mutex_unlock(&module_mutex);
  2986. err = wait_event_interruptible(module_wq,
  2987. finished_loading(mod->name));
  2988. if (err)
  2989. goto out_unlocked;
  2990. goto again;
  2991. }
  2992. err = -EEXIST;
  2993. goto out;
  2994. }
  2995. mod_update_bounds(mod);
  2996. list_add_rcu(&mod->list, &modules);
  2997. mod_tree_insert(mod);
  2998. err = 0;
  2999. out:
  3000. mutex_unlock(&module_mutex);
  3001. out_unlocked:
  3002. return err;
  3003. }
  3004. static int complete_formation(struct module *mod, struct load_info *info)
  3005. {
  3006. int err;
  3007. mutex_lock(&module_mutex);
  3008. /* Find duplicate symbols (must be called under lock). */
  3009. err = verify_export_symbols(mod);
  3010. if (err < 0)
  3011. goto out;
  3012. /* This relies on module_mutex for list integrity. */
  3013. module_bug_finalize(info->hdr, info->sechdrs, mod);
  3014. module_enable_ro(mod, false);
  3015. module_enable_nx(mod);
  3016. /* Mark state as coming so strong_try_module_get() ignores us,
  3017. * but kallsyms etc. can see us. */
  3018. mod->state = MODULE_STATE_COMING;
  3019. mutex_unlock(&module_mutex);
  3020. return 0;
  3021. out:
  3022. mutex_unlock(&module_mutex);
  3023. return err;
  3024. }
  3025. static int prepare_coming_module(struct module *mod)
  3026. {
  3027. int err;
  3028. ftrace_module_enable(mod);
  3029. err = klp_module_coming(mod);
  3030. if (err)
  3031. return err;
  3032. blocking_notifier_call_chain(&module_notify_list,
  3033. MODULE_STATE_COMING, mod);
  3034. return 0;
  3035. }
  3036. static int unknown_module_param_cb(char *param, char *val, const char *modname,
  3037. void *arg)
  3038. {
  3039. struct module *mod = arg;
  3040. int ret;
  3041. if (strcmp(param, "async_probe") == 0) {
  3042. mod->async_probe_requested = true;
  3043. return 0;
  3044. }
  3045. /* Check for magic 'dyndbg' arg */
  3046. ret = ddebug_dyndbg_module_param_cb(param, val, modname);
  3047. if (ret != 0)
  3048. pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
  3049. return 0;
  3050. }
  3051. /* Allocate and load the module: note that size of section 0 is always
  3052. zero, and we rely on this for optional sections. */
  3053. static int load_module(struct load_info *info, const char __user *uargs,
  3054. int flags)
  3055. {
  3056. struct module *mod;
  3057. long err;
  3058. char *after_dashes;
  3059. err = module_sig_check(info, flags);
  3060. if (err)
  3061. goto free_copy;
  3062. err = elf_header_check(info);
  3063. if (err)
  3064. goto free_copy;
  3065. /* Figure out module layout, and allocate all the memory. */
  3066. mod = layout_and_allocate(info, flags);
  3067. if (IS_ERR(mod)) {
  3068. err = PTR_ERR(mod);
  3069. goto free_copy;
  3070. }
  3071. /* Reserve our place in the list. */
  3072. err = add_unformed_module(mod);
  3073. if (err)
  3074. goto free_module;
  3075. #ifdef CONFIG_MODULE_SIG
  3076. mod->sig_ok = info->sig_ok;
  3077. if (!mod->sig_ok) {
  3078. pr_notice_once("%s: module verification failed: signature "
  3079. "and/or required key missing - tainting "
  3080. "kernel\n", mod->name);
  3081. add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
  3082. }
  3083. #endif
  3084. /* To avoid stressing percpu allocator, do this once we're unique. */
  3085. err = percpu_modalloc(mod, info);
  3086. if (err)
  3087. goto unlink_mod;
  3088. /* Now module is in final location, initialize linked lists, etc. */
  3089. err = module_unload_init(mod);
  3090. if (err)
  3091. goto unlink_mod;
  3092. init_param_lock(mod);
  3093. /* Now we've got everything in the final locations, we can
  3094. * find optional sections. */
  3095. err = find_module_sections(mod, info);
  3096. if (err)
  3097. goto free_unload;
  3098. err = check_module_license_and_versions(mod);
  3099. if (err)
  3100. goto free_unload;
  3101. /* Set up MODINFO_ATTR fields */
  3102. setup_modinfo(mod, info);
  3103. /* Fix up syms, so that st_value is a pointer to location. */
  3104. err = simplify_symbols(mod, info);
  3105. if (err < 0)
  3106. goto free_modinfo;
  3107. err = apply_relocations(mod, info);
  3108. if (err < 0)
  3109. goto free_modinfo;
  3110. err = post_relocation(mod, info);
  3111. if (err < 0)
  3112. goto free_modinfo;
  3113. flush_module_icache(mod);
  3114. /* Now copy in args */
  3115. mod->args = strndup_user(uargs, ~0UL >> 1);
  3116. if (IS_ERR(mod->args)) {
  3117. err = PTR_ERR(mod->args);
  3118. goto free_arch_cleanup;
  3119. }
  3120. dynamic_debug_setup(info->debug, info->num_debug);
  3121. /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
  3122. ftrace_module_init(mod);
  3123. /* Finally it's fully formed, ready to start executing. */
  3124. err = complete_formation(mod, info);
  3125. if (err)
  3126. goto ddebug_cleanup;
  3127. err = prepare_coming_module(mod);
  3128. if (err)
  3129. goto bug_cleanup;
  3130. /* Module is ready to execute: parsing args may do that. */
  3131. after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
  3132. -32768, 32767, mod,
  3133. unknown_module_param_cb);
  3134. if (IS_ERR(after_dashes)) {
  3135. err = PTR_ERR(after_dashes);
  3136. goto coming_cleanup;
  3137. } else if (after_dashes) {
  3138. pr_warn("%s: parameters '%s' after `--' ignored\n",
  3139. mod->name, after_dashes);
  3140. }
  3141. /* Link in to syfs. */
  3142. err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
  3143. if (err < 0)
  3144. goto coming_cleanup;
  3145. if (is_livepatch_module(mod)) {
  3146. err = copy_module_elf(mod, info);
  3147. if (err < 0)
  3148. goto sysfs_cleanup;
  3149. }
  3150. /* Get rid of temporary copy. */
  3151. free_copy(info);
  3152. /* Done! */
  3153. trace_module_load(mod);
  3154. return do_init_module(mod);
  3155. sysfs_cleanup:
  3156. mod_sysfs_teardown(mod);
  3157. coming_cleanup:
  3158. blocking_notifier_call_chain(&module_notify_list,
  3159. MODULE_STATE_GOING, mod);
  3160. klp_module_going(mod);
  3161. bug_cleanup:
  3162. /* module_bug_cleanup needs module_mutex protection */
  3163. mutex_lock(&module_mutex);
  3164. module_bug_cleanup(mod);
  3165. mutex_unlock(&module_mutex);
  3166. /* we can't deallocate the module until we clear memory protection */
  3167. module_disable_ro(mod);
  3168. module_disable_nx(mod);
  3169. ddebug_cleanup:
  3170. dynamic_debug_remove(info->debug);
  3171. synchronize_sched();
  3172. kfree(mod->args);
  3173. free_arch_cleanup:
  3174. module_arch_cleanup(mod);
  3175. free_modinfo:
  3176. free_modinfo(mod);
  3177. free_unload:
  3178. module_unload_free(mod);
  3179. unlink_mod:
  3180. mutex_lock(&module_mutex);
  3181. /* Unlink carefully: kallsyms could be walking list. */
  3182. list_del_rcu(&mod->list);
  3183. mod_tree_remove(mod);
  3184. wake_up_all(&module_wq);
  3185. /* Wait for RCU-sched synchronizing before releasing mod->list. */
  3186. synchronize_sched();
  3187. mutex_unlock(&module_mutex);
  3188. free_module:
  3189. /*
  3190. * Ftrace needs to clean up what it initialized.
  3191. * This does nothing if ftrace_module_init() wasn't called,
  3192. * but it must be called outside of module_mutex.
  3193. */
  3194. ftrace_release_mod(mod);
  3195. /* Free lock-classes; relies on the preceding sync_rcu() */
  3196. lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
  3197. module_deallocate(mod, info);
  3198. free_copy:
  3199. free_copy(info);
  3200. return err;
  3201. }
  3202. SYSCALL_DEFINE3(init_module, void __user *, umod,
  3203. unsigned long, len, const char __user *, uargs)
  3204. {
  3205. int err;
  3206. struct load_info info = { };
  3207. err = may_init_module();
  3208. if (err)
  3209. return err;
  3210. pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
  3211. umod, len, uargs);
  3212. err = copy_module_from_user(umod, len, &info);
  3213. if (err)
  3214. return err;
  3215. return load_module(&info, uargs, 0);
  3216. }
  3217. SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
  3218. {
  3219. struct load_info info = { };
  3220. loff_t size;
  3221. void *hdr;
  3222. int err;
  3223. err = may_init_module();
  3224. if (err)
  3225. return err;
  3226. pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
  3227. if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
  3228. |MODULE_INIT_IGNORE_VERMAGIC))
  3229. return -EINVAL;
  3230. err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
  3231. READING_MODULE);
  3232. if (err)
  3233. return err;
  3234. info.hdr = hdr;
  3235. info.len = size;
  3236. return load_module(&info, uargs, flags);
  3237. }
  3238. static inline int within(unsigned long addr, void *start, unsigned long size)
  3239. {
  3240. return ((void *)addr >= start && (void *)addr < start + size);
  3241. }
  3242. #ifdef CONFIG_KALLSYMS
  3243. /*
  3244. * This ignores the intensely annoying "mapping symbols" found
  3245. * in ARM ELF files: $a, $t and $d.
  3246. */
  3247. static inline int is_arm_mapping_symbol(const char *str)
  3248. {
  3249. if (str[0] == '.' && str[1] == 'L')
  3250. return true;
  3251. return str[0] == '$' && strchr("axtd", str[1])
  3252. && (str[2] == '\0' || str[2] == '.');
  3253. }
  3254. static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
  3255. {
  3256. return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
  3257. }
  3258. static const char *get_ksymbol(struct module *mod,
  3259. unsigned long addr,
  3260. unsigned long *size,
  3261. unsigned long *offset)
  3262. {
  3263. unsigned int i, best = 0;
  3264. unsigned long nextval;
  3265. struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
  3266. /* At worse, next value is at end of module */
  3267. if (within_module_init(addr, mod))
  3268. nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
  3269. else
  3270. nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
  3271. /* Scan for closest preceding symbol, and next symbol. (ELF
  3272. starts real symbols at 1). */
  3273. for (i = 1; i < kallsyms->num_symtab; i++) {
  3274. if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
  3275. continue;
  3276. /* We ignore unnamed symbols: they're uninformative
  3277. * and inserted at a whim. */
  3278. if (*symname(kallsyms, i) == '\0'
  3279. || is_arm_mapping_symbol(symname(kallsyms, i)))
  3280. continue;
  3281. if (kallsyms->symtab[i].st_value <= addr
  3282. && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
  3283. best = i;
  3284. if (kallsyms->symtab[i].st_value > addr
  3285. && kallsyms->symtab[i].st_value < nextval)
  3286. nextval = kallsyms->symtab[i].st_value;
  3287. }
  3288. if (!best)
  3289. return NULL;
  3290. if (size)
  3291. *size = nextval - kallsyms->symtab[best].st_value;
  3292. if (offset)
  3293. *offset = addr - kallsyms->symtab[best].st_value;
  3294. return symname(kallsyms, best);
  3295. }
  3296. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  3297. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  3298. const char *module_address_lookup(unsigned long addr,
  3299. unsigned long *size,
  3300. unsigned long *offset,
  3301. char **modname,
  3302. char *namebuf)
  3303. {
  3304. const char *ret = NULL;
  3305. struct module *mod;
  3306. preempt_disable();
  3307. mod = __module_address(addr);
  3308. if (mod) {
  3309. if (modname)
  3310. *modname = mod->name;
  3311. ret = get_ksymbol(mod, addr, size, offset);
  3312. }
  3313. /* Make a copy in here where it's safe */
  3314. if (ret) {
  3315. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  3316. ret = namebuf;
  3317. }
  3318. preempt_enable();
  3319. return ret;
  3320. }
  3321. int lookup_module_symbol_name(unsigned long addr, char *symname)
  3322. {
  3323. struct module *mod;
  3324. preempt_disable();
  3325. list_for_each_entry_rcu(mod, &modules, list) {
  3326. if (mod->state == MODULE_STATE_UNFORMED)
  3327. continue;
  3328. if (within_module(addr, mod)) {
  3329. const char *sym;
  3330. sym = get_ksymbol(mod, addr, NULL, NULL);
  3331. if (!sym)
  3332. goto out;
  3333. strlcpy(symname, sym, KSYM_NAME_LEN);
  3334. preempt_enable();
  3335. return 0;
  3336. }
  3337. }
  3338. out:
  3339. preempt_enable();
  3340. return -ERANGE;
  3341. }
  3342. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  3343. unsigned long *offset, char *modname, char *name)
  3344. {
  3345. struct module *mod;
  3346. preempt_disable();
  3347. list_for_each_entry_rcu(mod, &modules, list) {
  3348. if (mod->state == MODULE_STATE_UNFORMED)
  3349. continue;
  3350. if (within_module(addr, mod)) {
  3351. const char *sym;
  3352. sym = get_ksymbol(mod, addr, size, offset);
  3353. if (!sym)
  3354. goto out;
  3355. if (modname)
  3356. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  3357. if (name)
  3358. strlcpy(name, sym, KSYM_NAME_LEN);
  3359. preempt_enable();
  3360. return 0;
  3361. }
  3362. }
  3363. out:
  3364. preempt_enable();
  3365. return -ERANGE;
  3366. }
  3367. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  3368. char *name, char *module_name, int *exported)
  3369. {
  3370. struct module *mod;
  3371. preempt_disable();
  3372. list_for_each_entry_rcu(mod, &modules, list) {
  3373. struct mod_kallsyms *kallsyms;
  3374. if (mod->state == MODULE_STATE_UNFORMED)
  3375. continue;
  3376. kallsyms = rcu_dereference_sched(mod->kallsyms);
  3377. if (symnum < kallsyms->num_symtab) {
  3378. *value = kallsyms->symtab[symnum].st_value;
  3379. *type = kallsyms->symtab[symnum].st_info;
  3380. strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
  3381. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  3382. *exported = is_exported(name, *value, mod);
  3383. preempt_enable();
  3384. return 0;
  3385. }
  3386. symnum -= kallsyms->num_symtab;
  3387. }
  3388. preempt_enable();
  3389. return -ERANGE;
  3390. }
  3391. static unsigned long mod_find_symname(struct module *mod, const char *name)
  3392. {
  3393. unsigned int i;
  3394. struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
  3395. for (i = 0; i < kallsyms->num_symtab; i++)
  3396. if (strcmp(name, symname(kallsyms, i)) == 0 &&
  3397. kallsyms->symtab[i].st_info != 'U')
  3398. return kallsyms->symtab[i].st_value;
  3399. return 0;
  3400. }
  3401. /* Look for this name: can be of form module:name. */
  3402. unsigned long module_kallsyms_lookup_name(const char *name)
  3403. {
  3404. struct module *mod;
  3405. char *colon;
  3406. unsigned long ret = 0;
  3407. /* Don't lock: we're in enough trouble already. */
  3408. preempt_disable();
  3409. if ((colon = strchr(name, ':')) != NULL) {
  3410. if ((mod = find_module_all(name, colon - name, false)) != NULL)
  3411. ret = mod_find_symname(mod, colon+1);
  3412. } else {
  3413. list_for_each_entry_rcu(mod, &modules, list) {
  3414. if (mod->state == MODULE_STATE_UNFORMED)
  3415. continue;
  3416. if ((ret = mod_find_symname(mod, name)) != 0)
  3417. break;
  3418. }
  3419. }
  3420. preempt_enable();
  3421. return ret;
  3422. }
  3423. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  3424. struct module *, unsigned long),
  3425. void *data)
  3426. {
  3427. struct module *mod;
  3428. unsigned int i;
  3429. int ret;
  3430. module_assert_mutex();
  3431. list_for_each_entry(mod, &modules, list) {
  3432. /* We hold module_mutex: no need for rcu_dereference_sched */
  3433. struct mod_kallsyms *kallsyms = mod->kallsyms;
  3434. if (mod->state == MODULE_STATE_UNFORMED)
  3435. continue;
  3436. for (i = 0; i < kallsyms->num_symtab; i++) {
  3437. ret = fn(data, symname(kallsyms, i),
  3438. mod, kallsyms->symtab[i].st_value);
  3439. if (ret != 0)
  3440. return ret;
  3441. }
  3442. }
  3443. return 0;
  3444. }
  3445. #endif /* CONFIG_KALLSYMS */
  3446. static char *module_flags(struct module *mod, char *buf)
  3447. {
  3448. int bx = 0;
  3449. BUG_ON(mod->state == MODULE_STATE_UNFORMED);
  3450. if (mod->taints ||
  3451. mod->state == MODULE_STATE_GOING ||
  3452. mod->state == MODULE_STATE_COMING) {
  3453. buf[bx++] = '(';
  3454. bx += module_flags_taint(mod, buf + bx);
  3455. /* Show a - for module-is-being-unloaded */
  3456. if (mod->state == MODULE_STATE_GOING)
  3457. buf[bx++] = '-';
  3458. /* Show a + for module-is-being-loaded */
  3459. if (mod->state == MODULE_STATE_COMING)
  3460. buf[bx++] = '+';
  3461. buf[bx++] = ')';
  3462. }
  3463. buf[bx] = '\0';
  3464. return buf;
  3465. }
  3466. #ifdef CONFIG_PROC_FS
  3467. /* Called by the /proc file system to return a list of modules. */
  3468. static void *m_start(struct seq_file *m, loff_t *pos)
  3469. {
  3470. mutex_lock(&module_mutex);
  3471. return seq_list_start(&modules, *pos);
  3472. }
  3473. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  3474. {
  3475. return seq_list_next(p, &modules, pos);
  3476. }
  3477. static void m_stop(struct seq_file *m, void *p)
  3478. {
  3479. mutex_unlock(&module_mutex);
  3480. }
  3481. static int m_show(struct seq_file *m, void *p)
  3482. {
  3483. struct module *mod = list_entry(p, struct module, list);
  3484. char buf[8];
  3485. /* We always ignore unformed modules. */
  3486. if (mod->state == MODULE_STATE_UNFORMED)
  3487. return 0;
  3488. seq_printf(m, "%s %u",
  3489. mod->name, mod->init_layout.size + mod->core_layout.size);
  3490. print_unload_info(m, mod);
  3491. /* Informative for users. */
  3492. seq_printf(m, " %s",
  3493. mod->state == MODULE_STATE_GOING ? "Unloading" :
  3494. mod->state == MODULE_STATE_COMING ? "Loading" :
  3495. "Live");
  3496. /* Used by oprofile and other similar tools. */
  3497. seq_printf(m, " 0x%pK", mod->core_layout.base);
  3498. /* Taints info */
  3499. if (mod->taints)
  3500. seq_printf(m, " %s", module_flags(mod, buf));
  3501. seq_puts(m, "\n");
  3502. return 0;
  3503. }
  3504. /* Format: modulename size refcount deps address
  3505. Where refcount is a number or -, and deps is a comma-separated list
  3506. of depends or -.
  3507. */
  3508. static const struct seq_operations modules_op = {
  3509. .start = m_start,
  3510. .next = m_next,
  3511. .stop = m_stop,
  3512. .show = m_show
  3513. };
  3514. static int modules_open(struct inode *inode, struct file *file)
  3515. {
  3516. return seq_open(file, &modules_op);
  3517. }
  3518. static const struct file_operations proc_modules_operations = {
  3519. .open = modules_open,
  3520. .read = seq_read,
  3521. .llseek = seq_lseek,
  3522. .release = seq_release,
  3523. };
  3524. static int __init proc_modules_init(void)
  3525. {
  3526. proc_create("modules", 0, NULL, &proc_modules_operations);
  3527. return 0;
  3528. }
  3529. module_init(proc_modules_init);
  3530. #endif
  3531. /* Given an address, look for it in the module exception tables. */
  3532. const struct exception_table_entry *search_module_extables(unsigned long addr)
  3533. {
  3534. const struct exception_table_entry *e = NULL;
  3535. struct module *mod;
  3536. preempt_disable();
  3537. list_for_each_entry_rcu(mod, &modules, list) {
  3538. if (mod->state == MODULE_STATE_UNFORMED)
  3539. continue;
  3540. if (mod->num_exentries == 0)
  3541. continue;
  3542. e = search_extable(mod->extable,
  3543. mod->extable + mod->num_exentries - 1,
  3544. addr);
  3545. if (e)
  3546. break;
  3547. }
  3548. preempt_enable();
  3549. /* Now, if we found one, we are running inside it now, hence
  3550. we cannot unload the module, hence no refcnt needed. */
  3551. return e;
  3552. }
  3553. /*
  3554. * is_module_address - is this address inside a module?
  3555. * @addr: the address to check.
  3556. *
  3557. * See is_module_text_address() if you simply want to see if the address
  3558. * is code (not data).
  3559. */
  3560. bool is_module_address(unsigned long addr)
  3561. {
  3562. bool ret;
  3563. preempt_disable();
  3564. ret = __module_address(addr) != NULL;
  3565. preempt_enable();
  3566. return ret;
  3567. }
  3568. /*
  3569. * __module_address - get the module which contains an address.
  3570. * @addr: the address.
  3571. *
  3572. * Must be called with preempt disabled or module mutex held so that
  3573. * module doesn't get freed during this.
  3574. */
  3575. struct module *__module_address(unsigned long addr)
  3576. {
  3577. struct module *mod;
  3578. if (addr < module_addr_min || addr > module_addr_max)
  3579. return NULL;
  3580. module_assert_mutex_or_preempt();
  3581. mod = mod_find(addr);
  3582. if (mod) {
  3583. BUG_ON(!within_module(addr, mod));
  3584. if (mod->state == MODULE_STATE_UNFORMED)
  3585. mod = NULL;
  3586. }
  3587. return mod;
  3588. }
  3589. EXPORT_SYMBOL_GPL(__module_address);
  3590. /*
  3591. * is_module_text_address - is this address inside module code?
  3592. * @addr: the address to check.
  3593. *
  3594. * See is_module_address() if you simply want to see if the address is
  3595. * anywhere in a module. See kernel_text_address() for testing if an
  3596. * address corresponds to kernel or module code.
  3597. */
  3598. bool is_module_text_address(unsigned long addr)
  3599. {
  3600. bool ret;
  3601. preempt_disable();
  3602. ret = __module_text_address(addr) != NULL;
  3603. preempt_enable();
  3604. return ret;
  3605. }
  3606. /*
  3607. * __module_text_address - get the module whose code contains an address.
  3608. * @addr: the address.
  3609. *
  3610. * Must be called with preempt disabled or module mutex held so that
  3611. * module doesn't get freed during this.
  3612. */
  3613. struct module *__module_text_address(unsigned long addr)
  3614. {
  3615. struct module *mod = __module_address(addr);
  3616. if (mod) {
  3617. /* Make sure it's within the text section. */
  3618. if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
  3619. && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
  3620. mod = NULL;
  3621. }
  3622. return mod;
  3623. }
  3624. EXPORT_SYMBOL_GPL(__module_text_address);
  3625. /* Don't grab lock, we're oopsing. */
  3626. void print_modules(void)
  3627. {
  3628. struct module *mod;
  3629. char buf[8];
  3630. printk(KERN_DEFAULT "Modules linked in:");
  3631. /* Most callers should already have preempt disabled, but make sure */
  3632. preempt_disable();
  3633. list_for_each_entry_rcu(mod, &modules, list) {
  3634. if (mod->state == MODULE_STATE_UNFORMED)
  3635. continue;
  3636. pr_cont(" %s%s", mod->name, module_flags(mod, buf));
  3637. }
  3638. preempt_enable();
  3639. if (last_unloaded_module[0])
  3640. pr_cont(" [last unloaded: %s]", last_unloaded_module);
  3641. pr_cont("\n");
  3642. }
  3643. #ifdef CONFIG_MODVERSIONS
  3644. /* Generate the signature for all relevant module structures here.
  3645. * If these change, we don't want to try to parse the module. */
  3646. void module_layout(struct module *mod,
  3647. struct modversion_info *ver,
  3648. struct kernel_param *kp,
  3649. struct kernel_symbol *ks,
  3650. struct tracepoint * const *tp)
  3651. {
  3652. }
  3653. EXPORT_SYMBOL(module_layout);
  3654. #endif