module.c 110 KB

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