module.c 98 KB

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