mount_setattr_test.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <sched.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <sys/mount.h>
  11. #include <sys/wait.h>
  12. #include <sys/vfs.h>
  13. #include <sys/statvfs.h>
  14. #include <sys/sysinfo.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <grp.h>
  19. #include <stdbool.h>
  20. #include <stdarg.h>
  21. #include <linux/mount.h>
  22. #include "../filesystems/wrappers.h"
  23. #include "kselftest_harness.h"
  24. #ifndef CLONE_NEWNS
  25. #define CLONE_NEWNS 0x00020000
  26. #endif
  27. #ifndef CLONE_NEWUSER
  28. #define CLONE_NEWUSER 0x10000000
  29. #endif
  30. #ifndef MS_REC
  31. #define MS_REC 16384
  32. #endif
  33. #ifndef MS_RELATIME
  34. #define MS_RELATIME (1 << 21)
  35. #endif
  36. #ifndef MS_STRICTATIME
  37. #define MS_STRICTATIME (1 << 24)
  38. #endif
  39. #ifndef MOUNT_ATTR_RDONLY
  40. #define MOUNT_ATTR_RDONLY 0x00000001
  41. #endif
  42. #ifndef MOUNT_ATTR_NOSUID
  43. #define MOUNT_ATTR_NOSUID 0x00000002
  44. #endif
  45. #ifndef MOUNT_ATTR_NOEXEC
  46. #define MOUNT_ATTR_NOEXEC 0x00000008
  47. #endif
  48. #ifndef MOUNT_ATTR_NODIRATIME
  49. #define MOUNT_ATTR_NODIRATIME 0x00000080
  50. #endif
  51. #ifndef MOUNT_ATTR__ATIME
  52. #define MOUNT_ATTR__ATIME 0x00000070
  53. #endif
  54. #ifndef MOUNT_ATTR_RELATIME
  55. #define MOUNT_ATTR_RELATIME 0x00000000
  56. #endif
  57. #ifndef MOUNT_ATTR_NOATIME
  58. #define MOUNT_ATTR_NOATIME 0x00000010
  59. #endif
  60. #ifndef MOUNT_ATTR_STRICTATIME
  61. #define MOUNT_ATTR_STRICTATIME 0x00000020
  62. #endif
  63. #ifndef AT_RECURSIVE
  64. #define AT_RECURSIVE 0x8000
  65. #endif
  66. #ifndef MS_SHARED
  67. #define MS_SHARED (1 << 20)
  68. #endif
  69. #define DEFAULT_THREADS 4
  70. #define ptr_to_int(p) ((int)((intptr_t)(p)))
  71. #define int_to_ptr(u) ((void *)((intptr_t)(u)))
  72. #ifndef __NR_mount_setattr
  73. #if defined __alpha__
  74. #define __NR_mount_setattr 552
  75. #elif defined _MIPS_SIM
  76. #if _MIPS_SIM == _MIPS_SIM_ABI32 /* o32 */
  77. #define __NR_mount_setattr (442 + 4000)
  78. #endif
  79. #if _MIPS_SIM == _MIPS_SIM_NABI32 /* n32 */
  80. #define __NR_mount_setattr (442 + 6000)
  81. #endif
  82. #if _MIPS_SIM == _MIPS_SIM_ABI64 /* n64 */
  83. #define __NR_mount_setattr (442 + 5000)
  84. #endif
  85. #elif defined __ia64__
  86. #define __NR_mount_setattr (442 + 1024)
  87. #else
  88. #define __NR_mount_setattr 442
  89. #endif
  90. #endif
  91. #ifndef __NR_open_tree_attr
  92. #if defined __alpha__
  93. #define __NR_open_tree_attr 577
  94. #elif defined _MIPS_SIM
  95. #if _MIPS_SIM == _MIPS_SIM_ABI32 /* o32 */
  96. #define __NR_open_tree_attr (467 + 4000)
  97. #endif
  98. #if _MIPS_SIM == _MIPS_SIM_NABI32 /* n32 */
  99. #define __NR_open_tree_attr (467 + 6000)
  100. #endif
  101. #if _MIPS_SIM == _MIPS_SIM_ABI64 /* n64 */
  102. #define __NR_open_tree_attr (467 + 5000)
  103. #endif
  104. #elif defined __ia64__
  105. #define __NR_open_tree_attr (467 + 1024)
  106. #else
  107. #define __NR_open_tree_attr 467
  108. #endif
  109. #endif
  110. #ifndef MOUNT_ATTR_IDMAP
  111. #define MOUNT_ATTR_IDMAP 0x00100000
  112. #endif
  113. #ifndef MOUNT_ATTR_NOSYMFOLLOW
  114. #define MOUNT_ATTR_NOSYMFOLLOW 0x00200000
  115. #endif
  116. static inline int sys_mount_setattr(int dfd, const char *path, unsigned int flags,
  117. struct mount_attr *attr, size_t size)
  118. {
  119. return syscall(__NR_mount_setattr, dfd, path, flags, attr, size);
  120. }
  121. static inline int sys_open_tree_attr(int dfd, const char *path, unsigned int flags,
  122. struct mount_attr *attr, size_t size)
  123. {
  124. return syscall(__NR_open_tree_attr, dfd, path, flags, attr, size);
  125. }
  126. static ssize_t write_nointr(int fd, const void *buf, size_t count)
  127. {
  128. ssize_t ret;
  129. do {
  130. ret = write(fd, buf, count);
  131. } while (ret < 0 && errno == EINTR);
  132. return ret;
  133. }
  134. static int write_file(const char *path, const void *buf, size_t count)
  135. {
  136. int fd;
  137. ssize_t ret;
  138. fd = open(path, O_WRONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW);
  139. if (fd < 0)
  140. return -1;
  141. ret = write_nointr(fd, buf, count);
  142. close(fd);
  143. if (ret < 0 || (size_t)ret != count)
  144. return -1;
  145. return 0;
  146. }
  147. static int create_and_enter_userns(void)
  148. {
  149. uid_t uid;
  150. gid_t gid;
  151. char map[100];
  152. uid = getuid();
  153. gid = getgid();
  154. if (unshare(CLONE_NEWUSER))
  155. return -1;
  156. if (write_file("/proc/self/setgroups", "deny", sizeof("deny") - 1) &&
  157. errno != ENOENT)
  158. return -1;
  159. snprintf(map, sizeof(map), "0 %d 1", uid);
  160. if (write_file("/proc/self/uid_map", map, strlen(map)))
  161. return -1;
  162. snprintf(map, sizeof(map), "0 %d 1", gid);
  163. if (write_file("/proc/self/gid_map", map, strlen(map)))
  164. return -1;
  165. if (setgid(0))
  166. return -1;
  167. if (setuid(0))
  168. return -1;
  169. return 0;
  170. }
  171. static int prepare_unpriv_mountns(void)
  172. {
  173. if (create_and_enter_userns())
  174. return -1;
  175. if (unshare(CLONE_NEWNS))
  176. return -1;
  177. if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0))
  178. return -1;
  179. return 0;
  180. }
  181. #ifndef ST_NOSYMFOLLOW
  182. #define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */
  183. #endif
  184. static int read_mnt_flags(const char *path)
  185. {
  186. int ret;
  187. struct statvfs stat;
  188. unsigned int mnt_flags;
  189. ret = statvfs(path, &stat);
  190. if (ret != 0)
  191. return -EINVAL;
  192. if (stat.f_flag & ~(ST_RDONLY | ST_NOSUID | ST_NODEV | ST_NOEXEC |
  193. ST_NOATIME | ST_NODIRATIME | ST_RELATIME |
  194. ST_SYNCHRONOUS | ST_MANDLOCK | ST_NOSYMFOLLOW))
  195. return -EINVAL;
  196. mnt_flags = 0;
  197. if (stat.f_flag & ST_RDONLY)
  198. mnt_flags |= MS_RDONLY;
  199. if (stat.f_flag & ST_NOSUID)
  200. mnt_flags |= MS_NOSUID;
  201. if (stat.f_flag & ST_NODEV)
  202. mnt_flags |= MS_NODEV;
  203. if (stat.f_flag & ST_NOEXEC)
  204. mnt_flags |= MS_NOEXEC;
  205. if (stat.f_flag & ST_NOATIME)
  206. mnt_flags |= MS_NOATIME;
  207. if (stat.f_flag & ST_NODIRATIME)
  208. mnt_flags |= MS_NODIRATIME;
  209. if (stat.f_flag & ST_RELATIME)
  210. mnt_flags |= MS_RELATIME;
  211. if (stat.f_flag & ST_SYNCHRONOUS)
  212. mnt_flags |= MS_SYNCHRONOUS;
  213. if (stat.f_flag & ST_MANDLOCK)
  214. mnt_flags |= ST_MANDLOCK;
  215. if (stat.f_flag & ST_NOSYMFOLLOW)
  216. mnt_flags |= ST_NOSYMFOLLOW;
  217. return mnt_flags;
  218. }
  219. static char *get_field(char *src, int nfields)
  220. {
  221. int i;
  222. char *p = src;
  223. for (i = 0; i < nfields; i++) {
  224. while (*p && *p != ' ' && *p != '\t')
  225. p++;
  226. if (!*p)
  227. break;
  228. p++;
  229. }
  230. return p;
  231. }
  232. static void null_endofword(char *word)
  233. {
  234. while (*word && *word != ' ' && *word != '\t')
  235. word++;
  236. *word = '\0';
  237. }
  238. static bool is_shared_mount(const char *path)
  239. {
  240. size_t len = 0;
  241. char *line = NULL;
  242. FILE *f = NULL;
  243. f = fopen("/proc/self/mountinfo", "re");
  244. if (!f)
  245. return false;
  246. while (getline(&line, &len, f) != -1) {
  247. char *opts, *target;
  248. target = get_field(line, 4);
  249. if (!target)
  250. continue;
  251. opts = get_field(target, 2);
  252. if (!opts)
  253. continue;
  254. null_endofword(target);
  255. if (strcmp(target, path) != 0)
  256. continue;
  257. null_endofword(opts);
  258. if (strstr(opts, "shared:"))
  259. return true;
  260. }
  261. free(line);
  262. fclose(f);
  263. return false;
  264. }
  265. static void *mount_setattr_thread(void *data)
  266. {
  267. struct mount_attr attr = {
  268. .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID,
  269. .attr_clr = 0,
  270. .propagation = MS_SHARED,
  271. };
  272. if (sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)))
  273. pthread_exit(int_to_ptr(-1));
  274. pthread_exit(int_to_ptr(0));
  275. }
  276. /* Attempt to de-conflict with the selftests tree. */
  277. #ifndef SKIP
  278. #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__)
  279. #endif
  280. static bool mount_setattr_supported(void)
  281. {
  282. int ret;
  283. ret = sys_mount_setattr(-EBADF, "", AT_EMPTY_PATH, NULL, 0);
  284. if (ret < 0 && errno == ENOSYS)
  285. return false;
  286. return true;
  287. }
  288. FIXTURE(mount_setattr) {
  289. };
  290. #define NOSYMFOLLOW_TARGET "/mnt/A/AA/data"
  291. #define NOSYMFOLLOW_SYMLINK "/mnt/A/AA/symlink"
  292. FIXTURE_SETUP(mount_setattr)
  293. {
  294. int fd = -EBADF;
  295. if (!mount_setattr_supported())
  296. SKIP(return, "mount_setattr syscall not supported");
  297. ASSERT_EQ(prepare_unpriv_mountns(), 0);
  298. (void)umount2("/mnt", MNT_DETACH);
  299. (void)umount2("/tmp", MNT_DETACH);
  300. ASSERT_EQ(mount("testing", "/tmp", "tmpfs", MS_NOATIME | MS_NODEV,
  301. "size=100000,mode=700"), 0);
  302. ASSERT_EQ(mkdir("/tmp/B", 0777), 0);
  303. ASSERT_EQ(mount("testing", "/tmp/B", "tmpfs", MS_NOATIME | MS_NODEV,
  304. "size=100000,mode=700"), 0);
  305. ASSERT_EQ(mkdir("/tmp/B/BB", 0777), 0);
  306. ASSERT_EQ(mkdir("/tmp/target1", 0777), 0);
  307. ASSERT_EQ(mkdir("/tmp/target2", 0777), 0);
  308. ASSERT_EQ(mount("testing", "/tmp/B/BB", "tmpfs", MS_NOATIME | MS_NODEV,
  309. "size=100000,mode=700"), 0);
  310. ASSERT_EQ(mount("testing", "/mnt", "tmpfs", MS_NOATIME | MS_NODEV,
  311. "size=100000,mode=700"), 0);
  312. ASSERT_EQ(mkdir("/mnt/A", 0777), 0);
  313. ASSERT_EQ(mount("testing", "/mnt/A", "tmpfs", MS_NOATIME | MS_NODEV,
  314. "size=100000,mode=700"), 0);
  315. ASSERT_EQ(mkdir("/mnt/A/AA", 0777), 0);
  316. ASSERT_EQ(mount("/tmp", "/mnt/A/AA", NULL, MS_BIND | MS_REC, NULL), 0);
  317. ASSERT_EQ(mkdir("/mnt/B", 0777), 0);
  318. ASSERT_EQ(mount("testing", "/mnt/B", "ramfs",
  319. MS_NOATIME | MS_NODEV | MS_NOSUID, 0), 0);
  320. ASSERT_EQ(mkdir("/mnt/B/BB", 0777), 0);
  321. ASSERT_EQ(mount("testing", "/tmp/B/BB", "devpts",
  322. MS_RELATIME | MS_NOEXEC | MS_RDONLY, 0), 0);
  323. fd = creat(NOSYMFOLLOW_TARGET, O_RDWR | O_CLOEXEC);
  324. ASSERT_GT(fd, 0);
  325. ASSERT_EQ(symlink(NOSYMFOLLOW_TARGET, NOSYMFOLLOW_SYMLINK), 0);
  326. ASSERT_EQ(close(fd), 0);
  327. }
  328. FIXTURE_TEARDOWN(mount_setattr)
  329. {
  330. if (!mount_setattr_supported())
  331. SKIP(return, "mount_setattr syscall not supported");
  332. (void)umount2("/mnt/A", MNT_DETACH);
  333. (void)umount2("/tmp", MNT_DETACH);
  334. }
  335. TEST_F(mount_setattr, invalid_attributes)
  336. {
  337. struct mount_attr invalid_attr = {
  338. .attr_set = (1U << 31),
  339. };
  340. if (!mount_setattr_supported())
  341. SKIP(return, "mount_setattr syscall not supported");
  342. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  343. sizeof(invalid_attr)), 0);
  344. invalid_attr.attr_set = 0;
  345. invalid_attr.attr_clr = (1U << 31);
  346. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  347. sizeof(invalid_attr)), 0);
  348. invalid_attr.attr_clr = 0;
  349. invalid_attr.propagation = (1U << 31);
  350. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  351. sizeof(invalid_attr)), 0);
  352. invalid_attr.attr_set = (1U << 31);
  353. invalid_attr.attr_clr = (1U << 31);
  354. invalid_attr.propagation = (1U << 31);
  355. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  356. sizeof(invalid_attr)), 0);
  357. ASSERT_NE(sys_mount_setattr(-1, "mnt/A", AT_RECURSIVE, &invalid_attr,
  358. sizeof(invalid_attr)), 0);
  359. }
  360. TEST_F(mount_setattr, extensibility)
  361. {
  362. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  363. char *s = "dummy";
  364. struct mount_attr invalid_attr = {};
  365. struct mount_attr_large {
  366. struct mount_attr attr1;
  367. struct mount_attr attr2;
  368. struct mount_attr attr3;
  369. } large_attr = {};
  370. if (!mount_setattr_supported())
  371. SKIP(return, "mount_setattr syscall not supported");
  372. old_flags = read_mnt_flags("/mnt/A");
  373. ASSERT_GT(old_flags, 0);
  374. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, NULL,
  375. sizeof(invalid_attr)), 0);
  376. ASSERT_EQ(errno, EFAULT);
  377. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, (void *)s,
  378. sizeof(invalid_attr)), 0);
  379. ASSERT_EQ(errno, EINVAL);
  380. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr, 0), 0);
  381. ASSERT_EQ(errno, EINVAL);
  382. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  383. sizeof(invalid_attr) / 2), 0);
  384. ASSERT_EQ(errno, EINVAL);
  385. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &invalid_attr,
  386. sizeof(invalid_attr) / 2), 0);
  387. ASSERT_EQ(errno, EINVAL);
  388. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE,
  389. (void *)&large_attr, sizeof(large_attr)), 0);
  390. large_attr.attr3.attr_set = MOUNT_ATTR_RDONLY;
  391. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE,
  392. (void *)&large_attr, sizeof(large_attr)), 0);
  393. large_attr.attr3.attr_set = 0;
  394. large_attr.attr1.attr_set = MOUNT_ATTR_RDONLY;
  395. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE,
  396. (void *)&large_attr, sizeof(large_attr)), 0);
  397. expected_flags = old_flags;
  398. expected_flags |= MS_RDONLY;
  399. new_flags = read_mnt_flags("/mnt/A");
  400. ASSERT_EQ(new_flags, expected_flags);
  401. new_flags = read_mnt_flags("/mnt/A/AA");
  402. ASSERT_EQ(new_flags, expected_flags);
  403. new_flags = read_mnt_flags("/mnt/A/AA/B");
  404. ASSERT_EQ(new_flags, expected_flags);
  405. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  406. ASSERT_EQ(new_flags, expected_flags);
  407. }
  408. TEST_F(mount_setattr, basic)
  409. {
  410. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  411. struct mount_attr attr = {
  412. .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOEXEC | MOUNT_ATTR_RELATIME,
  413. .attr_clr = MOUNT_ATTR__ATIME,
  414. };
  415. if (!mount_setattr_supported())
  416. SKIP(return, "mount_setattr syscall not supported");
  417. old_flags = read_mnt_flags("/mnt/A");
  418. ASSERT_GT(old_flags, 0);
  419. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", 0, &attr, sizeof(attr)), 0);
  420. expected_flags = old_flags;
  421. expected_flags |= MS_RDONLY;
  422. expected_flags |= MS_NOEXEC;
  423. expected_flags &= ~MS_NOATIME;
  424. expected_flags |= MS_RELATIME;
  425. new_flags = read_mnt_flags("/mnt/A");
  426. ASSERT_EQ(new_flags, expected_flags);
  427. new_flags = read_mnt_flags("/mnt/A/AA");
  428. ASSERT_EQ(new_flags, old_flags);
  429. new_flags = read_mnt_flags("/mnt/A/AA/B");
  430. ASSERT_EQ(new_flags, old_flags);
  431. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  432. ASSERT_EQ(new_flags, old_flags);
  433. }
  434. TEST_F(mount_setattr, basic_recursive)
  435. {
  436. int fd;
  437. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  438. struct mount_attr attr = {
  439. .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOEXEC | MOUNT_ATTR_RELATIME,
  440. .attr_clr = MOUNT_ATTR__ATIME,
  441. };
  442. if (!mount_setattr_supported())
  443. SKIP(return, "mount_setattr syscall not supported");
  444. old_flags = read_mnt_flags("/mnt/A");
  445. ASSERT_GT(old_flags, 0);
  446. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  447. expected_flags = old_flags;
  448. expected_flags |= MS_RDONLY;
  449. expected_flags |= MS_NOEXEC;
  450. expected_flags &= ~MS_NOATIME;
  451. expected_flags |= MS_RELATIME;
  452. new_flags = read_mnt_flags("/mnt/A");
  453. ASSERT_EQ(new_flags, expected_flags);
  454. new_flags = read_mnt_flags("/mnt/A/AA");
  455. ASSERT_EQ(new_flags, expected_flags);
  456. new_flags = read_mnt_flags("/mnt/A/AA/B");
  457. ASSERT_EQ(new_flags, expected_flags);
  458. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  459. ASSERT_EQ(new_flags, expected_flags);
  460. memset(&attr, 0, sizeof(attr));
  461. attr.attr_clr = MOUNT_ATTR_RDONLY;
  462. attr.propagation = MS_SHARED;
  463. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  464. expected_flags &= ~MS_RDONLY;
  465. new_flags = read_mnt_flags("/mnt/A");
  466. ASSERT_EQ(new_flags, expected_flags);
  467. ASSERT_EQ(is_shared_mount("/mnt/A"), true);
  468. new_flags = read_mnt_flags("/mnt/A/AA");
  469. ASSERT_EQ(new_flags, expected_flags);
  470. ASSERT_EQ(is_shared_mount("/mnt/A/AA"), true);
  471. new_flags = read_mnt_flags("/mnt/A/AA/B");
  472. ASSERT_EQ(new_flags, expected_flags);
  473. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B"), true);
  474. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  475. ASSERT_EQ(new_flags, expected_flags);
  476. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B/BB"), true);
  477. fd = open("/mnt/A/AA/B/b", O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0777);
  478. ASSERT_GE(fd, 0);
  479. /*
  480. * We're holding a fd open for writing so this needs to fail somewhere
  481. * in the middle and the mount options need to be unchanged.
  482. */
  483. attr.attr_set = MOUNT_ATTR_RDONLY;
  484. ASSERT_LT(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  485. new_flags = read_mnt_flags("/mnt/A");
  486. ASSERT_EQ(new_flags, expected_flags);
  487. ASSERT_EQ(is_shared_mount("/mnt/A"), true);
  488. new_flags = read_mnt_flags("/mnt/A/AA");
  489. ASSERT_EQ(new_flags, expected_flags);
  490. ASSERT_EQ(is_shared_mount("/mnt/A/AA"), true);
  491. new_flags = read_mnt_flags("/mnt/A/AA/B");
  492. ASSERT_EQ(new_flags, expected_flags);
  493. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B"), true);
  494. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  495. ASSERT_EQ(new_flags, expected_flags);
  496. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B/BB"), true);
  497. EXPECT_EQ(close(fd), 0);
  498. }
  499. TEST_F(mount_setattr, mount_has_writers)
  500. {
  501. int fd, dfd;
  502. unsigned int old_flags = 0, new_flags = 0;
  503. struct mount_attr attr = {
  504. .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOEXEC | MOUNT_ATTR_RELATIME,
  505. .attr_clr = MOUNT_ATTR__ATIME,
  506. .propagation = MS_SHARED,
  507. };
  508. if (!mount_setattr_supported())
  509. SKIP(return, "mount_setattr syscall not supported");
  510. old_flags = read_mnt_flags("/mnt/A");
  511. ASSERT_GT(old_flags, 0);
  512. fd = open("/mnt/A/AA/B/b", O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0777);
  513. ASSERT_GE(fd, 0);
  514. /*
  515. * We're holding a fd open to a mount somwhere in the middle so this
  516. * needs to fail somewhere in the middle. After this the mount options
  517. * need to be unchanged.
  518. */
  519. ASSERT_LT(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  520. new_flags = read_mnt_flags("/mnt/A");
  521. ASSERT_EQ(new_flags, old_flags);
  522. ASSERT_EQ(is_shared_mount("/mnt/A"), false);
  523. new_flags = read_mnt_flags("/mnt/A/AA");
  524. ASSERT_EQ(new_flags, old_flags);
  525. ASSERT_EQ(is_shared_mount("/mnt/A/AA"), false);
  526. new_flags = read_mnt_flags("/mnt/A/AA/B");
  527. ASSERT_EQ(new_flags, old_flags);
  528. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B"), false);
  529. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  530. ASSERT_EQ(new_flags, old_flags);
  531. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B/BB"), false);
  532. dfd = open("/mnt/A/AA/B", O_DIRECTORY | O_CLOEXEC);
  533. ASSERT_GE(dfd, 0);
  534. EXPECT_EQ(fsync(dfd), 0);
  535. EXPECT_EQ(close(dfd), 0);
  536. EXPECT_EQ(fsync(fd), 0);
  537. EXPECT_EQ(close(fd), 0);
  538. /* All writers are gone so this should succeed. */
  539. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  540. }
  541. TEST_F(mount_setattr, mixed_mount_options)
  542. {
  543. unsigned int old_flags1 = 0, old_flags2 = 0, new_flags = 0, expected_flags = 0;
  544. struct mount_attr attr = {
  545. .attr_clr = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID | MOUNT_ATTR_NOEXEC | MOUNT_ATTR__ATIME,
  546. .attr_set = MOUNT_ATTR_RELATIME,
  547. };
  548. if (!mount_setattr_supported())
  549. SKIP(return, "mount_setattr syscall not supported");
  550. old_flags1 = read_mnt_flags("/mnt/B");
  551. ASSERT_GT(old_flags1, 0);
  552. old_flags2 = read_mnt_flags("/mnt/B/BB");
  553. ASSERT_GT(old_flags2, 0);
  554. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/B", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  555. expected_flags = old_flags2;
  556. expected_flags &= ~(MS_RDONLY | MS_NOEXEC | MS_NOATIME | MS_NOSUID);
  557. expected_flags |= MS_RELATIME;
  558. new_flags = read_mnt_flags("/mnt/B");
  559. ASSERT_EQ(new_flags, expected_flags);
  560. expected_flags = old_flags2;
  561. expected_flags &= ~(MS_RDONLY | MS_NOEXEC | MS_NOATIME | MS_NOSUID);
  562. expected_flags |= MS_RELATIME;
  563. new_flags = read_mnt_flags("/mnt/B/BB");
  564. ASSERT_EQ(new_flags, expected_flags);
  565. }
  566. TEST_F(mount_setattr, time_changes)
  567. {
  568. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  569. struct mount_attr attr = {
  570. .attr_set = MOUNT_ATTR_NODIRATIME | MOUNT_ATTR_NOATIME,
  571. };
  572. if (!mount_setattr_supported())
  573. SKIP(return, "mount_setattr syscall not supported");
  574. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  575. attr.attr_set = MOUNT_ATTR_STRICTATIME;
  576. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  577. attr.attr_set = MOUNT_ATTR_STRICTATIME | MOUNT_ATTR_NOATIME;
  578. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  579. attr.attr_set = MOUNT_ATTR_STRICTATIME | MOUNT_ATTR_NOATIME;
  580. attr.attr_clr = MOUNT_ATTR__ATIME;
  581. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  582. attr.attr_set = 0;
  583. attr.attr_clr = MOUNT_ATTR_STRICTATIME;
  584. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  585. attr.attr_clr = MOUNT_ATTR_NOATIME;
  586. ASSERT_NE(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  587. old_flags = read_mnt_flags("/mnt/A");
  588. ASSERT_GT(old_flags, 0);
  589. attr.attr_set = MOUNT_ATTR_NODIRATIME | MOUNT_ATTR_NOATIME;
  590. attr.attr_clr = MOUNT_ATTR__ATIME;
  591. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  592. expected_flags = old_flags;
  593. expected_flags |= MS_NOATIME;
  594. expected_flags |= MS_NODIRATIME;
  595. new_flags = read_mnt_flags("/mnt/A");
  596. ASSERT_EQ(new_flags, expected_flags);
  597. new_flags = read_mnt_flags("/mnt/A/AA");
  598. ASSERT_EQ(new_flags, expected_flags);
  599. new_flags = read_mnt_flags("/mnt/A/AA/B");
  600. ASSERT_EQ(new_flags, expected_flags);
  601. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  602. ASSERT_EQ(new_flags, expected_flags);
  603. memset(&attr, 0, sizeof(attr));
  604. attr.attr_set &= ~MOUNT_ATTR_NOATIME;
  605. attr.attr_set |= MOUNT_ATTR_RELATIME;
  606. attr.attr_clr |= MOUNT_ATTR__ATIME;
  607. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  608. expected_flags &= ~MS_NOATIME;
  609. expected_flags |= MS_RELATIME;
  610. new_flags = read_mnt_flags("/mnt/A");
  611. ASSERT_EQ(new_flags, expected_flags);
  612. new_flags = read_mnt_flags("/mnt/A/AA");
  613. ASSERT_EQ(new_flags, expected_flags);
  614. new_flags = read_mnt_flags("/mnt/A/AA/B");
  615. ASSERT_EQ(new_flags, expected_flags);
  616. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  617. ASSERT_EQ(new_flags, expected_flags);
  618. memset(&attr, 0, sizeof(attr));
  619. attr.attr_set &= ~MOUNT_ATTR_RELATIME;
  620. attr.attr_set |= MOUNT_ATTR_STRICTATIME;
  621. attr.attr_clr |= MOUNT_ATTR__ATIME;
  622. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  623. expected_flags &= ~MS_RELATIME;
  624. new_flags = read_mnt_flags("/mnt/A");
  625. ASSERT_EQ(new_flags, expected_flags);
  626. new_flags = read_mnt_flags("/mnt/A/AA");
  627. ASSERT_EQ(new_flags, expected_flags);
  628. new_flags = read_mnt_flags("/mnt/A/AA/B");
  629. ASSERT_EQ(new_flags, expected_flags);
  630. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  631. ASSERT_EQ(new_flags, expected_flags);
  632. memset(&attr, 0, sizeof(attr));
  633. attr.attr_set &= ~MOUNT_ATTR_STRICTATIME;
  634. attr.attr_set |= MOUNT_ATTR_NOATIME;
  635. attr.attr_clr |= MOUNT_ATTR__ATIME;
  636. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  637. expected_flags |= MS_NOATIME;
  638. new_flags = read_mnt_flags("/mnt/A");
  639. ASSERT_EQ(new_flags, expected_flags);
  640. new_flags = read_mnt_flags("/mnt/A/AA");
  641. ASSERT_EQ(new_flags, expected_flags);
  642. new_flags = read_mnt_flags("/mnt/A/AA/B");
  643. ASSERT_EQ(new_flags, expected_flags);
  644. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  645. ASSERT_EQ(new_flags, expected_flags);
  646. memset(&attr, 0, sizeof(attr));
  647. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  648. new_flags = read_mnt_flags("/mnt/A");
  649. ASSERT_EQ(new_flags, expected_flags);
  650. new_flags = read_mnt_flags("/mnt/A/AA");
  651. ASSERT_EQ(new_flags, expected_flags);
  652. new_flags = read_mnt_flags("/mnt/A/AA/B");
  653. ASSERT_EQ(new_flags, expected_flags);
  654. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  655. ASSERT_EQ(new_flags, expected_flags);
  656. memset(&attr, 0, sizeof(attr));
  657. attr.attr_clr = MOUNT_ATTR_NODIRATIME;
  658. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  659. expected_flags &= ~MS_NODIRATIME;
  660. new_flags = read_mnt_flags("/mnt/A");
  661. ASSERT_EQ(new_flags, expected_flags);
  662. new_flags = read_mnt_flags("/mnt/A/AA");
  663. ASSERT_EQ(new_flags, expected_flags);
  664. new_flags = read_mnt_flags("/mnt/A/AA/B");
  665. ASSERT_EQ(new_flags, expected_flags);
  666. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  667. ASSERT_EQ(new_flags, expected_flags);
  668. }
  669. TEST_F(mount_setattr, multi_threaded)
  670. {
  671. int i, j, nthreads, ret = 0;
  672. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  673. pthread_attr_t pattr;
  674. pthread_t threads[DEFAULT_THREADS];
  675. if (!mount_setattr_supported())
  676. SKIP(return, "mount_setattr syscall not supported");
  677. old_flags = read_mnt_flags("/mnt/A");
  678. ASSERT_GT(old_flags, 0);
  679. /* Try to change mount options from multiple threads. */
  680. nthreads = get_nprocs_conf();
  681. if (nthreads > DEFAULT_THREADS)
  682. nthreads = DEFAULT_THREADS;
  683. pthread_attr_init(&pattr);
  684. for (i = 0; i < nthreads; i++)
  685. ASSERT_EQ(pthread_create(&threads[i], &pattr, mount_setattr_thread, NULL), 0);
  686. for (j = 0; j < i; j++) {
  687. void *retptr = NULL;
  688. EXPECT_EQ(pthread_join(threads[j], &retptr), 0);
  689. ret += ptr_to_int(retptr);
  690. EXPECT_EQ(ret, 0);
  691. }
  692. pthread_attr_destroy(&pattr);
  693. ASSERT_EQ(ret, 0);
  694. expected_flags = old_flags;
  695. expected_flags |= MS_RDONLY;
  696. expected_flags |= MS_NOSUID;
  697. new_flags = read_mnt_flags("/mnt/A");
  698. ASSERT_EQ(new_flags, expected_flags);
  699. ASSERT_EQ(is_shared_mount("/mnt/A"), true);
  700. new_flags = read_mnt_flags("/mnt/A/AA");
  701. ASSERT_EQ(new_flags, expected_flags);
  702. ASSERT_EQ(is_shared_mount("/mnt/A/AA"), true);
  703. new_flags = read_mnt_flags("/mnt/A/AA/B");
  704. ASSERT_EQ(new_flags, expected_flags);
  705. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B"), true);
  706. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  707. ASSERT_EQ(new_flags, expected_flags);
  708. ASSERT_EQ(is_shared_mount("/mnt/A/AA/B/BB"), true);
  709. }
  710. TEST_F(mount_setattr, wrong_user_namespace)
  711. {
  712. int ret;
  713. struct mount_attr attr = {
  714. .attr_set = MOUNT_ATTR_RDONLY,
  715. };
  716. if (!mount_setattr_supported())
  717. SKIP(return, "mount_setattr syscall not supported");
  718. EXPECT_EQ(create_and_enter_userns(), 0);
  719. ret = sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr));
  720. ASSERT_LT(ret, 0);
  721. ASSERT_EQ(errno, EPERM);
  722. }
  723. TEST_F(mount_setattr, wrong_mount_namespace)
  724. {
  725. int fd, ret;
  726. struct mount_attr attr = {
  727. .attr_set = MOUNT_ATTR_RDONLY,
  728. };
  729. if (!mount_setattr_supported())
  730. SKIP(return, "mount_setattr syscall not supported");
  731. fd = open("/mnt/A", O_DIRECTORY | O_CLOEXEC);
  732. ASSERT_GE(fd, 0);
  733. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  734. ret = sys_mount_setattr(fd, "", AT_EMPTY_PATH | AT_RECURSIVE, &attr, sizeof(attr));
  735. ASSERT_LT(ret, 0);
  736. ASSERT_EQ(errno, EINVAL);
  737. }
  738. FIXTURE(mount_setattr_idmapped) {
  739. };
  740. FIXTURE_SETUP(mount_setattr_idmapped)
  741. {
  742. int img_fd = -EBADF;
  743. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  744. ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0), 0);
  745. (void)umount2("/mnt", MNT_DETACH);
  746. (void)umount2("/tmp", MNT_DETACH);
  747. ASSERT_EQ(mount("testing", "/tmp", "tmpfs", MS_NOATIME | MS_NODEV,
  748. "size=100000,mode=700"), 0);
  749. ASSERT_EQ(mkdir("/tmp/B", 0777), 0);
  750. ASSERT_EQ(mknodat(-EBADF, "/tmp/B/b", S_IFREG | 0644, 0), 0);
  751. ASSERT_EQ(chown("/tmp/B/b", 0, 0), 0);
  752. ASSERT_EQ(mount("testing", "/tmp/B", "tmpfs", MS_NOATIME | MS_NODEV,
  753. "size=100000,mode=700"), 0);
  754. ASSERT_EQ(mkdir("/tmp/B/BB", 0777), 0);
  755. ASSERT_EQ(mknodat(-EBADF, "/tmp/B/BB/b", S_IFREG | 0644, 0), 0);
  756. ASSERT_EQ(chown("/tmp/B/BB/b", 0, 0), 0);
  757. ASSERT_EQ(mount("testing", "/tmp/B/BB", "tmpfs", MS_NOATIME | MS_NODEV,
  758. "size=100000,mode=700"), 0);
  759. ASSERT_EQ(mount("testing", "/mnt", "tmpfs", MS_NOATIME | MS_NODEV,
  760. "size=256m,mode=700"), 0);
  761. ASSERT_EQ(mkdir("/mnt/A", 0777), 0);
  762. ASSERT_EQ(mount("testing", "/mnt/A", "tmpfs", MS_NOATIME | MS_NODEV,
  763. "size=100000,mode=700"), 0);
  764. ASSERT_EQ(mkdir("/mnt/A/AA", 0777), 0);
  765. ASSERT_EQ(mount("/tmp", "/mnt/A/AA", NULL, MS_BIND | MS_REC, NULL), 0);
  766. ASSERT_EQ(mkdir("/mnt/B", 0777), 0);
  767. ASSERT_EQ(mount("testing", "/mnt/B", "ramfs",
  768. MS_NOATIME | MS_NODEV | MS_NOSUID, 0), 0);
  769. ASSERT_EQ(mkdir("/mnt/B/BB", 0777), 0);
  770. ASSERT_EQ(mount("testing", "/tmp/B/BB", "devpts",
  771. MS_RELATIME | MS_NOEXEC | MS_RDONLY, 0), 0);
  772. ASSERT_EQ(mkdir("/mnt/C", 0777), 0);
  773. ASSERT_EQ(mkdir("/mnt/D", 0777), 0);
  774. img_fd = openat(-EBADF, "/mnt/C/ext4.img", O_CREAT | O_WRONLY, 0600);
  775. ASSERT_GE(img_fd, 0);
  776. ASSERT_EQ(ftruncate(img_fd, 2147483648 /* 2 GB */), 0);
  777. ASSERT_EQ(system("mkfs.ext4 -q /mnt/C/ext4.img"), 0);
  778. ASSERT_EQ(system("mount -o loop -t ext4 /mnt/C/ext4.img /mnt/D/"), 0);
  779. ASSERT_EQ(close(img_fd), 0);
  780. }
  781. FIXTURE_TEARDOWN(mount_setattr_idmapped)
  782. {
  783. (void)umount2("/mnt/A", MNT_DETACH);
  784. (void)umount2("/tmp", MNT_DETACH);
  785. }
  786. /**
  787. * Validate that negative fd values are rejected.
  788. */
  789. TEST_F(mount_setattr_idmapped, invalid_fd_negative)
  790. {
  791. struct mount_attr attr = {
  792. .attr_set = MOUNT_ATTR_IDMAP,
  793. .userns_fd = -EBADF,
  794. };
  795. if (!mount_setattr_supported())
  796. SKIP(return, "mount_setattr syscall not supported");
  797. ASSERT_NE(sys_mount_setattr(-1, "/", 0, &attr, sizeof(attr)), 0) {
  798. TH_LOG("failure: created idmapped mount with negative fd");
  799. }
  800. }
  801. /**
  802. * Validate that excessively large fd values are rejected.
  803. */
  804. TEST_F(mount_setattr_idmapped, invalid_fd_large)
  805. {
  806. struct mount_attr attr = {
  807. .attr_set = MOUNT_ATTR_IDMAP,
  808. .userns_fd = INT64_MAX,
  809. };
  810. if (!mount_setattr_supported())
  811. SKIP(return, "mount_setattr syscall not supported");
  812. ASSERT_NE(sys_mount_setattr(-1, "/", 0, &attr, sizeof(attr)), 0) {
  813. TH_LOG("failure: created idmapped mount with too large fd value");
  814. }
  815. }
  816. /**
  817. * Validate that closed fd values are rejected.
  818. */
  819. TEST_F(mount_setattr_idmapped, invalid_fd_closed)
  820. {
  821. int fd;
  822. struct mount_attr attr = {
  823. .attr_set = MOUNT_ATTR_IDMAP,
  824. };
  825. if (!mount_setattr_supported())
  826. SKIP(return, "mount_setattr syscall not supported");
  827. fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
  828. ASSERT_GE(fd, 0);
  829. ASSERT_GE(close(fd), 0);
  830. attr.userns_fd = fd;
  831. ASSERT_NE(sys_mount_setattr(-1, "/", 0, &attr, sizeof(attr)), 0) {
  832. TH_LOG("failure: created idmapped mount with closed fd");
  833. }
  834. }
  835. /**
  836. * Validate that the initial user namespace is rejected.
  837. */
  838. TEST_F(mount_setattr_idmapped, invalid_fd_initial_userns)
  839. {
  840. int open_tree_fd = -EBADF;
  841. struct mount_attr attr = {
  842. .attr_set = MOUNT_ATTR_IDMAP,
  843. };
  844. if (!mount_setattr_supported())
  845. SKIP(return, "mount_setattr syscall not supported");
  846. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  847. AT_NO_AUTOMOUNT |
  848. AT_SYMLINK_NOFOLLOW |
  849. OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
  850. ASSERT_GE(open_tree_fd, 0);
  851. attr.userns_fd = open("/proc/1/ns/user", O_RDONLY | O_CLOEXEC);
  852. ASSERT_GE(attr.userns_fd, 0);
  853. ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  854. ASSERT_EQ(errno, EPERM);
  855. ASSERT_EQ(close(attr.userns_fd), 0);
  856. ASSERT_EQ(close(open_tree_fd), 0);
  857. }
  858. static int map_ids(pid_t pid, unsigned long nsid, unsigned long hostid,
  859. unsigned long range)
  860. {
  861. char map[100], procfile[256];
  862. snprintf(procfile, sizeof(procfile), "/proc/%d/uid_map", pid);
  863. snprintf(map, sizeof(map), "%lu %lu %lu", nsid, hostid, range);
  864. if (write_file(procfile, map, strlen(map)))
  865. return -1;
  866. snprintf(procfile, sizeof(procfile), "/proc/%d/gid_map", pid);
  867. snprintf(map, sizeof(map), "%lu %lu %lu", nsid, hostid, range);
  868. if (write_file(procfile, map, strlen(map)))
  869. return -1;
  870. return 0;
  871. }
  872. #define __STACK_SIZE (8 * 1024 * 1024)
  873. static pid_t do_clone(int (*fn)(void *), void *arg, int flags)
  874. {
  875. void *stack;
  876. stack = malloc(__STACK_SIZE);
  877. if (!stack)
  878. return -ENOMEM;
  879. #ifdef __ia64__
  880. return __clone2(fn, stack, __STACK_SIZE, flags | SIGCHLD, arg, NULL);
  881. #else
  882. return clone(fn, stack + __STACK_SIZE, flags | SIGCHLD, arg, NULL);
  883. #endif
  884. }
  885. static int get_userns_fd_cb(void *data)
  886. {
  887. return kill(getpid(), SIGSTOP);
  888. }
  889. static int wait_for_pid(pid_t pid)
  890. {
  891. int status, ret;
  892. again:
  893. ret = waitpid(pid, &status, 0);
  894. if (ret == -1) {
  895. if (errno == EINTR)
  896. goto again;
  897. return -1;
  898. }
  899. if (!WIFEXITED(status))
  900. return -1;
  901. return WEXITSTATUS(status);
  902. }
  903. static int get_userns_fd(unsigned long nsid, unsigned long hostid, unsigned long range)
  904. {
  905. int ret;
  906. pid_t pid;
  907. char path[256];
  908. pid = do_clone(get_userns_fd_cb, NULL, CLONE_NEWUSER);
  909. if (pid < 0)
  910. return -errno;
  911. ret = map_ids(pid, nsid, hostid, range);
  912. if (ret < 0)
  913. return ret;
  914. snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
  915. ret = open(path, O_RDONLY | O_CLOEXEC);
  916. kill(pid, SIGKILL);
  917. wait_for_pid(pid);
  918. return ret;
  919. }
  920. /**
  921. * Validate that an attached mount in our mount namespace cannot be idmapped.
  922. * (The kernel enforces that the mount's mount namespace and the caller's mount
  923. * namespace match.)
  924. */
  925. TEST_F(mount_setattr_idmapped, attached_mount_inside_current_mount_namespace)
  926. {
  927. int open_tree_fd = -EBADF;
  928. struct mount_attr attr = {
  929. .attr_set = MOUNT_ATTR_IDMAP,
  930. };
  931. if (!mount_setattr_supported())
  932. SKIP(return, "mount_setattr syscall not supported");
  933. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  934. AT_EMPTY_PATH |
  935. AT_NO_AUTOMOUNT |
  936. AT_SYMLINK_NOFOLLOW |
  937. OPEN_TREE_CLOEXEC);
  938. ASSERT_GE(open_tree_fd, 0);
  939. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  940. ASSERT_GE(attr.userns_fd, 0);
  941. ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  942. /*
  943. * Make sure that open_tree_attr() without OPEN_TREE_CLONE is not a way
  944. * to bypass this mount_setattr() restriction.
  945. */
  946. ASSERT_LT(sys_open_tree_attr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  947. ASSERT_EQ(close(attr.userns_fd), 0);
  948. ASSERT_EQ(close(open_tree_fd), 0);
  949. }
  950. /**
  951. * Validate that idmapping a mount is rejected if the mount's mount namespace
  952. * and our mount namespace don't match.
  953. * (The kernel enforces that the mount's mount namespace and the caller's mount
  954. * namespace match.)
  955. */
  956. TEST_F(mount_setattr_idmapped, attached_mount_outside_current_mount_namespace)
  957. {
  958. int open_tree_fd = -EBADF;
  959. struct mount_attr attr = {
  960. .attr_set = MOUNT_ATTR_IDMAP,
  961. };
  962. if (!mount_setattr_supported())
  963. SKIP(return, "mount_setattr syscall not supported");
  964. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  965. AT_EMPTY_PATH |
  966. AT_NO_AUTOMOUNT |
  967. AT_SYMLINK_NOFOLLOW |
  968. OPEN_TREE_CLOEXEC);
  969. ASSERT_GE(open_tree_fd, 0);
  970. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  971. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  972. ASSERT_GE(attr.userns_fd, 0);
  973. ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr,
  974. sizeof(attr)), 0);
  975. /*
  976. * Make sure that open_tree_attr() without OPEN_TREE_CLONE is not a way
  977. * to bypass this mount_setattr() restriction.
  978. */
  979. ASSERT_LT(sys_open_tree_attr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  980. ASSERT_EQ(close(attr.userns_fd), 0);
  981. ASSERT_EQ(close(open_tree_fd), 0);
  982. }
  983. /**
  984. * Validate that an attached mount in our mount namespace can be idmapped.
  985. */
  986. TEST_F(mount_setattr_idmapped, detached_mount_inside_current_mount_namespace)
  987. {
  988. int open_tree_fd = -EBADF;
  989. struct mount_attr attr = {
  990. .attr_set = MOUNT_ATTR_IDMAP,
  991. };
  992. if (!mount_setattr_supported())
  993. SKIP(return, "mount_setattr syscall not supported");
  994. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  995. AT_EMPTY_PATH |
  996. AT_NO_AUTOMOUNT |
  997. AT_SYMLINK_NOFOLLOW |
  998. OPEN_TREE_CLOEXEC |
  999. OPEN_TREE_CLONE);
  1000. ASSERT_GE(open_tree_fd, 0);
  1001. /* Changing mount properties on a detached mount. */
  1002. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  1003. ASSERT_GE(attr.userns_fd, 0);
  1004. ASSERT_EQ(sys_mount_setattr(open_tree_fd, "",
  1005. AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1006. ASSERT_EQ(close(attr.userns_fd), 0);
  1007. ASSERT_EQ(close(open_tree_fd), 0);
  1008. }
  1009. /**
  1010. * Validate that a detached mount not in our mount namespace can be idmapped.
  1011. */
  1012. TEST_F(mount_setattr_idmapped, detached_mount_outside_current_mount_namespace)
  1013. {
  1014. int open_tree_fd = -EBADF;
  1015. struct mount_attr attr = {
  1016. .attr_set = MOUNT_ATTR_IDMAP,
  1017. };
  1018. if (!mount_setattr_supported())
  1019. SKIP(return, "mount_setattr syscall not supported");
  1020. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  1021. AT_EMPTY_PATH |
  1022. AT_NO_AUTOMOUNT |
  1023. AT_SYMLINK_NOFOLLOW |
  1024. OPEN_TREE_CLOEXEC |
  1025. OPEN_TREE_CLONE);
  1026. ASSERT_GE(open_tree_fd, 0);
  1027. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  1028. /* Changing mount properties on a detached mount. */
  1029. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  1030. ASSERT_GE(attr.userns_fd, 0);
  1031. ASSERT_EQ(sys_mount_setattr(open_tree_fd, "",
  1032. AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1033. ASSERT_EQ(close(attr.userns_fd), 0);
  1034. ASSERT_EQ(close(open_tree_fd), 0);
  1035. }
  1036. static bool expected_uid_gid(int dfd, const char *path, int flags,
  1037. uid_t expected_uid, gid_t expected_gid)
  1038. {
  1039. int ret;
  1040. struct stat st;
  1041. ret = fstatat(dfd, path, &st, flags);
  1042. if (ret < 0)
  1043. return false;
  1044. return st.st_uid == expected_uid && st.st_gid == expected_gid;
  1045. }
  1046. /**
  1047. * Validate that currently changing the idmapping of an idmapped mount fails.
  1048. */
  1049. TEST_F(mount_setattr_idmapped, change_idmapping)
  1050. {
  1051. int open_tree_fd = -EBADF;
  1052. struct mount_attr attr = {
  1053. .attr_set = MOUNT_ATTR_IDMAP,
  1054. };
  1055. ASSERT_TRUE(expected_uid_gid(-EBADF, "/mnt/D", 0, 0, 0));
  1056. if (!mount_setattr_supported())
  1057. SKIP(return, "mount_setattr syscall not supported");
  1058. open_tree_fd = sys_open_tree(-EBADF, "/mnt/D",
  1059. AT_EMPTY_PATH |
  1060. AT_NO_AUTOMOUNT |
  1061. AT_SYMLINK_NOFOLLOW |
  1062. OPEN_TREE_CLOEXEC |
  1063. OPEN_TREE_CLONE);
  1064. ASSERT_GE(open_tree_fd, 0);
  1065. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  1066. ASSERT_GE(attr.userns_fd, 0);
  1067. ASSERT_EQ(sys_mount_setattr(open_tree_fd, "",
  1068. AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1069. ASSERT_EQ(close(attr.userns_fd), 0);
  1070. EXPECT_FALSE(expected_uid_gid(open_tree_fd, ".", 0, 0, 0));
  1071. EXPECT_TRUE(expected_uid_gid(open_tree_fd, ".", 0, 10000, 10000));
  1072. /* Change idmapping on a detached mount that is already idmapped. */
  1073. attr.userns_fd = get_userns_fd(0, 20000, 10000);
  1074. ASSERT_GE(attr.userns_fd, 0);
  1075. ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1076. /*
  1077. * Make sure that open_tree_attr() without OPEN_TREE_CLONE is not a way
  1078. * to bypass this mount_setattr() restriction.
  1079. */
  1080. EXPECT_LT(sys_open_tree_attr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1081. EXPECT_FALSE(expected_uid_gid(open_tree_fd, ".", 0, 20000, 20000));
  1082. EXPECT_TRUE(expected_uid_gid(open_tree_fd, ".", 0, 10000, 10000));
  1083. ASSERT_EQ(close(attr.userns_fd), 0);
  1084. ASSERT_EQ(close(open_tree_fd), 0);
  1085. }
  1086. TEST_F(mount_setattr_idmapped, idmap_mount_tree_invalid)
  1087. {
  1088. int open_tree_fd = -EBADF;
  1089. struct mount_attr attr = {
  1090. .attr_set = MOUNT_ATTR_IDMAP,
  1091. };
  1092. if (!mount_setattr_supported())
  1093. SKIP(return, "mount_setattr syscall not supported");
  1094. ASSERT_EQ(expected_uid_gid(-EBADF, "/tmp/B/b", 0, 0, 0), 0);
  1095. ASSERT_EQ(expected_uid_gid(-EBADF, "/tmp/B/BB/b", 0, 0, 0), 0);
  1096. ASSERT_EQ(mount("testing", "/mnt/A", "ramfs", MS_NOATIME | MS_NODEV,
  1097. "size=100000,mode=700"), 0);
  1098. ASSERT_EQ(mkdir("/mnt/A/AA", 0777), 0);
  1099. ASSERT_EQ(mount("/tmp", "/mnt/A/AA", NULL, MS_BIND | MS_REC, NULL), 0);
  1100. open_tree_fd = sys_open_tree(-EBADF, "/mnt/A",
  1101. AT_RECURSIVE |
  1102. AT_EMPTY_PATH |
  1103. AT_NO_AUTOMOUNT |
  1104. AT_SYMLINK_NOFOLLOW |
  1105. OPEN_TREE_CLOEXEC |
  1106. OPEN_TREE_CLONE);
  1107. ASSERT_GE(open_tree_fd, 0);
  1108. attr.userns_fd = get_userns_fd(0, 10000, 10000);
  1109. ASSERT_GE(attr.userns_fd, 0);
  1110. ASSERT_NE(sys_mount_setattr(open_tree_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr)), 0);
  1111. ASSERT_EQ(close(attr.userns_fd), 0);
  1112. ASSERT_EQ(close(open_tree_fd), 0);
  1113. ASSERT_EQ(expected_uid_gid(-EBADF, "/tmp/B/b", 0, 0, 0), 0);
  1114. ASSERT_EQ(expected_uid_gid(-EBADF, "/tmp/B/BB/b", 0, 0, 0), 0);
  1115. ASSERT_EQ(expected_uid_gid(open_tree_fd, "B/b", 0, 0, 0), 0);
  1116. ASSERT_EQ(expected_uid_gid(open_tree_fd, "B/BB/b", 0, 0, 0), 0);
  1117. (void)umount2("/mnt/A", MNT_DETACH);
  1118. }
  1119. TEST_F(mount_setattr, mount_attr_nosymfollow)
  1120. {
  1121. int fd;
  1122. unsigned int old_flags = 0, new_flags = 0, expected_flags = 0;
  1123. struct mount_attr attr = {
  1124. .attr_set = MOUNT_ATTR_NOSYMFOLLOW,
  1125. };
  1126. if (!mount_setattr_supported())
  1127. SKIP(return, "mount_setattr syscall not supported");
  1128. fd = open(NOSYMFOLLOW_SYMLINK, O_RDWR | O_CLOEXEC);
  1129. ASSERT_GT(fd, 0);
  1130. ASSERT_EQ(close(fd), 0);
  1131. old_flags = read_mnt_flags("/mnt/A");
  1132. ASSERT_GT(old_flags, 0);
  1133. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  1134. expected_flags = old_flags;
  1135. expected_flags |= ST_NOSYMFOLLOW;
  1136. new_flags = read_mnt_flags("/mnt/A");
  1137. ASSERT_EQ(new_flags, expected_flags);
  1138. new_flags = read_mnt_flags("/mnt/A/AA");
  1139. ASSERT_EQ(new_flags, expected_flags);
  1140. new_flags = read_mnt_flags("/mnt/A/AA/B");
  1141. ASSERT_EQ(new_flags, expected_flags);
  1142. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  1143. ASSERT_EQ(new_flags, expected_flags);
  1144. fd = open(NOSYMFOLLOW_SYMLINK, O_RDWR | O_CLOEXEC);
  1145. ASSERT_LT(fd, 0);
  1146. ASSERT_EQ(errno, ELOOP);
  1147. attr.attr_set &= ~MOUNT_ATTR_NOSYMFOLLOW;
  1148. attr.attr_clr |= MOUNT_ATTR_NOSYMFOLLOW;
  1149. ASSERT_EQ(sys_mount_setattr(-1, "/mnt/A", AT_RECURSIVE, &attr, sizeof(attr)), 0);
  1150. expected_flags &= ~ST_NOSYMFOLLOW;
  1151. new_flags = read_mnt_flags("/mnt/A");
  1152. ASSERT_EQ(new_flags, expected_flags);
  1153. new_flags = read_mnt_flags("/mnt/A/AA");
  1154. ASSERT_EQ(new_flags, expected_flags);
  1155. new_flags = read_mnt_flags("/mnt/A/AA/B");
  1156. ASSERT_EQ(new_flags, expected_flags);
  1157. new_flags = read_mnt_flags("/mnt/A/AA/B/BB");
  1158. ASSERT_EQ(new_flags, expected_flags);
  1159. fd = open(NOSYMFOLLOW_SYMLINK, O_RDWR | O_CLOEXEC);
  1160. ASSERT_GT(fd, 0);
  1161. ASSERT_EQ(close(fd), 0);
  1162. }
  1163. TEST_F(mount_setattr, open_tree_detached)
  1164. {
  1165. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1166. struct statx stx;
  1167. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1168. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1169. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1170. OPEN_TREE_CLONE);
  1171. ASSERT_GE(fd_tree_base, 0);
  1172. /*
  1173. * /mnt testing tmpfs
  1174. * |-/mnt/A testing tmpfs
  1175. * | `-/mnt/A/AA testing tmpfs
  1176. * | `-/mnt/A/AA/B testing tmpfs
  1177. * | `-/mnt/A/AA/B/BB testing tmpfs
  1178. * `-/mnt/B testing ramfs
  1179. */
  1180. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1181. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1182. ASSERT_EQ(statx(fd_tree_base, "A/AA", 0, 0, &stx), 0);
  1183. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1184. ASSERT_EQ(statx(fd_tree_base, "A/AA/B", 0, 0, &stx), 0);
  1185. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1186. ASSERT_EQ(statx(fd_tree_base, "A/AA/B/BB", 0, 0, &stx), 0);
  1187. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1188. fd_tree_subdir = sys_open_tree(fd_tree_base, "A/AA",
  1189. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1190. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1191. OPEN_TREE_CLONE);
  1192. ASSERT_GE(fd_tree_subdir, 0);
  1193. /*
  1194. * /AA testing tmpfs
  1195. * `-/AA/B testing tmpfs
  1196. * `-/AA/B/BB testing tmpfs
  1197. */
  1198. ASSERT_EQ(statx(fd_tree_subdir, "B", 0, 0, &stx), 0);
  1199. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1200. ASSERT_EQ(statx(fd_tree_subdir, "B/BB", 0, 0, &stx), 0);
  1201. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1202. ASSERT_EQ(move_mount(fd_tree_subdir, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1203. /*
  1204. * /tmp/target1 testing tmpfs
  1205. * `-/tmp/target1/B testing tmpfs
  1206. * `-/tmp/target1/B/BB testing tmpfs
  1207. */
  1208. ASSERT_EQ(statx(-EBADF, "/tmp/target1", 0, 0, &stx), 0);
  1209. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1210. ASSERT_EQ(statx(-EBADF, "/tmp/target1/B", 0, 0, &stx), 0);
  1211. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1212. ASSERT_EQ(statx(-EBADF, "/tmp/target1/B/BB", 0, 0, &stx), 0);
  1213. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1214. ASSERT_EQ(move_mount(fd_tree_base, "", -EBADF, "/tmp/target2", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1215. /*
  1216. * /tmp/target2 testing tmpfs
  1217. * |-/tmp/target2/A testing tmpfs
  1218. * | `-/tmp/target2/A/AA testing tmpfs
  1219. * | `-/tmp/target2/A/AA/B testing tmpfs
  1220. * | `-/tmp/target2/A/AA/B/BB testing tmpfs
  1221. * `-/tmp/target2/B testing ramfs
  1222. */
  1223. ASSERT_EQ(statx(-EBADF, "/tmp/target2", 0, 0, &stx), 0);
  1224. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1225. ASSERT_EQ(statx(-EBADF, "/tmp/target2/A", 0, 0, &stx), 0);
  1226. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1227. ASSERT_EQ(statx(-EBADF, "/tmp/target2/A/AA", 0, 0, &stx), 0);
  1228. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1229. ASSERT_EQ(statx(-EBADF, "/tmp/target2/A/AA/B", 0, 0, &stx), 0);
  1230. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1231. ASSERT_EQ(statx(-EBADF, "/tmp/target2/A/AA/B/BB", 0, 0, &stx), 0);
  1232. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1233. ASSERT_EQ(statx(-EBADF, "/tmp/target2/B", 0, 0, &stx), 0);
  1234. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1235. EXPECT_EQ(close(fd_tree_base), 0);
  1236. EXPECT_EQ(close(fd_tree_subdir), 0);
  1237. }
  1238. TEST_F(mount_setattr, open_tree_detached_fail)
  1239. {
  1240. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1241. struct statx stx;
  1242. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1243. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1244. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1245. OPEN_TREE_CLONE);
  1246. ASSERT_GE(fd_tree_base, 0);
  1247. /*
  1248. * /mnt testing tmpfs
  1249. * |-/mnt/A testing tmpfs
  1250. * | `-/mnt/A/AA testing tmpfs
  1251. * | `-/mnt/A/AA/B testing tmpfs
  1252. * | `-/mnt/A/AA/B/BB testing tmpfs
  1253. * `-/mnt/B testing ramfs
  1254. */
  1255. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1256. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1257. ASSERT_EQ(statx(fd_tree_base, "A/AA", 0, 0, &stx), 0);
  1258. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1259. ASSERT_EQ(statx(fd_tree_base, "A/AA/B", 0, 0, &stx), 0);
  1260. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1261. ASSERT_EQ(statx(fd_tree_base, "A/AA/B/BB", 0, 0, &stx), 0);
  1262. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1263. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  1264. /*
  1265. * The origin mount namespace of the anonymous mount namespace
  1266. * of @fd_tree_base doesn't match the caller's mount namespace
  1267. * anymore so creation of another detached mounts must fail.
  1268. */
  1269. fd_tree_subdir = sys_open_tree(fd_tree_base, "A/AA",
  1270. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1271. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1272. OPEN_TREE_CLONE);
  1273. ASSERT_LT(fd_tree_subdir, 0);
  1274. ASSERT_EQ(errno, EINVAL);
  1275. }
  1276. TEST_F(mount_setattr, open_tree_detached_fail2)
  1277. {
  1278. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1279. struct statx stx;
  1280. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1281. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1282. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1283. OPEN_TREE_CLONE);
  1284. ASSERT_GE(fd_tree_base, 0);
  1285. /*
  1286. * /mnt testing tmpfs
  1287. * |-/mnt/A testing tmpfs
  1288. * | `-/mnt/A/AA testing tmpfs
  1289. * | `-/mnt/A/AA/B testing tmpfs
  1290. * | `-/mnt/A/AA/B/BB testing tmpfs
  1291. * `-/mnt/B testing ramfs
  1292. */
  1293. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1294. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1295. ASSERT_EQ(statx(fd_tree_base, "A/AA", 0, 0, &stx), 0);
  1296. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1297. ASSERT_EQ(statx(fd_tree_base, "A/AA/B", 0, 0, &stx), 0);
  1298. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1299. ASSERT_EQ(statx(fd_tree_base, "A/AA/B/BB", 0, 0, &stx), 0);
  1300. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1301. EXPECT_EQ(create_and_enter_userns(), 0);
  1302. /*
  1303. * The caller entered a new user namespace. They will have
  1304. * CAP_SYS_ADMIN in this user namespace. However, they're still
  1305. * located in a mount namespace that is owned by an ancestor
  1306. * user namespace in which they hold no privilege. Creating a
  1307. * detached mount must thus fail.
  1308. */
  1309. fd_tree_subdir = sys_open_tree(fd_tree_base, "A/AA",
  1310. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1311. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1312. OPEN_TREE_CLONE);
  1313. ASSERT_LT(fd_tree_subdir, 0);
  1314. ASSERT_EQ(errno, EPERM);
  1315. }
  1316. TEST_F(mount_setattr, open_tree_detached_fail3)
  1317. {
  1318. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1319. struct statx stx;
  1320. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1321. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1322. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1323. OPEN_TREE_CLONE);
  1324. ASSERT_GE(fd_tree_base, 0);
  1325. /*
  1326. * /mnt testing tmpfs
  1327. * |-/mnt/A testing tmpfs
  1328. * | `-/mnt/A/AA testing tmpfs
  1329. * | `-/mnt/A/AA/B testing tmpfs
  1330. * | `-/mnt/A/AA/B/BB testing tmpfs
  1331. * `-/mnt/B testing ramfs
  1332. */
  1333. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1334. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1335. ASSERT_EQ(statx(fd_tree_base, "A/AA", 0, 0, &stx), 0);
  1336. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1337. ASSERT_EQ(statx(fd_tree_base, "A/AA/B", 0, 0, &stx), 0);
  1338. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1339. ASSERT_EQ(statx(fd_tree_base, "A/AA/B/BB", 0, 0, &stx), 0);
  1340. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1341. EXPECT_EQ(prepare_unpriv_mountns(), 0);
  1342. /*
  1343. * The caller entered a new mount namespace. They will have
  1344. * CAP_SYS_ADMIN in the owning user namespace of their mount
  1345. * namespace.
  1346. *
  1347. * However, the origin mount namespace of the anonymous mount
  1348. * namespace of @fd_tree_base doesn't match the caller's mount
  1349. * namespace anymore so creation of another detached mounts must
  1350. * fail.
  1351. */
  1352. fd_tree_subdir = sys_open_tree(fd_tree_base, "A/AA",
  1353. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1354. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1355. OPEN_TREE_CLONE);
  1356. ASSERT_LT(fd_tree_subdir, 0);
  1357. ASSERT_EQ(errno, EINVAL);
  1358. }
  1359. TEST_F(mount_setattr, open_tree_subfolder)
  1360. {
  1361. int fd_context, fd_tmpfs, fd_tree;
  1362. fd_context = sys_fsopen("tmpfs", 0);
  1363. ASSERT_GE(fd_context, 0);
  1364. ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
  1365. fd_tmpfs = sys_fsmount(fd_context, 0, 0);
  1366. ASSERT_GE(fd_tmpfs, 0);
  1367. EXPECT_EQ(close(fd_context), 0);
  1368. ASSERT_EQ(mkdirat(fd_tmpfs, "subdir", 0755), 0);
  1369. fd_tree = sys_open_tree(fd_tmpfs, "subdir",
  1370. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1371. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1372. OPEN_TREE_CLONE);
  1373. ASSERT_GE(fd_tree, 0);
  1374. EXPECT_EQ(close(fd_tmpfs), 0);
  1375. ASSERT_EQ(mkdirat(-EBADF, "/mnt/open_tree_subfolder", 0755), 0);
  1376. ASSERT_EQ(sys_move_mount(fd_tree, "", -EBADF, "/mnt/open_tree_subfolder", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1377. EXPECT_EQ(close(fd_tree), 0);
  1378. ASSERT_EQ(umount2("/mnt/open_tree_subfolder", 0), 0);
  1379. EXPECT_EQ(rmdir("/mnt/open_tree_subfolder"), 0);
  1380. }
  1381. TEST_F(mount_setattr, mount_detached_mount_on_detached_mount_then_close)
  1382. {
  1383. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1384. struct statx stx;
  1385. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1386. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1387. OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
  1388. ASSERT_GE(fd_tree_base, 0);
  1389. /*
  1390. * /mnt testing tmpfs
  1391. */
  1392. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1393. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1394. fd_tree_subdir = sys_open_tree(fd_tree_base, "",
  1395. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1396. AT_EMPTY_PATH | OPEN_TREE_CLOEXEC |
  1397. OPEN_TREE_CLONE);
  1398. ASSERT_GE(fd_tree_subdir, 0);
  1399. /*
  1400. * /mnt testing tmpfs
  1401. */
  1402. ASSERT_EQ(statx(fd_tree_subdir, "A", 0, 0, &stx), 0);
  1403. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1404. /*
  1405. * /mnt testing tmpfs
  1406. * `-/mnt testing tmpfs
  1407. */
  1408. ASSERT_EQ(move_mount(fd_tree_subdir, "", fd_tree_base, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1409. ASSERT_EQ(statx(fd_tree_subdir, "", AT_EMPTY_PATH, 0, &stx), 0);
  1410. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1411. ASSERT_NE(move_mount(fd_tree_subdir, "", fd_tree_base, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1412. EXPECT_EQ(close(fd_tree_base), 0);
  1413. EXPECT_EQ(close(fd_tree_subdir), 0);
  1414. }
  1415. TEST_F(mount_setattr, mount_detached_mount_on_detached_mount_and_attach)
  1416. {
  1417. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1418. struct statx stx;
  1419. __u64 mnt_id = 0;
  1420. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1421. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1422. OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
  1423. ASSERT_GE(fd_tree_base, 0);
  1424. /*
  1425. * /mnt testing tmpfs
  1426. */
  1427. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1428. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1429. fd_tree_subdir = sys_open_tree(fd_tree_base, "",
  1430. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1431. AT_EMPTY_PATH | OPEN_TREE_CLOEXEC |
  1432. OPEN_TREE_CLONE);
  1433. ASSERT_GE(fd_tree_subdir, 0);
  1434. /*
  1435. * /mnt testing tmpfs
  1436. */
  1437. ASSERT_EQ(statx(fd_tree_subdir, "A", 0, 0, &stx), 0);
  1438. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1439. /*
  1440. * /mnt testing tmpfs
  1441. * `-/mnt testing tmpfs
  1442. */
  1443. ASSERT_EQ(move_mount(fd_tree_subdir, "", fd_tree_base, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1444. ASSERT_EQ(statx(fd_tree_subdir, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &stx), 0);
  1445. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1446. ASSERT_TRUE(stx.stx_mask & STATX_MNT_ID_UNIQUE);
  1447. mnt_id = stx.stx_mnt_id;
  1448. ASSERT_NE(move_mount(fd_tree_subdir, "", fd_tree_base, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1449. ASSERT_EQ(move_mount(fd_tree_base, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1450. ASSERT_EQ(statx(-EBADF, "/tmp/target1", 0, STATX_MNT_ID_UNIQUE, &stx), 0);
  1451. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1452. ASSERT_TRUE(stx.stx_mask & STATX_MNT_ID_UNIQUE);
  1453. ASSERT_EQ(stx.stx_mnt_id, mnt_id);
  1454. EXPECT_EQ(close(fd_tree_base), 0);
  1455. EXPECT_EQ(close(fd_tree_subdir), 0);
  1456. }
  1457. TEST_F(mount_setattr, move_mount_detached_fail)
  1458. {
  1459. int fd_tree_base = -EBADF, fd_tree_subdir = -EBADF;
  1460. struct statx stx;
  1461. fd_tree_base = sys_open_tree(-EBADF, "/mnt",
  1462. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1463. OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
  1464. ASSERT_GE(fd_tree_base, 0);
  1465. /* Attach the mount to the caller's mount namespace. */
  1466. ASSERT_EQ(move_mount(fd_tree_base, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1467. ASSERT_EQ(statx(fd_tree_base, "A", 0, 0, &stx), 0);
  1468. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1469. fd_tree_subdir = sys_open_tree(-EBADF, "/tmp/B",
  1470. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1471. OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
  1472. ASSERT_GE(fd_tree_subdir, 0);
  1473. ASSERT_EQ(statx(fd_tree_subdir, "BB", 0, 0, &stx), 0);
  1474. ASSERT_FALSE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1475. /* Not allowed to move an attached mount to a detached mount. */
  1476. ASSERT_NE(move_mount(fd_tree_base, "", fd_tree_subdir, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1477. ASSERT_EQ(errno, EINVAL);
  1478. EXPECT_EQ(close(fd_tree_base), 0);
  1479. EXPECT_EQ(close(fd_tree_subdir), 0);
  1480. }
  1481. TEST_F(mount_setattr, attach_detached_mount_then_umount_then_close)
  1482. {
  1483. int fd_tree = -EBADF;
  1484. struct statx stx;
  1485. fd_tree = sys_open_tree(-EBADF, "/mnt",
  1486. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1487. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1488. OPEN_TREE_CLONE);
  1489. ASSERT_GE(fd_tree, 0);
  1490. ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx), 0);
  1491. /* We copied with AT_RECURSIVE so /mnt/A must be a mountpoint. */
  1492. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1493. /* Attach the mount to the caller's mount namespace. */
  1494. ASSERT_EQ(move_mount(fd_tree, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1495. ASSERT_EQ(statx(-EBADF, "/tmp/target1", 0, 0, &stx), 0);
  1496. ASSERT_TRUE(stx.stx_attributes & STATX_ATTR_MOUNT_ROOT);
  1497. ASSERT_EQ(umount2("/tmp/target1", MNT_DETACH), 0);
  1498. /*
  1499. * This tests whether dissolve_on_fput() handles a NULL mount
  1500. * namespace correctly, i.e., that it doesn't splat.
  1501. */
  1502. EXPECT_EQ(close(fd_tree), 0);
  1503. }
  1504. TEST_F(mount_setattr, mount_detached1_onto_detached2_then_close_detached1_then_mount_detached2_onto_attached)
  1505. {
  1506. int fd_tree1 = -EBADF, fd_tree2 = -EBADF;
  1507. /*
  1508. * |-/mnt/A testing tmpfs
  1509. * `-/mnt/A/AA testing tmpfs
  1510. * `-/mnt/A/AA/B testing tmpfs
  1511. * `-/mnt/A/AA/B/BB testing tmpfs
  1512. */
  1513. fd_tree1 = sys_open_tree(-EBADF, "/mnt/A",
  1514. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1515. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1516. OPEN_TREE_CLONE);
  1517. ASSERT_GE(fd_tree1, 0);
  1518. /*
  1519. * `-/mnt/B testing ramfs
  1520. */
  1521. fd_tree2 = sys_open_tree(-EBADF, "/mnt/B",
  1522. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1523. AT_EMPTY_PATH | OPEN_TREE_CLOEXEC |
  1524. OPEN_TREE_CLONE);
  1525. ASSERT_GE(fd_tree2, 0);
  1526. /*
  1527. * Move the source detached mount tree to the target detached
  1528. * mount tree. This will move all the mounts in the source mount
  1529. * tree from the source anonymous mount namespace to the target
  1530. * anonymous mount namespace.
  1531. *
  1532. * The source detached mount tree and the target detached mount
  1533. * tree now both refer to the same anonymous mount namespace.
  1534. *
  1535. * |-"" testing ramfs
  1536. * `-"" testing tmpfs
  1537. * `-""/AA testing tmpfs
  1538. * `-""/AA/B testing tmpfs
  1539. * `-""/AA/B/BB testing tmpfs
  1540. */
  1541. ASSERT_EQ(move_mount(fd_tree1, "", fd_tree2, "", MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH), 0);
  1542. /*
  1543. * The source detached mount tree @fd_tree1 is now an attached
  1544. * mount, i.e., it has a parent. Specifically, it now has the
  1545. * root mount of the mount tree of @fd_tree2 as its parent.
  1546. *
  1547. * That means we are no longer allowed to attach it as we only
  1548. * allow attaching the root of an anonymous mount tree, not
  1549. * random bits and pieces. Verify that the kernel enforces this.
  1550. */
  1551. ASSERT_NE(move_mount(fd_tree1, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1552. /*
  1553. * Closing the source detached mount tree must not unmount and
  1554. * free the shared anonymous mount namespace. The kernel will
  1555. * quickly yell at us because the anonymous mount namespace
  1556. * won't be empty when it's freed.
  1557. */
  1558. EXPECT_EQ(close(fd_tree1), 0);
  1559. /*
  1560. * Attach the mount tree to a non-anonymous mount namespace.
  1561. * This can only succeed if closing fd_tree1 had proper
  1562. * semantics and didn't cause the anonymous mount namespace to
  1563. * be freed. If it did this will trigger a UAF which will be
  1564. * visible on any KASAN enabled kernel.
  1565. *
  1566. * |-/tmp/target1 testing ramfs
  1567. * `-/tmp/target1 testing tmpfs
  1568. * `-/tmp/target1/AA testing tmpfs
  1569. * `-/tmp/target1/AA/B testing tmpfs
  1570. * `-/tmp/target1/AA/B/BB testing tmpfs
  1571. */
  1572. ASSERT_EQ(move_mount(fd_tree2, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1573. EXPECT_EQ(close(fd_tree2), 0);
  1574. }
  1575. TEST_F(mount_setattr, two_detached_mounts_referring_to_same_anonymous_mount_namespace)
  1576. {
  1577. int fd_tree1 = -EBADF, fd_tree2 = -EBADF;
  1578. /*
  1579. * Copy the following mount tree:
  1580. *
  1581. * |-/mnt/A testing tmpfs
  1582. * `-/mnt/A/AA testing tmpfs
  1583. * `-/mnt/A/AA/B testing tmpfs
  1584. * `-/mnt/A/AA/B/BB testing tmpfs
  1585. */
  1586. fd_tree1 = sys_open_tree(-EBADF, "/mnt/A",
  1587. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1588. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1589. OPEN_TREE_CLONE);
  1590. ASSERT_GE(fd_tree1, 0);
  1591. /*
  1592. * Create an O_PATH file descriptors with a separate struct file
  1593. * that refers to the same detached mount tree as @fd_tree1
  1594. */
  1595. fd_tree2 = sys_open_tree(fd_tree1, "",
  1596. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1597. AT_EMPTY_PATH | OPEN_TREE_CLOEXEC);
  1598. ASSERT_GE(fd_tree2, 0);
  1599. /*
  1600. * Copy the following mount tree:
  1601. *
  1602. * |-/tmp/target1 testing tmpfs
  1603. * `-/tmp/target1/AA testing tmpfs
  1604. * `-/tmp/target1/AA/B testing tmpfs
  1605. * `-/tmp/target1/AA/B/BB testing tmpfs
  1606. */
  1607. ASSERT_EQ(move_mount(fd_tree2, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1608. /*
  1609. * This must fail as this would mean adding the same mount tree
  1610. * into the same mount tree.
  1611. */
  1612. ASSERT_NE(move_mount(fd_tree1, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1613. }
  1614. TEST_F(mount_setattr, two_detached_subtrees_of_same_anonymous_mount_namespace)
  1615. {
  1616. int fd_tree1 = -EBADF, fd_tree2 = -EBADF;
  1617. /*
  1618. * Copy the following mount tree:
  1619. *
  1620. * |-/mnt/A testing tmpfs
  1621. * `-/mnt/A/AA testing tmpfs
  1622. * `-/mnt/A/AA/B testing tmpfs
  1623. * `-/mnt/A/AA/B/BB testing tmpfs
  1624. */
  1625. fd_tree1 = sys_open_tree(-EBADF, "/mnt/A",
  1626. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1627. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1628. OPEN_TREE_CLONE);
  1629. ASSERT_GE(fd_tree1, 0);
  1630. /*
  1631. * Create an O_PATH file descriptors with a separate struct file that
  1632. * refers to a subtree of the same detached mount tree as @fd_tree1
  1633. */
  1634. fd_tree2 = sys_open_tree(fd_tree1, "AA",
  1635. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1636. AT_EMPTY_PATH | OPEN_TREE_CLOEXEC);
  1637. ASSERT_GE(fd_tree2, 0);
  1638. /*
  1639. * This must fail as it is only possible to attach the root of a
  1640. * detached mount tree.
  1641. */
  1642. ASSERT_NE(move_mount(fd_tree2, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1643. ASSERT_EQ(move_mount(fd_tree1, "", -EBADF, "/tmp/target1", MOVE_MOUNT_F_EMPTY_PATH), 0);
  1644. }
  1645. TEST_F(mount_setattr, detached_tree_propagation)
  1646. {
  1647. int fd_tree = -EBADF;
  1648. struct statx stx1, stx2, stx3, stx4;
  1649. ASSERT_EQ(unshare(CLONE_NEWNS), 0);
  1650. ASSERT_EQ(mount(NULL, "/mnt", NULL, MS_REC | MS_SHARED, NULL), 0);
  1651. /*
  1652. * Copy the following mount tree:
  1653. *
  1654. * /mnt testing tmpfs
  1655. * |-/mnt/A testing tmpfs
  1656. * | `-/mnt/A/AA testing tmpfs
  1657. * | `-/mnt/A/AA/B testing tmpfs
  1658. * | `-/mnt/A/AA/B/BB testing tmpfs
  1659. * `-/mnt/B testing ramfs
  1660. */
  1661. fd_tree = sys_open_tree(-EBADF, "/mnt",
  1662. AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW |
  1663. AT_RECURSIVE | OPEN_TREE_CLOEXEC |
  1664. OPEN_TREE_CLONE);
  1665. ASSERT_GE(fd_tree, 0);
  1666. ASSERT_EQ(statx(-EBADF, "/mnt/A", 0, 0, &stx1), 0);
  1667. ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx2), 0);
  1668. /*
  1669. * Copying the mount namespace like done above doesn't alter the
  1670. * mounts in any way so the filesystem mounted on /mnt must be
  1671. * identical even though the mounts will differ. Use the device
  1672. * information to verify that. Note that tmpfs will have a 0
  1673. * major number so comparing the major number is misleading.
  1674. */
  1675. ASSERT_EQ(stx1.stx_dev_minor, stx2.stx_dev_minor);
  1676. /* Mount a tmpfs filesystem over /mnt/A. */
  1677. ASSERT_EQ(mount(NULL, "/mnt/A", "tmpfs", 0, NULL), 0);
  1678. ASSERT_EQ(statx(-EBADF, "/mnt/A", 0, 0, &stx3), 0);
  1679. ASSERT_EQ(statx(fd_tree, "A", 0, 0, &stx4), 0);
  1680. /*
  1681. * A new filesystem has been mounted on top of /mnt/A which
  1682. * means that the device information will be different for any
  1683. * statx() that was taken from /mnt/A before the mount compared
  1684. * to one after the mount.
  1685. */
  1686. ASSERT_NE(stx1.stx_dev_minor, stx3.stx_dev_minor);
  1687. ASSERT_EQ(stx1.stx_dev_minor, stx4.stx_dev_minor);
  1688. EXPECT_EQ(close(fd_tree), 0);
  1689. }
  1690. TEST_HARNESS_MAIN