namei.c 107 KB

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