module.c 77 KB

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