module.c 77 KB

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