namei.c 107 KB

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