module.c 104 KB

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