module.c 96 KB

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