module.c 94 KB

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