namei.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837
  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 <linux/hash.h>
  35. #include <linux/bitops.h>
  36. #include <linux/init_task.h>
  37. #include <linux/uaccess.h>
  38. #include "internal.h"
  39. #include "mount.h"
  40. /* [Feb-1997 T. Schoebel-Theuer]
  41. * Fundamental changes in the pathname lookup mechanisms (namei)
  42. * were necessary because of omirr. The reason is that omirr needs
  43. * to know the _real_ pathname, not the user-supplied one, in case
  44. * of symlinks (and also when transname replacements occur).
  45. *
  46. * The new code replaces the old recursive symlink resolution with
  47. * an iterative one (in case of non-nested symlink chains). It does
  48. * this with calls to <fs>_follow_link().
  49. * As a side effect, dir_namei(), _namei() and follow_link() are now
  50. * replaced with a single function lookup_dentry() that can handle all
  51. * the special cases of the former code.
  52. *
  53. * With the new dcache, the pathname is stored at each inode, at least as
  54. * long as the refcount of the inode is positive. As a side effect, the
  55. * size of the dcache depends on the inode cache and thus is dynamic.
  56. *
  57. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  58. * resolution to correspond with current state of the code.
  59. *
  60. * Note that the symlink resolution is not *completely* iterative.
  61. * There is still a significant amount of tail- and mid- recursion in
  62. * the algorithm. Also, note that <fs>_readlink() is not used in
  63. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  64. * may return different results than <fs>_follow_link(). Many virtual
  65. * filesystems (including /proc) exhibit this behavior.
  66. */
  67. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  68. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  69. * and the name already exists in form of a symlink, try to create the new
  70. * name indicated by the symlink. The old code always complained that the
  71. * name already exists, due to not following the symlink even if its target
  72. * is nonexistent. The new semantics affects also mknod() and link() when
  73. * the name is a symlink pointing to a non-existent name.
  74. *
  75. * I don't know which semantics is the right one, since I have no access
  76. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  77. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  78. * "old" one. Personally, I think the new semantics is much more logical.
  79. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  80. * file does succeed in both HP-UX and SunOs, but not in Solaris
  81. * and in the old Linux semantics.
  82. */
  83. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  84. * semantics. See the comments in "open_namei" and "do_link" below.
  85. *
  86. * [10-Sep-98 Alan Modra] Another symlink change.
  87. */
  88. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  89. * inside the path - always follow.
  90. * in the last component in creation/removal/renaming - never follow.
  91. * if LOOKUP_FOLLOW passed - follow.
  92. * if the pathname has trailing slashes - follow.
  93. * otherwise - don't follow.
  94. * (applied in that order).
  95. *
  96. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  97. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  98. * During the 2.4 we need to fix the userland stuff depending on it -
  99. * hopefully we will be able to get rid of that wart in 2.5. So far only
  100. * XEmacs seems to be relying on it...
  101. */
  102. /*
  103. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  104. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  105. * any extra contention...
  106. */
  107. /* In order to reduce some races, while at the same time doing additional
  108. * checking and hopefully speeding things up, we copy filenames to the
  109. * kernel data space before using them..
  110. *
  111. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  112. * PATH_MAX includes the nul terminator --RR.
  113. */
  114. #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
  115. struct filename *
  116. getname_flags(const char __user *filename, int flags, int *empty)
  117. {
  118. struct filename *result;
  119. char *kname;
  120. int len;
  121. result = audit_reusename(filename);
  122. if (result)
  123. return result;
  124. result = __getname();
  125. if (unlikely(!result))
  126. return ERR_PTR(-ENOMEM);
  127. /*
  128. * First, try to embed the struct filename inside the names_cache
  129. * allocation
  130. */
  131. kname = (char *)result->iname;
  132. result->name = kname;
  133. len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
  134. if (unlikely(len < 0)) {
  135. __putname(result);
  136. return ERR_PTR(len);
  137. }
  138. /*
  139. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  140. * separate struct filename so we can dedicate the entire
  141. * names_cache allocation for the pathname, and re-do the copy from
  142. * userland.
  143. */
  144. if (unlikely(len == EMBEDDED_NAME_MAX)) {
  145. const size_t size = offsetof(struct filename, iname[1]);
  146. kname = (char *)result;
  147. /*
  148. * size is chosen that way we to guarantee that
  149. * result->iname[0] is within the same object and that
  150. * kname can't be equal to result->iname, no matter what.
  151. */
  152. result = kzalloc(size, GFP_KERNEL);
  153. if (unlikely(!result)) {
  154. __putname(kname);
  155. return ERR_PTR(-ENOMEM);
  156. }
  157. result->name = kname;
  158. len = strncpy_from_user(kname, filename, PATH_MAX);
  159. if (unlikely(len < 0)) {
  160. __putname(kname);
  161. kfree(result);
  162. return ERR_PTR(len);
  163. }
  164. if (unlikely(len == PATH_MAX)) {
  165. __putname(kname);
  166. kfree(result);
  167. return ERR_PTR(-ENAMETOOLONG);
  168. }
  169. }
  170. result->refcnt = 1;
  171. /* The empty path is special. */
  172. if (unlikely(!len)) {
  173. if (empty)
  174. *empty = 1;
  175. if (!(flags & LOOKUP_EMPTY)) {
  176. putname(result);
  177. return ERR_PTR(-ENOENT);
  178. }
  179. }
  180. result->uptr = filename;
  181. result->aname = NULL;
  182. audit_getname(result);
  183. return result;
  184. }
  185. struct filename *
  186. getname(const char __user * filename)
  187. {
  188. return getname_flags(filename, 0, NULL);
  189. }
  190. struct filename *
  191. getname_kernel(const char * filename)
  192. {
  193. struct filename *result;
  194. int len = strlen(filename) + 1;
  195. result = __getname();
  196. if (unlikely(!result))
  197. return ERR_PTR(-ENOMEM);
  198. if (len <= EMBEDDED_NAME_MAX) {
  199. result->name = (char *)result->iname;
  200. } else if (len <= PATH_MAX) {
  201. struct filename *tmp;
  202. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  203. if (unlikely(!tmp)) {
  204. __putname(result);
  205. return ERR_PTR(-ENOMEM);
  206. }
  207. tmp->name = (char *)result;
  208. result = tmp;
  209. } else {
  210. __putname(result);
  211. return ERR_PTR(-ENAMETOOLONG);
  212. }
  213. memcpy((char *)result->name, filename, len);
  214. result->uptr = NULL;
  215. result->aname = NULL;
  216. result->refcnt = 1;
  217. audit_getname(result);
  218. return result;
  219. }
  220. void putname(struct filename *name)
  221. {
  222. BUG_ON(name->refcnt <= 0);
  223. if (--name->refcnt > 0)
  224. return;
  225. if (name->name != name->iname) {
  226. __putname(name->name);
  227. kfree(name);
  228. } else
  229. __putname(name);
  230. }
  231. static int check_acl(struct inode *inode, int mask)
  232. {
  233. #ifdef CONFIG_FS_POSIX_ACL
  234. struct posix_acl *acl;
  235. if (mask & MAY_NOT_BLOCK) {
  236. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  237. if (!acl)
  238. return -EAGAIN;
  239. /* no ->get_acl() calls in RCU mode... */
  240. if (is_uncached_acl(acl))
  241. return -ECHILD;
  242. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  243. }
  244. acl = get_acl(inode, ACL_TYPE_ACCESS);
  245. if (IS_ERR(acl))
  246. return PTR_ERR(acl);
  247. if (acl) {
  248. int error = posix_acl_permission(inode, acl, mask);
  249. posix_acl_release(acl);
  250. return error;
  251. }
  252. #endif
  253. return -EAGAIN;
  254. }
  255. /*
  256. * This does the basic permission checking
  257. */
  258. static int acl_permission_check(struct inode *inode, int mask)
  259. {
  260. unsigned int mode = inode->i_mode;
  261. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  262. mode >>= 6;
  263. else {
  264. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  265. int error = check_acl(inode, mask);
  266. if (error != -EAGAIN)
  267. return error;
  268. }
  269. if (in_group_p(inode->i_gid))
  270. mode >>= 3;
  271. }
  272. /*
  273. * If the DACs are ok we don't need any capability check.
  274. */
  275. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  276. return 0;
  277. return -EACCES;
  278. }
  279. /**
  280. * generic_permission - check for access rights on a Posix-like filesystem
  281. * @inode: inode to check access rights for
  282. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  283. *
  284. * Used to check for read/write/execute permissions on a file.
  285. * We use "fsuid" for this, letting us set arbitrary permissions
  286. * for filesystem access without changing the "normal" uids which
  287. * are used for other things.
  288. *
  289. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  290. * request cannot be satisfied (eg. requires blocking or too much complexity).
  291. * It would then be called again in ref-walk mode.
  292. */
  293. int generic_permission(struct inode *inode, int mask)
  294. {
  295. int ret;
  296. /*
  297. * Do the basic permission checks.
  298. */
  299. ret = acl_permission_check(inode, mask);
  300. if (ret != -EACCES)
  301. return ret;
  302. if (S_ISDIR(inode->i_mode)) {
  303. /* DACs are overridable for directories */
  304. if (!(mask & MAY_WRITE))
  305. if (capable_wrt_inode_uidgid(inode,
  306. CAP_DAC_READ_SEARCH))
  307. return 0;
  308. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  309. return 0;
  310. return -EACCES;
  311. }
  312. /*
  313. * Searching includes executable on directories, else just read.
  314. */
  315. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  316. if (mask == MAY_READ)
  317. if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
  318. return 0;
  319. /*
  320. * Read/write DACs are always overridable.
  321. * Executable DACs are overridable when there is
  322. * at least one exec bit set.
  323. */
  324. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  325. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  326. return 0;
  327. return -EACCES;
  328. }
  329. EXPORT_SYMBOL(generic_permission);
  330. /*
  331. * We _really_ want to just do "generic_permission()" without
  332. * even looking at the inode->i_op values. So we keep a cache
  333. * flag in inode->i_opflags, that says "this has not special
  334. * permission function, use the fast case".
  335. */
  336. static inline int do_inode_permission(struct inode *inode, int mask)
  337. {
  338. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  339. if (likely(inode->i_op->permission))
  340. return inode->i_op->permission(inode, mask);
  341. /* This gets set once for the inode lifetime */
  342. spin_lock(&inode->i_lock);
  343. inode->i_opflags |= IOP_FASTPERM;
  344. spin_unlock(&inode->i_lock);
  345. }
  346. return generic_permission(inode, mask);
  347. }
  348. /**
  349. * __inode_permission - Check for access rights to a given inode
  350. * @inode: Inode to check permission on
  351. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  352. *
  353. * Check for read/write/execute permissions on an inode.
  354. *
  355. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  356. *
  357. * This does not check for a read-only file system. You probably want
  358. * inode_permission().
  359. */
  360. int __inode_permission(struct inode *inode, int mask)
  361. {
  362. int retval;
  363. if (unlikely(mask & MAY_WRITE)) {
  364. /*
  365. * Nobody gets write access to an immutable file.
  366. */
  367. if (IS_IMMUTABLE(inode))
  368. return -EPERM;
  369. /*
  370. * Updating mtime will likely cause i_uid and i_gid to be
  371. * written back improperly if their true value is unknown
  372. * to the vfs.
  373. */
  374. if (HAS_UNMAPPED_ID(inode))
  375. return -EACCES;
  376. }
  377. retval = do_inode_permission(inode, mask);
  378. if (retval)
  379. return retval;
  380. retval = devcgroup_inode_permission(inode, mask);
  381. if (retval)
  382. return retval;
  383. return security_inode_permission(inode, mask);
  384. }
  385. EXPORT_SYMBOL(__inode_permission);
  386. /**
  387. * sb_permission - Check superblock-level permissions
  388. * @sb: Superblock of inode to check permission on
  389. * @inode: Inode to check permission on
  390. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  391. *
  392. * Separate out file-system wide checks from inode-specific permission checks.
  393. */
  394. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  395. {
  396. if (unlikely(mask & MAY_WRITE)) {
  397. umode_t mode = inode->i_mode;
  398. /* Nobody gets write access to a read-only fs. */
  399. if ((sb->s_flags & MS_RDONLY) &&
  400. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  401. return -EROFS;
  402. }
  403. return 0;
  404. }
  405. /**
  406. * inode_permission - Check for access rights to a given inode
  407. * @inode: Inode to check permission on
  408. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  409. *
  410. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  411. * this, letting us set arbitrary permissions for filesystem access without
  412. * changing the "normal" UIDs which are used for other things.
  413. *
  414. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  415. */
  416. int inode_permission(struct inode *inode, int mask)
  417. {
  418. int retval;
  419. retval = sb_permission(inode->i_sb, inode, mask);
  420. if (retval)
  421. return retval;
  422. return __inode_permission(inode, mask);
  423. }
  424. EXPORT_SYMBOL(inode_permission);
  425. /**
  426. * path_get - get a reference to a path
  427. * @path: path to get the reference to
  428. *
  429. * Given a path increment the reference count to the dentry and the vfsmount.
  430. */
  431. void path_get(const struct path *path)
  432. {
  433. mntget(path->mnt);
  434. dget(path->dentry);
  435. }
  436. EXPORT_SYMBOL(path_get);
  437. /**
  438. * path_put - put a reference to a path
  439. * @path: path to put the reference to
  440. *
  441. * Given a path decrement the reference count to the dentry and the vfsmount.
  442. */
  443. void path_put(const struct path *path)
  444. {
  445. dput(path->dentry);
  446. mntput(path->mnt);
  447. }
  448. EXPORT_SYMBOL(path_put);
  449. #define EMBEDDED_LEVELS 2
  450. struct nameidata {
  451. struct path path;
  452. struct qstr last;
  453. struct path root;
  454. struct inode *inode; /* path.dentry.d_inode */
  455. unsigned int flags;
  456. unsigned seq, m_seq;
  457. int last_type;
  458. unsigned depth;
  459. int total_link_count;
  460. struct saved {
  461. struct path link;
  462. struct delayed_call done;
  463. const char *name;
  464. unsigned seq;
  465. } *stack, internal[EMBEDDED_LEVELS];
  466. struct filename *name;
  467. struct nameidata *saved;
  468. struct inode *link_inode;
  469. unsigned root_seq;
  470. int dfd;
  471. } __randomize_layout;
  472. static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
  473. {
  474. struct nameidata *old = current->nameidata;
  475. p->stack = p->internal;
  476. p->dfd = dfd;
  477. p->name = name;
  478. p->total_link_count = old ? old->total_link_count : 0;
  479. p->saved = old;
  480. current->nameidata = p;
  481. }
  482. static void restore_nameidata(void)
  483. {
  484. struct nameidata *now = current->nameidata, *old = now->saved;
  485. current->nameidata = old;
  486. if (old)
  487. old->total_link_count = now->total_link_count;
  488. if (now->stack != now->internal)
  489. kfree(now->stack);
  490. }
  491. static int __nd_alloc_stack(struct nameidata *nd)
  492. {
  493. struct saved *p;
  494. if (nd->flags & LOOKUP_RCU) {
  495. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  496. GFP_ATOMIC);
  497. if (unlikely(!p))
  498. return -ECHILD;
  499. } else {
  500. p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
  501. GFP_KERNEL);
  502. if (unlikely(!p))
  503. return -ENOMEM;
  504. }
  505. memcpy(p, nd->internal, sizeof(nd->internal));
  506. nd->stack = p;
  507. return 0;
  508. }
  509. /**
  510. * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
  511. * @path: nameidate to verify
  512. *
  513. * Rename can sometimes move a file or directory outside of a bind
  514. * mount, path_connected allows those cases to be detected.
  515. */
  516. static bool path_connected(const struct path *path)
  517. {
  518. struct vfsmount *mnt = path->mnt;
  519. /* Only bind mounts can have disconnected paths */
  520. if (mnt->mnt_root == mnt->mnt_sb->s_root)
  521. return true;
  522. return is_subdir(path->dentry, mnt->mnt_root);
  523. }
  524. static inline int nd_alloc_stack(struct nameidata *nd)
  525. {
  526. if (likely(nd->depth != EMBEDDED_LEVELS))
  527. return 0;
  528. if (likely(nd->stack != nd->internal))
  529. return 0;
  530. return __nd_alloc_stack(nd);
  531. }
  532. static void drop_links(struct nameidata *nd)
  533. {
  534. int i = nd->depth;
  535. while (i--) {
  536. struct saved *last = nd->stack + i;
  537. do_delayed_call(&last->done);
  538. clear_delayed_call(&last->done);
  539. }
  540. }
  541. static void terminate_walk(struct nameidata *nd)
  542. {
  543. drop_links(nd);
  544. if (!(nd->flags & LOOKUP_RCU)) {
  545. int i;
  546. path_put(&nd->path);
  547. for (i = 0; i < nd->depth; i++)
  548. path_put(&nd->stack[i].link);
  549. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  550. path_put(&nd->root);
  551. nd->root.mnt = NULL;
  552. }
  553. } else {
  554. nd->flags &= ~LOOKUP_RCU;
  555. if (!(nd->flags & LOOKUP_ROOT))
  556. nd->root.mnt = NULL;
  557. rcu_read_unlock();
  558. }
  559. nd->depth = 0;
  560. }
  561. /* path_put is needed afterwards regardless of success or failure */
  562. static bool legitimize_path(struct nameidata *nd,
  563. struct path *path, unsigned seq)
  564. {
  565. int res = __legitimize_mnt(path->mnt, nd->m_seq);
  566. if (unlikely(res)) {
  567. if (res > 0)
  568. path->mnt = NULL;
  569. path->dentry = NULL;
  570. return false;
  571. }
  572. if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
  573. path->dentry = NULL;
  574. return false;
  575. }
  576. return !read_seqcount_retry(&path->dentry->d_seq, seq);
  577. }
  578. static bool legitimize_links(struct nameidata *nd)
  579. {
  580. int i;
  581. for (i = 0; i < nd->depth; i++) {
  582. struct saved *last = nd->stack + i;
  583. if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
  584. drop_links(nd);
  585. nd->depth = i + 1;
  586. return false;
  587. }
  588. }
  589. return true;
  590. }
  591. /*
  592. * Path walking has 2 modes, rcu-walk and ref-walk (see
  593. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  594. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  595. * normal reference counts on dentries and vfsmounts to transition to ref-walk
  596. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  597. * got stuck, so ref-walk may continue from there. If this is not successful
  598. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  599. * to restart the path walk from the beginning in ref-walk mode.
  600. */
  601. /**
  602. * unlazy_walk - try to switch to ref-walk mode.
  603. * @nd: nameidata pathwalk data
  604. * Returns: 0 on success, -ECHILD on failure
  605. *
  606. * unlazy_walk attempts to legitimize the current nd->path and nd->root
  607. * for ref-walk mode.
  608. * Must be called from rcu-walk context.
  609. * Nothing should touch nameidata between unlazy_walk() failure and
  610. * terminate_walk().
  611. */
  612. static int unlazy_walk(struct nameidata *nd)
  613. {
  614. struct dentry *parent = nd->path.dentry;
  615. BUG_ON(!(nd->flags & LOOKUP_RCU));
  616. nd->flags &= ~LOOKUP_RCU;
  617. if (unlikely(!legitimize_links(nd)))
  618. goto out2;
  619. if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
  620. goto out1;
  621. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  622. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
  623. goto out;
  624. }
  625. rcu_read_unlock();
  626. BUG_ON(nd->inode != parent->d_inode);
  627. return 0;
  628. out2:
  629. nd->path.mnt = NULL;
  630. nd->path.dentry = NULL;
  631. out1:
  632. if (!(nd->flags & LOOKUP_ROOT))
  633. nd->root.mnt = NULL;
  634. out:
  635. rcu_read_unlock();
  636. return -ECHILD;
  637. }
  638. /**
  639. * unlazy_child - try to switch to ref-walk mode.
  640. * @nd: nameidata pathwalk data
  641. * @dentry: child of nd->path.dentry
  642. * @seq: seq number to check dentry against
  643. * Returns: 0 on success, -ECHILD on failure
  644. *
  645. * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
  646. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  647. * @nd. Must be called from rcu-walk context.
  648. * Nothing should touch nameidata between unlazy_child() failure and
  649. * terminate_walk().
  650. */
  651. static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
  652. {
  653. BUG_ON(!(nd->flags & LOOKUP_RCU));
  654. nd->flags &= ~LOOKUP_RCU;
  655. if (unlikely(!legitimize_links(nd)))
  656. goto out2;
  657. if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
  658. goto out2;
  659. if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
  660. goto out1;
  661. /*
  662. * We need to move both the parent and the dentry from the RCU domain
  663. * to be properly refcounted. And the sequence number in the dentry
  664. * validates *both* dentry counters, since we checked the sequence
  665. * number of the parent after we got the child sequence number. So we
  666. * know the parent must still be valid if the child sequence number is
  667. */
  668. if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
  669. goto out;
  670. if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
  671. rcu_read_unlock();
  672. dput(dentry);
  673. goto drop_root_mnt;
  674. }
  675. /*
  676. * Sequence counts matched. Now make sure that the root is
  677. * still valid and get it if required.
  678. */
  679. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  680. if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
  681. rcu_read_unlock();
  682. dput(dentry);
  683. return -ECHILD;
  684. }
  685. }
  686. rcu_read_unlock();
  687. return 0;
  688. out2:
  689. nd->path.mnt = NULL;
  690. out1:
  691. nd->path.dentry = NULL;
  692. out:
  693. rcu_read_unlock();
  694. drop_root_mnt:
  695. if (!(nd->flags & LOOKUP_ROOT))
  696. nd->root.mnt = NULL;
  697. return -ECHILD;
  698. }
  699. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  700. {
  701. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
  702. return dentry->d_op->d_revalidate(dentry, flags);
  703. else
  704. return 1;
  705. }
  706. /**
  707. * complete_walk - successful completion of path walk
  708. * @nd: pointer nameidata
  709. *
  710. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  711. * Revalidate the final result, unless we'd already done that during
  712. * the path walk or the filesystem doesn't ask for it. Return 0 on
  713. * success, -error on failure. In case of failure caller does not
  714. * need to drop nd->path.
  715. */
  716. static int complete_walk(struct nameidata *nd)
  717. {
  718. struct dentry *dentry = nd->path.dentry;
  719. int status;
  720. if (nd->flags & LOOKUP_RCU) {
  721. if (!(nd->flags & LOOKUP_ROOT))
  722. nd->root.mnt = NULL;
  723. if (unlikely(unlazy_walk(nd)))
  724. return -ECHILD;
  725. }
  726. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  727. return 0;
  728. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  729. return 0;
  730. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  731. if (status > 0)
  732. return 0;
  733. if (!status)
  734. status = -ESTALE;
  735. return status;
  736. }
  737. static void set_root(struct nameidata *nd)
  738. {
  739. struct fs_struct *fs = current->fs;
  740. if (nd->flags & LOOKUP_RCU) {
  741. unsigned seq;
  742. do {
  743. seq = read_seqcount_begin(&fs->seq);
  744. nd->root = fs->root;
  745. nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  746. } while (read_seqcount_retry(&fs->seq, seq));
  747. } else {
  748. get_fs_root(fs, &nd->root);
  749. }
  750. }
  751. static void path_put_conditional(struct path *path, struct nameidata *nd)
  752. {
  753. dput(path->dentry);
  754. if (path->mnt != nd->path.mnt)
  755. mntput(path->mnt);
  756. }
  757. static inline void path_to_nameidata(const struct path *path,
  758. struct nameidata *nd)
  759. {
  760. if (!(nd->flags & LOOKUP_RCU)) {
  761. dput(nd->path.dentry);
  762. if (nd->path.mnt != path->mnt)
  763. mntput(nd->path.mnt);
  764. }
  765. nd->path.mnt = path->mnt;
  766. nd->path.dentry = path->dentry;
  767. }
  768. static int nd_jump_root(struct nameidata *nd)
  769. {
  770. if (nd->flags & LOOKUP_RCU) {
  771. struct dentry *d;
  772. nd->path = nd->root;
  773. d = nd->path.dentry;
  774. nd->inode = d->d_inode;
  775. nd->seq = nd->root_seq;
  776. if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
  777. return -ECHILD;
  778. } else {
  779. path_put(&nd->path);
  780. nd->path = nd->root;
  781. path_get(&nd->path);
  782. nd->inode = nd->path.dentry->d_inode;
  783. }
  784. nd->flags |= LOOKUP_JUMPED;
  785. return 0;
  786. }
  787. /*
  788. * Helper to directly jump to a known parsed path from ->get_link,
  789. * caller must have taken a reference to path beforehand.
  790. */
  791. void nd_jump_link(struct path *path)
  792. {
  793. struct nameidata *nd = current->nameidata;
  794. path_put(&nd->path);
  795. nd->path = *path;
  796. nd->inode = nd->path.dentry->d_inode;
  797. nd->flags |= LOOKUP_JUMPED;
  798. }
  799. static inline void put_link(struct nameidata *nd)
  800. {
  801. struct saved *last = nd->stack + --nd->depth;
  802. do_delayed_call(&last->done);
  803. if (!(nd->flags & LOOKUP_RCU))
  804. path_put(&last->link);
  805. }
  806. int sysctl_protected_symlinks __read_mostly = 0;
  807. int sysctl_protected_hardlinks __read_mostly = 0;
  808. /**
  809. * may_follow_link - Check symlink following for unsafe situations
  810. * @nd: nameidata pathwalk data
  811. *
  812. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  813. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  814. * in a sticky world-writable directory. This is to protect privileged
  815. * processes from failing races against path names that may change out
  816. * from under them by way of other users creating malicious symlinks.
  817. * It will permit symlinks to be followed only when outside a sticky
  818. * world-writable directory, or when the uid of the symlink and follower
  819. * match, or when the directory owner matches the symlink's owner.
  820. *
  821. * Returns 0 if following the symlink is allowed, -ve on error.
  822. */
  823. static inline int may_follow_link(struct nameidata *nd)
  824. {
  825. const struct inode *inode;
  826. const struct inode *parent;
  827. kuid_t puid;
  828. if (!sysctl_protected_symlinks)
  829. return 0;
  830. /* Allowed if owner and follower match. */
  831. inode = nd->link_inode;
  832. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  833. return 0;
  834. /* Allowed if parent directory not sticky and world-writable. */
  835. parent = nd->inode;
  836. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  837. return 0;
  838. /* Allowed if parent directory and link owner match. */
  839. puid = parent->i_uid;
  840. if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
  841. return 0;
  842. if (nd->flags & LOOKUP_RCU)
  843. return -ECHILD;
  844. audit_log_link_denied("follow_link", &nd->stack[0].link);
  845. return -EACCES;
  846. }
  847. /**
  848. * safe_hardlink_source - Check for safe hardlink conditions
  849. * @inode: the source inode to hardlink from
  850. *
  851. * Return false if at least one of the following conditions:
  852. * - inode is not a regular file
  853. * - inode is setuid
  854. * - inode is setgid and group-exec
  855. * - access failure for read and write
  856. *
  857. * Otherwise returns true.
  858. */
  859. static bool safe_hardlink_source(struct inode *inode)
  860. {
  861. umode_t mode = inode->i_mode;
  862. /* Special files should not get pinned to the filesystem. */
  863. if (!S_ISREG(mode))
  864. return false;
  865. /* Setuid files should not get pinned to the filesystem. */
  866. if (mode & S_ISUID)
  867. return false;
  868. /* Executable setgid files should not get pinned to the filesystem. */
  869. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  870. return false;
  871. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  872. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  873. return false;
  874. return true;
  875. }
  876. /**
  877. * may_linkat - Check permissions for creating a hardlink
  878. * @link: the source to hardlink from
  879. *
  880. * Block hardlink when all of:
  881. * - sysctl_protected_hardlinks enabled
  882. * - fsuid does not match inode
  883. * - hardlink source is unsafe (see safe_hardlink_source() above)
  884. * - not CAP_FOWNER in a namespace with the inode owner uid mapped
  885. *
  886. * Returns 0 if successful, -ve on error.
  887. */
  888. static int may_linkat(struct path *link)
  889. {
  890. struct inode *inode;
  891. if (!sysctl_protected_hardlinks)
  892. return 0;
  893. inode = link->dentry->d_inode;
  894. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  895. * otherwise, it must be a safe source.
  896. */
  897. if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
  898. return 0;
  899. audit_log_link_denied("linkat", link);
  900. return -EPERM;
  901. }
  902. static __always_inline
  903. const char *get_link(struct nameidata *nd)
  904. {
  905. struct saved *last = nd->stack + nd->depth - 1;
  906. struct dentry *dentry = last->link.dentry;
  907. struct inode *inode = nd->link_inode;
  908. int error;
  909. const char *res;
  910. if (!(nd->flags & LOOKUP_RCU)) {
  911. touch_atime(&last->link);
  912. cond_resched();
  913. } else if (atime_needs_update_rcu(&last->link, inode)) {
  914. if (unlikely(unlazy_walk(nd)))
  915. return ERR_PTR(-ECHILD);
  916. touch_atime(&last->link);
  917. }
  918. error = security_inode_follow_link(dentry, inode,
  919. nd->flags & LOOKUP_RCU);
  920. if (unlikely(error))
  921. return ERR_PTR(error);
  922. nd->last_type = LAST_BIND;
  923. res = inode->i_link;
  924. if (!res) {
  925. const char * (*get)(struct dentry *, struct inode *,
  926. struct delayed_call *);
  927. get = inode->i_op->get_link;
  928. if (nd->flags & LOOKUP_RCU) {
  929. res = get(NULL, inode, &last->done);
  930. if (res == ERR_PTR(-ECHILD)) {
  931. if (unlikely(unlazy_walk(nd)))
  932. return ERR_PTR(-ECHILD);
  933. res = get(dentry, inode, &last->done);
  934. }
  935. } else {
  936. res = get(dentry, inode, &last->done);
  937. }
  938. if (IS_ERR_OR_NULL(res))
  939. return res;
  940. }
  941. if (*res == '/') {
  942. if (!nd->root.mnt)
  943. set_root(nd);
  944. if (unlikely(nd_jump_root(nd)))
  945. return ERR_PTR(-ECHILD);
  946. while (unlikely(*++res == '/'))
  947. ;
  948. }
  949. if (!*res)
  950. res = NULL;
  951. return res;
  952. }
  953. /*
  954. * follow_up - Find the mountpoint of path's vfsmount
  955. *
  956. * Given a path, find the mountpoint of its source file system.
  957. * Replace @path with the path of the mountpoint in the parent mount.
  958. * Up is towards /.
  959. *
  960. * Return 1 if we went up a level and 0 if we were already at the
  961. * root.
  962. */
  963. int follow_up(struct path *path)
  964. {
  965. struct mount *mnt = real_mount(path->mnt);
  966. struct mount *parent;
  967. struct dentry *mountpoint;
  968. read_seqlock_excl(&mount_lock);
  969. parent = mnt->mnt_parent;
  970. if (parent == mnt) {
  971. read_sequnlock_excl(&mount_lock);
  972. return 0;
  973. }
  974. mntget(&parent->mnt);
  975. mountpoint = dget(mnt->mnt_mountpoint);
  976. read_sequnlock_excl(&mount_lock);
  977. dput(path->dentry);
  978. path->dentry = mountpoint;
  979. mntput(path->mnt);
  980. path->mnt = &parent->mnt;
  981. return 1;
  982. }
  983. EXPORT_SYMBOL(follow_up);
  984. /*
  985. * Perform an automount
  986. * - return -EISDIR to tell follow_managed() to stop and return the path we
  987. * were called with.
  988. */
  989. static int follow_automount(struct path *path, struct nameidata *nd,
  990. bool *need_mntput)
  991. {
  992. struct vfsmount *mnt;
  993. int err;
  994. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  995. return -EREMOTE;
  996. /* We don't want to mount if someone's just doing a stat -
  997. * unless they're stat'ing a directory and appended a '/' to
  998. * the name.
  999. *
  1000. * We do, however, want to mount if someone wants to open or
  1001. * create a file of any type under the mountpoint, wants to
  1002. * traverse through the mountpoint or wants to open the
  1003. * mounted directory. Also, autofs may mark negative dentries
  1004. * as being automount points. These will need the attentions
  1005. * of the daemon to instantiate them before they can be used.
  1006. */
  1007. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  1008. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  1009. path->dentry->d_inode)
  1010. return -EISDIR;
  1011. if (path->dentry->d_sb->s_user_ns != &init_user_ns)
  1012. return -EACCES;
  1013. nd->total_link_count++;
  1014. if (nd->total_link_count >= 40)
  1015. return -ELOOP;
  1016. mnt = path->dentry->d_op->d_automount(path);
  1017. if (IS_ERR(mnt)) {
  1018. /*
  1019. * The filesystem is allowed to return -EISDIR here to indicate
  1020. * it doesn't want to automount. For instance, autofs would do
  1021. * this so that its userspace daemon can mount on this dentry.
  1022. *
  1023. * However, we can only permit this if it's a terminal point in
  1024. * the path being looked up; if it wasn't then the remainder of
  1025. * the path is inaccessible and we should say so.
  1026. */
  1027. if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
  1028. return -EREMOTE;
  1029. return PTR_ERR(mnt);
  1030. }
  1031. if (!mnt) /* mount collision */
  1032. return 0;
  1033. if (!*need_mntput) {
  1034. /* lock_mount() may release path->mnt on error */
  1035. mntget(path->mnt);
  1036. *need_mntput = true;
  1037. }
  1038. err = finish_automount(mnt, path);
  1039. switch (err) {
  1040. case -EBUSY:
  1041. /* Someone else made a mount here whilst we were busy */
  1042. return 0;
  1043. case 0:
  1044. path_put(path);
  1045. path->mnt = mnt;
  1046. path->dentry = dget(mnt->mnt_root);
  1047. return 0;
  1048. default:
  1049. return err;
  1050. }
  1051. }
  1052. /*
  1053. * Handle a dentry that is managed in some way.
  1054. * - Flagged for transit management (autofs)
  1055. * - Flagged as mountpoint
  1056. * - Flagged as automount point
  1057. *
  1058. * This may only be called in refwalk mode.
  1059. *
  1060. * Serialization is taken care of in namespace.c
  1061. */
  1062. static int follow_managed(struct path *path, struct nameidata *nd)
  1063. {
  1064. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  1065. unsigned managed;
  1066. bool need_mntput = false;
  1067. int ret = 0;
  1068. /* Given that we're not holding a lock here, we retain the value in a
  1069. * local variable for each dentry as we look at it so that we don't see
  1070. * the components of that value change under us */
  1071. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1072. managed &= DCACHE_MANAGED_DENTRY,
  1073. unlikely(managed != 0)) {
  1074. /* Allow the filesystem to manage the transit without i_mutex
  1075. * being held. */
  1076. if (managed & DCACHE_MANAGE_TRANSIT) {
  1077. BUG_ON(!path->dentry->d_op);
  1078. BUG_ON(!path->dentry->d_op->d_manage);
  1079. ret = path->dentry->d_op->d_manage(path, false);
  1080. if (ret < 0)
  1081. break;
  1082. }
  1083. /* Transit to a mounted filesystem. */
  1084. if (managed & DCACHE_MOUNTED) {
  1085. struct vfsmount *mounted = lookup_mnt(path);
  1086. if (mounted) {
  1087. dput(path->dentry);
  1088. if (need_mntput)
  1089. mntput(path->mnt);
  1090. path->mnt = mounted;
  1091. path->dentry = dget(mounted->mnt_root);
  1092. need_mntput = true;
  1093. continue;
  1094. }
  1095. /* Something is mounted on this dentry in another
  1096. * namespace and/or whatever was mounted there in this
  1097. * namespace got unmounted before lookup_mnt() could
  1098. * get it */
  1099. }
  1100. /* Handle an automount point */
  1101. if (managed & DCACHE_NEED_AUTOMOUNT) {
  1102. ret = follow_automount(path, nd, &need_mntput);
  1103. if (ret < 0)
  1104. break;
  1105. continue;
  1106. }
  1107. /* We didn't change the current path point */
  1108. break;
  1109. }
  1110. if (need_mntput && path->mnt == mnt)
  1111. mntput(path->mnt);
  1112. if (ret == -EISDIR || !ret)
  1113. ret = 1;
  1114. if (need_mntput)
  1115. nd->flags |= LOOKUP_JUMPED;
  1116. if (unlikely(ret < 0))
  1117. path_put_conditional(path, nd);
  1118. return ret;
  1119. }
  1120. int follow_down_one(struct path *path)
  1121. {
  1122. struct vfsmount *mounted;
  1123. mounted = lookup_mnt(path);
  1124. if (mounted) {
  1125. dput(path->dentry);
  1126. mntput(path->mnt);
  1127. path->mnt = mounted;
  1128. path->dentry = dget(mounted->mnt_root);
  1129. return 1;
  1130. }
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(follow_down_one);
  1134. static inline int managed_dentry_rcu(const struct path *path)
  1135. {
  1136. return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  1137. path->dentry->d_op->d_manage(path, true) : 0;
  1138. }
  1139. /*
  1140. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  1141. * we meet a managed dentry that would need blocking.
  1142. */
  1143. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  1144. struct inode **inode, unsigned *seqp)
  1145. {
  1146. for (;;) {
  1147. struct mount *mounted;
  1148. /*
  1149. * Don't forget we might have a non-mountpoint managed dentry
  1150. * that wants to block transit.
  1151. */
  1152. switch (managed_dentry_rcu(path)) {
  1153. case -ECHILD:
  1154. default:
  1155. return false;
  1156. case -EISDIR:
  1157. return true;
  1158. case 0:
  1159. break;
  1160. }
  1161. if (!d_mountpoint(path->dentry))
  1162. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1163. mounted = __lookup_mnt(path->mnt, path->dentry);
  1164. if (!mounted)
  1165. break;
  1166. path->mnt = &mounted->mnt;
  1167. path->dentry = mounted->mnt.mnt_root;
  1168. nd->flags |= LOOKUP_JUMPED;
  1169. *seqp = read_seqcount_begin(&path->dentry->d_seq);
  1170. /*
  1171. * Update the inode too. We don't need to re-check the
  1172. * dentry sequence number here after this d_inode read,
  1173. * because a mount-point is always pinned.
  1174. */
  1175. *inode = path->dentry->d_inode;
  1176. }
  1177. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1178. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1179. }
  1180. static int follow_dotdot_rcu(struct nameidata *nd)
  1181. {
  1182. struct inode *inode = nd->inode;
  1183. while (1) {
  1184. if (path_equal(&nd->path, &nd->root))
  1185. break;
  1186. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1187. struct dentry *old = nd->path.dentry;
  1188. struct dentry *parent = old->d_parent;
  1189. unsigned seq;
  1190. inode = parent->d_inode;
  1191. seq = read_seqcount_begin(&parent->d_seq);
  1192. if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
  1193. return -ECHILD;
  1194. nd->path.dentry = parent;
  1195. nd->seq = seq;
  1196. if (unlikely(!path_connected(&nd->path)))
  1197. return -ENOENT;
  1198. break;
  1199. } else {
  1200. struct mount *mnt = real_mount(nd->path.mnt);
  1201. struct mount *mparent = mnt->mnt_parent;
  1202. struct dentry *mountpoint = mnt->mnt_mountpoint;
  1203. struct inode *inode2 = mountpoint->d_inode;
  1204. unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
  1205. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1206. return -ECHILD;
  1207. if (&mparent->mnt == nd->path.mnt)
  1208. break;
  1209. /* we know that mountpoint was pinned */
  1210. nd->path.dentry = mountpoint;
  1211. nd->path.mnt = &mparent->mnt;
  1212. inode = inode2;
  1213. nd->seq = seq;
  1214. }
  1215. }
  1216. while (unlikely(d_mountpoint(nd->path.dentry))) {
  1217. struct mount *mounted;
  1218. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1219. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1220. return -ECHILD;
  1221. if (!mounted)
  1222. break;
  1223. nd->path.mnt = &mounted->mnt;
  1224. nd->path.dentry = mounted->mnt.mnt_root;
  1225. inode = nd->path.dentry->d_inode;
  1226. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1227. }
  1228. nd->inode = inode;
  1229. return 0;
  1230. }
  1231. /*
  1232. * Follow down to the covering mount currently visible to userspace. At each
  1233. * point, the filesystem owning that dentry may be queried as to whether the
  1234. * caller is permitted to proceed or not.
  1235. */
  1236. int follow_down(struct path *path)
  1237. {
  1238. unsigned managed;
  1239. int ret;
  1240. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1241. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1242. /* Allow the filesystem to manage the transit without i_mutex
  1243. * being held.
  1244. *
  1245. * We indicate to the filesystem if someone is trying to mount
  1246. * something here. This gives autofs the chance to deny anyone
  1247. * other than its daemon the right to mount on its
  1248. * superstructure.
  1249. *
  1250. * The filesystem may sleep at this point.
  1251. */
  1252. if (managed & DCACHE_MANAGE_TRANSIT) {
  1253. BUG_ON(!path->dentry->d_op);
  1254. BUG_ON(!path->dentry->d_op->d_manage);
  1255. ret = path->dentry->d_op->d_manage(path, false);
  1256. if (ret < 0)
  1257. return ret == -EISDIR ? 0 : ret;
  1258. }
  1259. /* Transit to a mounted filesystem. */
  1260. if (managed & DCACHE_MOUNTED) {
  1261. struct vfsmount *mounted = lookup_mnt(path);
  1262. if (!mounted)
  1263. break;
  1264. dput(path->dentry);
  1265. mntput(path->mnt);
  1266. path->mnt = mounted;
  1267. path->dentry = dget(mounted->mnt_root);
  1268. continue;
  1269. }
  1270. /* Don't handle automount points here */
  1271. break;
  1272. }
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(follow_down);
  1276. /*
  1277. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1278. */
  1279. static void follow_mount(struct path *path)
  1280. {
  1281. while (d_mountpoint(path->dentry)) {
  1282. struct vfsmount *mounted = lookup_mnt(path);
  1283. if (!mounted)
  1284. break;
  1285. dput(path->dentry);
  1286. mntput(path->mnt);
  1287. path->mnt = mounted;
  1288. path->dentry = dget(mounted->mnt_root);
  1289. }
  1290. }
  1291. static int path_parent_directory(struct path *path)
  1292. {
  1293. struct dentry *old = path->dentry;
  1294. /* rare case of legitimate dget_parent()... */
  1295. path->dentry = dget_parent(path->dentry);
  1296. dput(old);
  1297. if (unlikely(!path_connected(path)))
  1298. return -ENOENT;
  1299. return 0;
  1300. }
  1301. static int follow_dotdot(struct nameidata *nd)
  1302. {
  1303. while(1) {
  1304. if (nd->path.dentry == nd->root.dentry &&
  1305. nd->path.mnt == nd->root.mnt) {
  1306. break;
  1307. }
  1308. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1309. int ret = path_parent_directory(&nd->path);
  1310. if (ret)
  1311. return ret;
  1312. break;
  1313. }
  1314. if (!follow_up(&nd->path))
  1315. break;
  1316. }
  1317. follow_mount(&nd->path);
  1318. nd->inode = nd->path.dentry->d_inode;
  1319. return 0;
  1320. }
  1321. /*
  1322. * This looks up the name in dcache and possibly revalidates the found dentry.
  1323. * NULL is returned if the dentry does not exist in the cache.
  1324. */
  1325. static struct dentry *lookup_dcache(const struct qstr *name,
  1326. struct dentry *dir,
  1327. unsigned int flags)
  1328. {
  1329. struct dentry *dentry = d_lookup(dir, name);
  1330. if (dentry) {
  1331. int error = d_revalidate(dentry, flags);
  1332. if (unlikely(error <= 0)) {
  1333. if (!error)
  1334. d_invalidate(dentry);
  1335. dput(dentry);
  1336. return ERR_PTR(error);
  1337. }
  1338. }
  1339. return dentry;
  1340. }
  1341. /*
  1342. * Call i_op->lookup on the dentry. The dentry must be negative and
  1343. * unhashed.
  1344. *
  1345. * dir->d_inode->i_mutex must be held
  1346. */
  1347. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1348. unsigned int flags)
  1349. {
  1350. struct dentry *old;
  1351. /* Don't create child dentry for a dead directory. */
  1352. if (unlikely(IS_DEADDIR(dir))) {
  1353. dput(dentry);
  1354. return ERR_PTR(-ENOENT);
  1355. }
  1356. old = dir->i_op->lookup(dir, dentry, flags);
  1357. if (unlikely(old)) {
  1358. dput(dentry);
  1359. dentry = old;
  1360. }
  1361. return dentry;
  1362. }
  1363. static struct dentry *__lookup_hash(const struct qstr *name,
  1364. struct dentry *base, unsigned int flags)
  1365. {
  1366. struct dentry *dentry = lookup_dcache(name, base, flags);
  1367. if (dentry)
  1368. return dentry;
  1369. dentry = d_alloc(base, name);
  1370. if (unlikely(!dentry))
  1371. return ERR_PTR(-ENOMEM);
  1372. return lookup_real(base->d_inode, dentry, flags);
  1373. }
  1374. static int lookup_fast(struct nameidata *nd,
  1375. struct path *path, struct inode **inode,
  1376. unsigned *seqp)
  1377. {
  1378. struct vfsmount *mnt = nd->path.mnt;
  1379. struct dentry *dentry, *parent = nd->path.dentry;
  1380. int status = 1;
  1381. int err;
  1382. /*
  1383. * Rename seqlock is not required here because in the off chance
  1384. * of a false negative due to a concurrent rename, the caller is
  1385. * going to fall back to non-racy lookup.
  1386. */
  1387. if (nd->flags & LOOKUP_RCU) {
  1388. unsigned seq;
  1389. bool negative;
  1390. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1391. if (unlikely(!dentry)) {
  1392. if (unlazy_walk(nd))
  1393. return -ECHILD;
  1394. return 0;
  1395. }
  1396. /*
  1397. * This sequence count validates that the inode matches
  1398. * the dentry name information from lookup.
  1399. */
  1400. *inode = d_backing_inode(dentry);
  1401. negative = d_is_negative(dentry);
  1402. if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
  1403. return -ECHILD;
  1404. /*
  1405. * This sequence count validates that the parent had no
  1406. * changes while we did the lookup of the dentry above.
  1407. *
  1408. * The memory barrier in read_seqcount_begin of child is
  1409. * enough, we can use __read_seqcount_retry here.
  1410. */
  1411. if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
  1412. return -ECHILD;
  1413. *seqp = seq;
  1414. status = d_revalidate(dentry, nd->flags);
  1415. if (likely(status > 0)) {
  1416. /*
  1417. * Note: do negative dentry check after revalidation in
  1418. * case that drops it.
  1419. */
  1420. if (unlikely(negative))
  1421. return -ENOENT;
  1422. path->mnt = mnt;
  1423. path->dentry = dentry;
  1424. if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
  1425. return 1;
  1426. }
  1427. if (unlazy_child(nd, dentry, seq))
  1428. return -ECHILD;
  1429. if (unlikely(status == -ECHILD))
  1430. /* we'd been told to redo it in non-rcu mode */
  1431. status = d_revalidate(dentry, nd->flags);
  1432. } else {
  1433. dentry = __d_lookup(parent, &nd->last);
  1434. if (unlikely(!dentry))
  1435. return 0;
  1436. status = d_revalidate(dentry, nd->flags);
  1437. }
  1438. if (unlikely(status <= 0)) {
  1439. if (!status)
  1440. d_invalidate(dentry);
  1441. dput(dentry);
  1442. return status;
  1443. }
  1444. if (unlikely(d_is_negative(dentry))) {
  1445. dput(dentry);
  1446. return -ENOENT;
  1447. }
  1448. path->mnt = mnt;
  1449. path->dentry = dentry;
  1450. err = follow_managed(path, nd);
  1451. if (likely(err > 0))
  1452. *inode = d_backing_inode(path->dentry);
  1453. return err;
  1454. }
  1455. /* Fast lookup failed, do it the slow way */
  1456. static struct dentry *lookup_slow(const struct qstr *name,
  1457. struct dentry *dir,
  1458. unsigned int flags)
  1459. {
  1460. struct dentry *dentry = ERR_PTR(-ENOENT), *old;
  1461. struct inode *inode = dir->d_inode;
  1462. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1463. inode_lock_shared(inode);
  1464. /* Don't go there if it's already dead */
  1465. if (unlikely(IS_DEADDIR(inode)))
  1466. goto out;
  1467. again:
  1468. dentry = d_alloc_parallel(dir, name, &wq);
  1469. if (IS_ERR(dentry))
  1470. goto out;
  1471. if (unlikely(!d_in_lookup(dentry))) {
  1472. if (!(flags & LOOKUP_NO_REVAL)) {
  1473. int error = d_revalidate(dentry, flags);
  1474. if (unlikely(error <= 0)) {
  1475. if (!error) {
  1476. d_invalidate(dentry);
  1477. dput(dentry);
  1478. goto again;
  1479. }
  1480. dput(dentry);
  1481. dentry = ERR_PTR(error);
  1482. }
  1483. }
  1484. } else {
  1485. old = inode->i_op->lookup(inode, dentry, flags);
  1486. d_lookup_done(dentry);
  1487. if (unlikely(old)) {
  1488. dput(dentry);
  1489. dentry = old;
  1490. }
  1491. }
  1492. out:
  1493. inode_unlock_shared(inode);
  1494. return dentry;
  1495. }
  1496. static inline int may_lookup(struct nameidata *nd)
  1497. {
  1498. if (nd->flags & LOOKUP_RCU) {
  1499. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1500. if (err != -ECHILD)
  1501. return err;
  1502. if (unlazy_walk(nd))
  1503. return -ECHILD;
  1504. }
  1505. return inode_permission(nd->inode, MAY_EXEC);
  1506. }
  1507. static inline int handle_dots(struct nameidata *nd, int type)
  1508. {
  1509. if (type == LAST_DOTDOT) {
  1510. if (!nd->root.mnt)
  1511. set_root(nd);
  1512. if (nd->flags & LOOKUP_RCU) {
  1513. return follow_dotdot_rcu(nd);
  1514. } else
  1515. return follow_dotdot(nd);
  1516. }
  1517. return 0;
  1518. }
  1519. static int pick_link(struct nameidata *nd, struct path *link,
  1520. struct inode *inode, unsigned seq)
  1521. {
  1522. int error;
  1523. struct saved *last;
  1524. if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
  1525. path_to_nameidata(link, nd);
  1526. return -ELOOP;
  1527. }
  1528. if (!(nd->flags & LOOKUP_RCU)) {
  1529. if (link->mnt == nd->path.mnt)
  1530. mntget(link->mnt);
  1531. }
  1532. error = nd_alloc_stack(nd);
  1533. if (unlikely(error)) {
  1534. if (error == -ECHILD) {
  1535. if (unlikely(!legitimize_path(nd, link, seq))) {
  1536. drop_links(nd);
  1537. nd->depth = 0;
  1538. nd->flags &= ~LOOKUP_RCU;
  1539. nd->path.mnt = NULL;
  1540. nd->path.dentry = NULL;
  1541. if (!(nd->flags & LOOKUP_ROOT))
  1542. nd->root.mnt = NULL;
  1543. rcu_read_unlock();
  1544. } else if (likely(unlazy_walk(nd)) == 0)
  1545. error = nd_alloc_stack(nd);
  1546. }
  1547. if (error) {
  1548. path_put(link);
  1549. return error;
  1550. }
  1551. }
  1552. last = nd->stack + nd->depth++;
  1553. last->link = *link;
  1554. clear_delayed_call(&last->done);
  1555. nd->link_inode = inode;
  1556. last->seq = seq;
  1557. return 1;
  1558. }
  1559. enum {WALK_FOLLOW = 1, WALK_MORE = 2};
  1560. /*
  1561. * Do we need to follow links? We _really_ want to be able
  1562. * to do this check without having to look at inode->i_op,
  1563. * so we keep a cache of "no, this doesn't need follow_link"
  1564. * for the common case.
  1565. */
  1566. static inline int step_into(struct nameidata *nd, struct path *path,
  1567. int flags, struct inode *inode, unsigned seq)
  1568. {
  1569. if (!(flags & WALK_MORE) && nd->depth)
  1570. put_link(nd);
  1571. if (likely(!d_is_symlink(path->dentry)) ||
  1572. !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
  1573. /* not a symlink or should not follow */
  1574. path_to_nameidata(path, nd);
  1575. nd->inode = inode;
  1576. nd->seq = seq;
  1577. return 0;
  1578. }
  1579. /* make sure that d_is_symlink above matches inode */
  1580. if (nd->flags & LOOKUP_RCU) {
  1581. if (read_seqcount_retry(&path->dentry->d_seq, seq))
  1582. return -ECHILD;
  1583. }
  1584. return pick_link(nd, path, inode, seq);
  1585. }
  1586. static int walk_component(struct nameidata *nd, int flags)
  1587. {
  1588. struct path path;
  1589. struct inode *inode;
  1590. unsigned seq;
  1591. int err;
  1592. /*
  1593. * "." and ".." are special - ".." especially so because it has
  1594. * to be able to know about the current root directory and
  1595. * parent relationships.
  1596. */
  1597. if (unlikely(nd->last_type != LAST_NORM)) {
  1598. err = handle_dots(nd, nd->last_type);
  1599. if (!(flags & WALK_MORE) && nd->depth)
  1600. put_link(nd);
  1601. return err;
  1602. }
  1603. err = lookup_fast(nd, &path, &inode, &seq);
  1604. if (unlikely(err <= 0)) {
  1605. if (err < 0)
  1606. return err;
  1607. path.dentry = lookup_slow(&nd->last, nd->path.dentry,
  1608. nd->flags);
  1609. if (IS_ERR(path.dentry))
  1610. return PTR_ERR(path.dentry);
  1611. path.mnt = nd->path.mnt;
  1612. err = follow_managed(&path, nd);
  1613. if (unlikely(err < 0))
  1614. return err;
  1615. if (unlikely(d_is_negative(path.dentry))) {
  1616. path_to_nameidata(&path, nd);
  1617. return -ENOENT;
  1618. }
  1619. seq = 0; /* we are already out of RCU mode */
  1620. inode = d_backing_inode(path.dentry);
  1621. }
  1622. return step_into(nd, &path, flags, inode, seq);
  1623. }
  1624. /*
  1625. * We can do the critical dentry name comparison and hashing
  1626. * operations one word at a time, but we are limited to:
  1627. *
  1628. * - Architectures with fast unaligned word accesses. We could
  1629. * do a "get_unaligned()" if this helps and is sufficiently
  1630. * fast.
  1631. *
  1632. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1633. * do not trap on the (extremely unlikely) case of a page
  1634. * crossing operation.
  1635. *
  1636. * - Furthermore, we need an efficient 64-bit compile for the
  1637. * 64-bit case in order to generate the "number of bytes in
  1638. * the final mask". Again, that could be replaced with a
  1639. * efficient population count instruction or similar.
  1640. */
  1641. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1642. #include <asm/word-at-a-time.h>
  1643. #ifdef HASH_MIX
  1644. /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
  1645. #elif defined(CONFIG_64BIT)
  1646. /*
  1647. * Register pressure in the mixing function is an issue, particularly
  1648. * on 32-bit x86, but almost any function requires one state value and
  1649. * one temporary. Instead, use a function designed for two state values
  1650. * and no temporaries.
  1651. *
  1652. * This function cannot create a collision in only two iterations, so
  1653. * we have two iterations to achieve avalanche. In those two iterations,
  1654. * we have six layers of mixing, which is enough to spread one bit's
  1655. * influence out to 2^6 = 64 state bits.
  1656. *
  1657. * Rotate constants are scored by considering either 64 one-bit input
  1658. * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
  1659. * probability of that delta causing a change to each of the 128 output
  1660. * bits, using a sample of random initial states.
  1661. *
  1662. * The Shannon entropy of the computed probabilities is then summed
  1663. * to produce a score. Ideally, any input change has a 50% chance of
  1664. * toggling any given output bit.
  1665. *
  1666. * Mixing scores (in bits) for (12,45):
  1667. * Input delta: 1-bit 2-bit
  1668. * 1 round: 713.3 42542.6
  1669. * 2 rounds: 2753.7 140389.8
  1670. * 3 rounds: 5954.1 233458.2
  1671. * 4 rounds: 7862.6 256672.2
  1672. * Perfect: 8192 258048
  1673. * (64*128) (64*63/2 * 128)
  1674. */
  1675. #define HASH_MIX(x, y, a) \
  1676. ( x ^= (a), \
  1677. y ^= x, x = rol64(x,12),\
  1678. x += y, y = rol64(y,45),\
  1679. y *= 9 )
  1680. /*
  1681. * Fold two longs into one 32-bit hash value. This must be fast, but
  1682. * latency isn't quite as critical, as there is a fair bit of additional
  1683. * work done before the hash value is used.
  1684. */
  1685. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1686. {
  1687. y ^= x * GOLDEN_RATIO_64;
  1688. y *= GOLDEN_RATIO_64;
  1689. return y >> 32;
  1690. }
  1691. #else /* 32-bit case */
  1692. /*
  1693. * Mixing scores (in bits) for (7,20):
  1694. * Input delta: 1-bit 2-bit
  1695. * 1 round: 330.3 9201.6
  1696. * 2 rounds: 1246.4 25475.4
  1697. * 3 rounds: 1907.1 31295.1
  1698. * 4 rounds: 2042.3 31718.6
  1699. * Perfect: 2048 31744
  1700. * (32*64) (32*31/2 * 64)
  1701. */
  1702. #define HASH_MIX(x, y, a) \
  1703. ( x ^= (a), \
  1704. y ^= x, x = rol32(x, 7),\
  1705. x += y, y = rol32(y,20),\
  1706. y *= 9 )
  1707. static inline unsigned int fold_hash(unsigned long x, unsigned long y)
  1708. {
  1709. /* Use arch-optimized multiply if one exists */
  1710. return __hash_32(y ^ __hash_32(x));
  1711. }
  1712. #endif
  1713. /*
  1714. * Return the hash of a string of known length. This is carfully
  1715. * designed to match hash_name(), which is the more critical function.
  1716. * In particular, we must end by hashing a final word containing 0..7
  1717. * payload bytes, to match the way that hash_name() iterates until it
  1718. * finds the delimiter after the name.
  1719. */
  1720. unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
  1721. {
  1722. unsigned long a, x = 0, y = (unsigned long)salt;
  1723. for (;;) {
  1724. if (!len)
  1725. goto done;
  1726. a = load_unaligned_zeropad(name);
  1727. if (len < sizeof(unsigned long))
  1728. break;
  1729. HASH_MIX(x, y, a);
  1730. name += sizeof(unsigned long);
  1731. len -= sizeof(unsigned long);
  1732. }
  1733. x ^= a & bytemask_from_count(len);
  1734. done:
  1735. return fold_hash(x, y);
  1736. }
  1737. EXPORT_SYMBOL(full_name_hash);
  1738. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1739. u64 hashlen_string(const void *salt, const char *name)
  1740. {
  1741. unsigned long a = 0, x = 0, y = (unsigned long)salt;
  1742. unsigned long adata, mask, len;
  1743. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1744. len = 0;
  1745. goto inside;
  1746. do {
  1747. HASH_MIX(x, y, a);
  1748. len += sizeof(unsigned long);
  1749. inside:
  1750. a = load_unaligned_zeropad(name+len);
  1751. } while (!has_zero(a, &adata, &constants));
  1752. adata = prep_zero_mask(a, adata, &constants);
  1753. mask = create_zero_mask(adata);
  1754. x ^= a & zero_bytemask(mask);
  1755. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1756. }
  1757. EXPORT_SYMBOL(hashlen_string);
  1758. /*
  1759. * Calculate the length and hash of the path component, and
  1760. * return the "hash_len" as the result.
  1761. */
  1762. static inline u64 hash_name(const void *salt, const char *name)
  1763. {
  1764. unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
  1765. unsigned long adata, bdata, mask, len;
  1766. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1767. len = 0;
  1768. goto inside;
  1769. do {
  1770. HASH_MIX(x, y, a);
  1771. len += sizeof(unsigned long);
  1772. inside:
  1773. a = load_unaligned_zeropad(name+len);
  1774. b = a ^ REPEAT_BYTE('/');
  1775. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1776. adata = prep_zero_mask(a, adata, &constants);
  1777. bdata = prep_zero_mask(b, bdata, &constants);
  1778. mask = create_zero_mask(adata | bdata);
  1779. x ^= a & zero_bytemask(mask);
  1780. return hashlen_create(fold_hash(x, y), len + find_zero(mask));
  1781. }
  1782. #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
  1783. /* Return the hash of a string of known length */
  1784. unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
  1785. {
  1786. unsigned long hash = init_name_hash(salt);
  1787. while (len--)
  1788. hash = partial_name_hash((unsigned char)*name++, hash);
  1789. return end_name_hash(hash);
  1790. }
  1791. EXPORT_SYMBOL(full_name_hash);
  1792. /* Return the "hash_len" (hash and length) of a null-terminated string */
  1793. u64 hashlen_string(const void *salt, const char *name)
  1794. {
  1795. unsigned long hash = init_name_hash(salt);
  1796. unsigned long len = 0, c;
  1797. c = (unsigned char)*name;
  1798. while (c) {
  1799. len++;
  1800. hash = partial_name_hash(c, hash);
  1801. c = (unsigned char)name[len];
  1802. }
  1803. return hashlen_create(end_name_hash(hash), len);
  1804. }
  1805. EXPORT_SYMBOL(hashlen_string);
  1806. /*
  1807. * We know there's a real path component here of at least
  1808. * one character.
  1809. */
  1810. static inline u64 hash_name(const void *salt, const char *name)
  1811. {
  1812. unsigned long hash = init_name_hash(salt);
  1813. unsigned long len = 0, c;
  1814. c = (unsigned char)*name;
  1815. do {
  1816. len++;
  1817. hash = partial_name_hash(c, hash);
  1818. c = (unsigned char)name[len];
  1819. } while (c && c != '/');
  1820. return hashlen_create(end_name_hash(hash), len);
  1821. }
  1822. #endif
  1823. /*
  1824. * Name resolution.
  1825. * This is the basic name resolution function, turning a pathname into
  1826. * the final dentry. We expect 'base' to be positive and a directory.
  1827. *
  1828. * Returns 0 and nd will have valid dentry and mnt on success.
  1829. * Returns error and drops reference to input namei data on failure.
  1830. */
  1831. static int link_path_walk(const char *name, struct nameidata *nd)
  1832. {
  1833. int err;
  1834. while (*name=='/')
  1835. name++;
  1836. if (!*name)
  1837. return 0;
  1838. /* At this point we know we have a real path component. */
  1839. for(;;) {
  1840. u64 hash_len;
  1841. int type;
  1842. err = may_lookup(nd);
  1843. if (err)
  1844. return err;
  1845. hash_len = hash_name(nd->path.dentry, name);
  1846. type = LAST_NORM;
  1847. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1848. case 2:
  1849. if (name[1] == '.') {
  1850. type = LAST_DOTDOT;
  1851. nd->flags |= LOOKUP_JUMPED;
  1852. }
  1853. break;
  1854. case 1:
  1855. type = LAST_DOT;
  1856. }
  1857. if (likely(type == LAST_NORM)) {
  1858. struct dentry *parent = nd->path.dentry;
  1859. nd->flags &= ~LOOKUP_JUMPED;
  1860. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1861. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1862. err = parent->d_op->d_hash(parent, &this);
  1863. if (err < 0)
  1864. return err;
  1865. hash_len = this.hash_len;
  1866. name = this.name;
  1867. }
  1868. }
  1869. nd->last.hash_len = hash_len;
  1870. nd->last.name = name;
  1871. nd->last_type = type;
  1872. name += hashlen_len(hash_len);
  1873. if (!*name)
  1874. goto OK;
  1875. /*
  1876. * If it wasn't NUL, we know it was '/'. Skip that
  1877. * slash, and continue until no more slashes.
  1878. */
  1879. do {
  1880. name++;
  1881. } while (unlikely(*name == '/'));
  1882. if (unlikely(!*name)) {
  1883. OK:
  1884. /* pathname body, done */
  1885. if (!nd->depth)
  1886. return 0;
  1887. name = nd->stack[nd->depth - 1].name;
  1888. /* trailing symlink, done */
  1889. if (!name)
  1890. return 0;
  1891. /* last component of nested symlink */
  1892. err = walk_component(nd, WALK_FOLLOW);
  1893. } else {
  1894. /* not the last component */
  1895. err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
  1896. }
  1897. if (err < 0)
  1898. return err;
  1899. if (err) {
  1900. const char *s = get_link(nd);
  1901. if (IS_ERR(s))
  1902. return PTR_ERR(s);
  1903. err = 0;
  1904. if (unlikely(!s)) {
  1905. /* jumped */
  1906. put_link(nd);
  1907. } else {
  1908. nd->stack[nd->depth - 1].name = name;
  1909. name = s;
  1910. continue;
  1911. }
  1912. }
  1913. if (unlikely(!d_can_lookup(nd->path.dentry))) {
  1914. if (nd->flags & LOOKUP_RCU) {
  1915. if (unlazy_walk(nd))
  1916. return -ECHILD;
  1917. }
  1918. return -ENOTDIR;
  1919. }
  1920. }
  1921. }
  1922. static const char *path_init(struct nameidata *nd, unsigned flags)
  1923. {
  1924. const char *s = nd->name->name;
  1925. if (!*s)
  1926. flags &= ~LOOKUP_RCU;
  1927. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1928. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1929. nd->depth = 0;
  1930. if (flags & LOOKUP_ROOT) {
  1931. struct dentry *root = nd->root.dentry;
  1932. struct inode *inode = root->d_inode;
  1933. if (*s && unlikely(!d_can_lookup(root)))
  1934. return ERR_PTR(-ENOTDIR);
  1935. nd->path = nd->root;
  1936. nd->inode = inode;
  1937. if (flags & LOOKUP_RCU) {
  1938. rcu_read_lock();
  1939. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1940. nd->root_seq = nd->seq;
  1941. nd->m_seq = read_seqbegin(&mount_lock);
  1942. } else {
  1943. path_get(&nd->path);
  1944. }
  1945. return s;
  1946. }
  1947. nd->root.mnt = NULL;
  1948. nd->path.mnt = NULL;
  1949. nd->path.dentry = NULL;
  1950. nd->m_seq = read_seqbegin(&mount_lock);
  1951. if (*s == '/') {
  1952. if (flags & LOOKUP_RCU)
  1953. rcu_read_lock();
  1954. set_root(nd);
  1955. if (likely(!nd_jump_root(nd)))
  1956. return s;
  1957. nd->root.mnt = NULL;
  1958. rcu_read_unlock();
  1959. return ERR_PTR(-ECHILD);
  1960. } else if (nd->dfd == AT_FDCWD) {
  1961. if (flags & LOOKUP_RCU) {
  1962. struct fs_struct *fs = current->fs;
  1963. unsigned seq;
  1964. rcu_read_lock();
  1965. do {
  1966. seq = read_seqcount_begin(&fs->seq);
  1967. nd->path = fs->pwd;
  1968. nd->inode = nd->path.dentry->d_inode;
  1969. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1970. } while (read_seqcount_retry(&fs->seq, seq));
  1971. } else {
  1972. get_fs_pwd(current->fs, &nd->path);
  1973. nd->inode = nd->path.dentry->d_inode;
  1974. }
  1975. return s;
  1976. } else {
  1977. /* Caller must check execute permissions on the starting path component */
  1978. struct fd f = fdget_raw(nd->dfd);
  1979. struct dentry *dentry;
  1980. if (!f.file)
  1981. return ERR_PTR(-EBADF);
  1982. dentry = f.file->f_path.dentry;
  1983. if (*s) {
  1984. if (!d_can_lookup(dentry)) {
  1985. fdput(f);
  1986. return ERR_PTR(-ENOTDIR);
  1987. }
  1988. }
  1989. nd->path = f.file->f_path;
  1990. if (flags & LOOKUP_RCU) {
  1991. rcu_read_lock();
  1992. nd->inode = nd->path.dentry->d_inode;
  1993. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1994. } else {
  1995. path_get(&nd->path);
  1996. nd->inode = nd->path.dentry->d_inode;
  1997. }
  1998. fdput(f);
  1999. return s;
  2000. }
  2001. }
  2002. static const char *trailing_symlink(struct nameidata *nd)
  2003. {
  2004. const char *s;
  2005. int error = may_follow_link(nd);
  2006. if (unlikely(error))
  2007. return ERR_PTR(error);
  2008. nd->flags |= LOOKUP_PARENT;
  2009. nd->stack[0].name = NULL;
  2010. s = get_link(nd);
  2011. return s ? s : "";
  2012. }
  2013. static inline int lookup_last(struct nameidata *nd)
  2014. {
  2015. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  2016. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2017. nd->flags &= ~LOOKUP_PARENT;
  2018. return walk_component(nd, 0);
  2019. }
  2020. static int handle_lookup_down(struct nameidata *nd)
  2021. {
  2022. struct path path = nd->path;
  2023. struct inode *inode = nd->inode;
  2024. unsigned seq = nd->seq;
  2025. int err;
  2026. if (nd->flags & LOOKUP_RCU) {
  2027. /*
  2028. * don't bother with unlazy_walk on failure - we are
  2029. * at the very beginning of walk, so we lose nothing
  2030. * if we simply redo everything in non-RCU mode
  2031. */
  2032. if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
  2033. return -ECHILD;
  2034. } else {
  2035. dget(path.dentry);
  2036. err = follow_managed(&path, nd);
  2037. if (unlikely(err < 0))
  2038. return err;
  2039. inode = d_backing_inode(path.dentry);
  2040. seq = 0;
  2041. }
  2042. path_to_nameidata(&path, nd);
  2043. nd->inode = inode;
  2044. nd->seq = seq;
  2045. return 0;
  2046. }
  2047. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  2048. static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
  2049. {
  2050. const char *s = path_init(nd, flags);
  2051. int err;
  2052. if (IS_ERR(s))
  2053. return PTR_ERR(s);
  2054. if (unlikely(flags & LOOKUP_DOWN)) {
  2055. err = handle_lookup_down(nd);
  2056. if (unlikely(err < 0)) {
  2057. terminate_walk(nd);
  2058. return err;
  2059. }
  2060. }
  2061. while (!(err = link_path_walk(s, nd))
  2062. && ((err = lookup_last(nd)) > 0)) {
  2063. s = trailing_symlink(nd);
  2064. if (IS_ERR(s)) {
  2065. err = PTR_ERR(s);
  2066. break;
  2067. }
  2068. }
  2069. if (!err)
  2070. err = complete_walk(nd);
  2071. if (!err && nd->flags & LOOKUP_DIRECTORY)
  2072. if (!d_can_lookup(nd->path.dentry))
  2073. err = -ENOTDIR;
  2074. if (!err) {
  2075. *path = nd->path;
  2076. nd->path.mnt = NULL;
  2077. nd->path.dentry = NULL;
  2078. }
  2079. terminate_walk(nd);
  2080. return err;
  2081. }
  2082. static int filename_lookup(int dfd, struct filename *name, unsigned flags,
  2083. struct path *path, struct path *root)
  2084. {
  2085. int retval;
  2086. struct nameidata nd;
  2087. if (IS_ERR(name))
  2088. return PTR_ERR(name);
  2089. if (unlikely(root)) {
  2090. nd.root = *root;
  2091. flags |= LOOKUP_ROOT;
  2092. }
  2093. set_nameidata(&nd, dfd, name);
  2094. retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
  2095. if (unlikely(retval == -ECHILD))
  2096. retval = path_lookupat(&nd, flags, path);
  2097. if (unlikely(retval == -ESTALE))
  2098. retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
  2099. if (likely(!retval))
  2100. audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
  2101. restore_nameidata();
  2102. putname(name);
  2103. return retval;
  2104. }
  2105. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  2106. static int path_parentat(struct nameidata *nd, unsigned flags,
  2107. struct path *parent)
  2108. {
  2109. const char *s = path_init(nd, flags);
  2110. int err;
  2111. if (IS_ERR(s))
  2112. return PTR_ERR(s);
  2113. err = link_path_walk(s, nd);
  2114. if (!err)
  2115. err = complete_walk(nd);
  2116. if (!err) {
  2117. *parent = nd->path;
  2118. nd->path.mnt = NULL;
  2119. nd->path.dentry = NULL;
  2120. }
  2121. terminate_walk(nd);
  2122. return err;
  2123. }
  2124. static struct filename *filename_parentat(int dfd, struct filename *name,
  2125. unsigned int flags, struct path *parent,
  2126. struct qstr *last, int *type)
  2127. {
  2128. int retval;
  2129. struct nameidata nd;
  2130. if (IS_ERR(name))
  2131. return name;
  2132. set_nameidata(&nd, dfd, name);
  2133. retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
  2134. if (unlikely(retval == -ECHILD))
  2135. retval = path_parentat(&nd, flags, parent);
  2136. if (unlikely(retval == -ESTALE))
  2137. retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
  2138. if (likely(!retval)) {
  2139. *last = nd.last;
  2140. *type = nd.last_type;
  2141. audit_inode(name, parent->dentry, LOOKUP_PARENT);
  2142. } else {
  2143. putname(name);
  2144. name = ERR_PTR(retval);
  2145. }
  2146. restore_nameidata();
  2147. return name;
  2148. }
  2149. /* does lookup, returns the object with parent locked */
  2150. struct dentry *kern_path_locked(const char *name, struct path *path)
  2151. {
  2152. struct filename *filename;
  2153. struct dentry *d;
  2154. struct qstr last;
  2155. int type;
  2156. filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
  2157. &last, &type);
  2158. if (IS_ERR(filename))
  2159. return ERR_CAST(filename);
  2160. if (unlikely(type != LAST_NORM)) {
  2161. path_put(path);
  2162. putname(filename);
  2163. return ERR_PTR(-EINVAL);
  2164. }
  2165. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  2166. d = __lookup_hash(&last, path->dentry, 0);
  2167. if (IS_ERR(d)) {
  2168. inode_unlock(path->dentry->d_inode);
  2169. path_put(path);
  2170. }
  2171. putname(filename);
  2172. return d;
  2173. }
  2174. int kern_path(const char *name, unsigned int flags, struct path *path)
  2175. {
  2176. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2177. flags, path, NULL);
  2178. }
  2179. EXPORT_SYMBOL(kern_path);
  2180. /**
  2181. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  2182. * @dentry: pointer to dentry of the base directory
  2183. * @mnt: pointer to vfs mount of the base directory
  2184. * @name: pointer to file name
  2185. * @flags: lookup flags
  2186. * @path: pointer to struct path to fill
  2187. */
  2188. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  2189. const char *name, unsigned int flags,
  2190. struct path *path)
  2191. {
  2192. struct path root = {.mnt = mnt, .dentry = dentry};
  2193. /* the first argument of filename_lookup() is ignored with root */
  2194. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2195. flags , path, &root);
  2196. }
  2197. EXPORT_SYMBOL(vfs_path_lookup);
  2198. /**
  2199. * lookup_one_len - filesystem helper to lookup single pathname component
  2200. * @name: pathname component to lookup
  2201. * @base: base directory to lookup from
  2202. * @len: maximum length @len should be interpreted to
  2203. *
  2204. * Note that this routine is purely a helper for filesystem usage and should
  2205. * not be called by generic code.
  2206. *
  2207. * The caller must hold base->i_mutex.
  2208. */
  2209. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  2210. {
  2211. struct qstr this;
  2212. unsigned int c;
  2213. int err;
  2214. WARN_ON_ONCE(!inode_is_locked(base->d_inode));
  2215. this.name = name;
  2216. this.len = len;
  2217. this.hash = full_name_hash(base, name, len);
  2218. if (!len)
  2219. return ERR_PTR(-EACCES);
  2220. if (unlikely(name[0] == '.')) {
  2221. if (len < 2 || (len == 2 && name[1] == '.'))
  2222. return ERR_PTR(-EACCES);
  2223. }
  2224. while (len--) {
  2225. c = *(const unsigned char *)name++;
  2226. if (c == '/' || c == '\0')
  2227. return ERR_PTR(-EACCES);
  2228. }
  2229. /*
  2230. * See if the low-level filesystem might want
  2231. * to use its own hash..
  2232. */
  2233. if (base->d_flags & DCACHE_OP_HASH) {
  2234. int err = base->d_op->d_hash(base, &this);
  2235. if (err < 0)
  2236. return ERR_PTR(err);
  2237. }
  2238. err = inode_permission(base->d_inode, MAY_EXEC);
  2239. if (err)
  2240. return ERR_PTR(err);
  2241. return __lookup_hash(&this, base, 0);
  2242. }
  2243. EXPORT_SYMBOL(lookup_one_len);
  2244. /**
  2245. * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
  2246. * @name: pathname component to lookup
  2247. * @base: base directory to lookup from
  2248. * @len: maximum length @len should be interpreted to
  2249. *
  2250. * Note that this routine is purely a helper for filesystem usage and should
  2251. * not be called by generic code.
  2252. *
  2253. * Unlike lookup_one_len, it should be called without the parent
  2254. * i_mutex held, and will take the i_mutex itself if necessary.
  2255. */
  2256. struct dentry *lookup_one_len_unlocked(const char *name,
  2257. struct dentry *base, int len)
  2258. {
  2259. struct qstr this;
  2260. unsigned int c;
  2261. int err;
  2262. struct dentry *ret;
  2263. this.name = name;
  2264. this.len = len;
  2265. this.hash = full_name_hash(base, name, len);
  2266. if (!len)
  2267. return ERR_PTR(-EACCES);
  2268. if (unlikely(name[0] == '.')) {
  2269. if (len < 2 || (len == 2 && name[1] == '.'))
  2270. return ERR_PTR(-EACCES);
  2271. }
  2272. while (len--) {
  2273. c = *(const unsigned char *)name++;
  2274. if (c == '/' || c == '\0')
  2275. return ERR_PTR(-EACCES);
  2276. }
  2277. /*
  2278. * See if the low-level filesystem might want
  2279. * to use its own hash..
  2280. */
  2281. if (base->d_flags & DCACHE_OP_HASH) {
  2282. int err = base->d_op->d_hash(base, &this);
  2283. if (err < 0)
  2284. return ERR_PTR(err);
  2285. }
  2286. err = inode_permission(base->d_inode, MAY_EXEC);
  2287. if (err)
  2288. return ERR_PTR(err);
  2289. ret = lookup_dcache(&this, base, 0);
  2290. if (!ret)
  2291. ret = lookup_slow(&this, base, 0);
  2292. return ret;
  2293. }
  2294. EXPORT_SYMBOL(lookup_one_len_unlocked);
  2295. #ifdef CONFIG_UNIX98_PTYS
  2296. int path_pts(struct path *path)
  2297. {
  2298. /* Find something mounted on "pts" in the same directory as
  2299. * the input path.
  2300. */
  2301. struct dentry *child, *parent;
  2302. struct qstr this;
  2303. int ret;
  2304. ret = path_parent_directory(path);
  2305. if (ret)
  2306. return ret;
  2307. parent = path->dentry;
  2308. this.name = "pts";
  2309. this.len = 3;
  2310. child = d_hash_and_lookup(parent, &this);
  2311. if (!child)
  2312. return -ENOENT;
  2313. path->dentry = child;
  2314. dput(parent);
  2315. follow_mount(path);
  2316. return 0;
  2317. }
  2318. #endif
  2319. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  2320. struct path *path, int *empty)
  2321. {
  2322. return filename_lookup(dfd, getname_flags(name, flags, empty),
  2323. flags, path, NULL);
  2324. }
  2325. EXPORT_SYMBOL(user_path_at_empty);
  2326. /**
  2327. * mountpoint_last - look up last component for umount
  2328. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  2329. *
  2330. * This is a special lookup_last function just for umount. In this case, we
  2331. * need to resolve the path without doing any revalidation.
  2332. *
  2333. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  2334. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  2335. * in almost all cases, this lookup will be served out of the dcache. The only
  2336. * cases where it won't are if nd->last refers to a symlink or the path is
  2337. * bogus and it doesn't exist.
  2338. *
  2339. * Returns:
  2340. * -error: if there was an error during lookup. This includes -ENOENT if the
  2341. * lookup found a negative dentry.
  2342. *
  2343. * 0: if we successfully resolved nd->last and found it to not to be a
  2344. * symlink that needs to be followed.
  2345. *
  2346. * 1: if we successfully resolved nd->last and found it to be a symlink
  2347. * that needs to be followed.
  2348. */
  2349. static int
  2350. mountpoint_last(struct nameidata *nd)
  2351. {
  2352. int error = 0;
  2353. struct dentry *dir = nd->path.dentry;
  2354. struct path path;
  2355. /* If we're in rcuwalk, drop out of it to handle last component */
  2356. if (nd->flags & LOOKUP_RCU) {
  2357. if (unlazy_walk(nd))
  2358. return -ECHILD;
  2359. }
  2360. nd->flags &= ~LOOKUP_PARENT;
  2361. if (unlikely(nd->last_type != LAST_NORM)) {
  2362. error = handle_dots(nd, nd->last_type);
  2363. if (error)
  2364. return error;
  2365. path.dentry = dget(nd->path.dentry);
  2366. } else {
  2367. path.dentry = d_lookup(dir, &nd->last);
  2368. if (!path.dentry) {
  2369. /*
  2370. * No cached dentry. Mounted dentries are pinned in the
  2371. * cache, so that means that this dentry is probably
  2372. * a symlink or the path doesn't actually point
  2373. * to a mounted dentry.
  2374. */
  2375. path.dentry = lookup_slow(&nd->last, dir,
  2376. nd->flags | LOOKUP_NO_REVAL);
  2377. if (IS_ERR(path.dentry))
  2378. return PTR_ERR(path.dentry);
  2379. }
  2380. }
  2381. if (d_is_negative(path.dentry)) {
  2382. dput(path.dentry);
  2383. return -ENOENT;
  2384. }
  2385. path.mnt = nd->path.mnt;
  2386. return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
  2387. }
  2388. /**
  2389. * path_mountpoint - look up a path to be umounted
  2390. * @nd: lookup context
  2391. * @flags: lookup flags
  2392. * @path: pointer to container for result
  2393. *
  2394. * Look up the given name, but don't attempt to revalidate the last component.
  2395. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2396. */
  2397. static int
  2398. path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
  2399. {
  2400. const char *s = path_init(nd, flags);
  2401. int err;
  2402. if (IS_ERR(s))
  2403. return PTR_ERR(s);
  2404. while (!(err = link_path_walk(s, nd)) &&
  2405. (err = mountpoint_last(nd)) > 0) {
  2406. s = trailing_symlink(nd);
  2407. if (IS_ERR(s)) {
  2408. err = PTR_ERR(s);
  2409. break;
  2410. }
  2411. }
  2412. if (!err) {
  2413. *path = nd->path;
  2414. nd->path.mnt = NULL;
  2415. nd->path.dentry = NULL;
  2416. follow_mount(path);
  2417. }
  2418. terminate_walk(nd);
  2419. return err;
  2420. }
  2421. static int
  2422. filename_mountpoint(int dfd, struct filename *name, struct path *path,
  2423. unsigned int flags)
  2424. {
  2425. struct nameidata nd;
  2426. int error;
  2427. if (IS_ERR(name))
  2428. return PTR_ERR(name);
  2429. set_nameidata(&nd, dfd, name);
  2430. error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
  2431. if (unlikely(error == -ECHILD))
  2432. error = path_mountpoint(&nd, flags, path);
  2433. if (unlikely(error == -ESTALE))
  2434. error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
  2435. if (likely(!error))
  2436. audit_inode(name, path->dentry, 0);
  2437. restore_nameidata();
  2438. putname(name);
  2439. return error;
  2440. }
  2441. /**
  2442. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2443. * @dfd: directory file descriptor
  2444. * @name: pathname from userland
  2445. * @flags: lookup flags
  2446. * @path: pointer to container to hold result
  2447. *
  2448. * A umount is a special case for path walking. We're not actually interested
  2449. * in the inode in this situation, and ESTALE errors can be a problem. We
  2450. * simply want track down the dentry and vfsmount attached at the mountpoint
  2451. * and avoid revalidating the last component.
  2452. *
  2453. * Returns 0 and populates "path" on success.
  2454. */
  2455. int
  2456. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2457. struct path *path)
  2458. {
  2459. return filename_mountpoint(dfd, getname(name), path, flags);
  2460. }
  2461. int
  2462. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2463. unsigned int flags)
  2464. {
  2465. return filename_mountpoint(dfd, getname_kernel(name), path, flags);
  2466. }
  2467. EXPORT_SYMBOL(kern_path_mountpoint);
  2468. int __check_sticky(struct inode *dir, struct inode *inode)
  2469. {
  2470. kuid_t fsuid = current_fsuid();
  2471. if (uid_eq(inode->i_uid, fsuid))
  2472. return 0;
  2473. if (uid_eq(dir->i_uid, fsuid))
  2474. return 0;
  2475. return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
  2476. }
  2477. EXPORT_SYMBOL(__check_sticky);
  2478. /*
  2479. * Check whether we can remove a link victim from directory dir, check
  2480. * whether the type of victim is right.
  2481. * 1. We can't do it if dir is read-only (done in permission())
  2482. * 2. We should have write and exec permissions on dir
  2483. * 3. We can't remove anything from append-only dir
  2484. * 4. We can't do anything with immutable dir (done in permission())
  2485. * 5. If the sticky bit on dir is set we should either
  2486. * a. be owner of dir, or
  2487. * b. be owner of victim, or
  2488. * c. have CAP_FOWNER capability
  2489. * 6. If the victim is append-only or immutable we can't do antyhing with
  2490. * links pointing to it.
  2491. * 7. If the victim has an unknown uid or gid we can't change the inode.
  2492. * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2493. * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2494. * 10. We can't remove a root or mountpoint.
  2495. * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
  2496. * nfs_async_unlink().
  2497. */
  2498. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2499. {
  2500. struct inode *inode = d_backing_inode(victim);
  2501. int error;
  2502. if (d_is_negative(victim))
  2503. return -ENOENT;
  2504. BUG_ON(!inode);
  2505. BUG_ON(victim->d_parent->d_inode != dir);
  2506. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2507. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2508. if (error)
  2509. return error;
  2510. if (IS_APPEND(dir))
  2511. return -EPERM;
  2512. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2513. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
  2514. return -EPERM;
  2515. if (isdir) {
  2516. if (!d_is_dir(victim))
  2517. return -ENOTDIR;
  2518. if (IS_ROOT(victim))
  2519. return -EBUSY;
  2520. } else if (d_is_dir(victim))
  2521. return -EISDIR;
  2522. if (IS_DEADDIR(dir))
  2523. return -ENOENT;
  2524. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2525. return -EBUSY;
  2526. return 0;
  2527. }
  2528. /* Check whether we can create an object with dentry child in directory
  2529. * dir.
  2530. * 1. We can't do it if child already exists (open has special treatment for
  2531. * this case, but since we are inlined it's OK)
  2532. * 2. We can't do it if dir is read-only (done in permission())
  2533. * 3. We can't do it if the fs can't represent the fsuid or fsgid.
  2534. * 4. We should have write and exec permissions on dir
  2535. * 5. We can't do it if dir is immutable (done in permission())
  2536. */
  2537. static inline int may_create(struct inode *dir, struct dentry *child)
  2538. {
  2539. struct user_namespace *s_user_ns;
  2540. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2541. if (child->d_inode)
  2542. return -EEXIST;
  2543. if (IS_DEADDIR(dir))
  2544. return -ENOENT;
  2545. s_user_ns = dir->i_sb->s_user_ns;
  2546. if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
  2547. !kgid_has_mapping(s_user_ns, current_fsgid()))
  2548. return -EOVERFLOW;
  2549. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2550. }
  2551. /*
  2552. * p1 and p2 should be directories on the same fs.
  2553. */
  2554. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2555. {
  2556. struct dentry *p;
  2557. if (p1 == p2) {
  2558. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2559. return NULL;
  2560. }
  2561. mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
  2562. p = d_ancestor(p2, p1);
  2563. if (p) {
  2564. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
  2565. inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
  2566. return p;
  2567. }
  2568. p = d_ancestor(p1, p2);
  2569. if (p) {
  2570. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2571. inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
  2572. return p;
  2573. }
  2574. inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
  2575. inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
  2576. return NULL;
  2577. }
  2578. EXPORT_SYMBOL(lock_rename);
  2579. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2580. {
  2581. inode_unlock(p1->d_inode);
  2582. if (p1 != p2) {
  2583. inode_unlock(p2->d_inode);
  2584. mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
  2585. }
  2586. }
  2587. EXPORT_SYMBOL(unlock_rename);
  2588. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2589. bool want_excl)
  2590. {
  2591. int error = may_create(dir, dentry);
  2592. if (error)
  2593. return error;
  2594. if (!dir->i_op->create)
  2595. return -EACCES; /* shouldn't it be ENOSYS? */
  2596. mode &= S_IALLUGO;
  2597. mode |= S_IFREG;
  2598. error = security_inode_create(dir, dentry, mode);
  2599. if (error)
  2600. return error;
  2601. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2602. if (!error)
  2603. fsnotify_create(dir, dentry);
  2604. return error;
  2605. }
  2606. EXPORT_SYMBOL(vfs_create);
  2607. bool may_open_dev(const struct path *path)
  2608. {
  2609. return !(path->mnt->mnt_flags & MNT_NODEV) &&
  2610. !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
  2611. }
  2612. static int may_open(const struct path *path, int acc_mode, int flag)
  2613. {
  2614. struct dentry *dentry = path->dentry;
  2615. struct inode *inode = dentry->d_inode;
  2616. int error;
  2617. if (!inode)
  2618. return -ENOENT;
  2619. switch (inode->i_mode & S_IFMT) {
  2620. case S_IFLNK:
  2621. return -ELOOP;
  2622. case S_IFDIR:
  2623. if (acc_mode & MAY_WRITE)
  2624. return -EISDIR;
  2625. break;
  2626. case S_IFBLK:
  2627. case S_IFCHR:
  2628. if (!may_open_dev(path))
  2629. return -EACCES;
  2630. /*FALLTHRU*/
  2631. case S_IFIFO:
  2632. case S_IFSOCK:
  2633. flag &= ~O_TRUNC;
  2634. break;
  2635. }
  2636. error = inode_permission(inode, MAY_OPEN | acc_mode);
  2637. if (error)
  2638. return error;
  2639. /*
  2640. * An append-only file must be opened in append mode for writing.
  2641. */
  2642. if (IS_APPEND(inode)) {
  2643. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2644. return -EPERM;
  2645. if (flag & O_TRUNC)
  2646. return -EPERM;
  2647. }
  2648. /* O_NOATIME can only be set by the owner or superuser */
  2649. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2650. return -EPERM;
  2651. return 0;
  2652. }
  2653. static int handle_truncate(struct file *filp)
  2654. {
  2655. const struct path *path = &filp->f_path;
  2656. struct inode *inode = path->dentry->d_inode;
  2657. int error = get_write_access(inode);
  2658. if (error)
  2659. return error;
  2660. /*
  2661. * Refuse to truncate files with mandatory locks held on them.
  2662. */
  2663. error = locks_verify_locked(filp);
  2664. if (!error)
  2665. error = security_path_truncate(path);
  2666. if (!error) {
  2667. error = do_truncate(path->dentry, 0,
  2668. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2669. filp);
  2670. }
  2671. put_write_access(inode);
  2672. return error;
  2673. }
  2674. static inline int open_to_namei_flags(int flag)
  2675. {
  2676. if ((flag & O_ACCMODE) == 3)
  2677. flag--;
  2678. return flag;
  2679. }
  2680. static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
  2681. {
  2682. struct user_namespace *s_user_ns;
  2683. int error = security_path_mknod(dir, dentry, mode, 0);
  2684. if (error)
  2685. return error;
  2686. s_user_ns = dir->dentry->d_sb->s_user_ns;
  2687. if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
  2688. !kgid_has_mapping(s_user_ns, current_fsgid()))
  2689. return -EOVERFLOW;
  2690. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2691. if (error)
  2692. return error;
  2693. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2694. }
  2695. /*
  2696. * Attempt to atomically look up, create and open a file from a negative
  2697. * dentry.
  2698. *
  2699. * Returns 0 if successful. The file will have been created and attached to
  2700. * @file by the filesystem calling finish_open().
  2701. *
  2702. * Returns 1 if the file was looked up only or didn't need creating. The
  2703. * caller will need to perform the open themselves. @path will have been
  2704. * updated to point to the new dentry. This may be negative.
  2705. *
  2706. * Returns an error code otherwise.
  2707. */
  2708. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2709. struct path *path, struct file *file,
  2710. const struct open_flags *op,
  2711. int open_flag, umode_t mode,
  2712. int *opened)
  2713. {
  2714. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2715. struct inode *dir = nd->path.dentry->d_inode;
  2716. int error;
  2717. if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
  2718. open_flag &= ~O_TRUNC;
  2719. if (nd->flags & LOOKUP_DIRECTORY)
  2720. open_flag |= O_DIRECTORY;
  2721. file->f_path.dentry = DENTRY_NOT_SET;
  2722. file->f_path.mnt = nd->path.mnt;
  2723. error = dir->i_op->atomic_open(dir, dentry, file,
  2724. open_to_namei_flags(open_flag),
  2725. mode, opened);
  2726. d_lookup_done(dentry);
  2727. if (!error) {
  2728. /*
  2729. * We didn't have the inode before the open, so check open
  2730. * permission here.
  2731. */
  2732. int acc_mode = op->acc_mode;
  2733. if (*opened & FILE_CREATED) {
  2734. WARN_ON(!(open_flag & O_CREAT));
  2735. fsnotify_create(dir, dentry);
  2736. acc_mode = 0;
  2737. }
  2738. error = may_open(&file->f_path, acc_mode, open_flag);
  2739. if (WARN_ON(error > 0))
  2740. error = -EINVAL;
  2741. } else if (error > 0) {
  2742. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2743. error = -EIO;
  2744. } else {
  2745. if (file->f_path.dentry) {
  2746. dput(dentry);
  2747. dentry = file->f_path.dentry;
  2748. }
  2749. if (*opened & FILE_CREATED)
  2750. fsnotify_create(dir, dentry);
  2751. if (unlikely(d_is_negative(dentry))) {
  2752. error = -ENOENT;
  2753. } else {
  2754. path->dentry = dentry;
  2755. path->mnt = nd->path.mnt;
  2756. return 1;
  2757. }
  2758. }
  2759. }
  2760. dput(dentry);
  2761. return error;
  2762. }
  2763. /*
  2764. * Look up and maybe create and open the last component.
  2765. *
  2766. * Must be called with i_mutex held on parent.
  2767. *
  2768. * Returns 0 if the file was successfully atomically created (if necessary) and
  2769. * opened. In this case the file will be returned attached to @file.
  2770. *
  2771. * Returns 1 if the file was not completely opened at this time, though lookups
  2772. * and creations will have been performed and the dentry returned in @path will
  2773. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2774. * specified then a negative dentry may be returned.
  2775. *
  2776. * An error code is returned otherwise.
  2777. *
  2778. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2779. * cleared otherwise prior to returning.
  2780. */
  2781. static int lookup_open(struct nameidata *nd, struct path *path,
  2782. struct file *file,
  2783. const struct open_flags *op,
  2784. bool got_write, int *opened)
  2785. {
  2786. struct dentry *dir = nd->path.dentry;
  2787. struct inode *dir_inode = dir->d_inode;
  2788. int open_flag = op->open_flag;
  2789. struct dentry *dentry;
  2790. int error, create_error = 0;
  2791. umode_t mode = op->mode;
  2792. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  2793. if (unlikely(IS_DEADDIR(dir_inode)))
  2794. return -ENOENT;
  2795. *opened &= ~FILE_CREATED;
  2796. dentry = d_lookup(dir, &nd->last);
  2797. for (;;) {
  2798. if (!dentry) {
  2799. dentry = d_alloc_parallel(dir, &nd->last, &wq);
  2800. if (IS_ERR(dentry))
  2801. return PTR_ERR(dentry);
  2802. }
  2803. if (d_in_lookup(dentry))
  2804. break;
  2805. error = d_revalidate(dentry, nd->flags);
  2806. if (likely(error > 0))
  2807. break;
  2808. if (error)
  2809. goto out_dput;
  2810. d_invalidate(dentry);
  2811. dput(dentry);
  2812. dentry = NULL;
  2813. }
  2814. if (dentry->d_inode) {
  2815. /* Cached positive dentry: will open in f_op->open */
  2816. goto out_no_open;
  2817. }
  2818. /*
  2819. * Checking write permission is tricky, bacuse we don't know if we are
  2820. * going to actually need it: O_CREAT opens should work as long as the
  2821. * file exists. But checking existence breaks atomicity. The trick is
  2822. * to check access and if not granted clear O_CREAT from the flags.
  2823. *
  2824. * Another problem is returing the "right" error value (e.g. for an
  2825. * O_EXCL open we want to return EEXIST not EROFS).
  2826. */
  2827. if (open_flag & O_CREAT) {
  2828. if (!IS_POSIXACL(dir->d_inode))
  2829. mode &= ~current_umask();
  2830. if (unlikely(!got_write)) {
  2831. create_error = -EROFS;
  2832. open_flag &= ~O_CREAT;
  2833. if (open_flag & (O_EXCL | O_TRUNC))
  2834. goto no_open;
  2835. /* No side effects, safe to clear O_CREAT */
  2836. } else {
  2837. create_error = may_o_create(&nd->path, dentry, mode);
  2838. if (create_error) {
  2839. open_flag &= ~O_CREAT;
  2840. if (open_flag & O_EXCL)
  2841. goto no_open;
  2842. }
  2843. }
  2844. } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
  2845. unlikely(!got_write)) {
  2846. /*
  2847. * No O_CREATE -> atomicity not a requirement -> fall
  2848. * back to lookup + open
  2849. */
  2850. goto no_open;
  2851. }
  2852. if (dir_inode->i_op->atomic_open) {
  2853. error = atomic_open(nd, dentry, path, file, op, open_flag,
  2854. mode, opened);
  2855. if (unlikely(error == -ENOENT) && create_error)
  2856. error = create_error;
  2857. return error;
  2858. }
  2859. no_open:
  2860. if (d_in_lookup(dentry)) {
  2861. struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
  2862. nd->flags);
  2863. d_lookup_done(dentry);
  2864. if (unlikely(res)) {
  2865. if (IS_ERR(res)) {
  2866. error = PTR_ERR(res);
  2867. goto out_dput;
  2868. }
  2869. dput(dentry);
  2870. dentry = res;
  2871. }
  2872. }
  2873. /* Negative dentry, just create the file */
  2874. if (!dentry->d_inode && (open_flag & O_CREAT)) {
  2875. *opened |= FILE_CREATED;
  2876. audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  2877. if (!dir_inode->i_op->create) {
  2878. error = -EACCES;
  2879. goto out_dput;
  2880. }
  2881. error = dir_inode->i_op->create(dir_inode, dentry, mode,
  2882. open_flag & O_EXCL);
  2883. if (error)
  2884. goto out_dput;
  2885. fsnotify_create(dir_inode, dentry);
  2886. }
  2887. if (unlikely(create_error) && !dentry->d_inode) {
  2888. error = create_error;
  2889. goto out_dput;
  2890. }
  2891. out_no_open:
  2892. path->dentry = dentry;
  2893. path->mnt = nd->path.mnt;
  2894. return 1;
  2895. out_dput:
  2896. dput(dentry);
  2897. return error;
  2898. }
  2899. /*
  2900. * Handle the last step of open()
  2901. */
  2902. static int do_last(struct nameidata *nd,
  2903. struct file *file, const struct open_flags *op,
  2904. int *opened)
  2905. {
  2906. struct dentry *dir = nd->path.dentry;
  2907. int open_flag = op->open_flag;
  2908. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2909. bool got_write = false;
  2910. int acc_mode = op->acc_mode;
  2911. unsigned seq;
  2912. struct inode *inode;
  2913. struct path path;
  2914. int error;
  2915. nd->flags &= ~LOOKUP_PARENT;
  2916. nd->flags |= op->intent;
  2917. if (nd->last_type != LAST_NORM) {
  2918. error = handle_dots(nd, nd->last_type);
  2919. if (unlikely(error))
  2920. return error;
  2921. goto finish_open;
  2922. }
  2923. if (!(open_flag & O_CREAT)) {
  2924. if (nd->last.name[nd->last.len])
  2925. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2926. /* we _can_ be in RCU mode here */
  2927. error = lookup_fast(nd, &path, &inode, &seq);
  2928. if (likely(error > 0))
  2929. goto finish_lookup;
  2930. if (error < 0)
  2931. return error;
  2932. BUG_ON(nd->inode != dir->d_inode);
  2933. BUG_ON(nd->flags & LOOKUP_RCU);
  2934. } else {
  2935. /* create side of things */
  2936. /*
  2937. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2938. * has been cleared when we got to the last component we are
  2939. * about to look up
  2940. */
  2941. error = complete_walk(nd);
  2942. if (error)
  2943. return error;
  2944. audit_inode(nd->name, dir, LOOKUP_PARENT);
  2945. /* trailing slashes? */
  2946. if (unlikely(nd->last.name[nd->last.len]))
  2947. return -EISDIR;
  2948. }
  2949. if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2950. error = mnt_want_write(nd->path.mnt);
  2951. if (!error)
  2952. got_write = true;
  2953. /*
  2954. * do _not_ fail yet - we might not need that or fail with
  2955. * a different error; let lookup_open() decide; we'll be
  2956. * dropping this one anyway.
  2957. */
  2958. }
  2959. if (open_flag & O_CREAT)
  2960. inode_lock(dir->d_inode);
  2961. else
  2962. inode_lock_shared(dir->d_inode);
  2963. error = lookup_open(nd, &path, file, op, got_write, opened);
  2964. if (open_flag & O_CREAT)
  2965. inode_unlock(dir->d_inode);
  2966. else
  2967. inode_unlock_shared(dir->d_inode);
  2968. if (error <= 0) {
  2969. if (error)
  2970. goto out;
  2971. if ((*opened & FILE_CREATED) ||
  2972. !S_ISREG(file_inode(file)->i_mode))
  2973. will_truncate = false;
  2974. audit_inode(nd->name, file->f_path.dentry, 0);
  2975. goto opened;
  2976. }
  2977. if (*opened & FILE_CREATED) {
  2978. /* Don't check for write permission, don't truncate */
  2979. open_flag &= ~O_TRUNC;
  2980. will_truncate = false;
  2981. acc_mode = 0;
  2982. path_to_nameidata(&path, nd);
  2983. goto finish_open_created;
  2984. }
  2985. /*
  2986. * If atomic_open() acquired write access it is dropped now due to
  2987. * possible mount and symlink following (this might be optimized away if
  2988. * necessary...)
  2989. */
  2990. if (got_write) {
  2991. mnt_drop_write(nd->path.mnt);
  2992. got_write = false;
  2993. }
  2994. error = follow_managed(&path, nd);
  2995. if (unlikely(error < 0))
  2996. return error;
  2997. if (unlikely(d_is_negative(path.dentry))) {
  2998. path_to_nameidata(&path, nd);
  2999. return -ENOENT;
  3000. }
  3001. /*
  3002. * create/update audit record if it already exists.
  3003. */
  3004. audit_inode(nd->name, path.dentry, 0);
  3005. if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
  3006. path_to_nameidata(&path, nd);
  3007. return -EEXIST;
  3008. }
  3009. seq = 0; /* out of RCU mode, so the value doesn't matter */
  3010. inode = d_backing_inode(path.dentry);
  3011. finish_lookup:
  3012. error = step_into(nd, &path, 0, inode, seq);
  3013. if (unlikely(error))
  3014. return error;
  3015. finish_open:
  3016. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  3017. error = complete_walk(nd);
  3018. if (error)
  3019. return error;
  3020. audit_inode(nd->name, nd->path.dentry, 0);
  3021. error = -EISDIR;
  3022. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  3023. goto out;
  3024. error = -ENOTDIR;
  3025. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  3026. goto out;
  3027. if (!d_is_reg(nd->path.dentry))
  3028. will_truncate = false;
  3029. if (will_truncate) {
  3030. error = mnt_want_write(nd->path.mnt);
  3031. if (error)
  3032. goto out;
  3033. got_write = true;
  3034. }
  3035. finish_open_created:
  3036. error = may_open(&nd->path, acc_mode, open_flag);
  3037. if (error)
  3038. goto out;
  3039. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  3040. error = vfs_open(&nd->path, file, current_cred());
  3041. if (error)
  3042. goto out;
  3043. *opened |= FILE_OPENED;
  3044. opened:
  3045. error = open_check_o_direct(file);
  3046. if (!error)
  3047. error = ima_file_check(file, op->acc_mode, *opened);
  3048. if (!error && will_truncate)
  3049. error = handle_truncate(file);
  3050. out:
  3051. if (unlikely(error) && (*opened & FILE_OPENED))
  3052. fput(file);
  3053. if (unlikely(error > 0)) {
  3054. WARN_ON(1);
  3055. error = -EINVAL;
  3056. }
  3057. if (got_write)
  3058. mnt_drop_write(nd->path.mnt);
  3059. return error;
  3060. }
  3061. struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
  3062. {
  3063. static const struct qstr name = QSTR_INIT("/", 1);
  3064. struct dentry *child = NULL;
  3065. struct inode *dir = dentry->d_inode;
  3066. struct inode *inode;
  3067. int error;
  3068. /* we want directory to be writable */
  3069. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  3070. if (error)
  3071. goto out_err;
  3072. error = -EOPNOTSUPP;
  3073. if (!dir->i_op->tmpfile)
  3074. goto out_err;
  3075. error = -ENOMEM;
  3076. child = d_alloc(dentry, &name);
  3077. if (unlikely(!child))
  3078. goto out_err;
  3079. error = dir->i_op->tmpfile(dir, child, mode);
  3080. if (error)
  3081. goto out_err;
  3082. error = -ENOENT;
  3083. inode = child->d_inode;
  3084. if (unlikely(!inode))
  3085. goto out_err;
  3086. if (!(open_flag & O_EXCL)) {
  3087. spin_lock(&inode->i_lock);
  3088. inode->i_state |= I_LINKABLE;
  3089. spin_unlock(&inode->i_lock);
  3090. }
  3091. return child;
  3092. out_err:
  3093. dput(child);
  3094. return ERR_PTR(error);
  3095. }
  3096. EXPORT_SYMBOL(vfs_tmpfile);
  3097. static int do_tmpfile(struct nameidata *nd, unsigned flags,
  3098. const struct open_flags *op,
  3099. struct file *file, int *opened)
  3100. {
  3101. struct dentry *child;
  3102. struct path path;
  3103. int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
  3104. if (unlikely(error))
  3105. return error;
  3106. error = mnt_want_write(path.mnt);
  3107. if (unlikely(error))
  3108. goto out;
  3109. child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
  3110. error = PTR_ERR(child);
  3111. if (unlikely(IS_ERR(child)))
  3112. goto out2;
  3113. dput(path.dentry);
  3114. path.dentry = child;
  3115. audit_inode(nd->name, child, 0);
  3116. /* Don't check for other permissions, the inode was just created */
  3117. error = may_open(&path, 0, op->open_flag);
  3118. if (error)
  3119. goto out2;
  3120. file->f_path.mnt = path.mnt;
  3121. error = finish_open(file, child, NULL, opened);
  3122. if (error)
  3123. goto out2;
  3124. error = open_check_o_direct(file);
  3125. if (error)
  3126. fput(file);
  3127. out2:
  3128. mnt_drop_write(path.mnt);
  3129. out:
  3130. path_put(&path);
  3131. return error;
  3132. }
  3133. static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
  3134. {
  3135. struct path path;
  3136. int error = path_lookupat(nd, flags, &path);
  3137. if (!error) {
  3138. audit_inode(nd->name, path.dentry, 0);
  3139. error = vfs_open(&path, file, current_cred());
  3140. path_put(&path);
  3141. }
  3142. return error;
  3143. }
  3144. static struct file *path_openat(struct nameidata *nd,
  3145. const struct open_flags *op, unsigned flags)
  3146. {
  3147. const char *s;
  3148. struct file *file;
  3149. int opened = 0;
  3150. int error;
  3151. file = get_empty_filp();
  3152. if (IS_ERR(file))
  3153. return file;
  3154. file->f_flags = op->open_flag;
  3155. if (unlikely(file->f_flags & __O_TMPFILE)) {
  3156. error = do_tmpfile(nd, flags, op, file, &opened);
  3157. goto out2;
  3158. }
  3159. if (unlikely(file->f_flags & O_PATH)) {
  3160. error = do_o_path(nd, flags, file);
  3161. if (!error)
  3162. opened |= FILE_OPENED;
  3163. goto out2;
  3164. }
  3165. s = path_init(nd, flags);
  3166. if (IS_ERR(s)) {
  3167. put_filp(file);
  3168. return ERR_CAST(s);
  3169. }
  3170. while (!(error = link_path_walk(s, nd)) &&
  3171. (error = do_last(nd, file, op, &opened)) > 0) {
  3172. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  3173. s = trailing_symlink(nd);
  3174. if (IS_ERR(s)) {
  3175. error = PTR_ERR(s);
  3176. break;
  3177. }
  3178. }
  3179. terminate_walk(nd);
  3180. out2:
  3181. if (!(opened & FILE_OPENED)) {
  3182. BUG_ON(!error);
  3183. put_filp(file);
  3184. }
  3185. if (unlikely(error)) {
  3186. if (error == -EOPENSTALE) {
  3187. if (flags & LOOKUP_RCU)
  3188. error = -ECHILD;
  3189. else
  3190. error = -ESTALE;
  3191. }
  3192. file = ERR_PTR(error);
  3193. }
  3194. return file;
  3195. }
  3196. struct file *do_filp_open(int dfd, struct filename *pathname,
  3197. const struct open_flags *op)
  3198. {
  3199. struct nameidata nd;
  3200. int flags = op->lookup_flags;
  3201. struct file *filp;
  3202. set_nameidata(&nd, dfd, pathname);
  3203. filp = path_openat(&nd, op, flags | LOOKUP_RCU);
  3204. if (unlikely(filp == ERR_PTR(-ECHILD)))
  3205. filp = path_openat(&nd, op, flags);
  3206. if (unlikely(filp == ERR_PTR(-ESTALE)))
  3207. filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3208. restore_nameidata();
  3209. return filp;
  3210. }
  3211. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  3212. const char *name, const struct open_flags *op)
  3213. {
  3214. struct nameidata nd;
  3215. struct file *file;
  3216. struct filename *filename;
  3217. int flags = op->lookup_flags | LOOKUP_ROOT;
  3218. nd.root.mnt = mnt;
  3219. nd.root.dentry = dentry;
  3220. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  3221. return ERR_PTR(-ELOOP);
  3222. filename = getname_kernel(name);
  3223. if (IS_ERR(filename))
  3224. return ERR_CAST(filename);
  3225. set_nameidata(&nd, -1, filename);
  3226. file = path_openat(&nd, op, flags | LOOKUP_RCU);
  3227. if (unlikely(file == ERR_PTR(-ECHILD)))
  3228. file = path_openat(&nd, op, flags);
  3229. if (unlikely(file == ERR_PTR(-ESTALE)))
  3230. file = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3231. restore_nameidata();
  3232. putname(filename);
  3233. return file;
  3234. }
  3235. static struct dentry *filename_create(int dfd, struct filename *name,
  3236. struct path *path, unsigned int lookup_flags)
  3237. {
  3238. struct dentry *dentry = ERR_PTR(-EEXIST);
  3239. struct qstr last;
  3240. int type;
  3241. int err2;
  3242. int error;
  3243. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  3244. /*
  3245. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  3246. * other flags passed in are ignored!
  3247. */
  3248. lookup_flags &= LOOKUP_REVAL;
  3249. name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
  3250. if (IS_ERR(name))
  3251. return ERR_CAST(name);
  3252. /*
  3253. * Yucky last component or no last component at all?
  3254. * (foo/., foo/.., /////)
  3255. */
  3256. if (unlikely(type != LAST_NORM))
  3257. goto out;
  3258. /* don't fail immediately if it's r/o, at least try to report other errors */
  3259. err2 = mnt_want_write(path->mnt);
  3260. /*
  3261. * Do the final lookup.
  3262. */
  3263. lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  3264. inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
  3265. dentry = __lookup_hash(&last, path->dentry, lookup_flags);
  3266. if (IS_ERR(dentry))
  3267. goto unlock;
  3268. error = -EEXIST;
  3269. if (d_is_positive(dentry))
  3270. goto fail;
  3271. /*
  3272. * Special case - lookup gave negative, but... we had foo/bar/
  3273. * From the vfs_mknod() POV we just have a negative dentry -
  3274. * all is fine. Let's be bastards - you had / on the end, you've
  3275. * been asking for (non-existent) directory. -ENOENT for you.
  3276. */
  3277. if (unlikely(!is_dir && last.name[last.len])) {
  3278. error = -ENOENT;
  3279. goto fail;
  3280. }
  3281. if (unlikely(err2)) {
  3282. error = err2;
  3283. goto fail;
  3284. }
  3285. putname(name);
  3286. return dentry;
  3287. fail:
  3288. dput(dentry);
  3289. dentry = ERR_PTR(error);
  3290. unlock:
  3291. inode_unlock(path->dentry->d_inode);
  3292. if (!err2)
  3293. mnt_drop_write(path->mnt);
  3294. out:
  3295. path_put(path);
  3296. putname(name);
  3297. return dentry;
  3298. }
  3299. struct dentry *kern_path_create(int dfd, const char *pathname,
  3300. struct path *path, unsigned int lookup_flags)
  3301. {
  3302. return filename_create(dfd, getname_kernel(pathname),
  3303. path, lookup_flags);
  3304. }
  3305. EXPORT_SYMBOL(kern_path_create);
  3306. void done_path_create(struct path *path, struct dentry *dentry)
  3307. {
  3308. dput(dentry);
  3309. inode_unlock(path->dentry->d_inode);
  3310. mnt_drop_write(path->mnt);
  3311. path_put(path);
  3312. }
  3313. EXPORT_SYMBOL(done_path_create);
  3314. inline struct dentry *user_path_create(int dfd, const char __user *pathname,
  3315. struct path *path, unsigned int lookup_flags)
  3316. {
  3317. return filename_create(dfd, getname(pathname), path, lookup_flags);
  3318. }
  3319. EXPORT_SYMBOL(user_path_create);
  3320. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3321. {
  3322. int error = may_create(dir, dentry);
  3323. if (error)
  3324. return error;
  3325. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3326. return -EPERM;
  3327. if (!dir->i_op->mknod)
  3328. return -EPERM;
  3329. error = devcgroup_inode_mknod(mode, dev);
  3330. if (error)
  3331. return error;
  3332. error = security_inode_mknod(dir, dentry, mode, dev);
  3333. if (error)
  3334. return error;
  3335. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3336. if (!error)
  3337. fsnotify_create(dir, dentry);
  3338. return error;
  3339. }
  3340. EXPORT_SYMBOL(vfs_mknod);
  3341. static int may_mknod(umode_t mode)
  3342. {
  3343. switch (mode & S_IFMT) {
  3344. case S_IFREG:
  3345. case S_IFCHR:
  3346. case S_IFBLK:
  3347. case S_IFIFO:
  3348. case S_IFSOCK:
  3349. case 0: /* zero mode translates to S_IFREG */
  3350. return 0;
  3351. case S_IFDIR:
  3352. return -EPERM;
  3353. default:
  3354. return -EINVAL;
  3355. }
  3356. }
  3357. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3358. unsigned, dev)
  3359. {
  3360. struct dentry *dentry;
  3361. struct path path;
  3362. int error;
  3363. unsigned int lookup_flags = 0;
  3364. error = may_mknod(mode);
  3365. if (error)
  3366. return error;
  3367. retry:
  3368. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3369. if (IS_ERR(dentry))
  3370. return PTR_ERR(dentry);
  3371. if (!IS_POSIXACL(path.dentry->d_inode))
  3372. mode &= ~current_umask();
  3373. error = security_path_mknod(&path, dentry, mode, dev);
  3374. if (error)
  3375. goto out;
  3376. switch (mode & S_IFMT) {
  3377. case 0: case S_IFREG:
  3378. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3379. if (!error)
  3380. ima_post_path_mknod(dentry);
  3381. break;
  3382. case S_IFCHR: case S_IFBLK:
  3383. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3384. new_decode_dev(dev));
  3385. break;
  3386. case S_IFIFO: case S_IFSOCK:
  3387. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3388. break;
  3389. }
  3390. out:
  3391. done_path_create(&path, dentry);
  3392. if (retry_estale(error, lookup_flags)) {
  3393. lookup_flags |= LOOKUP_REVAL;
  3394. goto retry;
  3395. }
  3396. return error;
  3397. }
  3398. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3399. {
  3400. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3401. }
  3402. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3403. {
  3404. int error = may_create(dir, dentry);
  3405. unsigned max_links = dir->i_sb->s_max_links;
  3406. if (error)
  3407. return error;
  3408. if (!dir->i_op->mkdir)
  3409. return -EPERM;
  3410. mode &= (S_IRWXUGO|S_ISVTX);
  3411. error = security_inode_mkdir(dir, dentry, mode);
  3412. if (error)
  3413. return error;
  3414. if (max_links && dir->i_nlink >= max_links)
  3415. return -EMLINK;
  3416. error = dir->i_op->mkdir(dir, dentry, mode);
  3417. if (!error)
  3418. fsnotify_mkdir(dir, dentry);
  3419. return error;
  3420. }
  3421. EXPORT_SYMBOL(vfs_mkdir);
  3422. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3423. {
  3424. struct dentry *dentry;
  3425. struct path path;
  3426. int error;
  3427. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3428. retry:
  3429. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3430. if (IS_ERR(dentry))
  3431. return PTR_ERR(dentry);
  3432. if (!IS_POSIXACL(path.dentry->d_inode))
  3433. mode &= ~current_umask();
  3434. error = security_path_mkdir(&path, dentry, mode);
  3435. if (!error)
  3436. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3437. done_path_create(&path, dentry);
  3438. if (retry_estale(error, lookup_flags)) {
  3439. lookup_flags |= LOOKUP_REVAL;
  3440. goto retry;
  3441. }
  3442. return error;
  3443. }
  3444. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3445. {
  3446. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3447. }
  3448. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3449. {
  3450. int error = may_delete(dir, dentry, 1);
  3451. if (error)
  3452. return error;
  3453. if (!dir->i_op->rmdir)
  3454. return -EPERM;
  3455. dget(dentry);
  3456. inode_lock(dentry->d_inode);
  3457. error = -EBUSY;
  3458. if (is_local_mountpoint(dentry))
  3459. goto out;
  3460. error = security_inode_rmdir(dir, dentry);
  3461. if (error)
  3462. goto out;
  3463. shrink_dcache_parent(dentry);
  3464. error = dir->i_op->rmdir(dir, dentry);
  3465. if (error)
  3466. goto out;
  3467. dentry->d_inode->i_flags |= S_DEAD;
  3468. dont_mount(dentry);
  3469. detach_mounts(dentry);
  3470. out:
  3471. inode_unlock(dentry->d_inode);
  3472. dput(dentry);
  3473. if (!error)
  3474. d_delete(dentry);
  3475. return error;
  3476. }
  3477. EXPORT_SYMBOL(vfs_rmdir);
  3478. static long do_rmdir(int dfd, const char __user *pathname)
  3479. {
  3480. int error = 0;
  3481. struct filename *name;
  3482. struct dentry *dentry;
  3483. struct path path;
  3484. struct qstr last;
  3485. int type;
  3486. unsigned int lookup_flags = 0;
  3487. retry:
  3488. name = filename_parentat(dfd, getname(pathname), lookup_flags,
  3489. &path, &last, &type);
  3490. if (IS_ERR(name))
  3491. return PTR_ERR(name);
  3492. switch (type) {
  3493. case LAST_DOTDOT:
  3494. error = -ENOTEMPTY;
  3495. goto exit1;
  3496. case LAST_DOT:
  3497. error = -EINVAL;
  3498. goto exit1;
  3499. case LAST_ROOT:
  3500. error = -EBUSY;
  3501. goto exit1;
  3502. }
  3503. error = mnt_want_write(path.mnt);
  3504. if (error)
  3505. goto exit1;
  3506. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3507. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3508. error = PTR_ERR(dentry);
  3509. if (IS_ERR(dentry))
  3510. goto exit2;
  3511. if (!dentry->d_inode) {
  3512. error = -ENOENT;
  3513. goto exit3;
  3514. }
  3515. error = security_path_rmdir(&path, dentry);
  3516. if (error)
  3517. goto exit3;
  3518. error = vfs_rmdir(path.dentry->d_inode, dentry);
  3519. exit3:
  3520. dput(dentry);
  3521. exit2:
  3522. inode_unlock(path.dentry->d_inode);
  3523. mnt_drop_write(path.mnt);
  3524. exit1:
  3525. path_put(&path);
  3526. putname(name);
  3527. if (retry_estale(error, lookup_flags)) {
  3528. lookup_flags |= LOOKUP_REVAL;
  3529. goto retry;
  3530. }
  3531. return error;
  3532. }
  3533. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3534. {
  3535. return do_rmdir(AT_FDCWD, pathname);
  3536. }
  3537. /**
  3538. * vfs_unlink - unlink a filesystem object
  3539. * @dir: parent directory
  3540. * @dentry: victim
  3541. * @delegated_inode: returns victim inode, if the inode is delegated.
  3542. *
  3543. * The caller must hold dir->i_mutex.
  3544. *
  3545. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3546. * return a reference to the inode in delegated_inode. The caller
  3547. * should then break the delegation on that inode and retry. Because
  3548. * breaking a delegation may take a long time, the caller should drop
  3549. * dir->i_mutex before doing so.
  3550. *
  3551. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3552. * be appropriate for callers that expect the underlying filesystem not
  3553. * to be NFS exported.
  3554. */
  3555. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3556. {
  3557. struct inode *target = dentry->d_inode;
  3558. int error = may_delete(dir, dentry, 0);
  3559. if (error)
  3560. return error;
  3561. if (!dir->i_op->unlink)
  3562. return -EPERM;
  3563. inode_lock(target);
  3564. if (is_local_mountpoint(dentry))
  3565. error = -EBUSY;
  3566. else {
  3567. error = security_inode_unlink(dir, dentry);
  3568. if (!error) {
  3569. error = try_break_deleg(target, delegated_inode);
  3570. if (error)
  3571. goto out;
  3572. error = dir->i_op->unlink(dir, dentry);
  3573. if (!error) {
  3574. dont_mount(dentry);
  3575. detach_mounts(dentry);
  3576. }
  3577. }
  3578. }
  3579. out:
  3580. inode_unlock(target);
  3581. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3582. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3583. fsnotify_link_count(target);
  3584. d_delete(dentry);
  3585. }
  3586. return error;
  3587. }
  3588. EXPORT_SYMBOL(vfs_unlink);
  3589. /*
  3590. * Make sure that the actual truncation of the file will occur outside its
  3591. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3592. * writeout happening, and we don't want to prevent access to the directory
  3593. * while waiting on the I/O.
  3594. */
  3595. static long do_unlinkat(int dfd, const char __user *pathname)
  3596. {
  3597. int error;
  3598. struct filename *name;
  3599. struct dentry *dentry;
  3600. struct path path;
  3601. struct qstr last;
  3602. int type;
  3603. struct inode *inode = NULL;
  3604. struct inode *delegated_inode = NULL;
  3605. unsigned int lookup_flags = 0;
  3606. retry:
  3607. name = filename_parentat(dfd, getname(pathname), lookup_flags,
  3608. &path, &last, &type);
  3609. if (IS_ERR(name))
  3610. return PTR_ERR(name);
  3611. error = -EISDIR;
  3612. if (type != LAST_NORM)
  3613. goto exit1;
  3614. error = mnt_want_write(path.mnt);
  3615. if (error)
  3616. goto exit1;
  3617. retry_deleg:
  3618. inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
  3619. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3620. error = PTR_ERR(dentry);
  3621. if (!IS_ERR(dentry)) {
  3622. /* Why not before? Because we want correct error value */
  3623. if (last.name[last.len])
  3624. goto slashes;
  3625. inode = dentry->d_inode;
  3626. if (d_is_negative(dentry))
  3627. goto slashes;
  3628. ihold(inode);
  3629. error = security_path_unlink(&path, dentry);
  3630. if (error)
  3631. goto exit2;
  3632. error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
  3633. exit2:
  3634. dput(dentry);
  3635. }
  3636. inode_unlock(path.dentry->d_inode);
  3637. if (inode)
  3638. iput(inode); /* truncate the inode here */
  3639. inode = NULL;
  3640. if (delegated_inode) {
  3641. error = break_deleg_wait(&delegated_inode);
  3642. if (!error)
  3643. goto retry_deleg;
  3644. }
  3645. mnt_drop_write(path.mnt);
  3646. exit1:
  3647. path_put(&path);
  3648. putname(name);
  3649. if (retry_estale(error, lookup_flags)) {
  3650. lookup_flags |= LOOKUP_REVAL;
  3651. inode = NULL;
  3652. goto retry;
  3653. }
  3654. return error;
  3655. slashes:
  3656. if (d_is_negative(dentry))
  3657. error = -ENOENT;
  3658. else if (d_is_dir(dentry))
  3659. error = -EISDIR;
  3660. else
  3661. error = -ENOTDIR;
  3662. goto exit2;
  3663. }
  3664. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3665. {
  3666. if ((flag & ~AT_REMOVEDIR) != 0)
  3667. return -EINVAL;
  3668. if (flag & AT_REMOVEDIR)
  3669. return do_rmdir(dfd, pathname);
  3670. return do_unlinkat(dfd, pathname);
  3671. }
  3672. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3673. {
  3674. return do_unlinkat(AT_FDCWD, pathname);
  3675. }
  3676. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3677. {
  3678. int error = may_create(dir, dentry);
  3679. if (error)
  3680. return error;
  3681. if (!dir->i_op->symlink)
  3682. return -EPERM;
  3683. error = security_inode_symlink(dir, dentry, oldname);
  3684. if (error)
  3685. return error;
  3686. error = dir->i_op->symlink(dir, dentry, oldname);
  3687. if (!error)
  3688. fsnotify_create(dir, dentry);
  3689. return error;
  3690. }
  3691. EXPORT_SYMBOL(vfs_symlink);
  3692. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3693. int, newdfd, const char __user *, newname)
  3694. {
  3695. int error;
  3696. struct filename *from;
  3697. struct dentry *dentry;
  3698. struct path path;
  3699. unsigned int lookup_flags = 0;
  3700. from = getname(oldname);
  3701. if (IS_ERR(from))
  3702. return PTR_ERR(from);
  3703. retry:
  3704. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3705. error = PTR_ERR(dentry);
  3706. if (IS_ERR(dentry))
  3707. goto out_putname;
  3708. error = security_path_symlink(&path, dentry, from->name);
  3709. if (!error)
  3710. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3711. done_path_create(&path, dentry);
  3712. if (retry_estale(error, lookup_flags)) {
  3713. lookup_flags |= LOOKUP_REVAL;
  3714. goto retry;
  3715. }
  3716. out_putname:
  3717. putname(from);
  3718. return error;
  3719. }
  3720. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3721. {
  3722. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3723. }
  3724. /**
  3725. * vfs_link - create a new link
  3726. * @old_dentry: object to be linked
  3727. * @dir: new parent
  3728. * @new_dentry: where to create the new link
  3729. * @delegated_inode: returns inode needing a delegation break
  3730. *
  3731. * The caller must hold dir->i_mutex
  3732. *
  3733. * If vfs_link discovers a delegation on the to-be-linked file in need
  3734. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3735. * inode in delegated_inode. The caller should then break the delegation
  3736. * and retry. Because breaking a delegation may take a long time, the
  3737. * caller should drop the i_mutex before doing so.
  3738. *
  3739. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3740. * be appropriate for callers that expect the underlying filesystem not
  3741. * to be NFS exported.
  3742. */
  3743. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3744. {
  3745. struct inode *inode = old_dentry->d_inode;
  3746. unsigned max_links = dir->i_sb->s_max_links;
  3747. int error;
  3748. if (!inode)
  3749. return -ENOENT;
  3750. error = may_create(dir, new_dentry);
  3751. if (error)
  3752. return error;
  3753. if (dir->i_sb != inode->i_sb)
  3754. return -EXDEV;
  3755. /*
  3756. * A link to an append-only or immutable file cannot be created.
  3757. */
  3758. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3759. return -EPERM;
  3760. /*
  3761. * Updating the link count will likely cause i_uid and i_gid to
  3762. * be writen back improperly if their true value is unknown to
  3763. * the vfs.
  3764. */
  3765. if (HAS_UNMAPPED_ID(inode))
  3766. return -EPERM;
  3767. if (!dir->i_op->link)
  3768. return -EPERM;
  3769. if (S_ISDIR(inode->i_mode))
  3770. return -EPERM;
  3771. error = security_inode_link(old_dentry, dir, new_dentry);
  3772. if (error)
  3773. return error;
  3774. inode_lock(inode);
  3775. /* Make sure we don't allow creating hardlink to an unlinked file */
  3776. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3777. error = -ENOENT;
  3778. else if (max_links && inode->i_nlink >= max_links)
  3779. error = -EMLINK;
  3780. else {
  3781. error = try_break_deleg(inode, delegated_inode);
  3782. if (!error)
  3783. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3784. }
  3785. if (!error && (inode->i_state & I_LINKABLE)) {
  3786. spin_lock(&inode->i_lock);
  3787. inode->i_state &= ~I_LINKABLE;
  3788. spin_unlock(&inode->i_lock);
  3789. }
  3790. inode_unlock(inode);
  3791. if (!error)
  3792. fsnotify_link(dir, inode, new_dentry);
  3793. return error;
  3794. }
  3795. EXPORT_SYMBOL(vfs_link);
  3796. /*
  3797. * Hardlinks are often used in delicate situations. We avoid
  3798. * security-related surprises by not following symlinks on the
  3799. * newname. --KAB
  3800. *
  3801. * We don't follow them on the oldname either to be compatible
  3802. * with linux 2.0, and to avoid hard-linking to directories
  3803. * and other special files. --ADM
  3804. */
  3805. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3806. int, newdfd, const char __user *, newname, int, flags)
  3807. {
  3808. struct dentry *new_dentry;
  3809. struct path old_path, new_path;
  3810. struct inode *delegated_inode = NULL;
  3811. int how = 0;
  3812. int error;
  3813. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3814. return -EINVAL;
  3815. /*
  3816. * To use null names we require CAP_DAC_READ_SEARCH
  3817. * This ensures that not everyone will be able to create
  3818. * handlink using the passed filedescriptor.
  3819. */
  3820. if (flags & AT_EMPTY_PATH) {
  3821. if (!capable(CAP_DAC_READ_SEARCH))
  3822. return -ENOENT;
  3823. how = LOOKUP_EMPTY;
  3824. }
  3825. if (flags & AT_SYMLINK_FOLLOW)
  3826. how |= LOOKUP_FOLLOW;
  3827. retry:
  3828. error = user_path_at(olddfd, oldname, how, &old_path);
  3829. if (error)
  3830. return error;
  3831. new_dentry = user_path_create(newdfd, newname, &new_path,
  3832. (how & LOOKUP_REVAL));
  3833. error = PTR_ERR(new_dentry);
  3834. if (IS_ERR(new_dentry))
  3835. goto out;
  3836. error = -EXDEV;
  3837. if (old_path.mnt != new_path.mnt)
  3838. goto out_dput;
  3839. error = may_linkat(&old_path);
  3840. if (unlikely(error))
  3841. goto out_dput;
  3842. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3843. if (error)
  3844. goto out_dput;
  3845. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3846. out_dput:
  3847. done_path_create(&new_path, new_dentry);
  3848. if (delegated_inode) {
  3849. error = break_deleg_wait(&delegated_inode);
  3850. if (!error) {
  3851. path_put(&old_path);
  3852. goto retry;
  3853. }
  3854. }
  3855. if (retry_estale(error, how)) {
  3856. path_put(&old_path);
  3857. how |= LOOKUP_REVAL;
  3858. goto retry;
  3859. }
  3860. out:
  3861. path_put(&old_path);
  3862. return error;
  3863. }
  3864. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3865. {
  3866. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3867. }
  3868. /**
  3869. * vfs_rename - rename a filesystem object
  3870. * @old_dir: parent of source
  3871. * @old_dentry: source
  3872. * @new_dir: parent of destination
  3873. * @new_dentry: destination
  3874. * @delegated_inode: returns an inode needing a delegation break
  3875. * @flags: rename flags
  3876. *
  3877. * The caller must hold multiple mutexes--see lock_rename()).
  3878. *
  3879. * If vfs_rename discovers a delegation in need of breaking at either
  3880. * the source or destination, it will return -EWOULDBLOCK and return a
  3881. * reference to the inode in delegated_inode. The caller should then
  3882. * break the delegation and retry. Because breaking a delegation may
  3883. * take a long time, the caller should drop all locks before doing
  3884. * so.
  3885. *
  3886. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3887. * be appropriate for callers that expect the underlying filesystem not
  3888. * to be NFS exported.
  3889. *
  3890. * The worst of all namespace operations - renaming directory. "Perverted"
  3891. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3892. * Problems:
  3893. * a) we can get into loop creation.
  3894. * b) race potential - two innocent renames can create a loop together.
  3895. * That's where 4.4 screws up. Current fix: serialization on
  3896. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3897. * story.
  3898. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3899. * and source (if it is not a directory).
  3900. * And that - after we got ->i_mutex on parents (until then we don't know
  3901. * whether the target exists). Solution: try to be smart with locking
  3902. * order for inodes. We rely on the fact that tree topology may change
  3903. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3904. * move will be locked. Thus we can rank directories by the tree
  3905. * (ancestors first) and rank all non-directories after them.
  3906. * That works since everybody except rename does "lock parent, lookup,
  3907. * lock child" and rename is under ->s_vfs_rename_mutex.
  3908. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3909. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3910. * we'd better make sure that there's no link(2) for them.
  3911. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3912. * we are removing the target. Solution: we will have to grab ->i_mutex
  3913. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3914. * ->i_mutex on parents, which works but leads to some truly excessive
  3915. * locking].
  3916. */
  3917. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3918. struct inode *new_dir, struct dentry *new_dentry,
  3919. struct inode **delegated_inode, unsigned int flags)
  3920. {
  3921. int error;
  3922. bool is_dir = d_is_dir(old_dentry);
  3923. const unsigned char *old_name;
  3924. struct inode *source = old_dentry->d_inode;
  3925. struct inode *target = new_dentry->d_inode;
  3926. bool new_is_dir = false;
  3927. unsigned max_links = new_dir->i_sb->s_max_links;
  3928. if (source == target)
  3929. return 0;
  3930. error = may_delete(old_dir, old_dentry, is_dir);
  3931. if (error)
  3932. return error;
  3933. if (!target) {
  3934. error = may_create(new_dir, new_dentry);
  3935. } else {
  3936. new_is_dir = d_is_dir(new_dentry);
  3937. if (!(flags & RENAME_EXCHANGE))
  3938. error = may_delete(new_dir, new_dentry, is_dir);
  3939. else
  3940. error = may_delete(new_dir, new_dentry, new_is_dir);
  3941. }
  3942. if (error)
  3943. return error;
  3944. if (!old_dir->i_op->rename)
  3945. return -EPERM;
  3946. /*
  3947. * If we are going to change the parent - check write permissions,
  3948. * we'll need to flip '..'.
  3949. */
  3950. if (new_dir != old_dir) {
  3951. if (is_dir) {
  3952. error = inode_permission(source, MAY_WRITE);
  3953. if (error)
  3954. return error;
  3955. }
  3956. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3957. error = inode_permission(target, MAY_WRITE);
  3958. if (error)
  3959. return error;
  3960. }
  3961. }
  3962. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3963. flags);
  3964. if (error)
  3965. return error;
  3966. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3967. dget(new_dentry);
  3968. if (!is_dir || (flags & RENAME_EXCHANGE))
  3969. lock_two_nondirectories(source, target);
  3970. else if (target)
  3971. inode_lock(target);
  3972. error = -EBUSY;
  3973. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3974. goto out;
  3975. if (max_links && new_dir != old_dir) {
  3976. error = -EMLINK;
  3977. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3978. goto out;
  3979. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3980. old_dir->i_nlink >= max_links)
  3981. goto out;
  3982. }
  3983. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3984. shrink_dcache_parent(new_dentry);
  3985. if (!is_dir) {
  3986. error = try_break_deleg(source, delegated_inode);
  3987. if (error)
  3988. goto out;
  3989. }
  3990. if (target && !new_is_dir) {
  3991. error = try_break_deleg(target, delegated_inode);
  3992. if (error)
  3993. goto out;
  3994. }
  3995. error = old_dir->i_op->rename(old_dir, old_dentry,
  3996. new_dir, new_dentry, flags);
  3997. if (error)
  3998. goto out;
  3999. if (!(flags & RENAME_EXCHANGE) && target) {
  4000. if (is_dir)
  4001. target->i_flags |= S_DEAD;
  4002. dont_mount(new_dentry);
  4003. detach_mounts(new_dentry);
  4004. }
  4005. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  4006. if (!(flags & RENAME_EXCHANGE))
  4007. d_move(old_dentry, new_dentry);
  4008. else
  4009. d_exchange(old_dentry, new_dentry);
  4010. }
  4011. out:
  4012. if (!is_dir || (flags & RENAME_EXCHANGE))
  4013. unlock_two_nondirectories(source, target);
  4014. else if (target)
  4015. inode_unlock(target);
  4016. dput(new_dentry);
  4017. if (!error) {
  4018. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  4019. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  4020. if (flags & RENAME_EXCHANGE) {
  4021. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  4022. new_is_dir, NULL, new_dentry);
  4023. }
  4024. }
  4025. fsnotify_oldname_free(old_name);
  4026. return error;
  4027. }
  4028. EXPORT_SYMBOL(vfs_rename);
  4029. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  4030. int, newdfd, const char __user *, newname, unsigned int, flags)
  4031. {
  4032. struct dentry *old_dentry, *new_dentry;
  4033. struct dentry *trap;
  4034. struct path old_path, new_path;
  4035. struct qstr old_last, new_last;
  4036. int old_type, new_type;
  4037. struct inode *delegated_inode = NULL;
  4038. struct filename *from;
  4039. struct filename *to;
  4040. unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
  4041. bool should_retry = false;
  4042. int error;
  4043. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  4044. return -EINVAL;
  4045. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  4046. (flags & RENAME_EXCHANGE))
  4047. return -EINVAL;
  4048. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  4049. return -EPERM;
  4050. if (flags & RENAME_EXCHANGE)
  4051. target_flags = 0;
  4052. retry:
  4053. from = filename_parentat(olddfd, getname(oldname), lookup_flags,
  4054. &old_path, &old_last, &old_type);
  4055. if (IS_ERR(from)) {
  4056. error = PTR_ERR(from);
  4057. goto exit;
  4058. }
  4059. to = filename_parentat(newdfd, getname(newname), lookup_flags,
  4060. &new_path, &new_last, &new_type);
  4061. if (IS_ERR(to)) {
  4062. error = PTR_ERR(to);
  4063. goto exit1;
  4064. }
  4065. error = -EXDEV;
  4066. if (old_path.mnt != new_path.mnt)
  4067. goto exit2;
  4068. error = -EBUSY;
  4069. if (old_type != LAST_NORM)
  4070. goto exit2;
  4071. if (flags & RENAME_NOREPLACE)
  4072. error = -EEXIST;
  4073. if (new_type != LAST_NORM)
  4074. goto exit2;
  4075. error = mnt_want_write(old_path.mnt);
  4076. if (error)
  4077. goto exit2;
  4078. retry_deleg:
  4079. trap = lock_rename(new_path.dentry, old_path.dentry);
  4080. old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
  4081. error = PTR_ERR(old_dentry);
  4082. if (IS_ERR(old_dentry))
  4083. goto exit3;
  4084. /* source must exist */
  4085. error = -ENOENT;
  4086. if (d_is_negative(old_dentry))
  4087. goto exit4;
  4088. new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
  4089. error = PTR_ERR(new_dentry);
  4090. if (IS_ERR(new_dentry))
  4091. goto exit4;
  4092. error = -EEXIST;
  4093. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  4094. goto exit5;
  4095. if (flags & RENAME_EXCHANGE) {
  4096. error = -ENOENT;
  4097. if (d_is_negative(new_dentry))
  4098. goto exit5;
  4099. if (!d_is_dir(new_dentry)) {
  4100. error = -ENOTDIR;
  4101. if (new_last.name[new_last.len])
  4102. goto exit5;
  4103. }
  4104. }
  4105. /* unless the source is a directory trailing slashes give -ENOTDIR */
  4106. if (!d_is_dir(old_dentry)) {
  4107. error = -ENOTDIR;
  4108. if (old_last.name[old_last.len])
  4109. goto exit5;
  4110. if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
  4111. goto exit5;
  4112. }
  4113. /* source should not be ancestor of target */
  4114. error = -EINVAL;
  4115. if (old_dentry == trap)
  4116. goto exit5;
  4117. /* target should not be an ancestor of source */
  4118. if (!(flags & RENAME_EXCHANGE))
  4119. error = -ENOTEMPTY;
  4120. if (new_dentry == trap)
  4121. goto exit5;
  4122. error = security_path_rename(&old_path, old_dentry,
  4123. &new_path, new_dentry, flags);
  4124. if (error)
  4125. goto exit5;
  4126. error = vfs_rename(old_path.dentry->d_inode, old_dentry,
  4127. new_path.dentry->d_inode, new_dentry,
  4128. &delegated_inode, flags);
  4129. exit5:
  4130. dput(new_dentry);
  4131. exit4:
  4132. dput(old_dentry);
  4133. exit3:
  4134. unlock_rename(new_path.dentry, old_path.dentry);
  4135. if (delegated_inode) {
  4136. error = break_deleg_wait(&delegated_inode);
  4137. if (!error)
  4138. goto retry_deleg;
  4139. }
  4140. mnt_drop_write(old_path.mnt);
  4141. exit2:
  4142. if (retry_estale(error, lookup_flags))
  4143. should_retry = true;
  4144. path_put(&new_path);
  4145. putname(to);
  4146. exit1:
  4147. path_put(&old_path);
  4148. putname(from);
  4149. if (should_retry) {
  4150. should_retry = false;
  4151. lookup_flags |= LOOKUP_REVAL;
  4152. goto retry;
  4153. }
  4154. exit:
  4155. return error;
  4156. }
  4157. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  4158. int, newdfd, const char __user *, newname)
  4159. {
  4160. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  4161. }
  4162. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  4163. {
  4164. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  4165. }
  4166. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  4167. {
  4168. int error = may_create(dir, dentry);
  4169. if (error)
  4170. return error;
  4171. if (!dir->i_op->mknod)
  4172. return -EPERM;
  4173. return dir->i_op->mknod(dir, dentry,
  4174. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  4175. }
  4176. EXPORT_SYMBOL(vfs_whiteout);
  4177. int readlink_copy(char __user *buffer, int buflen, const char *link)
  4178. {
  4179. int len = PTR_ERR(link);
  4180. if (IS_ERR(link))
  4181. goto out;
  4182. len = strlen(link);
  4183. if (len > (unsigned) buflen)
  4184. len = buflen;
  4185. if (copy_to_user(buffer, link, len))
  4186. len = -EFAULT;
  4187. out:
  4188. return len;
  4189. }
  4190. /*
  4191. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  4192. * have ->get_link() not calling nd_jump_link(). Using (or not using) it
  4193. * for any given inode is up to filesystem.
  4194. */
  4195. static int generic_readlink(struct dentry *dentry, char __user *buffer,
  4196. int buflen)
  4197. {
  4198. DEFINE_DELAYED_CALL(done);
  4199. struct inode *inode = d_inode(dentry);
  4200. const char *link = inode->i_link;
  4201. int res;
  4202. if (!link) {
  4203. link = inode->i_op->get_link(dentry, inode, &done);
  4204. if (IS_ERR(link))
  4205. return PTR_ERR(link);
  4206. }
  4207. res = readlink_copy(buffer, buflen, link);
  4208. do_delayed_call(&done);
  4209. return res;
  4210. }
  4211. /**
  4212. * vfs_readlink - copy symlink body into userspace buffer
  4213. * @dentry: dentry on which to get symbolic link
  4214. * @buffer: user memory pointer
  4215. * @buflen: size of buffer
  4216. *
  4217. * Does not touch atime. That's up to the caller if necessary
  4218. *
  4219. * Does not call security hook.
  4220. */
  4221. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4222. {
  4223. struct inode *inode = d_inode(dentry);
  4224. if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
  4225. if (unlikely(inode->i_op->readlink))
  4226. return inode->i_op->readlink(dentry, buffer, buflen);
  4227. if (!d_is_symlink(dentry))
  4228. return -EINVAL;
  4229. spin_lock(&inode->i_lock);
  4230. inode->i_opflags |= IOP_DEFAULT_READLINK;
  4231. spin_unlock(&inode->i_lock);
  4232. }
  4233. return generic_readlink(dentry, buffer, buflen);
  4234. }
  4235. EXPORT_SYMBOL(vfs_readlink);
  4236. /**
  4237. * vfs_get_link - get symlink body
  4238. * @dentry: dentry on which to get symbolic link
  4239. * @done: caller needs to free returned data with this
  4240. *
  4241. * Calls security hook and i_op->get_link() on the supplied inode.
  4242. *
  4243. * It does not touch atime. That's up to the caller if necessary.
  4244. *
  4245. * Does not work on "special" symlinks like /proc/$$/fd/N
  4246. */
  4247. const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
  4248. {
  4249. const char *res = ERR_PTR(-EINVAL);
  4250. struct inode *inode = d_inode(dentry);
  4251. if (d_is_symlink(dentry)) {
  4252. res = ERR_PTR(security_inode_readlink(dentry));
  4253. if (!res)
  4254. res = inode->i_op->get_link(dentry, inode, done);
  4255. }
  4256. return res;
  4257. }
  4258. EXPORT_SYMBOL(vfs_get_link);
  4259. /* get the link contents into pagecache */
  4260. const char *page_get_link(struct dentry *dentry, struct inode *inode,
  4261. struct delayed_call *callback)
  4262. {
  4263. char *kaddr;
  4264. struct page *page;
  4265. struct address_space *mapping = inode->i_mapping;
  4266. if (!dentry) {
  4267. page = find_get_page(mapping, 0);
  4268. if (!page)
  4269. return ERR_PTR(-ECHILD);
  4270. if (!PageUptodate(page)) {
  4271. put_page(page);
  4272. return ERR_PTR(-ECHILD);
  4273. }
  4274. } else {
  4275. page = read_mapping_page(mapping, 0, NULL);
  4276. if (IS_ERR(page))
  4277. return (char*)page;
  4278. }
  4279. set_delayed_call(callback, page_put_link, page);
  4280. BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
  4281. kaddr = page_address(page);
  4282. nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
  4283. return kaddr;
  4284. }
  4285. EXPORT_SYMBOL(page_get_link);
  4286. void page_put_link(void *arg)
  4287. {
  4288. put_page(arg);
  4289. }
  4290. EXPORT_SYMBOL(page_put_link);
  4291. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4292. {
  4293. DEFINE_DELAYED_CALL(done);
  4294. int res = readlink_copy(buffer, buflen,
  4295. page_get_link(dentry, d_inode(dentry),
  4296. &done));
  4297. do_delayed_call(&done);
  4298. return res;
  4299. }
  4300. EXPORT_SYMBOL(page_readlink);
  4301. /*
  4302. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  4303. */
  4304. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  4305. {
  4306. struct address_space *mapping = inode->i_mapping;
  4307. struct page *page;
  4308. void *fsdata;
  4309. int err;
  4310. unsigned int flags = 0;
  4311. if (nofs)
  4312. flags |= AOP_FLAG_NOFS;
  4313. retry:
  4314. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4315. flags, &page, &fsdata);
  4316. if (err)
  4317. goto fail;
  4318. memcpy(page_address(page), symname, len-1);
  4319. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4320. page, fsdata);
  4321. if (err < 0)
  4322. goto fail;
  4323. if (err < len-1)
  4324. goto retry;
  4325. mark_inode_dirty(inode);
  4326. return 0;
  4327. fail:
  4328. return err;
  4329. }
  4330. EXPORT_SYMBOL(__page_symlink);
  4331. int page_symlink(struct inode *inode, const char *symname, int len)
  4332. {
  4333. return __page_symlink(inode, symname, len,
  4334. !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
  4335. }
  4336. EXPORT_SYMBOL(page_symlink);
  4337. const struct inode_operations page_symlink_inode_operations = {
  4338. .get_link = page_get_link,
  4339. };
  4340. EXPORT_SYMBOL(page_symlink_inode_operations);