module.c 77 KB

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