module.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064
  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/ftrace_event.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. #else /* !CONFIG_SYSFS */
  1550. static int mod_sysfs_setup(struct module *mod,
  1551. const struct load_info *info,
  1552. struct kernel_param *kparam,
  1553. unsigned int num_params)
  1554. {
  1555. return 0;
  1556. }
  1557. static void mod_sysfs_fini(struct module *mod)
  1558. {
  1559. }
  1560. static void module_remove_modinfo_attrs(struct module *mod)
  1561. {
  1562. }
  1563. static void del_usage_links(struct module *mod)
  1564. {
  1565. }
  1566. #endif /* CONFIG_SYSFS */
  1567. static void mod_sysfs_teardown(struct module *mod)
  1568. {
  1569. del_usage_links(mod);
  1570. module_remove_modinfo_attrs(mod);
  1571. module_param_sysfs_remove(mod);
  1572. kobject_put(mod->mkobj.drivers_dir);
  1573. kobject_put(mod->holders_dir);
  1574. mod_sysfs_fini(mod);
  1575. }
  1576. #ifdef CONFIG_DEBUG_SET_MODULE_RONX
  1577. /*
  1578. * LKM RO/NX protection: protect module's text/ro-data
  1579. * from modification and any data from execution.
  1580. */
  1581. void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
  1582. {
  1583. unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
  1584. unsigned long end_pfn = PFN_DOWN((unsigned long)end);
  1585. if (end_pfn > begin_pfn)
  1586. set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
  1587. }
  1588. static void set_section_ro_nx(void *base,
  1589. unsigned long text_size,
  1590. unsigned long ro_size,
  1591. unsigned long total_size)
  1592. {
  1593. /* begin and end PFNs of the current subsection */
  1594. unsigned long begin_pfn;
  1595. unsigned long end_pfn;
  1596. /*
  1597. * Set RO for module text and RO-data:
  1598. * - Always protect first page.
  1599. * - Do not protect last partial page.
  1600. */
  1601. if (ro_size > 0)
  1602. set_page_attributes(base, base + ro_size, set_memory_ro);
  1603. /*
  1604. * Set NX permissions for module data:
  1605. * - Do not protect first partial page.
  1606. * - Always protect last page.
  1607. */
  1608. if (total_size > text_size) {
  1609. begin_pfn = PFN_UP((unsigned long)base + text_size);
  1610. end_pfn = PFN_UP((unsigned long)base + total_size);
  1611. if (end_pfn > begin_pfn)
  1612. set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
  1613. }
  1614. }
  1615. static void unset_module_core_ro_nx(struct module *mod)
  1616. {
  1617. set_page_attributes(mod->module_core + mod->core_text_size,
  1618. mod->module_core + mod->core_size,
  1619. set_memory_x);
  1620. set_page_attributes(mod->module_core,
  1621. mod->module_core + mod->core_ro_size,
  1622. set_memory_rw);
  1623. }
  1624. static void unset_module_init_ro_nx(struct module *mod)
  1625. {
  1626. set_page_attributes(mod->module_init + mod->init_text_size,
  1627. mod->module_init + mod->init_size,
  1628. set_memory_x);
  1629. set_page_attributes(mod->module_init,
  1630. mod->module_init + mod->init_ro_size,
  1631. set_memory_rw);
  1632. }
  1633. /* Iterate through all modules and set each module's text as RW */
  1634. void set_all_modules_text_rw(void)
  1635. {
  1636. struct module *mod;
  1637. mutex_lock(&module_mutex);
  1638. list_for_each_entry_rcu(mod, &modules, list) {
  1639. if (mod->state == MODULE_STATE_UNFORMED)
  1640. continue;
  1641. if ((mod->module_core) && (mod->core_text_size)) {
  1642. set_page_attributes(mod->module_core,
  1643. mod->module_core + mod->core_text_size,
  1644. set_memory_rw);
  1645. }
  1646. if ((mod->module_init) && (mod->init_text_size)) {
  1647. set_page_attributes(mod->module_init,
  1648. mod->module_init + mod->init_text_size,
  1649. set_memory_rw);
  1650. }
  1651. }
  1652. mutex_unlock(&module_mutex);
  1653. }
  1654. /* Iterate through all modules and set each module's text as RO */
  1655. void set_all_modules_text_ro(void)
  1656. {
  1657. struct module *mod;
  1658. mutex_lock(&module_mutex);
  1659. list_for_each_entry_rcu(mod, &modules, list) {
  1660. if (mod->state == MODULE_STATE_UNFORMED)
  1661. continue;
  1662. if ((mod->module_core) && (mod->core_text_size)) {
  1663. set_page_attributes(mod->module_core,
  1664. mod->module_core + mod->core_text_size,
  1665. set_memory_ro);
  1666. }
  1667. if ((mod->module_init) && (mod->init_text_size)) {
  1668. set_page_attributes(mod->module_init,
  1669. mod->module_init + mod->init_text_size,
  1670. set_memory_ro);
  1671. }
  1672. }
  1673. mutex_unlock(&module_mutex);
  1674. }
  1675. #else
  1676. static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
  1677. static void unset_module_core_ro_nx(struct module *mod) { }
  1678. static void unset_module_init_ro_nx(struct module *mod) { }
  1679. #endif
  1680. void __weak module_memfree(void *module_region)
  1681. {
  1682. vfree(module_region);
  1683. }
  1684. void __weak module_arch_cleanup(struct module *mod)
  1685. {
  1686. }
  1687. void __weak module_arch_freeing_init(struct module *mod)
  1688. {
  1689. }
  1690. /* Free a module, remove from lists, etc. */
  1691. static void free_module(struct module *mod)
  1692. {
  1693. trace_module_free(mod);
  1694. mod_sysfs_teardown(mod);
  1695. /* We leave it in list to prevent duplicate loads, but make sure
  1696. * that noone uses it while it's being deconstructed. */
  1697. mutex_lock(&module_mutex);
  1698. mod->state = MODULE_STATE_UNFORMED;
  1699. mutex_unlock(&module_mutex);
  1700. /* Remove dynamic debug info */
  1701. ddebug_remove_module(mod->name);
  1702. /* Arch-specific cleanup. */
  1703. module_arch_cleanup(mod);
  1704. /* Module unload stuff */
  1705. module_unload_free(mod);
  1706. /* Free any allocated parameters. */
  1707. destroy_params(mod->kp, mod->num_kp);
  1708. /* Now we can delete it from the lists */
  1709. mutex_lock(&module_mutex);
  1710. /* Unlink carefully: kallsyms could be walking list. */
  1711. list_del_rcu(&mod->list);
  1712. mod_tree_remove(mod);
  1713. /* Remove this module from bug list, this uses list_del_rcu */
  1714. module_bug_cleanup(mod);
  1715. /* Wait for RCU-sched synchronizing before releasing mod->list and buglist. */
  1716. synchronize_sched();
  1717. mutex_unlock(&module_mutex);
  1718. /* This may be NULL, but that's OK */
  1719. unset_module_init_ro_nx(mod);
  1720. module_arch_freeing_init(mod);
  1721. module_memfree(mod->module_init);
  1722. kfree(mod->args);
  1723. percpu_modfree(mod);
  1724. /* Free lock-classes; relies on the preceding sync_rcu(). */
  1725. lockdep_free_key_range(mod->module_core, mod->core_size);
  1726. /* Finally, free the core (containing the module structure) */
  1727. unset_module_core_ro_nx(mod);
  1728. module_memfree(mod->module_core);
  1729. #ifdef CONFIG_MPU
  1730. update_protections(current->mm);
  1731. #endif
  1732. }
  1733. void *__symbol_get(const char *symbol)
  1734. {
  1735. struct module *owner;
  1736. const struct kernel_symbol *sym;
  1737. preempt_disable();
  1738. sym = find_symbol(symbol, &owner, NULL, true, true);
  1739. if (sym && strong_try_module_get(owner))
  1740. sym = NULL;
  1741. preempt_enable();
  1742. return sym ? (void *)sym->value : NULL;
  1743. }
  1744. EXPORT_SYMBOL_GPL(__symbol_get);
  1745. /*
  1746. * Ensure that an exported symbol [global namespace] does not already exist
  1747. * in the kernel or in some other module's exported symbol table.
  1748. *
  1749. * You must hold the module_mutex.
  1750. */
  1751. static int verify_export_symbols(struct module *mod)
  1752. {
  1753. unsigned int i;
  1754. struct module *owner;
  1755. const struct kernel_symbol *s;
  1756. struct {
  1757. const struct kernel_symbol *sym;
  1758. unsigned int num;
  1759. } arr[] = {
  1760. { mod->syms, mod->num_syms },
  1761. { mod->gpl_syms, mod->num_gpl_syms },
  1762. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1763. #ifdef CONFIG_UNUSED_SYMBOLS
  1764. { mod->unused_syms, mod->num_unused_syms },
  1765. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1766. #endif
  1767. };
  1768. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1769. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1770. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1771. pr_err("%s: exports duplicate symbol %s"
  1772. " (owned by %s)\n",
  1773. mod->name, s->name, module_name(owner));
  1774. return -ENOEXEC;
  1775. }
  1776. }
  1777. }
  1778. return 0;
  1779. }
  1780. /* Change all symbols so that st_value encodes the pointer directly. */
  1781. static int simplify_symbols(struct module *mod, const struct load_info *info)
  1782. {
  1783. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  1784. Elf_Sym *sym = (void *)symsec->sh_addr;
  1785. unsigned long secbase;
  1786. unsigned int i;
  1787. int ret = 0;
  1788. const struct kernel_symbol *ksym;
  1789. for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
  1790. const char *name = info->strtab + sym[i].st_name;
  1791. switch (sym[i].st_shndx) {
  1792. case SHN_COMMON:
  1793. /* Ignore common symbols */
  1794. if (!strncmp(name, "__gnu_lto", 9))
  1795. break;
  1796. /* We compiled with -fno-common. These are not
  1797. supposed to happen. */
  1798. pr_debug("Common symbol: %s\n", name);
  1799. pr_warn("%s: please compile with -fno-common\n",
  1800. mod->name);
  1801. ret = -ENOEXEC;
  1802. break;
  1803. case SHN_ABS:
  1804. /* Don't need to do anything */
  1805. pr_debug("Absolute symbol: 0x%08lx\n",
  1806. (long)sym[i].st_value);
  1807. break;
  1808. case SHN_UNDEF:
  1809. ksym = resolve_symbol_wait(mod, info, name);
  1810. /* Ok if resolved. */
  1811. if (ksym && !IS_ERR(ksym)) {
  1812. sym[i].st_value = ksym->value;
  1813. break;
  1814. }
  1815. /* Ok if weak. */
  1816. if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1817. break;
  1818. pr_warn("%s: Unknown symbol %s (err %li)\n",
  1819. mod->name, name, PTR_ERR(ksym));
  1820. ret = PTR_ERR(ksym) ?: -ENOENT;
  1821. break;
  1822. default:
  1823. /* Divert to percpu allocation if a percpu var. */
  1824. if (sym[i].st_shndx == info->index.pcpu)
  1825. secbase = (unsigned long)mod_percpu(mod);
  1826. else
  1827. secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
  1828. sym[i].st_value += secbase;
  1829. break;
  1830. }
  1831. }
  1832. return ret;
  1833. }
  1834. static int apply_relocations(struct module *mod, const struct load_info *info)
  1835. {
  1836. unsigned int i;
  1837. int err = 0;
  1838. /* Now do relocations. */
  1839. for (i = 1; i < info->hdr->e_shnum; i++) {
  1840. unsigned int infosec = info->sechdrs[i].sh_info;
  1841. /* Not a valid relocation section? */
  1842. if (infosec >= info->hdr->e_shnum)
  1843. continue;
  1844. /* Don't bother with non-allocated sections */
  1845. if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
  1846. continue;
  1847. if (info->sechdrs[i].sh_type == SHT_REL)
  1848. err = apply_relocate(info->sechdrs, info->strtab,
  1849. info->index.sym, i, mod);
  1850. else if (info->sechdrs[i].sh_type == SHT_RELA)
  1851. err = apply_relocate_add(info->sechdrs, info->strtab,
  1852. info->index.sym, i, mod);
  1853. if (err < 0)
  1854. break;
  1855. }
  1856. return err;
  1857. }
  1858. /* Additional bytes needed by arch in front of individual sections */
  1859. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1860. unsigned int section)
  1861. {
  1862. /* default implementation just returns zero */
  1863. return 0;
  1864. }
  1865. /* Update size with this section: return offset. */
  1866. static long get_offset(struct module *mod, unsigned int *size,
  1867. Elf_Shdr *sechdr, unsigned int section)
  1868. {
  1869. long ret;
  1870. *size += arch_mod_section_prepend(mod, section);
  1871. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1872. *size = ret + sechdr->sh_size;
  1873. return ret;
  1874. }
  1875. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1876. might -- code, read-only data, read-write data, small data. Tally
  1877. sizes, and place the offsets into sh_entsize fields: high bit means it
  1878. belongs in init. */
  1879. static void layout_sections(struct module *mod, struct load_info *info)
  1880. {
  1881. static unsigned long const masks[][2] = {
  1882. /* NOTE: all executable code must be the first section
  1883. * in this array; otherwise modify the text_size
  1884. * finder in the two loops below */
  1885. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1886. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1887. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1888. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1889. };
  1890. unsigned int m, i;
  1891. for (i = 0; i < info->hdr->e_shnum; i++)
  1892. info->sechdrs[i].sh_entsize = ~0UL;
  1893. pr_debug("Core section allocation order:\n");
  1894. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1895. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1896. Elf_Shdr *s = &info->sechdrs[i];
  1897. const char *sname = info->secstrings + s->sh_name;
  1898. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1899. || (s->sh_flags & masks[m][1])
  1900. || s->sh_entsize != ~0UL
  1901. || strstarts(sname, ".init"))
  1902. continue;
  1903. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1904. pr_debug("\t%s\n", sname);
  1905. }
  1906. switch (m) {
  1907. case 0: /* executable */
  1908. mod->core_size = debug_align(mod->core_size);
  1909. mod->core_text_size = mod->core_size;
  1910. break;
  1911. case 1: /* RO: text and ro-data */
  1912. mod->core_size = debug_align(mod->core_size);
  1913. mod->core_ro_size = mod->core_size;
  1914. break;
  1915. case 3: /* whole core */
  1916. mod->core_size = debug_align(mod->core_size);
  1917. break;
  1918. }
  1919. }
  1920. pr_debug("Init section allocation order:\n");
  1921. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1922. for (i = 0; i < info->hdr->e_shnum; ++i) {
  1923. Elf_Shdr *s = &info->sechdrs[i];
  1924. const char *sname = info->secstrings + s->sh_name;
  1925. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1926. || (s->sh_flags & masks[m][1])
  1927. || s->sh_entsize != ~0UL
  1928. || !strstarts(sname, ".init"))
  1929. continue;
  1930. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1931. | INIT_OFFSET_MASK);
  1932. pr_debug("\t%s\n", sname);
  1933. }
  1934. switch (m) {
  1935. case 0: /* executable */
  1936. mod->init_size = debug_align(mod->init_size);
  1937. mod->init_text_size = mod->init_size;
  1938. break;
  1939. case 1: /* RO: text and ro-data */
  1940. mod->init_size = debug_align(mod->init_size);
  1941. mod->init_ro_size = mod->init_size;
  1942. break;
  1943. case 3: /* whole init */
  1944. mod->init_size = debug_align(mod->init_size);
  1945. break;
  1946. }
  1947. }
  1948. }
  1949. static void set_license(struct module *mod, const char *license)
  1950. {
  1951. if (!license)
  1952. license = "unspecified";
  1953. if (!license_is_gpl_compatible(license)) {
  1954. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1955. pr_warn("%s: module license '%s' taints kernel.\n",
  1956. mod->name, license);
  1957. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  1958. LOCKDEP_NOW_UNRELIABLE);
  1959. }
  1960. }
  1961. /* Parse tag=value strings from .modinfo section */
  1962. static char *next_string(char *string, unsigned long *secsize)
  1963. {
  1964. /* Skip non-zero chars */
  1965. while (string[0]) {
  1966. string++;
  1967. if ((*secsize)-- <= 1)
  1968. return NULL;
  1969. }
  1970. /* Skip any zero padding. */
  1971. while (!string[0]) {
  1972. string++;
  1973. if ((*secsize)-- <= 1)
  1974. return NULL;
  1975. }
  1976. return string;
  1977. }
  1978. static char *get_modinfo(struct load_info *info, const char *tag)
  1979. {
  1980. char *p;
  1981. unsigned int taglen = strlen(tag);
  1982. Elf_Shdr *infosec = &info->sechdrs[info->index.info];
  1983. unsigned long size = infosec->sh_size;
  1984. for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
  1985. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1986. return p + taglen + 1;
  1987. }
  1988. return NULL;
  1989. }
  1990. static void setup_modinfo(struct module *mod, struct load_info *info)
  1991. {
  1992. struct module_attribute *attr;
  1993. int i;
  1994. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1995. if (attr->setup)
  1996. attr->setup(mod, get_modinfo(info, attr->attr.name));
  1997. }
  1998. }
  1999. static void free_modinfo(struct module *mod)
  2000. {
  2001. struct module_attribute *attr;
  2002. int i;
  2003. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  2004. if (attr->free)
  2005. attr->free(mod);
  2006. }
  2007. }
  2008. #ifdef CONFIG_KALLSYMS
  2009. /* lookup symbol in given range of kernel_symbols */
  2010. static const struct kernel_symbol *lookup_symbol(const char *name,
  2011. const struct kernel_symbol *start,
  2012. const struct kernel_symbol *stop)
  2013. {
  2014. return bsearch(name, start, stop - start,
  2015. sizeof(struct kernel_symbol), cmp_name);
  2016. }
  2017. static int is_exported(const char *name, unsigned long value,
  2018. const struct module *mod)
  2019. {
  2020. const struct kernel_symbol *ks;
  2021. if (!mod)
  2022. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  2023. else
  2024. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  2025. return ks != NULL && ks->value == value;
  2026. }
  2027. /* As per nm */
  2028. static char elf_type(const Elf_Sym *sym, const struct load_info *info)
  2029. {
  2030. const Elf_Shdr *sechdrs = info->sechdrs;
  2031. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  2032. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  2033. return 'v';
  2034. else
  2035. return 'w';
  2036. }
  2037. if (sym->st_shndx == SHN_UNDEF)
  2038. return 'U';
  2039. if (sym->st_shndx == SHN_ABS)
  2040. return 'a';
  2041. if (sym->st_shndx >= SHN_LORESERVE)
  2042. return '?';
  2043. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  2044. return 't';
  2045. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  2046. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  2047. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  2048. return 'r';
  2049. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2050. return 'g';
  2051. else
  2052. return 'd';
  2053. }
  2054. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  2055. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  2056. return 's';
  2057. else
  2058. return 'b';
  2059. }
  2060. if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
  2061. ".debug")) {
  2062. return 'n';
  2063. }
  2064. return '?';
  2065. }
  2066. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  2067. unsigned int shnum)
  2068. {
  2069. const Elf_Shdr *sec;
  2070. if (src->st_shndx == SHN_UNDEF
  2071. || src->st_shndx >= shnum
  2072. || !src->st_name)
  2073. return false;
  2074. sec = sechdrs + src->st_shndx;
  2075. if (!(sec->sh_flags & SHF_ALLOC)
  2076. #ifndef CONFIG_KALLSYMS_ALL
  2077. || !(sec->sh_flags & SHF_EXECINSTR)
  2078. #endif
  2079. || (sec->sh_entsize & INIT_OFFSET_MASK))
  2080. return false;
  2081. return true;
  2082. }
  2083. /*
  2084. * We only allocate and copy the strings needed by the parts of symtab
  2085. * we keep. This is simple, but has the effect of making multiple
  2086. * copies of duplicates. We could be more sophisticated, see
  2087. * linux-kernel thread starting with
  2088. * <73defb5e4bca04a6431392cc341112b1@localhost>.
  2089. */
  2090. static void layout_symtab(struct module *mod, struct load_info *info)
  2091. {
  2092. Elf_Shdr *symsect = info->sechdrs + info->index.sym;
  2093. Elf_Shdr *strsect = info->sechdrs + info->index.str;
  2094. const Elf_Sym *src;
  2095. unsigned int i, nsrc, ndst, strtab_size = 0;
  2096. /* Put symbol section at end of init part of module. */
  2097. symsect->sh_flags |= SHF_ALLOC;
  2098. symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
  2099. info->index.sym) | INIT_OFFSET_MASK;
  2100. pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
  2101. src = (void *)info->hdr + symsect->sh_offset;
  2102. nsrc = symsect->sh_size / sizeof(*src);
  2103. /* Compute total space required for the core symbols' strtab. */
  2104. for (ndst = i = 0; i < nsrc; i++) {
  2105. if (i == 0 ||
  2106. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
  2107. strtab_size += strlen(&info->strtab[src[i].st_name])+1;
  2108. ndst++;
  2109. }
  2110. }
  2111. /* Append room for core symbols at end of core part. */
  2112. info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
  2113. info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
  2114. mod->core_size += strtab_size;
  2115. mod->core_size = debug_align(mod->core_size);
  2116. /* Put string table section at end of init part of module. */
  2117. strsect->sh_flags |= SHF_ALLOC;
  2118. strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
  2119. info->index.str) | INIT_OFFSET_MASK;
  2120. mod->init_size = debug_align(mod->init_size);
  2121. pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
  2122. }
  2123. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2124. {
  2125. unsigned int i, ndst;
  2126. const Elf_Sym *src;
  2127. Elf_Sym *dst;
  2128. char *s;
  2129. Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
  2130. mod->symtab = (void *)symsec->sh_addr;
  2131. mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
  2132. /* Make sure we get permanent strtab: don't use info->strtab. */
  2133. mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
  2134. /* Set types up while we still have access to sections. */
  2135. for (i = 0; i < mod->num_symtab; i++)
  2136. mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
  2137. mod->core_symtab = dst = mod->module_core + info->symoffs;
  2138. mod->core_strtab = s = mod->module_core + info->stroffs;
  2139. src = mod->symtab;
  2140. for (ndst = i = 0; i < mod->num_symtab; i++) {
  2141. if (i == 0 ||
  2142. is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum)) {
  2143. dst[ndst] = src[i];
  2144. dst[ndst++].st_name = s - mod->core_strtab;
  2145. s += strlcpy(s, &mod->strtab[src[i].st_name],
  2146. KSYM_NAME_LEN) + 1;
  2147. }
  2148. }
  2149. mod->core_num_syms = ndst;
  2150. }
  2151. #else
  2152. static inline void layout_symtab(struct module *mod, struct load_info *info)
  2153. {
  2154. }
  2155. static void add_kallsyms(struct module *mod, const struct load_info *info)
  2156. {
  2157. }
  2158. #endif /* CONFIG_KALLSYMS */
  2159. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  2160. {
  2161. if (!debug)
  2162. return;
  2163. #ifdef CONFIG_DYNAMIC_DEBUG
  2164. if (ddebug_add_module(debug, num, debug->modname))
  2165. pr_err("dynamic debug error adding module: %s\n",
  2166. debug->modname);
  2167. #endif
  2168. }
  2169. static void dynamic_debug_remove(struct _ddebug *debug)
  2170. {
  2171. if (debug)
  2172. ddebug_remove_module(debug->modname);
  2173. }
  2174. void * __weak module_alloc(unsigned long size)
  2175. {
  2176. return vmalloc_exec(size);
  2177. }
  2178. #ifdef CONFIG_DEBUG_KMEMLEAK
  2179. static void kmemleak_load_module(const struct module *mod,
  2180. const struct load_info *info)
  2181. {
  2182. unsigned int i;
  2183. /* only scan the sections containing data */
  2184. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  2185. for (i = 1; i < info->hdr->e_shnum; i++) {
  2186. /* Scan all writable sections that's not executable */
  2187. if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) ||
  2188. !(info->sechdrs[i].sh_flags & SHF_WRITE) ||
  2189. (info->sechdrs[i].sh_flags & SHF_EXECINSTR))
  2190. continue;
  2191. kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
  2192. info->sechdrs[i].sh_size, GFP_KERNEL);
  2193. }
  2194. }
  2195. #else
  2196. static inline void kmemleak_load_module(const struct module *mod,
  2197. const struct load_info *info)
  2198. {
  2199. }
  2200. #endif
  2201. #ifdef CONFIG_MODULE_SIG
  2202. static int module_sig_check(struct load_info *info)
  2203. {
  2204. int err = -ENOKEY;
  2205. const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
  2206. const void *mod = info->hdr;
  2207. if (info->len > markerlen &&
  2208. memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) == 0) {
  2209. /* We truncate the module to discard the signature */
  2210. info->len -= markerlen;
  2211. err = mod_verify_sig(mod, &info->len);
  2212. }
  2213. if (!err) {
  2214. info->sig_ok = true;
  2215. return 0;
  2216. }
  2217. /* Not having a signature is only an error if we're strict. */
  2218. if (err == -ENOKEY && !sig_enforce)
  2219. err = 0;
  2220. return err;
  2221. }
  2222. #else /* !CONFIG_MODULE_SIG */
  2223. static int module_sig_check(struct load_info *info)
  2224. {
  2225. return 0;
  2226. }
  2227. #endif /* !CONFIG_MODULE_SIG */
  2228. /* Sanity checks against invalid binaries, wrong arch, weird elf version. */
  2229. static int elf_header_check(struct load_info *info)
  2230. {
  2231. if (info->len < sizeof(*(info->hdr)))
  2232. return -ENOEXEC;
  2233. if (memcmp(info->hdr->e_ident, ELFMAG, SELFMAG) != 0
  2234. || info->hdr->e_type != ET_REL
  2235. || !elf_check_arch(info->hdr)
  2236. || info->hdr->e_shentsize != sizeof(Elf_Shdr))
  2237. return -ENOEXEC;
  2238. if (info->hdr->e_shoff >= info->len
  2239. || (info->hdr->e_shnum * sizeof(Elf_Shdr) >
  2240. info->len - info->hdr->e_shoff))
  2241. return -ENOEXEC;
  2242. return 0;
  2243. }
  2244. #define COPY_CHUNK_SIZE (16*PAGE_SIZE)
  2245. static int copy_chunked_from_user(void *dst, const void __user *usrc, unsigned long len)
  2246. {
  2247. do {
  2248. unsigned long n = min(len, COPY_CHUNK_SIZE);
  2249. if (copy_from_user(dst, usrc, n) != 0)
  2250. return -EFAULT;
  2251. cond_resched();
  2252. dst += n;
  2253. usrc += n;
  2254. len -= n;
  2255. } while (len);
  2256. return 0;
  2257. }
  2258. /* Sets info->hdr and info->len. */
  2259. static int copy_module_from_user(const void __user *umod, unsigned long len,
  2260. struct load_info *info)
  2261. {
  2262. int err;
  2263. info->len = len;
  2264. if (info->len < sizeof(*(info->hdr)))
  2265. return -ENOEXEC;
  2266. err = security_kernel_module_from_file(NULL);
  2267. if (err)
  2268. return err;
  2269. /* Suck in entire file: we'll want most of it. */
  2270. info->hdr = __vmalloc(info->len,
  2271. GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, PAGE_KERNEL);
  2272. if (!info->hdr)
  2273. return -ENOMEM;
  2274. if (copy_chunked_from_user(info->hdr, umod, info->len) != 0) {
  2275. vfree(info->hdr);
  2276. return -EFAULT;
  2277. }
  2278. return 0;
  2279. }
  2280. /* Sets info->hdr and info->len. */
  2281. static int copy_module_from_fd(int fd, struct load_info *info)
  2282. {
  2283. struct fd f = fdget(fd);
  2284. int err;
  2285. struct kstat stat;
  2286. loff_t pos;
  2287. ssize_t bytes = 0;
  2288. if (!f.file)
  2289. return -ENOEXEC;
  2290. err = security_kernel_module_from_file(f.file);
  2291. if (err)
  2292. goto out;
  2293. err = vfs_getattr(&f.file->f_path, &stat);
  2294. if (err)
  2295. goto out;
  2296. if (stat.size > INT_MAX) {
  2297. err = -EFBIG;
  2298. goto out;
  2299. }
  2300. /* Don't hand 0 to vmalloc, it whines. */
  2301. if (stat.size == 0) {
  2302. err = -EINVAL;
  2303. goto out;
  2304. }
  2305. info->hdr = vmalloc(stat.size);
  2306. if (!info->hdr) {
  2307. err = -ENOMEM;
  2308. goto out;
  2309. }
  2310. pos = 0;
  2311. while (pos < stat.size) {
  2312. bytes = kernel_read(f.file, pos, (char *)(info->hdr) + pos,
  2313. stat.size - pos);
  2314. if (bytes < 0) {
  2315. vfree(info->hdr);
  2316. err = bytes;
  2317. goto out;
  2318. }
  2319. if (bytes == 0)
  2320. break;
  2321. pos += bytes;
  2322. }
  2323. info->len = pos;
  2324. out:
  2325. fdput(f);
  2326. return err;
  2327. }
  2328. static void free_copy(struct load_info *info)
  2329. {
  2330. vfree(info->hdr);
  2331. }
  2332. static int rewrite_section_headers(struct load_info *info, int flags)
  2333. {
  2334. unsigned int i;
  2335. /* This should always be true, but let's be sure. */
  2336. info->sechdrs[0].sh_addr = 0;
  2337. for (i = 1; i < info->hdr->e_shnum; i++) {
  2338. Elf_Shdr *shdr = &info->sechdrs[i];
  2339. if (shdr->sh_type != SHT_NOBITS
  2340. && info->len < shdr->sh_offset + shdr->sh_size) {
  2341. pr_err("Module len %lu truncated\n", info->len);
  2342. return -ENOEXEC;
  2343. }
  2344. /* Mark all sections sh_addr with their address in the
  2345. temporary image. */
  2346. shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
  2347. #ifndef CONFIG_MODULE_UNLOAD
  2348. /* Don't load .exit sections */
  2349. if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
  2350. shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
  2351. #endif
  2352. }
  2353. /* Track but don't keep modinfo and version sections. */
  2354. if (flags & MODULE_INIT_IGNORE_MODVERSIONS)
  2355. info->index.vers = 0; /* Pretend no __versions section! */
  2356. else
  2357. info->index.vers = find_sec(info, "__versions");
  2358. info->index.info = find_sec(info, ".modinfo");
  2359. info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2360. info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2361. return 0;
  2362. }
  2363. /*
  2364. * Set up our basic convenience variables (pointers to section headers,
  2365. * search for module section index etc), and do some basic section
  2366. * verification.
  2367. *
  2368. * Return the temporary module pointer (we'll replace it with the final
  2369. * one when we move the module sections around).
  2370. */
  2371. static struct module *setup_load_info(struct load_info *info, int flags)
  2372. {
  2373. unsigned int i;
  2374. int err;
  2375. struct module *mod;
  2376. /* Set up the convenience variables */
  2377. info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
  2378. info->secstrings = (void *)info->hdr
  2379. + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
  2380. err = rewrite_section_headers(info, flags);
  2381. if (err)
  2382. return ERR_PTR(err);
  2383. /* Find internal symbols and strings. */
  2384. for (i = 1; i < info->hdr->e_shnum; i++) {
  2385. if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
  2386. info->index.sym = i;
  2387. info->index.str = info->sechdrs[i].sh_link;
  2388. info->strtab = (char *)info->hdr
  2389. + info->sechdrs[info->index.str].sh_offset;
  2390. break;
  2391. }
  2392. }
  2393. info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
  2394. if (!info->index.mod) {
  2395. pr_warn("No module found in object\n");
  2396. return ERR_PTR(-ENOEXEC);
  2397. }
  2398. /* This is temporary: point mod into copy of data. */
  2399. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2400. if (info->index.sym == 0) {
  2401. pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
  2402. return ERR_PTR(-ENOEXEC);
  2403. }
  2404. info->index.pcpu = find_pcpusec(info);
  2405. /* Check module struct version now, before we try to use module. */
  2406. if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
  2407. return ERR_PTR(-ENOEXEC);
  2408. return mod;
  2409. }
  2410. static int check_modinfo(struct module *mod, struct load_info *info, int flags)
  2411. {
  2412. const char *modmagic = get_modinfo(info, "vermagic");
  2413. int err;
  2414. if (flags & MODULE_INIT_IGNORE_VERMAGIC)
  2415. modmagic = NULL;
  2416. /* This is allowed: modprobe --force will invalidate it. */
  2417. if (!modmagic) {
  2418. err = try_to_force_load(mod, "bad vermagic");
  2419. if (err)
  2420. return err;
  2421. } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
  2422. pr_err("%s: version magic '%s' should be '%s'\n",
  2423. mod->name, modmagic, vermagic);
  2424. return -ENOEXEC;
  2425. }
  2426. if (!get_modinfo(info, "intree"))
  2427. add_taint_module(mod, TAINT_OOT_MODULE, LOCKDEP_STILL_OK);
  2428. if (get_modinfo(info, "staging")) {
  2429. add_taint_module(mod, TAINT_CRAP, LOCKDEP_STILL_OK);
  2430. pr_warn("%s: module is from the staging directory, the quality "
  2431. "is unknown, you have been warned.\n", mod->name);
  2432. }
  2433. /* Set up license info based on the info section */
  2434. set_license(mod, get_modinfo(info, "license"));
  2435. return 0;
  2436. }
  2437. static int find_module_sections(struct module *mod, struct load_info *info)
  2438. {
  2439. mod->kp = section_objs(info, "__param",
  2440. sizeof(*mod->kp), &mod->num_kp);
  2441. mod->syms = section_objs(info, "__ksymtab",
  2442. sizeof(*mod->syms), &mod->num_syms);
  2443. mod->crcs = section_addr(info, "__kcrctab");
  2444. mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
  2445. sizeof(*mod->gpl_syms),
  2446. &mod->num_gpl_syms);
  2447. mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
  2448. mod->gpl_future_syms = section_objs(info,
  2449. "__ksymtab_gpl_future",
  2450. sizeof(*mod->gpl_future_syms),
  2451. &mod->num_gpl_future_syms);
  2452. mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
  2453. #ifdef CONFIG_UNUSED_SYMBOLS
  2454. mod->unused_syms = section_objs(info, "__ksymtab_unused",
  2455. sizeof(*mod->unused_syms),
  2456. &mod->num_unused_syms);
  2457. mod->unused_crcs = section_addr(info, "__kcrctab_unused");
  2458. mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
  2459. sizeof(*mod->unused_gpl_syms),
  2460. &mod->num_unused_gpl_syms);
  2461. mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
  2462. #endif
  2463. #ifdef CONFIG_CONSTRUCTORS
  2464. mod->ctors = section_objs(info, ".ctors",
  2465. sizeof(*mod->ctors), &mod->num_ctors);
  2466. if (!mod->ctors)
  2467. mod->ctors = section_objs(info, ".init_array",
  2468. sizeof(*mod->ctors), &mod->num_ctors);
  2469. else if (find_sec(info, ".init_array")) {
  2470. /*
  2471. * This shouldn't happen with same compiler and binutils
  2472. * building all parts of the module.
  2473. */
  2474. pr_warn("%s: has both .ctors and .init_array.\n",
  2475. mod->name);
  2476. return -EINVAL;
  2477. }
  2478. #endif
  2479. #ifdef CONFIG_TRACEPOINTS
  2480. mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
  2481. sizeof(*mod->tracepoints_ptrs),
  2482. &mod->num_tracepoints);
  2483. #endif
  2484. #ifdef HAVE_JUMP_LABEL
  2485. mod->jump_entries = section_objs(info, "__jump_table",
  2486. sizeof(*mod->jump_entries),
  2487. &mod->num_jump_entries);
  2488. #endif
  2489. #ifdef CONFIG_EVENT_TRACING
  2490. mod->trace_events = section_objs(info, "_ftrace_events",
  2491. sizeof(*mod->trace_events),
  2492. &mod->num_trace_events);
  2493. mod->trace_enums = section_objs(info, "_ftrace_enum_map",
  2494. sizeof(*mod->trace_enums),
  2495. &mod->num_trace_enums);
  2496. #endif
  2497. #ifdef CONFIG_TRACING
  2498. mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
  2499. sizeof(*mod->trace_bprintk_fmt_start),
  2500. &mod->num_trace_bprintk_fmt);
  2501. #endif
  2502. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  2503. /* sechdrs[0].sh_size is always zero */
  2504. mod->ftrace_callsites = section_objs(info, "__mcount_loc",
  2505. sizeof(*mod->ftrace_callsites),
  2506. &mod->num_ftrace_callsites);
  2507. #endif
  2508. mod->extable = section_objs(info, "__ex_table",
  2509. sizeof(*mod->extable), &mod->num_exentries);
  2510. if (section_addr(info, "__obsparm"))
  2511. pr_warn("%s: Ignoring obsolete parameters\n", mod->name);
  2512. info->debug = section_objs(info, "__verbose",
  2513. sizeof(*info->debug), &info->num_debug);
  2514. return 0;
  2515. }
  2516. static int move_module(struct module *mod, struct load_info *info)
  2517. {
  2518. int i;
  2519. void *ptr;
  2520. /* Do the allocs. */
  2521. ptr = module_alloc(mod->core_size);
  2522. /*
  2523. * The pointer to this block is stored in the module structure
  2524. * which is inside the block. Just mark it as not being a
  2525. * leak.
  2526. */
  2527. kmemleak_not_leak(ptr);
  2528. if (!ptr)
  2529. return -ENOMEM;
  2530. memset(ptr, 0, mod->core_size);
  2531. mod->module_core = ptr;
  2532. if (mod->init_size) {
  2533. ptr = module_alloc(mod->init_size);
  2534. /*
  2535. * The pointer to this block is stored in the module structure
  2536. * which is inside the block. This block doesn't need to be
  2537. * scanned as it contains data and code that will be freed
  2538. * after the module is initialized.
  2539. */
  2540. kmemleak_ignore(ptr);
  2541. if (!ptr) {
  2542. module_memfree(mod->module_core);
  2543. return -ENOMEM;
  2544. }
  2545. memset(ptr, 0, mod->init_size);
  2546. mod->module_init = ptr;
  2547. } else
  2548. mod->module_init = NULL;
  2549. /* Transfer each section which specifies SHF_ALLOC */
  2550. pr_debug("final section addresses:\n");
  2551. for (i = 0; i < info->hdr->e_shnum; i++) {
  2552. void *dest;
  2553. Elf_Shdr *shdr = &info->sechdrs[i];
  2554. if (!(shdr->sh_flags & SHF_ALLOC))
  2555. continue;
  2556. if (shdr->sh_entsize & INIT_OFFSET_MASK)
  2557. dest = mod->module_init
  2558. + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
  2559. else
  2560. dest = mod->module_core + shdr->sh_entsize;
  2561. if (shdr->sh_type != SHT_NOBITS)
  2562. memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
  2563. /* Update sh_addr to point to copy in image. */
  2564. shdr->sh_addr = (unsigned long)dest;
  2565. pr_debug("\t0x%lx %s\n",
  2566. (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
  2567. }
  2568. return 0;
  2569. }
  2570. static int check_module_license_and_versions(struct module *mod)
  2571. {
  2572. /*
  2573. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  2574. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  2575. * using GPL-only symbols it needs.
  2576. */
  2577. if (strcmp(mod->name, "ndiswrapper") == 0)
  2578. add_taint(TAINT_PROPRIETARY_MODULE, LOCKDEP_NOW_UNRELIABLE);
  2579. /* driverloader was caught wrongly pretending to be under GPL */
  2580. if (strcmp(mod->name, "driverloader") == 0)
  2581. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2582. LOCKDEP_NOW_UNRELIABLE);
  2583. /* lve claims to be GPL but upstream won't provide source */
  2584. if (strcmp(mod->name, "lve") == 0)
  2585. add_taint_module(mod, TAINT_PROPRIETARY_MODULE,
  2586. LOCKDEP_NOW_UNRELIABLE);
  2587. #ifdef CONFIG_MODVERSIONS
  2588. if ((mod->num_syms && !mod->crcs)
  2589. || (mod->num_gpl_syms && !mod->gpl_crcs)
  2590. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  2591. #ifdef CONFIG_UNUSED_SYMBOLS
  2592. || (mod->num_unused_syms && !mod->unused_crcs)
  2593. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  2594. #endif
  2595. ) {
  2596. return try_to_force_load(mod,
  2597. "no versions for exported symbols");
  2598. }
  2599. #endif
  2600. return 0;
  2601. }
  2602. static void flush_module_icache(const struct module *mod)
  2603. {
  2604. mm_segment_t old_fs;
  2605. /* flush the icache in correct context */
  2606. old_fs = get_fs();
  2607. set_fs(KERNEL_DS);
  2608. /*
  2609. * Flush the instruction cache, since we've played with text.
  2610. * Do it before processing of module parameters, so the module
  2611. * can provide parameter accessor functions of its own.
  2612. */
  2613. if (mod->module_init)
  2614. flush_icache_range((unsigned long)mod->module_init,
  2615. (unsigned long)mod->module_init
  2616. + mod->init_size);
  2617. flush_icache_range((unsigned long)mod->module_core,
  2618. (unsigned long)mod->module_core + mod->core_size);
  2619. set_fs(old_fs);
  2620. }
  2621. int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
  2622. Elf_Shdr *sechdrs,
  2623. char *secstrings,
  2624. struct module *mod)
  2625. {
  2626. return 0;
  2627. }
  2628. static struct module *layout_and_allocate(struct load_info *info, int flags)
  2629. {
  2630. /* Module within temporary copy. */
  2631. struct module *mod;
  2632. int err;
  2633. mod = setup_load_info(info, flags);
  2634. if (IS_ERR(mod))
  2635. return mod;
  2636. err = check_modinfo(mod, info, flags);
  2637. if (err)
  2638. return ERR_PTR(err);
  2639. /* Allow arches to frob section contents and sizes. */
  2640. err = module_frob_arch_sections(info->hdr, info->sechdrs,
  2641. info->secstrings, mod);
  2642. if (err < 0)
  2643. return ERR_PTR(err);
  2644. /* We will do a special allocation for per-cpu sections later. */
  2645. info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC;
  2646. /* Determine total sizes, and put offsets in sh_entsize. For now
  2647. this is done generically; there doesn't appear to be any
  2648. special cases for the architectures. */
  2649. layout_sections(mod, info);
  2650. layout_symtab(mod, info);
  2651. /* Allocate and move to the final place */
  2652. err = move_module(mod, info);
  2653. if (err)
  2654. return ERR_PTR(err);
  2655. /* Module has been copied to its final place now: return it. */
  2656. mod = (void *)info->sechdrs[info->index.mod].sh_addr;
  2657. kmemleak_load_module(mod, info);
  2658. return mod;
  2659. }
  2660. /* mod is no longer valid after this! */
  2661. static void module_deallocate(struct module *mod, struct load_info *info)
  2662. {
  2663. percpu_modfree(mod);
  2664. module_arch_freeing_init(mod);
  2665. module_memfree(mod->module_init);
  2666. module_memfree(mod->module_core);
  2667. }
  2668. int __weak module_finalize(const Elf_Ehdr *hdr,
  2669. const Elf_Shdr *sechdrs,
  2670. struct module *me)
  2671. {
  2672. return 0;
  2673. }
  2674. static int post_relocation(struct module *mod, const struct load_info *info)
  2675. {
  2676. /* Sort exception table now relocations are done. */
  2677. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2678. /* Copy relocated percpu area over. */
  2679. percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
  2680. info->sechdrs[info->index.pcpu].sh_size);
  2681. /* Setup kallsyms-specific fields. */
  2682. add_kallsyms(mod, info);
  2683. /* Arch-specific module finalizing. */
  2684. return module_finalize(info->hdr, info->sechdrs, mod);
  2685. }
  2686. /* Is this module of this name done loading? No locks held. */
  2687. static bool finished_loading(const char *name)
  2688. {
  2689. struct module *mod;
  2690. bool ret;
  2691. /*
  2692. * The module_mutex should not be a heavily contended lock;
  2693. * if we get the occasional sleep here, we'll go an extra iteration
  2694. * in the wait_event_interruptible(), which is harmless.
  2695. */
  2696. sched_annotate_sleep();
  2697. mutex_lock(&module_mutex);
  2698. mod = find_module_all(name, strlen(name), true);
  2699. ret = !mod || mod->state == MODULE_STATE_LIVE
  2700. || mod->state == MODULE_STATE_GOING;
  2701. mutex_unlock(&module_mutex);
  2702. return ret;
  2703. }
  2704. /* Call module constructors. */
  2705. static void do_mod_ctors(struct module *mod)
  2706. {
  2707. #ifdef CONFIG_CONSTRUCTORS
  2708. unsigned long i;
  2709. for (i = 0; i < mod->num_ctors; i++)
  2710. mod->ctors[i]();
  2711. #endif
  2712. }
  2713. /* For freeing module_init on success, in case kallsyms traversing */
  2714. struct mod_initfree {
  2715. struct rcu_head rcu;
  2716. void *module_init;
  2717. };
  2718. static void do_free_init(struct rcu_head *head)
  2719. {
  2720. struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
  2721. module_memfree(m->module_init);
  2722. kfree(m);
  2723. }
  2724. /*
  2725. * This is where the real work happens.
  2726. *
  2727. * Keep it uninlined to provide a reliable breakpoint target, e.g. for the gdb
  2728. * helper command 'lx-symbols'.
  2729. */
  2730. static noinline int do_init_module(struct module *mod)
  2731. {
  2732. int ret = 0;
  2733. struct mod_initfree *freeinit;
  2734. freeinit = kmalloc(sizeof(*freeinit), GFP_KERNEL);
  2735. if (!freeinit) {
  2736. ret = -ENOMEM;
  2737. goto fail;
  2738. }
  2739. freeinit->module_init = mod->module_init;
  2740. /*
  2741. * We want to find out whether @mod uses async during init. Clear
  2742. * PF_USED_ASYNC. async_schedule*() will set it.
  2743. */
  2744. current->flags &= ~PF_USED_ASYNC;
  2745. do_mod_ctors(mod);
  2746. /* Start the module */
  2747. if (mod->init != NULL)
  2748. ret = do_one_initcall(mod->init);
  2749. if (ret < 0) {
  2750. goto fail_free_freeinit;
  2751. }
  2752. if (ret > 0) {
  2753. pr_warn("%s: '%s'->init suspiciously returned %d, it should "
  2754. "follow 0/-E convention\n"
  2755. "%s: loading module anyway...\n",
  2756. __func__, mod->name, ret, __func__);
  2757. dump_stack();
  2758. }
  2759. /* Now it's a first class citizen! */
  2760. mod->state = MODULE_STATE_LIVE;
  2761. blocking_notifier_call_chain(&module_notify_list,
  2762. MODULE_STATE_LIVE, mod);
  2763. /*
  2764. * We need to finish all async code before the module init sequence
  2765. * is done. This has potential to deadlock. For example, a newly
  2766. * detected block device can trigger request_module() of the
  2767. * default iosched from async probing task. Once userland helper
  2768. * reaches here, async_synchronize_full() will wait on the async
  2769. * task waiting on request_module() and deadlock.
  2770. *
  2771. * This deadlock is avoided by perfomring async_synchronize_full()
  2772. * iff module init queued any async jobs. This isn't a full
  2773. * solution as it will deadlock the same if module loading from
  2774. * async jobs nests more than once; however, due to the various
  2775. * constraints, this hack seems to be the best option for now.
  2776. * Please refer to the following thread for details.
  2777. *
  2778. * http://thread.gmane.org/gmane.linux.kernel/1420814
  2779. */
  2780. if (current->flags & PF_USED_ASYNC)
  2781. async_synchronize_full();
  2782. mutex_lock(&module_mutex);
  2783. /* Drop initial reference. */
  2784. module_put(mod);
  2785. trim_init_extable(mod);
  2786. #ifdef CONFIG_KALLSYMS
  2787. mod->num_symtab = mod->core_num_syms;
  2788. mod->symtab = mod->core_symtab;
  2789. mod->strtab = mod->core_strtab;
  2790. #endif
  2791. mod_tree_remove_init(mod);
  2792. unset_module_init_ro_nx(mod);
  2793. module_arch_freeing_init(mod);
  2794. mod->module_init = NULL;
  2795. mod->init_size = 0;
  2796. mod->init_ro_size = 0;
  2797. mod->init_text_size = 0;
  2798. /*
  2799. * We want to free module_init, but be aware that kallsyms may be
  2800. * walking this with preempt disabled. In all the failure paths, we
  2801. * call synchronize_sched(), but we don't want to slow down the success
  2802. * path, so use actual RCU here.
  2803. */
  2804. call_rcu_sched(&freeinit->rcu, do_free_init);
  2805. mutex_unlock(&module_mutex);
  2806. wake_up_all(&module_wq);
  2807. return 0;
  2808. fail_free_freeinit:
  2809. kfree(freeinit);
  2810. fail:
  2811. /* Try to protect us from buggy refcounters. */
  2812. mod->state = MODULE_STATE_GOING;
  2813. synchronize_sched();
  2814. module_put(mod);
  2815. blocking_notifier_call_chain(&module_notify_list,
  2816. MODULE_STATE_GOING, mod);
  2817. free_module(mod);
  2818. wake_up_all(&module_wq);
  2819. return ret;
  2820. }
  2821. static int may_init_module(void)
  2822. {
  2823. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2824. return -EPERM;
  2825. return 0;
  2826. }
  2827. /*
  2828. * We try to place it in the list now to make sure it's unique before
  2829. * we dedicate too many resources. In particular, temporary percpu
  2830. * memory exhaustion.
  2831. */
  2832. static int add_unformed_module(struct module *mod)
  2833. {
  2834. int err;
  2835. struct module *old;
  2836. mod->state = MODULE_STATE_UNFORMED;
  2837. again:
  2838. mutex_lock(&module_mutex);
  2839. old = find_module_all(mod->name, strlen(mod->name), true);
  2840. if (old != NULL) {
  2841. if (old->state == MODULE_STATE_COMING
  2842. || old->state == MODULE_STATE_UNFORMED) {
  2843. /* Wait in case it fails to load. */
  2844. mutex_unlock(&module_mutex);
  2845. err = wait_event_interruptible(module_wq,
  2846. finished_loading(mod->name));
  2847. if (err)
  2848. goto out_unlocked;
  2849. goto again;
  2850. }
  2851. err = -EEXIST;
  2852. goto out;
  2853. }
  2854. mod_update_bounds(mod);
  2855. list_add_rcu(&mod->list, &modules);
  2856. mod_tree_insert(mod);
  2857. err = 0;
  2858. out:
  2859. mutex_unlock(&module_mutex);
  2860. out_unlocked:
  2861. return err;
  2862. }
  2863. static int complete_formation(struct module *mod, struct load_info *info)
  2864. {
  2865. int err;
  2866. mutex_lock(&module_mutex);
  2867. /* Find duplicate symbols (must be called under lock). */
  2868. err = verify_export_symbols(mod);
  2869. if (err < 0)
  2870. goto out;
  2871. /* This relies on module_mutex for list integrity. */
  2872. module_bug_finalize(info->hdr, info->sechdrs, mod);
  2873. /* Set RO and NX regions for core */
  2874. set_section_ro_nx(mod->module_core,
  2875. mod->core_text_size,
  2876. mod->core_ro_size,
  2877. mod->core_size);
  2878. /* Set RO and NX regions for init */
  2879. set_section_ro_nx(mod->module_init,
  2880. mod->init_text_size,
  2881. mod->init_ro_size,
  2882. mod->init_size);
  2883. /* Mark state as coming so strong_try_module_get() ignores us,
  2884. * but kallsyms etc. can see us. */
  2885. mod->state = MODULE_STATE_COMING;
  2886. mutex_unlock(&module_mutex);
  2887. blocking_notifier_call_chain(&module_notify_list,
  2888. MODULE_STATE_COMING, mod);
  2889. return 0;
  2890. out:
  2891. mutex_unlock(&module_mutex);
  2892. return err;
  2893. }
  2894. static int unknown_module_param_cb(char *param, char *val, const char *modname)
  2895. {
  2896. /* Check for magic 'dyndbg' arg */
  2897. int ret = ddebug_dyndbg_module_param_cb(param, val, modname);
  2898. if (ret != 0)
  2899. pr_warn("%s: unknown parameter '%s' ignored\n", modname, param);
  2900. return 0;
  2901. }
  2902. /* Allocate and load the module: note that size of section 0 is always
  2903. zero, and we rely on this for optional sections. */
  2904. static int load_module(struct load_info *info, const char __user *uargs,
  2905. int flags)
  2906. {
  2907. struct module *mod;
  2908. long err;
  2909. char *after_dashes;
  2910. err = module_sig_check(info);
  2911. if (err)
  2912. goto free_copy;
  2913. err = elf_header_check(info);
  2914. if (err)
  2915. goto free_copy;
  2916. /* Figure out module layout, and allocate all the memory. */
  2917. mod = layout_and_allocate(info, flags);
  2918. if (IS_ERR(mod)) {
  2919. err = PTR_ERR(mod);
  2920. goto free_copy;
  2921. }
  2922. /* Reserve our place in the list. */
  2923. err = add_unformed_module(mod);
  2924. if (err)
  2925. goto free_module;
  2926. #ifdef CONFIG_MODULE_SIG
  2927. mod->sig_ok = info->sig_ok;
  2928. if (!mod->sig_ok) {
  2929. pr_notice_once("%s: module verification failed: signature "
  2930. "and/or required key missing - tainting "
  2931. "kernel\n", mod->name);
  2932. add_taint_module(mod, TAINT_UNSIGNED_MODULE, LOCKDEP_STILL_OK);
  2933. }
  2934. #endif
  2935. /* To avoid stressing percpu allocator, do this once we're unique. */
  2936. err = percpu_modalloc(mod, info);
  2937. if (err)
  2938. goto unlink_mod;
  2939. /* Now module is in final location, initialize linked lists, etc. */
  2940. err = module_unload_init(mod);
  2941. if (err)
  2942. goto unlink_mod;
  2943. mutex_init(&mod->param_lock);
  2944. /* Now we've got everything in the final locations, we can
  2945. * find optional sections. */
  2946. err = find_module_sections(mod, info);
  2947. if (err)
  2948. goto free_unload;
  2949. err = check_module_license_and_versions(mod);
  2950. if (err)
  2951. goto free_unload;
  2952. /* Set up MODINFO_ATTR fields */
  2953. setup_modinfo(mod, info);
  2954. /* Fix up syms, so that st_value is a pointer to location. */
  2955. err = simplify_symbols(mod, info);
  2956. if (err < 0)
  2957. goto free_modinfo;
  2958. err = apply_relocations(mod, info);
  2959. if (err < 0)
  2960. goto free_modinfo;
  2961. err = post_relocation(mod, info);
  2962. if (err < 0)
  2963. goto free_modinfo;
  2964. flush_module_icache(mod);
  2965. /* Now copy in args */
  2966. mod->args = strndup_user(uargs, ~0UL >> 1);
  2967. if (IS_ERR(mod->args)) {
  2968. err = PTR_ERR(mod->args);
  2969. goto free_arch_cleanup;
  2970. }
  2971. dynamic_debug_setup(info->debug, info->num_debug);
  2972. /* Ftrace init must be called in the MODULE_STATE_UNFORMED state */
  2973. ftrace_module_init(mod);
  2974. /* Finally it's fully formed, ready to start executing. */
  2975. err = complete_formation(mod, info);
  2976. if (err)
  2977. goto ddebug_cleanup;
  2978. /* Module is ready to execute: parsing args may do that. */
  2979. after_dashes = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
  2980. -32768, 32767, unknown_module_param_cb);
  2981. if (IS_ERR(after_dashes)) {
  2982. err = PTR_ERR(after_dashes);
  2983. goto bug_cleanup;
  2984. } else if (after_dashes) {
  2985. pr_warn("%s: parameters '%s' after `--' ignored\n",
  2986. mod->name, after_dashes);
  2987. }
  2988. /* Link in to syfs. */
  2989. err = mod_sysfs_setup(mod, info, mod->kp, mod->num_kp);
  2990. if (err < 0)
  2991. goto bug_cleanup;
  2992. /* Get rid of temporary copy. */
  2993. free_copy(info);
  2994. /* Done! */
  2995. trace_module_load(mod);
  2996. return do_init_module(mod);
  2997. bug_cleanup:
  2998. /* module_bug_cleanup needs module_mutex protection */
  2999. mutex_lock(&module_mutex);
  3000. module_bug_cleanup(mod);
  3001. mutex_unlock(&module_mutex);
  3002. /* we can't deallocate the module until we clear memory protection */
  3003. unset_module_init_ro_nx(mod);
  3004. unset_module_core_ro_nx(mod);
  3005. ddebug_cleanup:
  3006. dynamic_debug_remove(info->debug);
  3007. synchronize_sched();
  3008. kfree(mod->args);
  3009. free_arch_cleanup:
  3010. module_arch_cleanup(mod);
  3011. free_modinfo:
  3012. free_modinfo(mod);
  3013. free_unload:
  3014. module_unload_free(mod);
  3015. unlink_mod:
  3016. mutex_lock(&module_mutex);
  3017. /* Unlink carefully: kallsyms could be walking list. */
  3018. list_del_rcu(&mod->list);
  3019. wake_up_all(&module_wq);
  3020. /* Wait for RCU-sched synchronizing before releasing mod->list. */
  3021. synchronize_sched();
  3022. mutex_unlock(&module_mutex);
  3023. free_module:
  3024. /* Free lock-classes; relies on the preceding sync_rcu() */
  3025. lockdep_free_key_range(mod->module_core, mod->core_size);
  3026. module_deallocate(mod, info);
  3027. free_copy:
  3028. free_copy(info);
  3029. return err;
  3030. }
  3031. SYSCALL_DEFINE3(init_module, void __user *, umod,
  3032. unsigned long, len, const char __user *, uargs)
  3033. {
  3034. int err;
  3035. struct load_info info = { };
  3036. err = may_init_module();
  3037. if (err)
  3038. return err;
  3039. pr_debug("init_module: umod=%p, len=%lu, uargs=%p\n",
  3040. umod, len, uargs);
  3041. err = copy_module_from_user(umod, len, &info);
  3042. if (err)
  3043. return err;
  3044. return load_module(&info, uargs, 0);
  3045. }
  3046. SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
  3047. {
  3048. int err;
  3049. struct load_info info = { };
  3050. err = may_init_module();
  3051. if (err)
  3052. return err;
  3053. pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
  3054. if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
  3055. |MODULE_INIT_IGNORE_VERMAGIC))
  3056. return -EINVAL;
  3057. err = copy_module_from_fd(fd, &info);
  3058. if (err)
  3059. return err;
  3060. return load_module(&info, uargs, flags);
  3061. }
  3062. static inline int within(unsigned long addr, void *start, unsigned long size)
  3063. {
  3064. return ((void *)addr >= start && (void *)addr < start + size);
  3065. }
  3066. #ifdef CONFIG_KALLSYMS
  3067. /*
  3068. * This ignores the intensely annoying "mapping symbols" found
  3069. * in ARM ELF files: $a, $t and $d.
  3070. */
  3071. static inline int is_arm_mapping_symbol(const char *str)
  3072. {
  3073. if (str[0] == '.' && str[1] == 'L')
  3074. return true;
  3075. return str[0] == '$' && strchr("axtd", str[1])
  3076. && (str[2] == '\0' || str[2] == '.');
  3077. }
  3078. static const char *get_ksymbol(struct module *mod,
  3079. unsigned long addr,
  3080. unsigned long *size,
  3081. unsigned long *offset)
  3082. {
  3083. unsigned int i, best = 0;
  3084. unsigned long nextval;
  3085. /* At worse, next value is at end of module */
  3086. if (within_module_init(addr, mod))
  3087. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  3088. else
  3089. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  3090. /* Scan for closest preceding symbol, and next symbol. (ELF
  3091. starts real symbols at 1). */
  3092. for (i = 1; i < mod->num_symtab; i++) {
  3093. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  3094. continue;
  3095. /* We ignore unnamed symbols: they're uninformative
  3096. * and inserted at a whim. */
  3097. if (mod->symtab[i].st_value <= addr
  3098. && mod->symtab[i].st_value > mod->symtab[best].st_value
  3099. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  3100. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  3101. best = i;
  3102. if (mod->symtab[i].st_value > addr
  3103. && mod->symtab[i].st_value < nextval
  3104. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  3105. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  3106. nextval = mod->symtab[i].st_value;
  3107. }
  3108. if (!best)
  3109. return NULL;
  3110. if (size)
  3111. *size = nextval - mod->symtab[best].st_value;
  3112. if (offset)
  3113. *offset = addr - mod->symtab[best].st_value;
  3114. return mod->strtab + mod->symtab[best].st_name;
  3115. }
  3116. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  3117. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  3118. const char *module_address_lookup(unsigned long addr,
  3119. unsigned long *size,
  3120. unsigned long *offset,
  3121. char **modname,
  3122. char *namebuf)
  3123. {
  3124. const char *ret = NULL;
  3125. struct module *mod;
  3126. preempt_disable();
  3127. mod = __module_address(addr);
  3128. if (mod) {
  3129. if (modname)
  3130. *modname = mod->name;
  3131. ret = get_ksymbol(mod, addr, size, offset);
  3132. }
  3133. /* Make a copy in here where it's safe */
  3134. if (ret) {
  3135. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  3136. ret = namebuf;
  3137. }
  3138. preempt_enable();
  3139. return ret;
  3140. }
  3141. int lookup_module_symbol_name(unsigned long addr, char *symname)
  3142. {
  3143. struct module *mod;
  3144. preempt_disable();
  3145. list_for_each_entry_rcu(mod, &modules, list) {
  3146. if (mod->state == MODULE_STATE_UNFORMED)
  3147. continue;
  3148. if (within_module(addr, mod)) {
  3149. const char *sym;
  3150. sym = get_ksymbol(mod, addr, NULL, NULL);
  3151. if (!sym)
  3152. goto out;
  3153. strlcpy(symname, sym, KSYM_NAME_LEN);
  3154. preempt_enable();
  3155. return 0;
  3156. }
  3157. }
  3158. out:
  3159. preempt_enable();
  3160. return -ERANGE;
  3161. }
  3162. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  3163. unsigned long *offset, char *modname, char *name)
  3164. {
  3165. struct module *mod;
  3166. preempt_disable();
  3167. list_for_each_entry_rcu(mod, &modules, list) {
  3168. if (mod->state == MODULE_STATE_UNFORMED)
  3169. continue;
  3170. if (within_module(addr, mod)) {
  3171. const char *sym;
  3172. sym = get_ksymbol(mod, addr, size, offset);
  3173. if (!sym)
  3174. goto out;
  3175. if (modname)
  3176. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  3177. if (name)
  3178. strlcpy(name, sym, KSYM_NAME_LEN);
  3179. preempt_enable();
  3180. return 0;
  3181. }
  3182. }
  3183. out:
  3184. preempt_enable();
  3185. return -ERANGE;
  3186. }
  3187. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  3188. char *name, char *module_name, int *exported)
  3189. {
  3190. struct module *mod;
  3191. preempt_disable();
  3192. list_for_each_entry_rcu(mod, &modules, list) {
  3193. if (mod->state == MODULE_STATE_UNFORMED)
  3194. continue;
  3195. if (symnum < mod->num_symtab) {
  3196. *value = mod->symtab[symnum].st_value;
  3197. *type = mod->symtab[symnum].st_info;
  3198. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  3199. KSYM_NAME_LEN);
  3200. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  3201. *exported = is_exported(name, *value, mod);
  3202. preempt_enable();
  3203. return 0;
  3204. }
  3205. symnum -= mod->num_symtab;
  3206. }
  3207. preempt_enable();
  3208. return -ERANGE;
  3209. }
  3210. static unsigned long mod_find_symname(struct module *mod, const char *name)
  3211. {
  3212. unsigned int i;
  3213. for (i = 0; i < mod->num_symtab; i++)
  3214. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  3215. mod->symtab[i].st_info != 'U')
  3216. return mod->symtab[i].st_value;
  3217. return 0;
  3218. }
  3219. /* Look for this name: can be of form module:name. */
  3220. unsigned long module_kallsyms_lookup_name(const char *name)
  3221. {
  3222. struct module *mod;
  3223. char *colon;
  3224. unsigned long ret = 0;
  3225. /* Don't lock: we're in enough trouble already. */
  3226. preempt_disable();
  3227. if ((colon = strchr(name, ':')) != NULL) {
  3228. if ((mod = find_module_all(name, colon - name, false)) != NULL)
  3229. ret = mod_find_symname(mod, colon+1);
  3230. } else {
  3231. list_for_each_entry_rcu(mod, &modules, list) {
  3232. if (mod->state == MODULE_STATE_UNFORMED)
  3233. continue;
  3234. if ((ret = mod_find_symname(mod, name)) != 0)
  3235. break;
  3236. }
  3237. }
  3238. preempt_enable();
  3239. return ret;
  3240. }
  3241. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  3242. struct module *, unsigned long),
  3243. void *data)
  3244. {
  3245. struct module *mod;
  3246. unsigned int i;
  3247. int ret;
  3248. module_assert_mutex();
  3249. list_for_each_entry(mod, &modules, list) {
  3250. if (mod->state == MODULE_STATE_UNFORMED)
  3251. continue;
  3252. for (i = 0; i < mod->num_symtab; i++) {
  3253. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  3254. mod, mod->symtab[i].st_value);
  3255. if (ret != 0)
  3256. return ret;
  3257. }
  3258. }
  3259. return 0;
  3260. }
  3261. #endif /* CONFIG_KALLSYMS */
  3262. static char *module_flags(struct module *mod, char *buf)
  3263. {
  3264. int bx = 0;
  3265. BUG_ON(mod->state == MODULE_STATE_UNFORMED);
  3266. if (mod->taints ||
  3267. mod->state == MODULE_STATE_GOING ||
  3268. mod->state == MODULE_STATE_COMING) {
  3269. buf[bx++] = '(';
  3270. bx += module_flags_taint(mod, buf + bx);
  3271. /* Show a - for module-is-being-unloaded */
  3272. if (mod->state == MODULE_STATE_GOING)
  3273. buf[bx++] = '-';
  3274. /* Show a + for module-is-being-loaded */
  3275. if (mod->state == MODULE_STATE_COMING)
  3276. buf[bx++] = '+';
  3277. buf[bx++] = ')';
  3278. }
  3279. buf[bx] = '\0';
  3280. return buf;
  3281. }
  3282. #ifdef CONFIG_PROC_FS
  3283. /* Called by the /proc file system to return a list of modules. */
  3284. static void *m_start(struct seq_file *m, loff_t *pos)
  3285. {
  3286. mutex_lock(&module_mutex);
  3287. return seq_list_start(&modules, *pos);
  3288. }
  3289. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  3290. {
  3291. return seq_list_next(p, &modules, pos);
  3292. }
  3293. static void m_stop(struct seq_file *m, void *p)
  3294. {
  3295. mutex_unlock(&module_mutex);
  3296. }
  3297. static int m_show(struct seq_file *m, void *p)
  3298. {
  3299. struct module *mod = list_entry(p, struct module, list);
  3300. char buf[8];
  3301. /* We always ignore unformed modules. */
  3302. if (mod->state == MODULE_STATE_UNFORMED)
  3303. return 0;
  3304. seq_printf(m, "%s %u",
  3305. mod->name, mod->init_size + mod->core_size);
  3306. print_unload_info(m, mod);
  3307. /* Informative for users. */
  3308. seq_printf(m, " %s",
  3309. mod->state == MODULE_STATE_GOING ? "Unloading" :
  3310. mod->state == MODULE_STATE_COMING ? "Loading" :
  3311. "Live");
  3312. /* Used by oprofile and other similar tools. */
  3313. seq_printf(m, " 0x%pK", mod->module_core);
  3314. /* Taints info */
  3315. if (mod->taints)
  3316. seq_printf(m, " %s", module_flags(mod, buf));
  3317. seq_puts(m, "\n");
  3318. return 0;
  3319. }
  3320. /* Format: modulename size refcount deps address
  3321. Where refcount is a number or -, and deps is a comma-separated list
  3322. of depends or -.
  3323. */
  3324. static const struct seq_operations modules_op = {
  3325. .start = m_start,
  3326. .next = m_next,
  3327. .stop = m_stop,
  3328. .show = m_show
  3329. };
  3330. static int modules_open(struct inode *inode, struct file *file)
  3331. {
  3332. return seq_open(file, &modules_op);
  3333. }
  3334. static const struct file_operations proc_modules_operations = {
  3335. .open = modules_open,
  3336. .read = seq_read,
  3337. .llseek = seq_lseek,
  3338. .release = seq_release,
  3339. };
  3340. static int __init proc_modules_init(void)
  3341. {
  3342. proc_create("modules", 0, NULL, &proc_modules_operations);
  3343. return 0;
  3344. }
  3345. module_init(proc_modules_init);
  3346. #endif
  3347. /* Given an address, look for it in the module exception tables. */
  3348. const struct exception_table_entry *search_module_extables(unsigned long addr)
  3349. {
  3350. const struct exception_table_entry *e = NULL;
  3351. struct module *mod;
  3352. preempt_disable();
  3353. list_for_each_entry_rcu(mod, &modules, list) {
  3354. if (mod->state == MODULE_STATE_UNFORMED)
  3355. continue;
  3356. if (mod->num_exentries == 0)
  3357. continue;
  3358. e = search_extable(mod->extable,
  3359. mod->extable + mod->num_exentries - 1,
  3360. addr);
  3361. if (e)
  3362. break;
  3363. }
  3364. preempt_enable();
  3365. /* Now, if we found one, we are running inside it now, hence
  3366. we cannot unload the module, hence no refcnt needed. */
  3367. return e;
  3368. }
  3369. /*
  3370. * is_module_address - is this address inside a module?
  3371. * @addr: the address to check.
  3372. *
  3373. * See is_module_text_address() if you simply want to see if the address
  3374. * is code (not data).
  3375. */
  3376. bool is_module_address(unsigned long addr)
  3377. {
  3378. bool ret;
  3379. preempt_disable();
  3380. ret = __module_address(addr) != NULL;
  3381. preempt_enable();
  3382. return ret;
  3383. }
  3384. /*
  3385. * __module_address - get the module which contains an address.
  3386. * @addr: the address.
  3387. *
  3388. * Must be called with preempt disabled or module mutex held so that
  3389. * module doesn't get freed during this.
  3390. */
  3391. struct module *__module_address(unsigned long addr)
  3392. {
  3393. struct module *mod;
  3394. if (addr < module_addr_min || addr > module_addr_max)
  3395. return NULL;
  3396. module_assert_mutex_or_preempt();
  3397. mod = mod_find(addr);
  3398. if (mod) {
  3399. BUG_ON(!within_module(addr, mod));
  3400. if (mod->state == MODULE_STATE_UNFORMED)
  3401. mod = NULL;
  3402. }
  3403. return mod;
  3404. }
  3405. EXPORT_SYMBOL_GPL(__module_address);
  3406. /*
  3407. * is_module_text_address - is this address inside module code?
  3408. * @addr: the address to check.
  3409. *
  3410. * See is_module_address() if you simply want to see if the address is
  3411. * anywhere in a module. See kernel_text_address() for testing if an
  3412. * address corresponds to kernel or module code.
  3413. */
  3414. bool is_module_text_address(unsigned long addr)
  3415. {
  3416. bool ret;
  3417. preempt_disable();
  3418. ret = __module_text_address(addr) != NULL;
  3419. preempt_enable();
  3420. return ret;
  3421. }
  3422. /*
  3423. * __module_text_address - get the module whose code contains an address.
  3424. * @addr: the address.
  3425. *
  3426. * Must be called with preempt disabled or module mutex held so that
  3427. * module doesn't get freed during this.
  3428. */
  3429. struct module *__module_text_address(unsigned long addr)
  3430. {
  3431. struct module *mod = __module_address(addr);
  3432. if (mod) {
  3433. /* Make sure it's within the text section. */
  3434. if (!within(addr, mod->module_init, mod->init_text_size)
  3435. && !within(addr, mod->module_core, mod->core_text_size))
  3436. mod = NULL;
  3437. }
  3438. return mod;
  3439. }
  3440. EXPORT_SYMBOL_GPL(__module_text_address);
  3441. /* Don't grab lock, we're oopsing. */
  3442. void print_modules(void)
  3443. {
  3444. struct module *mod;
  3445. char buf[8];
  3446. printk(KERN_DEFAULT "Modules linked in:");
  3447. /* Most callers should already have preempt disabled, but make sure */
  3448. preempt_disable();
  3449. list_for_each_entry_rcu(mod, &modules, list) {
  3450. if (mod->state == MODULE_STATE_UNFORMED)
  3451. continue;
  3452. pr_cont(" %s%s", mod->name, module_flags(mod, buf));
  3453. }
  3454. preempt_enable();
  3455. if (last_unloaded_module[0])
  3456. pr_cont(" [last unloaded: %s]", last_unloaded_module);
  3457. pr_cont("\n");
  3458. }
  3459. #ifdef CONFIG_MODVERSIONS
  3460. /* Generate the signature for all relevant module structures here.
  3461. * If these change, we don't want to try to parse the module. */
  3462. void module_layout(struct module *mod,
  3463. struct modversion_info *ver,
  3464. struct kernel_param *kp,
  3465. struct kernel_symbol *ks,
  3466. struct tracepoint * const *tp)
  3467. {
  3468. }
  3469. EXPORT_SYMBOL(module_layout);
  3470. #endif