module.c 79 KB

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