module.c 88 KB

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