namei.c 100 KB

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