module.c 110 KB

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