module.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  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/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/elf.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/capability.h>
  28. #include <linux/cpu.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/errno.h>
  31. #include <linux/err.h>
  32. #include <linux/vermagic.h>
  33. #include <linux/notifier.h>
  34. #include <linux/sched.h>
  35. #include <linux/stop_machine.h>
  36. #include <linux/device.h>
  37. #include <linux/string.h>
  38. #include <linux/mutex.h>
  39. #include <linux/unwind.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/semaphore.h>
  42. #include <asm/cacheflush.h>
  43. #include <linux/license.h>
  44. #if 0
  45. #define DEBUGP printk
  46. #else
  47. #define DEBUGP(fmt , a...)
  48. #endif
  49. #ifndef ARCH_SHF_SMALL
  50. #define ARCH_SHF_SMALL 0
  51. #endif
  52. /* If this is set, the section belongs in the init part of the module */
  53. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  54. /* Protects module list */
  55. static DEFINE_SPINLOCK(modlist_lock);
  56. /* List of modules, protected by module_mutex AND modlist_lock */
  57. static DEFINE_MUTEX(module_mutex);
  58. static LIST_HEAD(modules);
  59. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  60. int register_module_notifier(struct notifier_block * nb)
  61. {
  62. return blocking_notifier_chain_register(&module_notify_list, nb);
  63. }
  64. EXPORT_SYMBOL(register_module_notifier);
  65. int unregister_module_notifier(struct notifier_block * nb)
  66. {
  67. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  68. }
  69. EXPORT_SYMBOL(unregister_module_notifier);
  70. /* We require a truly strong try_module_get() */
  71. static inline int strong_try_module_get(struct module *mod)
  72. {
  73. if (mod && mod->state == MODULE_STATE_COMING)
  74. return 0;
  75. return try_module_get(mod);
  76. }
  77. static inline void add_taint_module(struct module *mod, unsigned flag)
  78. {
  79. add_taint(flag);
  80. mod->taints |= flag;
  81. }
  82. /* A thread that wants to hold a reference to a module only while it
  83. * is running can call ths to safely exit.
  84. * nfsd and lockd use this.
  85. */
  86. void __module_put_and_exit(struct module *mod, long code)
  87. {
  88. module_put(mod);
  89. do_exit(code);
  90. }
  91. EXPORT_SYMBOL(__module_put_and_exit);
  92. /* Find a module section: 0 means not found. */
  93. static unsigned int find_sec(Elf_Ehdr *hdr,
  94. Elf_Shdr *sechdrs,
  95. const char *secstrings,
  96. const char *name)
  97. {
  98. unsigned int i;
  99. for (i = 1; i < hdr->e_shnum; i++)
  100. /* Alloc bit cleared means "ignore it." */
  101. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  102. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  103. return i;
  104. return 0;
  105. }
  106. /* Provided by the linker */
  107. extern const struct kernel_symbol __start___ksymtab[];
  108. extern const struct kernel_symbol __stop___ksymtab[];
  109. extern const struct kernel_symbol __start___ksymtab_gpl[];
  110. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  111. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  112. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  113. extern const struct kernel_symbol __start___ksymtab_unused[];
  114. extern const struct kernel_symbol __stop___ksymtab_unused[];
  115. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  116. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  117. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  118. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  119. extern const unsigned long __start___kcrctab[];
  120. extern const unsigned long __start___kcrctab_gpl[];
  121. extern const unsigned long __start___kcrctab_gpl_future[];
  122. extern const unsigned long __start___kcrctab_unused[];
  123. extern const unsigned long __start___kcrctab_unused_gpl[];
  124. #ifndef CONFIG_MODVERSIONS
  125. #define symversion(base, idx) NULL
  126. #else
  127. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  128. #endif
  129. /* lookup symbol in given range of kernel_symbols */
  130. static const struct kernel_symbol *lookup_symbol(const char *name,
  131. const struct kernel_symbol *start,
  132. const struct kernel_symbol *stop)
  133. {
  134. const struct kernel_symbol *ks = start;
  135. for (; ks < stop; ks++)
  136. if (strcmp(ks->name, name) == 0)
  137. return ks;
  138. return NULL;
  139. }
  140. static void printk_unused_warning(const char *name)
  141. {
  142. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  143. "however this module is using it.\n", name);
  144. printk(KERN_WARNING "This symbol will go away in the future.\n");
  145. printk(KERN_WARNING "Please evalute if this is the right api to use, "
  146. "and if it really is, submit a report the linux kernel "
  147. "mailinglist together with submitting your code for "
  148. "inclusion.\n");
  149. }
  150. /* Find a symbol, return value, crc and module which owns it */
  151. static unsigned long __find_symbol(const char *name,
  152. struct module **owner,
  153. const unsigned long **crc,
  154. int gplok)
  155. {
  156. struct module *mod;
  157. const struct kernel_symbol *ks;
  158. /* Core kernel first. */
  159. *owner = NULL;
  160. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  161. if (ks) {
  162. *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
  163. return ks->value;
  164. }
  165. if (gplok) {
  166. ks = lookup_symbol(name, __start___ksymtab_gpl,
  167. __stop___ksymtab_gpl);
  168. if (ks) {
  169. *crc = symversion(__start___kcrctab_gpl,
  170. (ks - __start___ksymtab_gpl));
  171. return ks->value;
  172. }
  173. }
  174. ks = lookup_symbol(name, __start___ksymtab_gpl_future,
  175. __stop___ksymtab_gpl_future);
  176. if (ks) {
  177. if (!gplok) {
  178. printk(KERN_WARNING "Symbol %s is being used "
  179. "by a non-GPL module, which will not "
  180. "be allowed in the future\n", name);
  181. printk(KERN_WARNING "Please see the file "
  182. "Documentation/feature-removal-schedule.txt "
  183. "in the kernel source tree for more "
  184. "details.\n");
  185. }
  186. *crc = symversion(__start___kcrctab_gpl_future,
  187. (ks - __start___ksymtab_gpl_future));
  188. return ks->value;
  189. }
  190. ks = lookup_symbol(name, __start___ksymtab_unused,
  191. __stop___ksymtab_unused);
  192. if (ks) {
  193. printk_unused_warning(name);
  194. *crc = symversion(__start___kcrctab_unused,
  195. (ks - __start___ksymtab_unused));
  196. return ks->value;
  197. }
  198. if (gplok)
  199. ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
  200. __stop___ksymtab_unused_gpl);
  201. if (ks) {
  202. printk_unused_warning(name);
  203. *crc = symversion(__start___kcrctab_unused_gpl,
  204. (ks - __start___ksymtab_unused_gpl));
  205. return ks->value;
  206. }
  207. /* Now try modules. */
  208. list_for_each_entry(mod, &modules, list) {
  209. *owner = mod;
  210. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  211. if (ks) {
  212. *crc = symversion(mod->crcs, (ks - mod->syms));
  213. return ks->value;
  214. }
  215. if (gplok) {
  216. ks = lookup_symbol(name, mod->gpl_syms,
  217. mod->gpl_syms + mod->num_gpl_syms);
  218. if (ks) {
  219. *crc = symversion(mod->gpl_crcs,
  220. (ks - mod->gpl_syms));
  221. return ks->value;
  222. }
  223. }
  224. ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
  225. if (ks) {
  226. printk_unused_warning(name);
  227. *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
  228. return ks->value;
  229. }
  230. if (gplok) {
  231. ks = lookup_symbol(name, mod->unused_gpl_syms,
  232. mod->unused_gpl_syms + mod->num_unused_gpl_syms);
  233. if (ks) {
  234. printk_unused_warning(name);
  235. *crc = symversion(mod->unused_gpl_crcs,
  236. (ks - mod->unused_gpl_syms));
  237. return ks->value;
  238. }
  239. }
  240. ks = lookup_symbol(name, mod->gpl_future_syms,
  241. (mod->gpl_future_syms +
  242. mod->num_gpl_future_syms));
  243. if (ks) {
  244. if (!gplok) {
  245. printk(KERN_WARNING "Symbol %s is being used "
  246. "by a non-GPL module, which will not "
  247. "be allowed in the future\n", name);
  248. printk(KERN_WARNING "Please see the file "
  249. "Documentation/feature-removal-schedule.txt "
  250. "in the kernel source tree for more "
  251. "details.\n");
  252. }
  253. *crc = symversion(mod->gpl_future_crcs,
  254. (ks - mod->gpl_future_syms));
  255. return ks->value;
  256. }
  257. }
  258. DEBUGP("Failed to find symbol %s\n", name);
  259. return 0;
  260. }
  261. /* Search for module by name: must hold module_mutex. */
  262. static struct module *find_module(const char *name)
  263. {
  264. struct module *mod;
  265. list_for_each_entry(mod, &modules, list) {
  266. if (strcmp(mod->name, name) == 0)
  267. return mod;
  268. }
  269. return NULL;
  270. }
  271. #ifdef CONFIG_SMP
  272. /* Number of blocks used and allocated. */
  273. static unsigned int pcpu_num_used, pcpu_num_allocated;
  274. /* Size of each block. -ve means used. */
  275. static int *pcpu_size;
  276. static int split_block(unsigned int i, unsigned short size)
  277. {
  278. /* Reallocation required? */
  279. if (pcpu_num_used + 1 > pcpu_num_allocated) {
  280. int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
  281. GFP_KERNEL);
  282. if (!new)
  283. return 0;
  284. memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
  285. pcpu_num_allocated *= 2;
  286. kfree(pcpu_size);
  287. pcpu_size = new;
  288. }
  289. /* Insert a new subblock */
  290. memmove(&pcpu_size[i+1], &pcpu_size[i],
  291. sizeof(pcpu_size[0]) * (pcpu_num_used - i));
  292. pcpu_num_used++;
  293. pcpu_size[i+1] -= size;
  294. pcpu_size[i] = size;
  295. return 1;
  296. }
  297. static inline unsigned int block_size(int val)
  298. {
  299. if (val < 0)
  300. return -val;
  301. return val;
  302. }
  303. /* Created by linker magic */
  304. extern char __per_cpu_start[], __per_cpu_end[];
  305. static void *percpu_modalloc(unsigned long size, unsigned long align,
  306. const char *name)
  307. {
  308. unsigned long extra;
  309. unsigned int i;
  310. void *ptr;
  311. if (align > SMP_CACHE_BYTES) {
  312. printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
  313. name, align, SMP_CACHE_BYTES);
  314. align = SMP_CACHE_BYTES;
  315. }
  316. ptr = __per_cpu_start;
  317. for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  318. /* Extra for alignment requirement. */
  319. extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
  320. BUG_ON(i == 0 && extra != 0);
  321. if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
  322. continue;
  323. /* Transfer extra to previous block. */
  324. if (pcpu_size[i-1] < 0)
  325. pcpu_size[i-1] -= extra;
  326. else
  327. pcpu_size[i-1] += extra;
  328. pcpu_size[i] -= extra;
  329. ptr += extra;
  330. /* Split block if warranted */
  331. if (pcpu_size[i] - size > sizeof(unsigned long))
  332. if (!split_block(i, size))
  333. return NULL;
  334. /* Mark allocated */
  335. pcpu_size[i] = -pcpu_size[i];
  336. return ptr;
  337. }
  338. printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
  339. size);
  340. return NULL;
  341. }
  342. static void percpu_modfree(void *freeme)
  343. {
  344. unsigned int i;
  345. void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
  346. /* First entry is core kernel percpu data. */
  347. for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  348. if (ptr == freeme) {
  349. pcpu_size[i] = -pcpu_size[i];
  350. goto free;
  351. }
  352. }
  353. BUG();
  354. free:
  355. /* Merge with previous? */
  356. if (pcpu_size[i-1] >= 0) {
  357. pcpu_size[i-1] += pcpu_size[i];
  358. pcpu_num_used--;
  359. memmove(&pcpu_size[i], &pcpu_size[i+1],
  360. (pcpu_num_used - i) * sizeof(pcpu_size[0]));
  361. i--;
  362. }
  363. /* Merge with next? */
  364. if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
  365. pcpu_size[i] += pcpu_size[i+1];
  366. pcpu_num_used--;
  367. memmove(&pcpu_size[i+1], &pcpu_size[i+2],
  368. (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
  369. }
  370. }
  371. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  372. Elf_Shdr *sechdrs,
  373. const char *secstrings)
  374. {
  375. return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
  376. }
  377. static int percpu_modinit(void)
  378. {
  379. pcpu_num_used = 2;
  380. pcpu_num_allocated = 2;
  381. pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
  382. GFP_KERNEL);
  383. /* Static in-kernel percpu data (used). */
  384. pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
  385. /* Free room. */
  386. pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
  387. if (pcpu_size[1] < 0) {
  388. printk(KERN_ERR "No per-cpu room for modules.\n");
  389. pcpu_num_used = 1;
  390. }
  391. return 0;
  392. }
  393. __initcall(percpu_modinit);
  394. #else /* ... !CONFIG_SMP */
  395. static inline void *percpu_modalloc(unsigned long size, unsigned long align,
  396. const char *name)
  397. {
  398. return NULL;
  399. }
  400. static inline void percpu_modfree(void *pcpuptr)
  401. {
  402. BUG();
  403. }
  404. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  405. Elf_Shdr *sechdrs,
  406. const char *secstrings)
  407. {
  408. return 0;
  409. }
  410. static inline void percpu_modcopy(void *pcpudst, const void *src,
  411. unsigned long size)
  412. {
  413. /* pcpusec should be 0, and size of that section should be 0. */
  414. BUG_ON(size != 0);
  415. }
  416. #endif /* CONFIG_SMP */
  417. #define MODINFO_ATTR(field) \
  418. static void setup_modinfo_##field(struct module *mod, const char *s) \
  419. { \
  420. mod->field = kstrdup(s, GFP_KERNEL); \
  421. } \
  422. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  423. struct module *mod, char *buffer) \
  424. { \
  425. return sprintf(buffer, "%s\n", mod->field); \
  426. } \
  427. static int modinfo_##field##_exists(struct module *mod) \
  428. { \
  429. return mod->field != NULL; \
  430. } \
  431. static void free_modinfo_##field(struct module *mod) \
  432. { \
  433. kfree(mod->field); \
  434. mod->field = NULL; \
  435. } \
  436. static struct module_attribute modinfo_##field = { \
  437. .attr = { .name = __stringify(field), .mode = 0444, \
  438. .owner = THIS_MODULE }, \
  439. .show = show_modinfo_##field, \
  440. .setup = setup_modinfo_##field, \
  441. .test = modinfo_##field##_exists, \
  442. .free = free_modinfo_##field, \
  443. };
  444. MODINFO_ATTR(version);
  445. MODINFO_ATTR(srcversion);
  446. #ifdef CONFIG_MODULE_UNLOAD
  447. /* Init the unload section of the module. */
  448. static void module_unload_init(struct module *mod)
  449. {
  450. unsigned int i;
  451. INIT_LIST_HEAD(&mod->modules_which_use_me);
  452. for (i = 0; i < NR_CPUS; i++)
  453. local_set(&mod->ref[i].count, 0);
  454. /* Hold reference count during initialization. */
  455. local_set(&mod->ref[raw_smp_processor_id()].count, 1);
  456. /* Backwards compatibility macros put refcount during init. */
  457. mod->waiter = current;
  458. }
  459. /* modules using other modules */
  460. struct module_use
  461. {
  462. struct list_head list;
  463. struct module *module_which_uses;
  464. };
  465. /* Does a already use b? */
  466. static int already_uses(struct module *a, struct module *b)
  467. {
  468. struct module_use *use;
  469. list_for_each_entry(use, &b->modules_which_use_me, list) {
  470. if (use->module_which_uses == a) {
  471. DEBUGP("%s uses %s!\n", a->name, b->name);
  472. return 1;
  473. }
  474. }
  475. DEBUGP("%s does not use %s!\n", a->name, b->name);
  476. return 0;
  477. }
  478. /* Module a uses b */
  479. static int use_module(struct module *a, struct module *b)
  480. {
  481. struct module_use *use;
  482. if (b == NULL || already_uses(a, b)) return 1;
  483. if (!strong_try_module_get(b))
  484. return 0;
  485. DEBUGP("Allocating new usage for %s.\n", a->name);
  486. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  487. if (!use) {
  488. printk("%s: out of memory loading\n", a->name);
  489. module_put(b);
  490. return 0;
  491. }
  492. use->module_which_uses = a;
  493. list_add(&use->list, &b->modules_which_use_me);
  494. return 1;
  495. }
  496. /* Clear the unload stuff of the module. */
  497. static void module_unload_free(struct module *mod)
  498. {
  499. struct module *i;
  500. list_for_each_entry(i, &modules, list) {
  501. struct module_use *use;
  502. list_for_each_entry(use, &i->modules_which_use_me, list) {
  503. if (use->module_which_uses == mod) {
  504. DEBUGP("%s unusing %s\n", mod->name, i->name);
  505. module_put(i);
  506. list_del(&use->list);
  507. kfree(use);
  508. /* There can be at most one match. */
  509. break;
  510. }
  511. }
  512. }
  513. }
  514. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  515. static inline int try_force_unload(unsigned int flags)
  516. {
  517. int ret = (flags & O_TRUNC);
  518. if (ret)
  519. add_taint(TAINT_FORCED_RMMOD);
  520. return ret;
  521. }
  522. #else
  523. static inline int try_force_unload(unsigned int flags)
  524. {
  525. return 0;
  526. }
  527. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  528. struct stopref
  529. {
  530. struct module *mod;
  531. int flags;
  532. int *forced;
  533. };
  534. /* Whole machine is stopped with interrupts off when this runs. */
  535. static int __try_stop_module(void *_sref)
  536. {
  537. struct stopref *sref = _sref;
  538. /* If it's not unused, quit unless we are told to block. */
  539. if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
  540. if (!(*sref->forced = try_force_unload(sref->flags)))
  541. return -EWOULDBLOCK;
  542. }
  543. /* Mark it as dying. */
  544. sref->mod->state = MODULE_STATE_GOING;
  545. return 0;
  546. }
  547. static int try_stop_module(struct module *mod, int flags, int *forced)
  548. {
  549. struct stopref sref = { mod, flags, forced };
  550. return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
  551. }
  552. unsigned int module_refcount(struct module *mod)
  553. {
  554. unsigned int i, total = 0;
  555. for (i = 0; i < NR_CPUS; i++)
  556. total += local_read(&mod->ref[i].count);
  557. return total;
  558. }
  559. EXPORT_SYMBOL(module_refcount);
  560. /* This exists whether we can unload or not */
  561. static void free_module(struct module *mod);
  562. static void wait_for_zero_refcount(struct module *mod)
  563. {
  564. /* Since we might sleep for some time, drop the semaphore first */
  565. mutex_unlock(&module_mutex);
  566. for (;;) {
  567. DEBUGP("Looking at refcount...\n");
  568. set_current_state(TASK_UNINTERRUPTIBLE);
  569. if (module_refcount(mod) == 0)
  570. break;
  571. schedule();
  572. }
  573. current->state = TASK_RUNNING;
  574. mutex_lock(&module_mutex);
  575. }
  576. asmlinkage long
  577. sys_delete_module(const char __user *name_user, unsigned int flags)
  578. {
  579. struct module *mod;
  580. char name[MODULE_NAME_LEN];
  581. int ret, forced = 0;
  582. if (!capable(CAP_SYS_MODULE))
  583. return -EPERM;
  584. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  585. return -EFAULT;
  586. name[MODULE_NAME_LEN-1] = '\0';
  587. if (mutex_lock_interruptible(&module_mutex) != 0)
  588. return -EINTR;
  589. mod = find_module(name);
  590. if (!mod) {
  591. ret = -ENOENT;
  592. goto out;
  593. }
  594. if (!list_empty(&mod->modules_which_use_me)) {
  595. /* Other modules depend on us: get rid of them first. */
  596. ret = -EWOULDBLOCK;
  597. goto out;
  598. }
  599. /* Doing init or already dying? */
  600. if (mod->state != MODULE_STATE_LIVE) {
  601. /* FIXME: if (force), slam module count and wake up
  602. waiter --RR */
  603. DEBUGP("%s already dying\n", mod->name);
  604. ret = -EBUSY;
  605. goto out;
  606. }
  607. /* If it has an init func, it must have an exit func to unload */
  608. if ((mod->init != NULL && mod->exit == NULL)
  609. || mod->unsafe) {
  610. forced = try_force_unload(flags);
  611. if (!forced) {
  612. /* This module can't be removed */
  613. ret = -EBUSY;
  614. goto out;
  615. }
  616. }
  617. /* Set this up before setting mod->state */
  618. mod->waiter = current;
  619. /* Stop the machine so refcounts can't move and disable module. */
  620. ret = try_stop_module(mod, flags, &forced);
  621. if (ret != 0)
  622. goto out;
  623. /* Never wait if forced. */
  624. if (!forced && module_refcount(mod) != 0)
  625. wait_for_zero_refcount(mod);
  626. /* Final destruction now noone is using it. */
  627. if (mod->exit != NULL) {
  628. mutex_unlock(&module_mutex);
  629. mod->exit();
  630. mutex_lock(&module_mutex);
  631. }
  632. free_module(mod);
  633. out:
  634. mutex_unlock(&module_mutex);
  635. return ret;
  636. }
  637. static void print_unload_info(struct seq_file *m, struct module *mod)
  638. {
  639. struct module_use *use;
  640. int printed_something = 0;
  641. seq_printf(m, " %u ", module_refcount(mod));
  642. /* Always include a trailing , so userspace can differentiate
  643. between this and the old multi-field proc format. */
  644. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  645. printed_something = 1;
  646. seq_printf(m, "%s,", use->module_which_uses->name);
  647. }
  648. if (mod->unsafe) {
  649. printed_something = 1;
  650. seq_printf(m, "[unsafe],");
  651. }
  652. if (mod->init != NULL && mod->exit == NULL) {
  653. printed_something = 1;
  654. seq_printf(m, "[permanent],");
  655. }
  656. if (!printed_something)
  657. seq_printf(m, "-");
  658. }
  659. void __symbol_put(const char *symbol)
  660. {
  661. struct module *owner;
  662. unsigned long flags;
  663. const unsigned long *crc;
  664. spin_lock_irqsave(&modlist_lock, flags);
  665. if (!__find_symbol(symbol, &owner, &crc, 1))
  666. BUG();
  667. module_put(owner);
  668. spin_unlock_irqrestore(&modlist_lock, flags);
  669. }
  670. EXPORT_SYMBOL(__symbol_put);
  671. void symbol_put_addr(void *addr)
  672. {
  673. struct module *modaddr;
  674. if (core_kernel_text((unsigned long)addr))
  675. return;
  676. if (!(modaddr = module_text_address((unsigned long)addr)))
  677. BUG();
  678. module_put(modaddr);
  679. }
  680. EXPORT_SYMBOL_GPL(symbol_put_addr);
  681. static ssize_t show_refcnt(struct module_attribute *mattr,
  682. struct module *mod, char *buffer)
  683. {
  684. /* sysfs holds a reference */
  685. return sprintf(buffer, "%u\n", module_refcount(mod)-1);
  686. }
  687. static struct module_attribute refcnt = {
  688. .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
  689. .show = show_refcnt,
  690. };
  691. void module_put(struct module *module)
  692. {
  693. if (module) {
  694. unsigned int cpu = get_cpu();
  695. local_dec(&module->ref[cpu].count);
  696. /* Maybe they're waiting for us to drop reference? */
  697. if (unlikely(!module_is_live(module)))
  698. wake_up_process(module->waiter);
  699. put_cpu();
  700. }
  701. }
  702. EXPORT_SYMBOL(module_put);
  703. #else /* !CONFIG_MODULE_UNLOAD */
  704. static void print_unload_info(struct seq_file *m, struct module *mod)
  705. {
  706. /* We don't know the usage count, or what modules are using. */
  707. seq_printf(m, " - -");
  708. }
  709. static inline void module_unload_free(struct module *mod)
  710. {
  711. }
  712. static inline int use_module(struct module *a, struct module *b)
  713. {
  714. return strong_try_module_get(b);
  715. }
  716. static inline void module_unload_init(struct module *mod)
  717. {
  718. }
  719. #endif /* CONFIG_MODULE_UNLOAD */
  720. static struct module_attribute *modinfo_attrs[] = {
  721. &modinfo_version,
  722. &modinfo_srcversion,
  723. #ifdef CONFIG_MODULE_UNLOAD
  724. &refcnt,
  725. #endif
  726. NULL,
  727. };
  728. static const char vermagic[] = VERMAGIC_STRING;
  729. #ifdef CONFIG_MODVERSIONS
  730. static int check_version(Elf_Shdr *sechdrs,
  731. unsigned int versindex,
  732. const char *symname,
  733. struct module *mod,
  734. const unsigned long *crc)
  735. {
  736. unsigned int i, num_versions;
  737. struct modversion_info *versions;
  738. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  739. if (!crc)
  740. return 1;
  741. versions = (void *) sechdrs[versindex].sh_addr;
  742. num_versions = sechdrs[versindex].sh_size
  743. / sizeof(struct modversion_info);
  744. for (i = 0; i < num_versions; i++) {
  745. if (strcmp(versions[i].name, symname) != 0)
  746. continue;
  747. if (versions[i].crc == *crc)
  748. return 1;
  749. printk("%s: disagrees about version of symbol %s\n",
  750. mod->name, symname);
  751. DEBUGP("Found checksum %lX vs module %lX\n",
  752. *crc, versions[i].crc);
  753. return 0;
  754. }
  755. /* Not in module's version table. OK, but that taints the kernel. */
  756. if (!(tainted & TAINT_FORCED_MODULE))
  757. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  758. mod->name, symname);
  759. add_taint_module(mod, TAINT_FORCED_MODULE);
  760. return 1;
  761. }
  762. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  763. unsigned int versindex,
  764. struct module *mod)
  765. {
  766. const unsigned long *crc;
  767. struct module *owner;
  768. if (!__find_symbol("struct_module", &owner, &crc, 1))
  769. BUG();
  770. return check_version(sechdrs, versindex, "struct_module", mod,
  771. crc);
  772. }
  773. /* First part is kernel version, which we ignore. */
  774. static inline int same_magic(const char *amagic, const char *bmagic)
  775. {
  776. amagic += strcspn(amagic, " ");
  777. bmagic += strcspn(bmagic, " ");
  778. return strcmp(amagic, bmagic) == 0;
  779. }
  780. #else
  781. static inline int check_version(Elf_Shdr *sechdrs,
  782. unsigned int versindex,
  783. const char *symname,
  784. struct module *mod,
  785. const unsigned long *crc)
  786. {
  787. return 1;
  788. }
  789. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  790. unsigned int versindex,
  791. struct module *mod)
  792. {
  793. return 1;
  794. }
  795. static inline int same_magic(const char *amagic, const char *bmagic)
  796. {
  797. return strcmp(amagic, bmagic) == 0;
  798. }
  799. #endif /* CONFIG_MODVERSIONS */
  800. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  801. Must be holding module_mutex. */
  802. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  803. unsigned int versindex,
  804. const char *name,
  805. struct module *mod)
  806. {
  807. struct module *owner;
  808. unsigned long ret;
  809. const unsigned long *crc;
  810. ret = __find_symbol(name, &owner, &crc,
  811. !(mod->taints & TAINT_PROPRIETARY_MODULE));
  812. if (ret) {
  813. /* use_module can fail due to OOM, or module unloading */
  814. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  815. !use_module(mod, owner))
  816. ret = 0;
  817. }
  818. return ret;
  819. }
  820. /*
  821. * /sys/module/foo/sections stuff
  822. * J. Corbet <corbet@lwn.net>
  823. */
  824. #ifdef CONFIG_KALLSYMS
  825. static ssize_t module_sect_show(struct module_attribute *mattr,
  826. struct module *mod, char *buf)
  827. {
  828. struct module_sect_attr *sattr =
  829. container_of(mattr, struct module_sect_attr, mattr);
  830. return sprintf(buf, "0x%lx\n", sattr->address);
  831. }
  832. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  833. {
  834. int section;
  835. for (section = 0; section < sect_attrs->nsections; section++)
  836. kfree(sect_attrs->attrs[section].name);
  837. kfree(sect_attrs);
  838. }
  839. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  840. char *secstrings, Elf_Shdr *sechdrs)
  841. {
  842. unsigned int nloaded = 0, i, size[2];
  843. struct module_sect_attrs *sect_attrs;
  844. struct module_sect_attr *sattr;
  845. struct attribute **gattr;
  846. /* Count loaded sections and allocate structures */
  847. for (i = 0; i < nsect; i++)
  848. if (sechdrs[i].sh_flags & SHF_ALLOC)
  849. nloaded++;
  850. size[0] = ALIGN(sizeof(*sect_attrs)
  851. + nloaded * sizeof(sect_attrs->attrs[0]),
  852. sizeof(sect_attrs->grp.attrs[0]));
  853. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  854. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  855. if (sect_attrs == NULL)
  856. return;
  857. /* Setup section attributes. */
  858. sect_attrs->grp.name = "sections";
  859. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  860. sect_attrs->nsections = 0;
  861. sattr = &sect_attrs->attrs[0];
  862. gattr = &sect_attrs->grp.attrs[0];
  863. for (i = 0; i < nsect; i++) {
  864. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  865. continue;
  866. sattr->address = sechdrs[i].sh_addr;
  867. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  868. GFP_KERNEL);
  869. if (sattr->name == NULL)
  870. goto out;
  871. sect_attrs->nsections++;
  872. sattr->mattr.show = module_sect_show;
  873. sattr->mattr.store = NULL;
  874. sattr->mattr.attr.name = sattr->name;
  875. sattr->mattr.attr.owner = mod;
  876. sattr->mattr.attr.mode = S_IRUGO;
  877. *(gattr++) = &(sattr++)->mattr.attr;
  878. }
  879. *gattr = NULL;
  880. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  881. goto out;
  882. mod->sect_attrs = sect_attrs;
  883. return;
  884. out:
  885. free_sect_attrs(sect_attrs);
  886. }
  887. static void remove_sect_attrs(struct module *mod)
  888. {
  889. if (mod->sect_attrs) {
  890. sysfs_remove_group(&mod->mkobj.kobj,
  891. &mod->sect_attrs->grp);
  892. /* We are positive that no one is using any sect attrs
  893. * at this point. Deallocate immediately. */
  894. free_sect_attrs(mod->sect_attrs);
  895. mod->sect_attrs = NULL;
  896. }
  897. }
  898. #else
  899. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  900. char *sectstrings, Elf_Shdr *sechdrs)
  901. {
  902. }
  903. static inline void remove_sect_attrs(struct module *mod)
  904. {
  905. }
  906. #endif /* CONFIG_KALLSYMS */
  907. static int module_add_modinfo_attrs(struct module *mod)
  908. {
  909. struct module_attribute *attr;
  910. struct module_attribute *temp_attr;
  911. int error = 0;
  912. int i;
  913. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  914. (ARRAY_SIZE(modinfo_attrs) + 1)),
  915. GFP_KERNEL);
  916. if (!mod->modinfo_attrs)
  917. return -ENOMEM;
  918. temp_attr = mod->modinfo_attrs;
  919. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  920. if (!attr->test ||
  921. (attr->test && attr->test(mod))) {
  922. memcpy(temp_attr, attr, sizeof(*temp_attr));
  923. temp_attr->attr.owner = mod;
  924. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  925. ++temp_attr;
  926. }
  927. }
  928. return error;
  929. }
  930. static void module_remove_modinfo_attrs(struct module *mod)
  931. {
  932. struct module_attribute *attr;
  933. int i;
  934. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  935. /* pick a field to test for end of list */
  936. if (!attr->attr.name)
  937. break;
  938. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  939. if (attr->free)
  940. attr->free(mod);
  941. }
  942. kfree(mod->modinfo_attrs);
  943. }
  944. static int mod_sysfs_setup(struct module *mod,
  945. struct kernel_param *kparam,
  946. unsigned int num_params)
  947. {
  948. int err;
  949. if (!module_subsys.kset.subsys) {
  950. printk(KERN_ERR "%s: module_subsys not initialized\n",
  951. mod->name);
  952. err = -EINVAL;
  953. goto out;
  954. }
  955. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  956. err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
  957. if (err)
  958. goto out;
  959. kobj_set_kset_s(&mod->mkobj, module_subsys);
  960. mod->mkobj.mod = mod;
  961. /* delay uevent until full sysfs population */
  962. kobject_init(&mod->mkobj.kobj);
  963. err = kobject_add(&mod->mkobj.kobj);
  964. if (err)
  965. goto out;
  966. mod->drivers_dir = kobject_add_dir(&mod->mkobj.kobj, "drivers");
  967. if (!mod->drivers_dir)
  968. goto out_unreg;
  969. err = module_param_sysfs_setup(mod, kparam, num_params);
  970. if (err)
  971. goto out_unreg_drivers;
  972. err = module_add_modinfo_attrs(mod);
  973. if (err)
  974. goto out_unreg_param;
  975. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  976. return 0;
  977. out_unreg_drivers:
  978. kobject_unregister(mod->drivers_dir);
  979. out_unreg_param:
  980. module_param_sysfs_remove(mod);
  981. out_unreg:
  982. kobject_del(&mod->mkobj.kobj);
  983. kobject_put(&mod->mkobj.kobj);
  984. out:
  985. return err;
  986. }
  987. static void mod_kobject_remove(struct module *mod)
  988. {
  989. module_remove_modinfo_attrs(mod);
  990. module_param_sysfs_remove(mod);
  991. kobject_unregister(mod->drivers_dir);
  992. kobject_unregister(&mod->mkobj.kobj);
  993. }
  994. /*
  995. * unlink the module with the whole machine is stopped with interrupts off
  996. * - this defends against kallsyms not taking locks
  997. */
  998. static int __unlink_module(void *_mod)
  999. {
  1000. struct module *mod = _mod;
  1001. list_del(&mod->list);
  1002. return 0;
  1003. }
  1004. /* Free a module, remove from lists, etc (must hold module mutex). */
  1005. static void free_module(struct module *mod)
  1006. {
  1007. /* Delete from various lists */
  1008. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1009. remove_sect_attrs(mod);
  1010. mod_kobject_remove(mod);
  1011. unwind_remove_table(mod->unwind_info, 0);
  1012. /* Arch-specific cleanup. */
  1013. module_arch_cleanup(mod);
  1014. /* Module unload stuff */
  1015. module_unload_free(mod);
  1016. /* This may be NULL, but that's OK */
  1017. module_free(mod, mod->module_init);
  1018. kfree(mod->args);
  1019. if (mod->percpu)
  1020. percpu_modfree(mod->percpu);
  1021. /* Free lock-classes: */
  1022. lockdep_free_key_range(mod->module_core, mod->core_size);
  1023. /* Finally, free the core (containing the module structure) */
  1024. module_free(mod, mod->module_core);
  1025. }
  1026. void *__symbol_get(const char *symbol)
  1027. {
  1028. struct module *owner;
  1029. unsigned long value, flags;
  1030. const unsigned long *crc;
  1031. spin_lock_irqsave(&modlist_lock, flags);
  1032. value = __find_symbol(symbol, &owner, &crc, 1);
  1033. if (value && !strong_try_module_get(owner))
  1034. value = 0;
  1035. spin_unlock_irqrestore(&modlist_lock, flags);
  1036. return (void *)value;
  1037. }
  1038. EXPORT_SYMBOL_GPL(__symbol_get);
  1039. /*
  1040. * Ensure that an exported symbol [global namespace] does not already exist
  1041. * in the Kernel or in some other modules exported symbol table.
  1042. */
  1043. static int verify_export_symbols(struct module *mod)
  1044. {
  1045. const char *name = NULL;
  1046. unsigned long i, ret = 0;
  1047. struct module *owner;
  1048. const unsigned long *crc;
  1049. for (i = 0; i < mod->num_syms; i++)
  1050. if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
  1051. name = mod->syms[i].name;
  1052. ret = -ENOEXEC;
  1053. goto dup;
  1054. }
  1055. for (i = 0; i < mod->num_gpl_syms; i++)
  1056. if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
  1057. name = mod->gpl_syms[i].name;
  1058. ret = -ENOEXEC;
  1059. goto dup;
  1060. }
  1061. dup:
  1062. if (ret)
  1063. printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
  1064. mod->name, name, module_name(owner));
  1065. return ret;
  1066. }
  1067. /* Change all symbols so that sh_value encodes the pointer directly. */
  1068. static int simplify_symbols(Elf_Shdr *sechdrs,
  1069. unsigned int symindex,
  1070. const char *strtab,
  1071. unsigned int versindex,
  1072. unsigned int pcpuindex,
  1073. struct module *mod)
  1074. {
  1075. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1076. unsigned long secbase;
  1077. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1078. int ret = 0;
  1079. for (i = 1; i < n; i++) {
  1080. switch (sym[i].st_shndx) {
  1081. case SHN_COMMON:
  1082. /* We compiled with -fno-common. These are not
  1083. supposed to happen. */
  1084. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1085. printk("%s: please compile with -fno-common\n",
  1086. mod->name);
  1087. ret = -ENOEXEC;
  1088. break;
  1089. case SHN_ABS:
  1090. /* Don't need to do anything */
  1091. DEBUGP("Absolute symbol: 0x%08lx\n",
  1092. (long)sym[i].st_value);
  1093. break;
  1094. case SHN_UNDEF:
  1095. sym[i].st_value
  1096. = resolve_symbol(sechdrs, versindex,
  1097. strtab + sym[i].st_name, mod);
  1098. /* Ok if resolved. */
  1099. if (sym[i].st_value != 0)
  1100. break;
  1101. /* Ok if weak. */
  1102. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1103. break;
  1104. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1105. mod->name, strtab + sym[i].st_name);
  1106. ret = -ENOENT;
  1107. break;
  1108. default:
  1109. /* Divert to percpu allocation if a percpu var. */
  1110. if (sym[i].st_shndx == pcpuindex)
  1111. secbase = (unsigned long)mod->percpu;
  1112. else
  1113. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1114. sym[i].st_value += secbase;
  1115. break;
  1116. }
  1117. }
  1118. return ret;
  1119. }
  1120. /* Update size with this section: return offset. */
  1121. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1122. {
  1123. long ret;
  1124. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1125. *size = ret + sechdr->sh_size;
  1126. return ret;
  1127. }
  1128. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1129. might -- code, read-only data, read-write data, small data. Tally
  1130. sizes, and place the offsets into sh_entsize fields: high bit means it
  1131. belongs in init. */
  1132. static void layout_sections(struct module *mod,
  1133. const Elf_Ehdr *hdr,
  1134. Elf_Shdr *sechdrs,
  1135. const char *secstrings)
  1136. {
  1137. static unsigned long const masks[][2] = {
  1138. /* NOTE: all executable code must be the first section
  1139. * in this array; otherwise modify the text_size
  1140. * finder in the two loops below */
  1141. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1142. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1143. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1144. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1145. };
  1146. unsigned int m, i;
  1147. for (i = 0; i < hdr->e_shnum; i++)
  1148. sechdrs[i].sh_entsize = ~0UL;
  1149. DEBUGP("Core section allocation order:\n");
  1150. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1151. for (i = 0; i < hdr->e_shnum; ++i) {
  1152. Elf_Shdr *s = &sechdrs[i];
  1153. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1154. || (s->sh_flags & masks[m][1])
  1155. || s->sh_entsize != ~0UL
  1156. || strncmp(secstrings + s->sh_name,
  1157. ".init", 5) == 0)
  1158. continue;
  1159. s->sh_entsize = get_offset(&mod->core_size, s);
  1160. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1161. }
  1162. if (m == 0)
  1163. mod->core_text_size = mod->core_size;
  1164. }
  1165. DEBUGP("Init section allocation order:\n");
  1166. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1167. for (i = 0; i < hdr->e_shnum; ++i) {
  1168. Elf_Shdr *s = &sechdrs[i];
  1169. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1170. || (s->sh_flags & masks[m][1])
  1171. || s->sh_entsize != ~0UL
  1172. || strncmp(secstrings + s->sh_name,
  1173. ".init", 5) != 0)
  1174. continue;
  1175. s->sh_entsize = (get_offset(&mod->init_size, s)
  1176. | INIT_OFFSET_MASK);
  1177. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1178. }
  1179. if (m == 0)
  1180. mod->init_text_size = mod->init_size;
  1181. }
  1182. }
  1183. static void set_license(struct module *mod, const char *license)
  1184. {
  1185. if (!license)
  1186. license = "unspecified";
  1187. if (!license_is_gpl_compatible(license)) {
  1188. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1189. printk(KERN_WARNING "%s: module license '%s' taints "
  1190. "kernel.\n", mod->name, license);
  1191. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1192. }
  1193. }
  1194. /* Parse tag=value strings from .modinfo section */
  1195. static char *next_string(char *string, unsigned long *secsize)
  1196. {
  1197. /* Skip non-zero chars */
  1198. while (string[0]) {
  1199. string++;
  1200. if ((*secsize)-- <= 1)
  1201. return NULL;
  1202. }
  1203. /* Skip any zero padding. */
  1204. while (!string[0]) {
  1205. string++;
  1206. if ((*secsize)-- <= 1)
  1207. return NULL;
  1208. }
  1209. return string;
  1210. }
  1211. static char *get_modinfo(Elf_Shdr *sechdrs,
  1212. unsigned int info,
  1213. const char *tag)
  1214. {
  1215. char *p;
  1216. unsigned int taglen = strlen(tag);
  1217. unsigned long size = sechdrs[info].sh_size;
  1218. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1219. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1220. return p + taglen + 1;
  1221. }
  1222. return NULL;
  1223. }
  1224. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1225. unsigned int infoindex)
  1226. {
  1227. struct module_attribute *attr;
  1228. int i;
  1229. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1230. if (attr->setup)
  1231. attr->setup(mod,
  1232. get_modinfo(sechdrs,
  1233. infoindex,
  1234. attr->attr.name));
  1235. }
  1236. }
  1237. #ifdef CONFIG_KALLSYMS
  1238. int is_exported(const char *name, const struct module *mod)
  1239. {
  1240. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1241. return 1;
  1242. else
  1243. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1244. return 1;
  1245. else
  1246. return 0;
  1247. }
  1248. /* As per nm */
  1249. static char elf_type(const Elf_Sym *sym,
  1250. Elf_Shdr *sechdrs,
  1251. const char *secstrings,
  1252. struct module *mod)
  1253. {
  1254. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1255. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1256. return 'v';
  1257. else
  1258. return 'w';
  1259. }
  1260. if (sym->st_shndx == SHN_UNDEF)
  1261. return 'U';
  1262. if (sym->st_shndx == SHN_ABS)
  1263. return 'a';
  1264. if (sym->st_shndx >= SHN_LORESERVE)
  1265. return '?';
  1266. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1267. return 't';
  1268. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1269. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1270. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1271. return 'r';
  1272. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1273. return 'g';
  1274. else
  1275. return 'd';
  1276. }
  1277. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1278. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1279. return 's';
  1280. else
  1281. return 'b';
  1282. }
  1283. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1284. ".debug", strlen(".debug")) == 0)
  1285. return 'n';
  1286. return '?';
  1287. }
  1288. static void add_kallsyms(struct module *mod,
  1289. Elf_Shdr *sechdrs,
  1290. unsigned int symindex,
  1291. unsigned int strindex,
  1292. const char *secstrings)
  1293. {
  1294. unsigned int i;
  1295. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1296. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1297. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1298. /* Set types up while we still have access to sections. */
  1299. for (i = 0; i < mod->num_symtab; i++)
  1300. mod->symtab[i].st_info
  1301. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1302. }
  1303. #else
  1304. static inline void add_kallsyms(struct module *mod,
  1305. Elf_Shdr *sechdrs,
  1306. unsigned int symindex,
  1307. unsigned int strindex,
  1308. const char *secstrings)
  1309. {
  1310. }
  1311. #endif /* CONFIG_KALLSYMS */
  1312. /* Allocate and load the module: note that size of section 0 is always
  1313. zero, and we rely on this for optional sections. */
  1314. static struct module *load_module(void __user *umod,
  1315. unsigned long len,
  1316. const char __user *uargs)
  1317. {
  1318. Elf_Ehdr *hdr;
  1319. Elf_Shdr *sechdrs;
  1320. char *secstrings, *args, *modmagic, *strtab = NULL;
  1321. unsigned int i;
  1322. unsigned int symindex = 0;
  1323. unsigned int strindex = 0;
  1324. unsigned int setupindex;
  1325. unsigned int exindex;
  1326. unsigned int exportindex;
  1327. unsigned int modindex;
  1328. unsigned int obsparmindex;
  1329. unsigned int infoindex;
  1330. unsigned int gplindex;
  1331. unsigned int crcindex;
  1332. unsigned int gplcrcindex;
  1333. unsigned int versindex;
  1334. unsigned int pcpuindex;
  1335. unsigned int gplfutureindex;
  1336. unsigned int gplfuturecrcindex;
  1337. unsigned int unwindex = 0;
  1338. unsigned int unusedindex;
  1339. unsigned int unusedcrcindex;
  1340. unsigned int unusedgplindex;
  1341. unsigned int unusedgplcrcindex;
  1342. struct module *mod;
  1343. long err = 0;
  1344. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1345. struct exception_table_entry *extable;
  1346. mm_segment_t old_fs;
  1347. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1348. umod, len, uargs);
  1349. if (len < sizeof(*hdr))
  1350. return ERR_PTR(-ENOEXEC);
  1351. /* Suck in entire file: we'll want most of it. */
  1352. /* vmalloc barfs on "unusual" numbers. Check here */
  1353. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1354. return ERR_PTR(-ENOMEM);
  1355. if (copy_from_user(hdr, umod, len) != 0) {
  1356. err = -EFAULT;
  1357. goto free_hdr;
  1358. }
  1359. /* Sanity checks against insmoding binaries or wrong arch,
  1360. weird elf version */
  1361. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1362. || hdr->e_type != ET_REL
  1363. || !elf_check_arch(hdr)
  1364. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1365. err = -ENOEXEC;
  1366. goto free_hdr;
  1367. }
  1368. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1369. goto truncated;
  1370. /* Convenience variables */
  1371. sechdrs = (void *)hdr + hdr->e_shoff;
  1372. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1373. sechdrs[0].sh_addr = 0;
  1374. for (i = 1; i < hdr->e_shnum; i++) {
  1375. if (sechdrs[i].sh_type != SHT_NOBITS
  1376. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1377. goto truncated;
  1378. /* Mark all sections sh_addr with their address in the
  1379. temporary image. */
  1380. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1381. /* Internal symbols and strings. */
  1382. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1383. symindex = i;
  1384. strindex = sechdrs[i].sh_link;
  1385. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1386. }
  1387. #ifndef CONFIG_MODULE_UNLOAD
  1388. /* Don't load .exit sections */
  1389. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1390. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1391. #endif
  1392. }
  1393. modindex = find_sec(hdr, sechdrs, secstrings,
  1394. ".gnu.linkonce.this_module");
  1395. if (!modindex) {
  1396. printk(KERN_WARNING "No module found in object\n");
  1397. err = -ENOEXEC;
  1398. goto free_hdr;
  1399. }
  1400. mod = (void *)sechdrs[modindex].sh_addr;
  1401. if (symindex == 0) {
  1402. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1403. mod->name);
  1404. err = -ENOEXEC;
  1405. goto free_hdr;
  1406. }
  1407. /* Optional sections */
  1408. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1409. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1410. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1411. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1412. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1413. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1414. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1415. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1416. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1417. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1418. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1419. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1420. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1421. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1422. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1423. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1424. #ifdef ARCH_UNWIND_SECTION_NAME
  1425. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1426. #endif
  1427. /* Don't keep modinfo section */
  1428. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1429. #ifdef CONFIG_KALLSYMS
  1430. /* Keep symbol and string tables for decoding later. */
  1431. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1432. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1433. #endif
  1434. if (unwindex)
  1435. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1436. /* Check module struct version now, before we try to use module. */
  1437. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1438. err = -ENOEXEC;
  1439. goto free_hdr;
  1440. }
  1441. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1442. /* This is allowed: modprobe --force will invalidate it. */
  1443. if (!modmagic) {
  1444. add_taint_module(mod, TAINT_FORCED_MODULE);
  1445. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1446. mod->name);
  1447. } else if (!same_magic(modmagic, vermagic)) {
  1448. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1449. mod->name, modmagic, vermagic);
  1450. err = -ENOEXEC;
  1451. goto free_hdr;
  1452. }
  1453. /* Now copy in args */
  1454. args = strndup_user(uargs, ~0UL >> 1);
  1455. if (IS_ERR(args)) {
  1456. err = PTR_ERR(args);
  1457. goto free_hdr;
  1458. }
  1459. if (find_module(mod->name)) {
  1460. err = -EEXIST;
  1461. goto free_mod;
  1462. }
  1463. mod->state = MODULE_STATE_COMING;
  1464. /* Allow arches to frob section contents and sizes. */
  1465. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1466. if (err < 0)
  1467. goto free_mod;
  1468. if (pcpuindex) {
  1469. /* We have a special allocation for this section. */
  1470. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1471. sechdrs[pcpuindex].sh_addralign,
  1472. mod->name);
  1473. if (!percpu) {
  1474. err = -ENOMEM;
  1475. goto free_mod;
  1476. }
  1477. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1478. mod->percpu = percpu;
  1479. }
  1480. /* Determine total sizes, and put offsets in sh_entsize. For now
  1481. this is done generically; there doesn't appear to be any
  1482. special cases for the architectures. */
  1483. layout_sections(mod, hdr, sechdrs, secstrings);
  1484. /* Do the allocs. */
  1485. ptr = module_alloc(mod->core_size);
  1486. if (!ptr) {
  1487. err = -ENOMEM;
  1488. goto free_percpu;
  1489. }
  1490. memset(ptr, 0, mod->core_size);
  1491. mod->module_core = ptr;
  1492. ptr = module_alloc(mod->init_size);
  1493. if (!ptr && mod->init_size) {
  1494. err = -ENOMEM;
  1495. goto free_core;
  1496. }
  1497. memset(ptr, 0, mod->init_size);
  1498. mod->module_init = ptr;
  1499. /* Transfer each section which specifies SHF_ALLOC */
  1500. DEBUGP("final section addresses:\n");
  1501. for (i = 0; i < hdr->e_shnum; i++) {
  1502. void *dest;
  1503. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1504. continue;
  1505. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1506. dest = mod->module_init
  1507. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1508. else
  1509. dest = mod->module_core + sechdrs[i].sh_entsize;
  1510. if (sechdrs[i].sh_type != SHT_NOBITS)
  1511. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1512. sechdrs[i].sh_size);
  1513. /* Update sh_addr to point to copy in image. */
  1514. sechdrs[i].sh_addr = (unsigned long)dest;
  1515. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1516. }
  1517. /* Module has been moved. */
  1518. mod = (void *)sechdrs[modindex].sh_addr;
  1519. /* Now we've moved module, initialize linked lists, etc. */
  1520. module_unload_init(mod);
  1521. /* Set up license info based on the info section */
  1522. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1523. if (strcmp(mod->name, "ndiswrapper") == 0)
  1524. add_taint(TAINT_PROPRIETARY_MODULE);
  1525. if (strcmp(mod->name, "driverloader") == 0)
  1526. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1527. /* Set up MODINFO_ATTR fields */
  1528. setup_modinfo(mod, sechdrs, infoindex);
  1529. /* Fix up syms, so that st_value is a pointer to location. */
  1530. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1531. mod);
  1532. if (err < 0)
  1533. goto cleanup;
  1534. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1535. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1536. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1537. if (crcindex)
  1538. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1539. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1540. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1541. if (gplcrcindex)
  1542. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1543. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1544. sizeof(*mod->gpl_future_syms);
  1545. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1546. sizeof(*mod->unused_syms);
  1547. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1548. sizeof(*mod->unused_gpl_syms);
  1549. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1550. if (gplfuturecrcindex)
  1551. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1552. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1553. if (unusedcrcindex)
  1554. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1555. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1556. if (unusedgplcrcindex)
  1557. mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1558. #ifdef CONFIG_MODVERSIONS
  1559. if ((mod->num_syms && !crcindex) ||
  1560. (mod->num_gpl_syms && !gplcrcindex) ||
  1561. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1562. (mod->num_unused_syms && !unusedcrcindex) ||
  1563. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1564. printk(KERN_WARNING "%s: No versions for exported symbols."
  1565. " Tainting kernel.\n", mod->name);
  1566. add_taint_module(mod, TAINT_FORCED_MODULE);
  1567. }
  1568. #endif
  1569. /* Now do relocations. */
  1570. for (i = 1; i < hdr->e_shnum; i++) {
  1571. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1572. unsigned int info = sechdrs[i].sh_info;
  1573. /* Not a valid relocation section? */
  1574. if (info >= hdr->e_shnum)
  1575. continue;
  1576. /* Don't bother with non-allocated sections */
  1577. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1578. continue;
  1579. if (sechdrs[i].sh_type == SHT_REL)
  1580. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1581. else if (sechdrs[i].sh_type == SHT_RELA)
  1582. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1583. mod);
  1584. if (err < 0)
  1585. goto cleanup;
  1586. }
  1587. /* Find duplicate symbols */
  1588. err = verify_export_symbols(mod);
  1589. if (err < 0)
  1590. goto cleanup;
  1591. /* Set up and sort exception table */
  1592. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1593. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1594. sort_extable(extable, extable + mod->num_exentries);
  1595. /* Finally, copy percpu area over. */
  1596. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1597. sechdrs[pcpuindex].sh_size);
  1598. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1599. err = module_finalize(hdr, sechdrs, mod);
  1600. if (err < 0)
  1601. goto cleanup;
  1602. /* flush the icache in correct context */
  1603. old_fs = get_fs();
  1604. set_fs(KERNEL_DS);
  1605. /*
  1606. * Flush the instruction cache, since we've played with text.
  1607. * Do it before processing of module parameters, so the module
  1608. * can provide parameter accessor functions of its own.
  1609. */
  1610. if (mod->module_init)
  1611. flush_icache_range((unsigned long)mod->module_init,
  1612. (unsigned long)mod->module_init
  1613. + mod->init_size);
  1614. flush_icache_range((unsigned long)mod->module_core,
  1615. (unsigned long)mod->module_core + mod->core_size);
  1616. set_fs(old_fs);
  1617. mod->args = args;
  1618. if (obsparmindex)
  1619. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1620. mod->name);
  1621. /* Size of section 0 is 0, so this works well if no params */
  1622. err = parse_args(mod->name, mod->args,
  1623. (struct kernel_param *)
  1624. sechdrs[setupindex].sh_addr,
  1625. sechdrs[setupindex].sh_size
  1626. / sizeof(struct kernel_param),
  1627. NULL);
  1628. if (err < 0)
  1629. goto arch_cleanup;
  1630. err = mod_sysfs_setup(mod,
  1631. (struct kernel_param *)
  1632. sechdrs[setupindex].sh_addr,
  1633. sechdrs[setupindex].sh_size
  1634. / sizeof(struct kernel_param));
  1635. if (err < 0)
  1636. goto arch_cleanup;
  1637. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1638. /* Size of section 0 is 0, so this works well if no unwind info. */
  1639. mod->unwind_info = unwind_add_table(mod,
  1640. (void *)sechdrs[unwindex].sh_addr,
  1641. sechdrs[unwindex].sh_size);
  1642. /* Get rid of temporary copy */
  1643. vfree(hdr);
  1644. /* Done! */
  1645. return mod;
  1646. arch_cleanup:
  1647. module_arch_cleanup(mod);
  1648. cleanup:
  1649. module_unload_free(mod);
  1650. module_free(mod, mod->module_init);
  1651. free_core:
  1652. module_free(mod, mod->module_core);
  1653. free_percpu:
  1654. if (percpu)
  1655. percpu_modfree(percpu);
  1656. free_mod:
  1657. kfree(args);
  1658. free_hdr:
  1659. vfree(hdr);
  1660. return ERR_PTR(err);
  1661. truncated:
  1662. printk(KERN_ERR "Module len %lu truncated\n", len);
  1663. err = -ENOEXEC;
  1664. goto free_hdr;
  1665. }
  1666. /*
  1667. * link the module with the whole machine is stopped with interrupts off
  1668. * - this defends against kallsyms not taking locks
  1669. */
  1670. static int __link_module(void *_mod)
  1671. {
  1672. struct module *mod = _mod;
  1673. list_add(&mod->list, &modules);
  1674. return 0;
  1675. }
  1676. /* This is where the real work happens */
  1677. asmlinkage long
  1678. sys_init_module(void __user *umod,
  1679. unsigned long len,
  1680. const char __user *uargs)
  1681. {
  1682. struct module *mod;
  1683. int ret = 0;
  1684. /* Must have permission */
  1685. if (!capable(CAP_SYS_MODULE))
  1686. return -EPERM;
  1687. /* Only one module load at a time, please */
  1688. if (mutex_lock_interruptible(&module_mutex) != 0)
  1689. return -EINTR;
  1690. /* Do all the hard work */
  1691. mod = load_module(umod, len, uargs);
  1692. if (IS_ERR(mod)) {
  1693. mutex_unlock(&module_mutex);
  1694. return PTR_ERR(mod);
  1695. }
  1696. /* Now sew it into the lists. They won't access us, since
  1697. strong_try_module_get() will fail. */
  1698. stop_machine_run(__link_module, mod, NR_CPUS);
  1699. /* Drop lock so they can recurse */
  1700. mutex_unlock(&module_mutex);
  1701. blocking_notifier_call_chain(&module_notify_list,
  1702. MODULE_STATE_COMING, mod);
  1703. /* Start the module */
  1704. if (mod->init != NULL)
  1705. ret = mod->init();
  1706. if (ret < 0) {
  1707. /* Init routine failed: abort. Try to protect us from
  1708. buggy refcounters. */
  1709. mod->state = MODULE_STATE_GOING;
  1710. synchronize_sched();
  1711. if (mod->unsafe)
  1712. printk(KERN_ERR "%s: module is now stuck!\n",
  1713. mod->name);
  1714. else {
  1715. module_put(mod);
  1716. mutex_lock(&module_mutex);
  1717. free_module(mod);
  1718. mutex_unlock(&module_mutex);
  1719. }
  1720. return ret;
  1721. }
  1722. /* Now it's a first class citizen! */
  1723. mutex_lock(&module_mutex);
  1724. mod->state = MODULE_STATE_LIVE;
  1725. /* Drop initial reference. */
  1726. module_put(mod);
  1727. unwind_remove_table(mod->unwind_info, 1);
  1728. module_free(mod, mod->module_init);
  1729. mod->module_init = NULL;
  1730. mod->init_size = 0;
  1731. mod->init_text_size = 0;
  1732. mutex_unlock(&module_mutex);
  1733. return 0;
  1734. }
  1735. static inline int within(unsigned long addr, void *start, unsigned long size)
  1736. {
  1737. return ((void *)addr >= start && (void *)addr < start + size);
  1738. }
  1739. #ifdef CONFIG_KALLSYMS
  1740. /*
  1741. * This ignores the intensely annoying "mapping symbols" found
  1742. * in ARM ELF files: $a, $t and $d.
  1743. */
  1744. static inline int is_arm_mapping_symbol(const char *str)
  1745. {
  1746. return str[0] == '$' && strchr("atd", str[1])
  1747. && (str[2] == '\0' || str[2] == '.');
  1748. }
  1749. static const char *get_ksymbol(struct module *mod,
  1750. unsigned long addr,
  1751. unsigned long *size,
  1752. unsigned long *offset)
  1753. {
  1754. unsigned int i, best = 0;
  1755. unsigned long nextval;
  1756. /* At worse, next value is at end of module */
  1757. if (within(addr, mod->module_init, mod->init_size))
  1758. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1759. else
  1760. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1761. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1762. starts real symbols at 1). */
  1763. for (i = 1; i < mod->num_symtab; i++) {
  1764. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1765. continue;
  1766. /* We ignore unnamed symbols: they're uninformative
  1767. * and inserted at a whim. */
  1768. if (mod->symtab[i].st_value <= addr
  1769. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1770. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1771. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1772. best = i;
  1773. if (mod->symtab[i].st_value > addr
  1774. && mod->symtab[i].st_value < nextval
  1775. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1776. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1777. nextval = mod->symtab[i].st_value;
  1778. }
  1779. if (!best)
  1780. return NULL;
  1781. *size = nextval - mod->symtab[best].st_value;
  1782. *offset = addr - mod->symtab[best].st_value;
  1783. return mod->strtab + mod->symtab[best].st_name;
  1784. }
  1785. /* For kallsyms to ask for address resolution. NULL means not found.
  1786. We don't lock, as this is used for oops resolution and races are a
  1787. lesser concern. */
  1788. const char *module_address_lookup(unsigned long addr,
  1789. unsigned long *size,
  1790. unsigned long *offset,
  1791. char **modname)
  1792. {
  1793. struct module *mod;
  1794. list_for_each_entry(mod, &modules, list) {
  1795. if (within(addr, mod->module_init, mod->init_size)
  1796. || within(addr, mod->module_core, mod->core_size)) {
  1797. if (modname)
  1798. *modname = mod->name;
  1799. return get_ksymbol(mod, addr, size, offset);
  1800. }
  1801. }
  1802. return NULL;
  1803. }
  1804. struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
  1805. char *type, char *name, size_t namelen)
  1806. {
  1807. struct module *mod;
  1808. mutex_lock(&module_mutex);
  1809. list_for_each_entry(mod, &modules, list) {
  1810. if (symnum < mod->num_symtab) {
  1811. *value = mod->symtab[symnum].st_value;
  1812. *type = mod->symtab[symnum].st_info;
  1813. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  1814. namelen);
  1815. mutex_unlock(&module_mutex);
  1816. return mod;
  1817. }
  1818. symnum -= mod->num_symtab;
  1819. }
  1820. mutex_unlock(&module_mutex);
  1821. return NULL;
  1822. }
  1823. static unsigned long mod_find_symname(struct module *mod, const char *name)
  1824. {
  1825. unsigned int i;
  1826. for (i = 0; i < mod->num_symtab; i++)
  1827. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  1828. mod->symtab[i].st_info != 'U')
  1829. return mod->symtab[i].st_value;
  1830. return 0;
  1831. }
  1832. /* Look for this name: can be of form module:name. */
  1833. unsigned long module_kallsyms_lookup_name(const char *name)
  1834. {
  1835. struct module *mod;
  1836. char *colon;
  1837. unsigned long ret = 0;
  1838. /* Don't lock: we're in enough trouble already. */
  1839. if ((colon = strchr(name, ':')) != NULL) {
  1840. *colon = '\0';
  1841. if ((mod = find_module(name)) != NULL)
  1842. ret = mod_find_symname(mod, colon+1);
  1843. *colon = ':';
  1844. } else {
  1845. list_for_each_entry(mod, &modules, list)
  1846. if ((ret = mod_find_symname(mod, name)) != 0)
  1847. break;
  1848. }
  1849. return ret;
  1850. }
  1851. #endif /* CONFIG_KALLSYMS */
  1852. /* Called by the /proc file system to return a list of modules. */
  1853. static void *m_start(struct seq_file *m, loff_t *pos)
  1854. {
  1855. struct list_head *i;
  1856. loff_t n = 0;
  1857. mutex_lock(&module_mutex);
  1858. list_for_each(i, &modules) {
  1859. if (n++ == *pos)
  1860. break;
  1861. }
  1862. if (i == &modules)
  1863. return NULL;
  1864. return i;
  1865. }
  1866. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  1867. {
  1868. struct list_head *i = p;
  1869. (*pos)++;
  1870. if (i->next == &modules)
  1871. return NULL;
  1872. return i->next;
  1873. }
  1874. static void m_stop(struct seq_file *m, void *p)
  1875. {
  1876. mutex_unlock(&module_mutex);
  1877. }
  1878. static char *taint_flags(unsigned int taints, char *buf)
  1879. {
  1880. int bx = 0;
  1881. if (taints) {
  1882. buf[bx++] = '(';
  1883. if (taints & TAINT_PROPRIETARY_MODULE)
  1884. buf[bx++] = 'P';
  1885. if (taints & TAINT_FORCED_MODULE)
  1886. buf[bx++] = 'F';
  1887. /*
  1888. * TAINT_FORCED_RMMOD: could be added.
  1889. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  1890. * apply to modules.
  1891. */
  1892. buf[bx++] = ')';
  1893. }
  1894. buf[bx] = '\0';
  1895. return buf;
  1896. }
  1897. static int m_show(struct seq_file *m, void *p)
  1898. {
  1899. struct module *mod = list_entry(p, struct module, list);
  1900. char buf[8];
  1901. seq_printf(m, "%s %lu",
  1902. mod->name, mod->init_size + mod->core_size);
  1903. print_unload_info(m, mod);
  1904. /* Informative for users. */
  1905. seq_printf(m, " %s",
  1906. mod->state == MODULE_STATE_GOING ? "Unloading":
  1907. mod->state == MODULE_STATE_COMING ? "Loading":
  1908. "Live");
  1909. /* Used by oprofile and other similar tools. */
  1910. seq_printf(m, " 0x%p", mod->module_core);
  1911. /* Taints info */
  1912. if (mod->taints)
  1913. seq_printf(m, " %s", taint_flags(mod->taints, buf));
  1914. seq_printf(m, "\n");
  1915. return 0;
  1916. }
  1917. /* Format: modulename size refcount deps address
  1918. Where refcount is a number or -, and deps is a comma-separated list
  1919. of depends or -.
  1920. */
  1921. const struct seq_operations modules_op = {
  1922. .start = m_start,
  1923. .next = m_next,
  1924. .stop = m_stop,
  1925. .show = m_show
  1926. };
  1927. /* Given an address, look for it in the module exception tables. */
  1928. const struct exception_table_entry *search_module_extables(unsigned long addr)
  1929. {
  1930. unsigned long flags;
  1931. const struct exception_table_entry *e = NULL;
  1932. struct module *mod;
  1933. spin_lock_irqsave(&modlist_lock, flags);
  1934. list_for_each_entry(mod, &modules, list) {
  1935. if (mod->num_exentries == 0)
  1936. continue;
  1937. e = search_extable(mod->extable,
  1938. mod->extable + mod->num_exentries - 1,
  1939. addr);
  1940. if (e)
  1941. break;
  1942. }
  1943. spin_unlock_irqrestore(&modlist_lock, flags);
  1944. /* Now, if we found one, we are running inside it now, hence
  1945. we cannot unload the module, hence no refcnt needed. */
  1946. return e;
  1947. }
  1948. /*
  1949. * Is this a valid module address?
  1950. */
  1951. int is_module_address(unsigned long addr)
  1952. {
  1953. unsigned long flags;
  1954. struct module *mod;
  1955. spin_lock_irqsave(&modlist_lock, flags);
  1956. list_for_each_entry(mod, &modules, list) {
  1957. if (within(addr, mod->module_core, mod->core_size)) {
  1958. spin_unlock_irqrestore(&modlist_lock, flags);
  1959. return 1;
  1960. }
  1961. }
  1962. spin_unlock_irqrestore(&modlist_lock, flags);
  1963. return 0;
  1964. }
  1965. /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
  1966. struct module *__module_text_address(unsigned long addr)
  1967. {
  1968. struct module *mod;
  1969. list_for_each_entry(mod, &modules, list)
  1970. if (within(addr, mod->module_init, mod->init_text_size)
  1971. || within(addr, mod->module_core, mod->core_text_size))
  1972. return mod;
  1973. return NULL;
  1974. }
  1975. struct module *module_text_address(unsigned long addr)
  1976. {
  1977. struct module *mod;
  1978. unsigned long flags;
  1979. spin_lock_irqsave(&modlist_lock, flags);
  1980. mod = __module_text_address(addr);
  1981. spin_unlock_irqrestore(&modlist_lock, flags);
  1982. return mod;
  1983. }
  1984. /* Don't grab lock, we're oopsing. */
  1985. void print_modules(void)
  1986. {
  1987. struct module *mod;
  1988. char buf[8];
  1989. printk("Modules linked in:");
  1990. list_for_each_entry(mod, &modules, list)
  1991. printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
  1992. printk("\n");
  1993. }
  1994. void module_add_driver(struct module *mod, struct device_driver *drv)
  1995. {
  1996. int no_warn;
  1997. if (!mod || !drv)
  1998. return;
  1999. /* Don't check return codes; these calls are idempotent */
  2000. no_warn = sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
  2001. no_warn = sysfs_create_link(mod->drivers_dir, &drv->kobj, drv->name);
  2002. }
  2003. EXPORT_SYMBOL(module_add_driver);
  2004. void module_remove_driver(struct device_driver *drv)
  2005. {
  2006. if (!drv)
  2007. return;
  2008. sysfs_remove_link(&drv->kobj, "module");
  2009. if (drv->owner && drv->owner->drivers_dir)
  2010. sysfs_remove_link(drv->owner->drivers_dir, drv->name);
  2011. }
  2012. EXPORT_SYMBOL(module_remove_driver);
  2013. #ifdef CONFIG_MODVERSIONS
  2014. /* Generate the signature for struct module here, too, for modversions. */
  2015. void struct_module(struct module *mod) { return; }
  2016. EXPORT_SYMBOL(struct_module);
  2017. #endif