module.c 103 KB

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