module.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  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 ssize_t show_initstate(struct module_attribute *mattr,
  721. struct module *mod, char *buffer)
  722. {
  723. const char *state = "unknown";
  724. switch (mod->state) {
  725. case MODULE_STATE_LIVE:
  726. state = "live";
  727. break;
  728. case MODULE_STATE_COMING:
  729. state = "coming";
  730. break;
  731. case MODULE_STATE_GOING:
  732. state = "going";
  733. break;
  734. }
  735. return sprintf(buffer, "%s\n", state);
  736. }
  737. static struct module_attribute initstate = {
  738. .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE },
  739. .show = show_initstate,
  740. };
  741. static struct module_attribute *modinfo_attrs[] = {
  742. &modinfo_version,
  743. &modinfo_srcversion,
  744. &initstate,
  745. #ifdef CONFIG_MODULE_UNLOAD
  746. &refcnt,
  747. #endif
  748. NULL,
  749. };
  750. static const char vermagic[] = VERMAGIC_STRING;
  751. #ifdef CONFIG_MODVERSIONS
  752. static int check_version(Elf_Shdr *sechdrs,
  753. unsigned int versindex,
  754. const char *symname,
  755. struct module *mod,
  756. const unsigned long *crc)
  757. {
  758. unsigned int i, num_versions;
  759. struct modversion_info *versions;
  760. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  761. if (!crc)
  762. return 1;
  763. versions = (void *) sechdrs[versindex].sh_addr;
  764. num_versions = sechdrs[versindex].sh_size
  765. / sizeof(struct modversion_info);
  766. for (i = 0; i < num_versions; i++) {
  767. if (strcmp(versions[i].name, symname) != 0)
  768. continue;
  769. if (versions[i].crc == *crc)
  770. return 1;
  771. printk("%s: disagrees about version of symbol %s\n",
  772. mod->name, symname);
  773. DEBUGP("Found checksum %lX vs module %lX\n",
  774. *crc, versions[i].crc);
  775. return 0;
  776. }
  777. /* Not in module's version table. OK, but that taints the kernel. */
  778. if (!(tainted & TAINT_FORCED_MODULE))
  779. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  780. mod->name, symname);
  781. add_taint_module(mod, TAINT_FORCED_MODULE);
  782. return 1;
  783. }
  784. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  785. unsigned int versindex,
  786. struct module *mod)
  787. {
  788. const unsigned long *crc;
  789. struct module *owner;
  790. if (!__find_symbol("struct_module", &owner, &crc, 1))
  791. BUG();
  792. return check_version(sechdrs, versindex, "struct_module", mod,
  793. crc);
  794. }
  795. /* First part is kernel version, which we ignore. */
  796. static inline int same_magic(const char *amagic, const char *bmagic)
  797. {
  798. amagic += strcspn(amagic, " ");
  799. bmagic += strcspn(bmagic, " ");
  800. return strcmp(amagic, bmagic) == 0;
  801. }
  802. #else
  803. static inline int check_version(Elf_Shdr *sechdrs,
  804. unsigned int versindex,
  805. const char *symname,
  806. struct module *mod,
  807. const unsigned long *crc)
  808. {
  809. return 1;
  810. }
  811. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  812. unsigned int versindex,
  813. struct module *mod)
  814. {
  815. return 1;
  816. }
  817. static inline int same_magic(const char *amagic, const char *bmagic)
  818. {
  819. return strcmp(amagic, bmagic) == 0;
  820. }
  821. #endif /* CONFIG_MODVERSIONS */
  822. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  823. Must be holding module_mutex. */
  824. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  825. unsigned int versindex,
  826. const char *name,
  827. struct module *mod)
  828. {
  829. struct module *owner;
  830. unsigned long ret;
  831. const unsigned long *crc;
  832. ret = __find_symbol(name, &owner, &crc,
  833. !(mod->taints & TAINT_PROPRIETARY_MODULE));
  834. if (ret) {
  835. /* use_module can fail due to OOM, or module unloading */
  836. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  837. !use_module(mod, owner))
  838. ret = 0;
  839. }
  840. return ret;
  841. }
  842. /*
  843. * /sys/module/foo/sections stuff
  844. * J. Corbet <corbet@lwn.net>
  845. */
  846. #ifdef CONFIG_KALLSYMS
  847. static ssize_t module_sect_show(struct module_attribute *mattr,
  848. struct module *mod, char *buf)
  849. {
  850. struct module_sect_attr *sattr =
  851. container_of(mattr, struct module_sect_attr, mattr);
  852. return sprintf(buf, "0x%lx\n", sattr->address);
  853. }
  854. static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
  855. {
  856. int section;
  857. for (section = 0; section < sect_attrs->nsections; section++)
  858. kfree(sect_attrs->attrs[section].name);
  859. kfree(sect_attrs);
  860. }
  861. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  862. char *secstrings, Elf_Shdr *sechdrs)
  863. {
  864. unsigned int nloaded = 0, i, size[2];
  865. struct module_sect_attrs *sect_attrs;
  866. struct module_sect_attr *sattr;
  867. struct attribute **gattr;
  868. /* Count loaded sections and allocate structures */
  869. for (i = 0; i < nsect; i++)
  870. if (sechdrs[i].sh_flags & SHF_ALLOC)
  871. nloaded++;
  872. size[0] = ALIGN(sizeof(*sect_attrs)
  873. + nloaded * sizeof(sect_attrs->attrs[0]),
  874. sizeof(sect_attrs->grp.attrs[0]));
  875. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  876. sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
  877. if (sect_attrs == NULL)
  878. return;
  879. /* Setup section attributes. */
  880. sect_attrs->grp.name = "sections";
  881. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  882. sect_attrs->nsections = 0;
  883. sattr = &sect_attrs->attrs[0];
  884. gattr = &sect_attrs->grp.attrs[0];
  885. for (i = 0; i < nsect; i++) {
  886. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  887. continue;
  888. sattr->address = sechdrs[i].sh_addr;
  889. sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
  890. GFP_KERNEL);
  891. if (sattr->name == NULL)
  892. goto out;
  893. sect_attrs->nsections++;
  894. sattr->mattr.show = module_sect_show;
  895. sattr->mattr.store = NULL;
  896. sattr->mattr.attr.name = sattr->name;
  897. sattr->mattr.attr.owner = mod;
  898. sattr->mattr.attr.mode = S_IRUGO;
  899. *(gattr++) = &(sattr++)->mattr.attr;
  900. }
  901. *gattr = NULL;
  902. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  903. goto out;
  904. mod->sect_attrs = sect_attrs;
  905. return;
  906. out:
  907. free_sect_attrs(sect_attrs);
  908. }
  909. static void remove_sect_attrs(struct module *mod)
  910. {
  911. if (mod->sect_attrs) {
  912. sysfs_remove_group(&mod->mkobj.kobj,
  913. &mod->sect_attrs->grp);
  914. /* We are positive that no one is using any sect attrs
  915. * at this point. Deallocate immediately. */
  916. free_sect_attrs(mod->sect_attrs);
  917. mod->sect_attrs = NULL;
  918. }
  919. }
  920. #else
  921. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  922. char *sectstrings, Elf_Shdr *sechdrs)
  923. {
  924. }
  925. static inline void remove_sect_attrs(struct module *mod)
  926. {
  927. }
  928. #endif /* CONFIG_KALLSYMS */
  929. static int module_add_modinfo_attrs(struct module *mod)
  930. {
  931. struct module_attribute *attr;
  932. struct module_attribute *temp_attr;
  933. int error = 0;
  934. int i;
  935. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  936. (ARRAY_SIZE(modinfo_attrs) + 1)),
  937. GFP_KERNEL);
  938. if (!mod->modinfo_attrs)
  939. return -ENOMEM;
  940. temp_attr = mod->modinfo_attrs;
  941. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  942. if (!attr->test ||
  943. (attr->test && attr->test(mod))) {
  944. memcpy(temp_attr, attr, sizeof(*temp_attr));
  945. temp_attr->attr.owner = mod;
  946. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  947. ++temp_attr;
  948. }
  949. }
  950. return error;
  951. }
  952. static void module_remove_modinfo_attrs(struct module *mod)
  953. {
  954. struct module_attribute *attr;
  955. int i;
  956. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  957. /* pick a field to test for end of list */
  958. if (!attr->attr.name)
  959. break;
  960. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  961. if (attr->free)
  962. attr->free(mod);
  963. }
  964. kfree(mod->modinfo_attrs);
  965. }
  966. static int mod_sysfs_setup(struct module *mod,
  967. struct kernel_param *kparam,
  968. unsigned int num_params)
  969. {
  970. int err;
  971. if (!module_subsys.kset.subsys) {
  972. printk(KERN_ERR "%s: module_subsys not initialized\n",
  973. mod->name);
  974. err = -EINVAL;
  975. goto out;
  976. }
  977. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  978. err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
  979. if (err)
  980. goto out;
  981. kobj_set_kset_s(&mod->mkobj, module_subsys);
  982. mod->mkobj.mod = mod;
  983. /* delay uevent until full sysfs population */
  984. kobject_init(&mod->mkobj.kobj);
  985. err = kobject_add(&mod->mkobj.kobj);
  986. if (err)
  987. goto out;
  988. mod->drivers_dir = kobject_add_dir(&mod->mkobj.kobj, "drivers");
  989. if (!mod->drivers_dir) {
  990. err = -ENOMEM;
  991. goto out_unreg;
  992. }
  993. err = module_param_sysfs_setup(mod, kparam, num_params);
  994. if (err)
  995. goto out_unreg_drivers;
  996. err = module_add_modinfo_attrs(mod);
  997. if (err)
  998. goto out_unreg_param;
  999. kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
  1000. return 0;
  1001. out_unreg_param:
  1002. module_param_sysfs_remove(mod);
  1003. out_unreg_drivers:
  1004. kobject_unregister(mod->drivers_dir);
  1005. out_unreg:
  1006. kobject_del(&mod->mkobj.kobj);
  1007. kobject_put(&mod->mkobj.kobj);
  1008. out:
  1009. return err;
  1010. }
  1011. static void mod_kobject_remove(struct module *mod)
  1012. {
  1013. module_remove_modinfo_attrs(mod);
  1014. module_param_sysfs_remove(mod);
  1015. kobject_unregister(mod->drivers_dir);
  1016. kobject_unregister(&mod->mkobj.kobj);
  1017. }
  1018. /*
  1019. * unlink the module with the whole machine is stopped with interrupts off
  1020. * - this defends against kallsyms not taking locks
  1021. */
  1022. static int __unlink_module(void *_mod)
  1023. {
  1024. struct module *mod = _mod;
  1025. list_del(&mod->list);
  1026. return 0;
  1027. }
  1028. /* Free a module, remove from lists, etc (must hold module mutex). */
  1029. static void free_module(struct module *mod)
  1030. {
  1031. /* Delete from various lists */
  1032. stop_machine_run(__unlink_module, mod, NR_CPUS);
  1033. remove_sect_attrs(mod);
  1034. mod_kobject_remove(mod);
  1035. unwind_remove_table(mod->unwind_info, 0);
  1036. /* Arch-specific cleanup. */
  1037. module_arch_cleanup(mod);
  1038. /* Module unload stuff */
  1039. module_unload_free(mod);
  1040. /* This may be NULL, but that's OK */
  1041. module_free(mod, mod->module_init);
  1042. kfree(mod->args);
  1043. if (mod->percpu)
  1044. percpu_modfree(mod->percpu);
  1045. /* Free lock-classes: */
  1046. lockdep_free_key_range(mod->module_core, mod->core_size);
  1047. /* Finally, free the core (containing the module structure) */
  1048. module_free(mod, mod->module_core);
  1049. }
  1050. void *__symbol_get(const char *symbol)
  1051. {
  1052. struct module *owner;
  1053. unsigned long value, flags;
  1054. const unsigned long *crc;
  1055. spin_lock_irqsave(&modlist_lock, flags);
  1056. value = __find_symbol(symbol, &owner, &crc, 1);
  1057. if (value && !strong_try_module_get(owner))
  1058. value = 0;
  1059. spin_unlock_irqrestore(&modlist_lock, flags);
  1060. return (void *)value;
  1061. }
  1062. EXPORT_SYMBOL_GPL(__symbol_get);
  1063. /*
  1064. * Ensure that an exported symbol [global namespace] does not already exist
  1065. * in the Kernel or in some other modules exported symbol table.
  1066. */
  1067. static int verify_export_symbols(struct module *mod)
  1068. {
  1069. const char *name = NULL;
  1070. unsigned long i, ret = 0;
  1071. struct module *owner;
  1072. const unsigned long *crc;
  1073. for (i = 0; i < mod->num_syms; i++)
  1074. if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
  1075. name = mod->syms[i].name;
  1076. ret = -ENOEXEC;
  1077. goto dup;
  1078. }
  1079. for (i = 0; i < mod->num_gpl_syms; i++)
  1080. if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
  1081. name = mod->gpl_syms[i].name;
  1082. ret = -ENOEXEC;
  1083. goto dup;
  1084. }
  1085. dup:
  1086. if (ret)
  1087. printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
  1088. mod->name, name, module_name(owner));
  1089. return ret;
  1090. }
  1091. /* Change all symbols so that sh_value encodes the pointer directly. */
  1092. static int simplify_symbols(Elf_Shdr *sechdrs,
  1093. unsigned int symindex,
  1094. const char *strtab,
  1095. unsigned int versindex,
  1096. unsigned int pcpuindex,
  1097. struct module *mod)
  1098. {
  1099. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1100. unsigned long secbase;
  1101. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1102. int ret = 0;
  1103. for (i = 1; i < n; i++) {
  1104. switch (sym[i].st_shndx) {
  1105. case SHN_COMMON:
  1106. /* We compiled with -fno-common. These are not
  1107. supposed to happen. */
  1108. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1109. printk("%s: please compile with -fno-common\n",
  1110. mod->name);
  1111. ret = -ENOEXEC;
  1112. break;
  1113. case SHN_ABS:
  1114. /* Don't need to do anything */
  1115. DEBUGP("Absolute symbol: 0x%08lx\n",
  1116. (long)sym[i].st_value);
  1117. break;
  1118. case SHN_UNDEF:
  1119. sym[i].st_value
  1120. = resolve_symbol(sechdrs, versindex,
  1121. strtab + sym[i].st_name, mod);
  1122. /* Ok if resolved. */
  1123. if (sym[i].st_value != 0)
  1124. break;
  1125. /* Ok if weak. */
  1126. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1127. break;
  1128. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1129. mod->name, strtab + sym[i].st_name);
  1130. ret = -ENOENT;
  1131. break;
  1132. default:
  1133. /* Divert to percpu allocation if a percpu var. */
  1134. if (sym[i].st_shndx == pcpuindex)
  1135. secbase = (unsigned long)mod->percpu;
  1136. else
  1137. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1138. sym[i].st_value += secbase;
  1139. break;
  1140. }
  1141. }
  1142. return ret;
  1143. }
  1144. /* Update size with this section: return offset. */
  1145. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1146. {
  1147. long ret;
  1148. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1149. *size = ret + sechdr->sh_size;
  1150. return ret;
  1151. }
  1152. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1153. might -- code, read-only data, read-write data, small data. Tally
  1154. sizes, and place the offsets into sh_entsize fields: high bit means it
  1155. belongs in init. */
  1156. static void layout_sections(struct module *mod,
  1157. const Elf_Ehdr *hdr,
  1158. Elf_Shdr *sechdrs,
  1159. const char *secstrings)
  1160. {
  1161. static unsigned long const masks[][2] = {
  1162. /* NOTE: all executable code must be the first section
  1163. * in this array; otherwise modify the text_size
  1164. * finder in the two loops below */
  1165. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1166. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1167. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1168. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1169. };
  1170. unsigned int m, i;
  1171. for (i = 0; i < hdr->e_shnum; i++)
  1172. sechdrs[i].sh_entsize = ~0UL;
  1173. DEBUGP("Core section allocation order:\n");
  1174. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1175. for (i = 0; i < hdr->e_shnum; ++i) {
  1176. Elf_Shdr *s = &sechdrs[i];
  1177. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1178. || (s->sh_flags & masks[m][1])
  1179. || s->sh_entsize != ~0UL
  1180. || strncmp(secstrings + s->sh_name,
  1181. ".init", 5) == 0)
  1182. continue;
  1183. s->sh_entsize = get_offset(&mod->core_size, s);
  1184. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1185. }
  1186. if (m == 0)
  1187. mod->core_text_size = mod->core_size;
  1188. }
  1189. DEBUGP("Init section allocation order:\n");
  1190. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1191. for (i = 0; i < hdr->e_shnum; ++i) {
  1192. Elf_Shdr *s = &sechdrs[i];
  1193. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1194. || (s->sh_flags & masks[m][1])
  1195. || s->sh_entsize != ~0UL
  1196. || strncmp(secstrings + s->sh_name,
  1197. ".init", 5) != 0)
  1198. continue;
  1199. s->sh_entsize = (get_offset(&mod->init_size, s)
  1200. | INIT_OFFSET_MASK);
  1201. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1202. }
  1203. if (m == 0)
  1204. mod->init_text_size = mod->init_size;
  1205. }
  1206. }
  1207. static void set_license(struct module *mod, const char *license)
  1208. {
  1209. if (!license)
  1210. license = "unspecified";
  1211. if (!license_is_gpl_compatible(license)) {
  1212. if (!(tainted & TAINT_PROPRIETARY_MODULE))
  1213. printk(KERN_WARNING "%s: module license '%s' taints "
  1214. "kernel.\n", mod->name, license);
  1215. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1216. }
  1217. }
  1218. /* Parse tag=value strings from .modinfo section */
  1219. static char *next_string(char *string, unsigned long *secsize)
  1220. {
  1221. /* Skip non-zero chars */
  1222. while (string[0]) {
  1223. string++;
  1224. if ((*secsize)-- <= 1)
  1225. return NULL;
  1226. }
  1227. /* Skip any zero padding. */
  1228. while (!string[0]) {
  1229. string++;
  1230. if ((*secsize)-- <= 1)
  1231. return NULL;
  1232. }
  1233. return string;
  1234. }
  1235. static char *get_modinfo(Elf_Shdr *sechdrs,
  1236. unsigned int info,
  1237. const char *tag)
  1238. {
  1239. char *p;
  1240. unsigned int taglen = strlen(tag);
  1241. unsigned long size = sechdrs[info].sh_size;
  1242. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1243. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1244. return p + taglen + 1;
  1245. }
  1246. return NULL;
  1247. }
  1248. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1249. unsigned int infoindex)
  1250. {
  1251. struct module_attribute *attr;
  1252. int i;
  1253. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1254. if (attr->setup)
  1255. attr->setup(mod,
  1256. get_modinfo(sechdrs,
  1257. infoindex,
  1258. attr->attr.name));
  1259. }
  1260. }
  1261. #ifdef CONFIG_KALLSYMS
  1262. int is_exported(const char *name, const struct module *mod)
  1263. {
  1264. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1265. return 1;
  1266. else
  1267. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1268. return 1;
  1269. else
  1270. return 0;
  1271. }
  1272. /* As per nm */
  1273. static char elf_type(const Elf_Sym *sym,
  1274. Elf_Shdr *sechdrs,
  1275. const char *secstrings,
  1276. struct module *mod)
  1277. {
  1278. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1279. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1280. return 'v';
  1281. else
  1282. return 'w';
  1283. }
  1284. if (sym->st_shndx == SHN_UNDEF)
  1285. return 'U';
  1286. if (sym->st_shndx == SHN_ABS)
  1287. return 'a';
  1288. if (sym->st_shndx >= SHN_LORESERVE)
  1289. return '?';
  1290. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1291. return 't';
  1292. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1293. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1294. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1295. return 'r';
  1296. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1297. return 'g';
  1298. else
  1299. return 'd';
  1300. }
  1301. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1302. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1303. return 's';
  1304. else
  1305. return 'b';
  1306. }
  1307. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1308. ".debug", strlen(".debug")) == 0)
  1309. return 'n';
  1310. return '?';
  1311. }
  1312. static void add_kallsyms(struct module *mod,
  1313. Elf_Shdr *sechdrs,
  1314. unsigned int symindex,
  1315. unsigned int strindex,
  1316. const char *secstrings)
  1317. {
  1318. unsigned int i;
  1319. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1320. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1321. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1322. /* Set types up while we still have access to sections. */
  1323. for (i = 0; i < mod->num_symtab; i++)
  1324. mod->symtab[i].st_info
  1325. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1326. }
  1327. #else
  1328. static inline void add_kallsyms(struct module *mod,
  1329. Elf_Shdr *sechdrs,
  1330. unsigned int symindex,
  1331. unsigned int strindex,
  1332. const char *secstrings)
  1333. {
  1334. }
  1335. #endif /* CONFIG_KALLSYMS */
  1336. /* Allocate and load the module: note that size of section 0 is always
  1337. zero, and we rely on this for optional sections. */
  1338. static struct module *load_module(void __user *umod,
  1339. unsigned long len,
  1340. const char __user *uargs)
  1341. {
  1342. Elf_Ehdr *hdr;
  1343. Elf_Shdr *sechdrs;
  1344. char *secstrings, *args, *modmagic, *strtab = NULL;
  1345. unsigned int i;
  1346. unsigned int symindex = 0;
  1347. unsigned int strindex = 0;
  1348. unsigned int setupindex;
  1349. unsigned int exindex;
  1350. unsigned int exportindex;
  1351. unsigned int modindex;
  1352. unsigned int obsparmindex;
  1353. unsigned int infoindex;
  1354. unsigned int gplindex;
  1355. unsigned int crcindex;
  1356. unsigned int gplcrcindex;
  1357. unsigned int versindex;
  1358. unsigned int pcpuindex;
  1359. unsigned int gplfutureindex;
  1360. unsigned int gplfuturecrcindex;
  1361. unsigned int unwindex = 0;
  1362. unsigned int unusedindex;
  1363. unsigned int unusedcrcindex;
  1364. unsigned int unusedgplindex;
  1365. unsigned int unusedgplcrcindex;
  1366. struct module *mod;
  1367. long err = 0;
  1368. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1369. struct exception_table_entry *extable;
  1370. mm_segment_t old_fs;
  1371. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1372. umod, len, uargs);
  1373. if (len < sizeof(*hdr))
  1374. return ERR_PTR(-ENOEXEC);
  1375. /* Suck in entire file: we'll want most of it. */
  1376. /* vmalloc barfs on "unusual" numbers. Check here */
  1377. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1378. return ERR_PTR(-ENOMEM);
  1379. if (copy_from_user(hdr, umod, len) != 0) {
  1380. err = -EFAULT;
  1381. goto free_hdr;
  1382. }
  1383. /* Sanity checks against insmoding binaries or wrong arch,
  1384. weird elf version */
  1385. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1386. || hdr->e_type != ET_REL
  1387. || !elf_check_arch(hdr)
  1388. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1389. err = -ENOEXEC;
  1390. goto free_hdr;
  1391. }
  1392. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1393. goto truncated;
  1394. /* Convenience variables */
  1395. sechdrs = (void *)hdr + hdr->e_shoff;
  1396. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1397. sechdrs[0].sh_addr = 0;
  1398. for (i = 1; i < hdr->e_shnum; i++) {
  1399. if (sechdrs[i].sh_type != SHT_NOBITS
  1400. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1401. goto truncated;
  1402. /* Mark all sections sh_addr with their address in the
  1403. temporary image. */
  1404. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1405. /* Internal symbols and strings. */
  1406. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1407. symindex = i;
  1408. strindex = sechdrs[i].sh_link;
  1409. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1410. }
  1411. #ifndef CONFIG_MODULE_UNLOAD
  1412. /* Don't load .exit sections */
  1413. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1414. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1415. #endif
  1416. }
  1417. modindex = find_sec(hdr, sechdrs, secstrings,
  1418. ".gnu.linkonce.this_module");
  1419. if (!modindex) {
  1420. printk(KERN_WARNING "No module found in object\n");
  1421. err = -ENOEXEC;
  1422. goto free_hdr;
  1423. }
  1424. mod = (void *)sechdrs[modindex].sh_addr;
  1425. if (symindex == 0) {
  1426. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1427. mod->name);
  1428. err = -ENOEXEC;
  1429. goto free_hdr;
  1430. }
  1431. /* Optional sections */
  1432. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1433. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1434. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1435. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1436. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1437. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1438. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1439. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1440. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1441. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1442. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1443. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1444. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1445. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1446. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1447. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1448. #ifdef ARCH_UNWIND_SECTION_NAME
  1449. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1450. #endif
  1451. /* Don't keep modinfo section */
  1452. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1453. #ifdef CONFIG_KALLSYMS
  1454. /* Keep symbol and string tables for decoding later. */
  1455. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1456. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1457. #endif
  1458. if (unwindex)
  1459. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1460. /* Check module struct version now, before we try to use module. */
  1461. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1462. err = -ENOEXEC;
  1463. goto free_hdr;
  1464. }
  1465. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1466. /* This is allowed: modprobe --force will invalidate it. */
  1467. if (!modmagic) {
  1468. add_taint_module(mod, TAINT_FORCED_MODULE);
  1469. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1470. mod->name);
  1471. } else if (!same_magic(modmagic, vermagic)) {
  1472. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1473. mod->name, modmagic, vermagic);
  1474. err = -ENOEXEC;
  1475. goto free_hdr;
  1476. }
  1477. /* Now copy in args */
  1478. args = strndup_user(uargs, ~0UL >> 1);
  1479. if (IS_ERR(args)) {
  1480. err = PTR_ERR(args);
  1481. goto free_hdr;
  1482. }
  1483. if (find_module(mod->name)) {
  1484. err = -EEXIST;
  1485. goto free_mod;
  1486. }
  1487. mod->state = MODULE_STATE_COMING;
  1488. /* Allow arches to frob section contents and sizes. */
  1489. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1490. if (err < 0)
  1491. goto free_mod;
  1492. if (pcpuindex) {
  1493. /* We have a special allocation for this section. */
  1494. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1495. sechdrs[pcpuindex].sh_addralign,
  1496. mod->name);
  1497. if (!percpu) {
  1498. err = -ENOMEM;
  1499. goto free_mod;
  1500. }
  1501. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1502. mod->percpu = percpu;
  1503. }
  1504. /* Determine total sizes, and put offsets in sh_entsize. For now
  1505. this is done generically; there doesn't appear to be any
  1506. special cases for the architectures. */
  1507. layout_sections(mod, hdr, sechdrs, secstrings);
  1508. /* Do the allocs. */
  1509. ptr = module_alloc(mod->core_size);
  1510. if (!ptr) {
  1511. err = -ENOMEM;
  1512. goto free_percpu;
  1513. }
  1514. memset(ptr, 0, mod->core_size);
  1515. mod->module_core = ptr;
  1516. ptr = module_alloc(mod->init_size);
  1517. if (!ptr && mod->init_size) {
  1518. err = -ENOMEM;
  1519. goto free_core;
  1520. }
  1521. memset(ptr, 0, mod->init_size);
  1522. mod->module_init = ptr;
  1523. /* Transfer each section which specifies SHF_ALLOC */
  1524. DEBUGP("final section addresses:\n");
  1525. for (i = 0; i < hdr->e_shnum; i++) {
  1526. void *dest;
  1527. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1528. continue;
  1529. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1530. dest = mod->module_init
  1531. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1532. else
  1533. dest = mod->module_core + sechdrs[i].sh_entsize;
  1534. if (sechdrs[i].sh_type != SHT_NOBITS)
  1535. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1536. sechdrs[i].sh_size);
  1537. /* Update sh_addr to point to copy in image. */
  1538. sechdrs[i].sh_addr = (unsigned long)dest;
  1539. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1540. }
  1541. /* Module has been moved. */
  1542. mod = (void *)sechdrs[modindex].sh_addr;
  1543. /* Now we've moved module, initialize linked lists, etc. */
  1544. module_unload_init(mod);
  1545. /* Set up license info based on the info section */
  1546. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1547. if (strcmp(mod->name, "ndiswrapper") == 0)
  1548. add_taint(TAINT_PROPRIETARY_MODULE);
  1549. if (strcmp(mod->name, "driverloader") == 0)
  1550. add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
  1551. /* Set up MODINFO_ATTR fields */
  1552. setup_modinfo(mod, sechdrs, infoindex);
  1553. /* Fix up syms, so that st_value is a pointer to location. */
  1554. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1555. mod);
  1556. if (err < 0)
  1557. goto cleanup;
  1558. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1559. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1560. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1561. if (crcindex)
  1562. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1563. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1564. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1565. if (gplcrcindex)
  1566. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1567. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1568. sizeof(*mod->gpl_future_syms);
  1569. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1570. sizeof(*mod->unused_syms);
  1571. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1572. sizeof(*mod->unused_gpl_syms);
  1573. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1574. if (gplfuturecrcindex)
  1575. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1576. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1577. if (unusedcrcindex)
  1578. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1579. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1580. if (unusedgplcrcindex)
  1581. mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1582. #ifdef CONFIG_MODVERSIONS
  1583. if ((mod->num_syms && !crcindex) ||
  1584. (mod->num_gpl_syms && !gplcrcindex) ||
  1585. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1586. (mod->num_unused_syms && !unusedcrcindex) ||
  1587. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1588. printk(KERN_WARNING "%s: No versions for exported symbols."
  1589. " Tainting kernel.\n", mod->name);
  1590. add_taint_module(mod, TAINT_FORCED_MODULE);
  1591. }
  1592. #endif
  1593. /* Now do relocations. */
  1594. for (i = 1; i < hdr->e_shnum; i++) {
  1595. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1596. unsigned int info = sechdrs[i].sh_info;
  1597. /* Not a valid relocation section? */
  1598. if (info >= hdr->e_shnum)
  1599. continue;
  1600. /* Don't bother with non-allocated sections */
  1601. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1602. continue;
  1603. if (sechdrs[i].sh_type == SHT_REL)
  1604. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1605. else if (sechdrs[i].sh_type == SHT_RELA)
  1606. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1607. mod);
  1608. if (err < 0)
  1609. goto cleanup;
  1610. }
  1611. /* Find duplicate symbols */
  1612. err = verify_export_symbols(mod);
  1613. if (err < 0)
  1614. goto cleanup;
  1615. /* Set up and sort exception table */
  1616. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1617. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1618. sort_extable(extable, extable + mod->num_exentries);
  1619. /* Finally, copy percpu area over. */
  1620. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1621. sechdrs[pcpuindex].sh_size);
  1622. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1623. err = module_finalize(hdr, sechdrs, mod);
  1624. if (err < 0)
  1625. goto cleanup;
  1626. /* flush the icache in correct context */
  1627. old_fs = get_fs();
  1628. set_fs(KERNEL_DS);
  1629. /*
  1630. * Flush the instruction cache, since we've played with text.
  1631. * Do it before processing of module parameters, so the module
  1632. * can provide parameter accessor functions of its own.
  1633. */
  1634. if (mod->module_init)
  1635. flush_icache_range((unsigned long)mod->module_init,
  1636. (unsigned long)mod->module_init
  1637. + mod->init_size);
  1638. flush_icache_range((unsigned long)mod->module_core,
  1639. (unsigned long)mod->module_core + mod->core_size);
  1640. set_fs(old_fs);
  1641. mod->args = args;
  1642. if (obsparmindex)
  1643. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1644. mod->name);
  1645. /* Size of section 0 is 0, so this works well if no params */
  1646. err = parse_args(mod->name, mod->args,
  1647. (struct kernel_param *)
  1648. sechdrs[setupindex].sh_addr,
  1649. sechdrs[setupindex].sh_size
  1650. / sizeof(struct kernel_param),
  1651. NULL);
  1652. if (err < 0)
  1653. goto arch_cleanup;
  1654. err = mod_sysfs_setup(mod,
  1655. (struct kernel_param *)
  1656. sechdrs[setupindex].sh_addr,
  1657. sechdrs[setupindex].sh_size
  1658. / sizeof(struct kernel_param));
  1659. if (err < 0)
  1660. goto arch_cleanup;
  1661. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1662. /* Size of section 0 is 0, so this works well if no unwind info. */
  1663. mod->unwind_info = unwind_add_table(mod,
  1664. (void *)sechdrs[unwindex].sh_addr,
  1665. sechdrs[unwindex].sh_size);
  1666. /* Get rid of temporary copy */
  1667. vfree(hdr);
  1668. /* Done! */
  1669. return mod;
  1670. arch_cleanup:
  1671. module_arch_cleanup(mod);
  1672. cleanup:
  1673. module_unload_free(mod);
  1674. module_free(mod, mod->module_init);
  1675. free_core:
  1676. module_free(mod, mod->module_core);
  1677. free_percpu:
  1678. if (percpu)
  1679. percpu_modfree(percpu);
  1680. free_mod:
  1681. kfree(args);
  1682. free_hdr:
  1683. vfree(hdr);
  1684. return ERR_PTR(err);
  1685. truncated:
  1686. printk(KERN_ERR "Module len %lu truncated\n", len);
  1687. err = -ENOEXEC;
  1688. goto free_hdr;
  1689. }
  1690. /*
  1691. * link the module with the whole machine is stopped with interrupts off
  1692. * - this defends against kallsyms not taking locks
  1693. */
  1694. static int __link_module(void *_mod)
  1695. {
  1696. struct module *mod = _mod;
  1697. list_add(&mod->list, &modules);
  1698. return 0;
  1699. }
  1700. /* This is where the real work happens */
  1701. asmlinkage long
  1702. sys_init_module(void __user *umod,
  1703. unsigned long len,
  1704. const char __user *uargs)
  1705. {
  1706. struct module *mod;
  1707. int ret = 0;
  1708. /* Must have permission */
  1709. if (!capable(CAP_SYS_MODULE))
  1710. return -EPERM;
  1711. /* Only one module load at a time, please */
  1712. if (mutex_lock_interruptible(&module_mutex) != 0)
  1713. return -EINTR;
  1714. /* Do all the hard work */
  1715. mod = load_module(umod, len, uargs);
  1716. if (IS_ERR(mod)) {
  1717. mutex_unlock(&module_mutex);
  1718. return PTR_ERR(mod);
  1719. }
  1720. /* Now sew it into the lists. They won't access us, since
  1721. strong_try_module_get() will fail. */
  1722. stop_machine_run(__link_module, mod, NR_CPUS);
  1723. /* Drop lock so they can recurse */
  1724. mutex_unlock(&module_mutex);
  1725. blocking_notifier_call_chain(&module_notify_list,
  1726. MODULE_STATE_COMING, mod);
  1727. /* Start the module */
  1728. if (mod->init != NULL)
  1729. ret = mod->init();
  1730. if (ret < 0) {
  1731. /* Init routine failed: abort. Try to protect us from
  1732. buggy refcounters. */
  1733. mod->state = MODULE_STATE_GOING;
  1734. synchronize_sched();
  1735. if (mod->unsafe)
  1736. printk(KERN_ERR "%s: module is now stuck!\n",
  1737. mod->name);
  1738. else {
  1739. module_put(mod);
  1740. mutex_lock(&module_mutex);
  1741. free_module(mod);
  1742. mutex_unlock(&module_mutex);
  1743. }
  1744. return ret;
  1745. }
  1746. /* Now it's a first class citizen! */
  1747. mutex_lock(&module_mutex);
  1748. mod->state = MODULE_STATE_LIVE;
  1749. /* Drop initial reference. */
  1750. module_put(mod);
  1751. unwind_remove_table(mod->unwind_info, 1);
  1752. module_free(mod, mod->module_init);
  1753. mod->module_init = NULL;
  1754. mod->init_size = 0;
  1755. mod->init_text_size = 0;
  1756. mutex_unlock(&module_mutex);
  1757. return 0;
  1758. }
  1759. static inline int within(unsigned long addr, void *start, unsigned long size)
  1760. {
  1761. return ((void *)addr >= start && (void *)addr < start + size);
  1762. }
  1763. #ifdef CONFIG_KALLSYMS
  1764. /*
  1765. * This ignores the intensely annoying "mapping symbols" found
  1766. * in ARM ELF files: $a, $t and $d.
  1767. */
  1768. static inline int is_arm_mapping_symbol(const char *str)
  1769. {
  1770. return str[0] == '$' && strchr("atd", str[1])
  1771. && (str[2] == '\0' || str[2] == '.');
  1772. }
  1773. static const char *get_ksymbol(struct module *mod,
  1774. unsigned long addr,
  1775. unsigned long *size,
  1776. unsigned long *offset)
  1777. {
  1778. unsigned int i, best = 0;
  1779. unsigned long nextval;
  1780. /* At worse, next value is at end of module */
  1781. if (within(addr, mod->module_init, mod->init_size))
  1782. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1783. else
  1784. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1785. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1786. starts real symbols at 1). */
  1787. for (i = 1; i < mod->num_symtab; i++) {
  1788. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1789. continue;
  1790. /* We ignore unnamed symbols: they're uninformative
  1791. * and inserted at a whim. */
  1792. if (mod->symtab[i].st_value <= addr
  1793. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1794. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1795. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1796. best = i;
  1797. if (mod->symtab[i].st_value > addr
  1798. && mod->symtab[i].st_value < nextval
  1799. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1800. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1801. nextval = mod->symtab[i].st_value;
  1802. }
  1803. if (!best)
  1804. return NULL;
  1805. *size = nextval - mod->symtab[best].st_value;
  1806. *offset = addr - mod->symtab[best].st_value;
  1807. return mod->strtab + mod->symtab[best].st_name;
  1808. }
  1809. /* For kallsyms to ask for address resolution. NULL means not found.
  1810. We don't lock, as this is used for oops resolution and races are a
  1811. lesser concern. */
  1812. const char *module_address_lookup(unsigned long addr,
  1813. unsigned long *size,
  1814. unsigned long *offset,
  1815. char **modname)
  1816. {
  1817. struct module *mod;
  1818. list_for_each_entry(mod, &modules, list) {
  1819. if (within(addr, mod->module_init, mod->init_size)
  1820. || within(addr, mod->module_core, mod->core_size)) {
  1821. if (modname)
  1822. *modname = mod->name;
  1823. return get_ksymbol(mod, addr, size, offset);
  1824. }
  1825. }
  1826. return NULL;
  1827. }
  1828. struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
  1829. char *type, char *name, size_t namelen)
  1830. {
  1831. struct module *mod;
  1832. mutex_lock(&module_mutex);
  1833. list_for_each_entry(mod, &modules, list) {
  1834. if (symnum < mod->num_symtab) {
  1835. *value = mod->symtab[symnum].st_value;
  1836. *type = mod->symtab[symnum].st_info;
  1837. strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
  1838. namelen);
  1839. mutex_unlock(&module_mutex);
  1840. return mod;
  1841. }
  1842. symnum -= mod->num_symtab;
  1843. }
  1844. mutex_unlock(&module_mutex);
  1845. return NULL;
  1846. }
  1847. static unsigned long mod_find_symname(struct module *mod, const char *name)
  1848. {
  1849. unsigned int i;
  1850. for (i = 0; i < mod->num_symtab; i++)
  1851. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  1852. mod->symtab[i].st_info != 'U')
  1853. return mod->symtab[i].st_value;
  1854. return 0;
  1855. }
  1856. /* Look for this name: can be of form module:name. */
  1857. unsigned long module_kallsyms_lookup_name(const char *name)
  1858. {
  1859. struct module *mod;
  1860. char *colon;
  1861. unsigned long ret = 0;
  1862. /* Don't lock: we're in enough trouble already. */
  1863. if ((colon = strchr(name, ':')) != NULL) {
  1864. *colon = '\0';
  1865. if ((mod = find_module(name)) != NULL)
  1866. ret = mod_find_symname(mod, colon+1);
  1867. *colon = ':';
  1868. } else {
  1869. list_for_each_entry(mod, &modules, list)
  1870. if ((ret = mod_find_symname(mod, name)) != 0)
  1871. break;
  1872. }
  1873. return ret;
  1874. }
  1875. #endif /* CONFIG_KALLSYMS */
  1876. /* Called by the /proc file system to return a list of modules. */
  1877. static void *m_start(struct seq_file *m, loff_t *pos)
  1878. {
  1879. struct list_head *i;
  1880. loff_t n = 0;
  1881. mutex_lock(&module_mutex);
  1882. list_for_each(i, &modules) {
  1883. if (n++ == *pos)
  1884. break;
  1885. }
  1886. if (i == &modules)
  1887. return NULL;
  1888. return i;
  1889. }
  1890. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  1891. {
  1892. struct list_head *i = p;
  1893. (*pos)++;
  1894. if (i->next == &modules)
  1895. return NULL;
  1896. return i->next;
  1897. }
  1898. static void m_stop(struct seq_file *m, void *p)
  1899. {
  1900. mutex_unlock(&module_mutex);
  1901. }
  1902. static char *taint_flags(unsigned int taints, char *buf)
  1903. {
  1904. int bx = 0;
  1905. if (taints) {
  1906. buf[bx++] = '(';
  1907. if (taints & TAINT_PROPRIETARY_MODULE)
  1908. buf[bx++] = 'P';
  1909. if (taints & TAINT_FORCED_MODULE)
  1910. buf[bx++] = 'F';
  1911. /*
  1912. * TAINT_FORCED_RMMOD: could be added.
  1913. * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
  1914. * apply to modules.
  1915. */
  1916. buf[bx++] = ')';
  1917. }
  1918. buf[bx] = '\0';
  1919. return buf;
  1920. }
  1921. static int m_show(struct seq_file *m, void *p)
  1922. {
  1923. struct module *mod = list_entry(p, struct module, list);
  1924. char buf[8];
  1925. seq_printf(m, "%s %lu",
  1926. mod->name, mod->init_size + mod->core_size);
  1927. print_unload_info(m, mod);
  1928. /* Informative for users. */
  1929. seq_printf(m, " %s",
  1930. mod->state == MODULE_STATE_GOING ? "Unloading":
  1931. mod->state == MODULE_STATE_COMING ? "Loading":
  1932. "Live");
  1933. /* Used by oprofile and other similar tools. */
  1934. seq_printf(m, " 0x%p", mod->module_core);
  1935. /* Taints info */
  1936. if (mod->taints)
  1937. seq_printf(m, " %s", taint_flags(mod->taints, buf));
  1938. seq_printf(m, "\n");
  1939. return 0;
  1940. }
  1941. /* Format: modulename size refcount deps address
  1942. Where refcount is a number or -, and deps is a comma-separated list
  1943. of depends or -.
  1944. */
  1945. const struct seq_operations modules_op = {
  1946. .start = m_start,
  1947. .next = m_next,
  1948. .stop = m_stop,
  1949. .show = m_show
  1950. };
  1951. /* Given an address, look for it in the module exception tables. */
  1952. const struct exception_table_entry *search_module_extables(unsigned long addr)
  1953. {
  1954. unsigned long flags;
  1955. const struct exception_table_entry *e = NULL;
  1956. struct module *mod;
  1957. spin_lock_irqsave(&modlist_lock, flags);
  1958. list_for_each_entry(mod, &modules, list) {
  1959. if (mod->num_exentries == 0)
  1960. continue;
  1961. e = search_extable(mod->extable,
  1962. mod->extable + mod->num_exentries - 1,
  1963. addr);
  1964. if (e)
  1965. break;
  1966. }
  1967. spin_unlock_irqrestore(&modlist_lock, flags);
  1968. /* Now, if we found one, we are running inside it now, hence
  1969. we cannot unload the module, hence no refcnt needed. */
  1970. return e;
  1971. }
  1972. /*
  1973. * Is this a valid module address?
  1974. */
  1975. int is_module_address(unsigned long addr)
  1976. {
  1977. unsigned long flags;
  1978. struct module *mod;
  1979. spin_lock_irqsave(&modlist_lock, flags);
  1980. list_for_each_entry(mod, &modules, list) {
  1981. if (within(addr, mod->module_core, mod->core_size)) {
  1982. spin_unlock_irqrestore(&modlist_lock, flags);
  1983. return 1;
  1984. }
  1985. }
  1986. spin_unlock_irqrestore(&modlist_lock, flags);
  1987. return 0;
  1988. }
  1989. /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
  1990. struct module *__module_text_address(unsigned long addr)
  1991. {
  1992. struct module *mod;
  1993. list_for_each_entry(mod, &modules, list)
  1994. if (within(addr, mod->module_init, mod->init_text_size)
  1995. || within(addr, mod->module_core, mod->core_text_size))
  1996. return mod;
  1997. return NULL;
  1998. }
  1999. struct module *module_text_address(unsigned long addr)
  2000. {
  2001. struct module *mod;
  2002. unsigned long flags;
  2003. spin_lock_irqsave(&modlist_lock, flags);
  2004. mod = __module_text_address(addr);
  2005. spin_unlock_irqrestore(&modlist_lock, flags);
  2006. return mod;
  2007. }
  2008. /* Don't grab lock, we're oopsing. */
  2009. void print_modules(void)
  2010. {
  2011. struct module *mod;
  2012. char buf[8];
  2013. printk("Modules linked in:");
  2014. list_for_each_entry(mod, &modules, list)
  2015. printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
  2016. printk("\n");
  2017. }
  2018. static char *make_driver_name(struct device_driver *drv)
  2019. {
  2020. char *driver_name;
  2021. driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
  2022. GFP_KERNEL);
  2023. if (!driver_name)
  2024. return NULL;
  2025. sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
  2026. return driver_name;
  2027. }
  2028. void module_add_driver(struct module *mod, struct device_driver *drv)
  2029. {
  2030. char *driver_name;
  2031. int no_warn;
  2032. if (!mod || !drv)
  2033. return;
  2034. /* Don't check return codes; these calls are idempotent */
  2035. no_warn = sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
  2036. driver_name = make_driver_name(drv);
  2037. if (driver_name) {
  2038. no_warn = sysfs_create_link(mod->drivers_dir, &drv->kobj,
  2039. driver_name);
  2040. kfree(driver_name);
  2041. }
  2042. }
  2043. EXPORT_SYMBOL(module_add_driver);
  2044. void module_remove_driver(struct device_driver *drv)
  2045. {
  2046. char *driver_name;
  2047. if (!drv)
  2048. return;
  2049. sysfs_remove_link(&drv->kobj, "module");
  2050. if (drv->owner && drv->owner->drivers_dir) {
  2051. driver_name = make_driver_name(drv);
  2052. if (driver_name) {
  2053. sysfs_remove_link(drv->owner->drivers_dir,
  2054. driver_name);
  2055. kfree(driver_name);
  2056. }
  2057. }
  2058. }
  2059. EXPORT_SYMBOL(module_remove_driver);
  2060. #ifdef CONFIG_MODVERSIONS
  2061. /* Generate the signature for struct module here, too, for modversions. */
  2062. void struct_module(struct module *mod) { return; }
  2063. EXPORT_SYMBOL(struct_module);
  2064. #endif