module.c 96 KB

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