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