namei.c 107 KB

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