module.c 79 KB

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