module.c 79 KB

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