module.c 110 KB

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