1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
4 * Author: Dai Shili <daisl.fnst@fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * This code tests if STATX_ATTR_VERITY flag in the statx attributes is set correctly.
11 *
12 * The statx() system call sets STATX_ATTR_VERITY if the file has fs-verity
13 * enabled. This can perform better than FS_IOC_GETFLAGS and
14 * FS_IOC_MEASURE_VERITY because it doesn't require opening the file, and
15 * opening verity files can be expensive.
16 *
17 * Minimum Linux version required is v5.5.
18 */
19
20 #define _GNU_SOURCE
21 #include <sys/mount.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "tst_test.h"
25 #include "lapi/fs.h"
26 #include "lapi/fsverity.h"
27 #include "lapi/stat.h"
28 #include "lapi/fcntl.h"
29 #include <inttypes.h>
30
31 #define MNTPOINT "mnt_point"
32 #define TESTFILE_FLAGGED MNTPOINT"/test_file1"
33 #define TESTFILE_UNFLAGGED MNTPOINT"/test_file2"
34
35 static int mount_flag;
36
37 static const uint32_t hash_algorithms[] = {
38 FS_VERITY_HASH_ALG_SHA256,
39 };
40
test_flagged(void)41 static void test_flagged(void)
42 {
43 struct statx buf;
44
45 TST_EXP_PASS(statx(AT_FDCWD, TESTFILE_FLAGGED, 0, 0, &buf),
46 "statx(AT_FDCWD, %s, 0, 0, &buf)", TESTFILE_FLAGGED);
47
48 if (buf.stx_attributes & STATX_ATTR_VERITY)
49 tst_res(TPASS, "STATX_ATTR_VERITY flag is set: (%"PRIu64") ",
50 (uint64_t)buf.stx_attributes);
51 else
52 tst_res(TFAIL, "STATX_ATTR_VERITY flag is not set");
53 }
54
test_unflagged(void)55 static void test_unflagged(void)
56 {
57 struct statx buf;
58
59 TST_EXP_PASS(statx(AT_FDCWD, TESTFILE_UNFLAGGED, 0, 0, &buf),
60 "statx(AT_FDCWD, %s, 0, 0, &buf)", TESTFILE_UNFLAGGED);
61
62 if ((buf.stx_attributes & STATX_ATTR_VERITY) == 0)
63 tst_res(TPASS, "STATX_ATTR_VERITY flag is not set");
64 else
65 tst_res(TFAIL, "STATX_ATTR_VERITY flag is set");
66 }
67
68 static struct test_cases {
69 void (*tfunc)(void);
70 } tcases[] = {
71 {&test_flagged},
72 {&test_unflagged},
73 };
74
run(unsigned int i)75 static void run(unsigned int i)
76 {
77 tcases[i].tfunc();
78 }
79
flag_setup(void)80 static void flag_setup(void)
81 {
82 int fd, attr, ret;
83 struct fsverity_enable_arg enable;
84 struct stat statbuf;
85
86 fd = SAFE_OPEN(TESTFILE_FLAGGED, O_RDONLY, 0664);
87 SAFE_FSTAT(fd, &statbuf);
88
89 ret = ioctl(fd, FS_IOC_GETFLAGS, &attr);
90 if (ret < 0) {
91 if (errno == ENOTTY)
92 tst_brk(TCONF | TERRNO, "FS_IOC_GETFLAGS not supported");
93
94 tst_brk(TBROK | TERRNO, "ioctl(%i, FS_IOC_GETFLAGS, ...)", fd);
95 }
96
97 memset(&enable, 0, sizeof(enable));
98 enable.version = 1;
99 enable.hash_algorithm = hash_algorithms[0];
100 enable.block_size = statbuf.st_blksize;
101 enable.salt_size = 0;
102 enable.salt_ptr = (intptr_t)NULL;
103 enable.sig_size = 0;
104 enable.sig_ptr = (intptr_t)NULL;
105
106 ret = ioctl(fd, FS_IOC_ENABLE_VERITY, &enable);
107 if (ret < 0) {
108 if (errno == EOPNOTSUPP) {
109 tst_brk(TCONF,
110 "fs-verity is not supported on the file system or by the kernel");
111 }
112 tst_brk(TBROK | TERRNO, "ioctl(%i, FS_IOC_ENABLE_VERITY) failed", fd);
113 }
114
115 ret = ioctl(fd, FS_IOC_GETFLAGS, &attr);
116 if ((ret == 0) && !(attr & FS_VERITY_FL))
117 tst_res(TFAIL, "%i: fs-verity enabled but FS_VERITY_FL bit not set", fd);
118
119 SAFE_CLOSE(fd);
120 }
121
setup(void)122 static void setup(void)
123 {
124 char opt_bsize[32];
125 const char *const fs_opts[] = {"-O verity", opt_bsize, NULL};
126
127 snprintf(opt_bsize, sizeof(opt_bsize), "-b %i", getpagesize());
128 SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
129
130 TEST(mount(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL));
131 if (TST_RET) {
132 if (TST_ERR == EINVAL)
133 tst_brk(TCONF, "fs-verity not supported on loopdev");
134
135 tst_brk(TBROK | TERRNO, "mount() failed with %ld", TST_RET);
136 }
137 mount_flag = 1;
138
139 SAFE_FILE_PRINTF(TESTFILE_FLAGGED, "a");
140 SAFE_FILE_PRINTF(TESTFILE_UNFLAGGED, "a");
141
142 flag_setup();
143 }
144
cleanup(void)145 static void cleanup(void)
146 {
147 if (mount_flag)
148 tst_umount(MNTPOINT);
149 }
150
151 static struct tst_test test = {
152 .test = run,
153 .tcnt = ARRAY_SIZE(tcases),
154 .setup = setup,
155 .cleanup = cleanup,
156 .needs_root = 1,
157 .needs_device = 1,
158 .mntpoint = MNTPOINT,
159 .dev_fs_type = "ext4",
160 .needs_kconfigs = (const char *[]) {
161 "CONFIG_FS_VERITY",
162 NULL
163 },
164 .needs_cmds = (const char *[]) {
165 "mkfs.ext4 >= 1.45.2",
166 NULL
167 }
168 };
169