module.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023
  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. static inline bool sect_empty(const Elf_Shdr *sect)
  876. {
  877. return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
  878. }
  879. struct module_sect_attr
  880. {
  881. struct module_attribute mattr;
  882. char *name;
  883. unsigned long address;
  884. };
  885. struct module_sect_attrs
  886. {
  887. struct attribute_group grp;
  888. unsigned int nsections;
  889. struct module_sect_attr attrs[0];
  890. };
  891. static ssize_t module_sect_show(struct module_attribute *mattr,
  892. struct module *mod, char *buf)
  893. {
  894. struct module_sect_attr *sattr =
  895. container_of(mattr, struct module_sect_attr, mattr);
  896. return sprintf(buf, "0x%lx\n", sattr->address);
  897. }
  898. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  899. {
  900. unsigned int section;
  901. for (section = 0; section < sect_attrs->nsections; section++)
  902. kfree(sect_attrs->attrs[section].name);
  903. kfree(sect_attrs);
  904. }
  905. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  906. char *secstrings, Elf_Shdr *sechdrs)
  907. {
  908. unsigned int nloaded = 0, i, size[2];
  909. struct module_sect_attrs *sect_attrs;
  910. struct module_sect_attr *sattr;
  911. struct attribute **gattr;
  912. /* Count loaded sections and allocate structures */
  913. for (i = 0; i < nsect; i++)
  914. if (!sect_empty(&sechdrs[i]))
  915. nloaded++;
  916. size[0] = ALIGN(sizeof(*sect_attrs)
  917. + nloaded * sizeof(sect_attrs->attrs[0]),
  918. sizeof(sect_attrs->grp.attrs[0]));
  919. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  920. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  921. if (sect_attrs == NULL)
  922. return;
  923. /* Setup section attributes. */
  924. sect_attrs->grp.name = "sections";
  925. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  926. sect_attrs->nsections = 0;
  927. sattr = &sect_attrs->attrs[0];
  928. gattr = &sect_attrs->grp.attrs[0];
  929. for (i = 0; i < nsect; i++) {
  930. if (sect_empty(&sechdrs[i]))
  931. continue;
  932. sattr->address = sechdrs[i].sh_addr;
  933. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  934. GFP_KERNEL);
  935. if (sattr->name == NULL)
  936. goto out;
  937. sect_attrs->nsections++;
  938. sysfs_attr_init(&sattr->mattr.attr);
  939. sattr->mattr.show = module_sect_show;
  940. sattr->mattr.store = NULL;
  941. sattr->mattr.attr.name = sattr->name;
  942. sattr->mattr.attr.mode = S_IRUGO;
  943. *(gattr++) = &(sattr++)->mattr.attr;
  944. }
  945. *gattr = NULL;
  946. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  947. goto out;
  948. mod->sect_attrs = sect_attrs;
  949. return;
  950. out:
  951. free_sect_attrs(sect_attrs);
  952. }
  953. static void remove_sect_attrs(struct module *mod)
  954. {
  955. if (mod->sect_attrs) {
  956. sysfs_remove_group(&mod->mkobj.kobj,
  957. &mod->sect_attrs->grp);
  958. /* We are positive that no one is using any sect attrs
  959. * at this point. Deallocate immediately. */
  960. free_sect_attrs(mod->sect_attrs);
  961. mod->sect_attrs = NULL;
  962. }
  963. }
  964. /*
  965. * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
  966. */
  967. struct module_notes_attrs {
  968. struct kobject *dir;
  969. unsigned int notes;
  970. struct bin_attribute attrs[0];
  971. };
  972. static ssize_t module_notes_read(struct kobject *kobj,
  973. struct bin_attribute *bin_attr,
  974. char *buf, loff_t pos, size_t count)
  975. {
  976. /*
  977. * The caller checked the pos and count against our size.
  978. */
  979. memcpy(buf, bin_attr->private + pos, count);
  980. return count;
  981. }
  982. static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
  983. unsigned int i)
  984. {
  985. if (notes_attrs->dir) {
  986. while (i-- > 0)
  987. sysfs_remove_bin_file(notes_attrs->dir,
  988. &notes_attrs->attrs[i]);
  989. kobject_put(notes_attrs->dir);
  990. }
  991. kfree(notes_attrs);
  992. }
  993. static void add_notes_attrs(struct module *mod, unsigned int nsect,
  994. char *secstrings, Elf_Shdr *sechdrs)
  995. {
  996. unsigned int notes, loaded, i;
  997. struct module_notes_attrs *notes_attrs;
  998. struct bin_attribute *nattr;
  999. /* failed to create section attributes, so can't create notes */
  1000. if (!mod->sect_attrs)
  1001. return;
  1002. /* Count notes sections and allocate structures. */
  1003. notes = 0;
  1004. for (i = 0; i < nsect; i++)
  1005. if (!sect_empty(&sechdrs[i]) &&
  1006. (sechdrs[i].sh_type == SHT_NOTE))
  1007. ++notes;
  1008. if (notes == 0)
  1009. return;
  1010. notes_attrs = kzalloc(sizeof(*notes_attrs)
  1011. + notes * sizeof(notes_attrs->attrs[0]),
  1012. GFP_KERNEL);
  1013. if (notes_attrs == NULL)
  1014. return;
  1015. notes_attrs->notes = notes;
  1016. nattr = &notes_attrs->attrs[0];
  1017. for (loaded = i = 0; i < nsect; ++i) {
  1018. if (sect_empty(&sechdrs[i]))
  1019. continue;
  1020. if (sechdrs[i].sh_type == SHT_NOTE) {
  1021. sysfs_bin_attr_init(nattr);
  1022. nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
  1023. nattr->attr.mode = S_IRUGO;
  1024. nattr->size = sechdrs[i].sh_size;
  1025. nattr->private = (void *) sechdrs[i].sh_addr;
  1026. nattr->read = module_notes_read;
  1027. ++nattr;
  1028. }
  1029. ++loaded;
  1030. }
  1031. notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
  1032. if (!notes_attrs->dir)
  1033. goto out;
  1034. for (i = 0; i < notes; ++i)
  1035. if (sysfs_create_bin_file(notes_attrs->dir,
  1036. &notes_attrs->attrs[i]))
  1037. goto out;
  1038. mod->notes_attrs = notes_attrs;
  1039. return;
  1040. out:
  1041. free_notes_attrs(notes_attrs, i);
  1042. }
  1043. static void remove_notes_attrs(struct module *mod)
  1044. {
  1045. if (mod->notes_attrs)
  1046. free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
  1047. }
  1048. #else
  1049. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  1050. char *sectstrings, Elf_Shdr *sechdrs)
  1051. {
  1052. }
  1053. static inline void remove_sect_attrs(struct module *mod)
  1054. {
  1055. }
  1056. static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
  1057. char *sectstrings, Elf_Shdr *sechdrs)
  1058. {
  1059. }
  1060. static inline void remove_notes_attrs(struct module *mod)
  1061. {
  1062. }
  1063. #endif
  1064. #ifdef CONFIG_SYSFS
  1065. int module_add_modinfo_attrs(struct module *mod)
  1066. {
  1067. struct module_attribute *attr;
  1068. struct module_attribute *temp_attr;
  1069. int error = 0;
  1070. int i;
  1071. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  1072. (ARRAY_SIZE(modinfo_attrs) + 1)),
  1073. GFP_KERNEL);
  1074. if (!mod->modinfo_attrs)
  1075. return -ENOMEM;
  1076. temp_attr = mod->modinfo_attrs;
  1077. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  1078. if (!attr->test ||
  1079. (attr->test && attr->test(mod))) {
  1080. memcpy(temp_attr, attr, sizeof(*temp_attr));
  1081. sysfs_attr_init(&temp_attr->attr);
  1082. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  1083. ++temp_attr;
  1084. }
  1085. }
  1086. return error;
  1087. }
  1088. void module_remove_modinfo_attrs(struct module *mod)
  1089. {
  1090. struct module_attribute *attr;
  1091. int i;
  1092. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  1093. /* pick a field to test for end of list */
  1094. if (!attr->attr.name)
  1095. break;
  1096. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  1097. if (attr->free)
  1098. attr->free(mod);
  1099. }
  1100. kfree(mod->modinfo_attrs);
  1101. }
  1102. int mod_sysfs_init(struct module *mod)
  1103. {
  1104. int err;
  1105. struct kobject *kobj;
  1106. if (!module_sysfs_initialized) {
  1107. printk(KERN_ERR "%s: module sysfs not initialized\n",
  1108. mod->name);
  1109. err = -EINVAL;
  1110. goto out;
  1111. }
  1112. kobj = kset_find_obj(module_kset, mod->name);
  1113. if (kobj) {
  1114. printk(KERN_ERR "%s: module is already loaded\n", mod->name);
  1115. kobject_put(kobj);
  1116. err = -EINVAL;
  1117. goto out;
  1118. }
  1119. mod->mkobj.mod = mod;
  1120. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  1121. mod->mkobj.kobj.kset = module_kset;
  1122. err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
  1123. "%s", mod->name);
  1124. if (err)
  1125. kobject_put(&mod->mkobj.kobj);
  1126. /* delay uevent until full sysfs population */
  1127. out:
  1128. return err;
  1129. }
  1130. int mod_sysfs_setup(struct module *mod,
  1131. struct kernel_param *kparam,
  1132. unsigned int num_params)
  1133. {
  1134. int err;
  1135. mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
  1136. if (!mod->holders_dir) {
  1137. err = -ENOMEM;
  1138. goto out_unreg;
  1139. }
  1140. err = module_param_sysfs_setup(mod, kparam, num_params);
  1141. if (err)
  1142. goto out_unreg_holders;
  1143. err = module_add_modinfo_attrs(mod);
  1144. if (err)
  1145. goto out_unreg_param;
  1146. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1147. return 0;
  1148. out_unreg_param:
  1149. module_param_sysfs_remove(mod);
  1150. out_unreg_holders:
  1151. kobject_put(mod->holders_dir);
  1152. out_unreg:
  1153. kobject_put(&mod->mkobj.kobj);
  1154. return err;
  1155. }
  1156. static void mod_sysfs_fini(struct module *mod)
  1157. {
  1158. kobject_put(&mod->mkobj.kobj);
  1159. }
  1160. #else /* CONFIG_SYSFS */
  1161. static void mod_sysfs_fini(struct module *mod)
  1162. {
  1163. }
  1164. #endif /* CONFIG_SYSFS */
  1165. static void mod_kobject_remove(struct module *mod)
  1166. {
  1167. module_remove_modinfo_attrs(mod);
  1168. module_param_sysfs_remove(mod);
  1169. kobject_put(mod->mkobj.drivers_dir);
  1170. kobject_put(mod->holders_dir);
  1171. mod_sysfs_fini(mod);
  1172. }
  1173. /*
  1174. * unlink the module with the whole machine is stopped with interrupts off
  1175. * - this defends against kallsyms not taking locks
  1176. */
  1177. static int __unlink_module(void *_mod)
  1178. {
  1179. struct module *mod = _mod;
  1180. list_del(&mod->list);
  1181. return 0;
  1182. }
  1183. /* Free a module, remove from lists, etc (must hold module_mutex). */
  1184. static void free_module(struct module *mod)
  1185. {
  1186. trace_module_free(mod);
  1187. /* Delete from various lists */
  1188. stop_machine(__unlink_module, mod, NULL);
  1189. remove_notes_attrs(mod);
  1190. remove_sect_attrs(mod);
  1191. mod_kobject_remove(mod);
  1192. /* Arch-specific cleanup. */
  1193. module_arch_cleanup(mod);
  1194. /* Module unload stuff */
  1195. module_unload_free(mod);
  1196. /* Free any allocated parameters. */
  1197. destroy_params(mod->kp, mod->num_kp);
  1198. /* This may be NULL, but that's OK */
  1199. module_free(mod, mod->module_init);
  1200. kfree(mod->args);
  1201. if (mod->percpu)
  1202. percpu_modfree(mod->percpu);
  1203. #if defined(CONFIG_MODULE_UNLOAD)
  1204. if (mod->refptr)
  1205. free_percpu(mod->refptr);
  1206. #endif
  1207. /* Free lock-classes: */
  1208. lockdep_free_key_range(mod->module_core, mod->core_size);
  1209. /* Finally, free the core (containing the module structure) */
  1210. module_free(mod, mod->module_core);
  1211. #ifdef CONFIG_MPU
  1212. update_protections(current->mm);
  1213. #endif
  1214. }
  1215. void *__symbol_get(const char *symbol)
  1216. {
  1217. struct module *owner;
  1218. const struct kernel_symbol *sym;
  1219. preempt_disable();
  1220. sym = find_symbol(symbol, &owner, NULL, true, true);
  1221. if (sym && strong_try_module_get(owner))
  1222. sym = NULL;
  1223. preempt_enable();
  1224. return sym ? (void *)sym->value : NULL;
  1225. }
  1226. EXPORT_SYMBOL_GPL(__symbol_get);
  1227. /*
  1228. * Ensure that an exported symbol [global namespace] does not already exist
  1229. * in the kernel or in some other module's exported symbol table.
  1230. */
  1231. static int verify_export_symbols(struct module *mod)
  1232. {
  1233. unsigned int i;
  1234. struct module *owner;
  1235. const struct kernel_symbol *s;
  1236. struct {
  1237. const struct kernel_symbol *sym;
  1238. unsigned int num;
  1239. } arr[] = {
  1240. { mod->syms, mod->num_syms },
  1241. { mod->gpl_syms, mod->num_gpl_syms },
  1242. { mod->gpl_future_syms, mod->num_gpl_future_syms },
  1243. #ifdef CONFIG_UNUSED_SYMBOLS
  1244. { mod->unused_syms, mod->num_unused_syms },
  1245. { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
  1246. #endif
  1247. };
  1248. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1249. for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
  1250. if (find_symbol(s->name, &owner, NULL, true, false)) {
  1251. printk(KERN_ERR
  1252. "%s: exports duplicate symbol %s"
  1253. " (owned by %s)\n",
  1254. mod->name, s->name, module_name(owner));
  1255. return -ENOEXEC;
  1256. }
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. /* Change all symbols so that st_value encodes the pointer directly. */
  1262. static int simplify_symbols(Elf_Shdr *sechdrs,
  1263. unsigned int symindex,
  1264. const char *strtab,
  1265. unsigned int versindex,
  1266. unsigned int pcpuindex,
  1267. struct module *mod)
  1268. {
  1269. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1270. unsigned long secbase;
  1271. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1272. int ret = 0;
  1273. const struct kernel_symbol *ksym;
  1274. for (i = 1; i < n; i++) {
  1275. switch (sym[i].st_shndx) {
  1276. case SHN_COMMON:
  1277. /* We compiled with -fno-common. These are not
  1278. supposed to happen. */
  1279. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1280. printk("%s: please compile with -fno-common\n",
  1281. mod->name);
  1282. ret = -ENOEXEC;
  1283. break;
  1284. case SHN_ABS:
  1285. /* Don't need to do anything */
  1286. DEBUGP("Absolute symbol: 0x%08lx\n",
  1287. (long)sym[i].st_value);
  1288. break;
  1289. case SHN_UNDEF:
  1290. ksym = resolve_symbol(sechdrs, versindex,
  1291. strtab + sym[i].st_name, mod);
  1292. /* Ok if resolved. */
  1293. if (ksym) {
  1294. sym[i].st_value = ksym->value;
  1295. break;
  1296. }
  1297. /* Ok if weak. */
  1298. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1299. break;
  1300. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1301. mod->name, strtab + sym[i].st_name);
  1302. ret = -ENOENT;
  1303. break;
  1304. default:
  1305. /* Divert to percpu allocation if a percpu var. */
  1306. if (sym[i].st_shndx == pcpuindex)
  1307. secbase = (unsigned long)mod->percpu;
  1308. else
  1309. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1310. sym[i].st_value += secbase;
  1311. break;
  1312. }
  1313. }
  1314. return ret;
  1315. }
  1316. /* Additional bytes needed by arch in front of individual sections */
  1317. unsigned int __weak arch_mod_section_prepend(struct module *mod,
  1318. unsigned int section)
  1319. {
  1320. /* default implementation just returns zero */
  1321. return 0;
  1322. }
  1323. /* Update size with this section: return offset. */
  1324. static long get_offset(struct module *mod, unsigned int *size,
  1325. Elf_Shdr *sechdr, unsigned int section)
  1326. {
  1327. long ret;
  1328. *size += arch_mod_section_prepend(mod, section);
  1329. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1330. *size = ret + sechdr->sh_size;
  1331. return ret;
  1332. }
  1333. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1334. might -- code, read-only data, read-write data, small data. Tally
  1335. sizes, and place the offsets into sh_entsize fields: high bit means it
  1336. belongs in init. */
  1337. static void layout_sections(struct module *mod,
  1338. const Elf_Ehdr *hdr,
  1339. Elf_Shdr *sechdrs,
  1340. const char *secstrings)
  1341. {
  1342. static unsigned long const masks[][2] = {
  1343. /* NOTE: all executable code must be the first section
  1344. * in this array; otherwise modify the text_size
  1345. * finder in the two loops below */
  1346. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1347. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1348. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1349. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1350. };
  1351. unsigned int m, i;
  1352. for (i = 0; i < hdr->e_shnum; i++)
  1353. sechdrs[i].sh_entsize = ~0UL;
  1354. DEBUGP("Core section allocation order:\n");
  1355. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1356. for (i = 0; i < hdr->e_shnum; ++i) {
  1357. Elf_Shdr *s = &sechdrs[i];
  1358. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1359. || (s->sh_flags & masks[m][1])
  1360. || s->sh_entsize != ~0UL
  1361. || strstarts(secstrings + s->sh_name, ".init"))
  1362. continue;
  1363. s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
  1364. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1365. }
  1366. if (m == 0)
  1367. mod->core_text_size = mod->core_size;
  1368. }
  1369. DEBUGP("Init section allocation order:\n");
  1370. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1371. for (i = 0; i < hdr->e_shnum; ++i) {
  1372. Elf_Shdr *s = &sechdrs[i];
  1373. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1374. || (s->sh_flags & masks[m][1])
  1375. || s->sh_entsize != ~0UL
  1376. || !strstarts(secstrings + s->sh_name, ".init"))
  1377. continue;
  1378. s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
  1379. | INIT_OFFSET_MASK);
  1380. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1381. }
  1382. if (m == 0)
  1383. mod->init_text_size = mod->init_size;
  1384. }
  1385. }
  1386. static void set_license(struct module *mod, const char *license)
  1387. {
  1388. if (!license)
  1389. license = "unspecified";
  1390. if (!license_is_gpl_compatible(license)) {
  1391. if (!test_taint(TAINT_PROPRIETARY_MODULE))
  1392. printk(KERN_WARNING "%s: module license '%s' taints "
  1393. "kernel.\n", mod->name, license);
  1394. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1395. }
  1396. }
  1397. /* Parse tag=value strings from .modinfo section */
  1398. static char *next_string(char *string, unsigned long *secsize)
  1399. {
  1400. /* Skip non-zero chars */
  1401. while (string[0]) {
  1402. string++;
  1403. if ((*secsize)-- <= 1)
  1404. return NULL;
  1405. }
  1406. /* Skip any zero padding. */
  1407. while (!string[0]) {
  1408. string++;
  1409. if ((*secsize)-- <= 1)
  1410. return NULL;
  1411. }
  1412. return string;
  1413. }
  1414. static char *get_modinfo(Elf_Shdr *sechdrs,
  1415. unsigned int info,
  1416. const char *tag)
  1417. {
  1418. char *p;
  1419. unsigned int taglen = strlen(tag);
  1420. unsigned long size = sechdrs[info].sh_size;
  1421. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1422. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1423. return p + taglen + 1;
  1424. }
  1425. return NULL;
  1426. }
  1427. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1428. unsigned int infoindex)
  1429. {
  1430. struct module_attribute *attr;
  1431. int i;
  1432. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1433. if (attr->setup)
  1434. attr->setup(mod,
  1435. get_modinfo(sechdrs,
  1436. infoindex,
  1437. attr->attr.name));
  1438. }
  1439. }
  1440. static void free_modinfo(struct module *mod)
  1441. {
  1442. struct module_attribute *attr;
  1443. int i;
  1444. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1445. if (attr->free)
  1446. attr->free(mod);
  1447. }
  1448. }
  1449. #ifdef CONFIG_KALLSYMS
  1450. /* lookup symbol in given range of kernel_symbols */
  1451. static const struct kernel_symbol *lookup_symbol(const char *name,
  1452. const struct kernel_symbol *start,
  1453. const struct kernel_symbol *stop)
  1454. {
  1455. const struct kernel_symbol *ks = start;
  1456. for (; ks < stop; ks++)
  1457. if (strcmp(ks->name, name) == 0)
  1458. return ks;
  1459. return NULL;
  1460. }
  1461. static int is_exported(const char *name, unsigned long value,
  1462. const struct module *mod)
  1463. {
  1464. const struct kernel_symbol *ks;
  1465. if (!mod)
  1466. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  1467. else
  1468. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  1469. return ks != NULL && ks->value == value;
  1470. }
  1471. /* As per nm */
  1472. static char elf_type(const Elf_Sym *sym,
  1473. Elf_Shdr *sechdrs,
  1474. const char *secstrings,
  1475. struct module *mod)
  1476. {
  1477. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1478. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1479. return 'v';
  1480. else
  1481. return 'w';
  1482. }
  1483. if (sym->st_shndx == SHN_UNDEF)
  1484. return 'U';
  1485. if (sym->st_shndx == SHN_ABS)
  1486. return 'a';
  1487. if (sym->st_shndx >= SHN_LORESERVE)
  1488. return '?';
  1489. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1490. return 't';
  1491. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1492. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1493. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1494. return 'r';
  1495. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1496. return 'g';
  1497. else
  1498. return 'd';
  1499. }
  1500. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1501. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1502. return 's';
  1503. else
  1504. return 'b';
  1505. }
  1506. if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
  1507. return 'n';
  1508. return '?';
  1509. }
  1510. static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
  1511. unsigned int shnum)
  1512. {
  1513. const Elf_Shdr *sec;
  1514. if (src->st_shndx == SHN_UNDEF
  1515. || src->st_shndx >= shnum
  1516. || !src->st_name)
  1517. return false;
  1518. sec = sechdrs + src->st_shndx;
  1519. if (!(sec->sh_flags & SHF_ALLOC)
  1520. #ifndef CONFIG_KALLSYMS_ALL
  1521. || !(sec->sh_flags & SHF_EXECINSTR)
  1522. #endif
  1523. || (sec->sh_entsize & INIT_OFFSET_MASK))
  1524. return false;
  1525. return true;
  1526. }
  1527. static unsigned long layout_symtab(struct module *mod,
  1528. Elf_Shdr *sechdrs,
  1529. unsigned int symindex,
  1530. unsigned int strindex,
  1531. const Elf_Ehdr *hdr,
  1532. const char *secstrings,
  1533. unsigned long *pstroffs,
  1534. unsigned long *strmap)
  1535. {
  1536. unsigned long symoffs;
  1537. Elf_Shdr *symsect = sechdrs + symindex;
  1538. Elf_Shdr *strsect = sechdrs + strindex;
  1539. const Elf_Sym *src;
  1540. const char *strtab;
  1541. unsigned int i, nsrc, ndst;
  1542. /* Put symbol section at end of init part of module. */
  1543. symsect->sh_flags |= SHF_ALLOC;
  1544. symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
  1545. symindex) | INIT_OFFSET_MASK;
  1546. DEBUGP("\t%s\n", secstrings + symsect->sh_name);
  1547. src = (void *)hdr + symsect->sh_offset;
  1548. nsrc = symsect->sh_size / sizeof(*src);
  1549. strtab = (void *)hdr + strsect->sh_offset;
  1550. for (ndst = i = 1; i < nsrc; ++i, ++src)
  1551. if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
  1552. unsigned int j = src->st_name;
  1553. while(!__test_and_set_bit(j, strmap) && strtab[j])
  1554. ++j;
  1555. ++ndst;
  1556. }
  1557. /* Append room for core symbols at end of core part. */
  1558. symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
  1559. mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
  1560. /* Put string table section at end of init part of module. */
  1561. strsect->sh_flags |= SHF_ALLOC;
  1562. strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
  1563. strindex) | INIT_OFFSET_MASK;
  1564. DEBUGP("\t%s\n", secstrings + strsect->sh_name);
  1565. /* Append room for core symbols' strings at end of core part. */
  1566. *pstroffs = mod->core_size;
  1567. __set_bit(0, strmap);
  1568. mod->core_size += bitmap_weight(strmap, strsect->sh_size);
  1569. return symoffs;
  1570. }
  1571. static void add_kallsyms(struct module *mod,
  1572. Elf_Shdr *sechdrs,
  1573. unsigned int shnum,
  1574. unsigned int symindex,
  1575. unsigned int strindex,
  1576. unsigned long symoffs,
  1577. unsigned long stroffs,
  1578. const char *secstrings,
  1579. unsigned long *strmap)
  1580. {
  1581. unsigned int i, ndst;
  1582. const Elf_Sym *src;
  1583. Elf_Sym *dst;
  1584. char *s;
  1585. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1586. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1587. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1588. /* Set types up while we still have access to sections. */
  1589. for (i = 0; i < mod->num_symtab; i++)
  1590. mod->symtab[i].st_info
  1591. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1592. mod->core_symtab = dst = mod->module_core + symoffs;
  1593. src = mod->symtab;
  1594. *dst = *src;
  1595. for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
  1596. if (!is_core_symbol(src, sechdrs, shnum))
  1597. continue;
  1598. dst[ndst] = *src;
  1599. dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
  1600. ++ndst;
  1601. }
  1602. mod->core_num_syms = ndst;
  1603. mod->core_strtab = s = mod->module_core + stroffs;
  1604. for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
  1605. if (test_bit(i, strmap))
  1606. *++s = mod->strtab[i];
  1607. }
  1608. #else
  1609. static inline unsigned long layout_symtab(struct module *mod,
  1610. Elf_Shdr *sechdrs,
  1611. unsigned int symindex,
  1612. unsigned int strindex,
  1613. const Elf_Ehdr *hdr,
  1614. const char *secstrings,
  1615. unsigned long *pstroffs,
  1616. unsigned long *strmap)
  1617. {
  1618. return 0;
  1619. }
  1620. static inline void add_kallsyms(struct module *mod,
  1621. Elf_Shdr *sechdrs,
  1622. unsigned int shnum,
  1623. unsigned int symindex,
  1624. unsigned int strindex,
  1625. unsigned long symoffs,
  1626. unsigned long stroffs,
  1627. const char *secstrings,
  1628. const unsigned long *strmap)
  1629. {
  1630. }
  1631. #endif /* CONFIG_KALLSYMS */
  1632. static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
  1633. {
  1634. #ifdef CONFIG_DYNAMIC_DEBUG
  1635. if (ddebug_add_module(debug, num, debug->modname))
  1636. printk(KERN_ERR "dynamic debug error adding module: %s\n",
  1637. debug->modname);
  1638. #endif
  1639. }
  1640. static void *module_alloc_update_bounds(unsigned long size)
  1641. {
  1642. void *ret = module_alloc(size);
  1643. if (ret) {
  1644. /* Update module bounds. */
  1645. if ((unsigned long)ret < module_addr_min)
  1646. module_addr_min = (unsigned long)ret;
  1647. if ((unsigned long)ret + size > module_addr_max)
  1648. module_addr_max = (unsigned long)ret + size;
  1649. }
  1650. return ret;
  1651. }
  1652. #ifdef CONFIG_DEBUG_KMEMLEAK
  1653. static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1654. Elf_Shdr *sechdrs, char *secstrings)
  1655. {
  1656. unsigned int i;
  1657. /* only scan the sections containing data */
  1658. kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
  1659. for (i = 1; i < hdr->e_shnum; i++) {
  1660. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1661. continue;
  1662. if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
  1663. && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
  1664. continue;
  1665. kmemleak_scan_area((void *)sechdrs[i].sh_addr,
  1666. sechdrs[i].sh_size, GFP_KERNEL);
  1667. }
  1668. }
  1669. #else
  1670. static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
  1671. Elf_Shdr *sechdrs, char *secstrings)
  1672. {
  1673. }
  1674. #endif
  1675. /* Allocate and load the module: note that size of section 0 is always
  1676. zero, and we rely on this for optional sections. */
  1677. static noinline struct module *load_module(void __user *umod,
  1678. unsigned long len,
  1679. const char __user *uargs)
  1680. {
  1681. Elf_Ehdr *hdr;
  1682. Elf_Shdr *sechdrs;
  1683. char *secstrings, *args, *modmagic, *strtab = NULL;
  1684. char *staging;
  1685. unsigned int i;
  1686. unsigned int symindex = 0;
  1687. unsigned int strindex = 0;
  1688. unsigned int modindex, versindex, infoindex, pcpuindex;
  1689. struct module *mod;
  1690. long err = 0;
  1691. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1692. unsigned long symoffs, stroffs, *strmap;
  1693. mm_segment_t old_fs;
  1694. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1695. umod, len, uargs);
  1696. if (len < sizeof(*hdr))
  1697. return ERR_PTR(-ENOEXEC);
  1698. /* Suck in entire file: we'll want most of it. */
  1699. /* vmalloc barfs on "unusual" numbers. Check here */
  1700. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1701. return ERR_PTR(-ENOMEM);
  1702. if (copy_from_user(hdr, umod, len) != 0) {
  1703. err = -EFAULT;
  1704. goto free_hdr;
  1705. }
  1706. /* Sanity checks against insmoding binaries or wrong arch,
  1707. weird elf version */
  1708. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  1709. || hdr->e_type != ET_REL
  1710. || !elf_check_arch(hdr)
  1711. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1712. err = -ENOEXEC;
  1713. goto free_hdr;
  1714. }
  1715. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1716. goto truncated;
  1717. /* Convenience variables */
  1718. sechdrs = (void *)hdr + hdr->e_shoff;
  1719. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1720. sechdrs[0].sh_addr = 0;
  1721. for (i = 1; i < hdr->e_shnum; i++) {
  1722. if (sechdrs[i].sh_type != SHT_NOBITS
  1723. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1724. goto truncated;
  1725. /* Mark all sections sh_addr with their address in the
  1726. temporary image. */
  1727. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1728. /* Internal symbols and strings. */
  1729. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1730. symindex = i;
  1731. strindex = sechdrs[i].sh_link;
  1732. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1733. }
  1734. #ifndef CONFIG_MODULE_UNLOAD
  1735. /* Don't load .exit sections */
  1736. if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
  1737. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1738. #endif
  1739. }
  1740. modindex = find_sec(hdr, sechdrs, secstrings,
  1741. ".gnu.linkonce.this_module");
  1742. if (!modindex) {
  1743. printk(KERN_WARNING "No module found in object\n");
  1744. err = -ENOEXEC;
  1745. goto free_hdr;
  1746. }
  1747. /* This is temporary: point mod into copy of data. */
  1748. mod = (void *)sechdrs[modindex].sh_addr;
  1749. if (symindex == 0) {
  1750. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1751. mod->name);
  1752. err = -ENOEXEC;
  1753. goto free_hdr;
  1754. }
  1755. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1756. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1757. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1758. /* Don't keep modinfo and version sections. */
  1759. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1760. sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1761. /* Check module struct version now, before we try to use module. */
  1762. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1763. err = -ENOEXEC;
  1764. goto free_hdr;
  1765. }
  1766. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1767. /* This is allowed: modprobe --force will invalidate it. */
  1768. if (!modmagic) {
  1769. err = try_to_force_load(mod, "bad vermagic");
  1770. if (err)
  1771. goto free_hdr;
  1772. } else if (!same_magic(modmagic, vermagic, versindex)) {
  1773. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1774. mod->name, modmagic, vermagic);
  1775. err = -ENOEXEC;
  1776. goto free_hdr;
  1777. }
  1778. staging = get_modinfo(sechdrs, infoindex, "staging");
  1779. if (staging) {
  1780. add_taint_module(mod, TAINT_CRAP);
  1781. printk(KERN_WARNING "%s: module is from the staging directory,"
  1782. " the quality is unknown, you have been warned.\n",
  1783. mod->name);
  1784. }
  1785. /* Now copy in args */
  1786. args = strndup_user(uargs, ~0UL >> 1);
  1787. if (IS_ERR(args)) {
  1788. err = PTR_ERR(args);
  1789. goto free_hdr;
  1790. }
  1791. strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
  1792. * sizeof(long), GFP_KERNEL);
  1793. if (!strmap) {
  1794. err = -ENOMEM;
  1795. goto free_mod;
  1796. }
  1797. if (find_module(mod->name)) {
  1798. err = -EEXIST;
  1799. goto free_mod;
  1800. }
  1801. mod->state = MODULE_STATE_COMING;
  1802. /* Allow arches to frob section contents and sizes. */
  1803. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1804. if (err < 0)
  1805. goto free_mod;
  1806. if (pcpuindex) {
  1807. /* We have a special allocation for this section. */
  1808. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1809. sechdrs[pcpuindex].sh_addralign,
  1810. mod->name);
  1811. if (!percpu) {
  1812. err = -ENOMEM;
  1813. goto free_mod;
  1814. }
  1815. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1816. mod->percpu = percpu;
  1817. }
  1818. /* Determine total sizes, and put offsets in sh_entsize. For now
  1819. this is done generically; there doesn't appear to be any
  1820. special cases for the architectures. */
  1821. layout_sections(mod, hdr, sechdrs, secstrings);
  1822. symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
  1823. secstrings, &stroffs, strmap);
  1824. /* Do the allocs. */
  1825. ptr = module_alloc_update_bounds(mod->core_size);
  1826. /*
  1827. * The pointer to this block is stored in the module structure
  1828. * which is inside the block. Just mark it as not being a
  1829. * leak.
  1830. */
  1831. kmemleak_not_leak(ptr);
  1832. if (!ptr) {
  1833. err = -ENOMEM;
  1834. goto free_percpu;
  1835. }
  1836. memset(ptr, 0, mod->core_size);
  1837. mod->module_core = ptr;
  1838. ptr = module_alloc_update_bounds(mod->init_size);
  1839. /*
  1840. * The pointer to this block is stored in the module structure
  1841. * which is inside the block. This block doesn't need to be
  1842. * scanned as it contains data and code that will be freed
  1843. * after the module is initialized.
  1844. */
  1845. kmemleak_ignore(ptr);
  1846. if (!ptr && mod->init_size) {
  1847. err = -ENOMEM;
  1848. goto free_core;
  1849. }
  1850. memset(ptr, 0, mod->init_size);
  1851. mod->module_init = ptr;
  1852. /* Transfer each section which specifies SHF_ALLOC */
  1853. DEBUGP("final section addresses:\n");
  1854. for (i = 0; i < hdr->e_shnum; i++) {
  1855. void *dest;
  1856. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1857. continue;
  1858. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1859. dest = mod->module_init
  1860. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1861. else
  1862. dest = mod->module_core + sechdrs[i].sh_entsize;
  1863. if (sechdrs[i].sh_type != SHT_NOBITS)
  1864. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1865. sechdrs[i].sh_size);
  1866. /* Update sh_addr to point to copy in image. */
  1867. sechdrs[i].sh_addr = (unsigned long)dest;
  1868. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1869. }
  1870. /* Module has been moved. */
  1871. mod = (void *)sechdrs[modindex].sh_addr;
  1872. kmemleak_load_module(mod, hdr, sechdrs, secstrings);
  1873. #if defined(CONFIG_MODULE_UNLOAD)
  1874. mod->refptr = alloc_percpu(struct module_ref);
  1875. if (!mod->refptr) {
  1876. err = -ENOMEM;
  1877. goto free_init;
  1878. }
  1879. #endif
  1880. /* Now we've moved module, initialize linked lists, etc. */
  1881. module_unload_init(mod);
  1882. /* add kobject, so we can reference it. */
  1883. err = mod_sysfs_init(mod);
  1884. if (err)
  1885. goto free_unload;
  1886. /* Set up license info based on the info section */
  1887. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1888. /*
  1889. * ndiswrapper is under GPL by itself, but loads proprietary modules.
  1890. * Don't use add_taint_module(), as it would prevent ndiswrapper from
  1891. * using GPL-only symbols it needs.
  1892. */
  1893. if (strcmp(mod->name, "ndiswrapper") == 0)
  1894. add_taint(TAINT_PROPRIETARY_MODULE);
  1895. /* driverloader was caught wrongly pretending to be under GPL */
  1896. if (strcmp(mod->name, "driverloader") == 0)
  1897. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1898. /* Set up MODINFO_ATTR fields */
  1899. setup_modinfo(mod, sechdrs, infoindex);
  1900. /* Fix up syms, so that st_value is a pointer to location. */
  1901. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1902. mod);
  1903. if (err < 0)
  1904. goto cleanup;
  1905. /* Now we've got everything in the final locations, we can
  1906. * find optional sections. */
  1907. mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
  1908. sizeof(*mod->kp), &mod->num_kp);
  1909. mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
  1910. sizeof(*mod->syms), &mod->num_syms);
  1911. mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
  1912. mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
  1913. sizeof(*mod->gpl_syms),
  1914. &mod->num_gpl_syms);
  1915. mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1916. mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
  1917. "__ksymtab_gpl_future",
  1918. sizeof(*mod->gpl_future_syms),
  1919. &mod->num_gpl_future_syms);
  1920. mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
  1921. "__kcrctab_gpl_future");
  1922. #ifdef CONFIG_UNUSED_SYMBOLS
  1923. mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
  1924. "__ksymtab_unused",
  1925. sizeof(*mod->unused_syms),
  1926. &mod->num_unused_syms);
  1927. mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
  1928. "__kcrctab_unused");
  1929. mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
  1930. "__ksymtab_unused_gpl",
  1931. sizeof(*mod->unused_gpl_syms),
  1932. &mod->num_unused_gpl_syms);
  1933. mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
  1934. "__kcrctab_unused_gpl");
  1935. #endif
  1936. #ifdef CONFIG_CONSTRUCTORS
  1937. mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
  1938. sizeof(*mod->ctors), &mod->num_ctors);
  1939. #endif
  1940. #ifdef CONFIG_TRACEPOINTS
  1941. mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
  1942. "__tracepoints",
  1943. sizeof(*mod->tracepoints),
  1944. &mod->num_tracepoints);
  1945. #endif
  1946. #ifdef CONFIG_EVENT_TRACING
  1947. mod->trace_events = section_objs(hdr, sechdrs, secstrings,
  1948. "_ftrace_events",
  1949. sizeof(*mod->trace_events),
  1950. &mod->num_trace_events);
  1951. /*
  1952. * This section contains pointers to allocated objects in the trace
  1953. * code and not scanning it leads to false positives.
  1954. */
  1955. kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
  1956. mod->num_trace_events, GFP_KERNEL);
  1957. #endif
  1958. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  1959. /* sechdrs[0].sh_size is always zero */
  1960. mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
  1961. "__mcount_loc",
  1962. sizeof(*mod->ftrace_callsites),
  1963. &mod->num_ftrace_callsites);
  1964. #endif
  1965. #ifdef CONFIG_MODVERSIONS
  1966. if ((mod->num_syms && !mod->crcs)
  1967. || (mod->num_gpl_syms && !mod->gpl_crcs)
  1968. || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
  1969. #ifdef CONFIG_UNUSED_SYMBOLS
  1970. || (mod->num_unused_syms && !mod->unused_crcs)
  1971. || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
  1972. #endif
  1973. ) {
  1974. err = try_to_force_load(mod,
  1975. "no versions for exported symbols");
  1976. if (err)
  1977. goto cleanup;
  1978. }
  1979. #endif
  1980. /* Now do relocations. */
  1981. for (i = 1; i < hdr->e_shnum; i++) {
  1982. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1983. unsigned int info = sechdrs[i].sh_info;
  1984. /* Not a valid relocation section? */
  1985. if (info >= hdr->e_shnum)
  1986. continue;
  1987. /* Don't bother with non-allocated sections */
  1988. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1989. continue;
  1990. if (sechdrs[i].sh_type == SHT_REL)
  1991. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1992. else if (sechdrs[i].sh_type == SHT_RELA)
  1993. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1994. mod);
  1995. if (err < 0)
  1996. goto cleanup;
  1997. }
  1998. /* Find duplicate symbols */
  1999. err = verify_export_symbols(mod);
  2000. if (err < 0)
  2001. goto cleanup;
  2002. /* Set up and sort exception table */
  2003. mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
  2004. sizeof(*mod->extable), &mod->num_exentries);
  2005. sort_extable(mod->extable, mod->extable + mod->num_exentries);
  2006. /* Finally, copy percpu area over. */
  2007. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  2008. sechdrs[pcpuindex].sh_size);
  2009. add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
  2010. symoffs, stroffs, secstrings, strmap);
  2011. kfree(strmap);
  2012. strmap = NULL;
  2013. if (!mod->taints) {
  2014. struct _ddebug *debug;
  2015. unsigned int num_debug;
  2016. debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
  2017. sizeof(*debug), &num_debug);
  2018. if (debug)
  2019. dynamic_debug_setup(debug, num_debug);
  2020. }
  2021. err = module_finalize(hdr, sechdrs, mod);
  2022. if (err < 0)
  2023. goto cleanup;
  2024. /* flush the icache in correct context */
  2025. old_fs = get_fs();
  2026. set_fs(KERNEL_DS);
  2027. /*
  2028. * Flush the instruction cache, since we've played with text.
  2029. * Do it before processing of module parameters, so the module
  2030. * can provide parameter accessor functions of its own.
  2031. */
  2032. if (mod->module_init)
  2033. flush_icache_range((unsigned long)mod->module_init,
  2034. (unsigned long)mod->module_init
  2035. + mod->init_size);
  2036. flush_icache_range((unsigned long)mod->module_core,
  2037. (unsigned long)mod->module_core + mod->core_size);
  2038. set_fs(old_fs);
  2039. mod->args = args;
  2040. if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
  2041. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  2042. mod->name);
  2043. /* Now sew it into the lists so we can get lockdep and oops
  2044. * info during argument parsing. Noone should access us, since
  2045. * strong_try_module_get() will fail.
  2046. * lockdep/oops can run asynchronous, so use the RCU list insertion
  2047. * function to insert in a way safe to concurrent readers.
  2048. * The mutex protects against concurrent writers.
  2049. */
  2050. list_add_rcu(&mod->list, &modules);
  2051. err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
  2052. if (err < 0)
  2053. goto unlink;
  2054. err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
  2055. if (err < 0)
  2056. goto unlink;
  2057. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2058. add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  2059. /* Get rid of temporary copy */
  2060. vfree(hdr);
  2061. trace_module_load(mod);
  2062. /* Done! */
  2063. return mod;
  2064. unlink:
  2065. /* Unlink carefully: kallsyms could be walking list. */
  2066. list_del_rcu(&mod->list);
  2067. synchronize_sched();
  2068. module_arch_cleanup(mod);
  2069. cleanup:
  2070. free_modinfo(mod);
  2071. kobject_del(&mod->mkobj.kobj);
  2072. kobject_put(&mod->mkobj.kobj);
  2073. free_unload:
  2074. module_unload_free(mod);
  2075. #if defined(CONFIG_MODULE_UNLOAD)
  2076. free_percpu(mod->refptr);
  2077. free_init:
  2078. #endif
  2079. module_free(mod, mod->module_init);
  2080. free_core:
  2081. module_free(mod, mod->module_core);
  2082. /* mod will be freed with core. Don't access it beyond this line! */
  2083. free_percpu:
  2084. if (percpu)
  2085. percpu_modfree(percpu);
  2086. free_mod:
  2087. kfree(args);
  2088. kfree(strmap);
  2089. free_hdr:
  2090. vfree(hdr);
  2091. return ERR_PTR(err);
  2092. truncated:
  2093. printk(KERN_ERR "Module len %lu truncated\n", len);
  2094. err = -ENOEXEC;
  2095. goto free_hdr;
  2096. }
  2097. /* Call module constructors. */
  2098. static void do_mod_ctors(struct module *mod)
  2099. {
  2100. #ifdef CONFIG_CONSTRUCTORS
  2101. unsigned long i;
  2102. for (i = 0; i < mod->num_ctors; i++)
  2103. mod->ctors[i]();
  2104. #endif
  2105. }
  2106. /* This is where the real work happens */
  2107. SYSCALL_DEFINE3(init_module, void __user *, umod,
  2108. unsigned long, len, const char __user *, uargs)
  2109. {
  2110. struct module *mod;
  2111. int ret = 0;
  2112. /* Must have permission */
  2113. if (!capable(CAP_SYS_MODULE) || modules_disabled)
  2114. return -EPERM;
  2115. /* Only one module load at a time, please */
  2116. if (mutex_lock_interruptible(&module_mutex) != 0)
  2117. return -EINTR;
  2118. /* Do all the hard work */
  2119. mod = load_module(umod, len, uargs);
  2120. if (IS_ERR(mod)) {
  2121. mutex_unlock(&module_mutex);
  2122. return PTR_ERR(mod);
  2123. }
  2124. /* Drop lock so they can recurse */
  2125. mutex_unlock(&module_mutex);
  2126. blocking_notifier_call_chain(&module_notify_list,
  2127. MODULE_STATE_COMING, mod);
  2128. do_mod_ctors(mod);
  2129. /* Start the module */
  2130. if (mod->init != NULL)
  2131. ret = do_one_initcall(mod->init);
  2132. if (ret < 0) {
  2133. /* Init routine failed: abort. Try to protect us from
  2134. buggy refcounters. */
  2135. mod->state = MODULE_STATE_GOING;
  2136. synchronize_sched();
  2137. module_put(mod);
  2138. blocking_notifier_call_chain(&module_notify_list,
  2139. MODULE_STATE_GOING, mod);
  2140. mutex_lock(&module_mutex);
  2141. free_module(mod);
  2142. mutex_unlock(&module_mutex);
  2143. wake_up(&module_wq);
  2144. return ret;
  2145. }
  2146. if (ret > 0) {
  2147. printk(KERN_WARNING
  2148. "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
  2149. "%s: loading module anyway...\n",
  2150. __func__, mod->name, ret,
  2151. __func__);
  2152. dump_stack();
  2153. }
  2154. /* Now it's a first class citizen! Wake up anyone waiting for it. */
  2155. mod->state = MODULE_STATE_LIVE;
  2156. wake_up(&module_wq);
  2157. blocking_notifier_call_chain(&module_notify_list,
  2158. MODULE_STATE_LIVE, mod);
  2159. /* We need to finish all async code before the module init sequence is done */
  2160. async_synchronize_full();
  2161. mutex_lock(&module_mutex);
  2162. /* Drop initial reference. */
  2163. module_put(mod);
  2164. trim_init_extable(mod);
  2165. #ifdef CONFIG_KALLSYMS
  2166. mod->num_symtab = mod->core_num_syms;
  2167. mod->symtab = mod->core_symtab;
  2168. mod->strtab = mod->core_strtab;
  2169. #endif
  2170. module_free(mod, mod->module_init);
  2171. mod->module_init = NULL;
  2172. mod->init_size = 0;
  2173. mod->init_text_size = 0;
  2174. mutex_unlock(&module_mutex);
  2175. return 0;
  2176. }
  2177. static inline int within(unsigned long addr, void *start, unsigned long size)
  2178. {
  2179. return ((void *)addr >= start && (void *)addr < start + size);
  2180. }
  2181. #ifdef CONFIG_KALLSYMS
  2182. /*
  2183. * This ignores the intensely annoying "mapping symbols" found
  2184. * in ARM ELF files: $a, $t and $d.
  2185. */
  2186. static inline int is_arm_mapping_symbol(const char *str)
  2187. {
  2188. return str[0] == '$' && strchr("atd", str[1])
  2189. && (str[2] == '\0' || str[2] == '.');
  2190. }
  2191. static const char *get_ksymbol(struct module *mod,
  2192. unsigned long addr,
  2193. unsigned long *size,
  2194. unsigned long *offset)
  2195. {
  2196. unsigned int i, best = 0;
  2197. unsigned long nextval;
  2198. /* At worse, next value is at end of module */
  2199. if (within_module_init(addr, mod))
  2200. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  2201. else
  2202. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  2203. /* Scan for closest preceeding symbol, and next symbol. (ELF
  2204. starts real symbols at 1). */
  2205. for (i = 1; i < mod->num_symtab; i++) {
  2206. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  2207. continue;
  2208. /* We ignore unnamed symbols: they're uninformative
  2209. * and inserted at a whim. */
  2210. if (mod->symtab[i].st_value <= addr
  2211. && mod->symtab[i].st_value > mod->symtab[best].st_value
  2212. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2213. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2214. best = i;
  2215. if (mod->symtab[i].st_value > addr
  2216. && mod->symtab[i].st_value < nextval
  2217. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  2218. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  2219. nextval = mod->symtab[i].st_value;
  2220. }
  2221. if (!best)
  2222. return NULL;
  2223. if (size)
  2224. *size = nextval - mod->symtab[best].st_value;
  2225. if (offset)
  2226. *offset = addr - mod->symtab[best].st_value;
  2227. return mod->strtab + mod->symtab[best].st_name;
  2228. }
  2229. /* For kallsyms to ask for address resolution. NULL means not found. Careful
  2230. * not to lock to avoid deadlock on oopses, simply disable preemption. */
  2231. const char *module_address_lookup(unsigned long addr,
  2232. unsigned long *size,
  2233. unsigned long *offset,
  2234. char **modname,
  2235. char *namebuf)
  2236. {
  2237. struct module *mod;
  2238. const char *ret = NULL;
  2239. preempt_disable();
  2240. list_for_each_entry_rcu(mod, &modules, list) {
  2241. if (within_module_init(addr, mod) ||
  2242. within_module_core(addr, mod)) {
  2243. if (modname)
  2244. *modname = mod->name;
  2245. ret = get_ksymbol(mod, addr, size, offset);
  2246. break;
  2247. }
  2248. }
  2249. /* Make a copy in here where it's safe */
  2250. if (ret) {
  2251. strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
  2252. ret = namebuf;
  2253. }
  2254. preempt_enable();
  2255. return ret;
  2256. }
  2257. int lookup_module_symbol_name(unsigned long addr, char *symname)
  2258. {
  2259. struct module *mod;
  2260. preempt_disable();
  2261. list_for_each_entry_rcu(mod, &modules, list) {
  2262. if (within_module_init(addr, mod) ||
  2263. within_module_core(addr, mod)) {
  2264. const char *sym;
  2265. sym = get_ksymbol(mod, addr, NULL, NULL);
  2266. if (!sym)
  2267. goto out;
  2268. strlcpy(symname, sym, KSYM_NAME_LEN);
  2269. preempt_enable();
  2270. return 0;
  2271. }
  2272. }
  2273. out:
  2274. preempt_enable();
  2275. return -ERANGE;
  2276. }
  2277. int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
  2278. unsigned long *offset, char *modname, char *name)
  2279. {
  2280. struct module *mod;
  2281. preempt_disable();
  2282. list_for_each_entry_rcu(mod, &modules, list) {
  2283. if (within_module_init(addr, mod) ||
  2284. within_module_core(addr, mod)) {
  2285. const char *sym;
  2286. sym = get_ksymbol(mod, addr, size, offset);
  2287. if (!sym)
  2288. goto out;
  2289. if (modname)
  2290. strlcpy(modname, mod->name, MODULE_NAME_LEN);
  2291. if (name)
  2292. strlcpy(name, sym, KSYM_NAME_LEN);
  2293. preempt_enable();
  2294. return 0;
  2295. }
  2296. }
  2297. out:
  2298. preempt_enable();
  2299. return -ERANGE;
  2300. }
  2301. int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
  2302. char *name, char *module_name, int *exported)
  2303. {
  2304. struct module *mod;
  2305. preempt_disable();
  2306. list_for_each_entry_rcu(mod, &modules, list) {
  2307. if (symnum < mod->num_symtab) {
  2308. *value = mod->symtab[symnum].st_value;
  2309. *type = mod->symtab[symnum].st_info;
  2310. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  2311. KSYM_NAME_LEN);
  2312. strlcpy(module_name, mod->name, MODULE_NAME_LEN);
  2313. *exported = is_exported(name, *value, mod);
  2314. preempt_enable();
  2315. return 0;
  2316. }
  2317. symnum -= mod->num_symtab;
  2318. }
  2319. preempt_enable();
  2320. return -ERANGE;
  2321. }
  2322. static unsigned long mod_find_symname(struct module *mod, const char *name)
  2323. {
  2324. unsigned int i;
  2325. for (i = 0; i < mod->num_symtab; i++)
  2326. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  2327. mod->symtab[i].st_info != 'U')
  2328. return mod->symtab[i].st_value;
  2329. return 0;
  2330. }
  2331. /* Look for this name: can be of form module:name. */
  2332. unsigned long module_kallsyms_lookup_name(const char *name)
  2333. {
  2334. struct module *mod;
  2335. char *colon;
  2336. unsigned long ret = 0;
  2337. /* Don't lock: we're in enough trouble already. */
  2338. preempt_disable();
  2339. if ((colon = strchr(name, ':')) != NULL) {
  2340. *colon = '\0';
  2341. if ((mod = find_module(name)) != NULL)
  2342. ret = mod_find_symname(mod, colon+1);
  2343. *colon = ':';
  2344. } else {
  2345. list_for_each_entry_rcu(mod, &modules, list)
  2346. if ((ret = mod_find_symname(mod, name)) != 0)
  2347. break;
  2348. }
  2349. preempt_enable();
  2350. return ret;
  2351. }
  2352. int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
  2353. struct module *, unsigned long),
  2354. void *data)
  2355. {
  2356. struct module *mod;
  2357. unsigned int i;
  2358. int ret;
  2359. list_for_each_entry(mod, &modules, list) {
  2360. for (i = 0; i < mod->num_symtab; i++) {
  2361. ret = fn(data, mod->strtab + mod->symtab[i].st_name,
  2362. mod, mod->symtab[i].st_value);
  2363. if (ret != 0)
  2364. return ret;
  2365. }
  2366. }
  2367. return 0;
  2368. }
  2369. #endif /* CONFIG_KALLSYMS */
  2370. static char *module_flags(struct module *mod, char *buf)
  2371. {
  2372. int bx = 0;
  2373. if (mod->taints ||
  2374. mod->state == MODULE_STATE_GOING ||
  2375. mod->state == MODULE_STATE_COMING) {
  2376. buf[bx++] = '(';
  2377. if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
  2378. buf[bx++] = 'P';
  2379. if (mod->taints & (1 << TAINT_FORCED_MODULE))
  2380. buf[bx++] = 'F';
  2381. if (mod->taints & (1 << TAINT_CRAP))
  2382. buf[bx++] = 'C';
  2383. /*
  2384. * TAINT_FORCED_RMMOD: could be added.
  2385. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  2386. * apply to modules.
  2387. */
  2388. /* Show a - for module-is-being-unloaded */
  2389. if (mod->state == MODULE_STATE_GOING)
  2390. buf[bx++] = '-';
  2391. /* Show a + for module-is-being-loaded */
  2392. if (mod->state == MODULE_STATE_COMING)
  2393. buf[bx++] = '+';
  2394. buf[bx++] = ')';
  2395. }
  2396. buf[bx] = '\0';
  2397. return buf;
  2398. }
  2399. #ifdef CONFIG_PROC_FS
  2400. /* Called by the /proc file system to return a list of modules. */
  2401. static void *m_start(struct seq_file *m, loff_t *pos)
  2402. {
  2403. mutex_lock(&module_mutex);
  2404. return seq_list_start(&modules, *pos);
  2405. }
  2406. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  2407. {
  2408. return seq_list_next(p, &modules, pos);
  2409. }
  2410. static void m_stop(struct seq_file *m, void *p)
  2411. {
  2412. mutex_unlock(&module_mutex);
  2413. }
  2414. static int m_show(struct seq_file *m, void *p)
  2415. {
  2416. struct module *mod = list_entry(p, struct module, list);
  2417. char buf[8];
  2418. seq_printf(m, "%s %u",
  2419. mod->name, mod->init_size + mod->core_size);
  2420. print_unload_info(m, mod);
  2421. /* Informative for users. */
  2422. seq_printf(m, " %s",
  2423. mod->state == MODULE_STATE_GOING ? "Unloading":
  2424. mod->state == MODULE_STATE_COMING ? "Loading":
  2425. "Live");
  2426. /* Used by oprofile and other similar tools. */
  2427. seq_printf(m, " 0x%p", mod->module_core);
  2428. /* Taints info */
  2429. if (mod->taints)
  2430. seq_printf(m, " %s", module_flags(mod, buf));
  2431. seq_printf(m, "\n");
  2432. return 0;
  2433. }
  2434. /* Format: modulename size refcount deps address
  2435. Where refcount is a number or -, and deps is a comma-separated list
  2436. of depends or -.
  2437. */
  2438. static const struct seq_operations modules_op = {
  2439. .start = m_start,
  2440. .next = m_next,
  2441. .stop = m_stop,
  2442. .show = m_show
  2443. };
  2444. static int modules_open(struct inode *inode, struct file *file)
  2445. {
  2446. return seq_open(file, &modules_op);
  2447. }
  2448. static const struct file_operations proc_modules_operations = {
  2449. .open = modules_open,
  2450. .read = seq_read,
  2451. .llseek = seq_lseek,
  2452. .release = seq_release,
  2453. };
  2454. static int __init proc_modules_init(void)
  2455. {
  2456. proc_create("modules", 0, NULL, &proc_modules_operations);
  2457. return 0;
  2458. }
  2459. module_init(proc_modules_init);
  2460. #endif
  2461. /* Given an address, look for it in the module exception tables. */
  2462. const struct exception_table_entry *search_module_extables(unsigned long addr)
  2463. {
  2464. const struct exception_table_entry *e = NULL;
  2465. struct module *mod;
  2466. preempt_disable();
  2467. list_for_each_entry_rcu(mod, &modules, list) {
  2468. if (mod->num_exentries == 0)
  2469. continue;
  2470. e = search_extable(mod->extable,
  2471. mod->extable + mod->num_exentries - 1,
  2472. addr);
  2473. if (e)
  2474. break;
  2475. }
  2476. preempt_enable();
  2477. /* Now, if we found one, we are running inside it now, hence
  2478. we cannot unload the module, hence no refcnt needed. */
  2479. return e;
  2480. }
  2481. /*
  2482. * is_module_address - is this address inside a module?
  2483. * @addr: the address to check.
  2484. *
  2485. * See is_module_text_address() if you simply want to see if the address
  2486. * is code (not data).
  2487. */
  2488. bool is_module_address(unsigned long addr)
  2489. {
  2490. bool ret;
  2491. preempt_disable();
  2492. ret = __module_address(addr) != NULL;
  2493. preempt_enable();
  2494. return ret;
  2495. }
  2496. /*
  2497. * __module_address - get the module which contains an address.
  2498. * @addr: the address.
  2499. *
  2500. * Must be called with preempt disabled or module mutex held so that
  2501. * module doesn't get freed during this.
  2502. */
  2503. struct module *__module_address(unsigned long addr)
  2504. {
  2505. struct module *mod;
  2506. if (addr < module_addr_min || addr > module_addr_max)
  2507. return NULL;
  2508. list_for_each_entry_rcu(mod, &modules, list)
  2509. if (within_module_core(addr, mod)
  2510. || within_module_init(addr, mod))
  2511. return mod;
  2512. return NULL;
  2513. }
  2514. EXPORT_SYMBOL_GPL(__module_address);
  2515. /*
  2516. * is_module_text_address - is this address inside module code?
  2517. * @addr: the address to check.
  2518. *
  2519. * See is_module_address() if you simply want to see if the address is
  2520. * anywhere in a module. See kernel_text_address() for testing if an
  2521. * address corresponds to kernel or module code.
  2522. */
  2523. bool is_module_text_address(unsigned long addr)
  2524. {
  2525. bool ret;
  2526. preempt_disable();
  2527. ret = __module_text_address(addr) != NULL;
  2528. preempt_enable();
  2529. return ret;
  2530. }
  2531. /*
  2532. * __module_text_address - get the module whose code contains an address.
  2533. * @addr: the address.
  2534. *
  2535. * Must be called with preempt disabled or module mutex held so that
  2536. * module doesn't get freed during this.
  2537. */
  2538. struct module *__module_text_address(unsigned long addr)
  2539. {
  2540. struct module *mod = __module_address(addr);
  2541. if (mod) {
  2542. /* Make sure it's within the text section. */
  2543. if (!within(addr, mod->module_init, mod->init_text_size)
  2544. && !within(addr, mod->module_core, mod->core_text_size))
  2545. mod = NULL;
  2546. }
  2547. return mod;
  2548. }
  2549. EXPORT_SYMBOL_GPL(__module_text_address);
  2550. /* Don't grab lock, we're oopsing. */
  2551. void print_modules(void)
  2552. {
  2553. struct module *mod;
  2554. char buf[8];
  2555. printk(KERN_DEFAULT "Modules linked in:");
  2556. /* Most callers should already have preempt disabled, but make sure */
  2557. preempt_disable();
  2558. list_for_each_entry_rcu(mod, &modules, list)
  2559. printk(" %s%s", mod->name, module_flags(mod, buf));
  2560. preempt_enable();
  2561. if (last_unloaded_module[0])
  2562. printk(" [last unloaded: %s]", last_unloaded_module);
  2563. printk("\n");
  2564. }
  2565. #ifdef CONFIG_MODVERSIONS
  2566. /* Generate the signature for all relevant module structures here.
  2567. * If these change, we don't want to try to parse the module. */
  2568. void module_layout(struct module *mod,
  2569. struct modversion_info *ver,
  2570. struct kernel_param *kp,
  2571. struct kernel_symbol *ks,
  2572. struct tracepoint *tp)
  2573. {
  2574. }
  2575. EXPORT_SYMBOL(module_layout);
  2576. #endif
  2577. #ifdef CONFIG_TRACEPOINTS
  2578. void module_update_tracepoints(void)
  2579. {
  2580. struct module *mod;
  2581. mutex_lock(&module_mutex);
  2582. list_for_each_entry(mod, &modules, list)
  2583. if (!mod->taints)
  2584. tracepoint_update_probe_range(mod->tracepoints,
  2585. mod->tracepoints + mod->num_tracepoints);
  2586. mutex_unlock(&module_mutex);
  2587. }
  2588. /*
  2589. * Returns 0 if current not found.
  2590. * Returns 1 if current found.
  2591. */
  2592. int module_get_iter_tracepoints(struct tracepoint_iter *iter)
  2593. {
  2594. struct module *iter_mod;
  2595. int found = 0;
  2596. mutex_lock(&module_mutex);
  2597. list_for_each_entry(iter_mod, &modules, list) {
  2598. if (!iter_mod->taints) {
  2599. /*
  2600. * Sorted module list
  2601. */
  2602. if (iter_mod < iter->module)
  2603. continue;
  2604. else if (iter_mod > iter->module)
  2605. iter->tracepoint = NULL;
  2606. found = tracepoint_get_iter_range(&iter->tracepoint,
  2607. iter_mod->tracepoints,
  2608. iter_mod->tracepoints
  2609. + iter_mod->num_tracepoints);
  2610. if (found) {
  2611. iter->module = iter_mod;
  2612. break;
  2613. }
  2614. }
  2615. }
  2616. mutex_unlock(&module_mutex);
  2617. return found;
  2618. }
  2619. #endif