module.c 115 KB

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