module.c 96 KB

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