module.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301
  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 s32 __start___kcrctab[];
  332. extern const s32 __start___kcrctab_gpl[];
  333. extern const s32 __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 s32 __start___kcrctab_unused[];
  340. extern const s32 __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 s32 *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 s32 **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].c_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. static u32 resolve_rel_crc(const s32 *crc)
  1060. {
  1061. return *(u32 *)((void *)crc + *crc);
  1062. }
  1063. static int check_version(Elf_Shdr *sechdrs,
  1064. unsigned int versindex,
  1065. const char *symname,
  1066. struct module *mod,
  1067. const s32 *crc)
  1068. {
  1069. unsigned int i, num_versions;
  1070. struct modversion_info *versions;
  1071. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  1072. if (!crc)
  1073. return 1;
  1074. /* No versions at all? modprobe --force does this. */
  1075. if (versindex == 0)
  1076. return try_to_force_load(mod, symname) == 0;
  1077. versions = (void *) sechdrs[versindex].sh_addr;
  1078. num_versions = sechdrs[versindex].sh_size
  1079. / sizeof(struct modversion_info);
  1080. for (i = 0; i < num_versions; i++) {
  1081. u32 crcval;
  1082. if (strcmp(versions[i].name, symname) != 0)
  1083. continue;
  1084. if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
  1085. crcval = resolve_rel_crc(crc);
  1086. else
  1087. crcval = *crc;
  1088. if (versions[i].crc == crcval)
  1089. return 1;
  1090. pr_debug("Found checksum %X vs module %lX\n",
  1091. crcval, versions[i].crc);
  1092. goto bad_version;
  1093. }
  1094. /* Broken toolchain. Warn once, then let it go.. */
  1095. pr_warn_once("%s: no symbol version for %s\n", mod->name, symname);
  1096. return 1;
  1097. bad_version:
  1098. pr_warn("%s: disagrees about version of symbol %s\n",
  1099. mod->name, symname);
  1100. return 0;
  1101. }
  1102. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  1103. unsigned int versindex,
  1104. struct module *mod)
  1105. {
  1106. const s32 *crc;
  1107. /*
  1108. * Since this should be found in kernel (which can't be removed), no
  1109. * locking is necessary -- use preempt_disable() to placate lockdep.
  1110. */
  1111. preempt_disable();
  1112. if (!find_symbol(VMLINUX_SYMBOL_STR(module_layout), NULL,
  1113. &crc, true, false)) {
  1114. preempt_enable();
  1115. BUG();
  1116. }
  1117. preempt_enable();
  1118. return check_version(sechdrs, versindex,
  1119. VMLINUX_SYMBOL_STR(module_layout), mod, crc);
  1120. }
  1121. /* First part is kernel version, which we ignore if module has crcs. */
  1122. static inline int same_magic(const char *amagic, const char *bmagic,
  1123. bool has_crcs)
  1124. {
  1125. if (has_crcs) {
  1126. amagic += strcspn(amagic, " ");
  1127. bmagic += strcspn(bmagic, " ");
  1128. }
  1129. return strcmp(amagic, bmagic) == 0;
  1130. }
  1131. #else
  1132. static inline int check_version(Elf_Shdr *sechdrs,
  1133. unsigned int versindex,
  1134. const char *symname,
  1135. struct module *mod,
  1136. const s32 *crc)
  1137. {
  1138. return 1;
  1139. }
  1140. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  1141. unsigned int versindex,
  1142. struct module *mod)
  1143. {
  1144. return 1;
  1145. }
  1146. static inline int same_magic(const char *amagic, const char *bmagic,
  1147. bool has_crcs)
  1148. {
  1149. return strcmp(amagic, bmagic) == 0;
  1150. }
  1151. #endif /* CONFIG_MODVERSIONS */
  1152. /* Resolve a symbol for this module. I.e. if we find one, record usage. */
  1153. static const struct kernel_symbol *resolve_symbol(struct module *mod,
  1154. const struct load_info *info,
  1155. const char *name,
  1156. char ownername[])
  1157. {
  1158. struct module *owner;
  1159. const struct kernel_symbol *sym;
  1160. const s32 *crc;
  1161. int err;
  1162. /*
  1163. * The module_mutex should not be a heavily contended lock;
  1164. * if we get the occasional sleep here, we'll go an extra iteration
  1165. * in the wait_event_interruptible(), which is harmless.
  1166. */
  1167. sched_annotate_sleep();
  1168. mutex_lock(&module_mutex);
  1169. sym = find_symbol(name, &owner, &crc,
  1170. !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
  1171. if (!sym)
  1172. goto unlock;
  1173. if (!check_version(info->sechdrs, info->index.vers, name, mod, crc)) {
  1174. sym = ERR_PTR(-EINVAL);
  1175. goto getname;
  1176. }
  1177. err = ref_module(mod, owner);
  1178. if (err) {
  1179. sym = ERR_PTR(err);
  1180. goto getname;
  1181. }
  1182. getname:
  1183. /* We must make copy under the lock if we failed to get ref. */
  1184. strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
  1185. unlock:
  1186. mutex_unlock(&module_mutex);
  1187. return sym;
  1188. }
  1189. static const struct kernel_symbol *
  1190. resolve_symbol_wait(struct module *mod,
  1191. const struct load_info *info,
  1192. const char *name)
  1193. {
  1194. const struct kernel_symbol *ksym;
  1195. char owner[MODULE_NAME_LEN];
  1196. if (wait_event_interruptible_timeout(module_wq,
  1197. !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
  1198. || PTR_ERR(ksym) != -EBUSY,
  1199. 30 * HZ) <= 0) {
  1200. pr_warn("%s: gave up waiting for init of module %s.\n",
  1201. mod->name, owner);
  1202. }
  1203. return ksym;
  1204. }
  1205. /*
  1206. * /sys/module/foo/sections stuff
  1207. * J. Corbet <corbet@lwn.net>
  1208. */
  1209. #ifdef CONFIG_SYSFS
  1210. #ifdef CONFIG_KALLSYMS
  1211. static inline bool sect_empty(const Elf_Shdr *sect)
  1212. {
  1213. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  1214. }
  1215. struct module_sect_attr {
  1216. struct module_attribute mattr;
  1217. char *name;
  1218. unsigned long address;
  1219. };
  1220. struct module_sect_attrs {
  1221. struct attribute_group grp;
  1222. unsigned int nsections;
  1223. struct module_sect_attr attrs[0];
  1224. };
  1225. static ssize_t module_sect_show(struct module_attribute *mattr,
  1226. struct module_kobject *mk, char *buf)
  1227. {
  1228. struct module_sect_attr *sattr =
  1229. container_of(mattr, struct module_sect_attr, mattr);
  1230. return sprintf(buf, "0x%pK\n", (void *)sattr->address);
  1231. }
  1232. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  1233. {
  1234. unsigned int section;
  1235. for (section = 0; section < sect_attrs->nsections; section++)
  1236. kfree(sect_attrs->attrs[section].name);
  1237. kfree(sect_attrs);
  1238. }
  1239. static void add_sect_attrs(struct module *mod, const struct load_info *info)
  1240. {
  1241. unsigned int nloaded = 0, i, size[2];
  1242. struct module_sect_attrs *sect_attrs;
  1243. struct module_sect_attr *sattr;
  1244. struct attribute **gattr;
  1245. /* Count loaded sections and allocate structures */
  1246. for (i = 0; i < info->hdr->e_shnum; i++)
  1247. if (!sect_empty(&info->sechdrs[i]))
  1248. nloaded++;
  1249. size[0] = ALIGN(sizeof(*sect_attrs)
  1250. + nloaded * sizeof(sect_attrs->attrs[0]),
  1251. sizeof(sect_attrs->grp.attrs[0]));
  1252. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  1253. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  1254. if (sect_attrs == NULL)
  1255. return;
  1256. /* Setup section attributes. */
  1257. sect_attrs->grp.name = "sections";
  1258. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  1259. sect_attrs->nsections = 0;
  1260. sattr = &sect_attrs->attrs[0];
  1261. gattr = &sect_attrs->grp.attrs[0];
  1262. for (i = 0; i < info->hdr->e_shnum; i++) {
  1263. Elf_Shdr *sec = &info->sechdrs[i];
  1264. if (sect_empty(sec))
  1265. continue;
  1266. sattr->address = sec->sh_addr;
  1267. sattr->name = kstrdup(info->secstrings + sec->sh_name,
  1268. GFP_KERNEL);
  1269. if (sattr->name == NULL)
  1270. goto out;
  1271. sect_attrs->nsections++;
  1272. sysfs_attr_init(&sattr->mattr.attr);
  1273. sattr->mattr.show = module_sect_show;
  1274. sattr->mattr.store = NULL;
  1275. sattr->mattr.attr.name = sattr->name;
  1276. sattr->mattr.attr.mode = S_IRUGO;
  1277. *(gattr++) = &(sattr++)->mattr.attr;
  1278. }
  1279. *gattr = NULL;
  1280. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  1281. goto out;
  1282. mod->sect_attrs = sect_attrs;
  1283. return;
  1284. out:
  1285. free_sect_attrs(sect_attrs);
  1286. }
  1287. static void remove_sect_attrs(struct module *mod)
  1288. {
  1289. if (mod->sect_attrs) {
  1290. sysfs_remove_group(&mod->mkobj.kobj,
  1291. &mod->sect_attrs->grp);
  1292. /* We are positive that no one is using any sect attrs
  1293. * at this point. Deallocate immediately. */
  1294. free_sect_attrs(mod->sect_attrs);
  1295. mod->sect_attrs = NULL;
  1296. }
  1297. }
  1298. /*
  1299. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  1300. */
  1301. struct module_notes_attrs {
  1302. struct kobject *dir;
  1303. unsigned int notes;
  1304. struct bin_attribute attrs[0];
  1305. };
  1306. static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
  1307. struct bin_attribute *bin_attr,
  1308. char *buf, loff_t pos, size_t count)
  1309. {
  1310. /*
  1311. * The caller checked the pos and count against our size.
  1312. */
  1313. memcpy(buf, bin_attr->private + pos, count);
  1314. return count;
  1315. }
  1316. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  1317. unsigned int i)
  1318. {
  1319. if (notes_attrs->dir) {
  1320. while (i-- > 0)
  1321. sysfs_remove_bin_file(notes_attrs->dir,
  1322. &notes_attrs->attrs[i]);
  1323. kobject_put(notes_attrs->dir);
  1324. }
  1325. kfree(notes_attrs);
  1326. }
  1327. static void add_notes_attrs(struct module *mod, const struct load_info *info)
  1328. {
  1329. unsigned int notes, loaded, i;
  1330. struct module_notes_attrs *notes_attrs;
  1331. struct bin_attribute *nattr;
  1332. /* failed to create section attributes, so can't create notes */
  1333. if (!mod->sect_attrs)
  1334. return;
  1335. /* Count notes sections and allocate structures. */
  1336. notes = 0;
  1337. for (i = 0; i < info->hdr->e_shnum; i++)
  1338. if (!sect_empty(&info->sechdrs[i]) &&
  1339. (info->sechdrs[i].sh_type == SHT_NOTE))
  1340. ++notes;
  1341. if (notes == 0)
  1342. return;
  1343. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1344. + notes * sizeof(notes_attrs->attrs[0]),
  1345. GFP_KERNEL);
  1346. if (notes_attrs == NULL)
  1347. return;
  1348. notes_attrs->notes = notes;
  1349. nattr = &notes_attrs->attrs[0];
  1350. for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
  1351. if (sect_empty(&info->sechdrs[i]))
  1352. continue;
  1353. if (info->sechdrs[i].sh_type == SHT_NOTE) {
  1354. sysfs_bin_attr_init(nattr);
  1355. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1356. nattr->attr.mode = S_IRUGO;
  1357. nattr->size = info->sechdrs[i].sh_size;
  1358. nattr->private = (void *) info->sechdrs[i].sh_addr;
  1359. nattr->read = module_notes_read;
  1360. ++nattr;
  1361. }
  1362. ++loaded;
  1363. }
  1364. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1365. if (!notes_attrs->dir)
  1366. goto out;
  1367. for (i = 0; i < notes; ++i)
  1368. if (sysfs_create_bin_file(notes_attrs->dir,
  1369. &notes_attrs->attrs[i]))
  1370. goto out;
  1371. mod->notes_attrs = notes_attrs;
  1372. return;
  1373. out:
  1374. free_notes_attrs(notes_attrs, i);
  1375. }
  1376. static void remove_notes_attrs(struct module *mod)
  1377. {
  1378. if (mod->notes_attrs)
  1379. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1380. }
  1381. #else
  1382. static inline void add_sect_attrs(struct module *mod,
  1383. const struct load_info *info)
  1384. {
  1385. }
  1386. static inline void remove_sect_attrs(struct module *mod)
  1387. {
  1388. }
  1389. static inline void add_notes_attrs(struct module *mod,
  1390. const struct load_info *info)
  1391. {
  1392. }
  1393. static inline void remove_notes_attrs(struct module *mod)
  1394. {
  1395. }
  1396. #endif /* CONFIG_KALLSYMS */
  1397. static void add_usage_links(struct module *mod)
  1398. {
  1399. #ifdef CONFIG_MODULE_UNLOAD
  1400. struct module_use *use;
  1401. int nowarn;
  1402. mutex_lock(&module_mutex);
  1403. list_for_each_entry(use, &mod->target_list, target_list) {
  1404. nowarn = sysfs_create_link(use->target->holders_dir,
  1405. &mod->mkobj.kobj, mod->name);
  1406. }
  1407. mutex_unlock(&module_mutex);
  1408. #endif
  1409. }
  1410. static void del_usage_links(struct module *mod)
  1411. {
  1412. #ifdef CONFIG_MODULE_UNLOAD
  1413. struct module_use *use;
  1414. mutex_lock(&module_mutex);
  1415. list_for_each_entry(use, &mod->target_list, target_list)
  1416. sysfs_remove_link(use->target->holders_dir, mod->name);
  1417. mutex_unlock(&module_mutex);
  1418. #endif
  1419. }
  1420. static int module_add_modinfo_attrs(struct module *mod)
  1421. {
  1422. struct module_attribute *attr;
  1423. struct module_attribute *temp_attr;
  1424. int error = 0;
  1425. int i;
  1426. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1427. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1428. GFP_KERNEL);
  1429. if (!mod->modinfo_attrs)
  1430. return -ENOMEM;
  1431. temp_attr = mod->modinfo_attrs;
  1432. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1433. if (!attr->test || attr->test(mod)) {
  1434. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1435. sysfs_attr_init(&temp_attr->attr);
  1436. error = sysfs_create_file(&mod->mkobj.kobj,
  1437. &temp_attr->attr);
  1438. ++temp_attr;
  1439. }
  1440. }
  1441. return error;
  1442. }
  1443. static void module_remove_modinfo_attrs(struct module *mod)
  1444. {
  1445. struct module_attribute *attr;
  1446. int i;
  1447. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1448. /* pick a field to test for end of list */
  1449. if (!attr->attr.name)
  1450. break;
  1451. sysfs_remove_file(&mod->mkobj.kobj, &attr->attr);
  1452. if (attr->free)
  1453. attr->free(mod);
  1454. }
  1455. kfree(mod->modinfo_attrs);
  1456. }
  1457. static void mod_kobject_put(struct module *mod)
  1458. {
  1459. DECLARE_COMPLETION_ONSTACK(c);
  1460. mod->mkobj.kobj_completion = &c;
  1461. kobject_put(&mod->mkobj.kobj);
  1462. wait_for_completion(&c);
  1463. }
  1464. static int mod_sysfs_init(struct module *mod)
  1465. {
  1466. int err;
  1467. struct kobject *kobj;
  1468. if (!module_sysfs_initialized) {
  1469. pr_err("%s: module sysfs not initialized\n", mod->name);
  1470. err = -EINVAL;
  1471. goto out;
  1472. }
  1473. kobj = kset_find_obj(module_kset, mod->name);
  1474. if (kobj) {
  1475. pr_err("%s: module is already loaded\n", mod->name);
  1476. kobject_put(kobj);
  1477. err = -EINVAL;
  1478. goto out;
  1479. }
  1480. mod->mkobj.mod = mod;
  1481. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1482. mod->mkobj.kobj.kset = module_kset;
  1483. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1484. "%s", mod->name);
  1485. if (err)
  1486. mod_kobject_put(mod);
  1487. /* delay uevent until full sysfs population */
  1488. out:
  1489. return err;
  1490. }
  1491. static int mod_sysfs_setup(struct module *mod,
  1492. const struct load_info *info,
  1493. struct kernel_param *kparam,
  1494. unsigned int num_params)
  1495. {
  1496. int err;
  1497. err = mod_sysfs_init(mod);
  1498. if (err)
  1499. goto out;
  1500. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1501. if (!mod->holders_dir) {
  1502. err = -ENOMEM;
  1503. goto out_unreg;
  1504. }
  1505. err = module_param_sysfs_setup(mod, kparam, num_params);
  1506. if (err)
  1507. goto out_unreg_holders;
  1508. err = module_add_modinfo_attrs(mod);
  1509. if (err)
  1510. goto out_unreg_param;
  1511. add_usage_links(mod);
  1512. add_sect_attrs(mod, info);
  1513. add_notes_attrs(mod, info);
  1514. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1515. return 0;
  1516. out_unreg_param:
  1517. module_param_sysfs_remove(mod);
  1518. out_unreg_holders:
  1519. kobject_put(mod->holders_dir);
  1520. out_unreg:
  1521. mod_kobject_put(mod);
  1522. out:
  1523. return err;
  1524. }
  1525. static void mod_sysfs_fini(struct module *mod)
  1526. {
  1527. remove_notes_attrs(mod);
  1528. remove_sect_attrs(mod);
  1529. mod_kobject_put(mod);
  1530. }
  1531. static void init_param_lock(struct module *mod)
  1532. {
  1533. mutex_init(&mod->param_lock);
  1534. }
  1535. #else /* !CONFIG_SYSFS */
  1536. static int mod_sysfs_setup(struct module *mod,
  1537. const struct load_info *info,
  1538. struct kernel_param *kparam,
  1539. unsigned int num_params)
  1540. {
  1541. return 0;
  1542. }
  1543. static void mod_sysfs_fini(struct module *mod)
  1544. {
  1545. }
  1546. static void module_remove_modinfo_attrs(struct module *mod)
  1547. {
  1548. }
  1549. static void del_usage_links(struct module *mod)
  1550. {
  1551. }
  1552. static void init_param_lock(struct module *mod)
  1553. {
  1554. }
  1555. #endif /* CONFIG_SYSFS */
  1556. static void mod_sysfs_teardown(struct module *mod)
  1557. {
  1558. del_usage_links(mod);
  1559. module_remove_modinfo_attrs(mod);
  1560. module_param_sysfs_remove(mod);
  1561. kobject_put(mod->mkobj.drivers_dir);
  1562. kobject_put(mod->holders_dir);
  1563. mod_sysfs_fini(mod);
  1564. }
  1565. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  1566. /*
  1567. * LKM RO/NX protection: protect module's text/ro-data
  1568. * from modification and any data from execution.
  1569. *
  1570. * General layout of module is:
  1571. * [text] [read-only-data] [ro-after-init] [writable data]
  1572. * text_size -----^ ^ ^ ^
  1573. * ro_size ------------------------| | |
  1574. * ro_after_init_size -----------------------------| |
  1575. * size -----------------------------------------------------------|
  1576. *
  1577. * These values are always page-aligned (as is base)
  1578. */
  1579. static void frob_text(const struct module_layout *layout,
  1580. int (*set_memory)(unsigned long start, int num_pages))
  1581. {
  1582. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1583. BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
  1584. set_memory((unsigned long)layout->base,
  1585. layout->text_size >> PAGE_SHIFT);
  1586. }
  1587. static void frob_rodata(const struct module_layout *layout,
  1588. int (*set_memory)(unsigned long start, int num_pages))
  1589. {
  1590. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1591. BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1));
  1592. BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
  1593. set_memory((unsigned long)layout->base + layout->text_size,
  1594. (layout->ro_size - layout->text_size) >> PAGE_SHIFT);
  1595. }
  1596. static void frob_ro_after_init(const struct module_layout *layout,
  1597. int (*set_memory)(unsigned long start, int num_pages))
  1598. {
  1599. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1600. BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1));
  1601. BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
  1602. set_memory((unsigned long)layout->base + layout->ro_size,
  1603. (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT);
  1604. }
  1605. static void frob_writable_data(const struct module_layout *layout,
  1606. int (*set_memory)(unsigned long start, int num_pages))
  1607. {
  1608. BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1));
  1609. BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1));
  1610. BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1));
  1611. set_memory((unsigned long)layout->base + layout->ro_after_init_size,
  1612. (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT);
  1613. }
  1614. /* livepatching wants to disable read-only so it can frob module. */
  1615. void module_disable_ro(const struct module *mod)
  1616. {
  1617. if (!rodata_enabled)
  1618. return;
  1619. frob_text(&mod->core_layout, set_memory_rw);
  1620. frob_rodata(&mod->core_layout, set_memory_rw);
  1621. frob_ro_after_init(&mod->core_layout, set_memory_rw);
  1622. frob_text(&mod->init_layout, set_memory_rw);
  1623. frob_rodata(&mod->init_layout, set_memory_rw);
  1624. }
  1625. void module_enable_ro(const struct module *mod, bool after_init)
  1626. {
  1627. if (!rodata_enabled)
  1628. return;
  1629. frob_text(&mod->core_layout, set_memory_ro);
  1630. frob_rodata(&mod->core_layout, set_memory_ro);
  1631. frob_text(&mod->init_layout, set_memory_ro);
  1632. frob_rodata(&mod->init_layout, set_memory_ro);
  1633. if (after_init)
  1634. frob_ro_after_init(&mod->core_layout, set_memory_ro);
  1635. }
  1636. static void module_enable_nx(const struct module *mod)
  1637. {
  1638. frob_rodata(&mod->core_layout, set_memory_nx);
  1639. frob_ro_after_init(&mod->core_layout, set_memory_nx);
  1640. frob_writable_data(&mod->core_layout, set_memory_nx);
  1641. frob_rodata(&mod->init_layout, set_memory_nx);
  1642. frob_writable_data(&mod->init_layout, set_memory_nx);
  1643. }
  1644. static void module_disable_nx(const struct module *mod)
  1645. {
  1646. frob_rodata(&mod->core_layout, set_memory_x);
  1647. frob_ro_after_init(&mod->core_layout, set_memory_x);
  1648. frob_writable_data(&mod->core_layout, set_memory_x);
  1649. frob_rodata(&mod->init_layout, set_memory_x);
  1650. frob_writable_data(&mod->init_layout, set_memory_x);
  1651. }
  1652. /* Iterate through all modules and set each module's text as RW */
  1653. void set_all_modules_text_rw(void)
  1654. {
  1655. struct module *mod;
  1656. if (!rodata_enabled)
  1657. return;
  1658. mutex_lock(&module_mutex);
  1659. list_for_each_entry_rcu(mod, &modules, list) {
  1660. if (mod->state == MODULE_STATE_UNFORMED)
  1661. continue;
  1662. frob_text(&mod->core_layout, set_memory_rw);
  1663. frob_text(&mod->init_layout, set_memory_rw);
  1664. }
  1665. mutex_unlock(&module_mutex);
  1666. }
  1667. /* Iterate through all modules and set each module's text as RO */
  1668. void set_all_modules_text_ro(void)
  1669. {
  1670. struct module *mod;
  1671. if (!rodata_enabled)
  1672. return;
  1673. mutex_lock(&module_mutex);
  1674. list_for_each_entry_rcu(mod, &modules, list) {
  1675. /*
  1676. * Ignore going modules since it's possible that ro
  1677. * protection has already been disabled, otherwise we'll
  1678. * run into protection faults at module deallocation.
  1679. */
  1680. if (mod->state == MODULE_STATE_UNFORMED ||
  1681. mod->state == MODULE_STATE_GOING)
  1682. continue;
  1683. frob_text(&mod->core_layout, set_memory_ro);
  1684. frob_text(&mod->init_layout, set_memory_ro);
  1685. }
  1686. mutex_unlock(&module_mutex);
  1687. }
  1688. static void disable_ro_nx(const struct module_layout *layout)
  1689. {
  1690. if (rodata_enabled) {
  1691. frob_text(layout, set_memory_rw);
  1692. frob_rodata(layout, set_memory_rw);
  1693. frob_ro_after_init(layout, set_memory_rw);
  1694. }
  1695. frob_rodata(layout, set_memory_x);
  1696. frob_ro_after_init(layout, set_memory_x);
  1697. frob_writable_data(layout, set_memory_x);
  1698. }
  1699. #else
  1700. static void disable_ro_nx(const struct module_layout *layout) { }
  1701. static void module_enable_nx(const struct module *mod) { }
  1702. static void module_disable_nx(const struct module *mod) { }
  1703. #endif
  1704. #ifdef CONFIG_LIVEPATCH
  1705. /*
  1706. * Persist Elf information about a module. Copy the Elf header,
  1707. * section header table, section string table, and symtab section
  1708. * index from info to mod->klp_info.
  1709. */
  1710. static int copy_module_elf(struct module *mod, struct load_info *info)
  1711. {
  1712. unsigned int size, symndx;
  1713. int ret;
  1714. size = sizeof(*mod->klp_info);
  1715. mod->klp_info = kmalloc(size, GFP_KERNEL);
  1716. if (mod->klp_info == NULL)
  1717. return -ENOMEM;
  1718. /* Elf header */
  1719. size = sizeof(mod->klp_info->hdr);
  1720. memcpy(&mod->klp_info->hdr, info->hdr, size);
  1721. /* Elf section header table */
  1722. size = sizeof(*info->sechdrs) * info->hdr->e_shnum;
  1723. mod->klp_info->sechdrs = kmalloc(size, GFP_KERNEL);
  1724. if (mod->klp_info->sechdrs == NULL) {
  1725. ret = -ENOMEM;
  1726. goto free_info;
  1727. }
  1728. memcpy(mod->klp_info->sechdrs, info->sechdrs, size);
  1729. /* Elf section name string table */
  1730. size = info->sechdrs[info->hdr->e_shstrndx].sh_size;
  1731. mod->klp_info->secstrings = kmalloc(size, GFP_KERNEL);
  1732. if (mod->klp_info->secstrings == NULL) {
  1733. ret = -ENOMEM;
  1734. goto free_sechdrs;
  1735. }
  1736. memcpy(mod->klp_info->secstrings, info->secstrings, size);
  1737. /* Elf symbol section index */
  1738. symndx = info->index.sym;
  1739. mod->klp_info->symndx = symndx;
  1740. /*
  1741. * For livepatch modules, core_kallsyms.symtab is a complete
  1742. * copy of the original symbol table. Adjust sh_addr to point
  1743. * to core_kallsyms.symtab since the copy of the symtab in module
  1744. * init memory is freed at the end of do_init_module().
  1745. */
  1746. mod->klp_info->sechdrs[symndx].sh_addr = \
  1747. (unsigned long) mod->core_kallsyms.symtab;
  1748. return 0;
  1749. free_sechdrs:
  1750. kfree(mod->klp_info->sechdrs);
  1751. free_info:
  1752. kfree(mod->klp_info);
  1753. return ret;
  1754. }
  1755. static void free_module_elf(struct module *mod)
  1756. {
  1757. kfree(mod->klp_info->sechdrs);
  1758. kfree(mod->klp_info->secstrings);
  1759. kfree(mod->klp_info);
  1760. }
  1761. #else /* !CONFIG_LIVEPATCH */
  1762. static int copy_module_elf(struct module *mod, struct load_info *info)
  1763. {
  1764. return 0;
  1765. }
  1766. static void free_module_elf(struct module *mod)
  1767. {
  1768. }
  1769. #endif /* CONFIG_LIVEPATCH */
  1770. void __weak module_memfree(void *module_region)
  1771. {
  1772. vfree(module_region);
  1773. }
  1774. void __weak module_arch_cleanup(struct module *mod)
  1775. {
  1776. }
  1777. void __weak module_arch_freeing_init(struct module *mod)
  1778. {
  1779. }
  1780. /* Free a module, remove from lists, etc. */
  1781. static void free_module(struct module *mod)
  1782. {
  1783. trace_module_free(mod);
  1784. mod_sysfs_teardown(mod);
  1785. /* We leave it in list to prevent duplicate loads, but make sure
  1786. * that noone uses it while it's being deconstructed. */
  1787. mutex_lock(&module_mutex);
  1788. mod->state = MODULE_STATE_UNFORMED;
  1789. mutex_unlock(&module_mutex);
  1790. /* Remove dynamic debug info */
  1791. ddebug_remove_module(mod->name);
  1792. /* Arch-specific cleanup. */
  1793. module_arch_cleanup(mod);
  1794. /* Module unload stuff */
  1795. module_unload_free(mod);
  1796. /* Free any allocated parameters. */
  1797. destroy_params(mod->kp, mod->num_kp);
  1798. if (is_livepatch_module(mod))
  1799. free_module_elf(mod);
  1800. /* Now we can delete it from the lists */
  1801. mutex_lock(&module_mutex);
  1802. /* Unlink carefully: kallsyms could be walking list. */
  1803. list_del_rcu(&mod->list);
  1804. mod_tree_remove(mod);
  1805. /* Remove this module from bug list, this uses list_del_rcu */
  1806. module_bug_cleanup(mod);
  1807. /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
  1808. synchronize_sched();
  1809. mutex_unlock(&module_mutex);
  1810. /* This may be empty, but that's OK */
  1811. disable_ro_nx(&mod->init_layout);
  1812. module_arch_freeing_init(mod);
  1813. module_memfree(mod->init_layout.base);
  1814. kfree(mod->args);
  1815. percpu_modfree(mod);
  1816. /* Free lock-classes; relies on the preceding sync_rcu(). */
  1817. lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
  1818. /* Finally, free the core (containing the module structure) */
  1819. disable_ro_nx(&mod->core_layout);
  1820. module_memfree(mod->core_layout.base);
  1821. #ifdef CONFIG_MPU
  1822. update_protections(current->mm);
  1823. #endif
  1824. }
  1825. void *__symbol_get(const char *symbol)
  1826. {
  1827. struct module *owner;
  1828. const struct kernel_symbol *sym;
  1829. preempt_disable();
  1830. sym = find_symbol(symbol, &owner, NULL, true, true);
  1831. if (sym && strong_try_module_get(owner))
  1832. sym = NULL;
  1833. preempt_enable();
  1834. return sym ? (void *)sym->value : NULL;
  1835. }
  1836. EXPORT_SYMBOL_GPL(__symbol_get);
  1837. /*
  1838. * Ensure that an exported symbol [global namespace] does not already exist
  1839. * in the kernel or in some other module's exported symbol table.
  1840. *
  1841. * You must hold the module_mutex.
  1842. */
  1843. static int verify_export_symbols(struct module *mod)
  1844. {
  1845. unsigned int i;
  1846. struct module *owner;
  1847. const struct kernel_symbol *s;
  1848. struct {
  1849. const struct kernel_symbol *sym;
  1850. unsigned int num;
  1851. } arr[] = {
  1852. { mod->syms, mod->num_syms },
  1853. { mod->gpl_syms, mod->num_gpl_syms },
  1854. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1855. #ifdef CONFIG_UNUSED_SYMBOLS
  1856. { mod->unused_syms, mod->num_unused_syms },
  1857. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1858. #endif
  1859. };
  1860. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1861. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1862. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1863. pr_err("%s: exports duplicate symbol %s"
  1864. " (owned by %s)\n",
  1865. mod->name, s->name, module_name(owner));
  1866. return -ENOEXEC;
  1867. }
  1868. }
  1869. }
  1870. return 0;
  1871. }
  1872. /* Change all symbols so that st_value encodes the pointer directly. */
  1873. static int simplify_symbols(struct module *mod, const struct load_info *info)
  1874. {
  1875. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1876. Elf_Sym *sym = (void *)symsec->sh_addr;
  1877. unsigned long secbase;
  1878. unsigned int i;
  1879. int ret = 0;
  1880. const struct kernel_symbol *ksym;
  1881. for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
  1882. const char *name = info->strtab + sym[i].st_name;
  1883. switch (sym[i].st_shndx) {
  1884. case SHN_COMMON:
  1885. /* Ignore common symbols */
  1886. if (!strncmp(name, "__gnu_lto", 9))
  1887. break;
  1888. /* We compiled with -fno-common. These are not
  1889. supposed to happen. */
  1890. pr_debug("Common symbol: %s\n", name);
  1891. pr_warn("%s: please compile with -fno-common\n",
  1892. mod->name);
  1893. ret = -ENOEXEC;
  1894. break;
  1895. case SHN_ABS:
  1896. /* Don't need to do anything */
  1897. pr_debug("Absolute symbol: 0x%08lx\n",
  1898. (long)sym[i].st_value);
  1899. break;
  1900. case SHN_LIVEPATCH:
  1901. /* Livepatch symbols are resolved by livepatch */
  1902. break;
  1903. case SHN_UNDEF:
  1904. ksym = resolve_symbol_wait(mod, info, name);
  1905. /* Ok if resolved. */
  1906. if (ksym && !IS_ERR(ksym)) {
  1907. sym[i].st_value = ksym->value;
  1908. break;
  1909. }
  1910. /* Ok if weak. */
  1911. if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1912. break;
  1913. pr_warn("%s: Unknown symbol %s (err %li)\n",
  1914. mod->name, name, PTR_ERR(ksym));
  1915. ret = PTR_ERR(ksym) ?: -ENOENT;
  1916. break;
  1917. default:
  1918. /* Divert to percpu allocation if a percpu var. */
  1919. if (sym[i].st_shndx == info->index.pcpu)
  1920. secbase = (unsigned long)mod_percpu(mod);
  1921. else
  1922. secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
  1923. sym[i].st_value += secbase;
  1924. break;
  1925. }
  1926. }
  1927. return ret;
  1928. }
  1929. static int apply_relocations(struct module *mod, const struct load_info *info)
  1930. {
  1931. unsigned int i;
  1932. int err = 0;
  1933. /* Now do relocations. */
  1934. for (i = 1; i < info->hdr->e_shnum; i++) {
  1935. unsigned int infosec = info->sechdrs[i].sh_info;
  1936. /* Not a valid relocation section? */
  1937. if (infosec >= info->hdr->e_shnum)
  1938. continue;
  1939. /* Don't bother with non-allocated sections */
  1940. if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
  1941. continue;
  1942. /* Livepatch relocation sections are applied by livepatch */
  1943. if (info->sechdrs[i].sh_flags & SHF_RELA_LIVEPATCH)
  1944. continue;
  1945. if (info->sechdrs[i].sh_type == SHT_REL)
  1946. err = apply_relocate(info->sechdrs, info->strtab,
  1947. info->index.sym, i, mod);
  1948. else if (info->sechdrs[i].sh_type == SHT_RELA)
  1949. err = apply_relocate_add(info->sechdrs, info->strtab,
  1950. info->index.sym, i, mod);
  1951. if (err < 0)
  1952. break;
  1953. }
  1954. return err;
  1955. }
  1956. /* Additional bytes needed by arch in front of individual sections */
  1957. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1958. unsigned int section)
  1959. {
  1960. /* default implementation just returns zero */
  1961. return 0;
  1962. }
  1963. /* Update size with this section: return offset. */
  1964. static long get_offset(struct module *mod, unsigned int *size,
  1965. Elf_Shdr *sechdr, unsigned int section)
  1966. {
  1967. long ret;
  1968. *size += arch_mod_section_prepend(mod, section);
  1969. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1970. *size = ret + sechdr->sh_size;
  1971. return ret;
  1972. }
  1973. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1974. might -- code, read-only data, read-write data, small data. Tally
  1975. sizes, and place the offsets into sh_entsize fields: high bit means it
  1976. belongs in init. */
  1977. static void layout_sections(struct module *mod, struct load_info *info)
  1978. {
  1979. static unsigned long const masks[][2] = {
  1980. /* NOTE: all executable code must be the first section
  1981. * in this array; otherwise modify the text_size
  1982. * finder in the two loops below */
  1983. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1984. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1985. { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL },
  1986. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1987. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1988. };
  1989. unsigned int m, i;
  1990. for (i = 0; i < info->hdr->e_shnum; i++)
  1991. info->sechdrs[i].sh_entsize = ~0UL;
  1992. pr_debug("Core section allocation order:\n");
  1993. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1994. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1995. Elf_Shdr *s = &info->sechdrs[i];
  1996. const char *sname = info->secstrings + s->sh_name;
  1997. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1998. || (s->sh_flags & masks[m][1])
  1999. || s->sh_entsize != ~0UL
  2000. || strstarts(sname, ".init"))
  2001. continue;
  2002. s->sh_entsize = get_offset(mod, &mod->core_layout.size, s, i);
  2003. pr_debug("\t%s\n", sname);
  2004. }
  2005. switch (m) {
  2006. case 0: /* executable */
  2007. mod->core_layout.size = debug_align(mod->core_layout.size);
  2008. mod->core_layout.text_size = mod->core_layout.size;
  2009. break;
  2010. case 1: /* RO: text and ro-data */
  2011. mod->core_layout.size = debug_align(mod->core_layout.size);
  2012. mod->core_layout.ro_size = mod->core_layout.size;
  2013. break;
  2014. case 2: /* RO after init */
  2015. mod->core_layout.size = debug_align(mod->core_layout.size);
  2016. mod->core_layout.ro_after_init_size = mod->core_layout.size;
  2017. break;
  2018. case 4: /* whole core */
  2019. mod->core_layout.size = debug_align(mod->core_layout.size);
  2020. break;
  2021. }
  2022. }
  2023. pr_debug("Init section allocation order:\n");
  2024. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  2025. for (i = 0; i < info->hdr->e_shnum; ++i) {
  2026. Elf_Shdr *s = &info->sechdrs[i];
  2027. const char *sname = info->secstrings + s->sh_name;
  2028. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  2029. || (s->sh_flags & masks[m][1])
  2030. || s->sh_entsize != ~0UL
  2031. || !strstarts(sname, ".init"))
  2032. continue;
  2033. s->sh_entsize = (get_offset(mod, &mod->init_layout.size, s, i)
  2034. | INIT_OFFSET_MASK);
  2035. pr_debug("\t%s\n", sname);
  2036. }
  2037. switch (m) {
  2038. case 0: /* executable */
  2039. mod->init_layout.size = debug_align(mod->init_layout.size);
  2040. mod->init_layout.text_size = mod->init_layout.size;
  2041. break;
  2042. case 1: /* RO: text and ro-data */
  2043. mod->init_layout.size = debug_align(mod->init_layout.size);
  2044. mod->init_layout.ro_size = mod->init_layout.size;
  2045. break;
  2046. case 2:
  2047. /*
  2048. * RO after init doesn't apply to init_layout (only
  2049. * core_layout), so it just takes the value of ro_size.
  2050. */
  2051. mod->init_layout.ro_after_init_size = mod->init_layout.ro_size;
  2052. break;
  2053. case 4: /* whole init */
  2054. mod->init_layout.size = debug_align(mod->init_layout.size);
  2055. break;
  2056. }
  2057. }
  2058. }
  2059. static void set_license(struct module *mod, const char *license)
  2060. {
  2061. if (!license)
  2062. license = "unspecified";
  2063. if (!license_is_gpl_compatible(license)) {
  2064. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  2065. pr_warn("%s: module license '%s' taints kernel.\n",
  2066. mod->name, license);
  2067. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2068. LOCKDEP_NOW_UNRELIABLE);
  2069. }
  2070. }
  2071. /* Parse tag=value strings from .modinfo section */
  2072. static char *next_string(char *string, unsigned long *secsize)
  2073. {
  2074. /* Skip non-zero chars */
  2075. while (string[0]) {
  2076. string++;
  2077. if ((*secsize)-- <= 1)
  2078. return NULL;
  2079. }
  2080. /* Skip any zero padding. */
  2081. while (!string[0]) {
  2082. string++;
  2083. if ((*secsize)-- <= 1)
  2084. return NULL;
  2085. }
  2086. return string;
  2087. }
  2088. static char *get_modinfo(struct load_info *info, const char *tag)
  2089. {
  2090. char *p;
  2091. unsigned int taglen = strlen(tag);
  2092. Elf_Shdr *infosec = &info->sechdrs[info->index.info];
  2093. unsigned long size = infosec->sh_size;
  2094. for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
  2095. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  2096. return p + taglen + 1;
  2097. }
  2098. return NULL;
  2099. }
  2100. static void setup_modinfo(struct module *mod, struct load_info *info)
  2101. {
  2102. struct module_attribute *attr;
  2103. int i;
  2104. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  2105. if (attr->setup)
  2106. attr->setup(mod, get_modinfo(info, attr->attr.name));
  2107. }
  2108. }
  2109. static void free_modinfo(struct module *mod)
  2110. {
  2111. struct module_attribute *attr;
  2112. int i;
  2113. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  2114. if (attr->free)
  2115. attr->free(mod);
  2116. }
  2117. }
  2118. #ifdef CONFIG_KALLSYMS
  2119. /* lookup symbol in given range of kernel_symbols */
  2120. static const struct kernel_symbol *lookup_symbol(const char *name,
  2121. const struct kernel_symbol *start,
  2122. const struct kernel_symbol *stop)
  2123. {
  2124. return bsearch(name, start, stop - start,
  2125. sizeof(struct kernel_symbol), cmp_name);
  2126. }
  2127. static int is_exported(const char *name, unsigned long value,
  2128. const struct module *mod)
  2129. {
  2130. const struct kernel_symbol *ks;
  2131. if (!mod)
  2132. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  2133. else
  2134. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  2135. return ks != NULL && ks->value == value;
  2136. }
  2137. /* As per nm */
  2138. static char elf_type(const Elf_Sym *sym, const struct load_info *info)
  2139. {
  2140. const Elf_Shdr *sechdrs = info->sechdrs;
  2141. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  2142. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  2143. return 'v';
  2144. else
  2145. return 'w';
  2146. }
  2147. if (sym->st_shndx == SHN_UNDEF)
  2148. return 'U';
  2149. if (sym->st_shndx == SHN_ABS || sym->st_shndx == info->index.pcpu)
  2150. return 'a';
  2151. if (sym->st_shndx >= SHN_LORESERVE)
  2152. return '?';
  2153. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  2154. return 't';
  2155. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  2156. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  2157. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  2158. return 'r';
  2159. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2160. return 'g';
  2161. else
  2162. return 'd';
  2163. }
  2164. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  2165. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2166. return 's';
  2167. else
  2168. return 'b';
  2169. }
  2170. if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
  2171. ".debug")) {
  2172. return 'n';
  2173. }
  2174. return '?';
  2175. }
  2176. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  2177. unsigned int shnum, unsigned int pcpundx)
  2178. {
  2179. const Elf_Shdr *sec;
  2180. if (src->st_shndx == SHN_UNDEF
  2181. || src->st_shndx >= shnum
  2182. || !src->st_name)
  2183. return false;
  2184. #ifdef CONFIG_KALLSYMS_ALL
  2185. if (src->st_shndx == pcpundx)
  2186. return true;
  2187. #endif
  2188. sec = sechdrs + src->st_shndx;
  2189. if (!(sec->sh_flags & SHF_ALLOC)
  2190. #ifndef CONFIG_KALLSYMS_ALL
  2191. || !(sec->sh_flags & SHF_EXECINSTR)
  2192. #endif
  2193. || (sec->sh_entsize & INIT_OFFSET_MASK))
  2194. return false;
  2195. return true;
  2196. }
  2197. /*
  2198. * We only allocate and copy the strings needed by the parts of symtab
  2199. * we keep. This is simple, but has the effect of making multiple
  2200. * copies of duplicates. We could be more sophisticated, see
  2201. * linux-kernel thread starting with
  2202. * <73defb5e4bca04a6431392cc341112b1@localhost>.
  2203. */
  2204. static void layout_symtab(struct module *mod, struct load_info *info)
  2205. {
  2206. Elf_Shdr *symsect = info->sechdrs + info->index.sym;
  2207. Elf_Shdr *strsect = info->sechdrs + info->index.str;
  2208. const Elf_Sym *src;
  2209. unsigned int i, nsrc, ndst, strtab_size = 0;
  2210. /* Put symbol section at end of init part of module. */
  2211. symsect->sh_flags |= SHF_ALLOC;
  2212. symsect->sh_entsize = get_offset(mod, &mod->init_layout.size, symsect,
  2213. info->index.sym) | INIT_OFFSET_MASK;
  2214. pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
  2215. src = (void *)info->hdr + symsect->sh_offset;
  2216. nsrc = symsect->sh_size / sizeof(*src);
  2217. /* Compute total space required for the core symbols' strtab. */
  2218. for (ndst = i = 0; i < nsrc; i++) {
  2219. if (i == 0 || is_livepatch_module(mod) ||
  2220. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
  2221. info->index.pcpu)) {
  2222. strtab_size += strlen(&info->strtab[src[i].st_name])+1;
  2223. ndst++;
  2224. }
  2225. }
  2226. /* Append room for core symbols at end of core part. */
  2227. info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1);
  2228. info->stroffs = mod->core_layout.size = info->symoffs + ndst * sizeof(Elf_Sym);
  2229. mod->core_layout.size += strtab_size;
  2230. mod->core_layout.size = debug_align(mod->core_layout.size);
  2231. /* Put string table section at end of init part of module. */
  2232. strsect->sh_flags |= SHF_ALLOC;
  2233. strsect->sh_entsize = get_offset(mod, &mod->init_layout.size, strsect,
  2234. info->index.str) | INIT_OFFSET_MASK;
  2235. pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
  2236. /* We'll tack temporary mod_kallsyms on the end. */
  2237. mod->init_layout.size = ALIGN(mod->init_layout.size,
  2238. __alignof__(struct mod_kallsyms));
  2239. info->mod_kallsyms_init_off = mod->init_layout.size;
  2240. mod->init_layout.size += sizeof(struct mod_kallsyms);
  2241. mod->init_layout.size = debug_align(mod->init_layout.size);
  2242. }
  2243. /*
  2244. * We use the full symtab and strtab which layout_symtab arranged to
  2245. * be appended to the init section. Later we switch to the cut-down
  2246. * core-only ones.
  2247. */
  2248. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2249. {
  2250. unsigned int i, ndst;
  2251. const Elf_Sym *src;
  2252. Elf_Sym *dst;
  2253. char *s;
  2254. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  2255. /* Set up to point into init section. */
  2256. mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
  2257. mod->kallsyms->symtab = (void *)symsec->sh_addr;
  2258. mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
  2259. /* Make sure we get permanent strtab: don't use info->strtab. */
  2260. mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
  2261. /* Set types up while we still have access to sections. */
  2262. for (i = 0; i < mod->kallsyms->num_symtab; i++)
  2263. mod->kallsyms->symtab[i].st_info
  2264. = elf_type(&mod->kallsyms->symtab[i], info);
  2265. /* Now populate the cut down core kallsyms for after init. */
  2266. mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
  2267. mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
  2268. src = mod->kallsyms->symtab;
  2269. for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
  2270. if (i == 0 || is_livepatch_module(mod) ||
  2271. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum,
  2272. info->index.pcpu)) {
  2273. dst[ndst] = src[i];
  2274. dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
  2275. s += strlcpy(s, &mod->kallsyms->strtab[src[i].st_name],
  2276. KSYM_NAME_LEN) + 1;
  2277. }
  2278. }
  2279. mod->core_kallsyms.num_symtab = ndst;
  2280. }
  2281. #else
  2282. static inline void layout_symtab(struct module *mod, struct load_info *info)
  2283. {
  2284. }
  2285. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2286. {
  2287. }
  2288. #endif /* CONFIG_KALLSYMS */
  2289. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  2290. {
  2291. if (!debug)
  2292. return;
  2293. #ifdef CONFIG_DYNAMIC_DEBUG
  2294. if (ddebug_add_module(debug, num, debug->modname))
  2295. pr_err("dynamic debug error adding module: %s\n",
  2296. debug->modname);
  2297. #endif
  2298. }
  2299. static void dynamic_debug_remove(struct _ddebug *debug)
  2300. {
  2301. if (debug)
  2302. ddebug_remove_module(debug->modname);
  2303. }
  2304. void * __weak module_alloc(unsigned long size)
  2305. {
  2306. return vmalloc_exec(size);
  2307. }
  2308. #ifdef CONFIG_DEBUG_KMEMLEAK
  2309. static void kmemleak_load_module(const struct module *mod,
  2310. const struct load_info *info)
  2311. {
  2312. unsigned int i;
  2313. /* only scan the sections containing data */
  2314. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  2315. for (i = 1; i < info->hdr->e_shnum; i++) {
  2316. /* Scan all writable sections that's not executable */
  2317. if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
  2318. !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
  2319. (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
  2320. continue;
  2321. kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
  2322. info->sechdrs[i].sh_size, GFP_KERNEL);
  2323. }
  2324. }
  2325. #else
  2326. static inline void kmemleak_load_module(const struct module *mod,
  2327. const struct load_info *info)
  2328. {
  2329. }
  2330. #endif
  2331. #ifdef CONFIG_MODULE_SIG
  2332. static int module_sig_check(struct load_info *info, int flags)
  2333. {
  2334. int err = -ENOKEY;
  2335. const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
  2336. const void *mod = info->hdr;
  2337. /*
  2338. * Require flags == 0, as a module with version information
  2339. * removed is no longer the module that was signed
  2340. */
  2341. if (flags == 0 &&
  2342. info->len > markerlen &&
  2343. memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
  2344. /* We truncate the module to discard the signature */
  2345. info->len -= markerlen;
  2346. err = mod_verify_sig(mod, &info->len);
  2347. }
  2348. if (!err) {
  2349. info->sig_ok = true;
  2350. return 0;
  2351. }
  2352. /* Not having a signature is only an error if we're strict. */
  2353. if (err == -ENOKEY && !sig_enforce)
  2354. err = 0;
  2355. return err;
  2356. }
  2357. #else /* !CONFIG_MODULE_SIG */
  2358. static int module_sig_check(struct load_info *info, int flags)
  2359. {
  2360. return 0;
  2361. }
  2362. #endif /* !CONFIG_MODULE_SIG */
  2363. /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
  2364. static int elf_header_check(struct load_info *info)
  2365. {
  2366. if (info->len < sizeof(*(info->hdr)))
  2367. return -ENOEXEC;
  2368. if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
  2369. || info->hdr->e_type != ET_REL
  2370. || !elf_check_arch(info->hdr)
  2371. || info->hdr->e_shentsize != sizeof(Elf_Shdr))
  2372. return -ENOEXEC;
  2373. if (info->hdr->e_shoff >= info->len
  2374. || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
  2375. info->len - info->hdr->e_shoff))
  2376. return -ENOEXEC;
  2377. return 0;
  2378. }
  2379. #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
  2380. static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
  2381. {
  2382. do {
  2383. unsigned long n = min(len, COPY_CHUNK_SIZE);
  2384. if (copy_from_user(dst, usrc, n) != 0)
  2385. return -EFAULT;
  2386. cond_resched();
  2387. dst += n;
  2388. usrc += n;
  2389. len -= n;
  2390. } while (len);
  2391. return 0;
  2392. }
  2393. #ifdef CONFIG_LIVEPATCH
  2394. static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
  2395. {
  2396. if (get_modinfo(info, "livepatch")) {
  2397. mod->klp = true;
  2398. add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
  2399. }
  2400. return 0;
  2401. }
  2402. #else /* !CONFIG_LIVEPATCH */
  2403. static int check_modinfo_livepatch(struct module *mod, struct load_info *info)
  2404. {
  2405. if (get_modinfo(info, "livepatch")) {
  2406. pr_err("%s: module is marked as livepatch module, but livepatch support is disabled",
  2407. mod->name);
  2408. return -ENOEXEC;
  2409. }
  2410. return 0;
  2411. }
  2412. #endif /* CONFIG_LIVEPATCH */
  2413. /* Sets info->hdr and info->len. */
  2414. static int copy_module_from_user(const void __user *umod, unsigned long len,
  2415. struct load_info *info)
  2416. {
  2417. int err;
  2418. info->len = len;
  2419. if (info->len < sizeof(*(info->hdr)))
  2420. return -ENOEXEC;
  2421. err = security_kernel_read_file(NULL, READING_MODULE);
  2422. if (err)
  2423. return err;
  2424. /* Suck in entire file: we'll want most of it. */
  2425. info->hdr = __vmalloc(info->len,
  2426. GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
  2427. if (!info->hdr)
  2428. return -ENOMEM;
  2429. if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
  2430. vfree(info->hdr);
  2431. return -EFAULT;
  2432. }
  2433. return 0;
  2434. }
  2435. static void free_copy(struct load_info *info)
  2436. {
  2437. vfree(info->hdr);
  2438. }
  2439. static int rewrite_section_headers(struct load_info *info, int flags)
  2440. {
  2441. unsigned int i;
  2442. /* This should always be true, but let's be sure. */
  2443. info->sechdrs[0].sh_addr = 0;
  2444. for (i = 1; i < info->hdr->e_shnum; i++) {
  2445. Elf_Shdr *shdr = &info->sechdrs[i];
  2446. if (shdr->sh_type != SHT_NOBITS
  2447. && info->len < shdr->sh_offset + shdr->sh_size) {
  2448. pr_err("Module len %lu truncated\n", info->len);
  2449. return -ENOEXEC;
  2450. }
  2451. /* Mark all sections sh_addr with their address in the
  2452. temporary image. */
  2453. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  2454. #ifndef CONFIG_MODULE_UNLOAD
  2455. /* Don't load .exit sections */
  2456. if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
  2457. shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
  2458. #endif
  2459. }
  2460. /* Track but don't keep modinfo and version sections. */
  2461. if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
  2462. info->index.vers = 0; /* Pretend no __versions section! */
  2463. else
  2464. info->index.vers = find_sec(info, "__versions");
  2465. info->index.info = find_sec(info, ".modinfo");
  2466. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2467. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2468. return 0;
  2469. }
  2470. /*
  2471. * Set up our basic convenience variables (pointers to section headers,
  2472. * search for module section index etc), and do some basic section
  2473. * verification.
  2474. *
  2475. * Return the temporary module pointer (we'll replace it with the final
  2476. * one when we move the module sections around).
  2477. */
  2478. static struct module *setup_load_info(struct load_info *info, int flags)
  2479. {
  2480. unsigned int i;
  2481. int err;
  2482. struct module *mod;
  2483. /* Set up the convenience variables */
  2484. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  2485. info->secstrings = (void *)info->hdr
  2486. + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
  2487. err = rewrite_section_headers(info, flags);
  2488. if (err)
  2489. return ERR_PTR(err);
  2490. /* Find internal symbols and strings. */
  2491. for (i = 1; i < info->hdr->e_shnum; i++) {
  2492. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  2493. info->index.sym = i;
  2494. info->index.str = info->sechdrs[i].sh_link;
  2495. info->strtab = (char *)info->hdr
  2496. + info->sechdrs[info->index.str].sh_offset;
  2497. break;
  2498. }
  2499. }
  2500. info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
  2501. if (!info->index.mod) {
  2502. pr_warn("No module found in object\n");
  2503. return ERR_PTR(-ENOEXEC);
  2504. }
  2505. /* This is temporary: point mod into copy of data. */
  2506. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2507. if (info->index.sym == 0) {
  2508. pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
  2509. return ERR_PTR(-ENOEXEC);
  2510. }
  2511. info->index.pcpu = find_pcpusec(info);
  2512. /* Check module struct version now, before we try to use module. */
  2513. if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
  2514. return ERR_PTR(-ENOEXEC);
  2515. return mod;
  2516. }
  2517. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  2518. {
  2519. const char *modmagic = get_modinfo(info, "vermagic");
  2520. int err;
  2521. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  2522. modmagic = NULL;
  2523. /* This is allowed: modprobe --force will invalidate it. */
  2524. if (!modmagic) {
  2525. err = try_to_force_load(mod, "bad vermagic");
  2526. if (err)
  2527. return err;
  2528. } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
  2529. pr_err("%s: version magic '%s' should be '%s'\n",
  2530. mod->name, modmagic, vermagic);
  2531. return -ENOEXEC;
  2532. }
  2533. if (!get_modinfo(info, "intree")) {
  2534. if (!test_taint(TAINT_OOT_MODULE))
  2535. pr_warn("%s: loading out-of-tree module taints kernel.\n",
  2536. mod->name);
  2537. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  2538. }
  2539. if (get_modinfo(info, "staging")) {
  2540. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  2541. pr_warn("%s: module is from the staging directory, the quality "
  2542. "is unknown, you have been warned.\n", mod->name);
  2543. }
  2544. err = check_modinfo_livepatch(mod, info);
  2545. if (err)
  2546. return err;
  2547. /* Set up license info based on the info section */
  2548. set_license(mod, get_modinfo(info, "license"));
  2549. return 0;
  2550. }
  2551. static int find_module_sections(struct module *mod, struct load_info *info)
  2552. {
  2553. mod->kp = section_objs(info, "__param",
  2554. sizeof(*mod->kp), &mod->num_kp);
  2555. mod->syms = section_objs(info, "__ksymtab",
  2556. sizeof(*mod->syms), &mod->num_syms);
  2557. mod->crcs = section_addr(info, "__kcrctab");
  2558. mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
  2559. sizeof(*mod->gpl_syms),
  2560. &mod->num_gpl_syms);
  2561. mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
  2562. mod->gpl_future_syms = section_objs(info,
  2563. "__ksymtab_gpl_future",
  2564. sizeof(*mod->gpl_future_syms),
  2565. &mod->num_gpl_future_syms);
  2566. mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
  2567. #ifdef CONFIG_UNUSED_SYMBOLS
  2568. mod->unused_syms = section_objs(info, "__ksymtab_unused",
  2569. sizeof(*mod->unused_syms),
  2570. &mod->num_unused_syms);
  2571. mod->unused_crcs = section_addr(info, "__kcrctab_unused");
  2572. mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
  2573. sizeof(*mod->unused_gpl_syms),
  2574. &mod->num_unused_gpl_syms);
  2575. mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
  2576. #endif
  2577. #ifdef CONFIG_CONSTRUCTORS
  2578. mod->ctors = section_objs(info, ".ctors",
  2579. sizeof(*mod->ctors), &mod->num_ctors);
  2580. if (!mod->ctors)
  2581. mod->ctors = section_objs(info, ".init_array",
  2582. sizeof(*mod->ctors), &mod->num_ctors);
  2583. else if (find_sec(info, ".init_array")) {
  2584. /*
  2585. * This shouldn't happen with same compiler and binutils
  2586. * building all parts of the module.
  2587. */
  2588. pr_warn("%s: has both .ctors and .init_array.\n",
  2589. mod->name);
  2590. return -EINVAL;
  2591. }
  2592. #endif
  2593. #ifdef CONFIG_TRACEPOINTS
  2594. mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
  2595. sizeof(*mod->tracepoints_ptrs),
  2596. &mod->num_tracepoints);
  2597. #endif
  2598. #ifdef HAVE_JUMP_LABEL
  2599. mod->jump_entries = section_objs(info, "__jump_table",
  2600. sizeof(*mod->jump_entries),
  2601. &mod->num_jump_entries);
  2602. #endif
  2603. #ifdef CONFIG_EVENT_TRACING
  2604. mod->trace_events = section_objs(info, "_ftrace_events",
  2605. sizeof(*mod->trace_events),
  2606. &mod->num_trace_events);
  2607. mod->trace_enums = section_objs(info, "_ftrace_enum_map",
  2608. sizeof(*mod->trace_enums),
  2609. &mod->num_trace_enums);
  2610. #endif
  2611. #ifdef CONFIG_TRACING
  2612. mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
  2613. sizeof(*mod->trace_bprintk_fmt_start),
  2614. &mod->num_trace_bprintk_fmt);
  2615. #endif
  2616. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2617. /* sechdrs[0].sh_size is always zero */
  2618. mod->ftrace_callsites = section_objs(info, "__mcount_loc",
  2619. sizeof(*mod->ftrace_callsites),
  2620. &mod->num_ftrace_callsites);
  2621. #endif
  2622. mod->extable = section_objs(info, "__ex_table",
  2623. sizeof(*mod->extable), &mod->num_exentries);
  2624. if (section_addr(info, "__obsparm"))
  2625. pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
  2626. info->debug = section_objs(info, "__verbose",
  2627. sizeof(*info->debug), &info->num_debug);
  2628. return 0;
  2629. }
  2630. static int move_module(struct module *mod, struct load_info *info)
  2631. {
  2632. int i;
  2633. void *ptr;
  2634. /* Do the allocs. */
  2635. ptr = module_alloc(mod->core_layout.size);
  2636. /*
  2637. * The pointer to this block is stored in the module structure
  2638. * which is inside the block. Just mark it as not being a
  2639. * leak.
  2640. */
  2641. kmemleak_not_leak(ptr);
  2642. if (!ptr)
  2643. return -ENOMEM;
  2644. memset(ptr, 0, mod->core_layout.size);
  2645. mod->core_layout.base = ptr;
  2646. if (mod->init_layout.size) {
  2647. ptr = module_alloc(mod->init_layout.size);
  2648. /*
  2649. * The pointer to this block is stored in the module structure
  2650. * which is inside the block. This block doesn't need to be
  2651. * scanned as it contains data and code that will be freed
  2652. * after the module is initialized.
  2653. */
  2654. kmemleak_ignore(ptr);
  2655. if (!ptr) {
  2656. module_memfree(mod->core_layout.base);
  2657. return -ENOMEM;
  2658. }
  2659. memset(ptr, 0, mod->init_layout.size);
  2660. mod->init_layout.base = ptr;
  2661. } else
  2662. mod->init_layout.base = NULL;
  2663. /* Transfer each section which specifies SHF_ALLOC */
  2664. pr_debug("final section addresses:\n");
  2665. for (i = 0; i < info->hdr->e_shnum; i++) {
  2666. void *dest;
  2667. Elf_Shdr *shdr = &info->sechdrs[i];
  2668. if (!(shdr->sh_flags & SHF_ALLOC))
  2669. continue;
  2670. if (shdr->sh_entsize & INIT_OFFSET_MASK)
  2671. dest = mod->init_layout.base
  2672. + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
  2673. else
  2674. dest = mod->core_layout.base + shdr->sh_entsize;
  2675. if (shdr->sh_type != SHT_NOBITS)
  2676. memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
  2677. /* Update sh_addr to point to copy in image. */
  2678. shdr->sh_addr = (unsigned long)dest;
  2679. pr_debug("\t0x%lx %s\n",
  2680. (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
  2681. }
  2682. return 0;
  2683. }
  2684. static int check_module_license_and_versions(struct module *mod)
  2685. {
  2686. int prev_taint = test_taint(TAINT_PROPRIETARY_MODULE);
  2687. /*
  2688. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2689. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2690. * using GPL-only symbols it needs.
  2691. */
  2692. if (strcmp(mod->name, "ndiswrapper") == 0)
  2693. add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
  2694. /* driverloader was caught wrongly pretending to be under GPL */
  2695. if (strcmp(mod->name, "driverloader") == 0)
  2696. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2697. LOCKDEP_NOW_UNRELIABLE);
  2698. /* lve claims to be GPL but upstream won't provide source */
  2699. if (strcmp(mod->name, "lve") == 0)
  2700. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2701. LOCKDEP_NOW_UNRELIABLE);
  2702. if (!prev_taint && test_taint(TAINT_PROPRIETARY_MODULE))
  2703. pr_warn("%s: module license taints kernel.\n", mod->name);
  2704. #ifdef CONFIG_MODVERSIONS
  2705. if ((mod->num_syms && !mod->crcs)
  2706. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2707. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2708. #ifdef CONFIG_UNUSED_SYMBOLS
  2709. || (mod->num_unused_syms && !mod->unused_crcs)
  2710. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2711. #endif
  2712. ) {
  2713. return try_to_force_load(mod,
  2714. "no versions for exported symbols");
  2715. }
  2716. #endif
  2717. return 0;
  2718. }
  2719. static void flush_module_icache(const struct module *mod)
  2720. {
  2721. mm_segment_t old_fs;
  2722. /* flush the icache in correct context */
  2723. old_fs = get_fs();
  2724. set_fs(KERNEL_DS);
  2725. /*
  2726. * Flush the instruction cache, since we've played with text.
  2727. * Do it before processing of module parameters, so the module
  2728. * can provide parameter accessor functions of its own.
  2729. */
  2730. if (mod->init_layout.base)
  2731. flush_icache_range((unsigned long)mod->init_layout.base,
  2732. (unsigned long)mod->init_layout.base
  2733. + mod->init_layout.size);
  2734. flush_icache_range((unsigned long)mod->core_layout.base,
  2735. (unsigned long)mod->core_layout.base + mod->core_layout.size);
  2736. set_fs(old_fs);
  2737. }
  2738. int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
  2739. Elf_Shdr *sechdrs,
  2740. char *secstrings,
  2741. struct module *mod)
  2742. {
  2743. return 0;
  2744. }
  2745. /* module_blacklist is a comma-separated list of module names */
  2746. static char *module_blacklist;
  2747. static bool blacklisted(char *module_name)
  2748. {
  2749. const char *p;
  2750. size_t len;
  2751. if (!module_blacklist)
  2752. return false;
  2753. for (p = module_blacklist; *p; p += len) {
  2754. len = strcspn(p, ",");
  2755. if (strlen(module_name) == len && !memcmp(module_name, p, len))
  2756. return true;
  2757. if (p[len] == ',')
  2758. len++;
  2759. }
  2760. return false;
  2761. }
  2762. core_param(module_blacklist, module_blacklist, charp, 0400);
  2763. static struct module *layout_and_allocate(struct load_info *info, int flags)
  2764. {
  2765. /* Module within temporary copy. */
  2766. struct module *mod;
  2767. unsigned int ndx;
  2768. int err;
  2769. mod = setup_load_info(info, flags);
  2770. if (IS_ERR(mod))
  2771. return mod;
  2772. if (blacklisted(mod->name))
  2773. return ERR_PTR(-EPERM);
  2774. err = check_modinfo(mod, info, flags);
  2775. if (err)
  2776. return ERR_PTR(err);
  2777. /* Allow arches to frob section contents and sizes. */
  2778. err = module_frob_arch_sections(info->hdr, info->sechdrs,
  2779. info->secstrings, mod);
  2780. if (err < 0)
  2781. return ERR_PTR(err);
  2782. /* We will do a special allocation for per-cpu sections later. */
  2783. info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2784. /*
  2785. * Mark ro_after_init section with SHF_RO_AFTER_INIT so that
  2786. * layout_sections() can put it in the right place.
  2787. * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set.
  2788. */
  2789. ndx = find_sec(info, ".data..ro_after_init");
  2790. if (ndx)
  2791. info->sechdrs[ndx].sh_flags |= SHF_RO_AFTER_INIT;
  2792. /* Determine total sizes, and put offsets in sh_entsize. For now
  2793. this is done generically; there doesn't appear to be any
  2794. special cases for the architectures. */
  2795. layout_sections(mod, info);
  2796. layout_symtab(mod, info);
  2797. /* Allocate and move to the final place */
  2798. err = move_module(mod, info);
  2799. if (err)
  2800. return ERR_PTR(err);
  2801. /* Module has been copied to its final place now: return it. */
  2802. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2803. kmemleak_load_module(mod, info);
  2804. return mod;
  2805. }
  2806. /* mod is no longer valid after this! */
  2807. static void module_deallocate(struct module *mod, struct load_info *info)
  2808. {
  2809. percpu_modfree(mod);
  2810. module_arch_freeing_init(mod);
  2811. module_memfree(mod->init_layout.base);
  2812. module_memfree(mod->core_layout.base);
  2813. }
  2814. int __weak module_finalize(const Elf_Ehdr *hdr,
  2815. const Elf_Shdr *sechdrs,
  2816. struct module *me)
  2817. {
  2818. return 0;
  2819. }
  2820. static int post_relocation(struct module *mod, const struct load_info *info)
  2821. {
  2822. /* Sort exception table now relocations are done. */
  2823. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2824. /* Copy relocated percpu area over. */
  2825. percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
  2826. info->sechdrs[info->index.pcpu].sh_size);
  2827. /* Setup kallsyms-specific fields. */
  2828. add_kallsyms(mod, info);
  2829. /* Arch-specific module finalizing. */
  2830. return module_finalize(info->hdr, info->sechdrs, mod);
  2831. }
  2832. /* Is this module of this name done loading? No locks held. */
  2833. static bool finished_loading(const char *name)
  2834. {
  2835. struct module *mod;
  2836. bool ret;
  2837. /*
  2838. * The module_mutex should not be a heavily contended lock;
  2839. * if we get the occasional sleep here, we'll go an extra iteration
  2840. * in the wait_event_interruptible(), which is harmless.
  2841. */
  2842. sched_annotate_sleep();
  2843. mutex_lock(&module_mutex);
  2844. mod = find_module_all(name, strlen(name), true);
  2845. ret = !mod || mod->state == MODULE_STATE_LIVE
  2846. || mod->state == MODULE_STATE_GOING;
  2847. mutex_unlock(&module_mutex);
  2848. return ret;
  2849. }
  2850. /* Call module constructors. */
  2851. static void do_mod_ctors(struct module *mod)
  2852. {
  2853. #ifdef CONFIG_CONSTRUCTORS
  2854. unsigned long i;
  2855. for (i = 0; i < mod->num_ctors; i++)
  2856. mod->ctors[i]();
  2857. #endif
  2858. }
  2859. /* For freeing module_init on success, in case kallsyms traversing */
  2860. struct mod_initfree {
  2861. struct rcu_head rcu;
  2862. void *module_init;
  2863. };
  2864. static void do_free_init(struct rcu_head *head)
  2865. {
  2866. struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
  2867. module_memfree(m->module_init);
  2868. kfree(m);
  2869. }
  2870. /*
  2871. * This is where the real work happens.
  2872. *
  2873. * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
  2874. * helper command 'lx-symbols'.
  2875. */
  2876. static noinline int do_init_module(struct module *mod)
  2877. {
  2878. int ret = 0;
  2879. struct mod_initfree *freeinit;
  2880. freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
  2881. if (!freeinit) {
  2882. ret = -ENOMEM;
  2883. goto fail;
  2884. }
  2885. freeinit->module_init = mod->init_layout.base;
  2886. /*
  2887. * We want to find out whether @mod uses async during init. Clear
  2888. * PF_USED_ASYNC. async_schedule*() will set it.
  2889. */
  2890. current->flags &= ~PF_USED_ASYNC;
  2891. do_mod_ctors(mod);
  2892. /* Start the module */
  2893. if (mod->init != NULL)
  2894. ret = do_one_initcall(mod->init);
  2895. if (ret < 0) {
  2896. goto fail_free_freeinit;
  2897. }
  2898. if (ret > 0) {
  2899. pr_warn("%s: '%s'->init suspiciously returned %d, it should "
  2900. "follow 0/-E convention\n"
  2901. "%s: loading module anyway...\n",
  2902. __func__, mod->name, ret, __func__);
  2903. dump_stack();
  2904. }
  2905. /* Now it's a first class citizen! */
  2906. mod->state = MODULE_STATE_LIVE;
  2907. blocking_notifier_call_chain(&module_notify_list,
  2908. MODULE_STATE_LIVE, mod);
  2909. /*
  2910. * We need to finish all async code before the module init sequence
  2911. * is done. This has potential to deadlock. For example, a newly
  2912. * detected block device can trigger request_module() of the
  2913. * default iosched from async probing task. Once userland helper
  2914. * reaches here, async_synchronize_full() will wait on the async
  2915. * task waiting on request_module() and deadlock.
  2916. *
  2917. * This deadlock is avoided by perfomring async_synchronize_full()
  2918. * iff module init queued any async jobs. This isn't a full
  2919. * solution as it will deadlock the same if module loading from
  2920. * async jobs nests more than once; however, due to the various
  2921. * constraints, this hack seems to be the best option for now.
  2922. * Please refer to the following thread for details.
  2923. *
  2924. * http://thread.gmane.org/gmane.linux.kernel/1420814
  2925. */
  2926. if (!mod->async_probe_requested && (current->flags & PF_USED_ASYNC))
  2927. async_synchronize_full();
  2928. mutex_lock(&module_mutex);
  2929. /* Drop initial reference. */
  2930. module_put(mod);
  2931. trim_init_extable(mod);
  2932. #ifdef CONFIG_KALLSYMS
  2933. /* Switch to core kallsyms now init is done: kallsyms may be walking! */
  2934. rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
  2935. #endif
  2936. module_enable_ro(mod, true);
  2937. mod_tree_remove_init(mod);
  2938. disable_ro_nx(&mod->init_layout);
  2939. module_arch_freeing_init(mod);
  2940. mod->init_layout.base = NULL;
  2941. mod->init_layout.size = 0;
  2942. mod->init_layout.ro_size = 0;
  2943. mod->init_layout.ro_after_init_size = 0;
  2944. mod->init_layout.text_size = 0;
  2945. /*
  2946. * We want to free module_init, but be aware that kallsyms may be
  2947. * walking this with preempt disabled. In all the failure paths, we
  2948. * call synchronize_sched(), but we don't want to slow down the success
  2949. * path, so use actual RCU here.
  2950. */
  2951. call_rcu_sched(&freeinit->rcu, do_free_init);
  2952. mutex_unlock(&module_mutex);
  2953. wake_up_all(&module_wq);
  2954. return 0;
  2955. fail_free_freeinit:
  2956. kfree(freeinit);
  2957. fail:
  2958. /* Try to protect us from buggy refcounters. */
  2959. mod->state = MODULE_STATE_GOING;
  2960. synchronize_sched();
  2961. module_put(mod);
  2962. blocking_notifier_call_chain(&module_notify_list,
  2963. MODULE_STATE_GOING, mod);
  2964. klp_module_going(mod);
  2965. ftrace_release_mod(mod);
  2966. free_module(mod);
  2967. wake_up_all(&module_wq);
  2968. return ret;
  2969. }
  2970. static int may_init_module(void)
  2971. {
  2972. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2973. return -EPERM;
  2974. return 0;
  2975. }
  2976. /*
  2977. * We try to place it in the list now to make sure it's unique before
  2978. * we dedicate too many resources. In particular, temporary percpu
  2979. * memory exhaustion.
  2980. */
  2981. static int add_unformed_module(struct module *mod)
  2982. {
  2983. int err;
  2984. struct module *old;
  2985. mod->state = MODULE_STATE_UNFORMED;
  2986. again:
  2987. mutex_lock(&module_mutex);
  2988. old = find_module_all(mod->name, strlen(mod->name), true);
  2989. if (old != NULL) {
  2990. if (old->state == MODULE_STATE_COMING
  2991. || old->state == MODULE_STATE_UNFORMED) {
  2992. /* Wait in case it fails to load. */
  2993. mutex_unlock(&module_mutex);
  2994. err = wait_event_interruptible(module_wq,
  2995. finished_loading(mod->name));
  2996. if (err)
  2997. goto out_unlocked;
  2998. goto again;
  2999. }
  3000. err = -EEXIST;
  3001. goto out;
  3002. }
  3003. mod_update_bounds(mod);
  3004. list_add_rcu(&mod->list, &modules);
  3005. mod_tree_insert(mod);
  3006. err = 0;
  3007. out:
  3008. mutex_unlock(&module_mutex);
  3009. out_unlocked:
  3010. return err;
  3011. }
  3012. static int complete_formation(struct module *mod, struct load_info *info)
  3013. {
  3014. int err;
  3015. mutex_lock(&module_mutex);
  3016. /* Find duplicate symbols (must be called under lock). */
  3017. err = verify_export_symbols(mod);
  3018. if (err < 0)
  3019. goto out;
  3020. /* This relies on module_mutex for list integrity. */
  3021. module_bug_finalize(info->hdr, info->sechdrs, mod);
  3022. module_enable_ro(mod, false);
  3023. module_enable_nx(mod);
  3024. /* Mark state as coming so strong_try_module_get() ignores us,
  3025. * but kallsyms etc. can see us. */
  3026. mod->state = MODULE_STATE_COMING;
  3027. mutex_unlock(&module_mutex);
  3028. return 0;
  3029. out:
  3030. mutex_unlock(&module_mutex);
  3031. return err;
  3032. }
  3033. static int prepare_coming_module(struct module *mod)
  3034. {
  3035. int err;
  3036. ftrace_module_enable(mod);
  3037. err = klp_module_coming(mod);
  3038. if (err)
  3039. return err;
  3040. blocking_notifier_call_chain(&module_notify_list,
  3041. MODULE_STATE_COMING, mod);
  3042. return 0;
  3043. }
  3044. static int unknown_module_param_cb(char *param, char *val, const char *modname,
  3045. void *arg)
  3046. {
  3047. struct module *mod = arg;
  3048. int ret;
  3049. if (strcmp(param, "async_probe") == 0) {
  3050. mod->async_probe_requested = true;
  3051. return 0;
  3052. }
  3053. /* Check for magic 'dyndbg' arg */
  3054. ret = ddebug_dyndbg_module_param_cb(param, val, modname);
  3055. if (ret != 0)
  3056. pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
  3057. return 0;
  3058. }
  3059. /* Allocate and load the module: note that size of section 0 is always
  3060. zero, and we rely on this for optional sections. */
  3061. static int load_module(struct load_info *info, const char __user *uargs,
  3062. int flags)
  3063. {
  3064. struct module *mod;
  3065. long err;
  3066. char *after_dashes;
  3067. err = module_sig_check(info, flags);
  3068. if (err)
  3069. goto free_copy;
  3070. err = elf_header_check(info);
  3071. if (err)
  3072. goto free_copy;
  3073. /* Figure out module layout, and allocate all the memory. */
  3074. mod = layout_and_allocate(info, flags);
  3075. if (IS_ERR(mod)) {
  3076. err = PTR_ERR(mod);
  3077. goto free_copy;
  3078. }
  3079. /* Reserve our place in the list. */
  3080. err = add_unformed_module(mod);
  3081. if (err)
  3082. goto free_module;
  3083. #ifdef CONFIG_MODULE_SIG
  3084. mod->sig_ok = info->sig_ok;
  3085. if (!mod->sig_ok) {
  3086. pr_notice_once("%s: module verification failed: signature "
  3087. "and/or required key missing - tainting "
  3088. "kernel\n", mod->name);
  3089. add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
  3090. }
  3091. #endif
  3092. /* To avoid stressing percpu allocator, do this once we're unique. */
  3093. err = percpu_modalloc(mod, info);
  3094. if (err)
  3095. goto unlink_mod;
  3096. /* Now module is in final location, initialize linked lists, etc. */
  3097. err = module_unload_init(mod);
  3098. if (err)
  3099. goto unlink_mod;
  3100. init_param_lock(mod);
  3101. /* Now we've got everything in the final locations, we can
  3102. * find optional sections. */
  3103. err = find_module_sections(mod, info);
  3104. if (err)
  3105. goto free_unload;
  3106. err = check_module_license_and_versions(mod);
  3107. if (err)
  3108. goto free_unload;
  3109. /* Set up MODINFO_ATTR fields */
  3110. setup_modinfo(mod, info);
  3111. /* Fix up syms, so that st_value is a pointer to location. */
  3112. err = simplify_symbols(mod, info);
  3113. if (err < 0)
  3114. goto free_modinfo;
  3115. err = apply_relocations(mod, info);
  3116. if (err < 0)
  3117. goto free_modinfo;
  3118. err = post_relocation(mod, info);
  3119. if (err < 0)
  3120. goto free_modinfo;
  3121. flush_module_icache(mod);
  3122. /* Now copy in args */
  3123. mod->args = strndup_user(uargs, ~0UL >> 1);
  3124. if (IS_ERR(mod->args)) {
  3125. err = PTR_ERR(mod->args);
  3126. goto free_arch_cleanup;
  3127. }
  3128. dynamic_debug_setup(info->debug, info->num_debug);
  3129. /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
  3130. ftrace_module_init(mod);
  3131. /* Finally it's fully formed, ready to start executing. */
  3132. err = complete_formation(mod, info);
  3133. if (err)
  3134. goto ddebug_cleanup;
  3135. err = prepare_coming_module(mod);
  3136. if (err)
  3137. goto bug_cleanup;
  3138. /* Module is ready to execute: parsing args may do that. */
  3139. after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
  3140. -32768, 32767, mod,
  3141. unknown_module_param_cb);
  3142. if (IS_ERR(after_dashes)) {
  3143. err = PTR_ERR(after_dashes);
  3144. goto coming_cleanup;
  3145. } else if (after_dashes) {
  3146. pr_warn("%s: parameters '%s' after `--' ignored\n",
  3147. mod->name, after_dashes);
  3148. }
  3149. /* Link in to syfs. */
  3150. err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
  3151. if (err < 0)
  3152. goto coming_cleanup;
  3153. if (is_livepatch_module(mod)) {
  3154. err = copy_module_elf(mod, info);
  3155. if (err < 0)
  3156. goto sysfs_cleanup;
  3157. }
  3158. /* Get rid of temporary copy. */
  3159. free_copy(info);
  3160. /* Done! */
  3161. trace_module_load(mod);
  3162. return do_init_module(mod);
  3163. sysfs_cleanup:
  3164. mod_sysfs_teardown(mod);
  3165. coming_cleanup:
  3166. mod->state = MODULE_STATE_GOING;
  3167. blocking_notifier_call_chain(&module_notify_list,
  3168. MODULE_STATE_GOING, mod);
  3169. klp_module_going(mod);
  3170. bug_cleanup:
  3171. /* module_bug_cleanup needs module_mutex protection */
  3172. mutex_lock(&module_mutex);
  3173. module_bug_cleanup(mod);
  3174. mutex_unlock(&module_mutex);
  3175. /* we can't deallocate the module until we clear memory protection */
  3176. module_disable_ro(mod);
  3177. module_disable_nx(mod);
  3178. ddebug_cleanup:
  3179. dynamic_debug_remove(info->debug);
  3180. synchronize_sched();
  3181. kfree(mod->args);
  3182. free_arch_cleanup:
  3183. module_arch_cleanup(mod);
  3184. free_modinfo:
  3185. free_modinfo(mod);
  3186. free_unload:
  3187. module_unload_free(mod);
  3188. unlink_mod:
  3189. mutex_lock(&module_mutex);
  3190. /* Unlink carefully: kallsyms could be walking list. */
  3191. list_del_rcu(&mod->list);
  3192. mod_tree_remove(mod);
  3193. wake_up_all(&module_wq);
  3194. /* Wait for RCU-sched synchronizing before releasing mod->list. */
  3195. synchronize_sched();
  3196. mutex_unlock(&module_mutex);
  3197. free_module:
  3198. /*
  3199. * Ftrace needs to clean up what it initialized.
  3200. * This does nothing if ftrace_module_init() wasn't called,
  3201. * but it must be called outside of module_mutex.
  3202. */
  3203. ftrace_release_mod(mod);
  3204. /* Free lock-classes; relies on the preceding sync_rcu() */
  3205. lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
  3206. module_deallocate(mod, info);
  3207. free_copy:
  3208. free_copy(info);
  3209. return err;
  3210. }
  3211. SYSCALL_DEFINE3(init_module, void __user *, umod,
  3212. unsigned long, len, const char __user *, uargs)
  3213. {
  3214. int err;
  3215. struct load_info info = { };
  3216. err = may_init_module();
  3217. if (err)
  3218. return err;
  3219. pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
  3220. umod, len, uargs);
  3221. err = copy_module_from_user(umod, len, &info);
  3222. if (err)
  3223. return err;
  3224. return load_module(&info, uargs, 0);
  3225. }
  3226. SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
  3227. {
  3228. struct load_info info = { };
  3229. loff_t size;
  3230. void *hdr;
  3231. int err;
  3232. err = may_init_module();
  3233. if (err)
  3234. return err;
  3235. pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
  3236. if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
  3237. |MODULE_INIT_IGNORE_VERMAGIC))
  3238. return -EINVAL;
  3239. err = kernel_read_file_from_fd(fd, &hdr, &size, INT_MAX,
  3240. READING_MODULE);
  3241. if (err)
  3242. return err;
  3243. info.hdr = hdr;
  3244. info.len = size;
  3245. return load_module(&info, uargs, flags);
  3246. }
  3247. static inline int within(unsigned long addr, void *start, unsigned long size)
  3248. {
  3249. return ((void *)addr >= start && (void *)addr < start + size);
  3250. }
  3251. #ifdef CONFIG_KALLSYMS
  3252. /*
  3253. * This ignores the intensely annoying "mapping symbols" found
  3254. * in ARM ELF files: $a, $t and $d.
  3255. */
  3256. static inline int is_arm_mapping_symbol(const char *str)
  3257. {
  3258. if (str[0] == '.' && str[1] == 'L')
  3259. return true;
  3260. return str[0] == '$' && strchr("axtd", str[1])
  3261. && (str[2] == '\0' || str[2] == '.');
  3262. }
  3263. static const char *symname(struct mod_kallsyms *kallsyms, unsigned int symnum)
  3264. {
  3265. return kallsyms->strtab + kallsyms->symtab[symnum].st_name;
  3266. }
  3267. static const char *get_ksymbol(struct module *mod,
  3268. unsigned long addr,
  3269. unsigned long *size,
  3270. unsigned long *offset)
  3271. {
  3272. unsigned int i, best = 0;
  3273. unsigned long nextval;
  3274. struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
  3275. /* At worse, next value is at end of module */
  3276. if (within_module_init(addr, mod))
  3277. nextval = (unsigned long)mod->init_layout.base+mod->init_layout.text_size;
  3278. else
  3279. nextval = (unsigned long)mod->core_layout.base+mod->core_layout.text_size;
  3280. /* Scan for closest preceding symbol, and next symbol. (ELF
  3281. starts real symbols at 1). */
  3282. for (i = 1; i < kallsyms->num_symtab; i++) {
  3283. if (kallsyms->symtab[i].st_shndx == SHN_UNDEF)
  3284. continue;
  3285. /* We ignore unnamed symbols: they're uninformative
  3286. * and inserted at a whim. */
  3287. if (*symname(kallsyms, i) == '\0'
  3288. || is_arm_mapping_symbol(symname(kallsyms, i)))
  3289. continue;
  3290. if (kallsyms->symtab[i].st_value <= addr
  3291. && kallsyms->symtab[i].st_value > kallsyms->symtab[best].st_value)
  3292. best = i;
  3293. if (kallsyms->symtab[i].st_value > addr
  3294. && kallsyms->symtab[i].st_value < nextval)
  3295. nextval = kallsyms->symtab[i].st_value;
  3296. }
  3297. if (!best)
  3298. return NULL;
  3299. if (size)
  3300. *size = nextval - kallsyms->symtab[best].st_value;
  3301. if (offset)
  3302. *offset = addr - kallsyms->symtab[best].st_value;
  3303. return symname(kallsyms, best);
  3304. }
  3305. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  3306. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  3307. const char *module_address_lookup(unsigned long addr,
  3308. unsigned long *size,
  3309. unsigned long *offset,
  3310. char **modname,
  3311. char *namebuf)
  3312. {
  3313. const char *ret = NULL;
  3314. struct module *mod;
  3315. preempt_disable();
  3316. mod = __module_address(addr);
  3317. if (mod) {
  3318. if (modname)
  3319. *modname = mod->name;
  3320. ret = get_ksymbol(mod, addr, size, offset);
  3321. }
  3322. /* Make a copy in here where it's safe */
  3323. if (ret) {
  3324. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  3325. ret = namebuf;
  3326. }
  3327. preempt_enable();
  3328. return ret;
  3329. }
  3330. int lookup_module_symbol_name(unsigned long addr, char *symname)
  3331. {
  3332. struct module *mod;
  3333. preempt_disable();
  3334. list_for_each_entry_rcu(mod, &modules, list) {
  3335. if (mod->state == MODULE_STATE_UNFORMED)
  3336. continue;
  3337. if (within_module(addr, mod)) {
  3338. const char *sym;
  3339. sym = get_ksymbol(mod, addr, NULL, NULL);
  3340. if (!sym)
  3341. goto out;
  3342. strlcpy(symname, sym, KSYM_NAME_LEN);
  3343. preempt_enable();
  3344. return 0;
  3345. }
  3346. }
  3347. out:
  3348. preempt_enable();
  3349. return -ERANGE;
  3350. }
  3351. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  3352. unsigned long *offset, char *modname, char *name)
  3353. {
  3354. struct module *mod;
  3355. preempt_disable();
  3356. list_for_each_entry_rcu(mod, &modules, list) {
  3357. if (mod->state == MODULE_STATE_UNFORMED)
  3358. continue;
  3359. if (within_module(addr, mod)) {
  3360. const char *sym;
  3361. sym = get_ksymbol(mod, addr, size, offset);
  3362. if (!sym)
  3363. goto out;
  3364. if (modname)
  3365. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  3366. if (name)
  3367. strlcpy(name, sym, KSYM_NAME_LEN);
  3368. preempt_enable();
  3369. return 0;
  3370. }
  3371. }
  3372. out:
  3373. preempt_enable();
  3374. return -ERANGE;
  3375. }
  3376. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  3377. char *name, char *module_name, int *exported)
  3378. {
  3379. struct module *mod;
  3380. preempt_disable();
  3381. list_for_each_entry_rcu(mod, &modules, list) {
  3382. struct mod_kallsyms *kallsyms;
  3383. if (mod->state == MODULE_STATE_UNFORMED)
  3384. continue;
  3385. kallsyms = rcu_dereference_sched(mod->kallsyms);
  3386. if (symnum < kallsyms->num_symtab) {
  3387. *value = kallsyms->symtab[symnum].st_value;
  3388. *type = kallsyms->symtab[symnum].st_info;
  3389. strlcpy(name, symname(kallsyms, symnum), KSYM_NAME_LEN);
  3390. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  3391. *exported = is_exported(name, *value, mod);
  3392. preempt_enable();
  3393. return 0;
  3394. }
  3395. symnum -= kallsyms->num_symtab;
  3396. }
  3397. preempt_enable();
  3398. return -ERANGE;
  3399. }
  3400. static unsigned long mod_find_symname(struct module *mod, const char *name)
  3401. {
  3402. unsigned int i;
  3403. struct mod_kallsyms *kallsyms = rcu_dereference_sched(mod->kallsyms);
  3404. for (i = 0; i < kallsyms->num_symtab; i++)
  3405. if (strcmp(name, symname(kallsyms, i)) == 0 &&
  3406. kallsyms->symtab[i].st_info != 'U')
  3407. return kallsyms->symtab[i].st_value;
  3408. return 0;
  3409. }
  3410. /* Look for this name: can be of form module:name. */
  3411. unsigned long module_kallsyms_lookup_name(const char *name)
  3412. {
  3413. struct module *mod;
  3414. char *colon;
  3415. unsigned long ret = 0;
  3416. /* Don't lock: we're in enough trouble already. */
  3417. preempt_disable();
  3418. if ((colon = strchr(name, ':')) != NULL) {
  3419. if ((mod = find_module_all(name, colon - name, false)) != NULL)
  3420. ret = mod_find_symname(mod, colon+1);
  3421. } else {
  3422. list_for_each_entry_rcu(mod, &modules, list) {
  3423. if (mod->state == MODULE_STATE_UNFORMED)
  3424. continue;
  3425. if ((ret = mod_find_symname(mod, name)) != 0)
  3426. break;
  3427. }
  3428. }
  3429. preempt_enable();
  3430. return ret;
  3431. }
  3432. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  3433. struct module *, unsigned long),
  3434. void *data)
  3435. {
  3436. struct module *mod;
  3437. unsigned int i;
  3438. int ret;
  3439. module_assert_mutex();
  3440. list_for_each_entry(mod, &modules, list) {
  3441. /* We hold module_mutex: no need for rcu_dereference_sched */
  3442. struct mod_kallsyms *kallsyms = mod->kallsyms;
  3443. if (mod->state == MODULE_STATE_UNFORMED)
  3444. continue;
  3445. for (i = 0; i < kallsyms->num_symtab; i++) {
  3446. ret = fn(data, symname(kallsyms, i),
  3447. mod, kallsyms->symtab[i].st_value);
  3448. if (ret != 0)
  3449. return ret;
  3450. }
  3451. }
  3452. return 0;
  3453. }
  3454. #endif /* CONFIG_KALLSYMS */
  3455. /* Maximum number of characters written by module_flags() */
  3456. #define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
  3457. /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
  3458. static char *module_flags(struct module *mod, char *buf)
  3459. {
  3460. int bx = 0;
  3461. BUG_ON(mod->state == MODULE_STATE_UNFORMED);
  3462. if (mod->taints ||
  3463. mod->state == MODULE_STATE_GOING ||
  3464. mod->state == MODULE_STATE_COMING) {
  3465. buf[bx++] = '(';
  3466. bx += module_flags_taint(mod, buf + bx);
  3467. /* Show a - for module-is-being-unloaded */
  3468. if (mod->state == MODULE_STATE_GOING)
  3469. buf[bx++] = '-';
  3470. /* Show a + for module-is-being-loaded */
  3471. if (mod->state == MODULE_STATE_COMING)
  3472. buf[bx++] = '+';
  3473. buf[bx++] = ')';
  3474. }
  3475. buf[bx] = '\0';
  3476. return buf;
  3477. }
  3478. #ifdef CONFIG_PROC_FS
  3479. /* Called by the /proc file system to return a list of modules. */
  3480. static void *m_start(struct seq_file *m, loff_t *pos)
  3481. {
  3482. mutex_lock(&module_mutex);
  3483. return seq_list_start(&modules, *pos);
  3484. }
  3485. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  3486. {
  3487. return seq_list_next(p, &modules, pos);
  3488. }
  3489. static void m_stop(struct seq_file *m, void *p)
  3490. {
  3491. mutex_unlock(&module_mutex);
  3492. }
  3493. static int m_show(struct seq_file *m, void *p)
  3494. {
  3495. struct module *mod = list_entry(p, struct module, list);
  3496. char buf[MODULE_FLAGS_BUF_SIZE];
  3497. /* We always ignore unformed modules. */
  3498. if (mod->state == MODULE_STATE_UNFORMED)
  3499. return 0;
  3500. seq_printf(m, "%s %u",
  3501. mod->name, mod->init_layout.size + mod->core_layout.size);
  3502. print_unload_info(m, mod);
  3503. /* Informative for users. */
  3504. seq_printf(m, " %s",
  3505. mod->state == MODULE_STATE_GOING ? "Unloading" :
  3506. mod->state == MODULE_STATE_COMING ? "Loading" :
  3507. "Live");
  3508. /* Used by oprofile and other similar tools. */
  3509. seq_printf(m, " 0x%pK", mod->core_layout.base);
  3510. /* Taints info */
  3511. if (mod->taints)
  3512. seq_printf(m, " %s", module_flags(mod, buf));
  3513. seq_puts(m, "\n");
  3514. return 0;
  3515. }
  3516. /* Format: modulename size refcount deps address
  3517. Where refcount is a number or -, and deps is a comma-separated list
  3518. of depends or -.
  3519. */
  3520. static const struct seq_operations modules_op = {
  3521. .start = m_start,
  3522. .next = m_next,
  3523. .stop = m_stop,
  3524. .show = m_show
  3525. };
  3526. static int modules_open(struct inode *inode, struct file *file)
  3527. {
  3528. return seq_open(file, &modules_op);
  3529. }
  3530. static const struct file_operations proc_modules_operations = {
  3531. .open = modules_open,
  3532. .read = seq_read,
  3533. .llseek = seq_lseek,
  3534. .release = seq_release,
  3535. };
  3536. static int __init proc_modules_init(void)
  3537. {
  3538. proc_create("modules", 0, NULL, &proc_modules_operations);
  3539. return 0;
  3540. }
  3541. module_init(proc_modules_init);
  3542. #endif
  3543. /* Given an address, look for it in the module exception tables. */
  3544. const struct exception_table_entry *search_module_extables(unsigned long addr)
  3545. {
  3546. const struct exception_table_entry *e = NULL;
  3547. struct module *mod;
  3548. preempt_disable();
  3549. list_for_each_entry_rcu(mod, &modules, list) {
  3550. if (mod->state == MODULE_STATE_UNFORMED)
  3551. continue;
  3552. if (mod->num_exentries == 0)
  3553. continue;
  3554. e = search_extable(mod->extable,
  3555. mod->extable + mod->num_exentries - 1,
  3556. addr);
  3557. if (e)
  3558. break;
  3559. }
  3560. preempt_enable();
  3561. /* Now, if we found one, we are running inside it now, hence
  3562. we cannot unload the module, hence no refcnt needed. */
  3563. return e;
  3564. }
  3565. /*
  3566. * is_module_address - is this address inside a module?
  3567. * @addr: the address to check.
  3568. *
  3569. * See is_module_text_address() if you simply want to see if the address
  3570. * is code (not data).
  3571. */
  3572. bool is_module_address(unsigned long addr)
  3573. {
  3574. bool ret;
  3575. preempt_disable();
  3576. ret = __module_address(addr) != NULL;
  3577. preempt_enable();
  3578. return ret;
  3579. }
  3580. /*
  3581. * __module_address - get the module which contains an address.
  3582. * @addr: the address.
  3583. *
  3584. * Must be called with preempt disabled or module mutex held so that
  3585. * module doesn't get freed during this.
  3586. */
  3587. struct module *__module_address(unsigned long addr)
  3588. {
  3589. struct module *mod;
  3590. if (addr < module_addr_min || addr > module_addr_max)
  3591. return NULL;
  3592. module_assert_mutex_or_preempt();
  3593. mod = mod_find(addr);
  3594. if (mod) {
  3595. BUG_ON(!within_module(addr, mod));
  3596. if (mod->state == MODULE_STATE_UNFORMED)
  3597. mod = NULL;
  3598. }
  3599. return mod;
  3600. }
  3601. EXPORT_SYMBOL_GPL(__module_address);
  3602. /*
  3603. * is_module_text_address - is this address inside module code?
  3604. * @addr: the address to check.
  3605. *
  3606. * See is_module_address() if you simply want to see if the address is
  3607. * anywhere in a module. See kernel_text_address() for testing if an
  3608. * address corresponds to kernel or module code.
  3609. */
  3610. bool is_module_text_address(unsigned long addr)
  3611. {
  3612. bool ret;
  3613. preempt_disable();
  3614. ret = __module_text_address(addr) != NULL;
  3615. preempt_enable();
  3616. return ret;
  3617. }
  3618. /*
  3619. * __module_text_address - get the module whose code contains an address.
  3620. * @addr: the address.
  3621. *
  3622. * Must be called with preempt disabled or module mutex held so that
  3623. * module doesn't get freed during this.
  3624. */
  3625. struct module *__module_text_address(unsigned long addr)
  3626. {
  3627. struct module *mod = __module_address(addr);
  3628. if (mod) {
  3629. /* Make sure it's within the text section. */
  3630. if (!within(addr, mod->init_layout.base, mod->init_layout.text_size)
  3631. && !within(addr, mod->core_layout.base, mod->core_layout.text_size))
  3632. mod = NULL;
  3633. }
  3634. return mod;
  3635. }
  3636. EXPORT_SYMBOL_GPL(__module_text_address);
  3637. /* Don't grab lock, we're oopsing. */
  3638. void print_modules(void)
  3639. {
  3640. struct module *mod;
  3641. char buf[MODULE_FLAGS_BUF_SIZE];
  3642. printk(KERN_DEFAULT "Modules linked in:");
  3643. /* Most callers should already have preempt disabled, but make sure */
  3644. preempt_disable();
  3645. list_for_each_entry_rcu(mod, &modules, list) {
  3646. if (mod->state == MODULE_STATE_UNFORMED)
  3647. continue;
  3648. pr_cont(" %s%s", mod->name, module_flags(mod, buf));
  3649. }
  3650. preempt_enable();
  3651. if (last_unloaded_module[0])
  3652. pr_cont(" [last unloaded: %s]", last_unloaded_module);
  3653. pr_cont("\n");
  3654. }
  3655. #ifdef CONFIG_MODVERSIONS
  3656. /* Generate the signature for all relevant module structures here.
  3657. * If these change, we don't want to try to parse the module. */
  3658. void module_layout(struct module *mod,
  3659. struct modversion_info *ver,
  3660. struct kernel_param *kp,
  3661. struct kernel_symbol *ks,
  3662. struct tracepoint * const *tp)
  3663. {
  3664. }
  3665. EXPORT_SYMBOL(module_layout);
  3666. #endif