module.c 102 KB

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