namespace.c 85 KB

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