module.c 96 KB

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