module.c 115 KB

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