module.c 97 KB

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