module.c 96 KB

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