namespace.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/namespace.c
  4. *
  5. * (C) Copyright Al Viro 2000, 2001
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.h>
  17. #include <linux/cred.h>
  18. #include <linux/idr.h>
  19. #include <linux/init.h> /* init_rootfs */
  20. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  21. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  22. #include <linux/file.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/proc_ns.h>
  25. #include <linux/magic.h>
  26. #include <linux/memblock.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/task_work.h>
  29. #include <linux/sched/task.h>
  30. #include <uapi/linux/mount.h>
  31. #include <linux/fs_context.h>
  32. #include <linux/shmem_fs.h>
  33. #include <linux/mnt_idmapping.h>
  34. #if defined(CONFIG_KSU_SUSFS_SUS_MOUNT) || defined(CONFIG_KSU_SUSFS_TRY_UMOUNT)
  35. #include <linux/susfs_def.h>
  36. #endif
  37. #ifdef CONFIG_KDP_NS
  38. #include <linux/kdp.h>
  39. #endif
  40. #include "pnode.h"
  41. #include "internal.h"
  42. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  43. extern bool susfs_is_current_ksu_domain(void);
  44. extern bool susfs_is_current_zygote_domain(void);
  45. static DEFINE_IDA(susfs_mnt_id_ida);
  46. static DEFINE_IDA(susfs_mnt_group_ida);
  47. #define CL_ZYGOTE_COPY_MNT_NS BIT(24) /* used by copy_mnt_ns() */
  48. #define CL_COPY_MNT_NS BIT(25) /* used by copy_mnt_ns() */
  49. #endif
  50. #ifdef CONFIG_KSU_SUSFS_AUTO_ADD_SUS_KSU_DEFAULT_MOUNT
  51. extern void susfs_auto_add_sus_ksu_default_mount(const char __user *to_pathname);
  52. bool susfs_is_auto_add_sus_ksu_default_mount_enabled = true;
  53. #endif
  54. #ifdef CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT
  55. extern int susfs_auto_add_sus_bind_mount(const char *pathname, struct path *path_target);
  56. bool susfs_is_auto_add_sus_bind_mount_enabled = true;
  57. #endif
  58. #ifdef CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT
  59. extern void susfs_auto_add_try_umount_for_bind_mount(struct path *path);
  60. bool susfs_is_auto_add_try_umount_for_bind_mount_enabled = true;
  61. #endif
  62. /* Maximum number of mounts in a mount namespace */
  63. static unsigned int sysctl_mount_max __read_mostly = 100000;
  64. static unsigned int m_hash_mask __read_mostly;
  65. static unsigned int m_hash_shift __read_mostly;
  66. static unsigned int mp_hash_mask __read_mostly;
  67. static unsigned int mp_hash_shift __read_mostly;
  68. static __initdata unsigned long mhash_entries;
  69. static int __init set_mhash_entries(char *str)
  70. {
  71. if (!str)
  72. return 0;
  73. mhash_entries = simple_strtoul(str, &str, 0);
  74. return 1;
  75. }
  76. __setup("mhash_entries=", set_mhash_entries);
  77. static __initdata unsigned long mphash_entries;
  78. static int __init set_mphash_entries(char *str)
  79. {
  80. if (!str)
  81. return 0;
  82. mphash_entries = simple_strtoul(str, &str, 0);
  83. return 1;
  84. }
  85. __setup("mphash_entries=", set_mphash_entries);
  86. static u64 event;
  87. static DEFINE_IDA(mnt_id_ida);
  88. static DEFINE_IDA(mnt_group_ida);
  89. static struct hlist_head *mount_hashtable __read_mostly;
  90. static struct hlist_head *mountpoint_hashtable __read_mostly;
  91. static struct kmem_cache *mnt_cache __read_mostly;
  92. static DECLARE_RWSEM(namespace_sem);
  93. static HLIST_HEAD(unmounted); /* protected by namespace_sem */
  94. static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
  95. struct mount_kattr {
  96. unsigned int attr_set;
  97. unsigned int attr_clr;
  98. unsigned int propagation;
  99. unsigned int lookup_flags;
  100. bool recurse;
  101. struct user_namespace *mnt_userns;
  102. };
  103. /* /sys/fs */
  104. struct kobject *fs_kobj;
  105. EXPORT_SYMBOL_GPL(fs_kobj);
  106. /*
  107. * vfsmount lock may be taken for read to prevent changes to the
  108. * vfsmount hash, ie. during mountpoint lookups or walking back
  109. * up the tree.
  110. *
  111. * It should be taken for write in all cases where the vfsmount
  112. * tree or hash is modified or when a vfsmount structure is modified.
  113. */
  114. __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
  115. static inline void lock_mount_hash(void)
  116. {
  117. write_seqlock(&mount_lock);
  118. }
  119. static inline void unlock_mount_hash(void)
  120. {
  121. write_sequnlock(&mount_lock);
  122. }
  123. static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
  124. {
  125. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  126. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  127. tmp = tmp + (tmp >> m_hash_shift);
  128. return &mount_hashtable[tmp & m_hash_mask];
  129. }
  130. static inline struct hlist_head *mp_hash(struct dentry *dentry)
  131. {
  132. unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
  133. tmp = tmp + (tmp >> mp_hash_shift);
  134. return &mountpoint_hashtable[tmp & mp_hash_mask];
  135. }
  136. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  137. // Our own mnt_alloc_id() that assigns mnt_id starting from DEFAULT_SUS_MNT_ID
  138. static int susfs_mnt_alloc_id(struct mount *mnt)
  139. {
  140. int res = ida_alloc_min(&susfs_mnt_id_ida, DEFAULT_SUS_MNT_ID, GFP_KERNEL);
  141. if (res < 0)
  142. return res;
  143. mnt->mnt_id = res;
  144. return 0;
  145. }
  146. #endif
  147. static int mnt_alloc_id(struct mount *mnt)
  148. {
  149. int res = ida_alloc(&mnt_id_ida, GFP_KERNEL);
  150. if (res < 0)
  151. return res;
  152. mnt->mnt_id = res;
  153. return 0;
  154. }
  155. static void mnt_free_id(struct mount *mnt)
  156. {
  157. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  158. // We should first check the 'mnt->mnt.susfs_mnt_id_backup', see if it is DEFAULT_SUS_MNT_ID_FOR_KSU_PROC_UNSHARE
  159. // if so, these mnt_id were not assigned by mnt_alloc_id() so we don't need to free it.
  160. if (unlikely(mnt->mnt.susfs_mnt_id_backup == DEFAULT_SUS_MNT_ID_FOR_KSU_PROC_UNSHARE)) {
  161. return;
  162. }
  163. // Now we can check if its mnt_id is sus
  164. if (unlikely(mnt->mnt_id >= DEFAULT_SUS_MNT_ID)) {
  165. ida_free(&susfs_mnt_id_ida, mnt->mnt_id);
  166. return;
  167. }
  168. // Lastly if 'mnt->mnt.susfs_mnt_id_backup' is not 0, then it contains a backup origin mnt_id
  169. // so we free it in the original way
  170. if (likely(mnt->mnt.susfs_mnt_id_backup)) {
  171. // If mnt->mnt.susfs_mnt_id_backup is not zero, it means mnt->mnt_id is spoofed,
  172. // so here we return the original mnt_id for being freed.
  173. ida_free(&mnt_id_ida, mnt->mnt.susfs_mnt_id_backup);
  174. return;
  175. }
  176. #endif
  177. ida_free(&mnt_id_ida, mnt->mnt_id);
  178. }
  179. /*
  180. * Allocate a new peer group ID
  181. */
  182. static int mnt_alloc_group_id(struct mount *mnt)
  183. {
  184. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  185. int res;
  186. // Check if mnt has sus mnt_id
  187. if (mnt->mnt_id >= DEFAULT_SUS_MNT_ID) {
  188. // If so, assign a sus mnt_group id DEFAULT_SUS_MNT_GROUP_ID from susfs_mnt_group_ida
  189. res = ida_alloc_min(&susfs_mnt_group_ida, DEFAULT_SUS_MNT_GROUP_ID, GFP_KERNEL);
  190. goto bypass_orig_flow;
  191. }
  192. res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
  193. bypass_orig_flow:
  194. #else
  195. int res = ida_alloc_min(&mnt_group_ida, 1, GFP_KERNEL);
  196. #endif
  197. if (res < 0)
  198. return res;
  199. mnt->mnt_group_id = res;
  200. return 0;
  201. }
  202. /*
  203. * Release a peer group ID
  204. */
  205. void mnt_release_group_id(struct mount *mnt)
  206. {
  207. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  208. // If mnt->mnt_group_id >= DEFAULT_SUS_MNT_GROUP_ID, it means 'mnt' is also sus mount,
  209. // then we free the mnt->mnt_group_id from susfs_mnt_group_ida
  210. if (mnt->mnt_group_id >= DEFAULT_SUS_MNT_GROUP_ID) {
  211. ida_free(&susfs_mnt_group_ida, mnt->mnt_group_id);
  212. mnt->mnt_group_id = 0;
  213. return;
  214. }
  215. #endif
  216. ida_free(&mnt_group_ida, mnt->mnt_group_id);
  217. mnt->mnt_group_id = 0;
  218. }
  219. /*
  220. * vfsmount lock must be held for read
  221. */
  222. static inline void mnt_add_count(struct mount *mnt, int n)
  223. {
  224. #ifdef CONFIG_SMP
  225. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  226. #else
  227. preempt_disable();
  228. mnt->mnt_count += n;
  229. preempt_enable();
  230. #endif
  231. }
  232. /*
  233. * vfsmount lock must be held for write
  234. */
  235. int mnt_get_count(struct mount *mnt)
  236. {
  237. #ifdef CONFIG_SMP
  238. int count = 0;
  239. int cpu;
  240. for_each_possible_cpu(cpu) {
  241. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  242. }
  243. return count;
  244. #else
  245. return mnt->mnt_count;
  246. #endif
  247. }
  248. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  249. static struct mount *alloc_vfsmnt(const char *name, bool should_spoof, int custom_mnt_id)
  250. #else
  251. static struct mount *alloc_vfsmnt(const char *name)
  252. #endif
  253. {
  254. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  255. if (mnt) {
  256. int err;
  257. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  258. if (should_spoof) {
  259. if (!custom_mnt_id) {
  260. err = susfs_mnt_alloc_id(mnt);
  261. } else {
  262. mnt->mnt_id = custom_mnt_id;
  263. err = 0;
  264. }
  265. goto bypass_orig_flow;
  266. }
  267. #endif
  268. err = mnt_alloc_id(mnt);
  269. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  270. bypass_orig_flow:
  271. #endif
  272. if (err)
  273. goto out_free_cache;
  274. #ifdef CONFIG_KDP_NS
  275. err = kdp_mnt_alloc_vfsmount(mnt);
  276. if (err)
  277. goto out_free_cache;
  278. #endif
  279. if (name) {
  280. mnt->mnt_devname = kstrdup_const(name,
  281. GFP_KERNEL_ACCOUNT);
  282. if (!mnt->mnt_devname)
  283. goto out_free_id;
  284. }
  285. #ifdef CONFIG_SMP
  286. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  287. if (!mnt->mnt_pcp)
  288. goto out_free_devname;
  289. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  290. #else
  291. mnt->mnt_count = 1;
  292. mnt->mnt_writers = 0;
  293. #endif
  294. INIT_HLIST_NODE(&mnt->mnt_hash);
  295. INIT_LIST_HEAD(&mnt->mnt_child);
  296. INIT_LIST_HEAD(&mnt->mnt_mounts);
  297. INIT_LIST_HEAD(&mnt->mnt_list);
  298. INIT_LIST_HEAD(&mnt->mnt_expire);
  299. INIT_LIST_HEAD(&mnt->mnt_share);
  300. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  301. INIT_LIST_HEAD(&mnt->mnt_slave);
  302. INIT_HLIST_NODE(&mnt->mnt_mp_list);
  303. INIT_LIST_HEAD(&mnt->mnt_umounting);
  304. INIT_HLIST_HEAD(&mnt->mnt_stuck_children);
  305. #ifdef CONFIG_KDP_NS
  306. kdp_set_mnt_userns(((struct kdp_mount *)mnt)->mnt, &init_user_ns);
  307. #else
  308. mnt->mnt.mnt_userns = &init_user_ns;
  309. #endif
  310. }
  311. return mnt;
  312. #ifdef CONFIG_SMP
  313. out_free_devname:
  314. kfree_const(mnt->mnt_devname);
  315. #endif
  316. out_free_id:
  317. mnt_free_id(mnt);
  318. out_free_cache:
  319. kmem_cache_free(mnt_cache, mnt);
  320. return NULL;
  321. }
  322. /*
  323. * Most r/o checks on a fs are for operations that take
  324. * discrete amounts of time, like a write() or unlink().
  325. * We must keep track of when those operations start
  326. * (for permission checks) and when they end, so that
  327. * we can determine when writes are able to occur to
  328. * a filesystem.
  329. */
  330. /*
  331. * __mnt_is_readonly: check whether a mount is read-only
  332. * @mnt: the mount to check for its write status
  333. *
  334. * This shouldn't be used directly ouside of the VFS.
  335. * It does not guarantee that the filesystem will stay
  336. * r/w, just that it is right *now*. This can not and
  337. * should not be used in place of IS_RDONLY(inode).
  338. * mnt_want/drop_write() will _keep_ the filesystem
  339. * r/w.
  340. */
  341. bool __mnt_is_readonly(struct vfsmount *mnt)
  342. {
  343. return (mnt->mnt_flags & MNT_READONLY) || sb_rdonly(mnt->mnt_sb);
  344. }
  345. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  346. static inline void mnt_inc_writers(struct mount *mnt)
  347. {
  348. #ifdef CONFIG_SMP
  349. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  350. #else
  351. mnt->mnt_writers++;
  352. #endif
  353. }
  354. static inline void mnt_dec_writers(struct mount *mnt)
  355. {
  356. #ifdef CONFIG_SMP
  357. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  358. #else
  359. mnt->mnt_writers--;
  360. #endif
  361. }
  362. static unsigned int mnt_get_writers(struct mount *mnt)
  363. {
  364. #ifdef CONFIG_SMP
  365. unsigned int count = 0;
  366. int cpu;
  367. for_each_possible_cpu(cpu) {
  368. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  369. }
  370. return count;
  371. #else
  372. return mnt->mnt_writers;
  373. #endif
  374. }
  375. static int mnt_is_readonly(struct vfsmount *mnt)
  376. {
  377. if (mnt->mnt_sb->s_readonly_remount)
  378. return 1;
  379. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  380. smp_rmb();
  381. return __mnt_is_readonly(mnt);
  382. }
  383. /*
  384. * Most r/o & frozen checks on a fs are for operations that take discrete
  385. * amounts of time, like a write() or unlink(). We must keep track of when
  386. * those operations start (for permission checks) and when they end, so that we
  387. * can determine when writes are able to occur to a filesystem.
  388. */
  389. /**
  390. * __mnt_want_write - get write access to a mount without freeze protection
  391. * @m: the mount on which to take a write
  392. *
  393. * This tells the low-level filesystem that a write is about to be performed to
  394. * it, and makes sure that writes are allowed (mnt it read-write) before
  395. * returning success. This operation does not protect against filesystem being
  396. * frozen. When the write operation is finished, __mnt_drop_write() must be
  397. * called. This is effectively a refcount.
  398. */
  399. int __mnt_want_write(struct vfsmount *m)
  400. {
  401. struct mount *mnt = real_mount(m);
  402. int ret = 0;
  403. preempt_disable();
  404. mnt_inc_writers(mnt);
  405. /*
  406. * The store to mnt_inc_writers must be visible before we pass
  407. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  408. * incremented count after it has set MNT_WRITE_HOLD.
  409. */
  410. smp_mb();
  411. might_lock(&mount_lock.lock);
  412. #ifdef CONFIG_KDP_NS
  413. while (READ_ONCE(((struct kdp_mount *)mnt)->mnt->mnt_flags) & MNT_WRITE_HOLD) {
  414. #else
  415. while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD) {
  416. #endif
  417. if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
  418. cpu_relax();
  419. } else {
  420. /*
  421. * This prevents priority inversion, if the task
  422. * setting MNT_WRITE_HOLD got preempted on a remote
  423. * CPU, and it prevents life lock if the task setting
  424. * MNT_WRITE_HOLD has a lower priority and is bound to
  425. * the same CPU as the task that is spinning here.
  426. */
  427. preempt_enable();
  428. lock_mount_hash();
  429. unlock_mount_hash();
  430. preempt_disable();
  431. }
  432. }
  433. /*
  434. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  435. * be set to match its requirements. So we must not load that until
  436. * MNT_WRITE_HOLD is cleared.
  437. */
  438. smp_rmb();
  439. if (mnt_is_readonly(m)) {
  440. mnt_dec_writers(mnt);
  441. ret = -EROFS;
  442. }
  443. preempt_enable();
  444. return ret;
  445. }
  446. /**
  447. * mnt_want_write - get write access to a mount
  448. * @m: the mount on which to take a write
  449. *
  450. * This tells the low-level filesystem that a write is about to be performed to
  451. * it, and makes sure that writes are allowed (mount is read-write, filesystem
  452. * is not frozen) before returning success. When the write operation is
  453. * finished, mnt_drop_write() must be called. This is effectively a refcount.
  454. */
  455. int mnt_want_write(struct vfsmount *m)
  456. {
  457. int ret;
  458. sb_start_write(m->mnt_sb);
  459. ret = __mnt_want_write(m);
  460. if (ret)
  461. sb_end_write(m->mnt_sb);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(mnt_want_write);
  465. /**
  466. * __mnt_want_write_file - get write access to a file's mount
  467. * @file: the file who's mount on which to take a write
  468. *
  469. * This is like __mnt_want_write, but if the file is already open for writing it
  470. * skips incrementing mnt_writers (since the open file already has a reference)
  471. * and instead only does the check for emergency r/o remounts. This must be
  472. * paired with __mnt_drop_write_file.
  473. */
  474. int __mnt_want_write_file(struct file *file)
  475. {
  476. if (file->f_mode & FMODE_WRITER) {
  477. /*
  478. * Superblock may have become readonly while there are still
  479. * writable fd's, e.g. due to a fs error with errors=remount-ro
  480. */
  481. if (__mnt_is_readonly(file->f_path.mnt))
  482. return -EROFS;
  483. return 0;
  484. }
  485. return __mnt_want_write(file->f_path.mnt);
  486. }
  487. /**
  488. * mnt_want_write_file - get write access to a file's mount
  489. * @file: the file who's mount on which to take a write
  490. *
  491. * This is like mnt_want_write, but if the file is already open for writing it
  492. * skips incrementing mnt_writers (since the open file already has a reference)
  493. * and instead only does the freeze protection and the check for emergency r/o
  494. * remounts. This must be paired with mnt_drop_write_file.
  495. */
  496. int mnt_want_write_file(struct file *file)
  497. {
  498. int ret;
  499. sb_start_write(file_inode(file)->i_sb);
  500. ret = __mnt_want_write_file(file);
  501. if (ret)
  502. sb_end_write(file_inode(file)->i_sb);
  503. return ret;
  504. }
  505. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  506. /**
  507. * __mnt_drop_write - give up write access to a mount
  508. * @mnt: the mount on which to give up write access
  509. *
  510. * Tells the low-level filesystem that we are done
  511. * performing writes to it. Must be matched with
  512. * __mnt_want_write() call above.
  513. */
  514. void __mnt_drop_write(struct vfsmount *mnt)
  515. {
  516. preempt_disable();
  517. mnt_dec_writers(real_mount(mnt));
  518. preempt_enable();
  519. }
  520. /**
  521. * mnt_drop_write - give up write access to a mount
  522. * @mnt: the mount on which to give up write access
  523. *
  524. * Tells the low-level filesystem that we are done performing writes to it and
  525. * also allows filesystem to be frozen again. Must be matched with
  526. * mnt_want_write() call above.
  527. */
  528. void mnt_drop_write(struct vfsmount *mnt)
  529. {
  530. __mnt_drop_write(mnt);
  531. sb_end_write(mnt->mnt_sb);
  532. }
  533. EXPORT_SYMBOL_GPL(mnt_drop_write);
  534. void __mnt_drop_write_file(struct file *file)
  535. {
  536. if (!(file->f_mode & FMODE_WRITER))
  537. __mnt_drop_write(file->f_path.mnt);
  538. }
  539. void mnt_drop_write_file(struct file *file)
  540. {
  541. __mnt_drop_write_file(file);
  542. sb_end_write(file_inode(file)->i_sb);
  543. }
  544. EXPORT_SYMBOL(mnt_drop_write_file);
  545. /**
  546. * mnt_hold_writers - prevent write access to the given mount
  547. * @mnt: mnt to prevent write access to
  548. *
  549. * Prevents write access to @mnt if there are no active writers for @mnt.
  550. * This function needs to be called and return successfully before changing
  551. * properties of @mnt that need to remain stable for callers with write access
  552. * to @mnt.
  553. *
  554. * After this functions has been called successfully callers must pair it with
  555. * a call to mnt_unhold_writers() in order to stop preventing write access to
  556. * @mnt.
  557. *
  558. * Context: This function expects lock_mount_hash() to be held serializing
  559. * setting MNT_WRITE_HOLD.
  560. * Return: On success 0 is returned.
  561. * On error, -EBUSY is returned.
  562. */
  563. static inline int mnt_hold_writers(struct mount *mnt)
  564. {
  565. #ifdef CONFIG_KDP_NS
  566. kdp_set_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_WRITE_HOLD);
  567. #else
  568. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  569. #endif
  570. /*
  571. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  572. * should be visible before we do.
  573. */
  574. smp_mb();
  575. /*
  576. * With writers on hold, if this value is zero, then there are
  577. * definitely no active writers (although held writers may subsequently
  578. * increment the count, they'll have to wait, and decrement it after
  579. * seeing MNT_READONLY).
  580. *
  581. * It is OK to have counter incremented on one CPU and decremented on
  582. * another: the sum will add up correctly. The danger would be when we
  583. * sum up each counter, if we read a counter before it is incremented,
  584. * but then read another CPU's count which it has been subsequently
  585. * decremented from -- we would see more decrements than we should.
  586. * MNT_WRITE_HOLD protects against this scenario, because
  587. * mnt_want_write first increments count, then smp_mb, then spins on
  588. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  589. * we're counting up here.
  590. */
  591. if (mnt_get_writers(mnt) > 0)
  592. return -EBUSY;
  593. return 0;
  594. }
  595. /**
  596. * mnt_unhold_writers - stop preventing write access to the given mount
  597. * @mnt: mnt to stop preventing write access to
  598. *
  599. * Stop preventing write access to @mnt allowing callers to gain write access
  600. * to @mnt again.
  601. *
  602. * This function can only be called after a successful call to
  603. * mnt_hold_writers().
  604. *
  605. * Context: This function expects lock_mount_hash() to be held.
  606. */
  607. static inline void mnt_unhold_writers(struct mount *mnt)
  608. {
  609. /*
  610. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  611. * that become unheld will see MNT_READONLY.
  612. */
  613. smp_wmb();
  614. #ifdef CONFIG_KDP_NS
  615. kdp_clear_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_WRITE_HOLD);
  616. #else
  617. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  618. #endif
  619. }
  620. static int mnt_make_readonly(struct mount *mnt)
  621. {
  622. int ret;
  623. ret = mnt_hold_writers(mnt);
  624. if (!ret)
  625. #ifdef CONFIG_KDP_NS
  626. kdp_set_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_READONLY);
  627. #else
  628. mnt->mnt.mnt_flags |= MNT_READONLY;
  629. #endif
  630. mnt_unhold_writers(mnt);
  631. return ret;
  632. }
  633. int sb_prepare_remount_readonly(struct super_block *sb)
  634. {
  635. struct mount *mnt;
  636. int err = 0;
  637. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  638. if (atomic_long_read(&sb->s_remove_count))
  639. return -EBUSY;
  640. lock_mount_hash();
  641. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  642. #ifdef CONFIG_KDP_NS
  643. if (!(((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_READONLY)) {
  644. #else
  645. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  646. #endif
  647. err = mnt_hold_writers(mnt);
  648. if (err)
  649. break;
  650. }
  651. }
  652. if (!err && atomic_long_read(&sb->s_remove_count))
  653. err = -EBUSY;
  654. if (!err) {
  655. sb->s_readonly_remount = 1;
  656. smp_wmb();
  657. }
  658. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  659. #ifdef CONFIG_KDP_NS
  660. if (((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_WRITE_HOLD)
  661. kdp_clear_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_WRITE_HOLD);
  662. #else
  663. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  664. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  665. #endif
  666. }
  667. unlock_mount_hash();
  668. return err;
  669. }
  670. static void free_vfsmnt(struct mount *mnt)
  671. {
  672. struct user_namespace *mnt_userns;
  673. #ifdef CONFIG_KDP_NS
  674. mnt_userns = mnt_user_ns(((struct kdp_mount *)mnt)->mnt);
  675. #else
  676. mnt_userns = mnt_user_ns(&mnt->mnt);
  677. #endif
  678. if (!initial_idmapping(mnt_userns))
  679. put_user_ns(mnt_userns);
  680. kfree_const(mnt->mnt_devname);
  681. #ifdef CONFIG_SMP
  682. free_percpu(mnt->mnt_pcp);
  683. #endif
  684. #ifdef CONFIG_KDP_NS
  685. if(((struct kdp_mount *)mnt)->mnt && is_kdp_vfsmnt_cache((unsigned long)((struct kdp_mount *)mnt)->mnt))
  686. kdp_free_vfsmount(((struct kdp_mount *)mnt)->mnt);
  687. #endif
  688. kmem_cache_free(mnt_cache, mnt);
  689. }
  690. static void delayed_free_vfsmnt(struct rcu_head *head)
  691. {
  692. free_vfsmnt(container_of(head, struct mount, mnt_rcu));
  693. }
  694. /* call under rcu_read_lock */
  695. int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  696. {
  697. struct mount *mnt;
  698. if (read_seqretry(&mount_lock, seq))
  699. return 1;
  700. if (bastard == NULL)
  701. return 0;
  702. mnt = real_mount(bastard);
  703. mnt_add_count(mnt, 1);
  704. smp_mb(); // see mntput_no_expire()
  705. if (likely(!read_seqretry(&mount_lock, seq)))
  706. return 0;
  707. if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
  708. mnt_add_count(mnt, -1);
  709. return 1;
  710. }
  711. lock_mount_hash();
  712. if (unlikely(bastard->mnt_flags & MNT_DOOMED)) {
  713. mnt_add_count(mnt, -1);
  714. unlock_mount_hash();
  715. return 1;
  716. }
  717. unlock_mount_hash();
  718. /* caller will mntput() */
  719. return -1;
  720. }
  721. /* call under rcu_read_lock */
  722. static bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  723. {
  724. int res = __legitimize_mnt(bastard, seq);
  725. if (likely(!res))
  726. return true;
  727. if (unlikely(res < 0)) {
  728. rcu_read_unlock();
  729. mntput(bastard);
  730. rcu_read_lock();
  731. }
  732. return false;
  733. }
  734. /*
  735. * find the first mount at @dentry on vfsmount @mnt.
  736. * call under rcu_read_lock()
  737. */
  738. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  739. {
  740. struct hlist_head *head = m_hash(mnt, dentry);
  741. struct mount *p;
  742. hlist_for_each_entry_rcu(p, head, mnt_hash)
  743. #ifdef CONFIG_KDP_NS
  744. if (((struct kdp_mount *)(p->mnt_parent))->mnt == mnt && p->mnt_mountpoint == dentry)
  745. #else
  746. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
  747. #endif
  748. return p;
  749. return NULL;
  750. }
  751. /*
  752. * lookup_mnt - Return the first child mount mounted at path
  753. *
  754. * "First" means first mounted chronologically. If you create the
  755. * following mounts:
  756. *
  757. * mount /dev/sda1 /mnt
  758. * mount /dev/sda2 /mnt
  759. * mount /dev/sda3 /mnt
  760. *
  761. * Then lookup_mnt() on the base /mnt dentry in the root mount will
  762. * return successively the root dentry and vfsmount of /dev/sda1, then
  763. * /dev/sda2, then /dev/sda3, then NULL.
  764. *
  765. * lookup_mnt takes a reference to the found vfsmount.
  766. */
  767. struct vfsmount *lookup_mnt(const struct path *path)
  768. {
  769. struct mount *child_mnt;
  770. struct vfsmount *m;
  771. unsigned seq;
  772. rcu_read_lock();
  773. do {
  774. seq = read_seqbegin(&mount_lock);
  775. child_mnt = __lookup_mnt(path->mnt, path->dentry);
  776. #ifdef CONFIG_KDP_NS
  777. m = child_mnt ? ((struct kdp_mount *)child_mnt)->mnt : NULL;
  778. #else
  779. m = child_mnt ? &child_mnt->mnt : NULL;
  780. #endif
  781. } while (!legitimize_mnt(m, seq));
  782. rcu_read_unlock();
  783. return m;
  784. }
  785. static inline void lock_ns_list(struct mnt_namespace *ns)
  786. {
  787. spin_lock(&ns->ns_lock);
  788. }
  789. static inline void unlock_ns_list(struct mnt_namespace *ns)
  790. {
  791. spin_unlock(&ns->ns_lock);
  792. }
  793. static inline bool mnt_is_cursor(struct mount *mnt)
  794. {
  795. return mnt->mnt.mnt_flags & MNT_CURSOR;
  796. }
  797. /*
  798. * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
  799. * current mount namespace.
  800. *
  801. * The common case is dentries are not mountpoints at all and that
  802. * test is handled inline. For the slow case when we are actually
  803. * dealing with a mountpoint of some kind, walk through all of the
  804. * mounts in the current mount namespace and test to see if the dentry
  805. * is a mountpoint.
  806. *
  807. * The mount_hashtable is not usable in the context because we
  808. * need to identify all mounts that may be in the current mount
  809. * namespace not just a mount that happens to have some specified
  810. * parent mount.
  811. */
  812. bool __is_local_mountpoint(struct dentry *dentry)
  813. {
  814. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  815. struct mount *mnt;
  816. bool is_covered = false;
  817. down_read(&namespace_sem);
  818. lock_ns_list(ns);
  819. list_for_each_entry(mnt, &ns->list, mnt_list) {
  820. if (mnt_is_cursor(mnt))
  821. continue;
  822. is_covered = (mnt->mnt_mountpoint == dentry);
  823. if (is_covered)
  824. break;
  825. }
  826. unlock_ns_list(ns);
  827. up_read(&namespace_sem);
  828. return is_covered;
  829. }
  830. static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
  831. {
  832. struct hlist_head *chain = mp_hash(dentry);
  833. struct mountpoint *mp;
  834. hlist_for_each_entry(mp, chain, m_hash) {
  835. if (mp->m_dentry == dentry) {
  836. mp->m_count++;
  837. return mp;
  838. }
  839. }
  840. return NULL;
  841. }
  842. static struct mountpoint *get_mountpoint(struct dentry *dentry)
  843. {
  844. struct mountpoint *mp, *new = NULL;
  845. int ret;
  846. if (d_mountpoint(dentry)) {
  847. /* might be worth a WARN_ON() */
  848. if (d_unlinked(dentry))
  849. return ERR_PTR(-ENOENT);
  850. mountpoint:
  851. read_seqlock_excl(&mount_lock);
  852. mp = lookup_mountpoint(dentry);
  853. read_sequnlock_excl(&mount_lock);
  854. if (mp)
  855. goto done;
  856. }
  857. if (!new)
  858. new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
  859. if (!new)
  860. return ERR_PTR(-ENOMEM);
  861. /* Exactly one processes may set d_mounted */
  862. ret = d_set_mounted(dentry);
  863. /* Someone else set d_mounted? */
  864. if (ret == -EBUSY)
  865. goto mountpoint;
  866. /* The dentry is not available as a mountpoint? */
  867. mp = ERR_PTR(ret);
  868. if (ret)
  869. goto done;
  870. /* Add the new mountpoint to the hash table */
  871. read_seqlock_excl(&mount_lock);
  872. new->m_dentry = dget(dentry);
  873. new->m_count = 1;
  874. hlist_add_head(&new->m_hash, mp_hash(dentry));
  875. INIT_HLIST_HEAD(&new->m_list);
  876. read_sequnlock_excl(&mount_lock);
  877. mp = new;
  878. new = NULL;
  879. done:
  880. kfree(new);
  881. return mp;
  882. }
  883. /*
  884. * vfsmount lock must be held. Additionally, the caller is responsible
  885. * for serializing calls for given disposal list.
  886. */
  887. static void __put_mountpoint(struct mountpoint *mp, struct list_head *list)
  888. {
  889. if (!--mp->m_count) {
  890. struct dentry *dentry = mp->m_dentry;
  891. BUG_ON(!hlist_empty(&mp->m_list));
  892. spin_lock(&dentry->d_lock);
  893. dentry->d_flags &= ~DCACHE_MOUNTED;
  894. spin_unlock(&dentry->d_lock);
  895. dput_to_list(dentry, list);
  896. hlist_del(&mp->m_hash);
  897. kfree(mp);
  898. }
  899. }
  900. /* called with namespace_lock and vfsmount lock */
  901. static void put_mountpoint(struct mountpoint *mp)
  902. {
  903. __put_mountpoint(mp, &ex_mountpoints);
  904. }
  905. static inline int check_mnt(struct mount *mnt)
  906. {
  907. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  908. }
  909. /*
  910. * vfsmount lock must be held for write
  911. */
  912. static void touch_mnt_namespace(struct mnt_namespace *ns)
  913. {
  914. if (ns) {
  915. ns->event = ++event;
  916. wake_up_interruptible(&ns->poll);
  917. }
  918. }
  919. /*
  920. * vfsmount lock must be held for write
  921. */
  922. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  923. {
  924. if (ns && ns->event != event) {
  925. ns->event = event;
  926. wake_up_interruptible(&ns->poll);
  927. }
  928. }
  929. /*
  930. * vfsmount lock must be held for write
  931. */
  932. static struct mountpoint *unhash_mnt(struct mount *mnt)
  933. {
  934. struct mountpoint *mp;
  935. mnt->mnt_parent = mnt;
  936. #ifdef CONFIG_KDP_NS
  937. mnt->mnt_mountpoint = ((struct kdp_mount *)mnt)->mnt->mnt_root;
  938. #else
  939. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  940. #endif
  941. list_del_init(&mnt->mnt_child);
  942. hlist_del_init_rcu(&mnt->mnt_hash);
  943. hlist_del_init(&mnt->mnt_mp_list);
  944. mp = mnt->mnt_mp;
  945. mnt->mnt_mp = NULL;
  946. return mp;
  947. }
  948. /*
  949. * vfsmount lock must be held for write
  950. */
  951. static void umount_mnt(struct mount *mnt)
  952. {
  953. put_mountpoint(unhash_mnt(mnt));
  954. }
  955. /*
  956. * vfsmount lock must be held for write
  957. */
  958. void mnt_set_mountpoint(struct mount *mnt,
  959. struct mountpoint *mp,
  960. struct mount *child_mnt)
  961. {
  962. mp->m_count++;
  963. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  964. child_mnt->mnt_mountpoint = mp->m_dentry;
  965. child_mnt->mnt_parent = mnt;
  966. child_mnt->mnt_mp = mp;
  967. hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
  968. }
  969. static void __attach_mnt(struct mount *mnt, struct mount *parent)
  970. {
  971. #ifdef CONFIG_KDP_NS
  972. hlist_add_head_rcu(&mnt->mnt_hash,
  973. m_hash(((struct kdp_mount *)parent)->mnt, mnt->mnt_mountpoint));
  974. #else
  975. hlist_add_head_rcu(&mnt->mnt_hash,
  976. m_hash(&parent->mnt, mnt->mnt_mountpoint));
  977. #endif
  978. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  979. }
  980. /*
  981. * vfsmount lock must be held for write
  982. */
  983. static void attach_mnt(struct mount *mnt,
  984. struct mount *parent,
  985. struct mountpoint *mp)
  986. {
  987. mnt_set_mountpoint(parent, mp, mnt);
  988. __attach_mnt(mnt, parent);
  989. }
  990. void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
  991. {
  992. struct mountpoint *old_mp = mnt->mnt_mp;
  993. struct mount *old_parent = mnt->mnt_parent;
  994. list_del_init(&mnt->mnt_child);
  995. hlist_del_init(&mnt->mnt_mp_list);
  996. hlist_del_init_rcu(&mnt->mnt_hash);
  997. attach_mnt(mnt, parent, mp);
  998. put_mountpoint(old_mp);
  999. mnt_add_count(old_parent, -1);
  1000. }
  1001. /*
  1002. * vfsmount lock must be held for write
  1003. */
  1004. static void commit_tree(struct mount *mnt)
  1005. {
  1006. struct mount *parent = mnt->mnt_parent;
  1007. struct mount *m;
  1008. LIST_HEAD(head);
  1009. struct mnt_namespace *n = parent->mnt_ns;
  1010. BUG_ON(parent == mnt);
  1011. list_add_tail(&head, &mnt->mnt_list);
  1012. list_for_each_entry(m, &head, mnt_list)
  1013. m->mnt_ns = n;
  1014. list_splice(&head, n->list.prev);
  1015. n->mounts += n->pending_mounts;
  1016. n->pending_mounts = 0;
  1017. __attach_mnt(mnt, parent);
  1018. touch_mnt_namespace(n);
  1019. }
  1020. static struct mount *next_mnt(struct mount *p, struct mount *root)
  1021. {
  1022. struct list_head *next = p->mnt_mounts.next;
  1023. if (next == &p->mnt_mounts) {
  1024. while (1) {
  1025. if (p == root)
  1026. return NULL;
  1027. next = p->mnt_child.next;
  1028. if (next != &p->mnt_parent->mnt_mounts)
  1029. break;
  1030. p = p->mnt_parent;
  1031. }
  1032. }
  1033. return list_entry(next, struct mount, mnt_child);
  1034. }
  1035. static struct mount *skip_mnt_tree(struct mount *p)
  1036. {
  1037. struct list_head *prev = p->mnt_mounts.prev;
  1038. while (prev != &p->mnt_mounts) {
  1039. p = list_entry(prev, struct mount, mnt_child);
  1040. prev = p->mnt_mounts.prev;
  1041. }
  1042. return p;
  1043. }
  1044. /**
  1045. * vfs_create_mount - Create a mount for a configured superblock
  1046. * @fc: The configuration context with the superblock attached
  1047. *
  1048. * Create a mount to an already configured superblock. If necessary, the
  1049. * caller should invoke vfs_get_tree() before calling this.
  1050. *
  1051. * Note that this does not attach the mount to anything.
  1052. */
  1053. struct vfsmount *vfs_create_mount(struct fs_context *fc)
  1054. {
  1055. struct mount *mnt;
  1056. struct user_namespace *fs_userns;
  1057. #ifdef CONFIG_KDP_NS
  1058. struct user_namespace *userns;
  1059. #endif
  1060. if (!fc->root)
  1061. return ERR_PTR(-EINVAL);
  1062. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  1063. // For newly created mounts, the only caller process we care is KSU
  1064. if (unlikely(susfs_is_current_ksu_domain())) {
  1065. mnt = alloc_vfsmnt(fc->source ?: "none", true, 0);
  1066. goto bypass_orig_flow;
  1067. }
  1068. mnt = alloc_vfsmnt(fc->source ?: "none", false, 0);
  1069. bypass_orig_flow:
  1070. #else
  1071. mnt = alloc_vfsmnt(fc->source ?: "none");
  1072. #endif
  1073. if (!mnt)
  1074. return ERR_PTR(-ENOMEM);
  1075. if (fc->sb_flags & SB_KERNMOUNT)
  1076. #ifdef CONFIG_KDP_NS
  1077. kdp_set_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_INTERNAL);
  1078. #else
  1079. mnt->mnt.mnt_flags = MNT_INTERNAL;
  1080. #endif
  1081. atomic_inc(&fc->root->d_sb->s_active);
  1082. #ifdef CONFIG_KDP_NS
  1083. kdp_set_mnt_root_sb(((struct kdp_mount *)mnt)->mnt, dget(fc->root), fc->root->d_sb);
  1084. mnt->mnt_mountpoint = ((struct kdp_mount *)mnt)->mnt->mnt_root;
  1085. #else
  1086. mnt->mnt.mnt_sb = fc->root->d_sb;
  1087. mnt->mnt.mnt_root = dget(fc->root);
  1088. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  1089. #endif
  1090. mnt->mnt_parent = mnt;
  1091. #ifdef CONFIG_KDP_NS
  1092. fs_userns = ((struct kdp_mount *)mnt)->mnt->mnt_sb->s_user_ns;
  1093. if (!initial_idmapping(fs_userns)) {
  1094. userns = get_user_ns(fs_userns);
  1095. kdp_set_mnt_userns(((struct kdp_mount *)mnt)->mnt, userns);
  1096. }
  1097. #else
  1098. fs_userns = mnt->mnt.mnt_sb->s_user_ns;
  1099. if (!initial_idmapping(fs_userns))
  1100. mnt->mnt.mnt_userns = get_user_ns(fs_userns);
  1101. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  1102. // If caller process is zygote, then it is a normal mount, so we just reorder the mnt_id
  1103. if (susfs_is_current_zygote_domain()) {
  1104. mnt->mnt.susfs_mnt_id_backup = mnt->mnt_id;
  1105. mnt->mnt_id = current->susfs_last_fake_mnt_id++;
  1106. }
  1107. #endif
  1108. #endif
  1109. lock_mount_hash();
  1110. #ifdef CONFIG_KDP_NS
  1111. list_add_tail(&mnt->mnt_instance, &((struct kdp_mount *)mnt)->mnt->mnt_sb->s_mounts);
  1112. #else
  1113. list_add_tail(&mnt->mnt_instance, &mnt->mnt.mnt_sb->s_mounts);
  1114. #endif
  1115. unlock_mount_hash();
  1116. #ifdef CONFIG_KDP_NS
  1117. return ((struct kdp_mount *)mnt)->mnt;
  1118. #else
  1119. return &mnt->mnt;
  1120. #endif
  1121. }
  1122. EXPORT_SYMBOL(vfs_create_mount);
  1123. struct vfsmount *fc_mount(struct fs_context *fc)
  1124. {
  1125. int err = vfs_get_tree(fc);
  1126. if (!err) {
  1127. up_write(&fc->root->d_sb->s_umount);
  1128. return vfs_create_mount(fc);
  1129. }
  1130. return ERR_PTR(err);
  1131. }
  1132. EXPORT_SYMBOL(fc_mount);
  1133. struct vfsmount *vfs_kern_mount(struct file_system_type *type,
  1134. int flags, const char *name,
  1135. void *data)
  1136. {
  1137. struct fs_context *fc;
  1138. struct vfsmount *mnt;
  1139. int ret = 0;
  1140. if (!type)
  1141. return ERR_PTR(-EINVAL);
  1142. fc = fs_context_for_mount(type, flags);
  1143. if (IS_ERR(fc))
  1144. return ERR_CAST(fc);
  1145. if (name)
  1146. ret = vfs_parse_fs_string(fc, "source",
  1147. name, strlen(name));
  1148. if (!ret)
  1149. ret = parse_monolithic_mount_data(fc, data);
  1150. if (!ret)
  1151. mnt = fc_mount(fc);
  1152. else
  1153. mnt = ERR_PTR(ret);
  1154. put_fs_context(fc);
  1155. return mnt;
  1156. }
  1157. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  1158. struct vfsmount *
  1159. vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
  1160. const char *name, void *data)
  1161. {
  1162. /* Until it is worked out how to pass the user namespace
  1163. * through from the parent mount to the submount don't support
  1164. * unprivileged mounts with submounts.
  1165. */
  1166. if (mountpoint->d_sb->s_user_ns != &init_user_ns)
  1167. return ERR_PTR(-EPERM);
  1168. return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
  1169. }
  1170. EXPORT_SYMBOL_GPL(vfs_submount);
  1171. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  1172. int flag)
  1173. {
  1174. #ifdef CONFIG_KDP_NS
  1175. struct super_block *sb = ((struct kdp_mount *)old)->mnt->mnt_sb;
  1176. int nsflags;
  1177. struct user_namespace *userns;
  1178. #else
  1179. struct super_block *sb = old->mnt.mnt_sb;
  1180. #endif
  1181. struct mount *mnt;
  1182. int err;
  1183. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  1184. bool is_current_ksu_domain = susfs_is_current_ksu_domain();
  1185. bool is_current_zygote_domain = susfs_is_current_zygote_domain();
  1186. /* - It is very important that we need to use CL_COPY_MNT_NS to identify whether
  1187. * the clone is a copy_tree() or single mount like called by __do_loopback()
  1188. * - if caller process is KSU, consider the following situation:
  1189. * 1. it is NOT doing unshare => call alloc_vfsmnt() to assign a new sus mnt_id
  1190. * 2. it is doing unshare => spoof the new mnt_id with the old mnt_id
  1191. * - If caller process is zygote and old mnt_id is sus => call alloc_vfsmnt() to assign a new sus mnt_id
  1192. * - For the rest of caller process that doing unshare => call alloc_vfsmnt() to assign a new sus mnt_id only for old sus mount
  1193. */
  1194. // Firstly, check if it is KSU process
  1195. if (unlikely(is_current_ksu_domain)) {
  1196. // if it is doing single clone
  1197. if (!(flag & CL_COPY_MNT_NS)) {
  1198. mnt = alloc_vfsmnt(old->mnt_devname, true, 0);
  1199. goto bypass_orig_flow;
  1200. }
  1201. // if it is doing unshare
  1202. mnt = alloc_vfsmnt(old->mnt_devname, true, old->mnt_id);
  1203. if (mnt) {
  1204. mnt->mnt.susfs_mnt_id_backup = DEFAULT_SUS_MNT_ID_FOR_KSU_PROC_UNSHARE;
  1205. }
  1206. goto bypass_orig_flow;
  1207. }
  1208. // Secondly, check if it is zygote process and no matter it is doing unshare or not
  1209. if (likely(is_current_zygote_domain) && (old->mnt_id >= DEFAULT_SUS_MNT_ID)) {
  1210. /* Important Note:
  1211. * - Here we can't determine whether the unshare is called zygisk or not,
  1212. * so we can only patch out the unshare code in zygisk source code for now
  1213. * - But at least we can deal with old sus mounts using alloc_vfsmnt()
  1214. */
  1215. mnt = alloc_vfsmnt(old->mnt_devname, true, 0);
  1216. goto bypass_orig_flow;
  1217. }
  1218. // Lastly, for other process that is doing unshare operation, but only deal with old sus mount
  1219. if ((flag & CL_COPY_MNT_NS) && (old->mnt_id >= DEFAULT_SUS_MNT_ID)) {
  1220. mnt = alloc_vfsmnt(old->mnt_devname, true, 0);
  1221. goto bypass_orig_flow;
  1222. }
  1223. mnt = alloc_vfsmnt(old->mnt_devname, false, 0);
  1224. bypass_orig_flow:
  1225. #else
  1226. mnt = alloc_vfsmnt(old->mnt_devname);
  1227. #endif
  1228. if (!mnt)
  1229. return ERR_PTR(-ENOMEM);
  1230. if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
  1231. mnt->mnt_group_id = 0; /* not a peer of original */
  1232. else
  1233. mnt->mnt_group_id = old->mnt_group_id;
  1234. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  1235. err = mnt_alloc_group_id(mnt);
  1236. if (err)
  1237. goto out_free;
  1238. }
  1239. #ifdef CONFIG_KDP_NS
  1240. nsflags = ((struct kdp_mount *)old)->mnt->mnt_flags;
  1241. nsflags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
  1242. kdp_assign_mnt_flags(((struct kdp_mount *)mnt)->mnt, nsflags);
  1243. atomic_inc(&sb->s_active);
  1244. userns = mnt_user_ns(((struct kdp_mount *)old)->mnt);
  1245. kdp_set_mnt_userns(((struct kdp_mount *)mnt)->mnt, userns);
  1246. if (!initial_idmapping(((struct kdp_mount *)mnt)->mnt->mnt_userns)) {
  1247. userns = get_user_ns(((struct kdp_mount *)mnt)->mnt->mnt_userns);
  1248. kdp_set_mnt_userns(((struct kdp_mount *)mnt)->mnt, userns);
  1249. }
  1250. kdp_set_mnt_root_sb(((struct kdp_mount *)mnt)->mnt, dget(root), sb);
  1251. mnt->mnt_mountpoint = ((struct kdp_mount *)mnt)->mnt->mnt_root;
  1252. #else
  1253. mnt->mnt.mnt_flags = old->mnt.mnt_flags;
  1254. mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
  1255. atomic_inc(&sb->s_active);
  1256. mnt->mnt.mnt_userns = mnt_user_ns(&old->mnt);
  1257. if (!initial_idmapping(mnt->mnt.mnt_userns))
  1258. mnt->mnt.mnt_userns = get_user_ns(mnt->mnt.mnt_userns);
  1259. mnt->mnt.mnt_sb = sb;
  1260. mnt->mnt.mnt_root = dget(root);
  1261. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  1262. #endif
  1263. mnt->mnt_parent = mnt;
  1264. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  1265. // If caller process is zygote and not doing unshare, so we just reorder the mnt_id
  1266. if (likely(is_current_zygote_domain) && !(flag & CL_ZYGOTE_COPY_MNT_NS)) {
  1267. mnt->mnt.susfs_mnt_id_backup = mnt->mnt_id;
  1268. mnt->mnt_id = current->susfs_last_fake_mnt_id++;
  1269. }
  1270. #endif
  1271. lock_mount_hash();
  1272. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  1273. unlock_mount_hash();
  1274. if ((flag & CL_SLAVE) ||
  1275. ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
  1276. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  1277. mnt->mnt_master = old;
  1278. CLEAR_MNT_SHARED(mnt);
  1279. } else if (!(flag & CL_PRIVATE)) {
  1280. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  1281. list_add(&mnt->mnt_share, &old->mnt_share);
  1282. if (IS_MNT_SLAVE(old))
  1283. list_add(&mnt->mnt_slave, &old->mnt_slave);
  1284. mnt->mnt_master = old->mnt_master;
  1285. } else {
  1286. CLEAR_MNT_SHARED(mnt);
  1287. }
  1288. if (flag & CL_MAKE_SHARED)
  1289. set_mnt_shared(mnt);
  1290. /* stick the duplicate mount on the same expiry list
  1291. * as the original if that was on one */
  1292. if (flag & CL_EXPIRE) {
  1293. if (!list_empty(&old->mnt_expire))
  1294. list_add(&mnt->mnt_expire, &old->mnt_expire);
  1295. }
  1296. return mnt;
  1297. out_free:
  1298. mnt_free_id(mnt);
  1299. free_vfsmnt(mnt);
  1300. return ERR_PTR(err);
  1301. }
  1302. static void cleanup_mnt(struct mount *mnt)
  1303. {
  1304. struct hlist_node *p;
  1305. struct mount *m;
  1306. /*
  1307. * The warning here probably indicates that somebody messed
  1308. * up a mnt_want/drop_write() pair. If this happens, the
  1309. * filesystem was probably unable to make r/w->r/o transitions.
  1310. * The locking used to deal with mnt_count decrement provides barriers,
  1311. * so mnt_get_writers() below is safe.
  1312. */
  1313. WARN_ON(mnt_get_writers(mnt));
  1314. if (unlikely(mnt->mnt_pins.first))
  1315. mnt_pin_kill(mnt);
  1316. #ifdef CONFIG_KDP_NS
  1317. hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
  1318. hlist_del(&m->mnt_umount);
  1319. mntput(((struct kdp_mount *)m)->mnt);
  1320. }
  1321. fsnotify_vfsmount_delete(((struct kdp_mount *)mnt)->mnt);
  1322. dput(((struct kdp_mount *)mnt)->mnt->mnt_root);
  1323. deactivate_super(((struct kdp_mount *)mnt)->mnt->mnt_sb);
  1324. #else
  1325. hlist_for_each_entry_safe(m, p, &mnt->mnt_stuck_children, mnt_umount) {
  1326. hlist_del(&m->mnt_umount);
  1327. mntput(&m->mnt);
  1328. }
  1329. fsnotify_vfsmount_delete(&mnt->mnt);
  1330. dput(mnt->mnt.mnt_root);
  1331. deactivate_super(mnt->mnt.mnt_sb);
  1332. #endif
  1333. mnt_free_id(mnt);
  1334. call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
  1335. }
  1336. static void __cleanup_mnt(struct rcu_head *head)
  1337. {
  1338. cleanup_mnt(container_of(head, struct mount, mnt_rcu));
  1339. }
  1340. static LLIST_HEAD(delayed_mntput_list);
  1341. static void delayed_mntput(struct work_struct *unused)
  1342. {
  1343. struct llist_node *node = llist_del_all(&delayed_mntput_list);
  1344. struct mount *m, *t;
  1345. llist_for_each_entry_safe(m, t, node, mnt_llist)
  1346. cleanup_mnt(m);
  1347. }
  1348. static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
  1349. static void mntput_no_expire(struct mount *mnt)
  1350. {
  1351. LIST_HEAD(list);
  1352. int count;
  1353. rcu_read_lock();
  1354. if (likely(READ_ONCE(mnt->mnt_ns))) {
  1355. /*
  1356. * Since we don't do lock_mount_hash() here,
  1357. * ->mnt_ns can change under us. However, if it's
  1358. * non-NULL, then there's a reference that won't
  1359. * be dropped until after an RCU delay done after
  1360. * turning ->mnt_ns NULL. So if we observe it
  1361. * non-NULL under rcu_read_lock(), the reference
  1362. * we are dropping is not the final one.
  1363. */
  1364. mnt_add_count(mnt, -1);
  1365. rcu_read_unlock();
  1366. return;
  1367. }
  1368. lock_mount_hash();
  1369. /*
  1370. * make sure that if __legitimize_mnt() has not seen us grab
  1371. * mount_lock, we'll see their refcount increment here.
  1372. */
  1373. smp_mb();
  1374. mnt_add_count(mnt, -1);
  1375. count = mnt_get_count(mnt);
  1376. if (count != 0) {
  1377. WARN_ON(count < 0);
  1378. rcu_read_unlock();
  1379. unlock_mount_hash();
  1380. return;
  1381. }
  1382. #ifdef CONFIG_KDP_NS
  1383. if (unlikely(((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_DOOMED)) {
  1384. #else
  1385. if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
  1386. #endif
  1387. rcu_read_unlock();
  1388. unlock_mount_hash();
  1389. return;
  1390. }
  1391. #ifdef CONFIG_KDP_NS
  1392. kdp_set_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_DOOMED);
  1393. #else
  1394. mnt->mnt.mnt_flags |= MNT_DOOMED;
  1395. #endif
  1396. rcu_read_unlock();
  1397. list_del(&mnt->mnt_instance);
  1398. if (unlikely(!list_empty(&mnt->mnt_mounts))) {
  1399. struct mount *p, *tmp;
  1400. list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
  1401. __put_mountpoint(unhash_mnt(p), &list);
  1402. hlist_add_head(&p->mnt_umount, &mnt->mnt_stuck_children);
  1403. }
  1404. }
  1405. unlock_mount_hash();
  1406. shrink_dentry_list(&list);
  1407. #ifdef CONFIG_KDP_NS
  1408. if (likely(!(((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_INTERNAL))) {
  1409. #else
  1410. if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
  1411. #endif
  1412. struct task_struct *task = current;
  1413. if (likely(!(task->flags & PF_KTHREAD))) {
  1414. init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
  1415. if (!task_work_add(task, &mnt->mnt_rcu, TWA_RESUME))
  1416. return;
  1417. }
  1418. if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
  1419. schedule_delayed_work(&delayed_mntput_work, 1);
  1420. return;
  1421. }
  1422. cleanup_mnt(mnt);
  1423. }
  1424. void mntput(struct vfsmount *mnt)
  1425. {
  1426. if (mnt) {
  1427. struct mount *m = real_mount(mnt);
  1428. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  1429. if (unlikely(m->mnt_expiry_mark))
  1430. m->mnt_expiry_mark = 0;
  1431. mntput_no_expire(m);
  1432. }
  1433. }
  1434. EXPORT_SYMBOL(mntput);
  1435. struct vfsmount *mntget(struct vfsmount *mnt)
  1436. {
  1437. if (mnt)
  1438. mnt_add_count(real_mount(mnt), 1);
  1439. return mnt;
  1440. }
  1441. EXPORT_SYMBOL(mntget);
  1442. /**
  1443. * path_is_mountpoint() - Check if path is a mount in the current namespace.
  1444. * @path: path to check
  1445. *
  1446. * d_mountpoint() can only be used reliably to establish if a dentry is
  1447. * not mounted in any namespace and that common case is handled inline.
  1448. * d_mountpoint() isn't aware of the possibility there may be multiple
  1449. * mounts using a given dentry in a different namespace. This function
  1450. * checks if the passed in path is a mountpoint rather than the dentry
  1451. * alone.
  1452. */
  1453. bool path_is_mountpoint(const struct path *path)
  1454. {
  1455. unsigned seq;
  1456. bool res;
  1457. if (!d_mountpoint(path->dentry))
  1458. return false;
  1459. rcu_read_lock();
  1460. do {
  1461. seq = read_seqbegin(&mount_lock);
  1462. res = __path_is_mountpoint(path);
  1463. } while (read_seqretry(&mount_lock, seq));
  1464. rcu_read_unlock();
  1465. return res;
  1466. }
  1467. EXPORT_SYMBOL(path_is_mountpoint);
  1468. struct vfsmount *mnt_clone_internal(const struct path *path)
  1469. {
  1470. struct mount *p;
  1471. p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
  1472. if (IS_ERR(p))
  1473. return ERR_CAST(p);
  1474. #ifdef CONFIG_KDP_NS
  1475. kdp_set_mnt_flags(((struct kdp_mount *)p)->mnt, MNT_INTERNAL);
  1476. return ((struct kdp_mount *)p)->mnt;
  1477. #else
  1478. p->mnt.mnt_flags |= MNT_INTERNAL;
  1479. return &p->mnt;
  1480. #endif
  1481. }
  1482. #ifdef CONFIG_PROC_FS
  1483. static struct mount *mnt_list_next(struct mnt_namespace *ns,
  1484. struct list_head *p)
  1485. {
  1486. struct mount *mnt, *ret = NULL;
  1487. lock_ns_list(ns);
  1488. list_for_each_continue(p, &ns->list) {
  1489. mnt = list_entry(p, typeof(*mnt), mnt_list);
  1490. if (!mnt_is_cursor(mnt)) {
  1491. ret = mnt;
  1492. break;
  1493. }
  1494. }
  1495. unlock_ns_list(ns);
  1496. return ret;
  1497. }
  1498. /* iterator; we want it to have access to namespace_sem, thus here... */
  1499. static void *m_start(struct seq_file *m, loff_t *pos)
  1500. {
  1501. struct proc_mounts *p = m->private;
  1502. struct list_head *prev;
  1503. down_read(&namespace_sem);
  1504. if (!*pos) {
  1505. prev = &p->ns->list;
  1506. } else {
  1507. prev = &p->cursor.mnt_list;
  1508. /* Read after we'd reached the end? */
  1509. if (list_empty(prev))
  1510. return NULL;
  1511. }
  1512. return mnt_list_next(p->ns, prev);
  1513. }
  1514. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  1515. {
  1516. struct proc_mounts *p = m->private;
  1517. struct mount *mnt = v;
  1518. ++*pos;
  1519. return mnt_list_next(p->ns, &mnt->mnt_list);
  1520. }
  1521. static void m_stop(struct seq_file *m, void *v)
  1522. {
  1523. struct proc_mounts *p = m->private;
  1524. struct mount *mnt = v;
  1525. lock_ns_list(p->ns);
  1526. if (mnt)
  1527. list_move_tail(&p->cursor.mnt_list, &mnt->mnt_list);
  1528. else
  1529. list_del_init(&p->cursor.mnt_list);
  1530. unlock_ns_list(p->ns);
  1531. up_read(&namespace_sem);
  1532. }
  1533. static int m_show(struct seq_file *m, void *v)
  1534. {
  1535. struct proc_mounts *p = m->private;
  1536. struct mount *r = v;
  1537. #ifdef CONFIG_KDP_NS
  1538. return p->show(m, ((struct kdp_mount *)r)->mnt);
  1539. #else
  1540. return p->show(m, &r->mnt);
  1541. #endif
  1542. }
  1543. const struct seq_operations mounts_op = {
  1544. .start = m_start,
  1545. .next = m_next,
  1546. .stop = m_stop,
  1547. .show = m_show,
  1548. };
  1549. void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor)
  1550. {
  1551. down_read(&namespace_sem);
  1552. lock_ns_list(ns);
  1553. list_del(&cursor->mnt_list);
  1554. unlock_ns_list(ns);
  1555. up_read(&namespace_sem);
  1556. }
  1557. #endif /* CONFIG_PROC_FS */
  1558. /**
  1559. * may_umount_tree - check if a mount tree is busy
  1560. * @m: root of mount tree
  1561. *
  1562. * This is called to check if a tree of mounts has any
  1563. * open files, pwds, chroots or sub mounts that are
  1564. * busy.
  1565. */
  1566. int may_umount_tree(struct vfsmount *m)
  1567. {
  1568. struct mount *mnt = real_mount(m);
  1569. int actual_refs = 0;
  1570. int minimum_refs = 0;
  1571. struct mount *p;
  1572. BUG_ON(!m);
  1573. /* write lock needed for mnt_get_count */
  1574. lock_mount_hash();
  1575. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1576. actual_refs += mnt_get_count(p);
  1577. minimum_refs += 2;
  1578. }
  1579. unlock_mount_hash();
  1580. if (actual_refs > minimum_refs)
  1581. return 0;
  1582. return 1;
  1583. }
  1584. EXPORT_SYMBOL(may_umount_tree);
  1585. /**
  1586. * may_umount - check if a mount point is busy
  1587. * @mnt: root of mount
  1588. *
  1589. * This is called to check if a mount point has any
  1590. * open files, pwds, chroots or sub mounts. If the
  1591. * mount has sub mounts this will return busy
  1592. * regardless of whether the sub mounts are busy.
  1593. *
  1594. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1595. * give false negatives. The main reason why it's here is that we need
  1596. * a non-destructive way to look for easily umountable filesystems.
  1597. */
  1598. int may_umount(struct vfsmount *mnt)
  1599. {
  1600. int ret = 1;
  1601. down_read(&namespace_sem);
  1602. lock_mount_hash();
  1603. if (propagate_mount_busy(real_mount(mnt), 2))
  1604. ret = 0;
  1605. unlock_mount_hash();
  1606. up_read(&namespace_sem);
  1607. return ret;
  1608. }
  1609. EXPORT_SYMBOL(may_umount);
  1610. static void namespace_unlock(void)
  1611. {
  1612. struct hlist_head head;
  1613. struct hlist_node *p;
  1614. struct mount *m;
  1615. LIST_HEAD(list);
  1616. hlist_move_list(&unmounted, &head);
  1617. list_splice_init(&ex_mountpoints, &list);
  1618. up_write(&namespace_sem);
  1619. shrink_dentry_list(&list);
  1620. if (likely(hlist_empty(&head)))
  1621. return;
  1622. synchronize_rcu_expedited();
  1623. hlist_for_each_entry_safe(m, p, &head, mnt_umount) {
  1624. hlist_del(&m->mnt_umount);
  1625. #ifdef CONFIG_KDP_NS
  1626. mntput(((struct kdp_mount *)m)->mnt);
  1627. #else
  1628. mntput(&m->mnt);
  1629. #endif
  1630. }
  1631. }
  1632. static inline void namespace_lock(void)
  1633. {
  1634. down_write(&namespace_sem);
  1635. }
  1636. enum umount_tree_flags {
  1637. UMOUNT_SYNC = 1,
  1638. UMOUNT_PROPAGATE = 2,
  1639. UMOUNT_CONNECTED = 4,
  1640. };
  1641. static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
  1642. {
  1643. /* Leaving mounts connected is only valid for lazy umounts */
  1644. if (how & UMOUNT_SYNC)
  1645. return true;
  1646. /* A mount without a parent has nothing to be connected to */
  1647. if (!mnt_has_parent(mnt))
  1648. return true;
  1649. /* Because the reference counting rules change when mounts are
  1650. * unmounted and connected, umounted mounts may not be
  1651. * connected to mounted mounts.
  1652. */
  1653. #ifdef CONFIG_KDP_NS
  1654. if (!(((struct kdp_mount *)mnt->mnt_parent)->mnt->mnt_flags & MNT_UMOUNT))
  1655. #else
  1656. if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
  1657. #endif
  1658. return true;
  1659. /* Has it been requested that the mount remain connected? */
  1660. if (how & UMOUNT_CONNECTED)
  1661. return false;
  1662. /* Is the mount locked such that it needs to remain connected? */
  1663. if (IS_MNT_LOCKED(mnt))
  1664. return false;
  1665. /* By default disconnect the mount */
  1666. return true;
  1667. }
  1668. /*
  1669. * mount_lock must be held
  1670. * namespace_sem must be held for write
  1671. */
  1672. static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
  1673. {
  1674. LIST_HEAD(tmp_list);
  1675. struct mount *p;
  1676. if (how & UMOUNT_PROPAGATE)
  1677. propagate_mount_unlock(mnt);
  1678. /* Gather the mounts to umount */
  1679. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1680. #ifdef CONFIG_KDP_NS
  1681. kdp_set_mnt_flags(((struct kdp_mount *)p)->mnt, MNT_UMOUNT);
  1682. #else
  1683. p->mnt.mnt_flags |= MNT_UMOUNT;
  1684. #endif
  1685. list_move(&p->mnt_list, &tmp_list);
  1686. }
  1687. /* Hide the mounts from mnt_mounts */
  1688. list_for_each_entry(p, &tmp_list, mnt_list) {
  1689. list_del_init(&p->mnt_child);
  1690. }
  1691. /* Add propogated mounts to the tmp_list */
  1692. if (how & UMOUNT_PROPAGATE)
  1693. propagate_umount(&tmp_list);
  1694. while (!list_empty(&tmp_list)) {
  1695. struct mnt_namespace *ns;
  1696. bool disconnect;
  1697. p = list_first_entry(&tmp_list, struct mount, mnt_list);
  1698. list_del_init(&p->mnt_expire);
  1699. list_del_init(&p->mnt_list);
  1700. ns = p->mnt_ns;
  1701. if (ns) {
  1702. ns->mounts--;
  1703. __touch_mnt_namespace(ns);
  1704. }
  1705. p->mnt_ns = NULL;
  1706. if (how & UMOUNT_SYNC)
  1707. #ifdef CONFIG_KDP_NS
  1708. kdp_set_mnt_flags(((struct kdp_mount *)p)->mnt, MNT_SYNC_UMOUNT);
  1709. #else
  1710. p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
  1711. #endif
  1712. disconnect = disconnect_mount(p, how);
  1713. if (mnt_has_parent(p)) {
  1714. mnt_add_count(p->mnt_parent, -1);
  1715. if (!disconnect) {
  1716. /* Don't forget about p */
  1717. list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
  1718. } else {
  1719. umount_mnt(p);
  1720. }
  1721. }
  1722. change_mnt_propagation(p, MS_PRIVATE);
  1723. if (disconnect)
  1724. hlist_add_head(&p->mnt_umount, &unmounted);
  1725. }
  1726. }
  1727. static void shrink_submounts(struct mount *mnt);
  1728. static int do_umount_root(struct super_block *sb)
  1729. {
  1730. int ret = 0;
  1731. down_write(&sb->s_umount);
  1732. if (!sb_rdonly(sb)) {
  1733. struct fs_context *fc;
  1734. fc = fs_context_for_reconfigure(sb->s_root, SB_RDONLY,
  1735. SB_RDONLY);
  1736. if (IS_ERR(fc)) {
  1737. ret = PTR_ERR(fc);
  1738. } else {
  1739. ret = parse_monolithic_mount_data(fc, NULL);
  1740. if (!ret)
  1741. ret = reconfigure_super(fc);
  1742. put_fs_context(fc);
  1743. }
  1744. }
  1745. up_write(&sb->s_umount);
  1746. return ret;
  1747. }
  1748. static int do_umount(struct mount *mnt, int flags)
  1749. {
  1750. #ifdef CONFIG_KDP_NS
  1751. struct super_block *sb = ((struct kdp_mount *)mnt)->mnt->mnt_sb;
  1752. #else
  1753. struct super_block *sb = mnt->mnt.mnt_sb;
  1754. #endif
  1755. int retval;
  1756. #ifdef CONFIG_KDP_NS
  1757. retval = security_sb_umount(((struct kdp_mount *)mnt)->mnt, flags);
  1758. #else
  1759. retval = security_sb_umount(&mnt->mnt, flags);
  1760. #endif
  1761. if (retval)
  1762. return retval;
  1763. /*
  1764. * Allow userspace to request a mountpoint be expired rather than
  1765. * unmounting unconditionally. Unmount only happens if:
  1766. * (1) the mark is already set (the mark is cleared by mntput())
  1767. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1768. */
  1769. if (flags & MNT_EXPIRE) {
  1770. #ifdef CONFIG_KDP_NS
  1771. if (((struct kdp_mount *)mnt)->mnt == current->fs->root.mnt ||
  1772. #else
  1773. if (&mnt->mnt == current->fs->root.mnt ||
  1774. #endif
  1775. flags & (MNT_FORCE | MNT_DETACH))
  1776. return -EINVAL;
  1777. /*
  1778. * probably don't strictly need the lock here if we examined
  1779. * all race cases, but it's a slowpath.
  1780. */
  1781. lock_mount_hash();
  1782. if (mnt_get_count(mnt) != 2) {
  1783. unlock_mount_hash();
  1784. return -EBUSY;
  1785. }
  1786. unlock_mount_hash();
  1787. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1788. return -EAGAIN;
  1789. }
  1790. /*
  1791. * If we may have to abort operations to get out of this
  1792. * mount, and they will themselves hold resources we must
  1793. * allow the fs to do things. In the Unix tradition of
  1794. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1795. * might fail to complete on the first run through as other tasks
  1796. * must return, and the like. Thats for the mount program to worry
  1797. * about for the moment.
  1798. */
  1799. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1800. sb->s_op->umount_begin(sb);
  1801. }
  1802. /*
  1803. * No sense to grab the lock for this test, but test itself looks
  1804. * somewhat bogus. Suggestions for better replacement?
  1805. * Ho-hum... In principle, we might treat that as umount + switch
  1806. * to rootfs. GC would eventually take care of the old vfsmount.
  1807. * Actually it makes sense, especially if rootfs would contain a
  1808. * /reboot - static binary that would close all descriptors and
  1809. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1810. */
  1811. #ifdef CONFIG_KDP_NS
  1812. if (((struct kdp_mount *)mnt)->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1813. #else
  1814. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1815. #endif
  1816. /*
  1817. * Special case for "unmounting" root ...
  1818. * we just try to remount it readonly.
  1819. */
  1820. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  1821. return -EPERM;
  1822. return do_umount_root(sb);
  1823. }
  1824. namespace_lock();
  1825. lock_mount_hash();
  1826. /* Recheck MNT_LOCKED with the locks held */
  1827. retval = -EINVAL;
  1828. #ifdef CONFIG_KDP_NS
  1829. if (((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_LOCKED)
  1830. #else
  1831. if (mnt->mnt.mnt_flags & MNT_LOCKED)
  1832. #endif
  1833. goto out;
  1834. event++;
  1835. if (flags & MNT_DETACH) {
  1836. if (!list_empty(&mnt->mnt_list))
  1837. umount_tree(mnt, UMOUNT_PROPAGATE);
  1838. retval = 0;
  1839. } else {
  1840. shrink_submounts(mnt);
  1841. retval = -EBUSY;
  1842. if (!propagate_mount_busy(mnt, 2)) {
  1843. if (!list_empty(&mnt->mnt_list))
  1844. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  1845. retval = 0;
  1846. }
  1847. }
  1848. out:
  1849. unlock_mount_hash();
  1850. namespace_unlock();
  1851. return retval;
  1852. }
  1853. /*
  1854. * __detach_mounts - lazily unmount all mounts on the specified dentry
  1855. *
  1856. * During unlink, rmdir, and d_drop it is possible to loose the path
  1857. * to an existing mountpoint, and wind up leaking the mount.
  1858. * detach_mounts allows lazily unmounting those mounts instead of
  1859. * leaking them.
  1860. *
  1861. * The caller may hold dentry->d_inode->i_mutex.
  1862. */
  1863. void __detach_mounts(struct dentry *dentry)
  1864. {
  1865. struct mountpoint *mp;
  1866. struct mount *mnt;
  1867. namespace_lock();
  1868. lock_mount_hash();
  1869. mp = lookup_mountpoint(dentry);
  1870. if (!mp)
  1871. goto out_unlock;
  1872. event++;
  1873. while (!hlist_empty(&mp->m_list)) {
  1874. mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
  1875. #ifdef CONFIG_KDP_NS
  1876. if (((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_UMOUNT) {
  1877. #else
  1878. if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
  1879. #endif
  1880. umount_mnt(mnt);
  1881. hlist_add_head(&mnt->mnt_umount, &unmounted);
  1882. }
  1883. else umount_tree(mnt, UMOUNT_CONNECTED);
  1884. }
  1885. put_mountpoint(mp);
  1886. out_unlock:
  1887. unlock_mount_hash();
  1888. namespace_unlock();
  1889. }
  1890. /*
  1891. * Is the caller allowed to modify his namespace?
  1892. */
  1893. bool may_mount(void)
  1894. {
  1895. return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
  1896. }
  1897. static void warn_mandlock(void)
  1898. {
  1899. pr_warn_once("=======================================================\n"
  1900. "WARNING: The mand mount option has been deprecated and\n"
  1901. " and is ignored by this kernel. Remove the mand\n"
  1902. " option from the mount to silence this warning.\n"
  1903. "=======================================================\n");
  1904. }
  1905. static int can_umount(const struct path *path, int flags)
  1906. {
  1907. struct mount *mnt = real_mount(path->mnt);
  1908. if (!may_mount())
  1909. return -EPERM;
  1910. if (path->dentry != path->mnt->mnt_root)
  1911. return -EINVAL;
  1912. if (!check_mnt(mnt))
  1913. return -EINVAL;
  1914. #ifdef CONFIG_KDP_NS
  1915. if (((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_LOCKED)
  1916. #else
  1917. if (mnt->mnt.mnt_flags & MNT_LOCKED) /* Check optimistically */
  1918. #endif
  1919. return -EINVAL;
  1920. if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
  1921. return -EPERM;
  1922. return 0;
  1923. }
  1924. // caller is responsible for flags being sane
  1925. int path_umount(struct path *path, int flags)
  1926. {
  1927. struct mount *mnt = real_mount(path->mnt);
  1928. int ret;
  1929. ret = can_umount(path, flags);
  1930. if (!ret)
  1931. ret = do_umount(mnt, flags);
  1932. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1933. dput(path->dentry);
  1934. mntput_no_expire(mnt);
  1935. return ret;
  1936. }
  1937. static int ksys_umount(char __user *name, int flags)
  1938. {
  1939. int lookup_flags = LOOKUP_MOUNTPOINT;
  1940. struct path path;
  1941. int ret;
  1942. // basic validity checks done first
  1943. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1944. return -EINVAL;
  1945. if (!(flags & UMOUNT_NOFOLLOW))
  1946. lookup_flags |= LOOKUP_FOLLOW;
  1947. ret = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1948. if (ret)
  1949. return ret;
  1950. return path_umount(&path, flags);
  1951. }
  1952. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1953. {
  1954. return ksys_umount(name, flags);
  1955. }
  1956. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1957. /*
  1958. * The 2.0 compatible umount. No flags.
  1959. */
  1960. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1961. {
  1962. return ksys_umount(name, 0);
  1963. }
  1964. #endif
  1965. static bool is_mnt_ns_file(struct dentry *dentry)
  1966. {
  1967. /* Is this a proxy for a mount namespace? */
  1968. return dentry->d_op == &ns_dentry_operations &&
  1969. dentry->d_fsdata == &mntns_operations;
  1970. }
  1971. static struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
  1972. {
  1973. return container_of(ns, struct mnt_namespace, ns);
  1974. }
  1975. struct ns_common *from_mnt_ns(struct mnt_namespace *mnt)
  1976. {
  1977. return &mnt->ns;
  1978. }
  1979. static bool mnt_ns_loop(struct dentry *dentry)
  1980. {
  1981. /* Could bind mounting the mount namespace inode cause a
  1982. * mount namespace loop?
  1983. */
  1984. struct mnt_namespace *mnt_ns;
  1985. if (!is_mnt_ns_file(dentry))
  1986. return false;
  1987. mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
  1988. return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  1989. }
  1990. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1991. int flag)
  1992. {
  1993. struct mount *res, *p, *q, *r, *parent;
  1994. if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
  1995. return ERR_PTR(-EINVAL);
  1996. if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
  1997. return ERR_PTR(-EINVAL);
  1998. res = q = clone_mnt(mnt, dentry, flag);
  1999. if (IS_ERR(q))
  2000. return q;
  2001. q->mnt_mountpoint = mnt->mnt_mountpoint;
  2002. p = mnt;
  2003. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  2004. struct mount *s;
  2005. if (!is_subdir(r->mnt_mountpoint, dentry))
  2006. continue;
  2007. for (s = r; s; s = next_mnt(s, r)) {
  2008. if (!(flag & CL_COPY_UNBINDABLE) &&
  2009. IS_MNT_UNBINDABLE(s)) {
  2010. #ifdef CONFIG_KDP_NS
  2011. if (((struct kdp_mount *)s)->mnt->mnt_flags & MNT_LOCKED) {
  2012. #else
  2013. if (s->mnt.mnt_flags & MNT_LOCKED) {
  2014. #endif
  2015. /* Both unbindable and locked. */
  2016. q = ERR_PTR(-EPERM);
  2017. goto out;
  2018. } else {
  2019. s = skip_mnt_tree(s);
  2020. continue;
  2021. }
  2022. }
  2023. if (!(flag & CL_COPY_MNT_NS_FILE) &&
  2024. #ifdef CONFIG_KDP_NS
  2025. is_mnt_ns_file(((struct kdp_mount *)s)->mnt->mnt_root)) {
  2026. #else
  2027. is_mnt_ns_file(s->mnt.mnt_root)) {
  2028. #endif
  2029. s = skip_mnt_tree(s);
  2030. continue;
  2031. }
  2032. while (p != s->mnt_parent) {
  2033. p = p->mnt_parent;
  2034. q = q->mnt_parent;
  2035. }
  2036. p = s;
  2037. parent = q;
  2038. #ifdef CONFIG_KDP_NS
  2039. q = clone_mnt(p, ((struct kdp_mount *)p)->mnt->mnt_root, flag);
  2040. #else
  2041. q = clone_mnt(p, p->mnt.mnt_root, flag);
  2042. #endif
  2043. if (IS_ERR(q))
  2044. goto out;
  2045. lock_mount_hash();
  2046. list_add_tail(&q->mnt_list, &res->mnt_list);
  2047. attach_mnt(q, parent, p->mnt_mp);
  2048. unlock_mount_hash();
  2049. }
  2050. }
  2051. return res;
  2052. out:
  2053. if (res) {
  2054. lock_mount_hash();
  2055. umount_tree(res, UMOUNT_SYNC);
  2056. unlock_mount_hash();
  2057. }
  2058. return q;
  2059. }
  2060. /* Caller should check returned pointer for errors */
  2061. struct vfsmount *collect_mounts(const struct path *path)
  2062. {
  2063. struct mount *tree;
  2064. namespace_lock();
  2065. if (!check_mnt(real_mount(path->mnt)))
  2066. tree = ERR_PTR(-EINVAL);
  2067. else
  2068. tree = copy_tree(real_mount(path->mnt), path->dentry,
  2069. CL_COPY_ALL | CL_PRIVATE);
  2070. namespace_unlock();
  2071. if (IS_ERR(tree))
  2072. return ERR_CAST(tree);
  2073. #ifdef CONFIG_KDP_NS
  2074. return ((struct kdp_mount *)tree)->mnt;
  2075. #else
  2076. return &tree->mnt;
  2077. #endif
  2078. }
  2079. static void free_mnt_ns(struct mnt_namespace *);
  2080. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
  2081. void dissolve_on_fput(struct vfsmount *mnt)
  2082. {
  2083. struct mnt_namespace *ns;
  2084. namespace_lock();
  2085. lock_mount_hash();
  2086. ns = real_mount(mnt)->mnt_ns;
  2087. if (ns) {
  2088. if (is_anon_ns(ns))
  2089. umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
  2090. else
  2091. ns = NULL;
  2092. }
  2093. unlock_mount_hash();
  2094. namespace_unlock();
  2095. if (ns)
  2096. free_mnt_ns(ns);
  2097. }
  2098. void drop_collected_mounts(struct vfsmount *mnt)
  2099. {
  2100. namespace_lock();
  2101. lock_mount_hash();
  2102. umount_tree(real_mount(mnt), 0);
  2103. unlock_mount_hash();
  2104. namespace_unlock();
  2105. }
  2106. static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
  2107. {
  2108. struct mount *child;
  2109. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  2110. if (!is_subdir(child->mnt_mountpoint, dentry))
  2111. continue;
  2112. #ifdef CONFIG_KDP_NS
  2113. if (((struct kdp_mount *)child)->mnt->mnt_flags & MNT_LOCKED)
  2114. #else
  2115. if (child->mnt.mnt_flags & MNT_LOCKED)
  2116. #endif
  2117. return true;
  2118. }
  2119. return false;
  2120. }
  2121. /**
  2122. * clone_private_mount - create a private clone of a path
  2123. * @path: path to clone
  2124. *
  2125. * This creates a new vfsmount, which will be the clone of @path. The new mount
  2126. * will not be attached anywhere in the namespace and will be private (i.e.
  2127. * changes to the originating mount won't be propagated into this).
  2128. *
  2129. * Release with mntput().
  2130. */
  2131. struct vfsmount *clone_private_mount(const struct path *path)
  2132. {
  2133. struct mount *old_mnt = real_mount(path->mnt);
  2134. struct mount *new_mnt;
  2135. down_read(&namespace_sem);
  2136. if (IS_MNT_UNBINDABLE(old_mnt))
  2137. goto invalid;
  2138. if (!check_mnt(old_mnt))
  2139. goto invalid;
  2140. if (has_locked_children(old_mnt, path->dentry))
  2141. goto invalid;
  2142. new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
  2143. up_read(&namespace_sem);
  2144. if (IS_ERR(new_mnt))
  2145. return ERR_CAST(new_mnt);
  2146. /* Longterm mount to be removed by kern_unmount*() */
  2147. #ifdef CONFIG_KDP_NS
  2148. ((struct kdp_mount *)new_mnt)->mount.mnt_ns = MNT_NS_INTERNAL;
  2149. return ((struct kdp_mount *)new_mnt)->mnt;
  2150. #else
  2151. new_mnt->mnt_ns = MNT_NS_INTERNAL;
  2152. return &new_mnt->mnt;
  2153. #endif
  2154. invalid:
  2155. up_read(&namespace_sem);
  2156. return ERR_PTR(-EINVAL);
  2157. }
  2158. EXPORT_SYMBOL_GPL(clone_private_mount);
  2159. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  2160. struct vfsmount *root)
  2161. {
  2162. struct mount *mnt;
  2163. int res = f(root, arg);
  2164. if (res)
  2165. return res;
  2166. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  2167. #ifdef CONFIG_KDP_NS
  2168. res = f(((struct kdp_mount *)mnt)->mnt, arg);
  2169. #else
  2170. res = f(&mnt->mnt, arg);
  2171. #endif
  2172. if (res)
  2173. return res;
  2174. }
  2175. return 0;
  2176. }
  2177. static void lock_mnt_tree(struct mount *mnt)
  2178. {
  2179. struct mount *p;
  2180. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2181. #ifdef CONFIG_KDP_NS
  2182. int flags = ((struct kdp_mount *)p)->mnt->mnt_flags;
  2183. #else
  2184. int flags = p->mnt.mnt_flags;
  2185. #endif
  2186. /* Don't allow unprivileged users to change mount flags */
  2187. flags |= MNT_LOCK_ATIME;
  2188. if (flags & MNT_READONLY)
  2189. flags |= MNT_LOCK_READONLY;
  2190. if (flags & MNT_NODEV)
  2191. flags |= MNT_LOCK_NODEV;
  2192. if (flags & MNT_NOSUID)
  2193. flags |= MNT_LOCK_NOSUID;
  2194. if (flags & MNT_NOEXEC)
  2195. flags |= MNT_LOCK_NOEXEC;
  2196. /* Don't allow unprivileged users to reveal what is under a mount */
  2197. if (list_empty(&p->mnt_expire))
  2198. flags |= MNT_LOCKED;
  2199. #ifdef CONFIG_KDP_NS
  2200. kdp_assign_mnt_flags(((struct kdp_mount *)p)->mnt, flags);
  2201. #else
  2202. p->mnt.mnt_flags = flags;
  2203. #endif
  2204. }
  2205. }
  2206. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  2207. {
  2208. struct mount *p;
  2209. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  2210. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  2211. mnt_release_group_id(p);
  2212. }
  2213. }
  2214. static int invent_group_ids(struct mount *mnt, bool recurse)
  2215. {
  2216. struct mount *p;
  2217. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  2218. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  2219. int err = mnt_alloc_group_id(p);
  2220. if (err) {
  2221. cleanup_group_ids(mnt, p);
  2222. return err;
  2223. }
  2224. }
  2225. }
  2226. return 0;
  2227. }
  2228. int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
  2229. {
  2230. unsigned int max = READ_ONCE(sysctl_mount_max);
  2231. unsigned int mounts = 0;
  2232. struct mount *p;
  2233. if (ns->mounts >= max)
  2234. return -ENOSPC;
  2235. max -= ns->mounts;
  2236. if (ns->pending_mounts >= max)
  2237. return -ENOSPC;
  2238. max -= ns->pending_mounts;
  2239. for (p = mnt; p; p = next_mnt(p, mnt))
  2240. mounts++;
  2241. if (mounts > max)
  2242. return -ENOSPC;
  2243. ns->pending_mounts += mounts;
  2244. return 0;
  2245. }
  2246. /*
  2247. * @source_mnt : mount tree to be attached
  2248. * @nd : place the mount tree @source_mnt is attached
  2249. * @parent_nd : if non-null, detach the source_mnt from its parent and
  2250. * store the parent mount and mountpoint dentry.
  2251. * (done when source_mnt is moved)
  2252. *
  2253. * NOTE: in the table below explains the semantics when a source mount
  2254. * of a given type is attached to a destination mount of a given type.
  2255. * ---------------------------------------------------------------------------
  2256. * | BIND MOUNT OPERATION |
  2257. * |**************************************************************************
  2258. * | source-->| shared | private | slave | unbindable |
  2259. * | dest | | | | |
  2260. * | | | | | | |
  2261. * | v | | | | |
  2262. * |**************************************************************************
  2263. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  2264. * | | | | | |
  2265. * |non-shared| shared (+) | private | slave (*) | invalid |
  2266. * ***************************************************************************
  2267. * A bind operation clones the source mount and mounts the clone on the
  2268. * destination mount.
  2269. *
  2270. * (++) the cloned mount is propagated to all the mounts in the propagation
  2271. * tree of the destination mount and the cloned mount is added to
  2272. * the peer group of the source mount.
  2273. * (+) the cloned mount is created under the destination mount and is marked
  2274. * as shared. The cloned mount is added to the peer group of the source
  2275. * mount.
  2276. * (+++) the mount is propagated to all the mounts in the propagation tree
  2277. * of the destination mount and the cloned mount is made slave
  2278. * of the same master as that of the source mount. The cloned mount
  2279. * is marked as 'shared and slave'.
  2280. * (*) the cloned mount is made a slave of the same master as that of the
  2281. * source mount.
  2282. *
  2283. * ---------------------------------------------------------------------------
  2284. * | MOVE MOUNT OPERATION |
  2285. * |**************************************************************************
  2286. * | source-->| shared | private | slave | unbindable |
  2287. * | dest | | | | |
  2288. * | | | | | | |
  2289. * | v | | | | |
  2290. * |**************************************************************************
  2291. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  2292. * | | | | | |
  2293. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  2294. * ***************************************************************************
  2295. *
  2296. * (+) the mount is moved to the destination. And is then propagated to
  2297. * all the mounts in the propagation tree of the destination mount.
  2298. * (+*) the mount is moved to the destination.
  2299. * (+++) the mount is moved to the destination and is then propagated to
  2300. * all the mounts belonging to the destination mount's propagation tree.
  2301. * the mount is marked as 'shared and slave'.
  2302. * (*) the mount continues to be a slave at the new location.
  2303. *
  2304. * if the source mount is a tree, the operations explained above is
  2305. * applied to each mount in the tree.
  2306. * Must be called without spinlocks held, since this function can sleep
  2307. * in allocations.
  2308. */
  2309. static int attach_recursive_mnt(struct mount *source_mnt,
  2310. struct mount *dest_mnt,
  2311. struct mountpoint *dest_mp,
  2312. bool moving)
  2313. {
  2314. struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
  2315. HLIST_HEAD(tree_list);
  2316. struct mnt_namespace *ns = dest_mnt->mnt_ns;
  2317. struct mountpoint *smp;
  2318. struct mount *child, *p;
  2319. struct hlist_node *n;
  2320. int err;
  2321. /* Preallocate a mountpoint in case the new mounts need
  2322. * to be tucked under other mounts.
  2323. */
  2324. #ifdef CONFIG_KDP_NS
  2325. int nsflags;
  2326. smp = get_mountpoint(((struct kdp_mount *)source_mnt)->mnt->mnt_root);
  2327. #else
  2328. smp = get_mountpoint(source_mnt->mnt.mnt_root);
  2329. #endif
  2330. if (IS_ERR(smp))
  2331. return PTR_ERR(smp);
  2332. /* Is there space to add these mounts to the mount namespace? */
  2333. if (!moving) {
  2334. err = count_mounts(ns, source_mnt);
  2335. if (err)
  2336. goto out;
  2337. }
  2338. if (IS_MNT_SHARED(dest_mnt)) {
  2339. err = invent_group_ids(source_mnt, true);
  2340. if (err)
  2341. goto out;
  2342. err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
  2343. lock_mount_hash();
  2344. if (err)
  2345. goto out_cleanup_ids;
  2346. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  2347. set_mnt_shared(p);
  2348. } else {
  2349. lock_mount_hash();
  2350. }
  2351. if (moving) {
  2352. unhash_mnt(source_mnt);
  2353. attach_mnt(source_mnt, dest_mnt, dest_mp);
  2354. touch_mnt_namespace(source_mnt->mnt_ns);
  2355. } else {
  2356. if (source_mnt->mnt_ns) {
  2357. /* move from anon - the caller will destroy */
  2358. list_del_init(&source_mnt->mnt_ns->list);
  2359. }
  2360. mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
  2361. commit_tree(source_mnt);
  2362. }
  2363. hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
  2364. struct mount *q;
  2365. hlist_del_init(&child->mnt_hash);
  2366. #ifdef CONFIG_KDP_NS
  2367. q = __lookup_mnt(((struct kdp_mount *)child->mnt_parent)->mnt,
  2368. #else
  2369. q = __lookup_mnt(&child->mnt_parent->mnt,
  2370. #endif
  2371. child->mnt_mountpoint);
  2372. if (q)
  2373. mnt_change_mountpoint(child, smp, q);
  2374. /* Notice when we are propagating across user namespaces */
  2375. if (child->mnt_parent->mnt_ns->user_ns != user_ns)
  2376. lock_mnt_tree(child);
  2377. #ifdef CONFIG_KDP_NS
  2378. nsflags = ((struct kdp_mount *)child)->mnt->mnt_flags & ~MNT_LOCKED;
  2379. kdp_assign_mnt_flags(((struct kdp_mount *)child)->mnt, nsflags);
  2380. #else
  2381. child->mnt.mnt_flags &= ~MNT_LOCKED;
  2382. #endif
  2383. commit_tree(child);
  2384. }
  2385. put_mountpoint(smp);
  2386. unlock_mount_hash();
  2387. return 0;
  2388. out_cleanup_ids:
  2389. while (!hlist_empty(&tree_list)) {
  2390. child = hlist_entry(tree_list.first, struct mount, mnt_hash);
  2391. child->mnt_parent->mnt_ns->pending_mounts = 0;
  2392. umount_tree(child, UMOUNT_SYNC);
  2393. }
  2394. unlock_mount_hash();
  2395. cleanup_group_ids(source_mnt, NULL);
  2396. out:
  2397. ns->pending_mounts = 0;
  2398. read_seqlock_excl(&mount_lock);
  2399. put_mountpoint(smp);
  2400. read_sequnlock_excl(&mount_lock);
  2401. return err;
  2402. }
  2403. static struct mountpoint *lock_mount(struct path *path)
  2404. {
  2405. struct vfsmount *mnt;
  2406. struct dentry *dentry = path->dentry;
  2407. retry:
  2408. inode_lock(dentry->d_inode);
  2409. if (unlikely(cant_mount(dentry))) {
  2410. inode_unlock(dentry->d_inode);
  2411. return ERR_PTR(-ENOENT);
  2412. }
  2413. namespace_lock();
  2414. mnt = lookup_mnt(path);
  2415. if (likely(!mnt)) {
  2416. struct mountpoint *mp = get_mountpoint(dentry);
  2417. if (IS_ERR(mp)) {
  2418. namespace_unlock();
  2419. inode_unlock(dentry->d_inode);
  2420. return mp;
  2421. }
  2422. return mp;
  2423. }
  2424. namespace_unlock();
  2425. inode_unlock(path->dentry->d_inode);
  2426. path_put(path);
  2427. path->mnt = mnt;
  2428. dentry = path->dentry = dget(mnt->mnt_root);
  2429. goto retry;
  2430. }
  2431. static void unlock_mount(struct mountpoint *where)
  2432. {
  2433. struct dentry *dentry = where->m_dentry;
  2434. read_seqlock_excl(&mount_lock);
  2435. put_mountpoint(where);
  2436. read_sequnlock_excl(&mount_lock);
  2437. namespace_unlock();
  2438. inode_unlock(dentry->d_inode);
  2439. }
  2440. static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
  2441. {
  2442. #ifdef CONFIG_KDP_NS
  2443. if (((struct kdp_mount *)mnt)->mnt->mnt_sb->s_flags & SB_NOUSER)
  2444. #else
  2445. if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
  2446. #endif
  2447. return -EINVAL;
  2448. if (d_is_dir(mp->m_dentry) !=
  2449. #ifdef CONFIG_KDP_NS
  2450. d_is_dir(((struct kdp_mount *)mnt)->mnt->mnt_root))
  2451. #else
  2452. d_is_dir(mnt->mnt.mnt_root))
  2453. #endif
  2454. return -ENOTDIR;
  2455. return attach_recursive_mnt(mnt, p, mp, false);
  2456. }
  2457. /*
  2458. * Sanity check the flags to change_mnt_propagation.
  2459. */
  2460. static int flags_to_propagation_type(int ms_flags)
  2461. {
  2462. int type = ms_flags & ~(MS_REC | MS_SILENT);
  2463. /* Fail if any non-propagation flags are set */
  2464. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2465. return 0;
  2466. /* Only one propagation flag should be set */
  2467. if (!is_power_of_2(type))
  2468. return 0;
  2469. return type;
  2470. }
  2471. /*
  2472. * recursively change the type of the mountpoint.
  2473. */
  2474. static int do_change_type(struct path *path, int ms_flags)
  2475. {
  2476. struct mount *m;
  2477. struct mount *mnt = real_mount(path->mnt);
  2478. int recurse = ms_flags & MS_REC;
  2479. int type;
  2480. int err = 0;
  2481. if (path->dentry != path->mnt->mnt_root)
  2482. return -EINVAL;
  2483. type = flags_to_propagation_type(ms_flags);
  2484. if (!type)
  2485. return -EINVAL;
  2486. namespace_lock();
  2487. if (type == MS_SHARED) {
  2488. err = invent_group_ids(mnt, recurse);
  2489. if (err)
  2490. goto out_unlock;
  2491. }
  2492. lock_mount_hash();
  2493. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  2494. change_mnt_propagation(m, type);
  2495. unlock_mount_hash();
  2496. out_unlock:
  2497. namespace_unlock();
  2498. return err;
  2499. }
  2500. static struct mount *__do_loopback(struct path *old_path, int recurse)
  2501. {
  2502. struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt);
  2503. #ifdef CONFIG_KDP_NS
  2504. int nsflags;
  2505. #endif
  2506. if (IS_MNT_UNBINDABLE(old))
  2507. return mnt;
  2508. if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations)
  2509. return mnt;
  2510. if (!recurse && has_locked_children(old, old_path->dentry))
  2511. return mnt;
  2512. if (recurse)
  2513. mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE);
  2514. else
  2515. mnt = clone_mnt(old, old_path->dentry, 0);
  2516. #ifdef CONFIG_KDP_NS
  2517. if (!IS_ERR(mnt)) {
  2518. nsflags = ((struct kdp_mount *)mnt)->mnt->mnt_flags & ~MNT_LOCKED;
  2519. kdp_assign_mnt_flags(((struct kdp_mount *)mnt)->mnt, nsflags);
  2520. }
  2521. #else
  2522. if (!IS_ERR(mnt))
  2523. mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  2524. #endif
  2525. return mnt;
  2526. }
  2527. /*
  2528. * do loopback mount.
  2529. */
  2530. static int do_loopback(struct path *path, const char *old_name,
  2531. int recurse)
  2532. {
  2533. struct path old_path;
  2534. struct mount *mnt = NULL, *parent;
  2535. struct mountpoint *mp;
  2536. int err;
  2537. if (!old_name || !*old_name)
  2538. return -EINVAL;
  2539. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  2540. if (err)
  2541. return err;
  2542. err = -EINVAL;
  2543. if (mnt_ns_loop(old_path.dentry))
  2544. goto out;
  2545. mp = lock_mount(path);
  2546. if (IS_ERR(mp)) {
  2547. err = PTR_ERR(mp);
  2548. goto out;
  2549. }
  2550. parent = real_mount(path->mnt);
  2551. if (!check_mnt(parent))
  2552. goto out2;
  2553. mnt = __do_loopback(&old_path, recurse);
  2554. if (IS_ERR(mnt)) {
  2555. err = PTR_ERR(mnt);
  2556. goto out2;
  2557. }
  2558. err = graft_tree(mnt, parent, mp);
  2559. if (err) {
  2560. lock_mount_hash();
  2561. umount_tree(mnt, UMOUNT_SYNC);
  2562. unlock_mount_hash();
  2563. }
  2564. #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT) || defined(CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT)
  2565. // Check if bind mounted path should be hidden and umounted automatically.
  2566. // And we target only process with ksu domain.
  2567. if (susfs_is_current_ksu_domain()) {
  2568. #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT)
  2569. if (susfs_is_auto_add_sus_bind_mount_enabled &&
  2570. susfs_auto_add_sus_bind_mount(old_name, &old_path)) {
  2571. goto orig_flow;
  2572. }
  2573. #endif
  2574. #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT)
  2575. if (susfs_is_auto_add_try_umount_for_bind_mount_enabled) {
  2576. susfs_auto_add_try_umount_for_bind_mount(path);
  2577. }
  2578. #endif
  2579. }
  2580. #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT)
  2581. orig_flow:
  2582. #endif
  2583. #endif // #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT) || defined(CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT)
  2584. out2:
  2585. unlock_mount(mp);
  2586. out:
  2587. path_put(&old_path);
  2588. return err;
  2589. }
  2590. static struct file *open_detached_copy(struct path *path, bool recursive)
  2591. {
  2592. struct user_namespace *user_ns = current->nsproxy->mnt_ns->user_ns;
  2593. struct mnt_namespace *ns = alloc_mnt_ns(user_ns, true);
  2594. struct mount *mnt, *p;
  2595. struct file *file;
  2596. if (IS_ERR(ns))
  2597. return ERR_CAST(ns);
  2598. namespace_lock();
  2599. mnt = __do_loopback(path, recursive);
  2600. if (IS_ERR(mnt)) {
  2601. namespace_unlock();
  2602. free_mnt_ns(ns);
  2603. return ERR_CAST(mnt);
  2604. }
  2605. lock_mount_hash();
  2606. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2607. p->mnt_ns = ns;
  2608. ns->mounts++;
  2609. }
  2610. ns->root = mnt;
  2611. list_add_tail(&ns->list, &mnt->mnt_list);
  2612. #ifdef CONFIG_KDP_NS
  2613. mntget(((struct kdp_mount *)mnt)->mnt);
  2614. unlock_mount_hash();
  2615. namespace_unlock();
  2616. mntput(path->mnt);
  2617. path->mnt = ((struct kdp_mount *)mnt)->mnt;
  2618. #else
  2619. mntget(&mnt->mnt);
  2620. unlock_mount_hash();
  2621. namespace_unlock();
  2622. mntput(path->mnt);
  2623. path->mnt = &mnt->mnt;
  2624. #endif
  2625. file = dentry_open(path, O_PATH, current_cred());
  2626. if (IS_ERR(file))
  2627. dissolve_on_fput(path->mnt);
  2628. else
  2629. file->f_mode |= FMODE_NEED_UNMOUNT;
  2630. return file;
  2631. }
  2632. SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
  2633. {
  2634. struct file *file;
  2635. struct path path;
  2636. int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
  2637. bool detached = flags & OPEN_TREE_CLONE;
  2638. int error;
  2639. int fd;
  2640. BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
  2641. if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
  2642. AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
  2643. OPEN_TREE_CLOEXEC))
  2644. return -EINVAL;
  2645. if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
  2646. return -EINVAL;
  2647. if (flags & AT_NO_AUTOMOUNT)
  2648. lookup_flags &= ~LOOKUP_AUTOMOUNT;
  2649. if (flags & AT_SYMLINK_NOFOLLOW)
  2650. lookup_flags &= ~LOOKUP_FOLLOW;
  2651. if (flags & AT_EMPTY_PATH)
  2652. lookup_flags |= LOOKUP_EMPTY;
  2653. if (detached && !may_mount())
  2654. return -EPERM;
  2655. fd = get_unused_fd_flags(flags & O_CLOEXEC);
  2656. if (fd < 0)
  2657. return fd;
  2658. error = user_path_at(dfd, filename, lookup_flags, &path);
  2659. if (unlikely(error)) {
  2660. file = ERR_PTR(error);
  2661. } else {
  2662. if (detached)
  2663. file = open_detached_copy(&path, flags & AT_RECURSIVE);
  2664. else
  2665. file = dentry_open(&path, O_PATH, current_cred());
  2666. path_put(&path);
  2667. }
  2668. if (IS_ERR(file)) {
  2669. put_unused_fd(fd);
  2670. return PTR_ERR(file);
  2671. }
  2672. fd_install(fd, file);
  2673. return fd;
  2674. }
  2675. /*
  2676. * Don't allow locked mount flags to be cleared.
  2677. *
  2678. * No locks need to be held here while testing the various MNT_LOCK
  2679. * flags because those flags can never be cleared once they are set.
  2680. */
  2681. static bool can_change_locked_flags(struct mount *mnt, unsigned int mnt_flags)
  2682. {
  2683. #ifdef CONFIG_KDP_NS
  2684. unsigned int fl = ((struct kdp_mount *)mnt)->mnt->mnt_flags;
  2685. #else
  2686. unsigned int fl = mnt->mnt.mnt_flags;
  2687. #endif
  2688. if ((fl & MNT_LOCK_READONLY) &&
  2689. !(mnt_flags & MNT_READONLY))
  2690. return false;
  2691. if ((fl & MNT_LOCK_NODEV) &&
  2692. !(mnt_flags & MNT_NODEV))
  2693. return false;
  2694. if ((fl & MNT_LOCK_NOSUID) &&
  2695. !(mnt_flags & MNT_NOSUID))
  2696. return false;
  2697. if ((fl & MNT_LOCK_NOEXEC) &&
  2698. !(mnt_flags & MNT_NOEXEC))
  2699. return false;
  2700. if ((fl & MNT_LOCK_ATIME) &&
  2701. ((fl & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK)))
  2702. return false;
  2703. return true;
  2704. }
  2705. static int change_mount_ro_state(struct mount *mnt, unsigned int mnt_flags)
  2706. {
  2707. bool readonly_request = (mnt_flags & MNT_READONLY);
  2708. #ifdef CONFIG_KDP_NS
  2709. if (readonly_request == __mnt_is_readonly(((struct kdp_mount *)mnt)->mnt))
  2710. #else
  2711. if (readonly_request == __mnt_is_readonly(&mnt->mnt))
  2712. #endif
  2713. return 0;
  2714. if (readonly_request)
  2715. return mnt_make_readonly(mnt);
  2716. #ifdef CONFIG_KDP_NS
  2717. kdp_clear_mnt_flags(((struct kdp_mount *)mnt)->mnt, MNT_READONLY);
  2718. #else
  2719. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  2720. #endif
  2721. return 0;
  2722. }
  2723. static void set_mount_attributes(struct mount *mnt, unsigned int mnt_flags)
  2724. {
  2725. #ifdef CONFIG_KDP_NS
  2726. mnt_flags |= ((struct kdp_mount *)mnt)->mnt->mnt_flags & ~MNT_USER_SETTABLE_MASK;
  2727. kdp_assign_mnt_flags(((struct kdp_mount *)mnt)->mnt, mnt_flags);
  2728. #else
  2729. mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
  2730. mnt->mnt.mnt_flags = mnt_flags;
  2731. #endif
  2732. touch_mnt_namespace(mnt->mnt_ns);
  2733. }
  2734. static void mnt_warn_timestamp_expiry(struct path *mountpoint, struct vfsmount *mnt)
  2735. {
  2736. struct super_block *sb = mnt->mnt_sb;
  2737. if (!__mnt_is_readonly(mnt) &&
  2738. (!(sb->s_iflags & SB_I_TS_EXPIRY_WARNED)) &&
  2739. (ktime_get_real_seconds() + TIME_UPTIME_SEC_MAX > sb->s_time_max)) {
  2740. char *buf = (char *)__get_free_page(GFP_KERNEL);
  2741. char *mntpath = buf ? d_path(mountpoint, buf, PAGE_SIZE) : ERR_PTR(-ENOMEM);
  2742. struct tm tm;
  2743. time64_to_tm(sb->s_time_max, 0, &tm);
  2744. pr_warn("%s filesystem being %s at %s supports timestamps until %04ld (0x%llx)\n",
  2745. sb->s_type->name,
  2746. is_mounted(mnt) ? "remounted" : "mounted",
  2747. mntpath,
  2748. tm.tm_year+1900, (unsigned long long)sb->s_time_max);
  2749. free_page((unsigned long)buf);
  2750. sb->s_iflags |= SB_I_TS_EXPIRY_WARNED;
  2751. }
  2752. }
  2753. /*
  2754. * Handle reconfiguration of the mountpoint only without alteration of the
  2755. * superblock it refers to. This is triggered by specifying MS_REMOUNT|MS_BIND
  2756. * to mount(2).
  2757. */
  2758. static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
  2759. {
  2760. struct super_block *sb = path->mnt->mnt_sb;
  2761. struct mount *mnt = real_mount(path->mnt);
  2762. int ret;
  2763. if (!check_mnt(mnt))
  2764. return -EINVAL;
  2765. #ifdef CONFIG_KDP_NS
  2766. if (path->dentry != ((struct kdp_mount *)mnt)->mnt->mnt_root)
  2767. #else
  2768. if (path->dentry != mnt->mnt.mnt_root)
  2769. #endif
  2770. return -EINVAL;
  2771. if (!can_change_locked_flags(mnt, mnt_flags))
  2772. return -EPERM;
  2773. /*
  2774. * We're only checking whether the superblock is read-only not
  2775. * changing it, so only take down_read(&sb->s_umount).
  2776. */
  2777. down_read(&sb->s_umount);
  2778. lock_mount_hash();
  2779. ret = change_mount_ro_state(mnt, mnt_flags);
  2780. if (ret == 0)
  2781. set_mount_attributes(mnt, mnt_flags);
  2782. unlock_mount_hash();
  2783. up_read(&sb->s_umount);
  2784. #ifdef CONFIG_KDP_NS
  2785. mnt_warn_timestamp_expiry(path, ((struct kdp_mount *)mnt)->mnt);
  2786. #else
  2787. mnt_warn_timestamp_expiry(path, &mnt->mnt);
  2788. #endif
  2789. return ret;
  2790. }
  2791. /*
  2792. * change filesystem flags. dir should be a physical root of filesystem.
  2793. * If you've mounted a non-root directory somewhere and want to do remount
  2794. * on it - tough luck.
  2795. */
  2796. static int do_remount(struct path *path, int ms_flags, int sb_flags,
  2797. int mnt_flags, void *data)
  2798. {
  2799. int err;
  2800. struct super_block *sb = path->mnt->mnt_sb;
  2801. struct mount *mnt = real_mount(path->mnt);
  2802. struct fs_context *fc;
  2803. if (!check_mnt(mnt))
  2804. return -EINVAL;
  2805. if (path->dentry != path->mnt->mnt_root)
  2806. return -EINVAL;
  2807. if (!can_change_locked_flags(mnt, mnt_flags))
  2808. return -EPERM;
  2809. fc = fs_context_for_reconfigure(path->dentry, sb_flags, MS_RMT_MASK);
  2810. if (IS_ERR(fc))
  2811. return PTR_ERR(fc);
  2812. fc->oldapi = true;
  2813. err = parse_monolithic_mount_data(fc, data);
  2814. if (!err) {
  2815. down_write(&sb->s_umount);
  2816. err = -EPERM;
  2817. if (ns_capable(sb->s_user_ns, CAP_SYS_ADMIN)) {
  2818. err = reconfigure_super(fc);
  2819. if (!err) {
  2820. lock_mount_hash();
  2821. set_mount_attributes(mnt, mnt_flags);
  2822. unlock_mount_hash();
  2823. }
  2824. }
  2825. up_write(&sb->s_umount);
  2826. }
  2827. #ifdef CONFIG_KDP_NS
  2828. mnt_warn_timestamp_expiry(path, ((struct kdp_mount *)mnt)->mnt);
  2829. #else
  2830. mnt_warn_timestamp_expiry(path, &mnt->mnt);
  2831. #endif
  2832. put_fs_context(fc);
  2833. return err;
  2834. }
  2835. static inline int tree_contains_unbindable(struct mount *mnt)
  2836. {
  2837. struct mount *p;
  2838. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2839. if (IS_MNT_UNBINDABLE(p))
  2840. return 1;
  2841. }
  2842. return 0;
  2843. }
  2844. /*
  2845. * Check that there aren't references to earlier/same mount namespaces in the
  2846. * specified subtree. Such references can act as pins for mount namespaces
  2847. * that aren't checked by the mount-cycle checking code, thereby allowing
  2848. * cycles to be made.
  2849. */
  2850. static bool check_for_nsfs_mounts(struct mount *subtree)
  2851. {
  2852. struct mount *p;
  2853. bool ret = false;
  2854. lock_mount_hash();
  2855. for (p = subtree; p; p = next_mnt(p, subtree))
  2856. #ifdef CONFIG_KDP_NS
  2857. if (mnt_ns_loop(((struct kdp_mount *)p)->mnt->mnt_root))
  2858. #else
  2859. if (mnt_ns_loop(p->mnt.mnt_root))
  2860. #endif
  2861. goto out;
  2862. ret = true;
  2863. out:
  2864. unlock_mount_hash();
  2865. return ret;
  2866. }
  2867. static int do_set_group(struct path *from_path, struct path *to_path)
  2868. {
  2869. struct mount *from, *to;
  2870. int err;
  2871. from = real_mount(from_path->mnt);
  2872. to = real_mount(to_path->mnt);
  2873. namespace_lock();
  2874. err = -EINVAL;
  2875. /* To and From must be mounted */
  2876. #ifdef CONFIG_KDP_NS
  2877. if (!is_mounted(((struct kdp_mount *)from)->mnt))
  2878. goto out;
  2879. if (!is_mounted(((struct kdp_mount *)to)->mnt))
  2880. goto out;
  2881. #else
  2882. if (!is_mounted(&from->mnt))
  2883. goto out;
  2884. if (!is_mounted(&to->mnt))
  2885. goto out;
  2886. #endif
  2887. err = -EPERM;
  2888. /* We should be allowed to modify mount namespaces of both mounts */
  2889. if (!ns_capable(from->mnt_ns->user_ns, CAP_SYS_ADMIN))
  2890. goto out;
  2891. if (!ns_capable(to->mnt_ns->user_ns, CAP_SYS_ADMIN))
  2892. goto out;
  2893. err = -EINVAL;
  2894. /* To and From paths should be mount roots */
  2895. if (from_path->dentry != from_path->mnt->mnt_root)
  2896. goto out;
  2897. if (to_path->dentry != to_path->mnt->mnt_root)
  2898. goto out;
  2899. #ifdef CONFIG_KDP_NS
  2900. /* Setting sharing groups is only allowed across same superblock */
  2901. if (((struct kdp_mount *)from)->mnt->mnt_sb != ((struct kdp_mount *)to)->mnt->mnt_sb)
  2902. goto out;
  2903. /* From mount root should be wider than To mount root */
  2904. if (!is_subdir(((struct kdp_mount *)to)->mnt->mnt_root, ((struct kdp_mount *)from)->mnt->mnt_root))
  2905. goto out;
  2906. /* From mount should not have locked children in place of To's root */
  2907. if (has_locked_children(from, ((struct kdp_mount *)to)->mnt->mnt_root))
  2908. goto out;
  2909. #else
  2910. /* Setting sharing groups is only allowed across same superblock */
  2911. if (from->mnt.mnt_sb != to->mnt.mnt_sb)
  2912. goto out;
  2913. /* From mount root should be wider than To mount root */
  2914. if (!is_subdir(to->mnt.mnt_root, from->mnt.mnt_root))
  2915. goto out;
  2916. /* From mount should not have locked children in place of To's root */
  2917. if (has_locked_children(from, to->mnt.mnt_root))
  2918. goto out;
  2919. #endif
  2920. /* Setting sharing groups is only allowed on private mounts */
  2921. if (IS_MNT_SHARED(to) || IS_MNT_SLAVE(to))
  2922. goto out;
  2923. /* From should not be private */
  2924. if (!IS_MNT_SHARED(from) && !IS_MNT_SLAVE(from))
  2925. goto out;
  2926. if (IS_MNT_SLAVE(from)) {
  2927. struct mount *m = from->mnt_master;
  2928. list_add(&to->mnt_slave, &m->mnt_slave_list);
  2929. to->mnt_master = m;
  2930. }
  2931. if (IS_MNT_SHARED(from)) {
  2932. to->mnt_group_id = from->mnt_group_id;
  2933. list_add(&to->mnt_share, &from->mnt_share);
  2934. lock_mount_hash();
  2935. set_mnt_shared(to);
  2936. unlock_mount_hash();
  2937. }
  2938. err = 0;
  2939. out:
  2940. namespace_unlock();
  2941. return err;
  2942. }
  2943. static int do_move_mount(struct path *old_path, struct path *new_path)
  2944. {
  2945. struct mnt_namespace *ns;
  2946. struct mount *p;
  2947. struct mount *old;
  2948. struct mount *parent;
  2949. struct mountpoint *mp, *old_mp;
  2950. int err;
  2951. bool attached;
  2952. mp = lock_mount(new_path);
  2953. if (IS_ERR(mp))
  2954. return PTR_ERR(mp);
  2955. old = real_mount(old_path->mnt);
  2956. p = real_mount(new_path->mnt);
  2957. parent = old->mnt_parent;
  2958. attached = mnt_has_parent(old);
  2959. old_mp = old->mnt_mp;
  2960. ns = old->mnt_ns;
  2961. err = -EINVAL;
  2962. /* The mountpoint must be in our namespace. */
  2963. if (!check_mnt(p))
  2964. goto out;
  2965. /* The thing moved must be mounted... */
  2966. #ifdef CONFIG_KDP_NS
  2967. if (!is_mounted(((struct kdp_mount *)old)->mnt))
  2968. #else
  2969. if (!is_mounted(&old->mnt))
  2970. #endif
  2971. goto out;
  2972. /* ... and either ours or the root of anon namespace */
  2973. if (!(attached ? check_mnt(old) : is_anon_ns(ns)))
  2974. goto out;
  2975. #ifdef CONFIG_KDP_NS
  2976. if (((struct kdp_mount *)old)->mnt->mnt_flags & MNT_LOCKED)
  2977. #else
  2978. if (old->mnt.mnt_flags & MNT_LOCKED)
  2979. #endif
  2980. goto out;
  2981. if (old_path->dentry != old_path->mnt->mnt_root)
  2982. goto out;
  2983. if (d_is_dir(new_path->dentry) !=
  2984. d_is_dir(old_path->dentry))
  2985. goto out;
  2986. /*
  2987. * Don't move a mount residing in a shared parent.
  2988. */
  2989. if (attached && IS_MNT_SHARED(parent))
  2990. goto out;
  2991. /*
  2992. * Don't move a mount tree containing unbindable mounts to a destination
  2993. * mount which is shared.
  2994. */
  2995. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  2996. goto out;
  2997. err = -ELOOP;
  2998. if (!check_for_nsfs_mounts(old))
  2999. goto out;
  3000. for (; mnt_has_parent(p); p = p->mnt_parent)
  3001. if (p == old)
  3002. goto out;
  3003. err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp,
  3004. attached);
  3005. if (err)
  3006. goto out;
  3007. /* if the mount is moved, it should no longer be expire
  3008. * automatically */
  3009. list_del_init(&old->mnt_expire);
  3010. if (attached)
  3011. put_mountpoint(old_mp);
  3012. out:
  3013. unlock_mount(mp);
  3014. if (!err) {
  3015. if (attached)
  3016. mntput_no_expire(parent);
  3017. else
  3018. free_mnt_ns(ns);
  3019. }
  3020. return err;
  3021. }
  3022. static int do_move_mount_old(struct path *path, const char *old_name)
  3023. {
  3024. struct path old_path;
  3025. int err;
  3026. if (!old_name || !*old_name)
  3027. return -EINVAL;
  3028. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  3029. if (err)
  3030. return err;
  3031. err = do_move_mount(&old_path, path);
  3032. path_put(&old_path);
  3033. return err;
  3034. }
  3035. /*
  3036. * add a mount into a namespace's mount tree
  3037. */
  3038. static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
  3039. const struct path *path, int mnt_flags)
  3040. {
  3041. struct mount *parent = real_mount(path->mnt);
  3042. mnt_flags &= ~MNT_INTERNAL_FLAGS;
  3043. if (unlikely(!check_mnt(parent))) {
  3044. /* that's acceptable only for automounts done in private ns */
  3045. if (!(mnt_flags & MNT_SHRINKABLE))
  3046. return -EINVAL;
  3047. /* ... and for those we'd better have mountpoint still alive */
  3048. if (!parent->mnt_ns)
  3049. return -EINVAL;
  3050. }
  3051. /* Refuse the same filesystem on the same mount point */
  3052. #ifdef CONFIG_KDP_NS
  3053. if (path->mnt->mnt_sb == ((struct kdp_mount *)newmnt)->mnt->mnt_sb &&
  3054. #else
  3055. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  3056. #endif
  3057. path->mnt->mnt_root == path->dentry)
  3058. return -EBUSY;
  3059. #ifdef CONFIG_KDP_NS
  3060. if (d_is_symlink(((struct kdp_mount *)newmnt)->mnt->mnt_root))
  3061. #else
  3062. if (d_is_symlink(newmnt->mnt.mnt_root))
  3063. #endif
  3064. return -EINVAL;
  3065. #ifdef CONFIG_KDP_NS
  3066. kdp_assign_mnt_flags(((struct kdp_mount *)newmnt)->mnt, mnt_flags);
  3067. #else
  3068. newmnt->mnt.mnt_flags = mnt_flags;
  3069. #endif
  3070. return graft_tree(newmnt, parent, mp);
  3071. }
  3072. static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags);
  3073. /*
  3074. * Create a new mount using a superblock configuration and request it
  3075. * be added to the namespace tree.
  3076. */
  3077. static int do_new_mount_fc(struct fs_context *fc, struct path *mountpoint,
  3078. unsigned int mnt_flags)
  3079. {
  3080. struct vfsmount *mnt;
  3081. struct mountpoint *mp;
  3082. struct super_block *sb = fc->root->d_sb;
  3083. int error;
  3084. error = security_sb_kern_mount(sb);
  3085. if (!error && mount_too_revealing(sb, &mnt_flags))
  3086. error = -EPERM;
  3087. if (unlikely(error)) {
  3088. fc_drop_locked(fc);
  3089. return error;
  3090. }
  3091. up_write(&sb->s_umount);
  3092. mnt = vfs_create_mount(fc);
  3093. if (IS_ERR(mnt))
  3094. return PTR_ERR(mnt);
  3095. mnt_warn_timestamp_expiry(mountpoint, mnt);
  3096. mp = lock_mount(mountpoint);
  3097. if (IS_ERR(mp)) {
  3098. mntput(mnt);
  3099. return PTR_ERR(mp);
  3100. }
  3101. error = do_add_mount(real_mount(mnt), mp, mountpoint, mnt_flags);
  3102. unlock_mount(mp);
  3103. if (error < 0)
  3104. mntput(mnt);
  3105. return error;
  3106. }
  3107. /*
  3108. * create a new mount for userspace and request it to be added into the
  3109. * namespace's tree
  3110. */
  3111. static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
  3112. int mnt_flags, const char *name, void *data)
  3113. {
  3114. struct file_system_type *type;
  3115. struct fs_context *fc;
  3116. const char *subtype = NULL;
  3117. int err = 0;
  3118. if (!fstype)
  3119. return -EINVAL;
  3120. type = get_fs_type(fstype);
  3121. if (!type)
  3122. return -ENODEV;
  3123. if (type->fs_flags & FS_HAS_SUBTYPE) {
  3124. subtype = strchr(fstype, '.');
  3125. if (subtype) {
  3126. subtype++;
  3127. if (!*subtype) {
  3128. put_filesystem(type);
  3129. return -EINVAL;
  3130. }
  3131. }
  3132. }
  3133. fc = fs_context_for_mount(type, sb_flags);
  3134. put_filesystem(type);
  3135. if (IS_ERR(fc))
  3136. return PTR_ERR(fc);
  3137. if (subtype)
  3138. err = vfs_parse_fs_string(fc, "subtype",
  3139. subtype, strlen(subtype));
  3140. if (!err && name)
  3141. err = vfs_parse_fs_string(fc, "source", name, strlen(name));
  3142. if (!err)
  3143. err = parse_monolithic_mount_data(fc, data);
  3144. if (!err && !mount_capable(fc))
  3145. err = -EPERM;
  3146. if (!err)
  3147. err = vfs_get_tree(fc);
  3148. if (!err)
  3149. err = do_new_mount_fc(fc, path, mnt_flags);
  3150. put_fs_context(fc);
  3151. return err;
  3152. }
  3153. int finish_automount(struct vfsmount *m, const struct path *path)
  3154. {
  3155. struct dentry *dentry = path->dentry;
  3156. struct mountpoint *mp;
  3157. struct mount *mnt;
  3158. int err;
  3159. if (!m)
  3160. return 0;
  3161. if (IS_ERR(m))
  3162. return PTR_ERR(m);
  3163. mnt = real_mount(m);
  3164. /* The new mount record should have at least 2 refs to prevent it being
  3165. * expired before we get a chance to add it
  3166. */
  3167. BUG_ON(mnt_get_count(mnt) < 2);
  3168. if (m->mnt_sb == path->mnt->mnt_sb &&
  3169. m->mnt_root == dentry) {
  3170. err = -ELOOP;
  3171. goto discard;
  3172. }
  3173. /*
  3174. * we don't want to use lock_mount() - in this case finding something
  3175. * that overmounts our mountpoint to be means "quitely drop what we've
  3176. * got", not "try to mount it on top".
  3177. */
  3178. inode_lock(dentry->d_inode);
  3179. namespace_lock();
  3180. if (unlikely(cant_mount(dentry))) {
  3181. err = -ENOENT;
  3182. goto discard_locked;
  3183. }
  3184. rcu_read_lock();
  3185. if (unlikely(__lookup_mnt(path->mnt, dentry))) {
  3186. rcu_read_unlock();
  3187. err = 0;
  3188. goto discard_locked;
  3189. }
  3190. rcu_read_unlock();
  3191. mp = get_mountpoint(dentry);
  3192. if (IS_ERR(mp)) {
  3193. err = PTR_ERR(mp);
  3194. goto discard_locked;
  3195. }
  3196. err = do_add_mount(mnt, mp, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  3197. unlock_mount(mp);
  3198. if (unlikely(err))
  3199. goto discard;
  3200. mntput(m);
  3201. return 0;
  3202. discard_locked:
  3203. namespace_unlock();
  3204. inode_unlock(dentry->d_inode);
  3205. discard:
  3206. /* remove m from any expiration list it may be on */
  3207. if (!list_empty(&mnt->mnt_expire)) {
  3208. namespace_lock();
  3209. list_del_init(&mnt->mnt_expire);
  3210. namespace_unlock();
  3211. }
  3212. mntput(m);
  3213. mntput(m);
  3214. return err;
  3215. }
  3216. /**
  3217. * mnt_set_expiry - Put a mount on an expiration list
  3218. * @mnt: The mount to list.
  3219. * @expiry_list: The list to add the mount to.
  3220. */
  3221. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  3222. {
  3223. namespace_lock();
  3224. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  3225. namespace_unlock();
  3226. }
  3227. EXPORT_SYMBOL(mnt_set_expiry);
  3228. /*
  3229. * process a list of expirable mountpoints with the intent of discarding any
  3230. * mountpoints that aren't in use and haven't been touched since last we came
  3231. * here
  3232. */
  3233. void mark_mounts_for_expiry(struct list_head *mounts)
  3234. {
  3235. struct mount *mnt, *next;
  3236. LIST_HEAD(graveyard);
  3237. if (list_empty(mounts))
  3238. return;
  3239. namespace_lock();
  3240. lock_mount_hash();
  3241. /* extract from the expiration list every vfsmount that matches the
  3242. * following criteria:
  3243. * - only referenced by its parent vfsmount
  3244. * - still marked for expiry (marked on the last call here; marks are
  3245. * cleared by mntput())
  3246. */
  3247. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  3248. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  3249. propagate_mount_busy(mnt, 1))
  3250. continue;
  3251. list_move(&mnt->mnt_expire, &graveyard);
  3252. }
  3253. while (!list_empty(&graveyard)) {
  3254. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  3255. touch_mnt_namespace(mnt->mnt_ns);
  3256. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  3257. }
  3258. unlock_mount_hash();
  3259. namespace_unlock();
  3260. }
  3261. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  3262. /*
  3263. * Ripoff of 'select_parent()'
  3264. *
  3265. * search the list of submounts for a given mountpoint, and move any
  3266. * shrinkable submounts to the 'graveyard' list.
  3267. */
  3268. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  3269. {
  3270. struct mount *this_parent = parent;
  3271. struct list_head *next;
  3272. int found = 0;
  3273. repeat:
  3274. next = this_parent->mnt_mounts.next;
  3275. resume:
  3276. while (next != &this_parent->mnt_mounts) {
  3277. struct list_head *tmp = next;
  3278. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  3279. next = tmp->next;
  3280. #ifdef CONFIG_KDP_NS
  3281. if (!(((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_SHRINKABLE))
  3282. #else
  3283. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  3284. #endif
  3285. continue;
  3286. /*
  3287. * Descend a level if the d_mounts list is non-empty.
  3288. */
  3289. if (!list_empty(&mnt->mnt_mounts)) {
  3290. this_parent = mnt;
  3291. goto repeat;
  3292. }
  3293. if (!propagate_mount_busy(mnt, 1)) {
  3294. list_move_tail(&mnt->mnt_expire, graveyard);
  3295. found++;
  3296. }
  3297. }
  3298. /*
  3299. * All done at this level ... ascend and resume the search
  3300. */
  3301. if (this_parent != parent) {
  3302. next = this_parent->mnt_child.next;
  3303. this_parent = this_parent->mnt_parent;
  3304. goto resume;
  3305. }
  3306. return found;
  3307. }
  3308. /*
  3309. * process a list of expirable mountpoints with the intent of discarding any
  3310. * submounts of a specific parent mountpoint
  3311. *
  3312. * mount_lock must be held for write
  3313. */
  3314. static void shrink_submounts(struct mount *mnt)
  3315. {
  3316. LIST_HEAD(graveyard);
  3317. struct mount *m;
  3318. /* extract submounts of 'mountpoint' from the expiration list */
  3319. while (select_submounts(mnt, &graveyard)) {
  3320. while (!list_empty(&graveyard)) {
  3321. m = list_first_entry(&graveyard, struct mount,
  3322. mnt_expire);
  3323. touch_mnt_namespace(m->mnt_ns);
  3324. umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  3325. }
  3326. }
  3327. }
  3328. static void *copy_mount_options(const void __user * data)
  3329. {
  3330. char *copy;
  3331. unsigned left, offset;
  3332. if (!data)
  3333. return NULL;
  3334. copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3335. if (!copy)
  3336. return ERR_PTR(-ENOMEM);
  3337. left = copy_from_user(copy, data, PAGE_SIZE);
  3338. /*
  3339. * Not all architectures have an exact copy_from_user(). Resort to
  3340. * byte at a time.
  3341. */
  3342. offset = PAGE_SIZE - left;
  3343. while (left) {
  3344. char c;
  3345. if (get_user(c, (const char __user *)data + offset))
  3346. break;
  3347. copy[offset] = c;
  3348. left--;
  3349. offset++;
  3350. }
  3351. if (left == PAGE_SIZE) {
  3352. kfree(copy);
  3353. return ERR_PTR(-EFAULT);
  3354. }
  3355. return copy;
  3356. }
  3357. static char *copy_mount_string(const void __user *data)
  3358. {
  3359. return data ? strndup_user(data, PATH_MAX) : NULL;
  3360. }
  3361. /*
  3362. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  3363. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  3364. *
  3365. * data is a (void *) that can point to any structure up to
  3366. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  3367. * information (or be NULL).
  3368. *
  3369. * Pre-0.97 versions of mount() didn't have a flags word.
  3370. * When the flags word was introduced its top half was required
  3371. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  3372. * Therefore, if this magic number is present, it carries no information
  3373. * and must be discarded.
  3374. */
  3375. int path_mount(const char *dev_name, struct path *path,
  3376. const char *type_page, unsigned long flags, void *data_page)
  3377. {
  3378. unsigned int mnt_flags = 0, sb_flags;
  3379. int ret;
  3380. /* Discard magic */
  3381. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  3382. flags &= ~MS_MGC_MSK;
  3383. /* Basic sanity checks */
  3384. if (data_page)
  3385. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  3386. if (flags & MS_NOUSER)
  3387. return -EINVAL;
  3388. ret = security_sb_mount(dev_name, path, type_page, flags, data_page);
  3389. if (ret)
  3390. return ret;
  3391. if (!may_mount())
  3392. return -EPERM;
  3393. if (flags & SB_MANDLOCK)
  3394. warn_mandlock();
  3395. /* Default to relatime unless overriden */
  3396. if (!(flags & MS_NOATIME))
  3397. mnt_flags |= MNT_RELATIME;
  3398. /* Separate the per-mountpoint flags */
  3399. if (flags & MS_NOSUID)
  3400. mnt_flags |= MNT_NOSUID;
  3401. if (flags & MS_NODEV)
  3402. mnt_flags |= MNT_NODEV;
  3403. if (flags & MS_NOEXEC)
  3404. mnt_flags |= MNT_NOEXEC;
  3405. if (flags & MS_NOATIME)
  3406. mnt_flags |= MNT_NOATIME;
  3407. if (flags & MS_NODIRATIME)
  3408. mnt_flags |= MNT_NODIRATIME;
  3409. if (flags & MS_STRICTATIME)
  3410. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  3411. if (flags & MS_RDONLY)
  3412. mnt_flags |= MNT_READONLY;
  3413. if (flags & MS_NOSYMFOLLOW)
  3414. mnt_flags |= MNT_NOSYMFOLLOW;
  3415. /* The default atime for remount is preservation */
  3416. if ((flags & MS_REMOUNT) &&
  3417. ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
  3418. MS_STRICTATIME)) == 0)) {
  3419. mnt_flags &= ~MNT_ATIME_MASK;
  3420. mnt_flags |= path->mnt->mnt_flags & MNT_ATIME_MASK;
  3421. }
  3422. sb_flags = flags & (SB_RDONLY |
  3423. SB_SYNCHRONOUS |
  3424. SB_MANDLOCK |
  3425. SB_DIRSYNC |
  3426. SB_SILENT |
  3427. SB_POSIXACL |
  3428. SB_LAZYTIME |
  3429. SB_I_VERSION);
  3430. if ((flags & (MS_REMOUNT | MS_BIND)) == (MS_REMOUNT | MS_BIND))
  3431. return do_reconfigure_mnt(path, mnt_flags);
  3432. if (flags & MS_REMOUNT)
  3433. return do_remount(path, flags, sb_flags, mnt_flags, data_page);
  3434. if (flags & MS_BIND)
  3435. return do_loopback(path, dev_name, flags & MS_REC);
  3436. if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  3437. return do_change_type(path, flags);
  3438. if (flags & MS_MOVE)
  3439. return do_move_mount_old(path, dev_name);
  3440. return do_new_mount(path, type_page, sb_flags, mnt_flags, dev_name,
  3441. data_page);
  3442. }
  3443. long do_mount(const char *dev_name, const char __user *dir_name,
  3444. const char *type_page, unsigned long flags, void *data_page)
  3445. {
  3446. struct path path;
  3447. int ret;
  3448. ret = user_path_at(AT_FDCWD, dir_name, LOOKUP_FOLLOW, &path);
  3449. if (ret)
  3450. return ret;
  3451. ret = path_mount(dev_name, &path, type_page, flags, data_page);
  3452. path_put(&path);
  3453. return ret;
  3454. }
  3455. static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
  3456. {
  3457. return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
  3458. }
  3459. static void dec_mnt_namespaces(struct ucounts *ucounts)
  3460. {
  3461. dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
  3462. }
  3463. static void free_mnt_ns(struct mnt_namespace *ns)
  3464. {
  3465. if (!is_anon_ns(ns))
  3466. ns_free_inum(&ns->ns);
  3467. dec_mnt_namespaces(ns->ucounts);
  3468. put_user_ns(ns->user_ns);
  3469. kfree(ns);
  3470. }
  3471. /*
  3472. * Assign a sequence number so we can detect when we attempt to bind
  3473. * mount a reference to an older mount namespace into the current
  3474. * mount namespace, preventing reference counting loops. A 64bit
  3475. * number incrementing at 10Ghz will take 12,427 years to wrap which
  3476. * is effectively never, so we can ignore the possibility.
  3477. */
  3478. static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
  3479. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool anon)
  3480. {
  3481. struct mnt_namespace *new_ns;
  3482. struct ucounts *ucounts;
  3483. int ret;
  3484. ucounts = inc_mnt_namespaces(user_ns);
  3485. if (!ucounts)
  3486. return ERR_PTR(-ENOSPC);
  3487. new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
  3488. if (!new_ns) {
  3489. dec_mnt_namespaces(ucounts);
  3490. return ERR_PTR(-ENOMEM);
  3491. }
  3492. if (!anon) {
  3493. ret = ns_alloc_inum(&new_ns->ns);
  3494. if (ret) {
  3495. kfree(new_ns);
  3496. dec_mnt_namespaces(ucounts);
  3497. return ERR_PTR(ret);
  3498. }
  3499. }
  3500. new_ns->ns.ops = &mntns_operations;
  3501. if (!anon)
  3502. new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
  3503. refcount_set(&new_ns->ns.count, 1);
  3504. INIT_LIST_HEAD(&new_ns->list);
  3505. init_waitqueue_head(&new_ns->poll);
  3506. spin_lock_init(&new_ns->ns_lock);
  3507. new_ns->user_ns = get_user_ns(user_ns);
  3508. new_ns->ucounts = ucounts;
  3509. return new_ns;
  3510. }
  3511. __latent_entropy
  3512. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  3513. struct user_namespace *user_ns, struct fs_struct *new_fs)
  3514. {
  3515. struct mnt_namespace *new_ns;
  3516. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  3517. struct mount *p, *q;
  3518. struct mount *old;
  3519. struct mount *new;
  3520. int copy_flags;
  3521. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  3522. bool is_zygote_pid = susfs_is_current_zygote_domain();
  3523. int last_entry_mnt_id = 0;
  3524. #endif
  3525. BUG_ON(!ns);
  3526. if (likely(!(flags & CLONE_NEWNS))) {
  3527. get_mnt_ns(ns);
  3528. return ns;
  3529. }
  3530. old = ns->root;
  3531. new_ns = alloc_mnt_ns(user_ns, false);
  3532. if (IS_ERR(new_ns))
  3533. return new_ns;
  3534. namespace_lock();
  3535. /* First pass: copy the tree topology */
  3536. copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
  3537. if (user_ns != ns->user_ns)
  3538. copy_flags |= CL_SHARED_TO_SLAVE;
  3539. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  3540. // Always let clone_mnt() in copy_tree() know it is from copy_mnt_ns()
  3541. copy_flags |= CL_COPY_MNT_NS;
  3542. if (is_zygote_pid) {
  3543. // Let clone_mnt() in copy_tree() know copy_mnt_ns() is run by zygote process
  3544. copy_flags |= CL_ZYGOTE_COPY_MNT_NS;
  3545. }
  3546. #endif
  3547. #ifdef CONFIG_KDP_NS
  3548. new = copy_tree(old, ((struct kdp_mount *)old)->mnt->mnt_root, copy_flags);
  3549. #else
  3550. new = copy_tree(old, old->mnt.mnt_root, copy_flags);
  3551. #endif
  3552. if (IS_ERR(new)) {
  3553. namespace_unlock();
  3554. free_mnt_ns(new_ns);
  3555. return ERR_CAST(new);
  3556. }
  3557. if (user_ns != ns->user_ns) {
  3558. lock_mount_hash();
  3559. lock_mnt_tree(new);
  3560. unlock_mount_hash();
  3561. }
  3562. new_ns->root = new;
  3563. list_add_tail(&new_ns->list, &new->mnt_list);
  3564. /*
  3565. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  3566. * as belonging to new namespace. We have already acquired a private
  3567. * fs_struct, so tsk->fs->lock is not needed.
  3568. */
  3569. p = old;
  3570. q = new;
  3571. while (p) {
  3572. q->mnt_ns = new_ns;
  3573. new_ns->mounts++;
  3574. if (new_fs) {
  3575. #ifdef CONFIG_KDP_NS
  3576. if (((struct kdp_mount *)p)->mnt == new_fs->root.mnt) {
  3577. new_fs->root.mnt = mntget(((struct kdp_mount *)q)->mnt);
  3578. rootmnt = ((struct kdp_mount *)p)->mnt;
  3579. #else
  3580. if (&p->mnt == new_fs->root.mnt) {
  3581. new_fs->root.mnt = mntget(&q->mnt);
  3582. rootmnt = &p->mnt;
  3583. #endif
  3584. }
  3585. #ifdef CONFIG_KDP_NS
  3586. if (((struct kdp_mount *)p)->mnt == new_fs->pwd.mnt) {
  3587. new_fs->pwd.mnt = mntget(((struct kdp_mount *)q)->mnt);
  3588. pwdmnt = ((struct kdp_mount *)p)->mnt;
  3589. #else
  3590. if (&p->mnt == new_fs->pwd.mnt) {
  3591. new_fs->pwd.mnt = mntget(&q->mnt);
  3592. pwdmnt = &p->mnt;
  3593. #endif
  3594. }
  3595. }
  3596. p = next_mnt(p, old);
  3597. q = next_mnt(q, new);
  3598. if (!q)
  3599. break;
  3600. #ifdef CONFIG_KDP_NS
  3601. while (((struct kdp_mount *)p)->mnt->mnt_root != ((struct kdp_mount *)q)->mnt->mnt_root)
  3602. #else
  3603. while (p->mnt.mnt_root != q->mnt.mnt_root)
  3604. #endif
  3605. p = next_mnt(p, old);
  3606. }
  3607. #ifdef CONFIG_KSU_SUSFS_SUS_MOUNT
  3608. // current->susfs_last_fake_mnt_id -> to record last valid fake mnt_id to zygote pid
  3609. // q->mnt.susfs_mnt_id_backup -> original mnt_id
  3610. // q->mnt_id -> will be modified to the fake mnt_id
  3611. // Here We are only interested in processes of which original mnt namespace belongs to zygote
  3612. // Also we just make use of existing 'q' mount pointer, no need to delcare extra mount pointer
  3613. if (is_zygote_pid) {
  3614. last_entry_mnt_id = list_first_entry(&new_ns->list, struct mount, mnt_list)->mnt_id;
  3615. list_for_each_entry(q, &new_ns->list, mnt_list) {
  3616. if (unlikely(q->mnt_id >= DEFAULT_SUS_MNT_ID)) {
  3617. continue;
  3618. }
  3619. q->mnt.susfs_mnt_id_backup = q->mnt_id;
  3620. q->mnt_id = last_entry_mnt_id++;
  3621. }
  3622. }
  3623. // Assign the 'last_entry_mnt_id' to 'current->susfs_last_fake_mnt_id' for later use.
  3624. // should be fine here assuming zygote is forking/unsharing app in one single thread.
  3625. // Or should we put a lock here?
  3626. current->susfs_last_fake_mnt_id = last_entry_mnt_id;
  3627. #endif
  3628. namespace_unlock();
  3629. if (rootmnt)
  3630. mntput(rootmnt);
  3631. if (pwdmnt)
  3632. mntput(pwdmnt);
  3633. return new_ns;
  3634. }
  3635. struct dentry *mount_subtree(struct vfsmount *m, const char *name)
  3636. {
  3637. struct mount *mnt = real_mount(m);
  3638. struct mnt_namespace *ns;
  3639. struct super_block *s;
  3640. struct path path;
  3641. int err;
  3642. ns = alloc_mnt_ns(&init_user_ns, true);
  3643. if (IS_ERR(ns)) {
  3644. mntput(m);
  3645. return ERR_CAST(ns);
  3646. }
  3647. mnt->mnt_ns = ns;
  3648. ns->root = mnt;
  3649. ns->mounts++;
  3650. list_add(&mnt->mnt_list, &ns->list);
  3651. err = vfs_path_lookup(m->mnt_root, m,
  3652. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  3653. put_mnt_ns(ns);
  3654. if (err)
  3655. return ERR_PTR(err);
  3656. /* trade a vfsmount reference for active sb one */
  3657. s = path.mnt->mnt_sb;
  3658. atomic_inc(&s->s_active);
  3659. mntput(path.mnt);
  3660. /* lock the sucker */
  3661. down_write(&s->s_umount);
  3662. /* ... and return the root of (sub)tree on it */
  3663. return path.dentry;
  3664. }
  3665. EXPORT_SYMBOL(mount_subtree);
  3666. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  3667. char __user *, type, unsigned long, flags, void __user *, data)
  3668. {
  3669. int ret;
  3670. char *kernel_type;
  3671. char *kernel_dev;
  3672. void *options;
  3673. kernel_type = copy_mount_string(type);
  3674. ret = PTR_ERR(kernel_type);
  3675. if (IS_ERR(kernel_type))
  3676. goto out_type;
  3677. kernel_dev = copy_mount_string(dev_name);
  3678. ret = PTR_ERR(kernel_dev);
  3679. if (IS_ERR(kernel_dev))
  3680. goto out_dev;
  3681. options = copy_mount_options(data);
  3682. ret = PTR_ERR(options);
  3683. if (IS_ERR(options))
  3684. goto out_data;
  3685. ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  3686. #if defined(CONFIG_KSU_SUSFS_AUTO_ADD_SUS_KSU_DEFAULT_MOUNT) && defined(CONFIG_KSU_SUSFS_HAS_MAGIC_MOUNT)
  3687. // Just for the compatibility of Magic Mount KernelSU
  3688. if (!ret && susfs_is_auto_add_sus_ksu_default_mount_enabled && susfs_is_current_ksu_domain()) {
  3689. susfs_auto_add_sus_ksu_default_mount(dir_name);
  3690. }
  3691. #endif
  3692. kfree(options);
  3693. out_data:
  3694. kfree(kernel_dev);
  3695. out_dev:
  3696. kfree(kernel_type);
  3697. out_type:
  3698. return ret;
  3699. }
  3700. #define FSMOUNT_VALID_FLAGS \
  3701. (MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NODEV | \
  3702. MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME | MOUNT_ATTR_NODIRATIME | \
  3703. MOUNT_ATTR_NOSYMFOLLOW)
  3704. #define MOUNT_SETATTR_VALID_FLAGS (FSMOUNT_VALID_FLAGS | MOUNT_ATTR_IDMAP)
  3705. #define MOUNT_SETATTR_PROPAGATION_FLAGS \
  3706. (MS_UNBINDABLE | MS_PRIVATE | MS_SLAVE | MS_SHARED)
  3707. static unsigned int attr_flags_to_mnt_flags(u64 attr_flags)
  3708. {
  3709. unsigned int mnt_flags = 0;
  3710. if (attr_flags & MOUNT_ATTR_RDONLY)
  3711. mnt_flags |= MNT_READONLY;
  3712. if (attr_flags & MOUNT_ATTR_NOSUID)
  3713. mnt_flags |= MNT_NOSUID;
  3714. if (attr_flags & MOUNT_ATTR_NODEV)
  3715. mnt_flags |= MNT_NODEV;
  3716. if (attr_flags & MOUNT_ATTR_NOEXEC)
  3717. mnt_flags |= MNT_NOEXEC;
  3718. if (attr_flags & MOUNT_ATTR_NODIRATIME)
  3719. mnt_flags |= MNT_NODIRATIME;
  3720. if (attr_flags & MOUNT_ATTR_NOSYMFOLLOW)
  3721. mnt_flags |= MNT_NOSYMFOLLOW;
  3722. return mnt_flags;
  3723. }
  3724. /*
  3725. * Create a kernel mount representation for a new, prepared superblock
  3726. * (specified by fs_fd) and attach to an open_tree-like file descriptor.
  3727. */
  3728. SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
  3729. unsigned int, attr_flags)
  3730. {
  3731. struct mnt_namespace *ns;
  3732. struct fs_context *fc;
  3733. struct file *file;
  3734. struct path newmount;
  3735. struct mount *mnt;
  3736. struct fd f;
  3737. unsigned int mnt_flags = 0;
  3738. long ret;
  3739. if (!may_mount())
  3740. return -EPERM;
  3741. if ((flags & ~(FSMOUNT_CLOEXEC)) != 0)
  3742. return -EINVAL;
  3743. if (attr_flags & ~FSMOUNT_VALID_FLAGS)
  3744. return -EINVAL;
  3745. mnt_flags = attr_flags_to_mnt_flags(attr_flags);
  3746. switch (attr_flags & MOUNT_ATTR__ATIME) {
  3747. case MOUNT_ATTR_STRICTATIME:
  3748. break;
  3749. case MOUNT_ATTR_NOATIME:
  3750. mnt_flags |= MNT_NOATIME;
  3751. break;
  3752. case MOUNT_ATTR_RELATIME:
  3753. mnt_flags |= MNT_RELATIME;
  3754. break;
  3755. default:
  3756. return -EINVAL;
  3757. }
  3758. f = fdget(fs_fd);
  3759. if (!f.file)
  3760. return -EBADF;
  3761. ret = -EINVAL;
  3762. if (f.file->f_op != &fscontext_fops)
  3763. goto err_fsfd;
  3764. fc = f.file->private_data;
  3765. ret = mutex_lock_interruptible(&fc->uapi_mutex);
  3766. if (ret < 0)
  3767. goto err_fsfd;
  3768. /* There must be a valid superblock or we can't mount it */
  3769. ret = -EINVAL;
  3770. if (!fc->root)
  3771. goto err_unlock;
  3772. ret = -EPERM;
  3773. if (mount_too_revealing(fc->root->d_sb, &mnt_flags)) {
  3774. pr_warn("VFS: Mount too revealing\n");
  3775. goto err_unlock;
  3776. }
  3777. ret = -EBUSY;
  3778. if (fc->phase != FS_CONTEXT_AWAITING_MOUNT)
  3779. goto err_unlock;
  3780. if (fc->sb_flags & SB_MANDLOCK)
  3781. warn_mandlock();
  3782. newmount.mnt = vfs_create_mount(fc);
  3783. if (IS_ERR(newmount.mnt)) {
  3784. ret = PTR_ERR(newmount.mnt);
  3785. goto err_unlock;
  3786. }
  3787. newmount.dentry = dget(fc->root);
  3788. newmount.mnt->mnt_flags = mnt_flags;
  3789. /* We've done the mount bit - now move the file context into more or
  3790. * less the same state as if we'd done an fspick(). We don't want to
  3791. * do any memory allocation or anything like that at this point as we
  3792. * don't want to have to handle any errors incurred.
  3793. */
  3794. vfs_clean_context(fc);
  3795. ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
  3796. if (IS_ERR(ns)) {
  3797. ret = PTR_ERR(ns);
  3798. goto err_path;
  3799. }
  3800. mnt = real_mount(newmount.mnt);
  3801. mnt->mnt_ns = ns;
  3802. ns->root = mnt;
  3803. ns->mounts = 1;
  3804. list_add(&mnt->mnt_list, &ns->list);
  3805. mntget(newmount.mnt);
  3806. /* Attach to an apparent O_PATH fd with a note that we need to unmount
  3807. * it, not just simply put it.
  3808. */
  3809. file = dentry_open(&newmount, O_PATH, fc->cred);
  3810. if (IS_ERR(file)) {
  3811. dissolve_on_fput(newmount.mnt);
  3812. ret = PTR_ERR(file);
  3813. goto err_path;
  3814. }
  3815. file->f_mode |= FMODE_NEED_UNMOUNT;
  3816. ret = get_unused_fd_flags((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0);
  3817. if (ret >= 0)
  3818. fd_install(ret, file);
  3819. else
  3820. fput(file);
  3821. err_path:
  3822. path_put(&newmount);
  3823. err_unlock:
  3824. mutex_unlock(&fc->uapi_mutex);
  3825. err_fsfd:
  3826. fdput(f);
  3827. return ret;
  3828. }
  3829. /*
  3830. * Move a mount from one place to another. In combination with
  3831. * fsopen()/fsmount() this is used to install a new mount and in combination
  3832. * with open_tree(OPEN_TREE_CLONE [| AT_RECURSIVE]) it can be used to copy
  3833. * a mount subtree.
  3834. *
  3835. * Note the flags value is a combination of MOVE_MOUNT_* flags.
  3836. */
  3837. SYSCALL_DEFINE5(move_mount,
  3838. int, from_dfd, const char __user *, from_pathname,
  3839. int, to_dfd, const char __user *, to_pathname,
  3840. unsigned int, flags)
  3841. {
  3842. struct path from_path, to_path;
  3843. unsigned int lflags;
  3844. int ret = 0;
  3845. if (!may_mount())
  3846. return -EPERM;
  3847. if (flags & ~MOVE_MOUNT__MASK)
  3848. return -EINVAL;
  3849. /* If someone gives a pathname, they aren't permitted to move
  3850. * from an fd that requires unmount as we can't get at the flag
  3851. * to clear it afterwards.
  3852. */
  3853. lflags = 0;
  3854. if (flags & MOVE_MOUNT_F_SYMLINKS) lflags |= LOOKUP_FOLLOW;
  3855. if (flags & MOVE_MOUNT_F_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
  3856. if (flags & MOVE_MOUNT_F_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
  3857. ret = user_path_at(from_dfd, from_pathname, lflags, &from_path);
  3858. if (ret < 0)
  3859. return ret;
  3860. lflags = 0;
  3861. if (flags & MOVE_MOUNT_T_SYMLINKS) lflags |= LOOKUP_FOLLOW;
  3862. if (flags & MOVE_MOUNT_T_AUTOMOUNTS) lflags |= LOOKUP_AUTOMOUNT;
  3863. if (flags & MOVE_MOUNT_T_EMPTY_PATH) lflags |= LOOKUP_EMPTY;
  3864. ret = user_path_at(to_dfd, to_pathname, lflags, &to_path);
  3865. if (ret < 0)
  3866. goto out_from;
  3867. ret = security_move_mount(&from_path, &to_path);
  3868. if (ret < 0)
  3869. goto out_to;
  3870. if (flags & MOVE_MOUNT_SET_GROUP)
  3871. ret = do_set_group(&from_path, &to_path);
  3872. else
  3873. ret = do_move_mount(&from_path, &to_path);
  3874. out_to:
  3875. path_put(&to_path);
  3876. out_from:
  3877. path_put(&from_path);
  3878. #ifdef CONFIG_KSU_SUSFS_AUTO_ADD_SUS_KSU_DEFAULT_MOUNT
  3879. // For Legacy KSU mount scheme
  3880. if (!ret && susfs_is_auto_add_sus_ksu_default_mount_enabled && susfs_is_current_ksu_domain()) {
  3881. susfs_auto_add_sus_ksu_default_mount(to_pathname);
  3882. }
  3883. #endif
  3884. return ret;
  3885. }
  3886. /*
  3887. * Return true if path is reachable from root
  3888. *
  3889. * namespace_sem or mount_lock is held
  3890. */
  3891. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  3892. const struct path *root)
  3893. {
  3894. #ifdef CONFIG_KDP_NS
  3895. while (((struct kdp_mount *)mnt)->mnt != root->mnt && mnt_has_parent(mnt)) {
  3896. #else
  3897. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  3898. #endif
  3899. dentry = mnt->mnt_mountpoint;
  3900. mnt = mnt->mnt_parent;
  3901. }
  3902. #ifdef CONFIG_KDP_NS
  3903. return ((struct kdp_mount *)mnt)->mnt == root->mnt && is_subdir(dentry, root->dentry);
  3904. #else
  3905. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  3906. #endif
  3907. }
  3908. bool path_is_under(const struct path *path1, const struct path *path2)
  3909. {
  3910. bool res;
  3911. read_seqlock_excl(&mount_lock);
  3912. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  3913. read_sequnlock_excl(&mount_lock);
  3914. return res;
  3915. }
  3916. EXPORT_SYMBOL(path_is_under);
  3917. /*
  3918. * pivot_root Semantics:
  3919. * Moves the root file system of the current process to the directory put_old,
  3920. * makes new_root as the new root file system of the current process, and sets
  3921. * root/cwd of all processes which had them on the current root to new_root.
  3922. *
  3923. * Restrictions:
  3924. * The new_root and put_old must be directories, and must not be on the
  3925. * same file system as the current process root. The put_old must be
  3926. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  3927. * pointed to by put_old must yield the same directory as new_root. No other
  3928. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  3929. *
  3930. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  3931. * See Documentation/filesystems/ramfs-rootfs-initramfs.rst for alternatives
  3932. * in this situation.
  3933. *
  3934. * Notes:
  3935. * - we don't move root/cwd if they are not at the root (reason: if something
  3936. * cared enough to change them, it's probably wrong to force them elsewhere)
  3937. * - it's okay to pick a root that isn't the root of a file system, e.g.
  3938. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  3939. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  3940. * first.
  3941. */
  3942. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  3943. const char __user *, put_old)
  3944. {
  3945. struct path new, old, root;
  3946. struct mount *new_mnt, *root_mnt, *old_mnt, *root_parent, *ex_parent;
  3947. struct mountpoint *old_mp, *root_mp;
  3948. int error;
  3949. if (!may_mount())
  3950. return -EPERM;
  3951. error = user_path_at(AT_FDCWD, new_root,
  3952. LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &new);
  3953. if (error)
  3954. goto out0;
  3955. error = user_path_at(AT_FDCWD, put_old,
  3956. LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old);
  3957. if (error)
  3958. goto out1;
  3959. error = security_sb_pivotroot(&old, &new);
  3960. if (error)
  3961. goto out2;
  3962. get_fs_root(current->fs, &root);
  3963. old_mp = lock_mount(&old);
  3964. error = PTR_ERR(old_mp);
  3965. if (IS_ERR(old_mp))
  3966. goto out3;
  3967. error = -EINVAL;
  3968. new_mnt = real_mount(new.mnt);
  3969. root_mnt = real_mount(root.mnt);
  3970. old_mnt = real_mount(old.mnt);
  3971. ex_parent = new_mnt->mnt_parent;
  3972. root_parent = root_mnt->mnt_parent;
  3973. if (IS_MNT_SHARED(old_mnt) ||
  3974. IS_MNT_SHARED(ex_parent) ||
  3975. IS_MNT_SHARED(root_parent))
  3976. goto out4;
  3977. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  3978. goto out4;
  3979. #ifdef CONFIG_KDP_NS
  3980. if (((struct kdp_mount *)new_mnt)->mnt->mnt_flags & MNT_LOCKED)
  3981. #else
  3982. if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
  3983. #endif
  3984. goto out4;
  3985. error = -ENOENT;
  3986. if (d_unlinked(new.dentry))
  3987. goto out4;
  3988. error = -EBUSY;
  3989. if (new_mnt == root_mnt || old_mnt == root_mnt)
  3990. goto out4; /* loop, on the same file system */
  3991. error = -EINVAL;
  3992. if (root.mnt->mnt_root != root.dentry)
  3993. goto out4; /* not a mountpoint */
  3994. if (!mnt_has_parent(root_mnt))
  3995. goto out4; /* not attached */
  3996. if (new.mnt->mnt_root != new.dentry)
  3997. goto out4; /* not a mountpoint */
  3998. if (!mnt_has_parent(new_mnt))
  3999. goto out4; /* not attached */
  4000. /* make sure we can reach put_old from new_root */
  4001. if (!is_path_reachable(old_mnt, old.dentry, &new))
  4002. goto out4;
  4003. /* make certain new is below the root */
  4004. if (!is_path_reachable(new_mnt, new.dentry, &root))
  4005. goto out4;
  4006. lock_mount_hash();
  4007. umount_mnt(new_mnt);
  4008. root_mp = unhash_mnt(root_mnt); /* we'll need its mountpoint */
  4009. #ifdef CONFIG_KDP_NS
  4010. if (((struct kdp_mount *)root_mnt)->mnt->mnt_flags & MNT_LOCKED) {
  4011. kdp_set_mnt_flags(((struct kdp_mount *)new_mnt)->mnt, MNT_LOCKED);
  4012. kdp_clear_mnt_flags(((struct kdp_mount *)root_mnt)->mnt, MNT_LOCKED);
  4013. }
  4014. #else
  4015. if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
  4016. new_mnt->mnt.mnt_flags |= MNT_LOCKED;
  4017. root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  4018. }
  4019. #endif
  4020. /* mount old root on put_old */
  4021. attach_mnt(root_mnt, old_mnt, old_mp);
  4022. /* mount new_root on / */
  4023. attach_mnt(new_mnt, root_parent, root_mp);
  4024. mnt_add_count(root_parent, -1);
  4025. touch_mnt_namespace(current->nsproxy->mnt_ns);
  4026. /* A moved mount should not expire automatically */
  4027. list_del_init(&new_mnt->mnt_expire);
  4028. put_mountpoint(root_mp);
  4029. unlock_mount_hash();
  4030. chroot_fs_refs(&root, &new);
  4031. error = 0;
  4032. out4:
  4033. unlock_mount(old_mp);
  4034. if (!error)
  4035. mntput_no_expire(ex_parent);
  4036. out3:
  4037. path_put(&root);
  4038. out2:
  4039. path_put(&old);
  4040. out1:
  4041. path_put(&new);
  4042. out0:
  4043. return error;
  4044. }
  4045. static unsigned int recalc_flags(struct mount_kattr *kattr, struct mount *mnt)
  4046. {
  4047. #ifdef CONFIG_KDP_NS
  4048. unsigned int flags = ((struct kdp_mount *)mnt)->mnt->mnt_flags;
  4049. #else
  4050. unsigned int flags = mnt->mnt.mnt_flags;
  4051. #endif
  4052. /* flags to clear */
  4053. flags &= ~kattr->attr_clr;
  4054. /* flags to raise */
  4055. flags |= kattr->attr_set;
  4056. return flags;
  4057. }
  4058. static int can_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
  4059. {
  4060. #ifdef CONFIG_KDP_NS
  4061. struct vfsmount *m = ((struct kdp_mount *)mnt)->mnt;
  4062. #else
  4063. struct vfsmount *m = &mnt->mnt;
  4064. #endif
  4065. struct user_namespace *fs_userns = m->mnt_sb->s_user_ns;
  4066. if (!kattr->mnt_userns)
  4067. return 0;
  4068. /*
  4069. * Creating an idmapped mount with the filesystem wide idmapping
  4070. * doesn't make sense so block that. We don't allow mushy semantics.
  4071. */
  4072. if (kattr->mnt_userns == fs_userns)
  4073. return -EINVAL;
  4074. /*
  4075. * Once a mount has been idmapped we don't allow it to change its
  4076. * mapping. It makes things simpler and callers can just create
  4077. * another bind-mount they can idmap if they want to.
  4078. */
  4079. if (is_idmapped_mnt(m))
  4080. return -EPERM;
  4081. /* The underlying filesystem doesn't support idmapped mounts yet. */
  4082. if (!(m->mnt_sb->s_type->fs_flags & FS_ALLOW_IDMAP))
  4083. return -EINVAL;
  4084. /* We're not controlling the superblock. */
  4085. if (!ns_capable(fs_userns, CAP_SYS_ADMIN))
  4086. return -EPERM;
  4087. /* Mount has already been visible in the filesystem hierarchy. */
  4088. if (!is_anon_ns(mnt->mnt_ns))
  4089. return -EINVAL;
  4090. return 0;
  4091. }
  4092. /**
  4093. * mnt_allow_writers() - check whether the attribute change allows writers
  4094. * @kattr: the new mount attributes
  4095. * @mnt: the mount to which @kattr will be applied
  4096. *
  4097. * Check whether thew new mount attributes in @kattr allow concurrent writers.
  4098. *
  4099. * Return: true if writers need to be held, false if not
  4100. */
  4101. static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
  4102. const struct mount *mnt)
  4103. {
  4104. return (!(kattr->attr_set & MNT_READONLY) ||
  4105. #ifdef CONFIG_KDP_NS
  4106. (((struct kdp_mount *)mnt)->mnt->mnt_flags & MNT_READONLY)) &&
  4107. #else
  4108. (mnt->mnt.mnt_flags & MNT_READONLY)) &&
  4109. #endif
  4110. !kattr->mnt_userns;
  4111. }
  4112. static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
  4113. {
  4114. struct mount *m;
  4115. int err;
  4116. for (m = mnt; m; m = next_mnt(m, mnt)) {
  4117. if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
  4118. err = -EPERM;
  4119. break;
  4120. }
  4121. err = can_idmap_mount(kattr, m);
  4122. if (err)
  4123. break;
  4124. if (!mnt_allow_writers(kattr, m)) {
  4125. err = mnt_hold_writers(m);
  4126. if (err)
  4127. break;
  4128. }
  4129. if (!kattr->recurse)
  4130. return 0;
  4131. }
  4132. if (err) {
  4133. struct mount *p;
  4134. /*
  4135. * If we had to call mnt_hold_writers() MNT_WRITE_HOLD will
  4136. * be set in @mnt_flags. The loop unsets MNT_WRITE_HOLD for all
  4137. * mounts and needs to take care to include the first mount.
  4138. */
  4139. for (p = mnt; p; p = next_mnt(p, mnt)) {
  4140. /* If we had to hold writers unblock them. */
  4141. #ifdef CONFIG_KDP_NS
  4142. if (((struct kdp_mount *)p)->mnt->mnt_flags & MNT_WRITE_HOLD)
  4143. #else
  4144. if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
  4145. #endif
  4146. mnt_unhold_writers(p);
  4147. /*
  4148. * We're done once the first mount we changed got
  4149. * MNT_WRITE_HOLD unset.
  4150. */
  4151. if (p == m)
  4152. break;
  4153. }
  4154. }
  4155. return err;
  4156. }
  4157. static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
  4158. {
  4159. struct user_namespace *mnt_userns, *old_mnt_userns;
  4160. if (!kattr->mnt_userns)
  4161. return;
  4162. /*
  4163. * We're the only ones able to change the mount's idmapping. So
  4164. * mnt->mnt.mnt_userns is stable and we can retrieve it directly.
  4165. */
  4166. #ifdef CONFIG_KDP_NS
  4167. old_mnt_userns = ((struct kdp_mount *)mnt)->mnt->mnt_userns;
  4168. #else
  4169. old_mnt_userns = mnt->mnt.mnt_userns;
  4170. #endif
  4171. mnt_userns = get_user_ns(kattr->mnt_userns);
  4172. /* Pairs with smp_load_acquire() in mnt_user_ns(). */
  4173. #ifdef CONFIG_KDP_NS
  4174. compiletime_assert_atomic_type(((struct kdp_mount *)mnt)->mnt);
  4175. __smp_mb();
  4176. kdp_set_mnt_userns(((struct kdp_mount *)mnt)->mnt, mnt_userns);
  4177. #else
  4178. smp_store_release(&mnt->mnt.mnt_userns, mnt_userns);
  4179. #endif
  4180. /*
  4181. * If this is an idmapped filesystem drop the reference we've taken
  4182. * in vfs_create_mount() before.
  4183. */
  4184. if (!initial_idmapping(old_mnt_userns))
  4185. put_user_ns(old_mnt_userns);
  4186. }
  4187. static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
  4188. {
  4189. struct mount *m;
  4190. for (m = mnt; m; m = next_mnt(m, mnt)) {
  4191. unsigned int flags;
  4192. do_idmap_mount(kattr, m);
  4193. flags = recalc_flags(kattr, m);
  4194. #ifdef CONFIG_KDP_NS
  4195. kdp_assign_mnt_flags(((struct kdp_mount *)m)->mnt, flags);
  4196. #else
  4197. WRITE_ONCE(m->mnt.mnt_flags, flags);
  4198. #endif
  4199. /* If we had to hold writers unblock them. */
  4200. #ifdef CONFIG_KDP_NS
  4201. if (((struct kdp_mount*)m)->mnt->mnt_flags & MNT_WRITE_HOLD)
  4202. #else
  4203. if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
  4204. #endif
  4205. mnt_unhold_writers(m);
  4206. if (kattr->propagation)
  4207. change_mnt_propagation(m, kattr->propagation);
  4208. if (!kattr->recurse)
  4209. break;
  4210. }
  4211. touch_mnt_namespace(mnt->mnt_ns);
  4212. }
  4213. static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
  4214. {
  4215. struct mount *mnt = real_mount(path->mnt);
  4216. int err = 0;
  4217. #ifdef CONFIG_KDP_NS
  4218. if (path->dentry != ((struct kdp_mount *)mnt)->mnt->mnt_root)
  4219. #else
  4220. if (path->dentry != mnt->mnt.mnt_root)
  4221. #endif
  4222. return -EINVAL;
  4223. if (kattr->propagation) {
  4224. /*
  4225. * Only take namespace_lock() if we're actually changing
  4226. * propagation.
  4227. */
  4228. namespace_lock();
  4229. if (kattr->propagation == MS_SHARED) {
  4230. err = invent_group_ids(mnt, kattr->recurse);
  4231. if (err) {
  4232. namespace_unlock();
  4233. return err;
  4234. }
  4235. }
  4236. }
  4237. err = -EINVAL;
  4238. lock_mount_hash();
  4239. /* Ensure that this isn't anything purely vfs internal. */
  4240. if (!is_mounted(&mnt->mnt))
  4241. goto out;
  4242. /*
  4243. * If this is an attached mount make sure it's located in the callers
  4244. * mount namespace. If it's not don't let the caller interact with it.
  4245. * If this is a detached mount make sure it has an anonymous mount
  4246. * namespace attached to it, i.e. we've created it via OPEN_TREE_CLONE.
  4247. */
  4248. if (!(mnt_has_parent(mnt) ? check_mnt(mnt) : is_anon_ns(mnt->mnt_ns)))
  4249. goto out;
  4250. /*
  4251. * First, we get the mount tree in a shape where we can change mount
  4252. * properties without failure. If we succeeded to do so we commit all
  4253. * changes and if we failed we clean up.
  4254. */
  4255. err = mount_setattr_prepare(kattr, mnt);
  4256. if (!err)
  4257. mount_setattr_commit(kattr, mnt);
  4258. out:
  4259. unlock_mount_hash();
  4260. if (kattr->propagation) {
  4261. if (err)
  4262. cleanup_group_ids(mnt, NULL);
  4263. namespace_unlock();
  4264. }
  4265. return err;
  4266. }
  4267. static int build_mount_idmapped(const struct mount_attr *attr, size_t usize,
  4268. struct mount_kattr *kattr, unsigned int flags)
  4269. {
  4270. int err = 0;
  4271. struct ns_common *ns;
  4272. struct user_namespace *mnt_userns;
  4273. struct file *file;
  4274. if (!((attr->attr_set | attr->attr_clr) & MOUNT_ATTR_IDMAP))
  4275. return 0;
  4276. /*
  4277. * We currently do not support clearing an idmapped mount. If this ever
  4278. * is a use-case we can revisit this but for now let's keep it simple
  4279. * and not allow it.
  4280. */
  4281. if (attr->attr_clr & MOUNT_ATTR_IDMAP)
  4282. return -EINVAL;
  4283. if (attr->userns_fd > INT_MAX)
  4284. return -EINVAL;
  4285. file = fget(attr->userns_fd);
  4286. if (!file)
  4287. return -EBADF;
  4288. if (!proc_ns_file(file)) {
  4289. err = -EINVAL;
  4290. goto out_fput;
  4291. }
  4292. ns = get_proc_ns(file_inode(file));
  4293. if (ns->ops->type != CLONE_NEWUSER) {
  4294. err = -EINVAL;
  4295. goto out_fput;
  4296. }
  4297. /*
  4298. * The initial idmapping cannot be used to create an idmapped
  4299. * mount. We use the initial idmapping as an indicator of a mount
  4300. * that is not idmapped. It can simply be passed into helpers that
  4301. * are aware of idmapped mounts as a convenient shortcut. A user
  4302. * can just create a dedicated identity mapping to achieve the same
  4303. * result.
  4304. */
  4305. mnt_userns = container_of(ns, struct user_namespace, ns);
  4306. if (initial_idmapping(mnt_userns)) {
  4307. err = -EPERM;
  4308. goto out_fput;
  4309. }
  4310. /* We're not controlling the target namespace. */
  4311. if (!ns_capable(mnt_userns, CAP_SYS_ADMIN)) {
  4312. err = -EPERM;
  4313. goto out_fput;
  4314. }
  4315. kattr->mnt_userns = get_user_ns(mnt_userns);
  4316. out_fput:
  4317. fput(file);
  4318. return err;
  4319. }
  4320. static int build_mount_kattr(const struct mount_attr *attr, size_t usize,
  4321. struct mount_kattr *kattr, unsigned int flags)
  4322. {
  4323. unsigned int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
  4324. if (flags & AT_NO_AUTOMOUNT)
  4325. lookup_flags &= ~LOOKUP_AUTOMOUNT;
  4326. if (flags & AT_SYMLINK_NOFOLLOW)
  4327. lookup_flags &= ~LOOKUP_FOLLOW;
  4328. if (flags & AT_EMPTY_PATH)
  4329. lookup_flags |= LOOKUP_EMPTY;
  4330. *kattr = (struct mount_kattr) {
  4331. .lookup_flags = lookup_flags,
  4332. .recurse = !!(flags & AT_RECURSIVE),
  4333. };
  4334. if (attr->propagation & ~MOUNT_SETATTR_PROPAGATION_FLAGS)
  4335. return -EINVAL;
  4336. if (hweight32(attr->propagation & MOUNT_SETATTR_PROPAGATION_FLAGS) > 1)
  4337. return -EINVAL;
  4338. kattr->propagation = attr->propagation;
  4339. if ((attr->attr_set | attr->attr_clr) & ~MOUNT_SETATTR_VALID_FLAGS)
  4340. return -EINVAL;
  4341. kattr->attr_set = attr_flags_to_mnt_flags(attr->attr_set);
  4342. kattr->attr_clr = attr_flags_to_mnt_flags(attr->attr_clr);
  4343. /*
  4344. * Since the MOUNT_ATTR_<atime> values are an enum, not a bitmap,
  4345. * users wanting to transition to a different atime setting cannot
  4346. * simply specify the atime setting in @attr_set, but must also
  4347. * specify MOUNT_ATTR__ATIME in the @attr_clr field.
  4348. * So ensure that MOUNT_ATTR__ATIME can't be partially set in
  4349. * @attr_clr and that @attr_set can't have any atime bits set if
  4350. * MOUNT_ATTR__ATIME isn't set in @attr_clr.
  4351. */
  4352. if (attr->attr_clr & MOUNT_ATTR__ATIME) {
  4353. if ((attr->attr_clr & MOUNT_ATTR__ATIME) != MOUNT_ATTR__ATIME)
  4354. return -EINVAL;
  4355. /*
  4356. * Clear all previous time settings as they are mutually
  4357. * exclusive.
  4358. */
  4359. kattr->attr_clr |= MNT_RELATIME | MNT_NOATIME;
  4360. switch (attr->attr_set & MOUNT_ATTR__ATIME) {
  4361. case MOUNT_ATTR_RELATIME:
  4362. kattr->attr_set |= MNT_RELATIME;
  4363. break;
  4364. case MOUNT_ATTR_NOATIME:
  4365. kattr->attr_set |= MNT_NOATIME;
  4366. break;
  4367. case MOUNT_ATTR_STRICTATIME:
  4368. break;
  4369. default:
  4370. return -EINVAL;
  4371. }
  4372. } else {
  4373. if (attr->attr_set & MOUNT_ATTR__ATIME)
  4374. return -EINVAL;
  4375. }
  4376. return build_mount_idmapped(attr, usize, kattr, flags);
  4377. }
  4378. static void finish_mount_kattr(struct mount_kattr *kattr)
  4379. {
  4380. put_user_ns(kattr->mnt_userns);
  4381. kattr->mnt_userns = NULL;
  4382. }
  4383. SYSCALL_DEFINE5(mount_setattr, int, dfd, const char __user *, path,
  4384. unsigned int, flags, struct mount_attr __user *, uattr,
  4385. size_t, usize)
  4386. {
  4387. int err;
  4388. struct path target;
  4389. struct mount_attr attr;
  4390. struct mount_kattr kattr;
  4391. BUILD_BUG_ON(sizeof(struct mount_attr) != MOUNT_ATTR_SIZE_VER0);
  4392. if (flags & ~(AT_EMPTY_PATH |
  4393. AT_RECURSIVE |
  4394. AT_SYMLINK_NOFOLLOW |
  4395. AT_NO_AUTOMOUNT))
  4396. return -EINVAL;
  4397. if (unlikely(usize > PAGE_SIZE))
  4398. return -E2BIG;
  4399. if (unlikely(usize < MOUNT_ATTR_SIZE_VER0))
  4400. return -EINVAL;
  4401. if (!may_mount())
  4402. return -EPERM;
  4403. err = copy_struct_from_user(&attr, sizeof(attr), uattr, usize);
  4404. if (err)
  4405. return err;
  4406. /* Don't bother walking through the mounts if this is a nop. */
  4407. if (attr.attr_set == 0 &&
  4408. attr.attr_clr == 0 &&
  4409. attr.propagation == 0)
  4410. return 0;
  4411. err = build_mount_kattr(&attr, usize, &kattr, flags);
  4412. if (err)
  4413. return err;
  4414. err = user_path_at(dfd, path, kattr.lookup_flags, &target);
  4415. if (!err) {
  4416. err = do_mount_setattr(&target, &kattr);
  4417. path_put(&target);
  4418. }
  4419. finish_mount_kattr(&kattr);
  4420. return err;
  4421. }
  4422. static void __init init_mount_tree(void)
  4423. {
  4424. struct vfsmount *mnt;
  4425. struct mount *m;
  4426. struct mnt_namespace *ns;
  4427. struct path root;
  4428. mnt = vfs_kern_mount(&rootfs_fs_type, 0, "rootfs", NULL);
  4429. if (IS_ERR(mnt))
  4430. panic("Can't create rootfs");
  4431. ns = alloc_mnt_ns(&init_user_ns, false);
  4432. if (IS_ERR(ns))
  4433. panic("Can't allocate initial namespace");
  4434. m = real_mount(mnt);
  4435. m->mnt_ns = ns;
  4436. ns->root = m;
  4437. ns->mounts = 1;
  4438. list_add(&m->mnt_list, &ns->list);
  4439. init_task.nsproxy->mnt_ns = ns;
  4440. get_mnt_ns(ns);
  4441. root.mnt = mnt;
  4442. root.dentry = mnt->mnt_root;
  4443. #ifdef CONFIG_KDP_NS
  4444. kdp_set_mnt_flags(mnt, MNT_LOCKED);
  4445. #else
  4446. mnt->mnt_flags |= MNT_LOCKED;
  4447. #endif
  4448. set_fs_pwd(current->fs, &root);
  4449. set_fs_root(current->fs, &root);
  4450. }
  4451. void __init mnt_init(void)
  4452. {
  4453. int err;
  4454. #ifdef CONFIG_KDP_NS
  4455. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct kdp_mount),
  4456. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
  4457. kdp_mnt_init();
  4458. #else
  4459. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  4460. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
  4461. #endif
  4462. mount_hashtable = alloc_large_system_hash("Mount-cache",
  4463. sizeof(struct hlist_head),
  4464. mhash_entries, 19,
  4465. HASH_ZERO,
  4466. &m_hash_shift, &m_hash_mask, 0, 0);
  4467. mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
  4468. sizeof(struct hlist_head),
  4469. mphash_entries, 19,
  4470. HASH_ZERO,
  4471. &mp_hash_shift, &mp_hash_mask, 0, 0);
  4472. if (!mount_hashtable || !mountpoint_hashtable)
  4473. panic("Failed to allocate mount hash table\n");
  4474. kernfs_init();
  4475. err = sysfs_init();
  4476. if (err)
  4477. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  4478. __func__, err);
  4479. fs_kobj = kobject_create_and_add("fs", NULL);
  4480. if (!fs_kobj)
  4481. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  4482. shmem_init();
  4483. init_rootfs();
  4484. init_mount_tree();
  4485. }
  4486. void put_mnt_ns(struct mnt_namespace *ns)
  4487. {
  4488. if (!refcount_dec_and_test(&ns->ns.count))
  4489. return;
  4490. #ifdef CONFIG_KDP_NS
  4491. drop_collected_mounts(((struct kdp_mount *)ns->root)->mnt);
  4492. #else
  4493. drop_collected_mounts(&ns->root->mnt);
  4494. #endif
  4495. free_mnt_ns(ns);
  4496. }
  4497. struct vfsmount *kern_mount(struct file_system_type *type)
  4498. {
  4499. struct vfsmount *mnt;
  4500. mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
  4501. if (!IS_ERR(mnt)) {
  4502. /*
  4503. * it is a longterm mount, don't release mnt until
  4504. * we unmount before file sys is unregistered
  4505. */
  4506. real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
  4507. }
  4508. return mnt;
  4509. }
  4510. EXPORT_SYMBOL_GPL(kern_mount);
  4511. void kern_unmount(struct vfsmount *mnt)
  4512. {
  4513. /* release long term mount so mount point can be released */
  4514. if (!IS_ERR_OR_NULL(mnt)) {
  4515. real_mount(mnt)->mnt_ns = NULL;
  4516. synchronize_rcu(); /* yecchhh... */
  4517. mntput(mnt);
  4518. }
  4519. }
  4520. EXPORT_SYMBOL(kern_unmount);
  4521. void kern_unmount_array(struct vfsmount *mnt[], unsigned int num)
  4522. {
  4523. unsigned int i;
  4524. for (i = 0; i < num; i++)
  4525. if (mnt[i])
  4526. real_mount(mnt[i])->mnt_ns = NULL;
  4527. synchronize_rcu_expedited();
  4528. for (i = 0; i < num; i++)
  4529. mntput(mnt[i]);
  4530. }
  4531. EXPORT_SYMBOL(kern_unmount_array);
  4532. bool our_mnt(struct vfsmount *mnt)
  4533. {
  4534. return check_mnt(real_mount(mnt));
  4535. }
  4536. bool current_chrooted(void)
  4537. {
  4538. /* Does the current process have a non-standard root */
  4539. struct path ns_root;
  4540. struct path fs_root;
  4541. bool chrooted;
  4542. /* Find the namespace root */
  4543. #ifdef CONFIG_KDP_NS
  4544. ns_root.mnt = ((struct kdp_mount *)current->nsproxy->mnt_ns->root)->mnt;
  4545. #else
  4546. ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
  4547. #endif
  4548. ns_root.dentry = ns_root.mnt->mnt_root;
  4549. path_get(&ns_root);
  4550. while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
  4551. ;
  4552. get_fs_root(current->fs, &fs_root);
  4553. chrooted = !path_equal(&fs_root, &ns_root);
  4554. path_put(&fs_root);
  4555. path_put(&ns_root);
  4556. return chrooted;
  4557. }
  4558. static bool mnt_already_visible(struct mnt_namespace *ns,
  4559. const struct super_block *sb,
  4560. int *new_mnt_flags)
  4561. {
  4562. int new_flags = *new_mnt_flags;
  4563. struct mount *mnt;
  4564. bool visible = false;
  4565. down_read(&namespace_sem);
  4566. lock_ns_list(ns);
  4567. list_for_each_entry(mnt, &ns->list, mnt_list) {
  4568. struct mount *child;
  4569. int mnt_flags;
  4570. if (mnt_is_cursor(mnt))
  4571. continue;
  4572. #ifdef CONFIG_KDP_NS
  4573. if (((struct kdp_mount *)mnt)->mnt->mnt_sb->s_type != sb->s_type)
  4574. continue;
  4575. /* This mount is not fully visible if it's root directory
  4576. * is not the root directory of the filesystem.
  4577. */
  4578. if (((struct kdp_mount *)mnt)->mnt->mnt_root != ((struct kdp_mount *)mnt)->mnt->mnt_sb->s_root)
  4579. continue;
  4580. /* A local view of the mount flags */
  4581. mnt_flags = ((struct kdp_mount *)mnt)->mnt->mnt_flags;
  4582. /* Don't miss readonly hidden in the superblock flags */
  4583. if (sb_rdonly(((struct kdp_mount *)mnt)->mnt->mnt_sb))
  4584. mnt_flags |= MNT_LOCK_READONLY;
  4585. #else
  4586. if (mnt->mnt.mnt_sb->s_type != sb->s_type)
  4587. continue;
  4588. /* This mount is not fully visible if it's root directory
  4589. * is not the root directory of the filesystem.
  4590. */
  4591. if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
  4592. continue;
  4593. /* A local view of the mount flags */
  4594. mnt_flags = mnt->mnt.mnt_flags;
  4595. /* Don't miss readonly hidden in the superblock flags */
  4596. if (sb_rdonly(mnt->mnt.mnt_sb))
  4597. mnt_flags |= MNT_LOCK_READONLY;
  4598. #endif
  4599. /* Verify the mount flags are equal to or more permissive
  4600. * than the proposed new mount.
  4601. */
  4602. if ((mnt_flags & MNT_LOCK_READONLY) &&
  4603. !(new_flags & MNT_READONLY))
  4604. continue;
  4605. if ((mnt_flags & MNT_LOCK_ATIME) &&
  4606. ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
  4607. continue;
  4608. /* This mount is not fully visible if there are any
  4609. * locked child mounts that cover anything except for
  4610. * empty directories.
  4611. */
  4612. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  4613. struct inode *inode = child->mnt_mountpoint->d_inode;
  4614. /* Only worry about locked mounts */
  4615. #ifdef CONFIG_KDP_NS
  4616. if (!(((struct kdp_mount *)child)->mnt->mnt_flags & MNT_LOCKED))
  4617. continue;
  4618. #else
  4619. if (!(child->mnt.mnt_flags & MNT_LOCKED))
  4620. continue;
  4621. #endif
  4622. /* Is the directory permanetly empty? */
  4623. if (!is_empty_dir_inode(inode))
  4624. goto next;
  4625. }
  4626. /* Preserve the locked attributes */
  4627. *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
  4628. MNT_LOCK_ATIME);
  4629. visible = true;
  4630. goto found;
  4631. next: ;
  4632. }
  4633. found:
  4634. unlock_ns_list(ns);
  4635. up_read(&namespace_sem);
  4636. return visible;
  4637. }
  4638. static bool mount_too_revealing(const struct super_block *sb, int *new_mnt_flags)
  4639. {
  4640. const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
  4641. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  4642. unsigned long s_iflags;
  4643. if (ns->user_ns == &init_user_ns)
  4644. return false;
  4645. /* Can this filesystem be too revealing? */
  4646. s_iflags = sb->s_iflags;
  4647. if (!(s_iflags & SB_I_USERNS_VISIBLE))
  4648. return false;
  4649. if ((s_iflags & required_iflags) != required_iflags) {
  4650. WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
  4651. required_iflags);
  4652. return true;
  4653. }
  4654. return !mnt_already_visible(ns, sb, new_mnt_flags);
  4655. }
  4656. bool mnt_may_suid(struct vfsmount *mnt)
  4657. {
  4658. /*
  4659. * Foreign mounts (accessed via fchdir or through /proc
  4660. * symlinks) are always treated as if they are nosuid. This
  4661. * prevents namespaces from trusting potentially unsafe
  4662. * suid/sgid bits, file caps, or security labels that originate
  4663. * in other namespaces.
  4664. */
  4665. return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
  4666. current_in_userns(mnt->mnt_sb->s_user_ns);
  4667. }
  4668. static struct ns_common *mntns_get(struct task_struct *task)
  4669. {
  4670. struct ns_common *ns = NULL;
  4671. struct nsproxy *nsproxy;
  4672. task_lock(task);
  4673. nsproxy = task->nsproxy;
  4674. if (nsproxy) {
  4675. ns = &nsproxy->mnt_ns->ns;
  4676. get_mnt_ns(to_mnt_ns(ns));
  4677. }
  4678. task_unlock(task);
  4679. return ns;
  4680. }
  4681. static void mntns_put(struct ns_common *ns)
  4682. {
  4683. put_mnt_ns(to_mnt_ns(ns));
  4684. }
  4685. static int mntns_install(struct nsset *nsset, struct ns_common *ns)
  4686. {
  4687. struct nsproxy *nsproxy = nsset->nsproxy;
  4688. struct fs_struct *fs = nsset->fs;
  4689. struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
  4690. struct user_namespace *user_ns = nsset->cred->user_ns;
  4691. struct path root;
  4692. int err;
  4693. if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
  4694. !ns_capable(user_ns, CAP_SYS_CHROOT) ||
  4695. !ns_capable(user_ns, CAP_SYS_ADMIN))
  4696. return -EPERM;
  4697. if (is_anon_ns(mnt_ns))
  4698. return -EINVAL;
  4699. if (fs->users != 1)
  4700. return -EINVAL;
  4701. get_mnt_ns(mnt_ns);
  4702. old_mnt_ns = nsproxy->mnt_ns;
  4703. nsproxy->mnt_ns = mnt_ns;
  4704. /* Find the root */
  4705. #ifdef CONFIG_KDP_NS
  4706. err = vfs_path_lookup(((struct kdp_mount *)mnt_ns->root)->mnt->mnt_root, ((struct kdp_mount *)mnt_ns->root)->mnt,
  4707. "/", LOOKUP_DOWN, &root);
  4708. #else
  4709. err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
  4710. "/", LOOKUP_DOWN, &root);
  4711. #endif
  4712. if (err) {
  4713. /* revert to old namespace */
  4714. nsproxy->mnt_ns = old_mnt_ns;
  4715. put_mnt_ns(mnt_ns);
  4716. return err;
  4717. }
  4718. put_mnt_ns(old_mnt_ns);
  4719. /* Update the pwd and root */
  4720. set_fs_pwd(fs, &root);
  4721. set_fs_root(fs, &root);
  4722. path_put(&root);
  4723. return 0;
  4724. }
  4725. static struct user_namespace *mntns_owner(struct ns_common *ns)
  4726. {
  4727. return to_mnt_ns(ns)->user_ns;
  4728. }
  4729. const struct proc_ns_operations mntns_operations = {
  4730. .name = "mnt",
  4731. .type = CLONE_NEWNS,
  4732. .get = mntns_get,
  4733. .put = mntns_put,
  4734. .install = mntns_install,
  4735. .owner = mntns_owner,
  4736. };
  4737. #ifdef CONFIG_SYSCTL
  4738. static struct ctl_table fs_namespace_sysctls[] = {
  4739. {
  4740. .procname = "mount-max",
  4741. .data = &sysctl_mount_max,
  4742. .maxlen = sizeof(unsigned int),
  4743. .mode = 0644,
  4744. .proc_handler = proc_dointvec_minmax,
  4745. .extra1 = SYSCTL_ONE,
  4746. },
  4747. { }
  4748. };
  4749. static int __init init_fs_namespace_sysctls(void)
  4750. {
  4751. register_sysctl_init("fs", fs_namespace_sysctls);
  4752. return 0;
  4753. }
  4754. fs_initcall(init_fs_namespace_sysctls);
  4755. #endif /* CONFIG_SYSCTL */
  4756. #ifdef CONFIG_KSU_SUSFS_TRY_UMOUNT
  4757. extern void susfs_try_umount_all(uid_t uid);
  4758. void susfs_run_try_umount_for_current_mnt_ns(void) {
  4759. struct mount *mnt;
  4760. struct mnt_namespace *mnt_ns;
  4761. mnt_ns = current->nsproxy->mnt_ns;
  4762. // Lock the namespace
  4763. namespace_lock();
  4764. list_for_each_entry(mnt, &mnt_ns->list, mnt_list) {
  4765. // Change the sus mount to be private
  4766. if (mnt->mnt_id >= DEFAULT_SUS_MNT_ID) {
  4767. change_mnt_propagation(mnt, MS_PRIVATE);
  4768. }
  4769. }
  4770. // Unlock the namespace
  4771. namespace_unlock();
  4772. susfs_try_umount_all(current_uid().val);
  4773. }
  4774. #endif
  4775. #ifdef CONFIG_KSU_SUSFS
  4776. bool susfs_is_mnt_devname_ksu(struct path *path) {
  4777. struct mount *mnt;
  4778. if (path && path->mnt) {
  4779. mnt = real_mount(path->mnt);
  4780. if (mnt && mnt->mnt_devname && !strcmp(mnt->mnt_devname, "KSU")) {
  4781. return true;
  4782. }
  4783. }
  4784. return false;
  4785. }
  4786. #endif