module.c 79 KB

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