module.c 97 KB

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