1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Red Hat, Inc.
4f08c3bdfSopenharmony_ci * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Tests an unbindable mount: unbindable mount is an unbindable
11f08c3bdfSopenharmony_ci * private mount.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - Creates directories DIRA, DIRB and files DIRA/"A", DIRB/"B"
14f08c3bdfSopenharmony_ci * - Unshares mount namespace and makes it private (so mounts/umounts have no
15f08c3bdfSopenharmony_ci *   effect on a real system)
16f08c3bdfSopenharmony_ci * - Bind mounts directory DIRA to DIRA
17f08c3bdfSopenharmony_ci * - Makes directory DIRA unbindable
18f08c3bdfSopenharmony_ci * - Check if bind mount unbindable DIRA to DIRB fails as expected
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <sys/wait.h>
22f08c3bdfSopenharmony_ci#include <sys/mount.h>
23f08c3bdfSopenharmony_ci#include "mountns.h"
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci#include "lapi/sched.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic void run(void)
28f08c3bdfSopenharmony_ci{
29f08c3bdfSopenharmony_ci	SAFE_UNSHARE(CLONE_NEWNS);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	/* makes sure mounts/umounts have no effect on a real system */
32f08c3bdfSopenharmony_ci	SAFE_MOUNT("none", "/", "none", MS_REC | MS_PRIVATE, NULL);
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	SAFE_MOUNT(DIRA, DIRA, "none", MS_BIND, NULL);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	SAFE_MOUNT("none", DIRA, "none", MS_UNBINDABLE, NULL);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	if (mount(DIRA, DIRB, "none", MS_BIND, NULL) == -1) {
39f08c3bdfSopenharmony_ci		tst_res(TPASS, "unbindable mount passed");
40f08c3bdfSopenharmony_ci	} else {
41f08c3bdfSopenharmony_ci		SAFE_UMOUNT(DIRB);
42f08c3bdfSopenharmony_ci		tst_res(TFAIL, "unbindable mount faled");
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	SAFE_UMOUNT(DIRA);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void setup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	create_folders();
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void cleanup(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	umount_folders();
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic struct tst_test test = {
59f08c3bdfSopenharmony_ci	.setup = setup,
60f08c3bdfSopenharmony_ci	.cleanup = cleanup,
61f08c3bdfSopenharmony_ci	.test_all = run,
62f08c3bdfSopenharmony_ci	.needs_root = 1,
63f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
64f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
65f08c3bdfSopenharmony_ci		"CONFIG_USER_NS",
66f08c3bdfSopenharmony_ci		NULL,
67f08c3bdfSopenharmony_ci	},
68f08c3bdfSopenharmony_ci};
69