module.c 109 KB

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