namespace.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/namei.h>
  16. #include <linux/security.h>
  17. #include <linux/cred.h>
  18. #include <linux/idr.h>
  19. #include <linux/init.h> /* init_rootfs */
  20. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  21. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  22. #include <linux/uaccess.h>
  23. #include <linux/proc_ns.h>
  24. #include <linux/magic.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/task_work.h>
  27. #include <linux/sched/task.h>
  28. #include "pnode.h"
  29. #include "internal.h"
  30. /* Maximum number of mounts in a mount namespace */
  31. unsigned int sysctl_mount_max __read_mostly = 100000;
  32. static unsigned int m_hash_mask __read_mostly;
  33. static unsigned int m_hash_shift __read_mostly;
  34. static unsigned int mp_hash_mask __read_mostly;
  35. static unsigned int mp_hash_shift __read_mostly;
  36. static __initdata unsigned long mhash_entries;
  37. static int __init set_mhash_entries(char *str)
  38. {
  39. if (!str)
  40. return 0;
  41. mhash_entries = simple_strtoul(str, &str, 0);
  42. return 1;
  43. }
  44. __setup("mhash_entries=", set_mhash_entries);
  45. static __initdata unsigned long mphash_entries;
  46. static int __init set_mphash_entries(char *str)
  47. {
  48. if (!str)
  49. return 0;
  50. mphash_entries = simple_strtoul(str, &str, 0);
  51. return 1;
  52. }
  53. __setup("mphash_entries=", set_mphash_entries);
  54. static u64 event;
  55. static DEFINE_IDA(mnt_id_ida);
  56. static DEFINE_IDA(mnt_group_ida);
  57. static DEFINE_SPINLOCK(mnt_id_lock);
  58. static int mnt_id_start = 0;
  59. static int mnt_group_start = 1;
  60. static struct hlist_head *mount_hashtable __read_mostly;
  61. static struct hlist_head *mountpoint_hashtable __read_mostly;
  62. static struct kmem_cache *mnt_cache __read_mostly;
  63. static DECLARE_RWSEM(namespace_sem);
  64. /* /sys/fs */
  65. struct kobject *fs_kobj;
  66. EXPORT_SYMBOL_GPL(fs_kobj);
  67. /*
  68. * vfsmount lock may be taken for read to prevent changes to the
  69. * vfsmount hash, ie. during mountpoint lookups or walking back
  70. * up the tree.
  71. *
  72. * It should be taken for write in all cases where the vfsmount
  73. * tree or hash is modified or when a vfsmount structure is modified.
  74. */
  75. __cacheline_aligned_in_smp DEFINE_SEQLOCK(mount_lock);
  76. static inline struct hlist_head *m_hash(struct vfsmount *mnt, struct dentry *dentry)
  77. {
  78. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  79. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  80. tmp = tmp + (tmp >> m_hash_shift);
  81. return &mount_hashtable[tmp & m_hash_mask];
  82. }
  83. static inline struct hlist_head *mp_hash(struct dentry *dentry)
  84. {
  85. unsigned long tmp = ((unsigned long)dentry / L1_CACHE_BYTES);
  86. tmp = tmp + (tmp >> mp_hash_shift);
  87. return &mountpoint_hashtable[tmp & mp_hash_mask];
  88. }
  89. static int mnt_alloc_id(struct mount *mnt)
  90. {
  91. int res;
  92. retry:
  93. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  94. spin_lock(&mnt_id_lock);
  95. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  96. if (!res)
  97. mnt_id_start = mnt->mnt_id + 1;
  98. spin_unlock(&mnt_id_lock);
  99. if (res == -EAGAIN)
  100. goto retry;
  101. return res;
  102. }
  103. static void mnt_free_id(struct mount *mnt)
  104. {
  105. int id = mnt->mnt_id;
  106. spin_lock(&mnt_id_lock);
  107. ida_remove(&mnt_id_ida, id);
  108. if (mnt_id_start > id)
  109. mnt_id_start = id;
  110. spin_unlock(&mnt_id_lock);
  111. }
  112. /*
  113. * Allocate a new peer group ID
  114. *
  115. * mnt_group_ida is protected by namespace_sem
  116. */
  117. static int mnt_alloc_group_id(struct mount *mnt)
  118. {
  119. int res;
  120. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  121. return -ENOMEM;
  122. res = ida_get_new_above(&mnt_group_ida,
  123. mnt_group_start,
  124. &mnt->mnt_group_id);
  125. if (!res)
  126. mnt_group_start = mnt->mnt_group_id + 1;
  127. return res;
  128. }
  129. /*
  130. * Release a peer group ID
  131. */
  132. void mnt_release_group_id(struct mount *mnt)
  133. {
  134. int id = mnt->mnt_group_id;
  135. ida_remove(&mnt_group_ida, id);
  136. if (mnt_group_start > id)
  137. mnt_group_start = id;
  138. mnt->mnt_group_id = 0;
  139. }
  140. /*
  141. * vfsmount lock must be held for read
  142. */
  143. static inline void mnt_add_count(struct mount *mnt, int n)
  144. {
  145. #ifdef CONFIG_SMP
  146. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  147. #else
  148. preempt_disable();
  149. mnt->mnt_count += n;
  150. preempt_enable();
  151. #endif
  152. }
  153. /*
  154. * vfsmount lock must be held for write
  155. */
  156. unsigned int mnt_get_count(struct mount *mnt)
  157. {
  158. #ifdef CONFIG_SMP
  159. unsigned int count = 0;
  160. int cpu;
  161. for_each_possible_cpu(cpu) {
  162. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  163. }
  164. return count;
  165. #else
  166. return mnt->mnt_count;
  167. #endif
  168. }
  169. static void drop_mountpoint(struct fs_pin *p)
  170. {
  171. struct mount *m = container_of(p, struct mount, mnt_umount);
  172. dput(m->mnt_ex_mountpoint);
  173. pin_remove(p);
  174. mntput(&m->mnt);
  175. }
  176. static struct mount *alloc_vfsmnt(const char *name)
  177. {
  178. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  179. if (mnt) {
  180. int err;
  181. err = mnt_alloc_id(mnt);
  182. if (err)
  183. goto out_free_cache;
  184. if (name) {
  185. mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
  186. if (!mnt->mnt_devname)
  187. goto out_free_id;
  188. }
  189. #ifdef CONFIG_SMP
  190. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  191. if (!mnt->mnt_pcp)
  192. goto out_free_devname;
  193. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  194. #else
  195. mnt->mnt_count = 1;
  196. mnt->mnt_writers = 0;
  197. #endif
  198. INIT_HLIST_NODE(&mnt->mnt_hash);
  199. INIT_LIST_HEAD(&mnt->mnt_child);
  200. INIT_LIST_HEAD(&mnt->mnt_mounts);
  201. INIT_LIST_HEAD(&mnt->mnt_list);
  202. INIT_LIST_HEAD(&mnt->mnt_expire);
  203. INIT_LIST_HEAD(&mnt->mnt_share);
  204. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  205. INIT_LIST_HEAD(&mnt->mnt_slave);
  206. INIT_HLIST_NODE(&mnt->mnt_mp_list);
  207. INIT_LIST_HEAD(&mnt->mnt_umounting);
  208. init_fs_pin(&mnt->mnt_umount, drop_mountpoint);
  209. }
  210. return mnt;
  211. #ifdef CONFIG_SMP
  212. out_free_devname:
  213. kfree_const(mnt->mnt_devname);
  214. #endif
  215. out_free_id:
  216. mnt_free_id(mnt);
  217. out_free_cache:
  218. kmem_cache_free(mnt_cache, mnt);
  219. return NULL;
  220. }
  221. /*
  222. * Most r/o checks on a fs are for operations that take
  223. * discrete amounts of time, like a write() or unlink().
  224. * We must keep track of when those operations start
  225. * (for permission checks) and when they end, so that
  226. * we can determine when writes are able to occur to
  227. * a filesystem.
  228. */
  229. /*
  230. * __mnt_is_readonly: check whether a mount is read-only
  231. * @mnt: the mount to check for its write status
  232. *
  233. * This shouldn't be used directly ouside of the VFS.
  234. * It does not guarantee that the filesystem will stay
  235. * r/w, just that it is right *now*. This can not and
  236. * should not be used in place of IS_RDONLY(inode).
  237. * mnt_want/drop_write() will _keep_ the filesystem
  238. * r/w.
  239. */
  240. int __mnt_is_readonly(struct vfsmount *mnt)
  241. {
  242. if (mnt->mnt_flags & MNT_READONLY)
  243. return 1;
  244. if (sb_rdonly(mnt->mnt_sb))
  245. return 1;
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  249. static inline void mnt_inc_writers(struct mount *mnt)
  250. {
  251. #ifdef CONFIG_SMP
  252. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  253. #else
  254. mnt->mnt_writers++;
  255. #endif
  256. }
  257. static inline void mnt_dec_writers(struct mount *mnt)
  258. {
  259. #ifdef CONFIG_SMP
  260. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  261. #else
  262. mnt->mnt_writers--;
  263. #endif
  264. }
  265. static unsigned int mnt_get_writers(struct mount *mnt)
  266. {
  267. #ifdef CONFIG_SMP
  268. unsigned int count = 0;
  269. int cpu;
  270. for_each_possible_cpu(cpu) {
  271. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  272. }
  273. return count;
  274. #else
  275. return mnt->mnt_writers;
  276. #endif
  277. }
  278. static int mnt_is_readonly(struct vfsmount *mnt)
  279. {
  280. if (mnt->mnt_sb->s_readonly_remount)
  281. return 1;
  282. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  283. smp_rmb();
  284. return __mnt_is_readonly(mnt);
  285. }
  286. /*
  287. * Most r/o & frozen checks on a fs are for operations that take discrete
  288. * amounts of time, like a write() or unlink(). We must keep track of when
  289. * those operations start (for permission checks) and when they end, so that we
  290. * can determine when writes are able to occur to a filesystem.
  291. */
  292. /**
  293. * __mnt_want_write - get write access to a mount without freeze protection
  294. * @m: the mount on which to take a write
  295. *
  296. * This tells the low-level filesystem that a write is about to be performed to
  297. * it, and makes sure that writes are allowed (mnt it read-write) before
  298. * returning success. This operation does not protect against filesystem being
  299. * frozen. When the write operation is finished, __mnt_drop_write() must be
  300. * called. This is effectively a refcount.
  301. */
  302. int __mnt_want_write(struct vfsmount *m)
  303. {
  304. struct mount *mnt = real_mount(m);
  305. int ret = 0;
  306. preempt_disable();
  307. mnt_inc_writers(mnt);
  308. /*
  309. * The store to mnt_inc_writers must be visible before we pass
  310. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  311. * incremented count after it has set MNT_WRITE_HOLD.
  312. */
  313. smp_mb();
  314. while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
  315. cpu_relax();
  316. /*
  317. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  318. * be set to match its requirements. So we must not load that until
  319. * MNT_WRITE_HOLD is cleared.
  320. */
  321. smp_rmb();
  322. if (mnt_is_readonly(m)) {
  323. mnt_dec_writers(mnt);
  324. ret = -EROFS;
  325. }
  326. preempt_enable();
  327. return ret;
  328. }
  329. /**
  330. * mnt_want_write - get write access to a mount
  331. * @m: the mount on which to take a write
  332. *
  333. * This tells the low-level filesystem that a write is about to be performed to
  334. * it, and makes sure that writes are allowed (mount is read-write, filesystem
  335. * is not frozen) before returning success. When the write operation is
  336. * finished, mnt_drop_write() must be called. This is effectively a refcount.
  337. */
  338. int mnt_want_write(struct vfsmount *m)
  339. {
  340. int ret;
  341. sb_start_write(m->mnt_sb);
  342. ret = __mnt_want_write(m);
  343. if (ret)
  344. sb_end_write(m->mnt_sb);
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(mnt_want_write);
  348. /**
  349. * mnt_clone_write - get write access to a mount
  350. * @mnt: the mount on which to take a write
  351. *
  352. * This is effectively like mnt_want_write, except
  353. * it must only be used to take an extra write reference
  354. * on a mountpoint that we already know has a write reference
  355. * on it. This allows some optimisation.
  356. *
  357. * After finished, mnt_drop_write must be called as usual to
  358. * drop the reference.
  359. */
  360. int mnt_clone_write(struct vfsmount *mnt)
  361. {
  362. /* superblock may be r/o */
  363. if (__mnt_is_readonly(mnt))
  364. return -EROFS;
  365. preempt_disable();
  366. mnt_inc_writers(real_mount(mnt));
  367. preempt_enable();
  368. return 0;
  369. }
  370. EXPORT_SYMBOL_GPL(mnt_clone_write);
  371. /**
  372. * __mnt_want_write_file - get write access to a file's mount
  373. * @file: the file who's mount on which to take a write
  374. *
  375. * This is like __mnt_want_write, but it takes a file and can
  376. * do some optimisations if the file is open for write already
  377. */
  378. int __mnt_want_write_file(struct file *file)
  379. {
  380. if (!(file->f_mode & FMODE_WRITER))
  381. return __mnt_want_write(file->f_path.mnt);
  382. else
  383. return mnt_clone_write(file->f_path.mnt);
  384. }
  385. /**
  386. * mnt_want_write_file_path - get write access to a file's mount
  387. * @file: the file who's mount on which to take a write
  388. *
  389. * This is like mnt_want_write, but it takes a file and can
  390. * do some optimisations if the file is open for write already
  391. *
  392. * Called by the vfs for cases when we have an open file at hand, but will do an
  393. * inode operation on it (important distinction for files opened on overlayfs,
  394. * since the file operations will come from the real underlying file, while
  395. * inode operations come from the overlay).
  396. */
  397. int mnt_want_write_file_path(struct file *file)
  398. {
  399. int ret;
  400. sb_start_write(file->f_path.mnt->mnt_sb);
  401. ret = __mnt_want_write_file(file);
  402. if (ret)
  403. sb_end_write(file->f_path.mnt->mnt_sb);
  404. return ret;
  405. }
  406. static inline int may_write_real(struct file *file)
  407. {
  408. struct dentry *dentry = file->f_path.dentry;
  409. struct dentry *upperdentry;
  410. /* Writable file? */
  411. if (file->f_mode & FMODE_WRITER)
  412. return 0;
  413. /* Not overlayfs? */
  414. if (likely(!(dentry->d_flags & DCACHE_OP_REAL)))
  415. return 0;
  416. /* File refers to upper, writable layer? */
  417. upperdentry = d_real(dentry, NULL, 0, D_REAL_UPPER);
  418. if (upperdentry &&
  419. (file_inode(file) == d_inode(upperdentry) ||
  420. file_inode(file) == d_inode(dentry)))
  421. return 0;
  422. /* Lower layer: can't write to real file, sorry... */
  423. return -EPERM;
  424. }
  425. /**
  426. * mnt_want_write_file - get write access to a file's mount
  427. * @file: the file who's mount on which to take a write
  428. *
  429. * This is like mnt_want_write, but it takes a file and can
  430. * do some optimisations if the file is open for write already
  431. *
  432. * Mostly called by filesystems from their ioctl operation before performing
  433. * modification. On overlayfs this needs to check if the file is on a read-only
  434. * lower layer and deny access in that case.
  435. */
  436. int mnt_want_write_file(struct file *file)
  437. {
  438. int ret;
  439. ret = may_write_real(file);
  440. if (!ret) {
  441. sb_start_write(file_inode(file)->i_sb);
  442. ret = __mnt_want_write_file(file);
  443. if (ret)
  444. sb_end_write(file_inode(file)->i_sb);
  445. }
  446. return ret;
  447. }
  448. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  449. /**
  450. * __mnt_drop_write - give up write access to a mount
  451. * @mnt: the mount on which to give up write access
  452. *
  453. * Tells the low-level filesystem that we are done
  454. * performing writes to it. Must be matched with
  455. * __mnt_want_write() call above.
  456. */
  457. void __mnt_drop_write(struct vfsmount *mnt)
  458. {
  459. preempt_disable();
  460. mnt_dec_writers(real_mount(mnt));
  461. preempt_enable();
  462. }
  463. /**
  464. * mnt_drop_write - give up write access to a mount
  465. * @mnt: the mount on which to give up write access
  466. *
  467. * Tells the low-level filesystem that we are done performing writes to it and
  468. * also allows filesystem to be frozen again. Must be matched with
  469. * mnt_want_write() call above.
  470. */
  471. void mnt_drop_write(struct vfsmount *mnt)
  472. {
  473. __mnt_drop_write(mnt);
  474. sb_end_write(mnt->mnt_sb);
  475. }
  476. EXPORT_SYMBOL_GPL(mnt_drop_write);
  477. void __mnt_drop_write_file(struct file *file)
  478. {
  479. __mnt_drop_write(file->f_path.mnt);
  480. }
  481. void mnt_drop_write_file_path(struct file *file)
  482. {
  483. mnt_drop_write(file->f_path.mnt);
  484. }
  485. void mnt_drop_write_file(struct file *file)
  486. {
  487. __mnt_drop_write(file->f_path.mnt);
  488. sb_end_write(file_inode(file)->i_sb);
  489. }
  490. EXPORT_SYMBOL(mnt_drop_write_file);
  491. static int mnt_make_readonly(struct mount *mnt)
  492. {
  493. int ret = 0;
  494. lock_mount_hash();
  495. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  496. /*
  497. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  498. * should be visible before we do.
  499. */
  500. smp_mb();
  501. /*
  502. * With writers on hold, if this value is zero, then there are
  503. * definitely no active writers (although held writers may subsequently
  504. * increment the count, they'll have to wait, and decrement it after
  505. * seeing MNT_READONLY).
  506. *
  507. * It is OK to have counter incremented on one CPU and decremented on
  508. * another: the sum will add up correctly. The danger would be when we
  509. * sum up each counter, if we read a counter before it is incremented,
  510. * but then read another CPU's count which it has been subsequently
  511. * decremented from -- we would see more decrements than we should.
  512. * MNT_WRITE_HOLD protects against this scenario, because
  513. * mnt_want_write first increments count, then smp_mb, then spins on
  514. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  515. * we're counting up here.
  516. */
  517. if (mnt_get_writers(mnt) > 0)
  518. ret = -EBUSY;
  519. else
  520. mnt->mnt.mnt_flags |= MNT_READONLY;
  521. /*
  522. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  523. * that become unheld will see MNT_READONLY.
  524. */
  525. smp_wmb();
  526. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  527. unlock_mount_hash();
  528. return ret;
  529. }
  530. static void __mnt_unmake_readonly(struct mount *mnt)
  531. {
  532. lock_mount_hash();
  533. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  534. unlock_mount_hash();
  535. }
  536. int sb_prepare_remount_readonly(struct super_block *sb)
  537. {
  538. struct mount *mnt;
  539. int err = 0;
  540. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  541. if (atomic_long_read(&sb->s_remove_count))
  542. return -EBUSY;
  543. lock_mount_hash();
  544. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  545. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  546. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  547. smp_mb();
  548. if (mnt_get_writers(mnt) > 0) {
  549. err = -EBUSY;
  550. break;
  551. }
  552. }
  553. }
  554. if (!err && atomic_long_read(&sb->s_remove_count))
  555. err = -EBUSY;
  556. if (!err) {
  557. sb->s_readonly_remount = 1;
  558. smp_wmb();
  559. }
  560. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  561. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  562. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  563. }
  564. unlock_mount_hash();
  565. return err;
  566. }
  567. static void free_vfsmnt(struct mount *mnt)
  568. {
  569. kfree_const(mnt->mnt_devname);
  570. #ifdef CONFIG_SMP
  571. free_percpu(mnt->mnt_pcp);
  572. #endif
  573. kmem_cache_free(mnt_cache, mnt);
  574. }
  575. static void delayed_free_vfsmnt(struct rcu_head *head)
  576. {
  577. free_vfsmnt(container_of(head, struct mount, mnt_rcu));
  578. }
  579. /* call under rcu_read_lock */
  580. int __legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  581. {
  582. struct mount *mnt;
  583. if (read_seqretry(&mount_lock, seq))
  584. return 1;
  585. if (bastard == NULL)
  586. return 0;
  587. mnt = real_mount(bastard);
  588. mnt_add_count(mnt, 1);
  589. if (likely(!read_seqretry(&mount_lock, seq)))
  590. return 0;
  591. if (bastard->mnt_flags & MNT_SYNC_UMOUNT) {
  592. mnt_add_count(mnt, -1);
  593. return 1;
  594. }
  595. return -1;
  596. }
  597. /* call under rcu_read_lock */
  598. bool legitimize_mnt(struct vfsmount *bastard, unsigned seq)
  599. {
  600. int res = __legitimize_mnt(bastard, seq);
  601. if (likely(!res))
  602. return true;
  603. if (unlikely(res < 0)) {
  604. rcu_read_unlock();
  605. mntput(bastard);
  606. rcu_read_lock();
  607. }
  608. return false;
  609. }
  610. /*
  611. * find the first mount at @dentry on vfsmount @mnt.
  612. * call under rcu_read_lock()
  613. */
  614. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
  615. {
  616. struct hlist_head *head = m_hash(mnt, dentry);
  617. struct mount *p;
  618. hlist_for_each_entry_rcu(p, head, mnt_hash)
  619. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
  620. return p;
  621. return NULL;
  622. }
  623. /*
  624. * lookup_mnt - Return the first child mount mounted at path
  625. *
  626. * "First" means first mounted chronologically. If you create the
  627. * following mounts:
  628. *
  629. * mount /dev/sda1 /mnt
  630. * mount /dev/sda2 /mnt
  631. * mount /dev/sda3 /mnt
  632. *
  633. * Then lookup_mnt() on the base /mnt dentry in the root mount will
  634. * return successively the root dentry and vfsmount of /dev/sda1, then
  635. * /dev/sda2, then /dev/sda3, then NULL.
  636. *
  637. * lookup_mnt takes a reference to the found vfsmount.
  638. */
  639. struct vfsmount *lookup_mnt(const struct path *path)
  640. {
  641. struct mount *child_mnt;
  642. struct vfsmount *m;
  643. unsigned seq;
  644. rcu_read_lock();
  645. do {
  646. seq = read_seqbegin(&mount_lock);
  647. child_mnt = __lookup_mnt(path->mnt, path->dentry);
  648. m = child_mnt ? &child_mnt->mnt : NULL;
  649. } while (!legitimize_mnt(m, seq));
  650. rcu_read_unlock();
  651. return m;
  652. }
  653. /*
  654. * __is_local_mountpoint - Test to see if dentry is a mountpoint in the
  655. * current mount namespace.
  656. *
  657. * The common case is dentries are not mountpoints at all and that
  658. * test is handled inline. For the slow case when we are actually
  659. * dealing with a mountpoint of some kind, walk through all of the
  660. * mounts in the current mount namespace and test to see if the dentry
  661. * is a mountpoint.
  662. *
  663. * The mount_hashtable is not usable in the context because we
  664. * need to identify all mounts that may be in the current mount
  665. * namespace not just a mount that happens to have some specified
  666. * parent mount.
  667. */
  668. bool __is_local_mountpoint(struct dentry *dentry)
  669. {
  670. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  671. struct mount *mnt;
  672. bool is_covered = false;
  673. if (!d_mountpoint(dentry))
  674. goto out;
  675. down_read(&namespace_sem);
  676. list_for_each_entry(mnt, &ns->list, mnt_list) {
  677. is_covered = (mnt->mnt_mountpoint == dentry);
  678. if (is_covered)
  679. break;
  680. }
  681. up_read(&namespace_sem);
  682. out:
  683. return is_covered;
  684. }
  685. static struct mountpoint *lookup_mountpoint(struct dentry *dentry)
  686. {
  687. struct hlist_head *chain = mp_hash(dentry);
  688. struct mountpoint *mp;
  689. hlist_for_each_entry(mp, chain, m_hash) {
  690. if (mp->m_dentry == dentry) {
  691. /* might be worth a WARN_ON() */
  692. if (d_unlinked(dentry))
  693. return ERR_PTR(-ENOENT);
  694. mp->m_count++;
  695. return mp;
  696. }
  697. }
  698. return NULL;
  699. }
  700. static struct mountpoint *get_mountpoint(struct dentry *dentry)
  701. {
  702. struct mountpoint *mp, *new = NULL;
  703. int ret;
  704. if (d_mountpoint(dentry)) {
  705. mountpoint:
  706. read_seqlock_excl(&mount_lock);
  707. mp = lookup_mountpoint(dentry);
  708. read_sequnlock_excl(&mount_lock);
  709. if (mp)
  710. goto done;
  711. }
  712. if (!new)
  713. new = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
  714. if (!new)
  715. return ERR_PTR(-ENOMEM);
  716. /* Exactly one processes may set d_mounted */
  717. ret = d_set_mounted(dentry);
  718. /* Someone else set d_mounted? */
  719. if (ret == -EBUSY)
  720. goto mountpoint;
  721. /* The dentry is not available as a mountpoint? */
  722. mp = ERR_PTR(ret);
  723. if (ret)
  724. goto done;
  725. /* Add the new mountpoint to the hash table */
  726. read_seqlock_excl(&mount_lock);
  727. new->m_dentry = dentry;
  728. new->m_count = 1;
  729. hlist_add_head(&new->m_hash, mp_hash(dentry));
  730. INIT_HLIST_HEAD(&new->m_list);
  731. read_sequnlock_excl(&mount_lock);
  732. mp = new;
  733. new = NULL;
  734. done:
  735. kfree(new);
  736. return mp;
  737. }
  738. static void put_mountpoint(struct mountpoint *mp)
  739. {
  740. if (!--mp->m_count) {
  741. struct dentry *dentry = mp->m_dentry;
  742. BUG_ON(!hlist_empty(&mp->m_list));
  743. spin_lock(&dentry->d_lock);
  744. dentry->d_flags &= ~DCACHE_MOUNTED;
  745. spin_unlock(&dentry->d_lock);
  746. hlist_del(&mp->m_hash);
  747. kfree(mp);
  748. }
  749. }
  750. static inline int check_mnt(struct mount *mnt)
  751. {
  752. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  753. }
  754. /*
  755. * vfsmount lock must be held for write
  756. */
  757. static void touch_mnt_namespace(struct mnt_namespace *ns)
  758. {
  759. if (ns) {
  760. ns->event = ++event;
  761. wake_up_interruptible(&ns->poll);
  762. }
  763. }
  764. /*
  765. * vfsmount lock must be held for write
  766. */
  767. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  768. {
  769. if (ns && ns->event != event) {
  770. ns->event = event;
  771. wake_up_interruptible(&ns->poll);
  772. }
  773. }
  774. /*
  775. * vfsmount lock must be held for write
  776. */
  777. static void unhash_mnt(struct mount *mnt)
  778. {
  779. mnt->mnt_parent = mnt;
  780. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  781. list_del_init(&mnt->mnt_child);
  782. hlist_del_init_rcu(&mnt->mnt_hash);
  783. hlist_del_init(&mnt->mnt_mp_list);
  784. put_mountpoint(mnt->mnt_mp);
  785. mnt->mnt_mp = NULL;
  786. }
  787. /*
  788. * vfsmount lock must be held for write
  789. */
  790. static void detach_mnt(struct mount *mnt, struct path *old_path)
  791. {
  792. old_path->dentry = mnt->mnt_mountpoint;
  793. old_path->mnt = &mnt->mnt_parent->mnt;
  794. unhash_mnt(mnt);
  795. }
  796. /*
  797. * vfsmount lock must be held for write
  798. */
  799. static void umount_mnt(struct mount *mnt)
  800. {
  801. /* old mountpoint will be dropped when we can do that */
  802. mnt->mnt_ex_mountpoint = mnt->mnt_mountpoint;
  803. unhash_mnt(mnt);
  804. }
  805. /*
  806. * vfsmount lock must be held for write
  807. */
  808. void mnt_set_mountpoint(struct mount *mnt,
  809. struct mountpoint *mp,
  810. struct mount *child_mnt)
  811. {
  812. mp->m_count++;
  813. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  814. child_mnt->mnt_mountpoint = dget(mp->m_dentry);
  815. child_mnt->mnt_parent = mnt;
  816. child_mnt->mnt_mp = mp;
  817. hlist_add_head(&child_mnt->mnt_mp_list, &mp->m_list);
  818. }
  819. static void __attach_mnt(struct mount *mnt, struct mount *parent)
  820. {
  821. hlist_add_head_rcu(&mnt->mnt_hash,
  822. m_hash(&parent->mnt, mnt->mnt_mountpoint));
  823. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  824. }
  825. /*
  826. * vfsmount lock must be held for write
  827. */
  828. static void attach_mnt(struct mount *mnt,
  829. struct mount *parent,
  830. struct mountpoint *mp)
  831. {
  832. mnt_set_mountpoint(parent, mp, mnt);
  833. __attach_mnt(mnt, parent);
  834. }
  835. void mnt_change_mountpoint(struct mount *parent, struct mountpoint *mp, struct mount *mnt)
  836. {
  837. struct mountpoint *old_mp = mnt->mnt_mp;
  838. struct dentry *old_mountpoint = mnt->mnt_mountpoint;
  839. struct mount *old_parent = mnt->mnt_parent;
  840. list_del_init(&mnt->mnt_child);
  841. hlist_del_init(&mnt->mnt_mp_list);
  842. hlist_del_init_rcu(&mnt->mnt_hash);
  843. attach_mnt(mnt, parent, mp);
  844. put_mountpoint(old_mp);
  845. /*
  846. * Safely avoid even the suggestion this code might sleep or
  847. * lock the mount hash by taking advantage of the knowledge that
  848. * mnt_change_mountpoint will not release the final reference
  849. * to a mountpoint.
  850. *
  851. * During mounting, the mount passed in as the parent mount will
  852. * continue to use the old mountpoint and during unmounting, the
  853. * old mountpoint will continue to exist until namespace_unlock,
  854. * which happens well after mnt_change_mountpoint.
  855. */
  856. spin_lock(&old_mountpoint->d_lock);
  857. old_mountpoint->d_lockref.count--;
  858. spin_unlock(&old_mountpoint->d_lock);
  859. mnt_add_count(old_parent, -1);
  860. }
  861. /*
  862. * vfsmount lock must be held for write
  863. */
  864. static void commit_tree(struct mount *mnt)
  865. {
  866. struct mount *parent = mnt->mnt_parent;
  867. struct mount *m;
  868. LIST_HEAD(head);
  869. struct mnt_namespace *n = parent->mnt_ns;
  870. BUG_ON(parent == mnt);
  871. list_add_tail(&head, &mnt->mnt_list);
  872. list_for_each_entry(m, &head, mnt_list)
  873. m->mnt_ns = n;
  874. list_splice(&head, n->list.prev);
  875. n->mounts += n->pending_mounts;
  876. n->pending_mounts = 0;
  877. __attach_mnt(mnt, parent);
  878. touch_mnt_namespace(n);
  879. }
  880. static struct mount *next_mnt(struct mount *p, struct mount *root)
  881. {
  882. struct list_head *next = p->mnt_mounts.next;
  883. if (next == &p->mnt_mounts) {
  884. while (1) {
  885. if (p == root)
  886. return NULL;
  887. next = p->mnt_child.next;
  888. if (next != &p->mnt_parent->mnt_mounts)
  889. break;
  890. p = p->mnt_parent;
  891. }
  892. }
  893. return list_entry(next, struct mount, mnt_child);
  894. }
  895. static struct mount *skip_mnt_tree(struct mount *p)
  896. {
  897. struct list_head *prev = p->mnt_mounts.prev;
  898. while (prev != &p->mnt_mounts) {
  899. p = list_entry(prev, struct mount, mnt_child);
  900. prev = p->mnt_mounts.prev;
  901. }
  902. return p;
  903. }
  904. struct vfsmount *
  905. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  906. {
  907. struct mount *mnt;
  908. struct dentry *root;
  909. if (!type)
  910. return ERR_PTR(-ENODEV);
  911. mnt = alloc_vfsmnt(name);
  912. if (!mnt)
  913. return ERR_PTR(-ENOMEM);
  914. if (flags & SB_KERNMOUNT)
  915. mnt->mnt.mnt_flags = MNT_INTERNAL;
  916. root = mount_fs(type, flags, name, data);
  917. if (IS_ERR(root)) {
  918. mnt_free_id(mnt);
  919. free_vfsmnt(mnt);
  920. return ERR_CAST(root);
  921. }
  922. mnt->mnt.mnt_root = root;
  923. mnt->mnt.mnt_sb = root->d_sb;
  924. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  925. mnt->mnt_parent = mnt;
  926. lock_mount_hash();
  927. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  928. unlock_mount_hash();
  929. return &mnt->mnt;
  930. }
  931. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  932. struct vfsmount *
  933. vfs_submount(const struct dentry *mountpoint, struct file_system_type *type,
  934. const char *name, void *data)
  935. {
  936. /* Until it is worked out how to pass the user namespace
  937. * through from the parent mount to the submount don't support
  938. * unprivileged mounts with submounts.
  939. */
  940. if (mountpoint->d_sb->s_user_ns != &init_user_ns)
  941. return ERR_PTR(-EPERM);
  942. return vfs_kern_mount(type, SB_SUBMOUNT, name, data);
  943. }
  944. EXPORT_SYMBOL_GPL(vfs_submount);
  945. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  946. int flag)
  947. {
  948. struct super_block *sb = old->mnt.mnt_sb;
  949. struct mount *mnt;
  950. int err;
  951. mnt = alloc_vfsmnt(old->mnt_devname);
  952. if (!mnt)
  953. return ERR_PTR(-ENOMEM);
  954. if (flag & (CL_SLAVE | CL_PRIVATE | CL_SHARED_TO_SLAVE))
  955. mnt->mnt_group_id = 0; /* not a peer of original */
  956. else
  957. mnt->mnt_group_id = old->mnt_group_id;
  958. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  959. err = mnt_alloc_group_id(mnt);
  960. if (err)
  961. goto out_free;
  962. }
  963. mnt->mnt.mnt_flags = old->mnt.mnt_flags;
  964. mnt->mnt.mnt_flags &= ~(MNT_WRITE_HOLD|MNT_MARKED|MNT_INTERNAL);
  965. /* Don't allow unprivileged users to change mount flags */
  966. if (flag & CL_UNPRIVILEGED) {
  967. mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
  968. if (mnt->mnt.mnt_flags & MNT_READONLY)
  969. mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
  970. if (mnt->mnt.mnt_flags & MNT_NODEV)
  971. mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
  972. if (mnt->mnt.mnt_flags & MNT_NOSUID)
  973. mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
  974. if (mnt->mnt.mnt_flags & MNT_NOEXEC)
  975. mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
  976. }
  977. /* Don't allow unprivileged users to reveal what is under a mount */
  978. if ((flag & CL_UNPRIVILEGED) &&
  979. (!(flag & CL_EXPIRE) || list_empty(&old->mnt_expire)))
  980. mnt->mnt.mnt_flags |= MNT_LOCKED;
  981. atomic_inc(&sb->s_active);
  982. mnt->mnt.mnt_sb = sb;
  983. mnt->mnt.mnt_root = dget(root);
  984. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  985. mnt->mnt_parent = mnt;
  986. lock_mount_hash();
  987. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  988. unlock_mount_hash();
  989. if ((flag & CL_SLAVE) ||
  990. ((flag & CL_SHARED_TO_SLAVE) && IS_MNT_SHARED(old))) {
  991. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  992. mnt->mnt_master = old;
  993. CLEAR_MNT_SHARED(mnt);
  994. } else if (!(flag & CL_PRIVATE)) {
  995. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  996. list_add(&mnt->mnt_share, &old->mnt_share);
  997. if (IS_MNT_SLAVE(old))
  998. list_add(&mnt->mnt_slave, &old->mnt_slave);
  999. mnt->mnt_master = old->mnt_master;
  1000. } else {
  1001. CLEAR_MNT_SHARED(mnt);
  1002. }
  1003. if (flag & CL_MAKE_SHARED)
  1004. set_mnt_shared(mnt);
  1005. /* stick the duplicate mount on the same expiry list
  1006. * as the original if that was on one */
  1007. if (flag & CL_EXPIRE) {
  1008. if (!list_empty(&old->mnt_expire))
  1009. list_add(&mnt->mnt_expire, &old->mnt_expire);
  1010. }
  1011. return mnt;
  1012. out_free:
  1013. mnt_free_id(mnt);
  1014. free_vfsmnt(mnt);
  1015. return ERR_PTR(err);
  1016. }
  1017. static void cleanup_mnt(struct mount *mnt)
  1018. {
  1019. /*
  1020. * This probably indicates that somebody messed
  1021. * up a mnt_want/drop_write() pair. If this
  1022. * happens, the filesystem was probably unable
  1023. * to make r/w->r/o transitions.
  1024. */
  1025. /*
  1026. * The locking used to deal with mnt_count decrement provides barriers,
  1027. * so mnt_get_writers() below is safe.
  1028. */
  1029. WARN_ON(mnt_get_writers(mnt));
  1030. if (unlikely(mnt->mnt_pins.first))
  1031. mnt_pin_kill(mnt);
  1032. fsnotify_vfsmount_delete(&mnt->mnt);
  1033. dput(mnt->mnt.mnt_root);
  1034. deactivate_super(mnt->mnt.mnt_sb);
  1035. mnt_free_id(mnt);
  1036. call_rcu(&mnt->mnt_rcu, delayed_free_vfsmnt);
  1037. }
  1038. static void __cleanup_mnt(struct rcu_head *head)
  1039. {
  1040. cleanup_mnt(container_of(head, struct mount, mnt_rcu));
  1041. }
  1042. static LLIST_HEAD(delayed_mntput_list);
  1043. static void delayed_mntput(struct work_struct *unused)
  1044. {
  1045. struct llist_node *node = llist_del_all(&delayed_mntput_list);
  1046. struct mount *m, *t;
  1047. llist_for_each_entry_safe(m, t, node, mnt_llist)
  1048. cleanup_mnt(m);
  1049. }
  1050. static DECLARE_DELAYED_WORK(delayed_mntput_work, delayed_mntput);
  1051. static void mntput_no_expire(struct mount *mnt)
  1052. {
  1053. rcu_read_lock();
  1054. mnt_add_count(mnt, -1);
  1055. if (likely(mnt->mnt_ns)) { /* shouldn't be the last one */
  1056. rcu_read_unlock();
  1057. return;
  1058. }
  1059. lock_mount_hash();
  1060. if (mnt_get_count(mnt)) {
  1061. rcu_read_unlock();
  1062. unlock_mount_hash();
  1063. return;
  1064. }
  1065. if (unlikely(mnt->mnt.mnt_flags & MNT_DOOMED)) {
  1066. rcu_read_unlock();
  1067. unlock_mount_hash();
  1068. return;
  1069. }
  1070. mnt->mnt.mnt_flags |= MNT_DOOMED;
  1071. rcu_read_unlock();
  1072. list_del(&mnt->mnt_instance);
  1073. if (unlikely(!list_empty(&mnt->mnt_mounts))) {
  1074. struct mount *p, *tmp;
  1075. list_for_each_entry_safe(p, tmp, &mnt->mnt_mounts, mnt_child) {
  1076. umount_mnt(p);
  1077. }
  1078. }
  1079. unlock_mount_hash();
  1080. if (likely(!(mnt->mnt.mnt_flags & MNT_INTERNAL))) {
  1081. struct task_struct *task = current;
  1082. if (likely(!(task->flags & PF_KTHREAD))) {
  1083. init_task_work(&mnt->mnt_rcu, __cleanup_mnt);
  1084. if (!task_work_add(task, &mnt->mnt_rcu, true))
  1085. return;
  1086. }
  1087. if (llist_add(&mnt->mnt_llist, &delayed_mntput_list))
  1088. schedule_delayed_work(&delayed_mntput_work, 1);
  1089. return;
  1090. }
  1091. cleanup_mnt(mnt);
  1092. }
  1093. void mntput(struct vfsmount *mnt)
  1094. {
  1095. if (mnt) {
  1096. struct mount *m = real_mount(mnt);
  1097. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  1098. if (unlikely(m->mnt_expiry_mark))
  1099. m->mnt_expiry_mark = 0;
  1100. mntput_no_expire(m);
  1101. }
  1102. }
  1103. EXPORT_SYMBOL(mntput);
  1104. struct vfsmount *mntget(struct vfsmount *mnt)
  1105. {
  1106. if (mnt)
  1107. mnt_add_count(real_mount(mnt), 1);
  1108. return mnt;
  1109. }
  1110. EXPORT_SYMBOL(mntget);
  1111. /* path_is_mountpoint() - Check if path is a mount in the current
  1112. * namespace.
  1113. *
  1114. * d_mountpoint() can only be used reliably to establish if a dentry is
  1115. * not mounted in any namespace and that common case is handled inline.
  1116. * d_mountpoint() isn't aware of the possibility there may be multiple
  1117. * mounts using a given dentry in a different namespace. This function
  1118. * checks if the passed in path is a mountpoint rather than the dentry
  1119. * alone.
  1120. */
  1121. bool path_is_mountpoint(const struct path *path)
  1122. {
  1123. unsigned seq;
  1124. bool res;
  1125. if (!d_mountpoint(path->dentry))
  1126. return false;
  1127. rcu_read_lock();
  1128. do {
  1129. seq = read_seqbegin(&mount_lock);
  1130. res = __path_is_mountpoint(path);
  1131. } while (read_seqretry(&mount_lock, seq));
  1132. rcu_read_unlock();
  1133. return res;
  1134. }
  1135. EXPORT_SYMBOL(path_is_mountpoint);
  1136. struct vfsmount *mnt_clone_internal(const struct path *path)
  1137. {
  1138. struct mount *p;
  1139. p = clone_mnt(real_mount(path->mnt), path->dentry, CL_PRIVATE);
  1140. if (IS_ERR(p))
  1141. return ERR_CAST(p);
  1142. p->mnt.mnt_flags |= MNT_INTERNAL;
  1143. return &p->mnt;
  1144. }
  1145. #ifdef CONFIG_PROC_FS
  1146. /* iterator; we want it to have access to namespace_sem, thus here... */
  1147. static void *m_start(struct seq_file *m, loff_t *pos)
  1148. {
  1149. struct proc_mounts *p = m->private;
  1150. down_read(&namespace_sem);
  1151. if (p->cached_event == p->ns->event) {
  1152. void *v = p->cached_mount;
  1153. if (*pos == p->cached_index)
  1154. return v;
  1155. if (*pos == p->cached_index + 1) {
  1156. v = seq_list_next(v, &p->ns->list, &p->cached_index);
  1157. return p->cached_mount = v;
  1158. }
  1159. }
  1160. p->cached_event = p->ns->event;
  1161. p->cached_mount = seq_list_start(&p->ns->list, *pos);
  1162. p->cached_index = *pos;
  1163. return p->cached_mount;
  1164. }
  1165. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  1166. {
  1167. struct proc_mounts *p = m->private;
  1168. p->cached_mount = seq_list_next(v, &p->ns->list, pos);
  1169. p->cached_index = *pos;
  1170. return p->cached_mount;
  1171. }
  1172. static void m_stop(struct seq_file *m, void *v)
  1173. {
  1174. up_read(&namespace_sem);
  1175. }
  1176. static int m_show(struct seq_file *m, void *v)
  1177. {
  1178. struct proc_mounts *p = m->private;
  1179. struct mount *r = list_entry(v, struct mount, mnt_list);
  1180. return p->show(m, &r->mnt);
  1181. }
  1182. const struct seq_operations mounts_op = {
  1183. .start = m_start,
  1184. .next = m_next,
  1185. .stop = m_stop,
  1186. .show = m_show,
  1187. };
  1188. #endif /* CONFIG_PROC_FS */
  1189. /**
  1190. * may_umount_tree - check if a mount tree is busy
  1191. * @mnt: root of mount tree
  1192. *
  1193. * This is called to check if a tree of mounts has any
  1194. * open files, pwds, chroots or sub mounts that are
  1195. * busy.
  1196. */
  1197. int may_umount_tree(struct vfsmount *m)
  1198. {
  1199. struct mount *mnt = real_mount(m);
  1200. int actual_refs = 0;
  1201. int minimum_refs = 0;
  1202. struct mount *p;
  1203. BUG_ON(!m);
  1204. /* write lock needed for mnt_get_count */
  1205. lock_mount_hash();
  1206. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1207. actual_refs += mnt_get_count(p);
  1208. minimum_refs += 2;
  1209. }
  1210. unlock_mount_hash();
  1211. if (actual_refs > minimum_refs)
  1212. return 0;
  1213. return 1;
  1214. }
  1215. EXPORT_SYMBOL(may_umount_tree);
  1216. /**
  1217. * may_umount - check if a mount point is busy
  1218. * @mnt: root of mount
  1219. *
  1220. * This is called to check if a mount point has any
  1221. * open files, pwds, chroots or sub mounts. If the
  1222. * mount has sub mounts this will return busy
  1223. * regardless of whether the sub mounts are busy.
  1224. *
  1225. * Doesn't take quota and stuff into account. IOW, in some cases it will
  1226. * give false negatives. The main reason why it's here is that we need
  1227. * a non-destructive way to look for easily umountable filesystems.
  1228. */
  1229. int may_umount(struct vfsmount *mnt)
  1230. {
  1231. int ret = 1;
  1232. down_read(&namespace_sem);
  1233. lock_mount_hash();
  1234. if (propagate_mount_busy(real_mount(mnt), 2))
  1235. ret = 0;
  1236. unlock_mount_hash();
  1237. up_read(&namespace_sem);
  1238. return ret;
  1239. }
  1240. EXPORT_SYMBOL(may_umount);
  1241. static HLIST_HEAD(unmounted); /* protected by namespace_sem */
  1242. static void namespace_unlock(void)
  1243. {
  1244. struct hlist_head head;
  1245. hlist_move_list(&unmounted, &head);
  1246. up_write(&namespace_sem);
  1247. if (likely(hlist_empty(&head)))
  1248. return;
  1249. synchronize_rcu();
  1250. group_pin_kill(&head);
  1251. }
  1252. static inline void namespace_lock(void)
  1253. {
  1254. down_write(&namespace_sem);
  1255. }
  1256. enum umount_tree_flags {
  1257. UMOUNT_SYNC = 1,
  1258. UMOUNT_PROPAGATE = 2,
  1259. UMOUNT_CONNECTED = 4,
  1260. };
  1261. static bool disconnect_mount(struct mount *mnt, enum umount_tree_flags how)
  1262. {
  1263. /* Leaving mounts connected is only valid for lazy umounts */
  1264. if (how & UMOUNT_SYNC)
  1265. return true;
  1266. /* A mount without a parent has nothing to be connected to */
  1267. if (!mnt_has_parent(mnt))
  1268. return true;
  1269. /* Because the reference counting rules change when mounts are
  1270. * unmounted and connected, umounted mounts may not be
  1271. * connected to mounted mounts.
  1272. */
  1273. if (!(mnt->mnt_parent->mnt.mnt_flags & MNT_UMOUNT))
  1274. return true;
  1275. /* Has it been requested that the mount remain connected? */
  1276. if (how & UMOUNT_CONNECTED)
  1277. return false;
  1278. /* Is the mount locked such that it needs to remain connected? */
  1279. if (IS_MNT_LOCKED(mnt))
  1280. return false;
  1281. /* By default disconnect the mount */
  1282. return true;
  1283. }
  1284. /*
  1285. * mount_lock must be held
  1286. * namespace_sem must be held for write
  1287. */
  1288. static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
  1289. {
  1290. LIST_HEAD(tmp_list);
  1291. struct mount *p;
  1292. if (how & UMOUNT_PROPAGATE)
  1293. propagate_mount_unlock(mnt);
  1294. /* Gather the mounts to umount */
  1295. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1296. p->mnt.mnt_flags |= MNT_UMOUNT;
  1297. list_move(&p->mnt_list, &tmp_list);
  1298. }
  1299. /* Hide the mounts from mnt_mounts */
  1300. list_for_each_entry(p, &tmp_list, mnt_list) {
  1301. list_del_init(&p->mnt_child);
  1302. }
  1303. /* Add propogated mounts to the tmp_list */
  1304. if (how & UMOUNT_PROPAGATE)
  1305. propagate_umount(&tmp_list);
  1306. while (!list_empty(&tmp_list)) {
  1307. struct mnt_namespace *ns;
  1308. bool disconnect;
  1309. p = list_first_entry(&tmp_list, struct mount, mnt_list);
  1310. list_del_init(&p->mnt_expire);
  1311. list_del_init(&p->mnt_list);
  1312. ns = p->mnt_ns;
  1313. if (ns) {
  1314. ns->mounts--;
  1315. __touch_mnt_namespace(ns);
  1316. }
  1317. p->mnt_ns = NULL;
  1318. if (how & UMOUNT_SYNC)
  1319. p->mnt.mnt_flags |= MNT_SYNC_UMOUNT;
  1320. disconnect = disconnect_mount(p, how);
  1321. pin_insert_group(&p->mnt_umount, &p->mnt_parent->mnt,
  1322. disconnect ? &unmounted : NULL);
  1323. if (mnt_has_parent(p)) {
  1324. mnt_add_count(p->mnt_parent, -1);
  1325. if (!disconnect) {
  1326. /* Don't forget about p */
  1327. list_add_tail(&p->mnt_child, &p->mnt_parent->mnt_mounts);
  1328. } else {
  1329. umount_mnt(p);
  1330. }
  1331. }
  1332. change_mnt_propagation(p, MS_PRIVATE);
  1333. }
  1334. }
  1335. static void shrink_submounts(struct mount *mnt);
  1336. static int do_umount(struct mount *mnt, int flags)
  1337. {
  1338. struct super_block *sb = mnt->mnt.mnt_sb;
  1339. int retval;
  1340. retval = security_sb_umount(&mnt->mnt, flags);
  1341. if (retval)
  1342. return retval;
  1343. /*
  1344. * Allow userspace to request a mountpoint be expired rather than
  1345. * unmounting unconditionally. Unmount only happens if:
  1346. * (1) the mark is already set (the mark is cleared by mntput())
  1347. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  1348. */
  1349. if (flags & MNT_EXPIRE) {
  1350. if (&mnt->mnt == current->fs->root.mnt ||
  1351. flags & (MNT_FORCE | MNT_DETACH))
  1352. return -EINVAL;
  1353. /*
  1354. * probably don't strictly need the lock here if we examined
  1355. * all race cases, but it's a slowpath.
  1356. */
  1357. lock_mount_hash();
  1358. if (mnt_get_count(mnt) != 2) {
  1359. unlock_mount_hash();
  1360. return -EBUSY;
  1361. }
  1362. unlock_mount_hash();
  1363. if (!xchg(&mnt->mnt_expiry_mark, 1))
  1364. return -EAGAIN;
  1365. }
  1366. /*
  1367. * If we may have to abort operations to get out of this
  1368. * mount, and they will themselves hold resources we must
  1369. * allow the fs to do things. In the Unix tradition of
  1370. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1371. * might fail to complete on the first run through as other tasks
  1372. * must return, and the like. Thats for the mount program to worry
  1373. * about for the moment.
  1374. */
  1375. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1376. sb->s_op->umount_begin(sb);
  1377. }
  1378. /*
  1379. * No sense to grab the lock for this test, but test itself looks
  1380. * somewhat bogus. Suggestions for better replacement?
  1381. * Ho-hum... In principle, we might treat that as umount + switch
  1382. * to rootfs. GC would eventually take care of the old vfsmount.
  1383. * Actually it makes sense, especially if rootfs would contain a
  1384. * /reboot - static binary that would close all descriptors and
  1385. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1386. */
  1387. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1388. /*
  1389. * Special case for "unmounting" root ...
  1390. * we just try to remount it readonly.
  1391. */
  1392. if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  1393. return -EPERM;
  1394. down_write(&sb->s_umount);
  1395. if (!sb_rdonly(sb))
  1396. retval = do_remount_sb(sb, SB_RDONLY, NULL, 0);
  1397. up_write(&sb->s_umount);
  1398. return retval;
  1399. }
  1400. namespace_lock();
  1401. lock_mount_hash();
  1402. event++;
  1403. if (flags & MNT_DETACH) {
  1404. if (!list_empty(&mnt->mnt_list))
  1405. umount_tree(mnt, UMOUNT_PROPAGATE);
  1406. retval = 0;
  1407. } else {
  1408. shrink_submounts(mnt);
  1409. retval = -EBUSY;
  1410. if (!propagate_mount_busy(mnt, 2)) {
  1411. if (!list_empty(&mnt->mnt_list))
  1412. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  1413. retval = 0;
  1414. }
  1415. }
  1416. unlock_mount_hash();
  1417. namespace_unlock();
  1418. return retval;
  1419. }
  1420. /*
  1421. * __detach_mounts - lazily unmount all mounts on the specified dentry
  1422. *
  1423. * During unlink, rmdir, and d_drop it is possible to loose the path
  1424. * to an existing mountpoint, and wind up leaking the mount.
  1425. * detach_mounts allows lazily unmounting those mounts instead of
  1426. * leaking them.
  1427. *
  1428. * The caller may hold dentry->d_inode->i_mutex.
  1429. */
  1430. void __detach_mounts(struct dentry *dentry)
  1431. {
  1432. struct mountpoint *mp;
  1433. struct mount *mnt;
  1434. namespace_lock();
  1435. lock_mount_hash();
  1436. mp = lookup_mountpoint(dentry);
  1437. if (IS_ERR_OR_NULL(mp))
  1438. goto out_unlock;
  1439. event++;
  1440. while (!hlist_empty(&mp->m_list)) {
  1441. mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
  1442. if (mnt->mnt.mnt_flags & MNT_UMOUNT) {
  1443. hlist_add_head(&mnt->mnt_umount.s_list, &unmounted);
  1444. umount_mnt(mnt);
  1445. }
  1446. else umount_tree(mnt, UMOUNT_CONNECTED);
  1447. }
  1448. put_mountpoint(mp);
  1449. out_unlock:
  1450. unlock_mount_hash();
  1451. namespace_unlock();
  1452. }
  1453. /*
  1454. * Is the caller allowed to modify his namespace?
  1455. */
  1456. static inline bool may_mount(void)
  1457. {
  1458. return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
  1459. }
  1460. static inline bool may_mandlock(void)
  1461. {
  1462. #ifndef CONFIG_MANDATORY_FILE_LOCKING
  1463. return false;
  1464. #endif
  1465. return capable(CAP_SYS_ADMIN);
  1466. }
  1467. /*
  1468. * Now umount can handle mount points as well as block devices.
  1469. * This is important for filesystems which use unnamed block devices.
  1470. *
  1471. * We now support a flag for forced unmount like the other 'big iron'
  1472. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1473. */
  1474. int ksys_umount(char __user *name, int flags)
  1475. {
  1476. struct path path;
  1477. struct mount *mnt;
  1478. int retval;
  1479. int lookup_flags = 0;
  1480. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1481. return -EINVAL;
  1482. if (!may_mount())
  1483. return -EPERM;
  1484. if (!(flags & UMOUNT_NOFOLLOW))
  1485. lookup_flags |= LOOKUP_FOLLOW;
  1486. retval = user_path_mountpoint_at(AT_FDCWD, name, lookup_flags, &path);
  1487. if (retval)
  1488. goto out;
  1489. mnt = real_mount(path.mnt);
  1490. retval = -EINVAL;
  1491. if (path.dentry != path.mnt->mnt_root)
  1492. goto dput_and_out;
  1493. if (!check_mnt(mnt))
  1494. goto dput_and_out;
  1495. if (mnt->mnt.mnt_flags & MNT_LOCKED)
  1496. goto dput_and_out;
  1497. retval = -EPERM;
  1498. if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
  1499. goto dput_and_out;
  1500. retval = do_umount(mnt, flags);
  1501. dput_and_out:
  1502. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1503. dput(path.dentry);
  1504. mntput_no_expire(mnt);
  1505. out:
  1506. return retval;
  1507. }
  1508. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1509. {
  1510. return ksys_umount(name, flags);
  1511. }
  1512. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1513. /*
  1514. * The 2.0 compatible umount. No flags.
  1515. */
  1516. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1517. {
  1518. return ksys_umount(name, 0);
  1519. }
  1520. #endif
  1521. static bool is_mnt_ns_file(struct dentry *dentry)
  1522. {
  1523. /* Is this a proxy for a mount namespace? */
  1524. return dentry->d_op == &ns_dentry_operations &&
  1525. dentry->d_fsdata == &mntns_operations;
  1526. }
  1527. struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
  1528. {
  1529. return container_of(ns, struct mnt_namespace, ns);
  1530. }
  1531. static bool mnt_ns_loop(struct dentry *dentry)
  1532. {
  1533. /* Could bind mounting the mount namespace inode cause a
  1534. * mount namespace loop?
  1535. */
  1536. struct mnt_namespace *mnt_ns;
  1537. if (!is_mnt_ns_file(dentry))
  1538. return false;
  1539. mnt_ns = to_mnt_ns(get_proc_ns(dentry->d_inode));
  1540. return current->nsproxy->mnt_ns->seq >= mnt_ns->seq;
  1541. }
  1542. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1543. int flag)
  1544. {
  1545. struct mount *res, *p, *q, *r, *parent;
  1546. if (!(flag & CL_COPY_UNBINDABLE) && IS_MNT_UNBINDABLE(mnt))
  1547. return ERR_PTR(-EINVAL);
  1548. if (!(flag & CL_COPY_MNT_NS_FILE) && is_mnt_ns_file(dentry))
  1549. return ERR_PTR(-EINVAL);
  1550. res = q = clone_mnt(mnt, dentry, flag);
  1551. if (IS_ERR(q))
  1552. return q;
  1553. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1554. p = mnt;
  1555. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1556. struct mount *s;
  1557. if (!is_subdir(r->mnt_mountpoint, dentry))
  1558. continue;
  1559. for (s = r; s; s = next_mnt(s, r)) {
  1560. if (!(flag & CL_COPY_UNBINDABLE) &&
  1561. IS_MNT_UNBINDABLE(s)) {
  1562. s = skip_mnt_tree(s);
  1563. continue;
  1564. }
  1565. if (!(flag & CL_COPY_MNT_NS_FILE) &&
  1566. is_mnt_ns_file(s->mnt.mnt_root)) {
  1567. s = skip_mnt_tree(s);
  1568. continue;
  1569. }
  1570. while (p != s->mnt_parent) {
  1571. p = p->mnt_parent;
  1572. q = q->mnt_parent;
  1573. }
  1574. p = s;
  1575. parent = q;
  1576. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1577. if (IS_ERR(q))
  1578. goto out;
  1579. lock_mount_hash();
  1580. list_add_tail(&q->mnt_list, &res->mnt_list);
  1581. attach_mnt(q, parent, p->mnt_mp);
  1582. unlock_mount_hash();
  1583. }
  1584. }
  1585. return res;
  1586. out:
  1587. if (res) {
  1588. lock_mount_hash();
  1589. umount_tree(res, UMOUNT_SYNC);
  1590. unlock_mount_hash();
  1591. }
  1592. return q;
  1593. }
  1594. /* Caller should check returned pointer for errors */
  1595. struct vfsmount *collect_mounts(const struct path *path)
  1596. {
  1597. struct mount *tree;
  1598. namespace_lock();
  1599. if (!check_mnt(real_mount(path->mnt)))
  1600. tree = ERR_PTR(-EINVAL);
  1601. else
  1602. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1603. CL_COPY_ALL | CL_PRIVATE);
  1604. namespace_unlock();
  1605. if (IS_ERR(tree))
  1606. return ERR_CAST(tree);
  1607. return &tree->mnt;
  1608. }
  1609. void drop_collected_mounts(struct vfsmount *mnt)
  1610. {
  1611. namespace_lock();
  1612. lock_mount_hash();
  1613. umount_tree(real_mount(mnt), UMOUNT_SYNC);
  1614. unlock_mount_hash();
  1615. namespace_unlock();
  1616. }
  1617. /**
  1618. * clone_private_mount - create a private clone of a path
  1619. *
  1620. * This creates a new vfsmount, which will be the clone of @path. The new will
  1621. * not be attached anywhere in the namespace and will be private (i.e. changes
  1622. * to the originating mount won't be propagated into this).
  1623. *
  1624. * Release with mntput().
  1625. */
  1626. struct vfsmount *clone_private_mount(const struct path *path)
  1627. {
  1628. struct mount *old_mnt = real_mount(path->mnt);
  1629. struct mount *new_mnt;
  1630. if (IS_MNT_UNBINDABLE(old_mnt))
  1631. return ERR_PTR(-EINVAL);
  1632. new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
  1633. if (IS_ERR(new_mnt))
  1634. return ERR_CAST(new_mnt);
  1635. return &new_mnt->mnt;
  1636. }
  1637. EXPORT_SYMBOL_GPL(clone_private_mount);
  1638. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1639. struct vfsmount *root)
  1640. {
  1641. struct mount *mnt;
  1642. int res = f(root, arg);
  1643. if (res)
  1644. return res;
  1645. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1646. res = f(&mnt->mnt, arg);
  1647. if (res)
  1648. return res;
  1649. }
  1650. return 0;
  1651. }
  1652. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1653. {
  1654. struct mount *p;
  1655. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1656. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1657. mnt_release_group_id(p);
  1658. }
  1659. }
  1660. static int invent_group_ids(struct mount *mnt, bool recurse)
  1661. {
  1662. struct mount *p;
  1663. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1664. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1665. int err = mnt_alloc_group_id(p);
  1666. if (err) {
  1667. cleanup_group_ids(mnt, p);
  1668. return err;
  1669. }
  1670. }
  1671. }
  1672. return 0;
  1673. }
  1674. int count_mounts(struct mnt_namespace *ns, struct mount *mnt)
  1675. {
  1676. unsigned int max = READ_ONCE(sysctl_mount_max);
  1677. unsigned int mounts = 0, old, pending, sum;
  1678. struct mount *p;
  1679. for (p = mnt; p; p = next_mnt(p, mnt))
  1680. mounts++;
  1681. old = ns->mounts;
  1682. pending = ns->pending_mounts;
  1683. sum = old + pending;
  1684. if ((old > sum) ||
  1685. (pending > sum) ||
  1686. (max < sum) ||
  1687. (mounts > (max - sum)))
  1688. return -ENOSPC;
  1689. ns->pending_mounts = pending + mounts;
  1690. return 0;
  1691. }
  1692. /*
  1693. * @source_mnt : mount tree to be attached
  1694. * @nd : place the mount tree @source_mnt is attached
  1695. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1696. * store the parent mount and mountpoint dentry.
  1697. * (done when source_mnt is moved)
  1698. *
  1699. * NOTE: in the table below explains the semantics when a source mount
  1700. * of a given type is attached to a destination mount of a given type.
  1701. * ---------------------------------------------------------------------------
  1702. * | BIND MOUNT OPERATION |
  1703. * |**************************************************************************
  1704. * | source-->| shared | private | slave | unbindable |
  1705. * | dest | | | | |
  1706. * | | | | | | |
  1707. * | v | | | | |
  1708. * |**************************************************************************
  1709. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1710. * | | | | | |
  1711. * |non-shared| shared (+) | private | slave (*) | invalid |
  1712. * ***************************************************************************
  1713. * A bind operation clones the source mount and mounts the clone on the
  1714. * destination mount.
  1715. *
  1716. * (++) the cloned mount is propagated to all the mounts in the propagation
  1717. * tree of the destination mount and the cloned mount is added to
  1718. * the peer group of the source mount.
  1719. * (+) the cloned mount is created under the destination mount and is marked
  1720. * as shared. The cloned mount is added to the peer group of the source
  1721. * mount.
  1722. * (+++) the mount is propagated to all the mounts in the propagation tree
  1723. * of the destination mount and the cloned mount is made slave
  1724. * of the same master as that of the source mount. The cloned mount
  1725. * is marked as 'shared and slave'.
  1726. * (*) the cloned mount is made a slave of the same master as that of the
  1727. * source mount.
  1728. *
  1729. * ---------------------------------------------------------------------------
  1730. * | MOVE MOUNT OPERATION |
  1731. * |**************************************************************************
  1732. * | source-->| shared | private | slave | unbindable |
  1733. * | dest | | | | |
  1734. * | | | | | | |
  1735. * | v | | | | |
  1736. * |**************************************************************************
  1737. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1738. * | | | | | |
  1739. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1740. * ***************************************************************************
  1741. *
  1742. * (+) the mount is moved to the destination. And is then propagated to
  1743. * all the mounts in the propagation tree of the destination mount.
  1744. * (+*) the mount is moved to the destination.
  1745. * (+++) the mount is moved to the destination and is then propagated to
  1746. * all the mounts belonging to the destination mount's propagation tree.
  1747. * the mount is marked as 'shared and slave'.
  1748. * (*) the mount continues to be a slave at the new location.
  1749. *
  1750. * if the source mount is a tree, the operations explained above is
  1751. * applied to each mount in the tree.
  1752. * Must be called without spinlocks held, since this function can sleep
  1753. * in allocations.
  1754. */
  1755. static int attach_recursive_mnt(struct mount *source_mnt,
  1756. struct mount *dest_mnt,
  1757. struct mountpoint *dest_mp,
  1758. struct path *parent_path)
  1759. {
  1760. HLIST_HEAD(tree_list);
  1761. struct mnt_namespace *ns = dest_mnt->mnt_ns;
  1762. struct mountpoint *smp;
  1763. struct mount *child, *p;
  1764. struct hlist_node *n;
  1765. int err;
  1766. /* Preallocate a mountpoint in case the new mounts need
  1767. * to be tucked under other mounts.
  1768. */
  1769. smp = get_mountpoint(source_mnt->mnt.mnt_root);
  1770. if (IS_ERR(smp))
  1771. return PTR_ERR(smp);
  1772. /* Is there space to add these mounts to the mount namespace? */
  1773. if (!parent_path) {
  1774. err = count_mounts(ns, source_mnt);
  1775. if (err)
  1776. goto out;
  1777. }
  1778. if (IS_MNT_SHARED(dest_mnt)) {
  1779. err = invent_group_ids(source_mnt, true);
  1780. if (err)
  1781. goto out;
  1782. err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
  1783. lock_mount_hash();
  1784. if (err)
  1785. goto out_cleanup_ids;
  1786. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1787. set_mnt_shared(p);
  1788. } else {
  1789. lock_mount_hash();
  1790. }
  1791. if (parent_path) {
  1792. detach_mnt(source_mnt, parent_path);
  1793. attach_mnt(source_mnt, dest_mnt, dest_mp);
  1794. touch_mnt_namespace(source_mnt->mnt_ns);
  1795. } else {
  1796. mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
  1797. commit_tree(source_mnt);
  1798. }
  1799. hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
  1800. struct mount *q;
  1801. hlist_del_init(&child->mnt_hash);
  1802. q = __lookup_mnt(&child->mnt_parent->mnt,
  1803. child->mnt_mountpoint);
  1804. if (q)
  1805. mnt_change_mountpoint(child, smp, q);
  1806. commit_tree(child);
  1807. }
  1808. put_mountpoint(smp);
  1809. unlock_mount_hash();
  1810. return 0;
  1811. out_cleanup_ids:
  1812. while (!hlist_empty(&tree_list)) {
  1813. child = hlist_entry(tree_list.first, struct mount, mnt_hash);
  1814. child->mnt_parent->mnt_ns->pending_mounts = 0;
  1815. umount_tree(child, UMOUNT_SYNC);
  1816. }
  1817. unlock_mount_hash();
  1818. cleanup_group_ids(source_mnt, NULL);
  1819. out:
  1820. ns->pending_mounts = 0;
  1821. read_seqlock_excl(&mount_lock);
  1822. put_mountpoint(smp);
  1823. read_sequnlock_excl(&mount_lock);
  1824. return err;
  1825. }
  1826. static struct mountpoint *lock_mount(struct path *path)
  1827. {
  1828. struct vfsmount *mnt;
  1829. struct dentry *dentry = path->dentry;
  1830. retry:
  1831. inode_lock(dentry->d_inode);
  1832. if (unlikely(cant_mount(dentry))) {
  1833. inode_unlock(dentry->d_inode);
  1834. return ERR_PTR(-ENOENT);
  1835. }
  1836. namespace_lock();
  1837. mnt = lookup_mnt(path);
  1838. if (likely(!mnt)) {
  1839. struct mountpoint *mp = get_mountpoint(dentry);
  1840. if (IS_ERR(mp)) {
  1841. namespace_unlock();
  1842. inode_unlock(dentry->d_inode);
  1843. return mp;
  1844. }
  1845. return mp;
  1846. }
  1847. namespace_unlock();
  1848. inode_unlock(path->dentry->d_inode);
  1849. path_put(path);
  1850. path->mnt = mnt;
  1851. dentry = path->dentry = dget(mnt->mnt_root);
  1852. goto retry;
  1853. }
  1854. static void unlock_mount(struct mountpoint *where)
  1855. {
  1856. struct dentry *dentry = where->m_dentry;
  1857. read_seqlock_excl(&mount_lock);
  1858. put_mountpoint(where);
  1859. read_sequnlock_excl(&mount_lock);
  1860. namespace_unlock();
  1861. inode_unlock(dentry->d_inode);
  1862. }
  1863. static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
  1864. {
  1865. if (mnt->mnt.mnt_sb->s_flags & SB_NOUSER)
  1866. return -EINVAL;
  1867. if (d_is_dir(mp->m_dentry) !=
  1868. d_is_dir(mnt->mnt.mnt_root))
  1869. return -ENOTDIR;
  1870. return attach_recursive_mnt(mnt, p, mp, NULL);
  1871. }
  1872. /*
  1873. * Sanity check the flags to change_mnt_propagation.
  1874. */
  1875. static int flags_to_propagation_type(int ms_flags)
  1876. {
  1877. int type = ms_flags & ~(MS_REC | MS_SILENT);
  1878. /* Fail if any non-propagation flags are set */
  1879. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1880. return 0;
  1881. /* Only one propagation flag should be set */
  1882. if (!is_power_of_2(type))
  1883. return 0;
  1884. return type;
  1885. }
  1886. /*
  1887. * recursively change the type of the mountpoint.
  1888. */
  1889. static int do_change_type(struct path *path, int ms_flags)
  1890. {
  1891. struct mount *m;
  1892. struct mount *mnt = real_mount(path->mnt);
  1893. int recurse = ms_flags & MS_REC;
  1894. int type;
  1895. int err = 0;
  1896. if (path->dentry != path->mnt->mnt_root)
  1897. return -EINVAL;
  1898. type = flags_to_propagation_type(ms_flags);
  1899. if (!type)
  1900. return -EINVAL;
  1901. namespace_lock();
  1902. if (type == MS_SHARED) {
  1903. err = invent_group_ids(mnt, recurse);
  1904. if (err)
  1905. goto out_unlock;
  1906. }
  1907. lock_mount_hash();
  1908. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1909. change_mnt_propagation(m, type);
  1910. unlock_mount_hash();
  1911. out_unlock:
  1912. namespace_unlock();
  1913. return err;
  1914. }
  1915. static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
  1916. {
  1917. struct mount *child;
  1918. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  1919. if (!is_subdir(child->mnt_mountpoint, dentry))
  1920. continue;
  1921. if (child->mnt.mnt_flags & MNT_LOCKED)
  1922. return true;
  1923. }
  1924. return false;
  1925. }
  1926. /*
  1927. * do loopback mount.
  1928. */
  1929. static int do_loopback(struct path *path, const char *old_name,
  1930. int recurse)
  1931. {
  1932. struct path old_path;
  1933. struct mount *mnt = NULL, *old, *parent;
  1934. struct mountpoint *mp;
  1935. int err;
  1936. if (!old_name || !*old_name)
  1937. return -EINVAL;
  1938. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1939. if (err)
  1940. return err;
  1941. err = -EINVAL;
  1942. if (mnt_ns_loop(old_path.dentry))
  1943. goto out;
  1944. mp = lock_mount(path);
  1945. err = PTR_ERR(mp);
  1946. if (IS_ERR(mp))
  1947. goto out;
  1948. old = real_mount(old_path.mnt);
  1949. parent = real_mount(path->mnt);
  1950. err = -EINVAL;
  1951. if (IS_MNT_UNBINDABLE(old))
  1952. goto out2;
  1953. if (!check_mnt(parent))
  1954. goto out2;
  1955. if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations)
  1956. goto out2;
  1957. if (!recurse && has_locked_children(old, old_path.dentry))
  1958. goto out2;
  1959. if (recurse)
  1960. mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE);
  1961. else
  1962. mnt = clone_mnt(old, old_path.dentry, 0);
  1963. if (IS_ERR(mnt)) {
  1964. err = PTR_ERR(mnt);
  1965. goto out2;
  1966. }
  1967. mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  1968. err = graft_tree(mnt, parent, mp);
  1969. if (err) {
  1970. lock_mount_hash();
  1971. umount_tree(mnt, UMOUNT_SYNC);
  1972. unlock_mount_hash();
  1973. }
  1974. out2:
  1975. unlock_mount(mp);
  1976. out:
  1977. path_put(&old_path);
  1978. return err;
  1979. }
  1980. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1981. {
  1982. int error = 0;
  1983. int readonly_request = 0;
  1984. if (ms_flags & MS_RDONLY)
  1985. readonly_request = 1;
  1986. if (readonly_request == __mnt_is_readonly(mnt))
  1987. return 0;
  1988. if (readonly_request)
  1989. error = mnt_make_readonly(real_mount(mnt));
  1990. else
  1991. __mnt_unmake_readonly(real_mount(mnt));
  1992. return error;
  1993. }
  1994. /*
  1995. * change filesystem flags. dir should be a physical root of filesystem.
  1996. * If you've mounted a non-root directory somewhere and want to do remount
  1997. * on it - tough luck.
  1998. */
  1999. static int do_remount(struct path *path, int ms_flags, int sb_flags,
  2000. int mnt_flags, void *data)
  2001. {
  2002. int err;
  2003. struct super_block *sb = path->mnt->mnt_sb;
  2004. struct mount *mnt = real_mount(path->mnt);
  2005. if (!check_mnt(mnt))
  2006. return -EINVAL;
  2007. if (path->dentry != path->mnt->mnt_root)
  2008. return -EINVAL;
  2009. /* Don't allow changing of locked mnt flags.
  2010. *
  2011. * No locks need to be held here while testing the various
  2012. * MNT_LOCK flags because those flags can never be cleared
  2013. * once they are set.
  2014. */
  2015. if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
  2016. !(mnt_flags & MNT_READONLY)) {
  2017. return -EPERM;
  2018. }
  2019. if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
  2020. !(mnt_flags & MNT_NODEV)) {
  2021. return -EPERM;
  2022. }
  2023. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
  2024. !(mnt_flags & MNT_NOSUID)) {
  2025. return -EPERM;
  2026. }
  2027. if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
  2028. !(mnt_flags & MNT_NOEXEC)) {
  2029. return -EPERM;
  2030. }
  2031. if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
  2032. ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
  2033. return -EPERM;
  2034. }
  2035. err = security_sb_remount(sb, data);
  2036. if (err)
  2037. return err;
  2038. down_write(&sb->s_umount);
  2039. if (ms_flags & MS_BIND)
  2040. err = change_mount_flags(path->mnt, ms_flags);
  2041. else if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
  2042. err = -EPERM;
  2043. else
  2044. err = do_remount_sb(sb, sb_flags, data, 0);
  2045. if (!err) {
  2046. lock_mount_hash();
  2047. mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
  2048. mnt->mnt.mnt_flags = mnt_flags;
  2049. touch_mnt_namespace(mnt->mnt_ns);
  2050. unlock_mount_hash();
  2051. }
  2052. up_write(&sb->s_umount);
  2053. return err;
  2054. }
  2055. static inline int tree_contains_unbindable(struct mount *mnt)
  2056. {
  2057. struct mount *p;
  2058. for (p = mnt; p; p = next_mnt(p, mnt)) {
  2059. if (IS_MNT_UNBINDABLE(p))
  2060. return 1;
  2061. }
  2062. return 0;
  2063. }
  2064. static int do_move_mount(struct path *path, const char *old_name)
  2065. {
  2066. struct path old_path, parent_path;
  2067. struct mount *p;
  2068. struct mount *old;
  2069. struct mountpoint *mp;
  2070. int err;
  2071. if (!old_name || !*old_name)
  2072. return -EINVAL;
  2073. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  2074. if (err)
  2075. return err;
  2076. mp = lock_mount(path);
  2077. err = PTR_ERR(mp);
  2078. if (IS_ERR(mp))
  2079. goto out;
  2080. old = real_mount(old_path.mnt);
  2081. p = real_mount(path->mnt);
  2082. err = -EINVAL;
  2083. if (!check_mnt(p) || !check_mnt(old))
  2084. goto out1;
  2085. if (old->mnt.mnt_flags & MNT_LOCKED)
  2086. goto out1;
  2087. err = -EINVAL;
  2088. if (old_path.dentry != old_path.mnt->mnt_root)
  2089. goto out1;
  2090. if (!mnt_has_parent(old))
  2091. goto out1;
  2092. if (d_is_dir(path->dentry) !=
  2093. d_is_dir(old_path.dentry))
  2094. goto out1;
  2095. /*
  2096. * Don't move a mount residing in a shared parent.
  2097. */
  2098. if (IS_MNT_SHARED(old->mnt_parent))
  2099. goto out1;
  2100. /*
  2101. * Don't move a mount tree containing unbindable mounts to a destination
  2102. * mount which is shared.
  2103. */
  2104. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  2105. goto out1;
  2106. err = -ELOOP;
  2107. for (; mnt_has_parent(p); p = p->mnt_parent)
  2108. if (p == old)
  2109. goto out1;
  2110. err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
  2111. if (err)
  2112. goto out1;
  2113. /* if the mount is moved, it should no longer be expire
  2114. * automatically */
  2115. list_del_init(&old->mnt_expire);
  2116. out1:
  2117. unlock_mount(mp);
  2118. out:
  2119. if (!err)
  2120. path_put(&parent_path);
  2121. path_put(&old_path);
  2122. return err;
  2123. }
  2124. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  2125. {
  2126. int err;
  2127. const char *subtype = strchr(fstype, '.');
  2128. if (subtype) {
  2129. subtype++;
  2130. err = -EINVAL;
  2131. if (!subtype[0])
  2132. goto err;
  2133. } else
  2134. subtype = "";
  2135. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  2136. err = -ENOMEM;
  2137. if (!mnt->mnt_sb->s_subtype)
  2138. goto err;
  2139. return mnt;
  2140. err:
  2141. mntput(mnt);
  2142. return ERR_PTR(err);
  2143. }
  2144. /*
  2145. * add a mount into a namespace's mount tree
  2146. */
  2147. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  2148. {
  2149. struct mountpoint *mp;
  2150. struct mount *parent;
  2151. int err;
  2152. mnt_flags &= ~MNT_INTERNAL_FLAGS;
  2153. mp = lock_mount(path);
  2154. if (IS_ERR(mp))
  2155. return PTR_ERR(mp);
  2156. parent = real_mount(path->mnt);
  2157. err = -EINVAL;
  2158. if (unlikely(!check_mnt(parent))) {
  2159. /* that's acceptable only for automounts done in private ns */
  2160. if (!(mnt_flags & MNT_SHRINKABLE))
  2161. goto unlock;
  2162. /* ... and for those we'd better have mountpoint still alive */
  2163. if (!parent->mnt_ns)
  2164. goto unlock;
  2165. }
  2166. /* Refuse the same filesystem on the same mount point */
  2167. err = -EBUSY;
  2168. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  2169. path->mnt->mnt_root == path->dentry)
  2170. goto unlock;
  2171. err = -EINVAL;
  2172. if (d_is_symlink(newmnt->mnt.mnt_root))
  2173. goto unlock;
  2174. newmnt->mnt.mnt_flags = mnt_flags;
  2175. err = graft_tree(newmnt, parent, mp);
  2176. unlock:
  2177. unlock_mount(mp);
  2178. return err;
  2179. }
  2180. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags);
  2181. /*
  2182. * create a new mount for userspace and request it to be added into the
  2183. * namespace's tree
  2184. */
  2185. static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
  2186. int mnt_flags, const char *name, void *data)
  2187. {
  2188. struct file_system_type *type;
  2189. struct vfsmount *mnt;
  2190. int err;
  2191. if (!fstype)
  2192. return -EINVAL;
  2193. type = get_fs_type(fstype);
  2194. if (!type)
  2195. return -ENODEV;
  2196. mnt = vfs_kern_mount(type, sb_flags, name, data);
  2197. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  2198. !mnt->mnt_sb->s_subtype)
  2199. mnt = fs_set_subtype(mnt, fstype);
  2200. put_filesystem(type);
  2201. if (IS_ERR(mnt))
  2202. return PTR_ERR(mnt);
  2203. if (mount_too_revealing(mnt, &mnt_flags)) {
  2204. mntput(mnt);
  2205. return -EPERM;
  2206. }
  2207. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  2208. if (err)
  2209. mntput(mnt);
  2210. return err;
  2211. }
  2212. int finish_automount(struct vfsmount *m, struct path *path)
  2213. {
  2214. struct mount *mnt = real_mount(m);
  2215. int err;
  2216. /* The new mount record should have at least 2 refs to prevent it being
  2217. * expired before we get a chance to add it
  2218. */
  2219. BUG_ON(mnt_get_count(mnt) < 2);
  2220. if (m->mnt_sb == path->mnt->mnt_sb &&
  2221. m->mnt_root == path->dentry) {
  2222. err = -ELOOP;
  2223. goto fail;
  2224. }
  2225. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  2226. if (!err)
  2227. return 0;
  2228. fail:
  2229. /* remove m from any expiration list it may be on */
  2230. if (!list_empty(&mnt->mnt_expire)) {
  2231. namespace_lock();
  2232. list_del_init(&mnt->mnt_expire);
  2233. namespace_unlock();
  2234. }
  2235. mntput(m);
  2236. mntput(m);
  2237. return err;
  2238. }
  2239. /**
  2240. * mnt_set_expiry - Put a mount on an expiration list
  2241. * @mnt: The mount to list.
  2242. * @expiry_list: The list to add the mount to.
  2243. */
  2244. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  2245. {
  2246. namespace_lock();
  2247. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  2248. namespace_unlock();
  2249. }
  2250. EXPORT_SYMBOL(mnt_set_expiry);
  2251. /*
  2252. * process a list of expirable mountpoints with the intent of discarding any
  2253. * mountpoints that aren't in use and haven't been touched since last we came
  2254. * here
  2255. */
  2256. void mark_mounts_for_expiry(struct list_head *mounts)
  2257. {
  2258. struct mount *mnt, *next;
  2259. LIST_HEAD(graveyard);
  2260. if (list_empty(mounts))
  2261. return;
  2262. namespace_lock();
  2263. lock_mount_hash();
  2264. /* extract from the expiration list every vfsmount that matches the
  2265. * following criteria:
  2266. * - only referenced by its parent vfsmount
  2267. * - still marked for expiry (marked on the last call here; marks are
  2268. * cleared by mntput())
  2269. */
  2270. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  2271. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  2272. propagate_mount_busy(mnt, 1))
  2273. continue;
  2274. list_move(&mnt->mnt_expire, &graveyard);
  2275. }
  2276. while (!list_empty(&graveyard)) {
  2277. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  2278. touch_mnt_namespace(mnt->mnt_ns);
  2279. umount_tree(mnt, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2280. }
  2281. unlock_mount_hash();
  2282. namespace_unlock();
  2283. }
  2284. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  2285. /*
  2286. * Ripoff of 'select_parent()'
  2287. *
  2288. * search the list of submounts for a given mountpoint, and move any
  2289. * shrinkable submounts to the 'graveyard' list.
  2290. */
  2291. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  2292. {
  2293. struct mount *this_parent = parent;
  2294. struct list_head *next;
  2295. int found = 0;
  2296. repeat:
  2297. next = this_parent->mnt_mounts.next;
  2298. resume:
  2299. while (next != &this_parent->mnt_mounts) {
  2300. struct list_head *tmp = next;
  2301. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  2302. next = tmp->next;
  2303. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  2304. continue;
  2305. /*
  2306. * Descend a level if the d_mounts list is non-empty.
  2307. */
  2308. if (!list_empty(&mnt->mnt_mounts)) {
  2309. this_parent = mnt;
  2310. goto repeat;
  2311. }
  2312. if (!propagate_mount_busy(mnt, 1)) {
  2313. list_move_tail(&mnt->mnt_expire, graveyard);
  2314. found++;
  2315. }
  2316. }
  2317. /*
  2318. * All done at this level ... ascend and resume the search
  2319. */
  2320. if (this_parent != parent) {
  2321. next = this_parent->mnt_child.next;
  2322. this_parent = this_parent->mnt_parent;
  2323. goto resume;
  2324. }
  2325. return found;
  2326. }
  2327. /*
  2328. * process a list of expirable mountpoints with the intent of discarding any
  2329. * submounts of a specific parent mountpoint
  2330. *
  2331. * mount_lock must be held for write
  2332. */
  2333. static void shrink_submounts(struct mount *mnt)
  2334. {
  2335. LIST_HEAD(graveyard);
  2336. struct mount *m;
  2337. /* extract submounts of 'mountpoint' from the expiration list */
  2338. while (select_submounts(mnt, &graveyard)) {
  2339. while (!list_empty(&graveyard)) {
  2340. m = list_first_entry(&graveyard, struct mount,
  2341. mnt_expire);
  2342. touch_mnt_namespace(m->mnt_ns);
  2343. umount_tree(m, UMOUNT_PROPAGATE|UMOUNT_SYNC);
  2344. }
  2345. }
  2346. }
  2347. /*
  2348. * Some copy_from_user() implementations do not return the exact number of
  2349. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  2350. * Note that this function differs from copy_from_user() in that it will oops
  2351. * on bad values of `to', rather than returning a short copy.
  2352. */
  2353. static long exact_copy_from_user(void *to, const void __user * from,
  2354. unsigned long n)
  2355. {
  2356. char *t = to;
  2357. const char __user *f = from;
  2358. char c;
  2359. if (!access_ok(VERIFY_READ, from, n))
  2360. return n;
  2361. while (n) {
  2362. if (__get_user(c, f)) {
  2363. memset(t, 0, n);
  2364. break;
  2365. }
  2366. *t++ = c;
  2367. f++;
  2368. n--;
  2369. }
  2370. return n;
  2371. }
  2372. void *copy_mount_options(const void __user * data)
  2373. {
  2374. int i;
  2375. unsigned long size;
  2376. char *copy;
  2377. if (!data)
  2378. return NULL;
  2379. copy = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2380. if (!copy)
  2381. return ERR_PTR(-ENOMEM);
  2382. /* We only care that *some* data at the address the user
  2383. * gave us is valid. Just in case, we'll zero
  2384. * the remainder of the page.
  2385. */
  2386. /* copy_from_user cannot cross TASK_SIZE ! */
  2387. size = TASK_SIZE - (unsigned long)data;
  2388. if (size > PAGE_SIZE)
  2389. size = PAGE_SIZE;
  2390. i = size - exact_copy_from_user(copy, data, size);
  2391. if (!i) {
  2392. kfree(copy);
  2393. return ERR_PTR(-EFAULT);
  2394. }
  2395. if (i != PAGE_SIZE)
  2396. memset(copy + i, 0, PAGE_SIZE - i);
  2397. return copy;
  2398. }
  2399. char *copy_mount_string(const void __user *data)
  2400. {
  2401. return data ? strndup_user(data, PAGE_SIZE) : NULL;
  2402. }
  2403. /*
  2404. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  2405. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  2406. *
  2407. * data is a (void *) that can point to any structure up to
  2408. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  2409. * information (or be NULL).
  2410. *
  2411. * Pre-0.97 versions of mount() didn't have a flags word.
  2412. * When the flags word was introduced its top half was required
  2413. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  2414. * Therefore, if this magic number is present, it carries no information
  2415. * and must be discarded.
  2416. */
  2417. long do_mount(const char *dev_name, const char __user *dir_name,
  2418. const char *type_page, unsigned long flags, void *data_page)
  2419. {
  2420. struct path path;
  2421. unsigned int mnt_flags = 0, sb_flags;
  2422. int retval = 0;
  2423. /* Discard magic */
  2424. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  2425. flags &= ~MS_MGC_MSK;
  2426. /* Basic sanity checks */
  2427. if (data_page)
  2428. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  2429. if (flags & MS_NOUSER)
  2430. return -EINVAL;
  2431. /* ... and get the mountpoint */
  2432. retval = user_path(dir_name, &path);
  2433. if (retval)
  2434. return retval;
  2435. retval = security_sb_mount(dev_name, &path,
  2436. type_page, flags, data_page);
  2437. if (!retval && !may_mount())
  2438. retval = -EPERM;
  2439. if (!retval && (flags & SB_MANDLOCK) && !may_mandlock())
  2440. retval = -EPERM;
  2441. if (retval)
  2442. goto dput_out;
  2443. /* Default to relatime unless overriden */
  2444. if (!(flags & MS_NOATIME))
  2445. mnt_flags |= MNT_RELATIME;
  2446. /* Separate the per-mountpoint flags */
  2447. if (flags & MS_NOSUID)
  2448. mnt_flags |= MNT_NOSUID;
  2449. if (flags & MS_NODEV)
  2450. mnt_flags |= MNT_NODEV;
  2451. if (flags & MS_NOEXEC)
  2452. mnt_flags |= MNT_NOEXEC;
  2453. if (flags & MS_NOATIME)
  2454. mnt_flags |= MNT_NOATIME;
  2455. if (flags & MS_NODIRATIME)
  2456. mnt_flags |= MNT_NODIRATIME;
  2457. if (flags & MS_STRICTATIME)
  2458. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  2459. if (flags & MS_RDONLY)
  2460. mnt_flags |= MNT_READONLY;
  2461. /* The default atime for remount is preservation */
  2462. if ((flags & MS_REMOUNT) &&
  2463. ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
  2464. MS_STRICTATIME)) == 0)) {
  2465. mnt_flags &= ~MNT_ATIME_MASK;
  2466. mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
  2467. }
  2468. sb_flags = flags & (SB_RDONLY |
  2469. SB_SYNCHRONOUS |
  2470. SB_MANDLOCK |
  2471. SB_DIRSYNC |
  2472. SB_SILENT |
  2473. SB_POSIXACL |
  2474. SB_LAZYTIME |
  2475. SB_I_VERSION);
  2476. if (flags & MS_REMOUNT)
  2477. retval = do_remount(&path, flags, sb_flags, mnt_flags,
  2478. data_page);
  2479. else if (flags & MS_BIND)
  2480. retval = do_loopback(&path, dev_name, flags & MS_REC);
  2481. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  2482. retval = do_change_type(&path, flags);
  2483. else if (flags & MS_MOVE)
  2484. retval = do_move_mount(&path, dev_name);
  2485. else
  2486. retval = do_new_mount(&path, type_page, sb_flags, mnt_flags,
  2487. dev_name, data_page);
  2488. dput_out:
  2489. path_put(&path);
  2490. return retval;
  2491. }
  2492. static struct ucounts *inc_mnt_namespaces(struct user_namespace *ns)
  2493. {
  2494. return inc_ucount(ns, current_euid(), UCOUNT_MNT_NAMESPACES);
  2495. }
  2496. static void dec_mnt_namespaces(struct ucounts *ucounts)
  2497. {
  2498. dec_ucount(ucounts, UCOUNT_MNT_NAMESPACES);
  2499. }
  2500. static void free_mnt_ns(struct mnt_namespace *ns)
  2501. {
  2502. ns_free_inum(&ns->ns);
  2503. dec_mnt_namespaces(ns->ucounts);
  2504. put_user_ns(ns->user_ns);
  2505. kfree(ns);
  2506. }
  2507. /*
  2508. * Assign a sequence number so we can detect when we attempt to bind
  2509. * mount a reference to an older mount namespace into the current
  2510. * mount namespace, preventing reference counting loops. A 64bit
  2511. * number incrementing at 10Ghz will take 12,427 years to wrap which
  2512. * is effectively never, so we can ignore the possibility.
  2513. */
  2514. static atomic64_t mnt_ns_seq = ATOMIC64_INIT(1);
  2515. static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns)
  2516. {
  2517. struct mnt_namespace *new_ns;
  2518. struct ucounts *ucounts;
  2519. int ret;
  2520. ucounts = inc_mnt_namespaces(user_ns);
  2521. if (!ucounts)
  2522. return ERR_PTR(-ENOSPC);
  2523. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  2524. if (!new_ns) {
  2525. dec_mnt_namespaces(ucounts);
  2526. return ERR_PTR(-ENOMEM);
  2527. }
  2528. ret = ns_alloc_inum(&new_ns->ns);
  2529. if (ret) {
  2530. kfree(new_ns);
  2531. dec_mnt_namespaces(ucounts);
  2532. return ERR_PTR(ret);
  2533. }
  2534. new_ns->ns.ops = &mntns_operations;
  2535. new_ns->seq = atomic64_add_return(1, &mnt_ns_seq);
  2536. atomic_set(&new_ns->count, 1);
  2537. new_ns->root = NULL;
  2538. INIT_LIST_HEAD(&new_ns->list);
  2539. init_waitqueue_head(&new_ns->poll);
  2540. new_ns->event = 0;
  2541. new_ns->user_ns = get_user_ns(user_ns);
  2542. new_ns->ucounts = ucounts;
  2543. new_ns->mounts = 0;
  2544. new_ns->pending_mounts = 0;
  2545. return new_ns;
  2546. }
  2547. __latent_entropy
  2548. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2549. struct user_namespace *user_ns, struct fs_struct *new_fs)
  2550. {
  2551. struct mnt_namespace *new_ns;
  2552. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  2553. struct mount *p, *q;
  2554. struct mount *old;
  2555. struct mount *new;
  2556. int copy_flags;
  2557. BUG_ON(!ns);
  2558. if (likely(!(flags & CLONE_NEWNS))) {
  2559. get_mnt_ns(ns);
  2560. return ns;
  2561. }
  2562. old = ns->root;
  2563. new_ns = alloc_mnt_ns(user_ns);
  2564. if (IS_ERR(new_ns))
  2565. return new_ns;
  2566. namespace_lock();
  2567. /* First pass: copy the tree topology */
  2568. copy_flags = CL_COPY_UNBINDABLE | CL_EXPIRE;
  2569. if (user_ns != ns->user_ns)
  2570. copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
  2571. new = copy_tree(old, old->mnt.mnt_root, copy_flags);
  2572. if (IS_ERR(new)) {
  2573. namespace_unlock();
  2574. free_mnt_ns(new_ns);
  2575. return ERR_CAST(new);
  2576. }
  2577. new_ns->root = new;
  2578. list_add_tail(&new_ns->list, &new->mnt_list);
  2579. /*
  2580. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  2581. * as belonging to new namespace. We have already acquired a private
  2582. * fs_struct, so tsk->fs->lock is not needed.
  2583. */
  2584. p = old;
  2585. q = new;
  2586. while (p) {
  2587. q->mnt_ns = new_ns;
  2588. new_ns->mounts++;
  2589. if (new_fs) {
  2590. if (&p->mnt == new_fs->root.mnt) {
  2591. new_fs->root.mnt = mntget(&q->mnt);
  2592. rootmnt = &p->mnt;
  2593. }
  2594. if (&p->mnt == new_fs->pwd.mnt) {
  2595. new_fs->pwd.mnt = mntget(&q->mnt);
  2596. pwdmnt = &p->mnt;
  2597. }
  2598. }
  2599. p = next_mnt(p, old);
  2600. q = next_mnt(q, new);
  2601. if (!q)
  2602. break;
  2603. while (p->mnt.mnt_root != q->mnt.mnt_root)
  2604. p = next_mnt(p, old);
  2605. }
  2606. namespace_unlock();
  2607. if (rootmnt)
  2608. mntput(rootmnt);
  2609. if (pwdmnt)
  2610. mntput(pwdmnt);
  2611. return new_ns;
  2612. }
  2613. /**
  2614. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2615. * @mnt: pointer to the new root filesystem mountpoint
  2616. */
  2617. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2618. {
  2619. struct mnt_namespace *new_ns = alloc_mnt_ns(&init_user_ns);
  2620. if (!IS_ERR(new_ns)) {
  2621. struct mount *mnt = real_mount(m);
  2622. mnt->mnt_ns = new_ns;
  2623. new_ns->root = mnt;
  2624. new_ns->mounts++;
  2625. list_add(&mnt->mnt_list, &new_ns->list);
  2626. } else {
  2627. mntput(m);
  2628. }
  2629. return new_ns;
  2630. }
  2631. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2632. {
  2633. struct mnt_namespace *ns;
  2634. struct super_block *s;
  2635. struct path path;
  2636. int err;
  2637. ns = create_mnt_ns(mnt);
  2638. if (IS_ERR(ns))
  2639. return ERR_CAST(ns);
  2640. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2641. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2642. put_mnt_ns(ns);
  2643. if (err)
  2644. return ERR_PTR(err);
  2645. /* trade a vfsmount reference for active sb one */
  2646. s = path.mnt->mnt_sb;
  2647. atomic_inc(&s->s_active);
  2648. mntput(path.mnt);
  2649. /* lock the sucker */
  2650. down_write(&s->s_umount);
  2651. /* ... and return the root of (sub)tree on it */
  2652. return path.dentry;
  2653. }
  2654. EXPORT_SYMBOL(mount_subtree);
  2655. int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
  2656. unsigned long flags, void __user *data)
  2657. {
  2658. int ret;
  2659. char *kernel_type;
  2660. char *kernel_dev;
  2661. void *options;
  2662. kernel_type = copy_mount_string(type);
  2663. ret = PTR_ERR(kernel_type);
  2664. if (IS_ERR(kernel_type))
  2665. goto out_type;
  2666. kernel_dev = copy_mount_string(dev_name);
  2667. ret = PTR_ERR(kernel_dev);
  2668. if (IS_ERR(kernel_dev))
  2669. goto out_dev;
  2670. options = copy_mount_options(data);
  2671. ret = PTR_ERR(options);
  2672. if (IS_ERR(options))
  2673. goto out_data;
  2674. ret = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
  2675. kfree(options);
  2676. out_data:
  2677. kfree(kernel_dev);
  2678. out_dev:
  2679. kfree(kernel_type);
  2680. out_type:
  2681. return ret;
  2682. }
  2683. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2684. char __user *, type, unsigned long, flags, void __user *, data)
  2685. {
  2686. return ksys_mount(dev_name, dir_name, type, flags, data);
  2687. }
  2688. /*
  2689. * Return true if path is reachable from root
  2690. *
  2691. * namespace_sem or mount_lock is held
  2692. */
  2693. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2694. const struct path *root)
  2695. {
  2696. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2697. dentry = mnt->mnt_mountpoint;
  2698. mnt = mnt->mnt_parent;
  2699. }
  2700. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2701. }
  2702. bool path_is_under(const struct path *path1, const struct path *path2)
  2703. {
  2704. bool res;
  2705. read_seqlock_excl(&mount_lock);
  2706. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2707. read_sequnlock_excl(&mount_lock);
  2708. return res;
  2709. }
  2710. EXPORT_SYMBOL(path_is_under);
  2711. /*
  2712. * pivot_root Semantics:
  2713. * Moves the root file system of the current process to the directory put_old,
  2714. * makes new_root as the new root file system of the current process, and sets
  2715. * root/cwd of all processes which had them on the current root to new_root.
  2716. *
  2717. * Restrictions:
  2718. * The new_root and put_old must be directories, and must not be on the
  2719. * same file system as the current process root. The put_old must be
  2720. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2721. * pointed to by put_old must yield the same directory as new_root. No other
  2722. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2723. *
  2724. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2725. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2726. * in this situation.
  2727. *
  2728. * Notes:
  2729. * - we don't move root/cwd if they are not at the root (reason: if something
  2730. * cared enough to change them, it's probably wrong to force them elsewhere)
  2731. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2732. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2733. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2734. * first.
  2735. */
  2736. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2737. const char __user *, put_old)
  2738. {
  2739. struct path new, old, parent_path, root_parent, root;
  2740. struct mount *new_mnt, *root_mnt, *old_mnt;
  2741. struct mountpoint *old_mp, *root_mp;
  2742. int error;
  2743. if (!may_mount())
  2744. return -EPERM;
  2745. error = user_path_dir(new_root, &new);
  2746. if (error)
  2747. goto out0;
  2748. error = user_path_dir(put_old, &old);
  2749. if (error)
  2750. goto out1;
  2751. error = security_sb_pivotroot(&old, &new);
  2752. if (error)
  2753. goto out2;
  2754. get_fs_root(current->fs, &root);
  2755. old_mp = lock_mount(&old);
  2756. error = PTR_ERR(old_mp);
  2757. if (IS_ERR(old_mp))
  2758. goto out3;
  2759. error = -EINVAL;
  2760. new_mnt = real_mount(new.mnt);
  2761. root_mnt = real_mount(root.mnt);
  2762. old_mnt = real_mount(old.mnt);
  2763. if (IS_MNT_SHARED(old_mnt) ||
  2764. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2765. IS_MNT_SHARED(root_mnt->mnt_parent))
  2766. goto out4;
  2767. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2768. goto out4;
  2769. if (new_mnt->mnt.mnt_flags & MNT_LOCKED)
  2770. goto out4;
  2771. error = -ENOENT;
  2772. if (d_unlinked(new.dentry))
  2773. goto out4;
  2774. error = -EBUSY;
  2775. if (new_mnt == root_mnt || old_mnt == root_mnt)
  2776. goto out4; /* loop, on the same file system */
  2777. error = -EINVAL;
  2778. if (root.mnt->mnt_root != root.dentry)
  2779. goto out4; /* not a mountpoint */
  2780. if (!mnt_has_parent(root_mnt))
  2781. goto out4; /* not attached */
  2782. root_mp = root_mnt->mnt_mp;
  2783. if (new.mnt->mnt_root != new.dentry)
  2784. goto out4; /* not a mountpoint */
  2785. if (!mnt_has_parent(new_mnt))
  2786. goto out4; /* not attached */
  2787. /* make sure we can reach put_old from new_root */
  2788. if (!is_path_reachable(old_mnt, old.dentry, &new))
  2789. goto out4;
  2790. /* make certain new is below the root */
  2791. if (!is_path_reachable(new_mnt, new.dentry, &root))
  2792. goto out4;
  2793. root_mp->m_count++; /* pin it so it won't go away */
  2794. lock_mount_hash();
  2795. detach_mnt(new_mnt, &parent_path);
  2796. detach_mnt(root_mnt, &root_parent);
  2797. if (root_mnt->mnt.mnt_flags & MNT_LOCKED) {
  2798. new_mnt->mnt.mnt_flags |= MNT_LOCKED;
  2799. root_mnt->mnt.mnt_flags &= ~MNT_LOCKED;
  2800. }
  2801. /* mount old root on put_old */
  2802. attach_mnt(root_mnt, old_mnt, old_mp);
  2803. /* mount new_root on / */
  2804. attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
  2805. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2806. /* A moved mount should not expire automatically */
  2807. list_del_init(&new_mnt->mnt_expire);
  2808. put_mountpoint(root_mp);
  2809. unlock_mount_hash();
  2810. chroot_fs_refs(&root, &new);
  2811. error = 0;
  2812. out4:
  2813. unlock_mount(old_mp);
  2814. if (!error) {
  2815. path_put(&root_parent);
  2816. path_put(&parent_path);
  2817. }
  2818. out3:
  2819. path_put(&root);
  2820. out2:
  2821. path_put(&old);
  2822. out1:
  2823. path_put(&new);
  2824. out0:
  2825. return error;
  2826. }
  2827. static void __init init_mount_tree(void)
  2828. {
  2829. struct vfsmount *mnt;
  2830. struct mnt_namespace *ns;
  2831. struct path root;
  2832. struct file_system_type *type;
  2833. type = get_fs_type("rootfs");
  2834. if (!type)
  2835. panic("Can't find rootfs type");
  2836. mnt = vfs_kern_mount(type, 0, "rootfs", NULL);
  2837. put_filesystem(type);
  2838. if (IS_ERR(mnt))
  2839. panic("Can't create rootfs");
  2840. ns = create_mnt_ns(mnt);
  2841. if (IS_ERR(ns))
  2842. panic("Can't allocate initial namespace");
  2843. init_task.nsproxy->mnt_ns = ns;
  2844. get_mnt_ns(ns);
  2845. root.mnt = mnt;
  2846. root.dentry = mnt->mnt_root;
  2847. mnt->mnt_flags |= MNT_LOCKED;
  2848. set_fs_pwd(current->fs, &root);
  2849. set_fs_root(current->fs, &root);
  2850. }
  2851. void __init mnt_init(void)
  2852. {
  2853. int err;
  2854. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2855. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2856. mount_hashtable = alloc_large_system_hash("Mount-cache",
  2857. sizeof(struct hlist_head),
  2858. mhash_entries, 19,
  2859. HASH_ZERO,
  2860. &m_hash_shift, &m_hash_mask, 0, 0);
  2861. mountpoint_hashtable = alloc_large_system_hash("Mountpoint-cache",
  2862. sizeof(struct hlist_head),
  2863. mphash_entries, 19,
  2864. HASH_ZERO,
  2865. &mp_hash_shift, &mp_hash_mask, 0, 0);
  2866. if (!mount_hashtable || !mountpoint_hashtable)
  2867. panic("Failed to allocate mount hash table\n");
  2868. kernfs_init();
  2869. err = sysfs_init();
  2870. if (err)
  2871. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2872. __func__, err);
  2873. fs_kobj = kobject_create_and_add("fs", NULL);
  2874. if (!fs_kobj)
  2875. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2876. init_rootfs();
  2877. init_mount_tree();
  2878. }
  2879. void put_mnt_ns(struct mnt_namespace *ns)
  2880. {
  2881. if (!atomic_dec_and_test(&ns->count))
  2882. return;
  2883. drop_collected_mounts(&ns->root->mnt);
  2884. free_mnt_ns(ns);
  2885. }
  2886. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2887. {
  2888. struct vfsmount *mnt;
  2889. mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, data);
  2890. if (!IS_ERR(mnt)) {
  2891. /*
  2892. * it is a longterm mount, don't release mnt until
  2893. * we unmount before file sys is unregistered
  2894. */
  2895. real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
  2896. }
  2897. return mnt;
  2898. }
  2899. EXPORT_SYMBOL_GPL(kern_mount_data);
  2900. void kern_unmount(struct vfsmount *mnt)
  2901. {
  2902. /* release long term mount so mount point can be released */
  2903. if (!IS_ERR_OR_NULL(mnt)) {
  2904. real_mount(mnt)->mnt_ns = NULL;
  2905. synchronize_rcu(); /* yecchhh... */
  2906. mntput(mnt);
  2907. }
  2908. }
  2909. EXPORT_SYMBOL(kern_unmount);
  2910. bool our_mnt(struct vfsmount *mnt)
  2911. {
  2912. return check_mnt(real_mount(mnt));
  2913. }
  2914. bool current_chrooted(void)
  2915. {
  2916. /* Does the current process have a non-standard root */
  2917. struct path ns_root;
  2918. struct path fs_root;
  2919. bool chrooted;
  2920. /* Find the namespace root */
  2921. ns_root.mnt = &current->nsproxy->mnt_ns->root->mnt;
  2922. ns_root.dentry = ns_root.mnt->mnt_root;
  2923. path_get(&ns_root);
  2924. while (d_mountpoint(ns_root.dentry) && follow_down_one(&ns_root))
  2925. ;
  2926. get_fs_root(current->fs, &fs_root);
  2927. chrooted = !path_equal(&fs_root, &ns_root);
  2928. path_put(&fs_root);
  2929. path_put(&ns_root);
  2930. return chrooted;
  2931. }
  2932. static bool mnt_already_visible(struct mnt_namespace *ns, struct vfsmount *new,
  2933. int *new_mnt_flags)
  2934. {
  2935. int new_flags = *new_mnt_flags;
  2936. struct mount *mnt;
  2937. bool visible = false;
  2938. down_read(&namespace_sem);
  2939. list_for_each_entry(mnt, &ns->list, mnt_list) {
  2940. struct mount *child;
  2941. int mnt_flags;
  2942. if (mnt->mnt.mnt_sb->s_type != new->mnt_sb->s_type)
  2943. continue;
  2944. /* This mount is not fully visible if it's root directory
  2945. * is not the root directory of the filesystem.
  2946. */
  2947. if (mnt->mnt.mnt_root != mnt->mnt.mnt_sb->s_root)
  2948. continue;
  2949. /* A local view of the mount flags */
  2950. mnt_flags = mnt->mnt.mnt_flags;
  2951. /* Don't miss readonly hidden in the superblock flags */
  2952. if (sb_rdonly(mnt->mnt.mnt_sb))
  2953. mnt_flags |= MNT_LOCK_READONLY;
  2954. /* Verify the mount flags are equal to or more permissive
  2955. * than the proposed new mount.
  2956. */
  2957. if ((mnt_flags & MNT_LOCK_READONLY) &&
  2958. !(new_flags & MNT_READONLY))
  2959. continue;
  2960. if ((mnt_flags & MNT_LOCK_ATIME) &&
  2961. ((mnt_flags & MNT_ATIME_MASK) != (new_flags & MNT_ATIME_MASK)))
  2962. continue;
  2963. /* This mount is not fully visible if there are any
  2964. * locked child mounts that cover anything except for
  2965. * empty directories.
  2966. */
  2967. list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
  2968. struct inode *inode = child->mnt_mountpoint->d_inode;
  2969. /* Only worry about locked mounts */
  2970. if (!(child->mnt.mnt_flags & MNT_LOCKED))
  2971. continue;
  2972. /* Is the directory permanetly empty? */
  2973. if (!is_empty_dir_inode(inode))
  2974. goto next;
  2975. }
  2976. /* Preserve the locked attributes */
  2977. *new_mnt_flags |= mnt_flags & (MNT_LOCK_READONLY | \
  2978. MNT_LOCK_ATIME);
  2979. visible = true;
  2980. goto found;
  2981. next: ;
  2982. }
  2983. found:
  2984. up_read(&namespace_sem);
  2985. return visible;
  2986. }
  2987. static bool mount_too_revealing(struct vfsmount *mnt, int *new_mnt_flags)
  2988. {
  2989. const unsigned long required_iflags = SB_I_NOEXEC | SB_I_NODEV;
  2990. struct mnt_namespace *ns = current->nsproxy->mnt_ns;
  2991. unsigned long s_iflags;
  2992. if (ns->user_ns == &init_user_ns)
  2993. return false;
  2994. /* Can this filesystem be too revealing? */
  2995. s_iflags = mnt->mnt_sb->s_iflags;
  2996. if (!(s_iflags & SB_I_USERNS_VISIBLE))
  2997. return false;
  2998. if ((s_iflags & required_iflags) != required_iflags) {
  2999. WARN_ONCE(1, "Expected s_iflags to contain 0x%lx\n",
  3000. required_iflags);
  3001. return true;
  3002. }
  3003. return !mnt_already_visible(ns, mnt, new_mnt_flags);
  3004. }
  3005. bool mnt_may_suid(struct vfsmount *mnt)
  3006. {
  3007. /*
  3008. * Foreign mounts (accessed via fchdir or through /proc
  3009. * symlinks) are always treated as if they are nosuid. This
  3010. * prevents namespaces from trusting potentially unsafe
  3011. * suid/sgid bits, file caps, or security labels that originate
  3012. * in other namespaces.
  3013. */
  3014. return !(mnt->mnt_flags & MNT_NOSUID) && check_mnt(real_mount(mnt)) &&
  3015. current_in_userns(mnt->mnt_sb->s_user_ns);
  3016. }
  3017. static struct ns_common *mntns_get(struct task_struct *task)
  3018. {
  3019. struct ns_common *ns = NULL;
  3020. struct nsproxy *nsproxy;
  3021. task_lock(task);
  3022. nsproxy = task->nsproxy;
  3023. if (nsproxy) {
  3024. ns = &nsproxy->mnt_ns->ns;
  3025. get_mnt_ns(to_mnt_ns(ns));
  3026. }
  3027. task_unlock(task);
  3028. return ns;
  3029. }
  3030. static void mntns_put(struct ns_common *ns)
  3031. {
  3032. put_mnt_ns(to_mnt_ns(ns));
  3033. }
  3034. static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  3035. {
  3036. struct fs_struct *fs = current->fs;
  3037. struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
  3038. struct path root;
  3039. int err;
  3040. if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
  3041. !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
  3042. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  3043. return -EPERM;
  3044. if (fs->users != 1)
  3045. return -EINVAL;
  3046. get_mnt_ns(mnt_ns);
  3047. old_mnt_ns = nsproxy->mnt_ns;
  3048. nsproxy->mnt_ns = mnt_ns;
  3049. /* Find the root */
  3050. err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
  3051. "/", LOOKUP_DOWN, &root);
  3052. if (err) {
  3053. /* revert to old namespace */
  3054. nsproxy->mnt_ns = old_mnt_ns;
  3055. put_mnt_ns(mnt_ns);
  3056. return err;
  3057. }
  3058. put_mnt_ns(old_mnt_ns);
  3059. /* Update the pwd and root */
  3060. set_fs_pwd(fs, &root);
  3061. set_fs_root(fs, &root);
  3062. path_put(&root);
  3063. return 0;
  3064. }
  3065. static struct user_namespace *mntns_owner(struct ns_common *ns)
  3066. {
  3067. return to_mnt_ns(ns)->user_ns;
  3068. }
  3069. const struct proc_ns_operations mntns_operations = {
  3070. .name = "mnt",
  3071. .type = CLONE_NEWNS,
  3072. .get = mntns_get,
  3073. .put = mntns_put,
  3074. .install = mntns_install,
  3075. .owner = mntns_owner,
  3076. };