namei.c 123 KB

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