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