module.c 103 KB

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