module.c 92 KB

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