module.c 88 KB

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