module.c 96 KB

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