module.c 104 KB

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