module.c 104 KB

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