namespace.c 87 KB

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