module.c 110 KB

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