namei.c 101 KB

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