module.c 98 KB

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