module.c 94 KB

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