module.c 102 KB

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