module.c 101 KB

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