module.c 77 KB

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