module.c 97 KB

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