namei.c 100 KB

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