module.c 103 KB

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