module.c 111 KB

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