module.c 93 KB

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