module.c 110 KB

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