namei.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/ima.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/device_cgroup.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/posix_acl.h>
  34. #include <linux/hash.h>
  35. #include <linux/bitops.h>
  36. #include <linux/init_task.h>
  37. #include <linux/uaccess.h>
  38. #include "internal.h"
  39. #include "mount.h"
  40. /* [Feb-1997 T. Schoebel-Theuer]
  41. * Fundamental changes in the pathname lookup mechanisms (namei)
  42. * were necessary because of omirr. The reason is that omirr needs
  43. * to know the _real_ pathname, not the user-supplied one, in case
  44. * of symlinks (and also when transname replacements occur).
  45. *
  46. * The new code replaces the old recursive symlink resolution with
  47. * an iterative one (in case of non-nested symlink chains). It does
  48. * this with calls to <fs>_follow_link().
  49. * As a side effect, dir_namei(), _namei() and follow_link() are now
  50. * replaced with a single function lookup_dentry() that can handle all
  51. * the special cases of the former code.
  52. *
  53. * With the new dcache, the pathname is stored at each inode, at least as
  54. * long as the refcount of the inode is positive. As a side effect, the
  55. * size of the dcache depends on the inode cache and thus is dynamic.
  56. *
  57. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  58. * resolution to correspond with current state of the code.
  59. *
  60. * Note that the symlink resolution is not *completely* iterative.
  61. * There is still a significant amount of tail- and mid- recursion in
  62. * the algorithm. Also, note that <fs>_readlink() is not used in
  63. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  64. * may return different results than <fs>_follow_link(). Many virtual
  65. * filesystems (including /proc) exhibit this behavior.
  66. */
  67. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  68. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  69. * and the name already exists in form of a symlink, try to create the new
  70. * name indicated by the symlink. The old code always complained that the
  71. * name already exists, due to not following the symlink even if its target
  72. * is nonexistent. The new semantics affects also mknod() and link() when
  73. * the name is a symlink pointing to a non-existent name.
  74. *
  75. * I don't know which semantics is the right one, since I have no access
  76. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  77. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  78. * "old" one. Personally, I think the new semantics is much more logical.
  79. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  80. * file does succeed in both HP-UX and SunOs, but not in Solaris
  81. * and in the old Linux semantics.
  82. */
  83. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  84. * semantics. See the comments in "open_namei" and "do_link" below.
  85. *
  86. * [10-Sep-98 Alan Modra] Another symlink change.
  87. */
  88. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  89. * inside the path - always follow.
  90. * in the last component in creation/removal/renaming - never follow.
  91. * if LOOKUP_FOLLOW passed - follow.
  92. * if the pathname has trailing slashes - follow.
  93. * otherwise - don't follow.
  94. * (applied in that order).
  95. *
  96. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  97. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  98. * During the 2.4 we need to fix the userland stuff depending on it -
  99. * hopefully we will be able to get rid of that wart in 2.5. So far only
  100. * XEmacs seems to be relying on it...
  101. */
  102. /*
  103. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  104. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  105. * any extra contention...
  106. */
  107. /* In order to reduce some races, while at the same time doing additional
  108. * checking and hopefully speeding things up, we copy filenames to the
  109. * kernel data space before using them..
  110. *
  111. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  112. * PATH_MAX includes the nul terminator --RR.
  113. */
  114. #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
  115. struct filename *
  116. getname_flags(const char __user *filename, int flags, int *empty)
  117. {
  118. struct filename *result;
  119. char *kname;
  120. int len;
  121. result = audit_reusename(filename);
  122. if (result)
  123. return result;
  124. result = __getname();
  125. if (unlikely(!result))
  126. return ERR_PTR(-ENOMEM);
  127. /*
  128. * First, try to embed the struct filename inside the names_cache
  129. * allocation
  130. */
  131. kname = (char *)result->iname;
  132. result->name = kname;
  133. len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
  134. if (unlikely(len < 0)) {
  135. __putname(result);
  136. return ERR_PTR(len);
  137. }
  138. /*
  139. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  140. * separate struct filename so we can dedicate the entire
  141. * names_cache allocation for the pathname, and re-do the copy from
  142. * userland.
  143. */
  144. if (unlikely(len == EMBEDDED_NAME_MAX)) {
  145. const size_t size = offsetof(struct filename, iname[1]);
  146. kname = (char *)result;
  147. /*
  148. * size is chosen that way we to guarantee that
  149. * result->iname[0] is within the same object and that
  150. * kname can't be equal to result->iname, no matter what.
  151. */
  152. result = kzalloc(size, GFP_KERNEL);
  153. if (unlikely(!result)) {
  154. __putname(kname);
  155. return ERR_PTR(-ENOMEM);
  156. }
  157. result->name = kname;
  158. len = strncpy_from_user(kname, filename, PATH_MAX);
  159. if (unlikely(len < 0)) {
  160. __putname(kname);
  161. kfree(result);
  162. return ERR_PTR(len);
  163. }
  164. if (unlikely(len == PATH_MAX)) {
  165. __putname(kname);
  166. kfree(result);
  167. return ERR_PTR(-ENAMETOOLONG);
  168. }
  169. }
  170. result->refcnt = 1;
  171. /* The empty path is special. */
  172. if (unlikely(!len)) {
  173. if (empty)
  174. *empty = 1;
  175. if (!(flags & LOOKUP_EMPTY)) {
  176. putname(result);
  177. return ERR_PTR(-ENOENT);
  178. }
  179. }
  180. result->uptr = filename;
  181. result->aname = NULL;
  182. audit_getname(result);
  183. return result;
  184. }
  185. struct filename *
  186. getname(const char __user * filename)
  187. {
  188. return getname_flags(filename, 0, NULL);
  189. }
  190. struct filename *
  191. getname_kernel(const char * filename)
  192. {
  193. struct filename *result;
  194. int len = strlen(filename) + 1;
  195. result = __getname();
  196. if (unlikely(!result))
  197. return ERR_PTR(-ENOMEM);
  198. if (len <= EMBEDDED_NAME_MAX) {
  199. result->name = (char *)result->iname;
  200. } else if (len <= PATH_MAX) {
  201. struct filename *tmp;
  202. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  203. if (unlikely(!tmp)) {
  204. __putname(result);
  205. return ERR_PTR(-ENOMEM);
  206. }
  207. tmp->name = (char *)result;
  208. result = tmp;
  209. } else {
  210. __putname(result);
  211. return ERR_PTR(-ENAMETOOLONG);
  212. }
  213. memcpy((char *)result->name, filename, len);
  214. result->uptr = NULL;
  215. result->aname = NULL;
  216. result->refcnt = 1;
  217. audit_getname(result);
  218. return result;
  219. }
  220. void putname(struct filename *name)
  221. {
  222. BUG_ON(name->refcnt <= 0);
  223. if (--name->refcnt > 0)
  224. return;
  225. if (name->name != name->iname) {
  226. __putname(name->name);
  227. kfree(name);
  228. } else
  229. __putname(name);
  230. }
  231. static int check_acl(struct inode *inode, int mask)
  232. {
  233. #ifdef CONFIG_FS_POSIX_ACL
  234. struct posix_acl *acl;
  235. if (mask & MAY_NOT_BLOCK) {
  236. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  237. if (!acl)
  238. return -EAGAIN;
  239. /* no ->get_acl() calls in RCU mode... */
  240. if (is_uncached_acl(acl))
  241. return -ECHILD;
  242. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  243. }
  244. acl = get_acl(inode, ACL_TYPE_ACCESS);
  245. if (IS_ERR(acl))
  246. return PTR_ERR(acl);
  247. if (acl) {
  248. int error = posix_acl_permission(inode, acl, mask);
  249. posix_acl_release(acl);
  250. return error;
  251. }
  252. #endif
  253. return -EAGAIN;
  254. }
  255. /*
  256. * This does the basic permission checking
  257. */
  258. static int acl_permission_check(struct inode *inode, int mask)
  259. {
  260. unsigned int mode = inode->i_mode;
  261. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  262. mode >>= 6;
  263. else {
  264. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  265. int error = check_acl(inode, mask);
  266. if (error != -EAGAIN)
  267. return error;
  268. }
  269. if (in_group_p(inode->i_gid))
  270. mode >>= 3;
  271. }
  272. /*
  273. * If the DACs are ok we don't need any capability check.
  274. */
  275. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  276. return 0;
  277. return -EACCES;
  278. }
  279. /**
  280. * generic_permission - check for access rights on a Posix-like filesystem
  281. * @inode: inode to check access rights for
  282. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  283. *
  284. * Used to check for read/write/execute permissions on a file.
  285. * We use "fsuid" for this, letting us set arbitrary permissions
  286. * for filesystem access without changing the "normal" uids which
  287. * are used for other things.
  288. *
  289. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  290. * request cannot be satisfied (eg. requires blocking or too much complexity).
  291. * It would then be called again in ref-walk mode.
  292. */
  293. int generic_permission(struct inode *inode, int mask)
  294. {
  295. int ret;
  296. /*
  297. * Do the basic permission checks.
  298. */
  299. ret = acl_permission_check(inode, mask);
  300. if (ret != -EACCES)
  301. return ret;
  302. if (S_ISDIR(inode->i_mode)) {
  303. /* DACs are overridable for directories */
  304. if (!(mask & MAY_WRITE))
  305. if (capable_wrt_inode_uidgid(inode,
  306. CAP_DAC_READ_SEARCH))
  307. return 0;
  308. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  309. return 0;
  310. return -EACCES;
  311. }
  312. /*
  313. * Searching includes executable on directories, else just read.
  314. */
  315. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  316. if (mask == MAY_READ)
  317. if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
  318. return 0;
  319. /*
  320. * Read/write DACs are always overridable.
  321. * Executable DACs are overridable when there is
  322. * at least one exec bit set.
  323. */
  324. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  325. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  326. return 0;
  327. return -EACCES;
  328. }
  329. EXPORT_SYMBOL(generic_permission);
  330. /*
  331. * We _really_ want to just do "generic_permission()" without
  332. * even looking at the inode->i_op values. So we keep a cache
  333. * flag in inode->i_opflags, that says "this has not special
  334. * permission function, use the fast case".
  335. */
  336. static inline int do_inode_permission(struct inode *inode, int mask)
  337. {
  338. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  339. if (likely(inode->i_op->permission))
  340. return inode->i_op->permission(inode, mask);
  341. /* This gets set once for the inode lifetime */
  342. spin_lock(&inode->i_lock);
  343. inode->i_opflags |= IOP_FASTPERM;
  344. spin_unlock(&inode->i_lock);
  345. }
  346. return generic_permission(inode, mask);
  347. }
  348. /**
  349. * __inode_permission - Check for access rights to a given inode
  350. * @inode: Inode to check permission on
  351. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  352. *
  353. * Check for read/write/execute permissions on an inode.
  354. *
  355. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  356. *
  357. * This does not check for a read-only file system. You probably want
  358. * inode_permission().
  359. */
  360. int __inode_permission(struct inode *inode, int mask)
  361. {
  362. int retval;
  363. if (unlikely(mask & MAY_WRITE)) {
  364. /*
  365. * Nobody gets write access to an immutable file.
  366. */
  367. if (IS_IMMUTABLE(inode))
  368. return -EPERM;
  369. /*
  370. * Updating mtime will likely cause i_uid and i_gid to be
  371. * written back improperly if their true value is unknown
  372. * to the vfs.
  373. */
  374. if (HAS_UNMAPPED_ID(inode))
  375. return -EACCES;
  376. }
  377. retval = do_inode_permission(inode, mask);
  378. if (retval)
  379. return retval;
  380. retval = devcgroup_inode_permission(inode, mask);
  381. if (retval)
  382. return retval;
  383. return security_inode_permission(inode, mask);
  384. }
  385. EXPORT_SYMBOL(__inode_permission);
  386. /**
  387. * sb_permission - Check superblock-level permissions
  388. * @sb: Superblock of inode to check permission on
  389. * @inode: Inode to check permission on
  390. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  391. *
  392. * Separate out file-system wide checks from inode-specific permission checks.
  393. */
  394. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  395. {
  396. if (unlikely(mask & MAY_WRITE)) {
  397. umode_t mode = inode->i_mode;
  398. /* Nobody gets write access to a read-only fs. */
  399. if ((sb->s_flags & MS_RDONLY) &&
  400. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  401. return -EROFS;
  402. }
  403. return 0;
  404. }
  405. /**
  406. * inode_permission - Check for access rights to a given inode
  407. * @inode: Inode to check permission on
  408. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  409. *
  410. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  411. * this, letting us set arbitrary permissions for filesystem access without
  412. * changing the "normal" UIDs which are used for other things.
  413. *
  414. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  415. */
  416. int inode_permission(struct inode *inode, int mask)
  417. {
  418. int retval;
  419. retval = sb_permission(inode->i_sb, inode, mask);
  420. if (retval)
  421. return retval;
  422. return __inode_permission(inode, mask);
  423. }
  424. EXPORT_SYMBOL(inode_permission);
  425. /**
  426. * path_get - get a reference to a path
  427. * @path: path to get the reference to
  428. *
  429. * Given a path increment the reference count to the dentry and the vfsmount.
  430. */
  431. void path_get(const struct path *path)
  432. {
  433. mntget(path->mnt);
  434. dget(path->dentry);
  435. }
  436. EXPORT_SYMBOL(path_get);
  437. /**
  438. * path_put - put a reference to a path
  439. * @path: path to put the reference to
  440. *
  441. * Given a path decrement the reference count to the dentry and the vfsmount.
  442. */
  443. void path_put(const struct path *path)
  444. {
  445. dput(path->dentry);
  446. mntput(path->mnt);
  447. }
  448. EXPORT_SYMBOL(path_put);
  449. #define EMBEDDED_LEVELS 2
  450. struct nameidata {
  451. struct path path;
  452. struct qstr last;
  453. struct path root;
  454. struct inode *inode; /* path.dentry.d_inode */
  455. unsigned int flags;
  456. unsigned seq, m_seq;
  457. int last_type;
  458. unsigned depth;
  459. int total_link_count;
  460. struct saved {
  461. struct path link;
  462. struct delayed_call done;
  463. const char *name;
  464. unsigned seq;
  465. } *stack, internal[EMBEDDED_LEVELS];
  466. struct filename *name;
  467. struct nameidata *saved;
  468. struct inode *link_inode;
  469. unsigned root_seq;
  470. int dfd;
  471. };
  472. static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
  473. {
  474. struct nameidata *old = current->nameidata;
  475. p->stack = p->internal;
  476. p->dfd = dfd;
  477. p->name = name;
  478. p->total_link_count = old ? old->total_link_count : 0;
  479. p->saved = old;
  480. current->nameidata = p;
  481. }
  482. static void restore_nameidata(void)
  483. {
  484. struct nameidata *now = current->nameidata, *old = now->saved;
  485. current->nameidata = old;
  486. if (old)
  487. old->total_link_count = now->total_link_count;
  488. if (now->stack != now->internal)
  489. kfree(now->stack);
  490. }
  491. static int __nd_alloc_stack(struct nameidata *nd)
  492. {
  493. struct saved *p;
  494. if (nd->flags & LOOKUP_RCU) {
  495. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  496. GFP_ATOMIC);
  497. if (unlikely(!p))
  498. return -ECHILD;
  499. } else {
  500. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  501. GFP_KERNEL);
  502. if (unlikely(!p))
  503. return -ENOMEM;
  504. }
  505. memcpy(p, nd->internal, sizeof(nd->internal));
  506. nd->stack = p;
  507. return 0;
  508. }
  509. /**
  510. * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
  511. * @path: nameidate to verify
  512. *
  513. * Rename can sometimes move a file or directory outside of a bind
  514. * mount, path_connected allows those cases to be detected.
  515. */
  516. static bool path_connected(const struct path *path)
  517. {
  518. struct vfsmount *mnt = path->mnt;
  519. /* Only bind mounts can have disconnected paths */
  520. if (mnt->mnt_root == mnt->mnt_sb->s_root)
  521. return true;
  522. return is_subdir(path->dentry, mnt->mnt_root);
  523. }
  524. static inline int nd_alloc_stack(struct nameidata *nd)
  525. {
  526. if (likely(nd->depth != EMBEDDED_LEVELS))
  527. return 0;
  528. if (likely(nd->stack != nd->internal))
  529. return 0;
  530. return __nd_alloc_stack(nd);
  531. }
  532. static void drop_links(struct nameidata *nd)
  533. {
  534. int i = nd->depth;
  535. while (i--) {
  536. struct saved *last = nd->stack + i;
  537. do_delayed_call(&last->done);
  538. clear_delayed_call(&last->done);
  539. }
  540. }
  541. static void terminate_walk(struct nameidata *nd)
  542. {
  543. drop_links(nd);
  544. if (!(nd->flags & LOOKUP_RCU)) {
  545. int i;
  546. path_put(&nd->path);
  547. for (i = 0; i < nd->depth; i++)
  548. path_put(&nd->stack[i].link);
  549. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  550. path_put(&nd->root);
  551. nd->root.mnt = NULL;
  552. }
  553. } else {
  554. nd->flags &= ~LOOKUP_RCU;
  555. if (!(nd->flags & LOOKUP_ROOT))
  556. nd->root.mnt = NULL;
  557. rcu_read_unlock();
  558. }
  559. nd->depth = 0;
  560. }
  561. /* path_put is needed afterwards regardless of success or failure */
  562. static bool legitimize_path(struct nameidata *nd,
  563. struct path *path, unsigned seq)
  564. {
  565. int res = __legitimize_mnt(path->mnt, nd->m_seq);
  566. if (unlikely(res)) {
  567. if (res > 0)
  568. path->mnt = NULL;
  569. path->dentry = NULL;
  570. return false;
  571. }
  572. if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
  573. path->dentry = NULL;
  574. return false;
  575. }
  576. return !read_seqcount_retry(&path->dentry->d_seq, seq);
  577. }
  578. static bool legitimize_links(struct nameidata *nd)
  579. {
  580. int i;
  581. for (i = 0; i < nd->depth; i++) {
  582. struct saved *last = nd->stack + i;
  583. if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
  584. drop_links(nd);
  585. nd->depth = i + 1;
  586. return false;
  587. }
  588. }
  589. return true;
  590. }
  591. /*
  592. * Path walking has 2 modes, rcu-walk and ref-walk (see
  593. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  594. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  595. * normal reference counts on dentries and vfsmounts to transition to ref-walk
  596. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  597. * got stuck, so ref-walk may continue from there. If this is not successful
  598. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  599. * to restart the path walk from the beginning in ref-walk mode.
  600. */
  601. /**
  602. * unlazy_walk - try to switch to ref-walk mode.
  603. * @nd: nameidata pathwalk data
  604. * Returns: 0 on success, -ECHILD on failure
  605. *
  606. * unlazy_walk attempts to legitimize the current nd->path and nd->root
  607. * for ref-walk mode.
  608. * Must be called from rcu-walk context.
  609. * Nothing should touch nameidata between unlazy_walk() failure and
  610. * terminate_walk().
  611. */
  612. static int unlazy_walk(struct nameidata *nd)
  613. {
  614. struct dentry *parent = nd->path.dentry;
  615. BUG_ON(!(nd->flags & LOOKUP_RCU));
  616. nd->flags &= ~LOOKUP_RCU;
  617. if (unlikely(!legitimize_links(nd)))
  618. goto out2;
  619. if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
  620. goto out1;
  621. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  622. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
  623. goto out;
  624. }
  625. rcu_read_unlock();
  626. BUG_ON(nd->inode != parent->d_inode);
  627. return 0;
  628. out2:
  629. nd->path.mnt = NULL;
  630. nd->path.dentry = NULL;
  631. out1:
  632. if (!(nd->flags & LOOKUP_ROOT))
  633. nd->root.mnt = NULL;
  634. out:
  635. rcu_read_unlock();
  636. return -ECHILD;
  637. }
  638. /**
  639. * unlazy_child - try to switch to ref-walk mode.
  640. * @nd: nameidata pathwalk data
  641. * @dentry: child of nd->path.dentry
  642. * @seq: seq number to check dentry against
  643. * Returns: 0 on success, -ECHILD on failure
  644. *
  645. * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
  646. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  647. * @nd. Must be called from rcu-walk context.
  648. * Nothing should touch nameidata between unlazy_child() failure and
  649. * terminate_walk().
  650. */
  651. static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
  652. {
  653. BUG_ON(!(nd->flags & LOOKUP_RCU));
  654. nd->flags &= ~LOOKUP_RCU;
  655. if (unlikely(!legitimize_links(nd)))
  656. goto out2;
  657. if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
  658. goto out2;
  659. if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
  660. goto out1;
  661. /*
  662. * We need to move both the parent and the dentry from the RCU domain
  663. * to be properly refcounted. And the sequence number in the dentry
  664. * validates *both* dentry counters, since we checked the sequence
  665. * number of the parent after we got the child sequence number. So we
  666. * know the parent must still be valid if the child sequence number is
  667. */
  668. if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
  669. goto out;
  670. if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
  671. rcu_read_unlock();
  672. dput(dentry);
  673. goto drop_root_mnt;
  674. }
  675. /*
  676. * Sequence counts matched. Now make sure that the root is
  677. * still valid and get it if required.
  678. */
  679. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  680. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
  681. rcu_read_unlock();
  682. dput(dentry);
  683. return -ECHILD;
  684. }
  685. }
  686. rcu_read_unlock();
  687. return 0;
  688. out2:
  689. nd->path.mnt = NULL;
  690. out1:
  691. nd->path.dentry = NULL;
  692. out:
  693. rcu_read_unlock();
  694. drop_root_mnt:
  695. if (!(nd->flags & LOOKUP_ROOT))
  696. nd->root.mnt = NULL;
  697. return -ECHILD;
  698. }
  699. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  700. {
  701. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
  702. return dentry->d_op->d_revalidate(dentry, flags);
  703. else
  704. return 1;
  705. }
  706. /**
  707. * complete_walk - successful completion of path walk
  708. * @nd: pointer nameidata
  709. *
  710. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  711. * Revalidate the final result, unless we'd already done that during
  712. * the path walk or the filesystem doesn't ask for it. Return 0 on
  713. * success, -error on failure. In case of failure caller does not
  714. * need to drop nd->path.
  715. */
  716. static int complete_walk(struct nameidata *nd)
  717. {
  718. struct dentry *dentry = nd->path.dentry;
  719. int status;
  720. if (nd->flags & LOOKUP_RCU) {
  721. if (!(nd->flags & LOOKUP_ROOT))
  722. nd->root.mnt = NULL;
  723. if (unlikely(unlazy_walk(nd)))
  724. return -ECHILD;
  725. }
  726. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  727. return 0;
  728. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  729. return 0;
  730. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  731. if (status > 0)
  732. return 0;
  733. if (!status)
  734. status = -ESTALE;
  735. return status;
  736. }
  737. static void set_root(struct nameidata *nd)
  738. {
  739. struct fs_struct *fs = current->fs;
  740. if (nd->flags & LOOKUP_RCU) {
  741. unsigned seq;
  742. do {
  743. seq = read_seqcount_begin(&fs->seq);
  744. nd->root = fs->root;
  745. nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  746. } while (read_seqcount_retry(&fs->seq, seq));
  747. } else {
  748. get_fs_root(fs, &nd->root);
  749. }
  750. }
  751. static void path_put_conditional(struct path *path, struct nameidata *nd)
  752. {
  753. dput(path->dentry);
  754. if (path->mnt != nd->path.mnt)
  755. mntput(path->mnt);
  756. }
  757. static inline void path_to_nameidata(const struct path *path,
  758. struct nameidata *nd)
  759. {
  760. if (!(nd->flags & LOOKUP_RCU)) {
  761. dput(nd->path.dentry);
  762. if (nd->path.mnt != path->mnt)
  763. mntput(nd->path.mnt);
  764. }
  765. nd->path.mnt = path->mnt;
  766. nd->path.dentry = path->dentry;
  767. }
  768. static int nd_jump_root(struct nameidata *nd)
  769. {
  770. if (nd->flags & LOOKUP_RCU) {
  771. struct dentry *d;
  772. nd->path = nd->root;
  773. d = nd->path.dentry;
  774. nd->inode = d->d_inode;
  775. nd->seq = nd->root_seq;
  776. if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
  777. return -ECHILD;
  778. } else {
  779. path_put(&nd->path);
  780. nd->path = nd->root;
  781. path_get(&nd->path);
  782. nd->inode = nd->path.dentry->d_inode;
  783. }
  784. nd->flags |= LOOKUP_JUMPED;
  785. return 0;
  786. }
  787. /*
  788. * Helper to directly jump to a known parsed path from ->get_link,
  789. * caller must have taken a reference to path beforehand.
  790. */
  791. void nd_jump_link(struct path *path)
  792. {
  793. struct nameidata *nd = current->nameidata;
  794. path_put(&nd->path);
  795. nd->path = *path;
  796. nd->inode = nd->path.dentry->d_inode;
  797. nd->flags |= LOOKUP_JUMPED;
  798. }
  799. static inline void put_link(struct nameidata *nd)
  800. {
  801. struct saved *last = nd->stack + --nd->depth;
  802. do_delayed_call(&last->done);
  803. if (!(nd->flags & LOOKUP_RCU))
  804. path_put(&last->link);
  805. }
  806. int sysctl_protected_symlinks __read_mostly = 0;
  807. int sysctl_protected_hardlinks __read_mostly = 0;
  808. /**
  809. * may_follow_link - Check symlink following for unsafe situations
  810. * @nd: nameidata pathwalk data
  811. *
  812. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  813. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  814. * in a sticky world-writable directory. This is to protect privileged
  815. * processes from failing races against path names that may change out
  816. * from under them by way of other users creating malicious symlinks.
  817. * It will permit symlinks to be followed only when outside a sticky
  818. * world-writable directory, or when the uid of the symlink and follower
  819. * match, or when the directory owner matches the symlink's owner.
  820. *
  821. * Returns 0 if following the symlink is allowed, -ve on error.
  822. */
  823. static inline int may_follow_link(struct nameidata *nd)
  824. {
  825. const struct inode *inode;
  826. const struct inode *parent;
  827. kuid_t puid;
  828. if (!sysctl_protected_symlinks)
  829. return 0;
  830. /* Allowed if owner and follower match. */
  831. inode = nd->link_inode;
  832. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  833. return 0;
  834. /* Allowed if parent directory not sticky and world-writable. */
  835. parent = nd->inode;
  836. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  837. return 0;
  838. /* Allowed if parent directory and link owner match. */
  839. puid = parent->i_uid;
  840. if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
  841. return 0;
  842. if (nd->flags & LOOKUP_RCU)
  843. return -ECHILD;
  844. audit_log_link_denied("follow_link", &nd->stack[0].link);
  845. return -EACCES;
  846. }
  847. /**
  848. * safe_hardlink_source - Check for safe hardlink conditions
  849. * @inode: the source inode to hardlink from
  850. *
  851. * Return false if at least one of the following conditions:
  852. * - inode is not a regular file
  853. * - inode is setuid
  854. * - inode is setgid and group-exec
  855. * - access failure for read and write
  856. *
  857. * Otherwise returns true.
  858. */
  859. static bool safe_hardlink_source(struct inode *inode)
  860. {
  861. umode_t mode = inode->i_mode;
  862. /* Special files should not get pinned to the filesystem. */
  863. if (!S_ISREG(mode))
  864. return false;
  865. /* Setuid files should not get pinned to the filesystem. */
  866. if (mode & S_ISUID)
  867. return false;
  868. /* Executable setgid files should not get pinned to the filesystem. */
  869. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  870. return false;
  871. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  872. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  873. return false;
  874. return true;
  875. }
  876. /**
  877. * may_linkat - Check permissions for creating a hardlink
  878. * @link: the source to hardlink from
  879. *
  880. * Block hardlink when all of:
  881. * - sysctl_protected_hardlinks enabled
  882. * - fsuid does not match inode
  883. * - hardlink source is unsafe (see safe_hardlink_source() above)
  884. * - not CAP_FOWNER in a namespace with the inode owner uid mapped
  885. *
  886. * Returns 0 if successful, -ve on error.
  887. */
  888. static int may_linkat(struct path *link)
  889. {
  890. struct inode *inode;
  891. if (!sysctl_protected_hardlinks)
  892. return 0;
  893. inode = link->dentry->d_inode;
  894. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  895. * otherwise, it must be a safe source.
  896. */
  897. if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
  898. return 0;
  899. audit_log_link_denied("linkat", link);
  900. return -EPERM;
  901. }
  902. static __always_inline
  903. const char *get_link(struct nameidata *nd)
  904. {
  905. struct saved *last = nd->stack + nd->depth - 1;
  906. struct dentry *dentry = last->link.dentry;
  907. struct inode *inode = nd->link_inode;
  908. int error;
  909. const char *res;
  910. if (!(nd->flags & LOOKUP_RCU)) {
  911. touch_atime(&last->link);
  912. cond_resched();
  913. } else if (atime_needs_update_rcu(&last->link, inode)) {
  914. if (unlikely(unlazy_walk(nd)))
  915. return ERR_PTR(-ECHILD);
  916. touch_atime(&last->link);
  917. }
  918. error = security_inode_follow_link(dentry, inode,
  919. nd->flags & LOOKUP_RCU);
  920. if (unlikely(error))
  921. return ERR_PTR(error);
  922. nd->last_type = LAST_BIND;
  923. res = inode->i_link;
  924. if (!res) {
  925. const char * (*get)(struct dentry *, struct inode *,
  926. struct delayed_call *);
  927. get = inode->i_op->get_link;
  928. if (nd->flags & LOOKUP_RCU) {
  929. res = get(NULL, inode, &last->done);
  930. if (res == ERR_PTR(-ECHILD)) {
  931. if (unlikely(unlazy_walk(nd)))
  932. return ERR_PTR(-ECHILD);
  933. res = get(dentry, inode, &last->done);
  934. }
  935. } else {
  936. res = get(dentry, inode, &last->done);
  937. }
  938. if (IS_ERR_OR_NULL(res))
  939. return res;
  940. }
  941. if (*res == '/') {
  942. if (!nd->root.mnt)
  943. set_root(nd);
  944. if (unlikely(nd_jump_root(nd)))
  945. return ERR_PTR(-ECHILD);
  946. while (unlikely(*++res == '/'))
  947. ;
  948. }
  949. if (!*res)
  950. res = NULL;
  951. return res;
  952. }
  953. /*
  954. * follow_up - Find the mountpoint of path's vfsmount
  955. *
  956. * Given a path, find the mountpoint of its source file system.
  957. * Replace @path with the path of the mountpoint in the parent mount.
  958. * Up is towards /.
  959. *
  960. * Return 1 if we went up a level and 0 if we were already at the
  961. * root.
  962. */
  963. int follow_up(struct path *path)
  964. {
  965. struct mount *mnt = real_mount(path->mnt);
  966. struct mount *parent;
  967. struct dentry *mountpoint;
  968. read_seqlock_excl(&mount_lock);
  969. parent = mnt->mnt_parent;
  970. if (parent == mnt) {
  971. read_sequnlock_excl(&mount_lock);
  972. return 0;
  973. }
  974. mntget(&parent->mnt);
  975. mountpoint = dget(mnt->mnt_mountpoint);
  976. read_sequnlock_excl(&mount_lock);
  977. dput(path->dentry);
  978. path->dentry = mountpoint;
  979. mntput(path->mnt);
  980. path->mnt = &parent->mnt;
  981. return 1;
  982. }
  983. EXPORT_SYMBOL(follow_up);
  984. /*
  985. * Perform an automount
  986. * - return -EISDIR to tell follow_managed() to stop and return the path we
  987. * were called with.
  988. */
  989. static int follow_automount(struct path *path, struct nameidata *nd,
  990. bool *need_mntput)
  991. {
  992. struct vfsmount *mnt;
  993. int err;
  994. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  995. return -EREMOTE;
  996. /* We don't want to mount if someone's just doing a stat -
  997. * unless they're stat'ing a directory and appended a '/' to
  998. * the name.
  999. *
  1000. * We do, however, want to mount if someone wants to open or
  1001. * create a file of any type under the mountpoint, wants to
  1002. * traverse through the mountpoint or wants to open the
  1003. * mounted directory. Also, autofs may mark negative dentries
  1004. * as being automount points. These will need the attentions
  1005. * of the daemon to instantiate them before they can be used.
  1006. */
  1007. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  1008. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  1009. path->dentry->d_inode)
  1010. return -EISDIR;
  1011. if (path->dentry->d_sb->s_user_ns != &init_user_ns)
  1012. return -EACCES;
  1013. nd->total_link_count++;
  1014. if (nd->total_link_count >= 40)
  1015. return -ELOOP;
  1016. mnt = path->dentry->d_op->d_automount(path);
  1017. if (IS_ERR(mnt)) {
  1018. /*
  1019. * The filesystem is allowed to return -EISDIR here to indicate
  1020. * it doesn't want to automount. For instance, autofs would do
  1021. * this so that its userspace daemon can mount on this dentry.
  1022. *
  1023. * However, we can only permit this if it's a terminal point in
  1024. * the path being looked up; if it wasn't then the remainder of
  1025. * the path is inaccessible and we should say so.
  1026. */
  1027. if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
  1028. return -EREMOTE;
  1029. return PTR_ERR(mnt);
  1030. }
  1031. if (!mnt) /* mount collision */
  1032. return 0;
  1033. if (!*need_mntput) {
  1034. /* lock_mount() may release path->mnt on error */
  1035. mntget(path->mnt);
  1036. *need_mntput = true;
  1037. }
  1038. err = finish_automount(mnt, path);
  1039. switch (err) {
  1040. case -EBUSY:
  1041. /* Someone else made a mount here whilst we were busy */
  1042. return 0;
  1043. case 0:
  1044. path_put(path);
  1045. path->mnt = mnt;
  1046. path->dentry = dget(mnt->mnt_root);
  1047. return 0;
  1048. default:
  1049. return err;
  1050. }
  1051. }
  1052. /*
  1053. * Handle a dentry that is managed in some way.
  1054. * - Flagged for transit management (autofs)
  1055. * - Flagged as mountpoint
  1056. * - Flagged as automount point
  1057. *
  1058. * This may only be called in refwalk mode.
  1059. *
  1060. * Serialization is taken care of in namespace.c
  1061. */
  1062. static int follow_managed(struct path *path, struct nameidata *nd)
  1063. {
  1064. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  1065. unsigned managed;
  1066. bool need_mntput = false;
  1067. int ret = 0;
  1068. /* Given that we're not holding a lock here, we retain the value in a
  1069. * local variable for each dentry as we look at it so that we don't see
  1070. * the components of that value change under us */
  1071. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1072. managed &= DCACHE_MANAGED_DENTRY,
  1073. unlikely(managed != 0)) {
  1074. /* Allow the filesystem to manage the transit without i_mutex
  1075. * being held. */
  1076. if (managed & DCACHE_MANAGE_TRANSIT) {
  1077. BUG_ON(!path->dentry->d_op);
  1078. BUG_ON(!path->dentry->d_op->d_manage);
  1079. ret = path->dentry->d_op->d_manage(path, false);
  1080. if (ret < 0)
  1081. break;
  1082. }
  1083. /* Transit to a mounted filesystem. */
  1084. if (managed & DCACHE_MOUNTED) {
  1085. struct vfsmount *mounted = lookup_mnt(path);
  1086. if (mounted) {
  1087. dput(path->dentry);
  1088. if (need_mntput)
  1089. mntput(path->mnt);
  1090. path->mnt = mounted;
  1091. path->dentry = dget(mounted->mnt_root);
  1092. need_mntput = true;
  1093. continue;
  1094. }
  1095. /* Something is mounted on this dentry in another
  1096. * namespace and/or whatever was mounted there in this
  1097. * namespace got unmounted before lookup_mnt() could
  1098. * get it */
  1099. }
  1100. /* Handle an automount point */
  1101. if (managed & DCACHE_NEED_AUTOMOUNT) {
  1102. ret = follow_automount(path, nd, &need_mntput);
  1103. if (ret < 0)
  1104. break;
  1105. continue;
  1106. }
  1107. /* We didn't change the current path point */
  1108. break;
  1109. }
  1110. if (need_mntput && path->mnt == mnt)
  1111. mntput(path->mnt);
  1112. if (ret == -EISDIR || !ret)
  1113. ret = 1;
  1114. if (need_mntput)
  1115. nd->flags |= LOOKUP_JUMPED;
  1116. if (unlikely(ret < 0))
  1117. path_put_conditional(path, nd);
  1118. return ret;
  1119. }
  1120. int follow_down_one(struct path *path)
  1121. {
  1122. struct vfsmount *mounted;
  1123. mounted = lookup_mnt(path);
  1124. if (mounted) {
  1125. dput(path->dentry);
  1126. mntput(path->mnt);
  1127. path->mnt = mounted;
  1128. path->dentry = dget(mounted->mnt_root);
  1129. return 1;
  1130. }
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(follow_down_one);
  1134. static inline int managed_dentry_rcu(const struct path *path)
  1135. {
  1136. return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  1137. path->dentry->d_op->d_manage(path, true) : 0;
  1138. }
  1139. /*
  1140. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  1141. * we meet a managed dentry that would need blocking.
  1142. */
  1143. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  1144. struct inode **inode, unsigned *seqp)
  1145. {
  1146. for (;;) {
  1147. struct mount *mounted;
  1148. /*
  1149. * Don't forget we might have a non-mountpoint managed dentry
  1150. * that wants to block transit.
  1151. */
  1152. switch (managed_dentry_rcu(path)) {
  1153. case -ECHILD:
  1154. default:
  1155. return false;
  1156. case -EISDIR:
  1157. return true;
  1158. case 0:
  1159. break;
  1160. }
  1161. if (!d_mountpoint(path->dentry))
  1162. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1163. mounted = __lookup_mnt(path->mnt, path->dentry);
  1164. if (!mounted)
  1165. break;
  1166. path->mnt = &mounted->mnt;
  1167. path->dentry = mounted->mnt.mnt_root;
  1168. nd->flags |= LOOKUP_JUMPED;
  1169. *seqp = read_seqcount_begin(&path->dentry->d_seq);
  1170. /*
  1171. * Update the inode too. We don't need to re-check the
  1172. * dentry sequence number here after this d_inode read,
  1173. * because a mount-point is always pinned.
  1174. */
  1175. *inode = path->dentry->d_inode;
  1176. }
  1177. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1178. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1179. }
  1180. static int follow_dotdot_rcu(struct nameidata *nd)
  1181. {
  1182. struct inode *inode = nd->inode;
  1183. while (1) {
  1184. if (path_equal(&nd->path, &nd->root))
  1185. break;
  1186. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1187. struct dentry *old = nd->path.dentry;
  1188. struct dentry *parent = old->d_parent;
  1189. unsigned seq;
  1190. inode = parent->d_inode;
  1191. seq = read_seqcount_begin(&parent->d_seq);
  1192. if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
  1193. return -ECHILD;
  1194. nd->path.dentry = parent;
  1195. nd->seq = seq;
  1196. if (unlikely(!path_connected(&nd->path)))
  1197. return -ENOENT;
  1198. break;
  1199. } else {
  1200. struct mount *mnt = real_mount(nd->path.mnt);
  1201. struct mount *mparent = mnt->mnt_parent;
  1202. struct dentry *mountpoint = mnt->mnt_mountpoint;
  1203. struct inode *inode2 = mountpoint->d_inode;
  1204. unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
  1205. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1206. return -ECHILD;
  1207. if (&mparent->mnt == nd->path.mnt)
  1208. break;
  1209. /* we know that mountpoint was pinned */
  1210. nd->path.dentry = mountpoint;
  1211. nd->path.mnt = &mparent->mnt;
  1212. inode = inode2;
  1213. nd->seq = seq;
  1214. }
  1215. }
  1216. while (unlikely(d_mountpoint(nd->path.dentry))) {
  1217. struct mount *mounted;
  1218. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1219. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1220. return -ECHILD;
  1221. if (!mounted)
  1222. break;
  1223. nd->path.mnt = &mounted->mnt;
  1224. nd->path.dentry = mounted->mnt.mnt_root;
  1225. inode = nd->path.dentry->d_inode;
  1226. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1227. }
  1228. nd->inode = inode;
  1229. return 0;
  1230. }
  1231. /*
  1232. * Follow down to the covering mount currently visible to userspace. At each
  1233. * point, the filesystem owning that dentry may be queried as to whether the
  1234. * caller is permitted to proceed or not.
  1235. */
  1236. int follow_down(struct path *path)
  1237. {
  1238. unsigned managed;
  1239. int ret;
  1240. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1241. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1242. /* Allow the filesystem to manage the transit without i_mutex
  1243. * being held.
  1244. *
  1245. * We indicate to the filesystem if someone is trying to mount
  1246. * something here. This gives autofs the chance to deny anyone
  1247. * other than its daemon the right to mount on its
  1248. * superstructure.
  1249. *
  1250. * The filesystem may sleep at this point.
  1251. */
  1252. if (managed & DCACHE_MANAGE_TRANSIT) {
  1253. BUG_ON(!path->dentry->d_op);
  1254. BUG_ON(!path->dentry->d_op->d_manage);
  1255. ret = path->dentry->d_op->d_manage(path, false);
  1256. if (ret < 0)
  1257. return ret == -EISDIR ? 0 : ret;
  1258. }
  1259. /* Transit to a mounted filesystem. */
  1260. if (managed & DCACHE_MOUNTED) {
  1261. struct vfsmount *mounted = lookup_mnt(path);
  1262. if (!mounted)
  1263. break;
  1264. dput(path->dentry);
  1265. mntput(path->mnt);
  1266. path->mnt = mounted;
  1267. path->dentry = dget(mounted->mnt_root);
  1268. continue;
  1269. }
  1270. /* Don't handle automount points here */
  1271. break;
  1272. }
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(follow_down);
  1276. /*
  1277. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1278. */
  1279. static void follow_mount(struct path *path)
  1280. {
  1281. while (d_mountpoint(path->dentry)) {
  1282. struct vfsmount *mounted = lookup_mnt(path);
  1283. if (!mounted)
  1284. break;
  1285. dput(path->dentry);
  1286. mntput(path->mnt);
  1287. path->mnt = mounted;
  1288. path->dentry = dget(mounted->mnt_root);
  1289. }
  1290. }
  1291. static int path_parent_directory(struct path *path)
  1292. {
  1293. struct dentry *old = path->dentry;
  1294. /* rare case of legitimate dget_parent()... */
  1295. path->dentry = dget_parent(path->dentry);
  1296. dput(old);
  1297. if (unlikely(!path_connected(path)))
  1298. return -ENOENT;
  1299. return 0;
  1300. }
  1301. static int follow_dotdot(struct nameidata *nd)
  1302. {
  1303. while(1) {
  1304. if (nd->path.dentry == nd->root.dentry &&
  1305. nd->path.mnt == nd->root.mnt) {
  1306. break;
  1307. }
  1308. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1309. int ret = path_parent_directory(&nd->path);
  1310. if (ret)
  1311. return ret;
  1312. break;
  1313. }
  1314. if (!follow_up(&nd->path))
  1315. break;
  1316. }
  1317. follow_mount(&nd->path);
  1318. nd->inode = nd->path.dentry->d_inode;
  1319. return 0;
  1320. }
  1321. /*
  1322. * This looks up the name in dcache and possibly revalidates the found dentry.
  1323. * NULL is returned if the dentry does not exist in the cache.
  1324. */
  1325. static struct dentry *lookup_dcache(const struct qstr *name,
  1326. struct dentry *dir,
  1327. unsigned int flags)
  1328. {
  1329. struct dentry *dentry = d_lookup(dir, name);
  1330. if (dentry) {
  1331. int error = d_revalidate(dentry, flags);
  1332. if (unlikely(error <= 0)) {
  1333. if (!error)
  1334. d_invalidate(dentry);
  1335. dput(dentry);
  1336. return ERR_PTR(error);
  1337. }
  1338. }
  1339. return dentry;
  1340. }
  1341. /*
  1342. * Call i_op->lookup on the dentry. The dentry must be negative and
  1343. * unhashed.
  1344. *
  1345. * dir->d_inode->i_mutex must be held
  1346. */
  1347. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1348. unsigned int flags)
  1349. {
  1350. struct dentry *old;
  1351. /* Don't create child dentry for a dead directory. */
  1352. if (unlikely(IS_DEADDIR(dir))) {
  1353. dput(dentry);
  1354. return ERR_PTR(-ENOENT);
  1355. }
  1356. old = dir->i_op->lookup(dir, dentry, flags);
  1357. if (unlikely(old)) {
  1358. dput(dentry);
  1359. dentry = old;
  1360. }
  1361. return dentry;
  1362. }
  1363. static struct dentry *__lookup_hash(const struct qstr *name,
  1364. struct dentry *base, unsigned int flags)
  1365. {
  1366. struct dentry *dentry = lookup_dcache(name, base, flags);
  1367. if (dentry)
  1368. return dentry;
  1369. dentry = d_alloc(base, name);
  1370. if (unlikely(!dentry))
  1371. return ERR_PTR(-ENOMEM);
  1372. return lookup_real(base->d_inode, dentry, flags);
  1373. }
  1374. static int lookup_fast(struct nameidata *nd,
  1375. struct path *path, struct inode **inode,
  1376. unsigned *seqp)
  1377. {
  1378. struct vfsmount *mnt = nd->path.mnt;
  1379. struct dentry *dentry, *parent = nd->path.dentry;
  1380. int status = 1;
  1381. int err;
  1382. /*
  1383. * Rename seqlock is not required here because in the off chance
  1384. * of a false negative due to a concurrent rename, the caller is
  1385. * going to fall back to non-racy lookup.
  1386. */
  1387. if (nd->flags & LOOKUP_RCU) {
  1388. unsigned seq;
  1389. bool negative;
  1390. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1391. if (unlikely(!dentry)) {
  1392. if (unlazy_walk(nd))
  1393. return -ECHILD;
  1394. return 0;
  1395. }
  1396. /*
  1397. * This sequence count validates that the inode matches
  1398. * the dentry name information from lookup.
  1399. */
  1400. *inode = d_backing_inode(dentry);
  1401. negative = d_is_negative(dentry);
  1402. if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
  1403. return -ECHILD;
  1404. /*
  1405. * This sequence count validates that the parent had no
  1406. * changes while we did the lookup of the dentry above.
  1407. *
  1408. * The memory barrier in read_seqcount_begin of child is
  1409. * enough, we can use __read_seqcount_retry here.
  1410. */
  1411. if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
  1412. return -ECHILD;
  1413. *seqp = seq;
  1414. status = d_revalidate(dentry, nd->flags);
  1415. if (likely(status > 0)) {
  1416. /*
  1417. * Note: do negative dentry check after revalidation in
  1418. * case that drops it.
  1419. */
  1420. if (unlikely(negative))
  1421. return -ENOENT;
  1422. path->mnt = mnt;
  1423. path->dentry = dentry;
  1424. if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
  1425. return 1;
  1426. }
  1427. if (unlazy_child(nd, dentry, seq))
  1428. return -ECHILD;
  1429. if (unlikely(status == -ECHILD))
  1430. /* we'd been told to redo it in non-rcu mode */
  1431. status = d_revalidate(dentry, nd->flags);
  1432. } else {
  1433. dentry = __d_lookup(parent, &nd->last);
  1434. if (unlikely(!dentry))
  1435. return 0;
  1436. status = d_revalidate(dentry, nd->flags);
  1437. }
  1438. if (unlikely(status <= 0)) {
  1439. if (!status)
  1440. d_invalidate(dentry);
  1441. dput(dentry);
  1442. return status;
  1443. }
  1444. if (unlikely(d_is_negative(dentry))) {
  1445. dput(dentry);
  1446. return -ENOENT;
  1447. }
  1448. path->mnt = mnt;
  1449. path->dentry = dentry;
  1450. err = follow_managed(path, nd);
  1451. if (likely(err > 0))
  1452. *inode = d_backing_inode(path->dentry);
  1453. return err;
  1454. }
  1455. /* Fast lookup failed, do it the slow way */
  1456. static struct dentry *lookup_slow(const struct qstr *name,
  1457. struct dentry *dir,
  1458. unsigned int flags)
  1459. {
  1460. struct dentry *dentry = ERR_PTR(-ENOENT), *old;
  1461. struct inode *inode = dir->d_inode;
  1462. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1463. inode_lock_shared(inode);
  1464. /* Don't go there if it's already dead */
  1465. if (unlikely(IS_DEADDIR(inode)))
  1466. goto out;
  1467. again:
  1468. dentry = d_alloc_parallel(dir, name, &wq);
  1469. if (IS_ERR(dentry))
  1470. goto out;
  1471. if (unlikely(!d_in_lookup(dentry))) {
  1472. if (!(flags & LOOKUP_NO_REVAL)) {
  1473. int error = d_revalidate(dentry, flags);
  1474. if (unlikely(error <= 0)) {
  1475. if (!error) {
  1476. d_invalidate(dentry);
  1477. dput(dentry);
  1478. goto again;
  1479. }
  1480. dput(dentry);
  1481. dentry = ERR_PTR(error);
  1482. }
  1483. }
  1484. } else {
  1485. old = inode->i_op->lookup(inode, dentry, flags);
  1486. d_lookup_done(dentry);
  1487. if (unlikely(old)) {
  1488. dput(dentry);
  1489. dentry = old;
  1490. }
  1491. }
  1492. out:
  1493. inode_unlock_shared(inode);
  1494. return dentry;
  1495. }
  1496. static inline int may_lookup(struct nameidata *nd)
  1497. {
  1498. if (nd->flags & LOOKUP_RCU) {
  1499. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1500. if (err != -ECHILD)
  1501. return err;
  1502. if (unlazy_walk(nd))
  1503. return -ECHILD;
  1504. }
  1505. return inode_permission(nd->inode, MAY_EXEC);
  1506. }
  1507. static inline int handle_dots(struct nameidata *nd, int type)
  1508. {
  1509. if (type == LAST_DOTDOT) {
  1510. if (!nd->root.mnt)
  1511. set_root(nd);
  1512. if (nd->flags & LOOKUP_RCU) {
  1513. return follow_dotdot_rcu(nd);
  1514. } else
  1515. return follow_dotdot(nd);
  1516. }
  1517. return 0;
  1518. }
  1519. static int pick_link(struct nameidata *nd, struct path *link,
  1520. struct inode *inode, unsigned seq)
  1521. {
  1522. int error;
  1523. struct saved *last;
  1524. if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
  1525. path_to_nameidata(link, nd);
  1526. return -ELOOP;
  1527. }
  1528. if (!(nd->flags & LOOKUP_RCU)) {
  1529. if (link->mnt == nd->path.mnt)
  1530. mntget(link->mnt);
  1531. }
  1532. error = nd_alloc_stack(nd);
  1533. if (unlikely(error)) {
  1534. if (error == -ECHILD) {
  1535. if (unlikely(!legitimize_path(nd, link, seq))) {
  1536. drop_links(nd);
  1537. nd->depth = 0;
  1538. nd->flags &= ~LOOKUP_RCU;
  1539. nd->path.mnt = NULL;
  1540. nd->path.dentry = NULL;
  1541. if (!(nd->flags & LOOKUP_ROOT))
  1542. nd->root.mnt = NULL;
  1543. rcu_read_unlock();
  1544. } else if (likely(unlazy_walk(nd)) == 0)
  1545. error = nd_alloc_stack(nd);
  1546. }
  1547. if (error) {
  1548. path_put(link);
  1549. return error;
  1550. }
  1551. }
  1552. last = nd->stack + nd->depth++;
  1553. last->link = *link;
  1554. clear_delayed_call(&last->done);
  1555. nd->link_inode = inode;
  1556. last->seq = seq;
  1557. return 1;
  1558. }
  1559. enum {WALK_FOLLOW = 1, WALK_MORE = 2};
  1560. /*
  1561. * Do we need to follow links? We _really_ want to be able
  1562. * to do this check without having to look at inode->i_op,
  1563. * so we keep a cache of "no, this doesn't need follow_link"
  1564. * for the common case.
  1565. */
  1566. static inline int step_into(struct nameidata *nd, struct path *path,
  1567. int flags, struct inode *inode, unsigned seq)
  1568. {
  1569. if (!(flags & WALK_MORE) && nd->depth)
  1570. put_link(nd);
  1571. if (likely(!d_is_symlink(path->dentry)) ||
  1572. !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
  1573. /* not a symlink or should not follow */
  1574. path_to_nameidata(path, nd);
  1575. nd->inode = inode;
  1576. nd->seq = seq;
  1577. return 0;
  1578. }
  1579. /* make sure that d_is_symlink above matches inode */
  1580. if (nd->flags & LOOKUP_RCU) {
  1581. if (read_seqcount_retry(&path->dentry->d_seq, seq))
  1582. return -ECHILD;
  1583. }
  1584. return pick_link(nd, path, inode, seq);
  1585. }
  1586. static int walk_component(struct nameidata *nd, int flags)
  1587. {
  1588. struct path path;
  1589. struct inode *inode;
  1590. unsigned seq;
  1591. int err;
  1592. /*
  1593. * "." and ".." are special - ".." especially so because it has
  1594. * to be able to know about the current root directory and
  1595. * parent relationships.
  1596. */
  1597. if (unlikely(nd->last_type != LAST_NORM)) {
  1598. err = handle_dots(nd, nd->last_type);
  1599. if (!(flags & WALK_MORE) && nd->depth)
  1600. put_link(nd);
  1601. return err;
  1602. }
  1603. err = lookup_fast(nd, &path, &inode, &seq);
  1604. if (unlikely(err <= 0)) {
  1605. if (err < 0)
  1606. return err;
  1607. path.dentry = lookup_slow(&nd->last, nd->path.dentry,
  1608. nd->flags);
  1609. if (IS_ERR(path.dentry))
  1610. return PTR_ERR(path.dentry);
  1611. path.mnt = nd->path.mnt;
  1612. err = follow_managed(&path, nd);
  1613. if (unlikely(err < 0))
  1614. return err;
  1615. if (unlikely(d_is_negative(path.dentry))) {
  1616. path_to_nameidata(&path, nd);
  1617. return -ENOENT;
  1618. }
  1619. seq = 0; /* we are already out of RCU mode */
  1620. inode = d_backing_inode(path.dentry);
  1621. }
  1622. return step_into(nd, &path, flags, inode, seq);
  1623. }
  1624. /*
  1625. * We can do the critical dentry name comparison and hashing
  1626. * operations one word at a time, but we are limited to:
  1627. *
  1628. * - Architectures with fast unaligned word accesses. We could
  1629. * do a "get_unaligned()" if this helps and is sufficiently
  1630. * fast.
  1631. *
  1632. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1633. * do not trap on the (extremely unlikely) case of a page
  1634. * crossing operation.
  1635. *
  1636. * - Furthermore, we need an efficient 64-bit compile for the
  1637. * 64-bit case in order to generate the "number of bytes in
  1638. * the final mask". Again, that could be replaced with a
  1639. * efficient population count instruction or similar.
  1640. */
  1641. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1642. #include <asm/word-at-a-time.h>
  1643. #ifdef HASH_MIX
  1644. /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
  1645. #elif defined(CONFIG_64BIT)
  1646. /*
  1647. * Register pressure in the mixing function is an issue, particularly
  1648. * on 32-bit x86, but almost any function requires one state value and
  1649. * one temporary. Instead, use a function designed for two state values
  1650. * and no temporaries.
  1651. *
  1652. * This function cannot create a collision in only two iterations, so
  1653. * we have two iterations to achieve avalanche. In those two iterations,
  1654. * we have six layers of mixing, which is enough to spread one bit's
  1655. * influence out to 2^6 = 64 state bits.
  1656. *
  1657. * Rotate constants are scored by considering either 64 one-bit input
  1658. * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
  1659. * probability of that delta causing a change to each of the 128 output
  1660. * bits, using a sample of random initial states.
  1661. *
  1662. * The Shannon entropy of the computed probabilities is then summed
  1663. * to produce a score. Ideally, any input change has a 50% chance of
  1664. * toggling any given output bit.
  1665. *
  1666. * Mixing scores (in bits) for (12,45):
  1667. * Input delta: 1-bit 2-bit
  1668. * 1 round: 713.3 42542.6
  1669. * 2 rounds: 2753.7 140389.8
  1670. * 3 rounds: 5954.1 233458.2
  1671. * 4 rounds: 7862.6 256672.2
  1672. * Perfect: 8192 258048
  1673. * (64*128) (64*63/2 * 128)
  1674. */
  1675. #define HASH_MIX(x, y, a) \
  1676. ( x ^= (a), \
  1677. y ^= x, x = rol64(x,12),\
  1678. x += y, y = rol64(y,45),\
  1679. y *= 9 )
  1680. /*
  1681. * Fold two longs into one 32-bit hash value. This must be fast, but
  1682. * latency isn't quite as critical, as there is a fair bit of additional
  1683. * work done before the hash value is used.
  1684. */
  1685. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1686. {
  1687. y ^= x * GOLDEN_RATIO_64;
  1688. y *= GOLDEN_RATIO_64;
  1689. return y >> 32;
  1690. }
  1691. #else /* 32-bit case */
  1692. /*
  1693. * Mixing scores (in bits) for (7,20):
  1694. * Input delta: 1-bit 2-bit
  1695. * 1 round: 330.3 9201.6
  1696. * 2 rounds: 1246.4 25475.4
  1697. * 3 rounds: 1907.1 31295.1
  1698. * 4 rounds: 2042.3 31718.6
  1699. * Perfect: 2048 31744
  1700. * (32*64) (32*31/2 * 64)
  1701. */
  1702. #define HASH_MIX(x, y, a) \
  1703. ( x ^= (a), \
  1704. y ^= x, x = rol32(x, 7),\
  1705. x += y, y = rol32(y,20),\
  1706. y *= 9 )
  1707. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1708. {
  1709. /* Use arch-optimized multiply if one exists */
  1710. return __hash_32(y ^ __hash_32(x));
  1711. }
  1712. #endif
  1713. /*
  1714. * Return the hash of a string of known length. This is carfully
  1715. * designed to match hash_name(), which is the more critical function.
  1716. * In particular, we must end by hashing a final word containing 0..7
  1717. * payload bytes, to match the way that hash_name() iterates until it
  1718. * finds the delimiter after the name.
  1719. */
  1720. unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
  1721. {
  1722. unsigned long a, x = 0, y = (unsigned long)salt;
  1723. for (;;) {
  1724. if (!len)
  1725. goto done;
  1726. a = load_unaligned_zeropad(name);
  1727. if (len < sizeof(unsigned long))
  1728. break;
  1729. HASH_MIX(x, y, a);
  1730. name += sizeof(unsigned long);
  1731. len -= sizeof(unsigned long);
  1732. }
  1733. x ^= a & bytemask_from_count(len);
  1734. done:
  1735. return fold_hash(x, y);
  1736. }
  1737. EXPORT_SYMBOL(full_name_hash);
  1738. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1739. u64 hashlen_string(const void *salt, const char *name)
  1740. {
  1741. unsigned long a = 0, x = 0, y = (unsigned long)salt;
  1742. unsigned long adata, mask, len;
  1743. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1744. len = 0;
  1745. goto inside;
  1746. do {
  1747. HASH_MIX(x, y, a);
  1748. len += sizeof(unsigned long);
  1749. inside:
  1750. a = load_unaligned_zeropad(name+len);
  1751. } while (!has_zero(a, &adata, &constants));
  1752. adata = prep_zero_mask(a, adata, &constants);
  1753. mask = create_zero_mask(adata);
  1754. x ^= a & zero_bytemask(mask);
  1755. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1756. }
  1757. EXPORT_SYMBOL(hashlen_string);
  1758. /*
  1759. * Calculate the length and hash of the path component, and
  1760. * return the "hash_len" as the result.
  1761. */
  1762. static inline u64 hash_name(const void *salt, const char *name)
  1763. {
  1764. unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
  1765. unsigned long adata, bdata, mask, len;
  1766. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1767. len = 0;
  1768. goto inside;
  1769. do {
  1770. HASH_MIX(x, y, a);
  1771. len += sizeof(unsigned long);
  1772. inside:
  1773. a = load_unaligned_zeropad(name+len);
  1774. b = a ^ REPEAT_BYTE('/');
  1775. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1776. adata = prep_zero_mask(a, adata, &constants);
  1777. bdata = prep_zero_mask(b, bdata, &constants);
  1778. mask = create_zero_mask(adata | bdata);
  1779. x ^= a & zero_bytemask(mask);
  1780. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1781. }
  1782. #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
  1783. /* Return the hash of a string of known length */
  1784. unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
  1785. {
  1786. unsigned long hash = init_name_hash(salt);
  1787. while (len--)
  1788. hash = partial_name_hash((unsigned char)*name++, hash);
  1789. return end_name_hash(hash);
  1790. }
  1791. EXPORT_SYMBOL(full_name_hash);
  1792. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1793. u64 hashlen_string(const void *salt, const char *name)
  1794. {
  1795. unsigned long hash = init_name_hash(salt);
  1796. unsigned long len = 0, c;
  1797. c = (unsigned char)*name;
  1798. while (c) {
  1799. len++;
  1800. hash = partial_name_hash(c, hash);
  1801. c = (unsigned char)name[len];
  1802. }
  1803. return hashlen_create(end_name_hash(hash), len);
  1804. }
  1805. EXPORT_SYMBOL(hashlen_string);
  1806. /*
  1807. * We know there's a real path component here of at least
  1808. * one character.
  1809. */
  1810. static inline u64 hash_name(const void *salt, const char *name)
  1811. {
  1812. unsigned long hash = init_name_hash(salt);
  1813. unsigned long len = 0, c;
  1814. c = (unsigned char)*name;
  1815. do {
  1816. len++;
  1817. hash = partial_name_hash(c, hash);
  1818. c = (unsigned char)name[len];
  1819. } while (c && c != '/');
  1820. return hashlen_create(end_name_hash(hash), len);
  1821. }
  1822. #endif
  1823. /*
  1824. * Name resolution.
  1825. * This is the basic name resolution function, turning a pathname into
  1826. * the final dentry. We expect 'base' to be positive and a directory.
  1827. *
  1828. * Returns 0 and nd will have valid dentry and mnt on success.
  1829. * Returns error and drops reference to input namei data on failure.
  1830. */
  1831. static int link_path_walk(const char *name, struct nameidata *nd)
  1832. {
  1833. int err;
  1834. while (*name=='/')
  1835. name++;
  1836. if (!*name)
  1837. return 0;
  1838. /* At this point we know we have a real path component. */
  1839. for(;;) {
  1840. u64 hash_len;
  1841. int type;
  1842. err = may_lookup(nd);
  1843. if (err)
  1844. return err;
  1845. hash_len = hash_name(nd->path.dentry, name);
  1846. type = LAST_NORM;
  1847. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1848. case 2:
  1849. if (name[1] == '.') {
  1850. type = LAST_DOTDOT;
  1851. nd->flags |= LOOKUP_JUMPED;
  1852. }
  1853. break;
  1854. case 1:
  1855. type = LAST_DOT;
  1856. }
  1857. if (likely(type == LAST_NORM)) {
  1858. struct dentry *parent = nd->path.dentry;
  1859. nd->flags &= ~LOOKUP_JUMPED;
  1860. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1861. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1862. err = parent->d_op->d_hash(parent, &this);
  1863. if (err < 0)
  1864. return err;
  1865. hash_len = this.hash_len;
  1866. name = this.name;
  1867. }
  1868. }
  1869. nd->last.hash_len = hash_len;
  1870. nd->last.name = name;
  1871. nd->last_type = type;
  1872. name += hashlen_len(hash_len);
  1873. if (!*name)
  1874. goto OK;
  1875. /*
  1876. * If it wasn't NUL, we know it was '/'. Skip that
  1877. * slash, and continue until no more slashes.
  1878. */
  1879. do {
  1880. name++;
  1881. } while (unlikely(*name == '/'));
  1882. if (unlikely(!*name)) {
  1883. OK:
  1884. /* pathname body, done */
  1885. if (!nd->depth)
  1886. return 0;
  1887. name = nd->stack[nd->depth - 1].name;
  1888. /* trailing symlink, done */
  1889. if (!name)
  1890. return 0;
  1891. /* last component of nested symlink */
  1892. err = walk_component(nd, WALK_FOLLOW);
  1893. } else {
  1894. /* not the last component */
  1895. err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
  1896. }
  1897. if (err < 0)
  1898. return err;
  1899. if (err) {
  1900. const char *s = get_link(nd);
  1901. if (IS_ERR(s))
  1902. return PTR_ERR(s);
  1903. err = 0;
  1904. if (unlikely(!s)) {
  1905. /* jumped */
  1906. put_link(nd);
  1907. } else {
  1908. nd->stack[nd->depth - 1].name = name;
  1909. name = s;
  1910. continue;
  1911. }
  1912. }
  1913. if (unlikely(!d_can_lookup(nd->path.dentry))) {
  1914. if (nd->flags & LOOKUP_RCU) {
  1915. if (unlazy_walk(nd))
  1916. return -ECHILD;
  1917. }
  1918. return -ENOTDIR;
  1919. }
  1920. }
  1921. }
  1922. static const char *path_init(struct nameidata *nd, unsigned flags)
  1923. {
  1924. int retval = 0;
  1925. const char *s = nd->name->name;
  1926. if (!*s)
  1927. flags &= ~LOOKUP_RCU;
  1928. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1929. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1930. nd->depth = 0;
  1931. if (flags & LOOKUP_ROOT) {
  1932. struct dentry *root = nd->root.dentry;
  1933. struct inode *inode = root->d_inode;
  1934. if (*s) {
  1935. if (!d_can_lookup(root))
  1936. return ERR_PTR(-ENOTDIR);
  1937. retval = inode_permission(inode, MAY_EXEC);
  1938. if (retval)
  1939. return ERR_PTR(retval);
  1940. }
  1941. nd->path = nd->root;
  1942. nd->inode = inode;
  1943. if (flags & LOOKUP_RCU) {
  1944. rcu_read_lock();
  1945. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1946. nd->root_seq = nd->seq;
  1947. nd->m_seq = read_seqbegin(&mount_lock);
  1948. } else {
  1949. path_get(&nd->path);
  1950. }
  1951. return s;
  1952. }
  1953. nd->root.mnt = NULL;
  1954. nd->path.mnt = NULL;
  1955. nd->path.dentry = NULL;
  1956. nd->m_seq = read_seqbegin(&mount_lock);
  1957. if (*s == '/') {
  1958. if (flags & LOOKUP_RCU)
  1959. rcu_read_lock();
  1960. set_root(nd);
  1961. if (likely(!nd_jump_root(nd)))
  1962. return s;
  1963. nd->root.mnt = NULL;
  1964. rcu_read_unlock();
  1965. return ERR_PTR(-ECHILD);
  1966. } else if (nd->dfd == AT_FDCWD) {
  1967. if (flags & LOOKUP_RCU) {
  1968. struct fs_struct *fs = current->fs;
  1969. unsigned seq;
  1970. rcu_read_lock();
  1971. do {
  1972. seq = read_seqcount_begin(&fs->seq);
  1973. nd->path = fs->pwd;
  1974. nd->inode = nd->path.dentry->d_inode;
  1975. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1976. } while (read_seqcount_retry(&fs->seq, seq));
  1977. } else {
  1978. get_fs_pwd(current->fs, &nd->path);
  1979. nd->inode = nd->path.dentry->d_inode;
  1980. }
  1981. return s;
  1982. } else {
  1983. /* Caller must check execute permissions on the starting path component */
  1984. struct fd f = fdget_raw(nd->dfd);
  1985. struct dentry *dentry;
  1986. if (!f.file)
  1987. return ERR_PTR(-EBADF);
  1988. dentry = f.file->f_path.dentry;
  1989. if (*s) {
  1990. if (!d_can_lookup(dentry)) {
  1991. fdput(f);
  1992. return ERR_PTR(-ENOTDIR);
  1993. }
  1994. }
  1995. nd->path = f.file->f_path;
  1996. if (flags & LOOKUP_RCU) {
  1997. rcu_read_lock();
  1998. nd->inode = nd->path.dentry->d_inode;
  1999. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  2000. } else {
  2001. path_get(&nd->path);
  2002. nd->inode = nd->path.dentry->d_inode;
  2003. }
  2004. fdput(f);
  2005. return s;
  2006. }
  2007. }
  2008. static const char *trailing_symlink(struct nameidata *nd)
  2009. {
  2010. const char *s;
  2011. int error = may_follow_link(nd);
  2012. if (unlikely(error))
  2013. return ERR_PTR(error);
  2014. nd->flags |= LOOKUP_PARENT;
  2015. nd->stack[0].name = NULL;
  2016. s = get_link(nd);
  2017. return s ? s : "";
  2018. }
  2019. static inline int lookup_last(struct nameidata *nd)
  2020. {
  2021. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  2022. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2023. nd->flags &= ~LOOKUP_PARENT;
  2024. return walk_component(nd, 0);
  2025. }
  2026. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  2027. static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
  2028. {
  2029. const char *s = path_init(nd, flags);
  2030. int err;
  2031. if (IS_ERR(s))
  2032. return PTR_ERR(s);
  2033. while (!(err = link_path_walk(s, nd))
  2034. && ((err = lookup_last(nd)) > 0)) {
  2035. s = trailing_symlink(nd);
  2036. if (IS_ERR(s)) {
  2037. err = PTR_ERR(s);
  2038. break;
  2039. }
  2040. }
  2041. if (!err)
  2042. err = complete_walk(nd);
  2043. if (!err && nd->flags & LOOKUP_DIRECTORY)
  2044. if (!d_can_lookup(nd->path.dentry))
  2045. err = -ENOTDIR;
  2046. if (!err) {
  2047. *path = nd->path;
  2048. nd->path.mnt = NULL;
  2049. nd->path.dentry = NULL;
  2050. }
  2051. terminate_walk(nd);
  2052. return err;
  2053. }
  2054. static int filename_lookup(int dfd, struct filename *name, unsigned flags,
  2055. struct path *path, struct path *root)
  2056. {
  2057. int retval;
  2058. struct nameidata nd;
  2059. if (IS_ERR(name))
  2060. return PTR_ERR(name);
  2061. if (unlikely(root)) {
  2062. nd.root = *root;
  2063. flags |= LOOKUP_ROOT;
  2064. }
  2065. set_nameidata(&nd, dfd, name);
  2066. retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
  2067. if (unlikely(retval == -ECHILD))
  2068. retval = path_lookupat(&nd, flags, path);
  2069. if (unlikely(retval == -ESTALE))
  2070. retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
  2071. if (likely(!retval))
  2072. audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
  2073. restore_nameidata();
  2074. putname(name);
  2075. return retval;
  2076. }
  2077. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  2078. static int path_parentat(struct nameidata *nd, unsigned flags,
  2079. struct path *parent)
  2080. {
  2081. const char *s = path_init(nd, flags);
  2082. int err;
  2083. if (IS_ERR(s))
  2084. return PTR_ERR(s);
  2085. err = link_path_walk(s, nd);
  2086. if (!err)
  2087. err = complete_walk(nd);
  2088. if (!err) {
  2089. *parent = nd->path;
  2090. nd->path.mnt = NULL;
  2091. nd->path.dentry = NULL;
  2092. }
  2093. terminate_walk(nd);
  2094. return err;
  2095. }
  2096. static struct filename *filename_parentat(int dfd, struct filename *name,
  2097. unsigned int flags, struct path *parent,
  2098. struct qstr *last, int *type)
  2099. {
  2100. int retval;
  2101. struct nameidata nd;
  2102. if (IS_ERR(name))
  2103. return name;
  2104. set_nameidata(&nd, dfd, name);
  2105. retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
  2106. if (unlikely(retval == -ECHILD))
  2107. retval = path_parentat(&nd, flags, parent);
  2108. if (unlikely(retval == -ESTALE))
  2109. retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
  2110. if (likely(!retval)) {
  2111. *last = nd.last;
  2112. *type = nd.last_type;
  2113. audit_inode(name, parent->dentry, LOOKUP_PARENT);
  2114. } else {
  2115. putname(name);
  2116. name = ERR_PTR(retval);
  2117. }
  2118. restore_nameidata();
  2119. return name;
  2120. }
  2121. /* does lookup, returns the object with parent locked */
  2122. struct dentry *kern_path_locked(const char *name, struct path *path)
  2123. {
  2124. struct filename *filename;
  2125. struct dentry *d;
  2126. struct qstr last;
  2127. int type;
  2128. filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
  2129. &last, &type);
  2130. if (IS_ERR(filename))
  2131. return ERR_CAST(filename);
  2132. if (unlikely(type != LAST_NORM)) {
  2133. path_put(path);
  2134. putname(filename);
  2135. return ERR_PTR(-EINVAL);
  2136. }
  2137. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  2138. d = __lookup_hash(&last, path->dentry, 0);
  2139. if (IS_ERR(d)) {
  2140. inode_unlock(path->dentry->d_inode);
  2141. path_put(path);
  2142. }
  2143. putname(filename);
  2144. return d;
  2145. }
  2146. int kern_path(const char *name, unsigned int flags, struct path *path)
  2147. {
  2148. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2149. flags, path, NULL);
  2150. }
  2151. EXPORT_SYMBOL(kern_path);
  2152. /**
  2153. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  2154. * @dentry: pointer to dentry of the base directory
  2155. * @mnt: pointer to vfs mount of the base directory
  2156. * @name: pointer to file name
  2157. * @flags: lookup flags
  2158. * @path: pointer to struct path to fill
  2159. */
  2160. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  2161. const char *name, unsigned int flags,
  2162. struct path *path)
  2163. {
  2164. struct path root = {.mnt = mnt, .dentry = dentry};
  2165. /* the first argument of filename_lookup() is ignored with root */
  2166. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2167. flags , path, &root);
  2168. }
  2169. EXPORT_SYMBOL(vfs_path_lookup);
  2170. /**
  2171. * lookup_one_len - filesystem helper to lookup single pathname component
  2172. * @name: pathname component to lookup
  2173. * @base: base directory to lookup from
  2174. * @len: maximum length @len should be interpreted to
  2175. *
  2176. * Note that this routine is purely a helper for filesystem usage and should
  2177. * not be called by generic code.
  2178. *
  2179. * The caller must hold base->i_mutex.
  2180. */
  2181. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  2182. {
  2183. struct qstr this;
  2184. unsigned int c;
  2185. int err;
  2186. WARN_ON_ONCE(!inode_is_locked(base->d_inode));
  2187. this.name = name;
  2188. this.len = len;
  2189. this.hash = full_name_hash(base, name, len);
  2190. if (!len)
  2191. return ERR_PTR(-EACCES);
  2192. if (unlikely(name[0] == '.')) {
  2193. if (len < 2 || (len == 2 && name[1] == '.'))
  2194. return ERR_PTR(-EACCES);
  2195. }
  2196. while (len--) {
  2197. c = *(const unsigned char *)name++;
  2198. if (c == '/' || c == '\0')
  2199. return ERR_PTR(-EACCES);
  2200. }
  2201. /*
  2202. * See if the low-level filesystem might want
  2203. * to use its own hash..
  2204. */
  2205. if (base->d_flags & DCACHE_OP_HASH) {
  2206. int err = base->d_op->d_hash(base, &this);
  2207. if (err < 0)
  2208. return ERR_PTR(err);
  2209. }
  2210. err = inode_permission(base->d_inode, MAY_EXEC);
  2211. if (err)
  2212. return ERR_PTR(err);
  2213. return __lookup_hash(&this, base, 0);
  2214. }
  2215. EXPORT_SYMBOL(lookup_one_len);
  2216. /**
  2217. * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
  2218. * @name: pathname component to lookup
  2219. * @base: base directory to lookup from
  2220. * @len: maximum length @len should be interpreted to
  2221. *
  2222. * Note that this routine is purely a helper for filesystem usage and should
  2223. * not be called by generic code.
  2224. *
  2225. * Unlike lookup_one_len, it should be called without the parent
  2226. * i_mutex held, and will take the i_mutex itself if necessary.
  2227. */
  2228. struct dentry *lookup_one_len_unlocked(const char *name,
  2229. struct dentry *base, int len)
  2230. {
  2231. struct qstr this;
  2232. unsigned int c;
  2233. int err;
  2234. struct dentry *ret;
  2235. this.name = name;
  2236. this.len = len;
  2237. this.hash = full_name_hash(base, name, len);
  2238. if (!len)
  2239. return ERR_PTR(-EACCES);
  2240. if (unlikely(name[0] == '.')) {
  2241. if (len < 2 || (len == 2 && name[1] == '.'))
  2242. return ERR_PTR(-EACCES);
  2243. }
  2244. while (len--) {
  2245. c = *(const unsigned char *)name++;
  2246. if (c == '/' || c == '\0')
  2247. return ERR_PTR(-EACCES);
  2248. }
  2249. /*
  2250. * See if the low-level filesystem might want
  2251. * to use its own hash..
  2252. */
  2253. if (base->d_flags & DCACHE_OP_HASH) {
  2254. int err = base->d_op->d_hash(base, &this);
  2255. if (err < 0)
  2256. return ERR_PTR(err);
  2257. }
  2258. err = inode_permission(base->d_inode, MAY_EXEC);
  2259. if (err)
  2260. return ERR_PTR(err);
  2261. ret = lookup_dcache(&this, base, 0);
  2262. if (!ret)
  2263. ret = lookup_slow(&this, base, 0);
  2264. return ret;
  2265. }
  2266. EXPORT_SYMBOL(lookup_one_len_unlocked);
  2267. #ifdef CONFIG_UNIX98_PTYS
  2268. int path_pts(struct path *path)
  2269. {
  2270. /* Find something mounted on "pts" in the same directory as
  2271. * the input path.
  2272. */
  2273. struct dentry *child, *parent;
  2274. struct qstr this;
  2275. int ret;
  2276. ret = path_parent_directory(path);
  2277. if (ret)
  2278. return ret;
  2279. parent = path->dentry;
  2280. this.name = "pts";
  2281. this.len = 3;
  2282. child = d_hash_and_lookup(parent, &this);
  2283. if (!child)
  2284. return -ENOENT;
  2285. path->dentry = child;
  2286. dput(parent);
  2287. follow_mount(path);
  2288. return 0;
  2289. }
  2290. #endif
  2291. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  2292. struct path *path, int *empty)
  2293. {
  2294. return filename_lookup(dfd, getname_flags(name, flags, empty),
  2295. flags, path, NULL);
  2296. }
  2297. EXPORT_SYMBOL(user_path_at_empty);
  2298. /**
  2299. * mountpoint_last - look up last component for umount
  2300. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  2301. *
  2302. * This is a special lookup_last function just for umount. In this case, we
  2303. * need to resolve the path without doing any revalidation.
  2304. *
  2305. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  2306. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  2307. * in almost all cases, this lookup will be served out of the dcache. The only
  2308. * cases where it won't are if nd->last refers to a symlink or the path is
  2309. * bogus and it doesn't exist.
  2310. *
  2311. * Returns:
  2312. * -error: if there was an error during lookup. This includes -ENOENT if the
  2313. * lookup found a negative dentry.
  2314. *
  2315. * 0: if we successfully resolved nd->last and found it to not to be a
  2316. * symlink that needs to be followed.
  2317. *
  2318. * 1: if we successfully resolved nd->last and found it to be a symlink
  2319. * that needs to be followed.
  2320. */
  2321. static int
  2322. mountpoint_last(struct nameidata *nd)
  2323. {
  2324. int error = 0;
  2325. struct dentry *dir = nd->path.dentry;
  2326. struct path path;
  2327. /* If we're in rcuwalk, drop out of it to handle last component */
  2328. if (nd->flags & LOOKUP_RCU) {
  2329. if (unlazy_walk(nd))
  2330. return -ECHILD;
  2331. }
  2332. nd->flags &= ~LOOKUP_PARENT;
  2333. if (unlikely(nd->last_type != LAST_NORM)) {
  2334. error = handle_dots(nd, nd->last_type);
  2335. if (error)
  2336. return error;
  2337. path.dentry = dget(nd->path.dentry);
  2338. } else {
  2339. path.dentry = d_lookup(dir, &nd->last);
  2340. if (!path.dentry) {
  2341. /*
  2342. * No cached dentry. Mounted dentries are pinned in the
  2343. * cache, so that means that this dentry is probably
  2344. * a symlink or the path doesn't actually point
  2345. * to a mounted dentry.
  2346. */
  2347. path.dentry = lookup_slow(&nd->last, dir,
  2348. nd->flags | LOOKUP_NO_REVAL);
  2349. if (IS_ERR(path.dentry))
  2350. return PTR_ERR(path.dentry);
  2351. }
  2352. }
  2353. if (d_is_negative(path.dentry)) {
  2354. dput(path.dentry);
  2355. return -ENOENT;
  2356. }
  2357. path.mnt = nd->path.mnt;
  2358. return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
  2359. }
  2360. /**
  2361. * path_mountpoint - look up a path to be umounted
  2362. * @nd: lookup context
  2363. * @flags: lookup flags
  2364. * @path: pointer to container for result
  2365. *
  2366. * Look up the given name, but don't attempt to revalidate the last component.
  2367. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2368. */
  2369. static int
  2370. path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
  2371. {
  2372. const char *s = path_init(nd, flags);
  2373. int err;
  2374. if (IS_ERR(s))
  2375. return PTR_ERR(s);
  2376. while (!(err = link_path_walk(s, nd)) &&
  2377. (err = mountpoint_last(nd)) > 0) {
  2378. s = trailing_symlink(nd);
  2379. if (IS_ERR(s)) {
  2380. err = PTR_ERR(s);
  2381. break;
  2382. }
  2383. }
  2384. if (!err) {
  2385. *path = nd->path;
  2386. nd->path.mnt = NULL;
  2387. nd->path.dentry = NULL;
  2388. follow_mount(path);
  2389. }
  2390. terminate_walk(nd);
  2391. return err;
  2392. }
  2393. static int
  2394. filename_mountpoint(int dfd, struct filename *name, struct path *path,
  2395. unsigned int flags)
  2396. {
  2397. struct nameidata nd;
  2398. int error;
  2399. if (IS_ERR(name))
  2400. return PTR_ERR(name);
  2401. set_nameidata(&nd, dfd, name);
  2402. error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
  2403. if (unlikely(error == -ECHILD))
  2404. error = path_mountpoint(&nd, flags, path);
  2405. if (unlikely(error == -ESTALE))
  2406. error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
  2407. if (likely(!error))
  2408. audit_inode(name, path->dentry, 0);
  2409. restore_nameidata();
  2410. putname(name);
  2411. return error;
  2412. }
  2413. /**
  2414. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2415. * @dfd: directory file descriptor
  2416. * @name: pathname from userland
  2417. * @flags: lookup flags
  2418. * @path: pointer to container to hold result
  2419. *
  2420. * A umount is a special case for path walking. We're not actually interested
  2421. * in the inode in this situation, and ESTALE errors can be a problem. We
  2422. * simply want track down the dentry and vfsmount attached at the mountpoint
  2423. * and avoid revalidating the last component.
  2424. *
  2425. * Returns 0 and populates "path" on success.
  2426. */
  2427. int
  2428. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2429. struct path *path)
  2430. {
  2431. return filename_mountpoint(dfd, getname(name), path, flags);
  2432. }
  2433. int
  2434. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2435. unsigned int flags)
  2436. {
  2437. return filename_mountpoint(dfd, getname_kernel(name), path, flags);
  2438. }
  2439. EXPORT_SYMBOL(kern_path_mountpoint);
  2440. int __check_sticky(struct inode *dir, struct inode *inode)
  2441. {
  2442. kuid_t fsuid = current_fsuid();
  2443. if (uid_eq(inode->i_uid, fsuid))
  2444. return 0;
  2445. if (uid_eq(dir->i_uid, fsuid))
  2446. return 0;
  2447. return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
  2448. }
  2449. EXPORT_SYMBOL(__check_sticky);
  2450. /*
  2451. * Check whether we can remove a link victim from directory dir, check
  2452. * whether the type of victim is right.
  2453. * 1. We can't do it if dir is read-only (done in permission())
  2454. * 2. We should have write and exec permissions on dir
  2455. * 3. We can't remove anything from append-only dir
  2456. * 4. We can't do anything with immutable dir (done in permission())
  2457. * 5. If the sticky bit on dir is set we should either
  2458. * a. be owner of dir, or
  2459. * b. be owner of victim, or
  2460. * c. have CAP_FOWNER capability
  2461. * 6. If the victim is append-only or immutable we can't do antyhing with
  2462. * links pointing to it.
  2463. * 7. If the victim has an unknown uid or gid we can't change the inode.
  2464. * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2465. * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2466. * 10. We can't remove a root or mountpoint.
  2467. * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
  2468. * nfs_async_unlink().
  2469. */
  2470. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2471. {
  2472. struct inode *inode = d_backing_inode(victim);
  2473. int error;
  2474. if (d_is_negative(victim))
  2475. return -ENOENT;
  2476. BUG_ON(!inode);
  2477. BUG_ON(victim->d_parent->d_inode != dir);
  2478. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2479. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2480. if (error)
  2481. return error;
  2482. if (IS_APPEND(dir))
  2483. return -EPERM;
  2484. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2485. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
  2486. return -EPERM;
  2487. if (isdir) {
  2488. if (!d_is_dir(victim))
  2489. return -ENOTDIR;
  2490. if (IS_ROOT(victim))
  2491. return -EBUSY;
  2492. } else if (d_is_dir(victim))
  2493. return -EISDIR;
  2494. if (IS_DEADDIR(dir))
  2495. return -ENOENT;
  2496. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2497. return -EBUSY;
  2498. return 0;
  2499. }
  2500. /* Check whether we can create an object with dentry child in directory
  2501. * dir.
  2502. * 1. We can't do it if child already exists (open has special treatment for
  2503. * this case, but since we are inlined it's OK)
  2504. * 2. We can't do it if dir is read-only (done in permission())
  2505. * 3. We can't do it if the fs can't represent the fsuid or fsgid.
  2506. * 4. We should have write and exec permissions on dir
  2507. * 5. We can't do it if dir is immutable (done in permission())
  2508. */
  2509. static inline int may_create(struct inode *dir, struct dentry *child)
  2510. {
  2511. struct user_namespace *s_user_ns;
  2512. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2513. if (child->d_inode)
  2514. return -EEXIST;
  2515. if (IS_DEADDIR(dir))
  2516. return -ENOENT;
  2517. s_user_ns = dir->i_sb->s_user_ns;
  2518. if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
  2519. !kgid_has_mapping(s_user_ns, current_fsgid()))
  2520. return -EOVERFLOW;
  2521. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2522. }
  2523. /*
  2524. * p1 and p2 should be directories on the same fs.
  2525. */
  2526. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2527. {
  2528. struct dentry *p;
  2529. if (p1 == p2) {
  2530. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2531. return NULL;
  2532. }
  2533. mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
  2534. p = d_ancestor(p2, p1);
  2535. if (p) {
  2536. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
  2537. inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
  2538. return p;
  2539. }
  2540. p = d_ancestor(p1, p2);
  2541. if (p) {
  2542. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2543. inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
  2544. return p;
  2545. }
  2546. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2547. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
  2548. return NULL;
  2549. }
  2550. EXPORT_SYMBOL(lock_rename);
  2551. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2552. {
  2553. inode_unlock(p1->d_inode);
  2554. if (p1 != p2) {
  2555. inode_unlock(p2->d_inode);
  2556. mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
  2557. }
  2558. }
  2559. EXPORT_SYMBOL(unlock_rename);
  2560. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2561. bool want_excl)
  2562. {
  2563. int error = may_create(dir, dentry);
  2564. if (error)
  2565. return error;
  2566. if (!dir->i_op->create)
  2567. return -EACCES; /* shouldn't it be ENOSYS? */
  2568. mode &= S_IALLUGO;
  2569. mode |= S_IFREG;
  2570. error = security_inode_create(dir, dentry, mode);
  2571. if (error)
  2572. return error;
  2573. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2574. if (!error)
  2575. fsnotify_create(dir, dentry);
  2576. return error;
  2577. }
  2578. EXPORT_SYMBOL(vfs_create);
  2579. bool may_open_dev(const struct path *path)
  2580. {
  2581. return !(path->mnt->mnt_flags & MNT_NODEV) &&
  2582. !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
  2583. }
  2584. static int may_open(const struct path *path, int acc_mode, int flag)
  2585. {
  2586. struct dentry *dentry = path->dentry;
  2587. struct inode *inode = dentry->d_inode;
  2588. int error;
  2589. if (!inode)
  2590. return -ENOENT;
  2591. switch (inode->i_mode & S_IFMT) {
  2592. case S_IFLNK:
  2593. return -ELOOP;
  2594. case S_IFDIR:
  2595. if (acc_mode & MAY_WRITE)
  2596. return -EISDIR;
  2597. break;
  2598. case S_IFBLK:
  2599. case S_IFCHR:
  2600. if (!may_open_dev(path))
  2601. return -EACCES;
  2602. /*FALLTHRU*/
  2603. case S_IFIFO:
  2604. case S_IFSOCK:
  2605. flag &= ~O_TRUNC;
  2606. break;
  2607. }
  2608. error = inode_permission(inode, MAY_OPEN | acc_mode);
  2609. if (error)
  2610. return error;
  2611. /*
  2612. * An append-only file must be opened in append mode for writing.
  2613. */
  2614. if (IS_APPEND(inode)) {
  2615. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2616. return -EPERM;
  2617. if (flag & O_TRUNC)
  2618. return -EPERM;
  2619. }
  2620. /* O_NOATIME can only be set by the owner or superuser */
  2621. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2622. return -EPERM;
  2623. return 0;
  2624. }
  2625. static int handle_truncate(struct file *filp)
  2626. {
  2627. const struct path *path = &filp->f_path;
  2628. struct inode *inode = path->dentry->d_inode;
  2629. int error = get_write_access(inode);
  2630. if (error)
  2631. return error;
  2632. /*
  2633. * Refuse to truncate files with mandatory locks held on them.
  2634. */
  2635. error = locks_verify_locked(filp);
  2636. if (!error)
  2637. error = security_path_truncate(path);
  2638. if (!error) {
  2639. error = do_truncate(path->dentry, 0,
  2640. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2641. filp);
  2642. }
  2643. put_write_access(inode);
  2644. return error;
  2645. }
  2646. static inline int open_to_namei_flags(int flag)
  2647. {
  2648. if ((flag & O_ACCMODE) == 3)
  2649. flag--;
  2650. return flag;
  2651. }
  2652. static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
  2653. {
  2654. struct user_namespace *s_user_ns;
  2655. int error = security_path_mknod(dir, dentry, mode, 0);
  2656. if (error)
  2657. return error;
  2658. s_user_ns = dir->dentry->d_sb->s_user_ns;
  2659. if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
  2660. !kgid_has_mapping(s_user_ns, current_fsgid()))
  2661. return -EOVERFLOW;
  2662. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2663. if (error)
  2664. return error;
  2665. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2666. }
  2667. /*
  2668. * Attempt to atomically look up, create and open a file from a negative
  2669. * dentry.
  2670. *
  2671. * Returns 0 if successful. The file will have been created and attached to
  2672. * @file by the filesystem calling finish_open().
  2673. *
  2674. * Returns 1 if the file was looked up only or didn't need creating. The
  2675. * caller will need to perform the open themselves. @path will have been
  2676. * updated to point to the new dentry. This may be negative.
  2677. *
  2678. * Returns an error code otherwise.
  2679. */
  2680. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2681. struct path *path, struct file *file,
  2682. const struct open_flags *op,
  2683. int open_flag, umode_t mode,
  2684. int *opened)
  2685. {
  2686. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2687. struct inode *dir = nd->path.dentry->d_inode;
  2688. int error;
  2689. if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
  2690. open_flag &= ~O_TRUNC;
  2691. if (nd->flags & LOOKUP_DIRECTORY)
  2692. open_flag |= O_DIRECTORY;
  2693. file->f_path.dentry = DENTRY_NOT_SET;
  2694. file->f_path.mnt = nd->path.mnt;
  2695. error = dir->i_op->atomic_open(dir, dentry, file,
  2696. open_to_namei_flags(open_flag),
  2697. mode, opened);
  2698. d_lookup_done(dentry);
  2699. if (!error) {
  2700. /*
  2701. * We didn't have the inode before the open, so check open
  2702. * permission here.
  2703. */
  2704. int acc_mode = op->acc_mode;
  2705. if (*opened & FILE_CREATED) {
  2706. WARN_ON(!(open_flag & O_CREAT));
  2707. fsnotify_create(dir, dentry);
  2708. acc_mode = 0;
  2709. }
  2710. error = may_open(&file->f_path, acc_mode, open_flag);
  2711. if (WARN_ON(error > 0))
  2712. error = -EINVAL;
  2713. } else if (error > 0) {
  2714. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2715. error = -EIO;
  2716. } else {
  2717. if (file->f_path.dentry) {
  2718. dput(dentry);
  2719. dentry = file->f_path.dentry;
  2720. }
  2721. if (*opened & FILE_CREATED)
  2722. fsnotify_create(dir, dentry);
  2723. if (unlikely(d_is_negative(dentry))) {
  2724. error = -ENOENT;
  2725. } else {
  2726. path->dentry = dentry;
  2727. path->mnt = nd->path.mnt;
  2728. return 1;
  2729. }
  2730. }
  2731. }
  2732. dput(dentry);
  2733. return error;
  2734. }
  2735. /*
  2736. * Look up and maybe create and open the last component.
  2737. *
  2738. * Must be called with i_mutex held on parent.
  2739. *
  2740. * Returns 0 if the file was successfully atomically created (if necessary) and
  2741. * opened. In this case the file will be returned attached to @file.
  2742. *
  2743. * Returns 1 if the file was not completely opened at this time, though lookups
  2744. * and creations will have been performed and the dentry returned in @path will
  2745. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2746. * specified then a negative dentry may be returned.
  2747. *
  2748. * An error code is returned otherwise.
  2749. *
  2750. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2751. * cleared otherwise prior to returning.
  2752. */
  2753. static int lookup_open(struct nameidata *nd, struct path *path,
  2754. struct file *file,
  2755. const struct open_flags *op,
  2756. bool got_write, int *opened)
  2757. {
  2758. struct dentry *dir = nd->path.dentry;
  2759. struct inode *dir_inode = dir->d_inode;
  2760. int open_flag = op->open_flag;
  2761. struct dentry *dentry;
  2762. int error, create_error = 0;
  2763. umode_t mode = op->mode;
  2764. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  2765. if (unlikely(IS_DEADDIR(dir_inode)))
  2766. return -ENOENT;
  2767. *opened &= ~FILE_CREATED;
  2768. dentry = d_lookup(dir, &nd->last);
  2769. for (;;) {
  2770. if (!dentry) {
  2771. dentry = d_alloc_parallel(dir, &nd->last, &wq);
  2772. if (IS_ERR(dentry))
  2773. return PTR_ERR(dentry);
  2774. }
  2775. if (d_in_lookup(dentry))
  2776. break;
  2777. error = d_revalidate(dentry, nd->flags);
  2778. if (likely(error > 0))
  2779. break;
  2780. if (error)
  2781. goto out_dput;
  2782. d_invalidate(dentry);
  2783. dput(dentry);
  2784. dentry = NULL;
  2785. }
  2786. if (dentry->d_inode) {
  2787. /* Cached positive dentry: will open in f_op->open */
  2788. goto out_no_open;
  2789. }
  2790. /*
  2791. * Checking write permission is tricky, bacuse we don't know if we are
  2792. * going to actually need it: O_CREAT opens should work as long as the
  2793. * file exists. But checking existence breaks atomicity. The trick is
  2794. * to check access and if not granted clear O_CREAT from the flags.
  2795. *
  2796. * Another problem is returing the "right" error value (e.g. for an
  2797. * O_EXCL open we want to return EEXIST not EROFS).
  2798. */
  2799. if (open_flag & O_CREAT) {
  2800. if (!IS_POSIXACL(dir->d_inode))
  2801. mode &= ~current_umask();
  2802. if (unlikely(!got_write)) {
  2803. create_error = -EROFS;
  2804. open_flag &= ~O_CREAT;
  2805. if (open_flag & (O_EXCL | O_TRUNC))
  2806. goto no_open;
  2807. /* No side effects, safe to clear O_CREAT */
  2808. } else {
  2809. create_error = may_o_create(&nd->path, dentry, mode);
  2810. if (create_error) {
  2811. open_flag &= ~O_CREAT;
  2812. if (open_flag & O_EXCL)
  2813. goto no_open;
  2814. }
  2815. }
  2816. } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
  2817. unlikely(!got_write)) {
  2818. /*
  2819. * No O_CREATE -> atomicity not a requirement -> fall
  2820. * back to lookup + open
  2821. */
  2822. goto no_open;
  2823. }
  2824. if (dir_inode->i_op->atomic_open) {
  2825. error = atomic_open(nd, dentry, path, file, op, open_flag,
  2826. mode, opened);
  2827. if (unlikely(error == -ENOENT) && create_error)
  2828. error = create_error;
  2829. return error;
  2830. }
  2831. no_open:
  2832. if (d_in_lookup(dentry)) {
  2833. struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
  2834. nd->flags);
  2835. d_lookup_done(dentry);
  2836. if (unlikely(res)) {
  2837. if (IS_ERR(res)) {
  2838. error = PTR_ERR(res);
  2839. goto out_dput;
  2840. }
  2841. dput(dentry);
  2842. dentry = res;
  2843. }
  2844. }
  2845. /* Negative dentry, just create the file */
  2846. if (!dentry->d_inode && (open_flag & O_CREAT)) {
  2847. *opened |= FILE_CREATED;
  2848. audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  2849. if (!dir_inode->i_op->create) {
  2850. error = -EACCES;
  2851. goto out_dput;
  2852. }
  2853. error = dir_inode->i_op->create(dir_inode, dentry, mode,
  2854. open_flag & O_EXCL);
  2855. if (error)
  2856. goto out_dput;
  2857. fsnotify_create(dir_inode, dentry);
  2858. }
  2859. if (unlikely(create_error) && !dentry->d_inode) {
  2860. error = create_error;
  2861. goto out_dput;
  2862. }
  2863. out_no_open:
  2864. path->dentry = dentry;
  2865. path->mnt = nd->path.mnt;
  2866. return 1;
  2867. out_dput:
  2868. dput(dentry);
  2869. return error;
  2870. }
  2871. /*
  2872. * Handle the last step of open()
  2873. */
  2874. static int do_last(struct nameidata *nd,
  2875. struct file *file, const struct open_flags *op,
  2876. int *opened)
  2877. {
  2878. struct dentry *dir = nd->path.dentry;
  2879. int open_flag = op->open_flag;
  2880. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2881. bool got_write = false;
  2882. int acc_mode = op->acc_mode;
  2883. unsigned seq;
  2884. struct inode *inode;
  2885. struct path path;
  2886. int error;
  2887. nd->flags &= ~LOOKUP_PARENT;
  2888. nd->flags |= op->intent;
  2889. if (nd->last_type != LAST_NORM) {
  2890. error = handle_dots(nd, nd->last_type);
  2891. if (unlikely(error))
  2892. return error;
  2893. goto finish_open;
  2894. }
  2895. if (!(open_flag & O_CREAT)) {
  2896. if (nd->last.name[nd->last.len])
  2897. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2898. /* we _can_ be in RCU mode here */
  2899. error = lookup_fast(nd, &path, &inode, &seq);
  2900. if (likely(error > 0))
  2901. goto finish_lookup;
  2902. if (error < 0)
  2903. return error;
  2904. BUG_ON(nd->inode != dir->d_inode);
  2905. BUG_ON(nd->flags & LOOKUP_RCU);
  2906. } else {
  2907. /* create side of things */
  2908. /*
  2909. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2910. * has been cleared when we got to the last component we are
  2911. * about to look up
  2912. */
  2913. error = complete_walk(nd);
  2914. if (error)
  2915. return error;
  2916. audit_inode(nd->name, dir, LOOKUP_PARENT);
  2917. /* trailing slashes? */
  2918. if (unlikely(nd->last.name[nd->last.len]))
  2919. return -EISDIR;
  2920. }
  2921. if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2922. error = mnt_want_write(nd->path.mnt);
  2923. if (!error)
  2924. got_write = true;
  2925. /*
  2926. * do _not_ fail yet - we might not need that or fail with
  2927. * a different error; let lookup_open() decide; we'll be
  2928. * dropping this one anyway.
  2929. */
  2930. }
  2931. if (open_flag & O_CREAT)
  2932. inode_lock(dir->d_inode);
  2933. else
  2934. inode_lock_shared(dir->d_inode);
  2935. error = lookup_open(nd, &path, file, op, got_write, opened);
  2936. if (open_flag & O_CREAT)
  2937. inode_unlock(dir->d_inode);
  2938. else
  2939. inode_unlock_shared(dir->d_inode);
  2940. if (error <= 0) {
  2941. if (error)
  2942. goto out;
  2943. if ((*opened & FILE_CREATED) ||
  2944. !S_ISREG(file_inode(file)->i_mode))
  2945. will_truncate = false;
  2946. audit_inode(nd->name, file->f_path.dentry, 0);
  2947. goto opened;
  2948. }
  2949. if (*opened & FILE_CREATED) {
  2950. /* Don't check for write permission, don't truncate */
  2951. open_flag &= ~O_TRUNC;
  2952. will_truncate = false;
  2953. acc_mode = 0;
  2954. path_to_nameidata(&path, nd);
  2955. goto finish_open_created;
  2956. }
  2957. /*
  2958. * If atomic_open() acquired write access it is dropped now due to
  2959. * possible mount and symlink following (this might be optimized away if
  2960. * necessary...)
  2961. */
  2962. if (got_write) {
  2963. mnt_drop_write(nd->path.mnt);
  2964. got_write = false;
  2965. }
  2966. error = follow_managed(&path, nd);
  2967. if (unlikely(error < 0))
  2968. return error;
  2969. if (unlikely(d_is_negative(path.dentry))) {
  2970. path_to_nameidata(&path, nd);
  2971. return -ENOENT;
  2972. }
  2973. /*
  2974. * create/update audit record if it already exists.
  2975. */
  2976. audit_inode(nd->name, path.dentry, 0);
  2977. if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
  2978. path_to_nameidata(&path, nd);
  2979. return -EEXIST;
  2980. }
  2981. seq = 0; /* out of RCU mode, so the value doesn't matter */
  2982. inode = d_backing_inode(path.dentry);
  2983. finish_lookup:
  2984. error = step_into(nd, &path, 0, inode, seq);
  2985. if (unlikely(error))
  2986. return error;
  2987. finish_open:
  2988. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2989. error = complete_walk(nd);
  2990. if (error)
  2991. return error;
  2992. audit_inode(nd->name, nd->path.dentry, 0);
  2993. error = -EISDIR;
  2994. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  2995. goto out;
  2996. error = -ENOTDIR;
  2997. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  2998. goto out;
  2999. if (!d_is_reg(nd->path.dentry))
  3000. will_truncate = false;
  3001. if (will_truncate) {
  3002. error = mnt_want_write(nd->path.mnt);
  3003. if (error)
  3004. goto out;
  3005. got_write = true;
  3006. }
  3007. finish_open_created:
  3008. error = may_open(&nd->path, acc_mode, open_flag);
  3009. if (error)
  3010. goto out;
  3011. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  3012. error = vfs_open(&nd->path, file, current_cred());
  3013. if (error)
  3014. goto out;
  3015. *opened |= FILE_OPENED;
  3016. opened:
  3017. error = open_check_o_direct(file);
  3018. if (!error)
  3019. error = ima_file_check(file, op->acc_mode, *opened);
  3020. if (!error && will_truncate)
  3021. error = handle_truncate(file);
  3022. out:
  3023. if (unlikely(error) && (*opened & FILE_OPENED))
  3024. fput(file);
  3025. if (unlikely(error > 0)) {
  3026. WARN_ON(1);
  3027. error = -EINVAL;
  3028. }
  3029. if (got_write)
  3030. mnt_drop_write(nd->path.mnt);
  3031. return error;
  3032. }
  3033. struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
  3034. {
  3035. static const struct qstr name = QSTR_INIT("/", 1);
  3036. struct dentry *child = NULL;
  3037. struct inode *dir = dentry->d_inode;
  3038. struct inode *inode;
  3039. int error;
  3040. /* we want directory to be writable */
  3041. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  3042. if (error)
  3043. goto out_err;
  3044. error = -EOPNOTSUPP;
  3045. if (!dir->i_op->tmpfile)
  3046. goto out_err;
  3047. error = -ENOMEM;
  3048. child = d_alloc(dentry, &name);
  3049. if (unlikely(!child))
  3050. goto out_err;
  3051. error = dir->i_op->tmpfile(dir, child, mode);
  3052. if (error)
  3053. goto out_err;
  3054. error = -ENOENT;
  3055. inode = child->d_inode;
  3056. if (unlikely(!inode))
  3057. goto out_err;
  3058. if (!(open_flag & O_EXCL)) {
  3059. spin_lock(&inode->i_lock);
  3060. inode->i_state |= I_LINKABLE;
  3061. spin_unlock(&inode->i_lock);
  3062. }
  3063. return child;
  3064. out_err:
  3065. dput(child);
  3066. return ERR_PTR(error);
  3067. }
  3068. EXPORT_SYMBOL(vfs_tmpfile);
  3069. static int do_tmpfile(struct nameidata *nd, unsigned flags,
  3070. const struct open_flags *op,
  3071. struct file *file, int *opened)
  3072. {
  3073. struct dentry *child;
  3074. struct path path;
  3075. int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
  3076. if (unlikely(error))
  3077. return error;
  3078. error = mnt_want_write(path.mnt);
  3079. if (unlikely(error))
  3080. goto out;
  3081. child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
  3082. error = PTR_ERR(child);
  3083. if (unlikely(IS_ERR(child)))
  3084. goto out2;
  3085. dput(path.dentry);
  3086. path.dentry = child;
  3087. audit_inode(nd->name, child, 0);
  3088. /* Don't check for other permissions, the inode was just created */
  3089. error = may_open(&path, 0, op->open_flag);
  3090. if (error)
  3091. goto out2;
  3092. file->f_path.mnt = path.mnt;
  3093. error = finish_open(file, child, NULL, opened);
  3094. if (error)
  3095. goto out2;
  3096. error = open_check_o_direct(file);
  3097. if (error)
  3098. fput(file);
  3099. out2:
  3100. mnt_drop_write(path.mnt);
  3101. out:
  3102. path_put(&path);
  3103. return error;
  3104. }
  3105. static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
  3106. {
  3107. struct path path;
  3108. int error = path_lookupat(nd, flags, &path);
  3109. if (!error) {
  3110. audit_inode(nd->name, path.dentry, 0);
  3111. error = vfs_open(&path, file, current_cred());
  3112. path_put(&path);
  3113. }
  3114. return error;
  3115. }
  3116. static struct file *path_openat(struct nameidata *nd,
  3117. const struct open_flags *op, unsigned flags)
  3118. {
  3119. const char *s;
  3120. struct file *file;
  3121. int opened = 0;
  3122. int error;
  3123. file = get_empty_filp();
  3124. if (IS_ERR(file))
  3125. return file;
  3126. file->f_flags = op->open_flag;
  3127. if (unlikely(file->f_flags & __O_TMPFILE)) {
  3128. error = do_tmpfile(nd, flags, op, file, &opened);
  3129. goto out2;
  3130. }
  3131. if (unlikely(file->f_flags & O_PATH)) {
  3132. error = do_o_path(nd, flags, file);
  3133. if (!error)
  3134. opened |= FILE_OPENED;
  3135. goto out2;
  3136. }
  3137. s = path_init(nd, flags);
  3138. if (IS_ERR(s)) {
  3139. put_filp(file);
  3140. return ERR_CAST(s);
  3141. }
  3142. while (!(error = link_path_walk(s, nd)) &&
  3143. (error = do_last(nd, file, op, &opened)) > 0) {
  3144. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  3145. s = trailing_symlink(nd);
  3146. if (IS_ERR(s)) {
  3147. error = PTR_ERR(s);
  3148. break;
  3149. }
  3150. }
  3151. terminate_walk(nd);
  3152. out2:
  3153. if (!(opened & FILE_OPENED)) {
  3154. BUG_ON(!error);
  3155. put_filp(file);
  3156. }
  3157. if (unlikely(error)) {
  3158. if (error == -EOPENSTALE) {
  3159. if (flags & LOOKUP_RCU)
  3160. error = -ECHILD;
  3161. else
  3162. error = -ESTALE;
  3163. }
  3164. file = ERR_PTR(error);
  3165. }
  3166. return file;
  3167. }
  3168. struct file *do_filp_open(int dfd, struct filename *pathname,
  3169. const struct open_flags *op)
  3170. {
  3171. struct nameidata nd;
  3172. int flags = op->lookup_flags;
  3173. struct file *filp;
  3174. set_nameidata(&nd, dfd, pathname);
  3175. filp = path_openat(&nd, op, flags | LOOKUP_RCU);
  3176. if (unlikely(filp == ERR_PTR(-ECHILD)))
  3177. filp = path_openat(&nd, op, flags);
  3178. if (unlikely(filp == ERR_PTR(-ESTALE)))
  3179. filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3180. restore_nameidata();
  3181. return filp;
  3182. }
  3183. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  3184. const char *name, const struct open_flags *op)
  3185. {
  3186. struct nameidata nd;
  3187. struct file *file;
  3188. struct filename *filename;
  3189. int flags = op->lookup_flags | LOOKUP_ROOT;
  3190. nd.root.mnt = mnt;
  3191. nd.root.dentry = dentry;
  3192. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  3193. return ERR_PTR(-ELOOP);
  3194. filename = getname_kernel(name);
  3195. if (IS_ERR(filename))
  3196. return ERR_CAST(filename);
  3197. set_nameidata(&nd, -1, filename);
  3198. file = path_openat(&nd, op, flags | LOOKUP_RCU);
  3199. if (unlikely(file == ERR_PTR(-ECHILD)))
  3200. file = path_openat(&nd, op, flags);
  3201. if (unlikely(file == ERR_PTR(-ESTALE)))
  3202. file = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3203. restore_nameidata();
  3204. putname(filename);
  3205. return file;
  3206. }
  3207. static struct dentry *filename_create(int dfd, struct filename *name,
  3208. struct path *path, unsigned int lookup_flags)
  3209. {
  3210. struct dentry *dentry = ERR_PTR(-EEXIST);
  3211. struct qstr last;
  3212. int type;
  3213. int err2;
  3214. int error;
  3215. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  3216. /*
  3217. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  3218. * other flags passed in are ignored!
  3219. */
  3220. lookup_flags &= LOOKUP_REVAL;
  3221. name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
  3222. if (IS_ERR(name))
  3223. return ERR_CAST(name);
  3224. /*
  3225. * Yucky last component or no last component at all?
  3226. * (foo/., foo/.., /////)
  3227. */
  3228. if (unlikely(type != LAST_NORM))
  3229. goto out;
  3230. /* don't fail immediately if it's r/o, at least try to report other errors */
  3231. err2 = mnt_want_write(path->mnt);
  3232. /*
  3233. * Do the final lookup.
  3234. */
  3235. lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  3236. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  3237. dentry = __lookup_hash(&last, path->dentry, lookup_flags);
  3238. if (IS_ERR(dentry))
  3239. goto unlock;
  3240. error = -EEXIST;
  3241. if (d_is_positive(dentry))
  3242. goto fail;
  3243. /*
  3244. * Special case - lookup gave negative, but... we had foo/bar/
  3245. * From the vfs_mknod() POV we just have a negative dentry -
  3246. * all is fine. Let's be bastards - you had / on the end, you've
  3247. * been asking for (non-existent) directory. -ENOENT for you.
  3248. */
  3249. if (unlikely(!is_dir && last.name[last.len])) {
  3250. error = -ENOENT;
  3251. goto fail;
  3252. }
  3253. if (unlikely(err2)) {
  3254. error = err2;
  3255. goto fail;
  3256. }
  3257. putname(name);
  3258. return dentry;
  3259. fail:
  3260. dput(dentry);
  3261. dentry = ERR_PTR(error);
  3262. unlock:
  3263. inode_unlock(path->dentry->d_inode);
  3264. if (!err2)
  3265. mnt_drop_write(path->mnt);
  3266. out:
  3267. path_put(path);
  3268. putname(name);
  3269. return dentry;
  3270. }
  3271. struct dentry *kern_path_create(int dfd, const char *pathname,
  3272. struct path *path, unsigned int lookup_flags)
  3273. {
  3274. return filename_create(dfd, getname_kernel(pathname),
  3275. path, lookup_flags);
  3276. }
  3277. EXPORT_SYMBOL(kern_path_create);
  3278. void done_path_create(struct path *path, struct dentry *dentry)
  3279. {
  3280. dput(dentry);
  3281. inode_unlock(path->dentry->d_inode);
  3282. mnt_drop_write(path->mnt);
  3283. path_put(path);
  3284. }
  3285. EXPORT_SYMBOL(done_path_create);
  3286. inline struct dentry *user_path_create(int dfd, const char __user *pathname,
  3287. struct path *path, unsigned int lookup_flags)
  3288. {
  3289. return filename_create(dfd, getname(pathname), path, lookup_flags);
  3290. }
  3291. EXPORT_SYMBOL(user_path_create);
  3292. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3293. {
  3294. int error = may_create(dir, dentry);
  3295. if (error)
  3296. return error;
  3297. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3298. return -EPERM;
  3299. if (!dir->i_op->mknod)
  3300. return -EPERM;
  3301. error = devcgroup_inode_mknod(mode, dev);
  3302. if (error)
  3303. return error;
  3304. error = security_inode_mknod(dir, dentry, mode, dev);
  3305. if (error)
  3306. return error;
  3307. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3308. if (!error)
  3309. fsnotify_create(dir, dentry);
  3310. return error;
  3311. }
  3312. EXPORT_SYMBOL(vfs_mknod);
  3313. static int may_mknod(umode_t mode)
  3314. {
  3315. switch (mode & S_IFMT) {
  3316. case S_IFREG:
  3317. case S_IFCHR:
  3318. case S_IFBLK:
  3319. case S_IFIFO:
  3320. case S_IFSOCK:
  3321. case 0: /* zero mode translates to S_IFREG */
  3322. return 0;
  3323. case S_IFDIR:
  3324. return -EPERM;
  3325. default:
  3326. return -EINVAL;
  3327. }
  3328. }
  3329. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3330. unsigned, dev)
  3331. {
  3332. struct dentry *dentry;
  3333. struct path path;
  3334. int error;
  3335. unsigned int lookup_flags = 0;
  3336. error = may_mknod(mode);
  3337. if (error)
  3338. return error;
  3339. retry:
  3340. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3341. if (IS_ERR(dentry))
  3342. return PTR_ERR(dentry);
  3343. if (!IS_POSIXACL(path.dentry->d_inode))
  3344. mode &= ~current_umask();
  3345. error = security_path_mknod(&path, dentry, mode, dev);
  3346. if (error)
  3347. goto out;
  3348. switch (mode & S_IFMT) {
  3349. case 0: case S_IFREG:
  3350. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3351. if (!error)
  3352. ima_post_path_mknod(dentry);
  3353. break;
  3354. case S_IFCHR: case S_IFBLK:
  3355. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3356. new_decode_dev(dev));
  3357. break;
  3358. case S_IFIFO: case S_IFSOCK:
  3359. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3360. break;
  3361. }
  3362. out:
  3363. done_path_create(&path, dentry);
  3364. if (retry_estale(error, lookup_flags)) {
  3365. lookup_flags |= LOOKUP_REVAL;
  3366. goto retry;
  3367. }
  3368. return error;
  3369. }
  3370. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3371. {
  3372. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3373. }
  3374. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3375. {
  3376. int error = may_create(dir, dentry);
  3377. unsigned max_links = dir->i_sb->s_max_links;
  3378. if (error)
  3379. return error;
  3380. if (!dir->i_op->mkdir)
  3381. return -EPERM;
  3382. mode &= (S_IRWXUGO|S_ISVTX);
  3383. error = security_inode_mkdir(dir, dentry, mode);
  3384. if (error)
  3385. return error;
  3386. if (max_links && dir->i_nlink >= max_links)
  3387. return -EMLINK;
  3388. error = dir->i_op->mkdir(dir, dentry, mode);
  3389. if (!error)
  3390. fsnotify_mkdir(dir, dentry);
  3391. return error;
  3392. }
  3393. EXPORT_SYMBOL(vfs_mkdir);
  3394. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3395. {
  3396. struct dentry *dentry;
  3397. struct path path;
  3398. int error;
  3399. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3400. retry:
  3401. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3402. if (IS_ERR(dentry))
  3403. return PTR_ERR(dentry);
  3404. if (!IS_POSIXACL(path.dentry->d_inode))
  3405. mode &= ~current_umask();
  3406. error = security_path_mkdir(&path, dentry, mode);
  3407. if (!error)
  3408. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3409. done_path_create(&path, dentry);
  3410. if (retry_estale(error, lookup_flags)) {
  3411. lookup_flags |= LOOKUP_REVAL;
  3412. goto retry;
  3413. }
  3414. return error;
  3415. }
  3416. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3417. {
  3418. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3419. }
  3420. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3421. {
  3422. int error = may_delete(dir, dentry, 1);
  3423. if (error)
  3424. return error;
  3425. if (!dir->i_op->rmdir)
  3426. return -EPERM;
  3427. dget(dentry);
  3428. inode_lock(dentry->d_inode);
  3429. error = -EBUSY;
  3430. if (is_local_mountpoint(dentry))
  3431. goto out;
  3432. error = security_inode_rmdir(dir, dentry);
  3433. if (error)
  3434. goto out;
  3435. shrink_dcache_parent(dentry);
  3436. error = dir->i_op->rmdir(dir, dentry);
  3437. if (error)
  3438. goto out;
  3439. dentry->d_inode->i_flags |= S_DEAD;
  3440. dont_mount(dentry);
  3441. detach_mounts(dentry);
  3442. out:
  3443. inode_unlock(dentry->d_inode);
  3444. dput(dentry);
  3445. if (!error)
  3446. d_delete(dentry);
  3447. return error;
  3448. }
  3449. EXPORT_SYMBOL(vfs_rmdir);
  3450. static long do_rmdir(int dfd, const char __user *pathname)
  3451. {
  3452. int error = 0;
  3453. struct filename *name;
  3454. struct dentry *dentry;
  3455. struct path path;
  3456. struct qstr last;
  3457. int type;
  3458. unsigned int lookup_flags = 0;
  3459. retry:
  3460. name = filename_parentat(dfd, getname(pathname), lookup_flags,
  3461. &path, &last, &type);
  3462. if (IS_ERR(name))
  3463. return PTR_ERR(name);
  3464. switch (type) {
  3465. case LAST_DOTDOT:
  3466. error = -ENOTEMPTY;
  3467. goto exit1;
  3468. case LAST_DOT:
  3469. error = -EINVAL;
  3470. goto exit1;
  3471. case LAST_ROOT:
  3472. error = -EBUSY;
  3473. goto exit1;
  3474. }
  3475. error = mnt_want_write(path.mnt);
  3476. if (error)
  3477. goto exit1;
  3478. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3479. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3480. error = PTR_ERR(dentry);
  3481. if (IS_ERR(dentry))
  3482. goto exit2;
  3483. if (!dentry->d_inode) {
  3484. error = -ENOENT;
  3485. goto exit3;
  3486. }
  3487. error = security_path_rmdir(&path, dentry);
  3488. if (error)
  3489. goto exit3;
  3490. error = vfs_rmdir(path.dentry->d_inode, dentry);
  3491. exit3:
  3492. dput(dentry);
  3493. exit2:
  3494. inode_unlock(path.dentry->d_inode);
  3495. mnt_drop_write(path.mnt);
  3496. exit1:
  3497. path_put(&path);
  3498. putname(name);
  3499. if (retry_estale(error, lookup_flags)) {
  3500. lookup_flags |= LOOKUP_REVAL;
  3501. goto retry;
  3502. }
  3503. return error;
  3504. }
  3505. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3506. {
  3507. return do_rmdir(AT_FDCWD, pathname);
  3508. }
  3509. /**
  3510. * vfs_unlink - unlink a filesystem object
  3511. * @dir: parent directory
  3512. * @dentry: victim
  3513. * @delegated_inode: returns victim inode, if the inode is delegated.
  3514. *
  3515. * The caller must hold dir->i_mutex.
  3516. *
  3517. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3518. * return a reference to the inode in delegated_inode. The caller
  3519. * should then break the delegation on that inode and retry. Because
  3520. * breaking a delegation may take a long time, the caller should drop
  3521. * dir->i_mutex before doing so.
  3522. *
  3523. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3524. * be appropriate for callers that expect the underlying filesystem not
  3525. * to be NFS exported.
  3526. */
  3527. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3528. {
  3529. struct inode *target = dentry->d_inode;
  3530. int error = may_delete(dir, dentry, 0);
  3531. if (error)
  3532. return error;
  3533. if (!dir->i_op->unlink)
  3534. return -EPERM;
  3535. inode_lock(target);
  3536. if (is_local_mountpoint(dentry))
  3537. error = -EBUSY;
  3538. else {
  3539. error = security_inode_unlink(dir, dentry);
  3540. if (!error) {
  3541. error = try_break_deleg(target, delegated_inode);
  3542. if (error)
  3543. goto out;
  3544. error = dir->i_op->unlink(dir, dentry);
  3545. if (!error) {
  3546. dont_mount(dentry);
  3547. detach_mounts(dentry);
  3548. }
  3549. }
  3550. }
  3551. out:
  3552. inode_unlock(target);
  3553. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3554. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3555. fsnotify_link_count(target);
  3556. d_delete(dentry);
  3557. }
  3558. return error;
  3559. }
  3560. EXPORT_SYMBOL(vfs_unlink);
  3561. /*
  3562. * Make sure that the actual truncation of the file will occur outside its
  3563. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3564. * writeout happening, and we don't want to prevent access to the directory
  3565. * while waiting on the I/O.
  3566. */
  3567. static long do_unlinkat(int dfd, const char __user *pathname)
  3568. {
  3569. int error;
  3570. struct filename *name;
  3571. struct dentry *dentry;
  3572. struct path path;
  3573. struct qstr last;
  3574. int type;
  3575. struct inode *inode = NULL;
  3576. struct inode *delegated_inode = NULL;
  3577. unsigned int lookup_flags = 0;
  3578. retry:
  3579. name = filename_parentat(dfd, getname(pathname), lookup_flags,
  3580. &path, &last, &type);
  3581. if (IS_ERR(name))
  3582. return PTR_ERR(name);
  3583. error = -EISDIR;
  3584. if (type != LAST_NORM)
  3585. goto exit1;
  3586. error = mnt_want_write(path.mnt);
  3587. if (error)
  3588. goto exit1;
  3589. retry_deleg:
  3590. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3591. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3592. error = PTR_ERR(dentry);
  3593. if (!IS_ERR(dentry)) {
  3594. /* Why not before? Because we want correct error value */
  3595. if (last.name[last.len])
  3596. goto slashes;
  3597. inode = dentry->d_inode;
  3598. if (d_is_negative(dentry))
  3599. goto slashes;
  3600. ihold(inode);
  3601. error = security_path_unlink(&path, dentry);
  3602. if (error)
  3603. goto exit2;
  3604. error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
  3605. exit2:
  3606. dput(dentry);
  3607. }
  3608. inode_unlock(path.dentry->d_inode);
  3609. if (inode)
  3610. iput(inode); /* truncate the inode here */
  3611. inode = NULL;
  3612. if (delegated_inode) {
  3613. error = break_deleg_wait(&delegated_inode);
  3614. if (!error)
  3615. goto retry_deleg;
  3616. }
  3617. mnt_drop_write(path.mnt);
  3618. exit1:
  3619. path_put(&path);
  3620. putname(name);
  3621. if (retry_estale(error, lookup_flags)) {
  3622. lookup_flags |= LOOKUP_REVAL;
  3623. inode = NULL;
  3624. goto retry;
  3625. }
  3626. return error;
  3627. slashes:
  3628. if (d_is_negative(dentry))
  3629. error = -ENOENT;
  3630. else if (d_is_dir(dentry))
  3631. error = -EISDIR;
  3632. else
  3633. error = -ENOTDIR;
  3634. goto exit2;
  3635. }
  3636. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3637. {
  3638. if ((flag & ~AT_REMOVEDIR) != 0)
  3639. return -EINVAL;
  3640. if (flag & AT_REMOVEDIR)
  3641. return do_rmdir(dfd, pathname);
  3642. return do_unlinkat(dfd, pathname);
  3643. }
  3644. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3645. {
  3646. return do_unlinkat(AT_FDCWD, pathname);
  3647. }
  3648. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3649. {
  3650. int error = may_create(dir, dentry);
  3651. if (error)
  3652. return error;
  3653. if (!dir->i_op->symlink)
  3654. return -EPERM;
  3655. error = security_inode_symlink(dir, dentry, oldname);
  3656. if (error)
  3657. return error;
  3658. error = dir->i_op->symlink(dir, dentry, oldname);
  3659. if (!error)
  3660. fsnotify_create(dir, dentry);
  3661. return error;
  3662. }
  3663. EXPORT_SYMBOL(vfs_symlink);
  3664. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3665. int, newdfd, const char __user *, newname)
  3666. {
  3667. int error;
  3668. struct filename *from;
  3669. struct dentry *dentry;
  3670. struct path path;
  3671. unsigned int lookup_flags = 0;
  3672. from = getname(oldname);
  3673. if (IS_ERR(from))
  3674. return PTR_ERR(from);
  3675. retry:
  3676. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3677. error = PTR_ERR(dentry);
  3678. if (IS_ERR(dentry))
  3679. goto out_putname;
  3680. error = security_path_symlink(&path, dentry, from->name);
  3681. if (!error)
  3682. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3683. done_path_create(&path, dentry);
  3684. if (retry_estale(error, lookup_flags)) {
  3685. lookup_flags |= LOOKUP_REVAL;
  3686. goto retry;
  3687. }
  3688. out_putname:
  3689. putname(from);
  3690. return error;
  3691. }
  3692. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3693. {
  3694. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3695. }
  3696. /**
  3697. * vfs_link - create a new link
  3698. * @old_dentry: object to be linked
  3699. * @dir: new parent
  3700. * @new_dentry: where to create the new link
  3701. * @delegated_inode: returns inode needing a delegation break
  3702. *
  3703. * The caller must hold dir->i_mutex
  3704. *
  3705. * If vfs_link discovers a delegation on the to-be-linked file in need
  3706. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3707. * inode in delegated_inode. The caller should then break the delegation
  3708. * and retry. Because breaking a delegation may take a long time, the
  3709. * caller should drop the i_mutex before doing so.
  3710. *
  3711. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3712. * be appropriate for callers that expect the underlying filesystem not
  3713. * to be NFS exported.
  3714. */
  3715. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3716. {
  3717. struct inode *inode = old_dentry->d_inode;
  3718. unsigned max_links = dir->i_sb->s_max_links;
  3719. int error;
  3720. if (!inode)
  3721. return -ENOENT;
  3722. error = may_create(dir, new_dentry);
  3723. if (error)
  3724. return error;
  3725. if (dir->i_sb != inode->i_sb)
  3726. return -EXDEV;
  3727. /*
  3728. * A link to an append-only or immutable file cannot be created.
  3729. */
  3730. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3731. return -EPERM;
  3732. /*
  3733. * Updating the link count will likely cause i_uid and i_gid to
  3734. * be writen back improperly if their true value is unknown to
  3735. * the vfs.
  3736. */
  3737. if (HAS_UNMAPPED_ID(inode))
  3738. return -EPERM;
  3739. if (!dir->i_op->link)
  3740. return -EPERM;
  3741. if (S_ISDIR(inode->i_mode))
  3742. return -EPERM;
  3743. error = security_inode_link(old_dentry, dir, new_dentry);
  3744. if (error)
  3745. return error;
  3746. inode_lock(inode);
  3747. /* Make sure we don't allow creating hardlink to an unlinked file */
  3748. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3749. error = -ENOENT;
  3750. else if (max_links && inode->i_nlink >= max_links)
  3751. error = -EMLINK;
  3752. else {
  3753. error = try_break_deleg(inode, delegated_inode);
  3754. if (!error)
  3755. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3756. }
  3757. if (!error && (inode->i_state & I_LINKABLE)) {
  3758. spin_lock(&inode->i_lock);
  3759. inode->i_state &= ~I_LINKABLE;
  3760. spin_unlock(&inode->i_lock);
  3761. }
  3762. inode_unlock(inode);
  3763. if (!error)
  3764. fsnotify_link(dir, inode, new_dentry);
  3765. return error;
  3766. }
  3767. EXPORT_SYMBOL(vfs_link);
  3768. /*
  3769. * Hardlinks are often used in delicate situations. We avoid
  3770. * security-related surprises by not following symlinks on the
  3771. * newname. --KAB
  3772. *
  3773. * We don't follow them on the oldname either to be compatible
  3774. * with linux 2.0, and to avoid hard-linking to directories
  3775. * and other special files. --ADM
  3776. */
  3777. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3778. int, newdfd, const char __user *, newname, int, flags)
  3779. {
  3780. struct dentry *new_dentry;
  3781. struct path old_path, new_path;
  3782. struct inode *delegated_inode = NULL;
  3783. int how = 0;
  3784. int error;
  3785. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3786. return -EINVAL;
  3787. /*
  3788. * To use null names we require CAP_DAC_READ_SEARCH
  3789. * This ensures that not everyone will be able to create
  3790. * handlink using the passed filedescriptor.
  3791. */
  3792. if (flags & AT_EMPTY_PATH) {
  3793. if (!capable(CAP_DAC_READ_SEARCH))
  3794. return -ENOENT;
  3795. how = LOOKUP_EMPTY;
  3796. }
  3797. if (flags & AT_SYMLINK_FOLLOW)
  3798. how |= LOOKUP_FOLLOW;
  3799. retry:
  3800. error = user_path_at(olddfd, oldname, how, &old_path);
  3801. if (error)
  3802. return error;
  3803. new_dentry = user_path_create(newdfd, newname, &new_path,
  3804. (how & LOOKUP_REVAL));
  3805. error = PTR_ERR(new_dentry);
  3806. if (IS_ERR(new_dentry))
  3807. goto out;
  3808. error = -EXDEV;
  3809. if (old_path.mnt != new_path.mnt)
  3810. goto out_dput;
  3811. error = may_linkat(&old_path);
  3812. if (unlikely(error))
  3813. goto out_dput;
  3814. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3815. if (error)
  3816. goto out_dput;
  3817. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3818. out_dput:
  3819. done_path_create(&new_path, new_dentry);
  3820. if (delegated_inode) {
  3821. error = break_deleg_wait(&delegated_inode);
  3822. if (!error) {
  3823. path_put(&old_path);
  3824. goto retry;
  3825. }
  3826. }
  3827. if (retry_estale(error, how)) {
  3828. path_put(&old_path);
  3829. how |= LOOKUP_REVAL;
  3830. goto retry;
  3831. }
  3832. out:
  3833. path_put(&old_path);
  3834. return error;
  3835. }
  3836. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3837. {
  3838. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3839. }
  3840. /**
  3841. * vfs_rename - rename a filesystem object
  3842. * @old_dir: parent of source
  3843. * @old_dentry: source
  3844. * @new_dir: parent of destination
  3845. * @new_dentry: destination
  3846. * @delegated_inode: returns an inode needing a delegation break
  3847. * @flags: rename flags
  3848. *
  3849. * The caller must hold multiple mutexes--see lock_rename()).
  3850. *
  3851. * If vfs_rename discovers a delegation in need of breaking at either
  3852. * the source or destination, it will return -EWOULDBLOCK and return a
  3853. * reference to the inode in delegated_inode. The caller should then
  3854. * break the delegation and retry. Because breaking a delegation may
  3855. * take a long time, the caller should drop all locks before doing
  3856. * so.
  3857. *
  3858. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3859. * be appropriate for callers that expect the underlying filesystem not
  3860. * to be NFS exported.
  3861. *
  3862. * The worst of all namespace operations - renaming directory. "Perverted"
  3863. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3864. * Problems:
  3865. * a) we can get into loop creation.
  3866. * b) race potential - two innocent renames can create a loop together.
  3867. * That's where 4.4 screws up. Current fix: serialization on
  3868. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3869. * story.
  3870. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3871. * and source (if it is not a directory).
  3872. * And that - after we got ->i_mutex on parents (until then we don't know
  3873. * whether the target exists). Solution: try to be smart with locking
  3874. * order for inodes. We rely on the fact that tree topology may change
  3875. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3876. * move will be locked. Thus we can rank directories by the tree
  3877. * (ancestors first) and rank all non-directories after them.
  3878. * That works since everybody except rename does "lock parent, lookup,
  3879. * lock child" and rename is under ->s_vfs_rename_mutex.
  3880. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3881. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3882. * we'd better make sure that there's no link(2) for them.
  3883. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3884. * we are removing the target. Solution: we will have to grab ->i_mutex
  3885. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3886. * ->i_mutex on parents, which works but leads to some truly excessive
  3887. * locking].
  3888. */
  3889. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3890. struct inode *new_dir, struct dentry *new_dentry,
  3891. struct inode **delegated_inode, unsigned int flags)
  3892. {
  3893. int error;
  3894. bool is_dir = d_is_dir(old_dentry);
  3895. const unsigned char *old_name;
  3896. struct inode *source = old_dentry->d_inode;
  3897. struct inode *target = new_dentry->d_inode;
  3898. bool new_is_dir = false;
  3899. unsigned max_links = new_dir->i_sb->s_max_links;
  3900. if (source == target)
  3901. return 0;
  3902. error = may_delete(old_dir, old_dentry, is_dir);
  3903. if (error)
  3904. return error;
  3905. if (!target) {
  3906. error = may_create(new_dir, new_dentry);
  3907. } else {
  3908. new_is_dir = d_is_dir(new_dentry);
  3909. if (!(flags & RENAME_EXCHANGE))
  3910. error = may_delete(new_dir, new_dentry, is_dir);
  3911. else
  3912. error = may_delete(new_dir, new_dentry, new_is_dir);
  3913. }
  3914. if (error)
  3915. return error;
  3916. if (!old_dir->i_op->rename)
  3917. return -EPERM;
  3918. /*
  3919. * If we are going to change the parent - check write permissions,
  3920. * we'll need to flip '..'.
  3921. */
  3922. if (new_dir != old_dir) {
  3923. if (is_dir) {
  3924. error = inode_permission(source, MAY_WRITE);
  3925. if (error)
  3926. return error;
  3927. }
  3928. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3929. error = inode_permission(target, MAY_WRITE);
  3930. if (error)
  3931. return error;
  3932. }
  3933. }
  3934. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3935. flags);
  3936. if (error)
  3937. return error;
  3938. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3939. dget(new_dentry);
  3940. if (!is_dir || (flags & RENAME_EXCHANGE))
  3941. lock_two_nondirectories(source, target);
  3942. else if (target)
  3943. inode_lock(target);
  3944. error = -EBUSY;
  3945. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3946. goto out;
  3947. if (max_links && new_dir != old_dir) {
  3948. error = -EMLINK;
  3949. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3950. goto out;
  3951. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3952. old_dir->i_nlink >= max_links)
  3953. goto out;
  3954. }
  3955. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3956. shrink_dcache_parent(new_dentry);
  3957. if (!is_dir) {
  3958. error = try_break_deleg(source, delegated_inode);
  3959. if (error)
  3960. goto out;
  3961. }
  3962. if (target && !new_is_dir) {
  3963. error = try_break_deleg(target, delegated_inode);
  3964. if (error)
  3965. goto out;
  3966. }
  3967. error = old_dir->i_op->rename(old_dir, old_dentry,
  3968. new_dir, new_dentry, flags);
  3969. if (error)
  3970. goto out;
  3971. if (!(flags & RENAME_EXCHANGE) && target) {
  3972. if (is_dir)
  3973. target->i_flags |= S_DEAD;
  3974. dont_mount(new_dentry);
  3975. detach_mounts(new_dentry);
  3976. }
  3977. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  3978. if (!(flags & RENAME_EXCHANGE))
  3979. d_move(old_dentry, new_dentry);
  3980. else
  3981. d_exchange(old_dentry, new_dentry);
  3982. }
  3983. out:
  3984. if (!is_dir || (flags & RENAME_EXCHANGE))
  3985. unlock_two_nondirectories(source, target);
  3986. else if (target)
  3987. inode_unlock(target);
  3988. dput(new_dentry);
  3989. if (!error) {
  3990. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3991. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  3992. if (flags & RENAME_EXCHANGE) {
  3993. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  3994. new_is_dir, NULL, new_dentry);
  3995. }
  3996. }
  3997. fsnotify_oldname_free(old_name);
  3998. return error;
  3999. }
  4000. EXPORT_SYMBOL(vfs_rename);
  4001. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  4002. int, newdfd, const char __user *, newname, unsigned int, flags)
  4003. {
  4004. struct dentry *old_dentry, *new_dentry;
  4005. struct dentry *trap;
  4006. struct path old_path, new_path;
  4007. struct qstr old_last, new_last;
  4008. int old_type, new_type;
  4009. struct inode *delegated_inode = NULL;
  4010. struct filename *from;
  4011. struct filename *to;
  4012. unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
  4013. bool should_retry = false;
  4014. int error;
  4015. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  4016. return -EINVAL;
  4017. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  4018. (flags & RENAME_EXCHANGE))
  4019. return -EINVAL;
  4020. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  4021. return -EPERM;
  4022. if (flags & RENAME_EXCHANGE)
  4023. target_flags = 0;
  4024. retry:
  4025. from = filename_parentat(olddfd, getname(oldname), lookup_flags,
  4026. &old_path, &old_last, &old_type);
  4027. if (IS_ERR(from)) {
  4028. error = PTR_ERR(from);
  4029. goto exit;
  4030. }
  4031. to = filename_parentat(newdfd, getname(newname), lookup_flags,
  4032. &new_path, &new_last, &new_type);
  4033. if (IS_ERR(to)) {
  4034. error = PTR_ERR(to);
  4035. goto exit1;
  4036. }
  4037. error = -EXDEV;
  4038. if (old_path.mnt != new_path.mnt)
  4039. goto exit2;
  4040. error = -EBUSY;
  4041. if (old_type != LAST_NORM)
  4042. goto exit2;
  4043. if (flags & RENAME_NOREPLACE)
  4044. error = -EEXIST;
  4045. if (new_type != LAST_NORM)
  4046. goto exit2;
  4047. error = mnt_want_write(old_path.mnt);
  4048. if (error)
  4049. goto exit2;
  4050. retry_deleg:
  4051. trap = lock_rename(new_path.dentry, old_path.dentry);
  4052. old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
  4053. error = PTR_ERR(old_dentry);
  4054. if (IS_ERR(old_dentry))
  4055. goto exit3;
  4056. /* source must exist */
  4057. error = -ENOENT;
  4058. if (d_is_negative(old_dentry))
  4059. goto exit4;
  4060. new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
  4061. error = PTR_ERR(new_dentry);
  4062. if (IS_ERR(new_dentry))
  4063. goto exit4;
  4064. error = -EEXIST;
  4065. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  4066. goto exit5;
  4067. if (flags & RENAME_EXCHANGE) {
  4068. error = -ENOENT;
  4069. if (d_is_negative(new_dentry))
  4070. goto exit5;
  4071. if (!d_is_dir(new_dentry)) {
  4072. error = -ENOTDIR;
  4073. if (new_last.name[new_last.len])
  4074. goto exit5;
  4075. }
  4076. }
  4077. /* unless the source is a directory trailing slashes give -ENOTDIR */
  4078. if (!d_is_dir(old_dentry)) {
  4079. error = -ENOTDIR;
  4080. if (old_last.name[old_last.len])
  4081. goto exit5;
  4082. if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
  4083. goto exit5;
  4084. }
  4085. /* source should not be ancestor of target */
  4086. error = -EINVAL;
  4087. if (old_dentry == trap)
  4088. goto exit5;
  4089. /* target should not be an ancestor of source */
  4090. if (!(flags & RENAME_EXCHANGE))
  4091. error = -ENOTEMPTY;
  4092. if (new_dentry == trap)
  4093. goto exit5;
  4094. error = security_path_rename(&old_path, old_dentry,
  4095. &new_path, new_dentry, flags);
  4096. if (error)
  4097. goto exit5;
  4098. error = vfs_rename(old_path.dentry->d_inode, old_dentry,
  4099. new_path.dentry->d_inode, new_dentry,
  4100. &delegated_inode, flags);
  4101. exit5:
  4102. dput(new_dentry);
  4103. exit4:
  4104. dput(old_dentry);
  4105. exit3:
  4106. unlock_rename(new_path.dentry, old_path.dentry);
  4107. if (delegated_inode) {
  4108. error = break_deleg_wait(&delegated_inode);
  4109. if (!error)
  4110. goto retry_deleg;
  4111. }
  4112. mnt_drop_write(old_path.mnt);
  4113. exit2:
  4114. if (retry_estale(error, lookup_flags))
  4115. should_retry = true;
  4116. path_put(&new_path);
  4117. putname(to);
  4118. exit1:
  4119. path_put(&old_path);
  4120. putname(from);
  4121. if (should_retry) {
  4122. should_retry = false;
  4123. lookup_flags |= LOOKUP_REVAL;
  4124. goto retry;
  4125. }
  4126. exit:
  4127. return error;
  4128. }
  4129. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  4130. int, newdfd, const char __user *, newname)
  4131. {
  4132. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  4133. }
  4134. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  4135. {
  4136. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  4137. }
  4138. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  4139. {
  4140. int error = may_create(dir, dentry);
  4141. if (error)
  4142. return error;
  4143. if (!dir->i_op->mknod)
  4144. return -EPERM;
  4145. return dir->i_op->mknod(dir, dentry,
  4146. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  4147. }
  4148. EXPORT_SYMBOL(vfs_whiteout);
  4149. int readlink_copy(char __user *buffer, int buflen, const char *link)
  4150. {
  4151. int len = PTR_ERR(link);
  4152. if (IS_ERR(link))
  4153. goto out;
  4154. len = strlen(link);
  4155. if (len > (unsigned) buflen)
  4156. len = buflen;
  4157. if (copy_to_user(buffer, link, len))
  4158. len = -EFAULT;
  4159. out:
  4160. return len;
  4161. }
  4162. /*
  4163. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  4164. * have ->get_link() not calling nd_jump_link(). Using (or not using) it
  4165. * for any given inode is up to filesystem.
  4166. */
  4167. static int generic_readlink(struct dentry *dentry, char __user *buffer,
  4168. int buflen)
  4169. {
  4170. DEFINE_DELAYED_CALL(done);
  4171. struct inode *inode = d_inode(dentry);
  4172. const char *link = inode->i_link;
  4173. int res;
  4174. if (!link) {
  4175. link = inode->i_op->get_link(dentry, inode, &done);
  4176. if (IS_ERR(link))
  4177. return PTR_ERR(link);
  4178. }
  4179. res = readlink_copy(buffer, buflen, link);
  4180. do_delayed_call(&done);
  4181. return res;
  4182. }
  4183. /**
  4184. * vfs_readlink - copy symlink body into userspace buffer
  4185. * @dentry: dentry on which to get symbolic link
  4186. * @buffer: user memory pointer
  4187. * @buflen: size of buffer
  4188. *
  4189. * Does not touch atime. That's up to the caller if necessary
  4190. *
  4191. * Does not call security hook.
  4192. */
  4193. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4194. {
  4195. struct inode *inode = d_inode(dentry);
  4196. if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
  4197. if (unlikely(inode->i_op->readlink))
  4198. return inode->i_op->readlink(dentry, buffer, buflen);
  4199. if (!d_is_symlink(dentry))
  4200. return -EINVAL;
  4201. spin_lock(&inode->i_lock);
  4202. inode->i_opflags |= IOP_DEFAULT_READLINK;
  4203. spin_unlock(&inode->i_lock);
  4204. }
  4205. return generic_readlink(dentry, buffer, buflen);
  4206. }
  4207. EXPORT_SYMBOL(vfs_readlink);
  4208. /**
  4209. * vfs_get_link - get symlink body
  4210. * @dentry: dentry on which to get symbolic link
  4211. * @done: caller needs to free returned data with this
  4212. *
  4213. * Calls security hook and i_op->get_link() on the supplied inode.
  4214. *
  4215. * It does not touch atime. That's up to the caller if necessary.
  4216. *
  4217. * Does not work on "special" symlinks like /proc/$$/fd/N
  4218. */
  4219. const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
  4220. {
  4221. const char *res = ERR_PTR(-EINVAL);
  4222. struct inode *inode = d_inode(dentry);
  4223. if (d_is_symlink(dentry)) {
  4224. res = ERR_PTR(security_inode_readlink(dentry));
  4225. if (!res)
  4226. res = inode->i_op->get_link(dentry, inode, done);
  4227. }
  4228. return res;
  4229. }
  4230. EXPORT_SYMBOL(vfs_get_link);
  4231. /* get the link contents into pagecache */
  4232. const char *page_get_link(struct dentry *dentry, struct inode *inode,
  4233. struct delayed_call *callback)
  4234. {
  4235. char *kaddr;
  4236. struct page *page;
  4237. struct address_space *mapping = inode->i_mapping;
  4238. if (!dentry) {
  4239. page = find_get_page(mapping, 0);
  4240. if (!page)
  4241. return ERR_PTR(-ECHILD);
  4242. if (!PageUptodate(page)) {
  4243. put_page(page);
  4244. return ERR_PTR(-ECHILD);
  4245. }
  4246. } else {
  4247. page = read_mapping_page(mapping, 0, NULL);
  4248. if (IS_ERR(page))
  4249. return (char*)page;
  4250. }
  4251. set_delayed_call(callback, page_put_link, page);
  4252. BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
  4253. kaddr = page_address(page);
  4254. nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
  4255. return kaddr;
  4256. }
  4257. EXPORT_SYMBOL(page_get_link);
  4258. void page_put_link(void *arg)
  4259. {
  4260. put_page(arg);
  4261. }
  4262. EXPORT_SYMBOL(page_put_link);
  4263. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4264. {
  4265. DEFINE_DELAYED_CALL(done);
  4266. int res = readlink_copy(buffer, buflen,
  4267. page_get_link(dentry, d_inode(dentry),
  4268. &done));
  4269. do_delayed_call(&done);
  4270. return res;
  4271. }
  4272. EXPORT_SYMBOL(page_readlink);
  4273. /*
  4274. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  4275. */
  4276. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  4277. {
  4278. struct address_space *mapping = inode->i_mapping;
  4279. struct page *page;
  4280. void *fsdata;
  4281. int err;
  4282. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  4283. if (nofs)
  4284. flags |= AOP_FLAG_NOFS;
  4285. retry:
  4286. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4287. flags, &page, &fsdata);
  4288. if (err)
  4289. goto fail;
  4290. memcpy(page_address(page), symname, len-1);
  4291. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4292. page, fsdata);
  4293. if (err < 0)
  4294. goto fail;
  4295. if (err < len-1)
  4296. goto retry;
  4297. mark_inode_dirty(inode);
  4298. return 0;
  4299. fail:
  4300. return err;
  4301. }
  4302. EXPORT_SYMBOL(__page_symlink);
  4303. int page_symlink(struct inode *inode, const char *symname, int len)
  4304. {
  4305. return __page_symlink(inode, symname, len,
  4306. !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
  4307. }
  4308. EXPORT_SYMBOL(page_symlink);
  4309. const struct inode_operations page_symlink_inode_operations = {
  4310. .get_link = page_get_link,
  4311. };
  4312. EXPORT_SYMBOL(page_symlink_inode_operations);