module.c 107 KB

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