namei.c 119 KB

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