module.c 110 KB

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