module.c 78 KB

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