module.c 105 KB

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