1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Zilogic Systems Pvt. Ltd., 2018
4f08c3bdfSopenharmony_ci * Email: code@zilogic.com
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test statx syscall with STATX_ATTR_ENCRYPTED flag, setting a key is required
11f08c3bdfSopenharmony_ci * for the file to be encrypted by the filesystem.
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * e4crypt is used to set the encrypt flag (currently supported only by ext4).
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * Two directories are tested.
16f08c3bdfSopenharmony_ci * First directory has all flags set.
17f08c3bdfSopenharmony_ci * Second directory has no flags set.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci * Minimum e2fsprogs version required is 1.43.
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define _GNU_SOURCE
23f08c3bdfSopenharmony_ci#include <stdlib.h>
24f08c3bdfSopenharmony_ci#include <stdio.h>
25f08c3bdfSopenharmony_ci#include <sys/types.h>
26f08c3bdfSopenharmony_ci#include <sys/wait.h>
27f08c3bdfSopenharmony_ci#include "tst_test.h"
28f08c3bdfSopenharmony_ci#include "lapi/fs.h"
29f08c3bdfSopenharmony_ci#include "lapi/stat.h"
30f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#define MNTPOINT "mnt_point"
33f08c3bdfSopenharmony_ci#define TESTDIR_FLAGGED MNTPOINT"/test_dir1"
34f08c3bdfSopenharmony_ci#define TESTDIR_UNFLAGGED MNTPOINT"/test_dir2"
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic int mount_flag;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void test_flagged(void)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct statx buf;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	TEST(statx(AT_FDCWD, TESTDIR_FLAGGED, 0, 0, &buf));
43f08c3bdfSopenharmony_ci	if (TST_RET == 0)
44f08c3bdfSopenharmony_ci		tst_res(TPASS,
45f08c3bdfSopenharmony_ci			"sys_statx(AT_FDCWD, %s, 0, 0, &buf)", TESTDIR_FLAGGED);
46f08c3bdfSopenharmony_ci	else
47f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO,
48f08c3bdfSopenharmony_ci			"sys_statx(AT_FDCWD, %s, 0, 0, &buf)", TESTDIR_FLAGGED);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	if (buf.stx_attributes & STATX_ATTR_ENCRYPTED)
51f08c3bdfSopenharmony_ci		tst_res(TPASS, "STATX_ATTR_ENCRYPTED flag is set");
52f08c3bdfSopenharmony_ci	else
53f08c3bdfSopenharmony_ci		tst_res(TFAIL, "STATX_ATTR_ENCRYPTED flag is not set");
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void test_unflagged(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	struct statx buf;
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	TEST(statx(AT_FDCWD, TESTDIR_UNFLAGGED, 0, 0, &buf));
61f08c3bdfSopenharmony_ci	if (TST_RET == 0)
62f08c3bdfSopenharmony_ci		tst_res(TPASS,
63f08c3bdfSopenharmony_ci			"sys_statx(AT_FDCWD, %s, 0, 0, &buf)",
64f08c3bdfSopenharmony_ci			TESTDIR_UNFLAGGED);
65f08c3bdfSopenharmony_ci	else
66f08c3bdfSopenharmony_ci		tst_brk(TFAIL | TTERRNO,
67f08c3bdfSopenharmony_ci			"sys_statx(AT_FDCWD, %s, 0, 0, &buf)",
68f08c3bdfSopenharmony_ci			TESTDIR_UNFLAGGED);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	if ((buf.stx_attributes & STATX_ATTR_ENCRYPTED) == 0)
71f08c3bdfSopenharmony_ci		tst_res(TPASS, "STATX_ATTR_ENCRYPTED flag is not set");
72f08c3bdfSopenharmony_ci	else
73f08c3bdfSopenharmony_ci		tst_res(TFAIL, "STATX_ATTR_ENCRYPTED flag is set");
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic struct test_cases {
77f08c3bdfSopenharmony_ci	void (*tfunc)(void);
78f08c3bdfSopenharmony_ci} tcases[] = {
79f08c3bdfSopenharmony_ci	{&test_flagged},
80f08c3bdfSopenharmony_ci	{&test_unflagged},
81f08c3bdfSopenharmony_ci};
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_cistatic void run(unsigned int i)
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	tcases[i].tfunc();
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic void setup(void)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	char opt_bsize[32];
91f08c3bdfSopenharmony_ci	const char *const fs_opts[] = {"-O encrypt", opt_bsize, NULL};
92f08c3bdfSopenharmony_ci	int ret;
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	snprintf(opt_bsize, sizeof(opt_bsize), "-b %i", getpagesize());
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
97f08c3bdfSopenharmony_ci	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, 0);
98f08c3bdfSopenharmony_ci	mount_flag = 1;
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR_FLAGGED, 0777);
101f08c3bdfSopenharmony_ci	SAFE_MKDIR(TESTDIR_UNFLAGGED, 0777);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	ret = tst_system("echo qwery | e4crypt add_key "TESTDIR_FLAGGED);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	if (ret)
106f08c3bdfSopenharmony_ci		tst_brk(TCONF, "e4crypt failed (CONFIG_EXT4_ENCRYPTION not set?)");
107f08c3bdfSopenharmony_ci}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_cistatic void cleanup(void)
110f08c3bdfSopenharmony_ci{
111f08c3bdfSopenharmony_ci	if (mount_flag)
112f08c3bdfSopenharmony_ci		tst_umount(MNTPOINT);
113f08c3bdfSopenharmony_ci}
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_cistatic struct tst_test test = {
116f08c3bdfSopenharmony_ci	.test = run,
117f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
118f08c3bdfSopenharmony_ci	.setup = setup,
119f08c3bdfSopenharmony_ci	.cleanup = cleanup,
120f08c3bdfSopenharmony_ci	.min_kver = "4.11",
121f08c3bdfSopenharmony_ci	.needs_root = 1,
122f08c3bdfSopenharmony_ci	.needs_device = 1,
123f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
124f08c3bdfSopenharmony_ci	.dev_fs_type = "ext4",
125f08c3bdfSopenharmony_ci	.needs_cmds = (const char *[]) {
126f08c3bdfSopenharmony_ci		"mkfs.ext4 >= 1.43.0",
127f08c3bdfSopenharmony_ci		"e4crypt",
128f08c3bdfSopenharmony_ci		NULL
129f08c3bdfSopenharmony_ci	}
130f08c3bdfSopenharmony_ci};
131