module.c 110 KB

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