module.c 98 KB

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