module.c 102 KB

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