module.c 94 KB

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