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