module.c 112 KB

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