module.c 97 KB

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