module.c 109 KB

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