module.c 90 KB

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