namei.c 100 KB

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