xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/squashfs/squashfs01.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) 2021 Joerg Vehlow <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Kernel commits
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * - f37aa4c7366 (squashfs: add more sanity checks in id lookup)
12*49cdfc7eSAndroid Build Coastguard Worker  * - eabac19e40c (squashfs: add more sanity checks in inode lookup)
13*49cdfc7eSAndroid Build Coastguard Worker  * - 506220d2ba2 (squashfs: add more sanity checks in xattr id lookup)
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * added some sanity checks, that verify the size of
16*49cdfc7eSAndroid Build Coastguard Worker  * inode lookup, id (uid/gid) and xattr blocks in the squashfs,
17*49cdfc7eSAndroid Build Coastguard Worker  * but broke mounting filesystems with completely filled blocks.
18*49cdfc7eSAndroid Build Coastguard Worker  * A block has a max size of 8192.
19*49cdfc7eSAndroid Build Coastguard Worker  * An inode lookup entry has an uncompressed size of 8 bytes,
20*49cdfc7eSAndroid Build Coastguard Worker  * an id block 4 bytes and an xattr block 16 bytes.
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * To fill up at least one block for each of the three tables,
24*49cdfc7eSAndroid Build Coastguard Worker  * 2048 files with unique uid/gid and xattr are created.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * The bugs are fixed in kernel commits
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * - c1b2028315c (squashfs: fix inode lookup sanity checks)
30*49cdfc7eSAndroid Build Coastguard Worker  * - 8b44ca2b634 (squashfs: fix xattr id and id lookup sanity checks)
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static const char *MOUNT_DIR = "mnt";
40*49cdfc7eSAndroid Build Coastguard Worker static const char *DATA_DIR = "data";
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static int mounted;
43*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	if (mounted)
47*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_UMOUNT("mnt");
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	int i;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Test squashfs sanity check regressions");
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(DATA_DIR, 0777);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 2048; ++i) {
59*49cdfc7eSAndroid Build Coastguard Worker 		int fd;
60*49cdfc7eSAndroid Build Coastguard Worker 		char name[20];
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(name, "%s/%d", DATA_DIR, i);
63*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_OPEN(name, O_CREAT | O_EXCL, 0666);
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FCHOWN(fd, i, i);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 		/* This must be either "security", "user" or "trusted" namespace,
67*49cdfc7eSAndroid Build Coastguard Worker 		 * because squashfs cannot store other namespaces.
68*49cdfc7eSAndroid Build Coastguard Worker 		 * Since the files are most likely created on a tmpfs,
69*49cdfc7eSAndroid Build Coastguard Worker 		 * "user" namespace is not possible, because it is not allowed.
70*49cdfc7eSAndroid Build Coastguard Worker 		 */
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FSETXATTR(fd, "security.x", &i, sizeof(i), 0);
72*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	/* Create squashfs without any compression.
76*49cdfc7eSAndroid Build Coastguard Worker 	 * This allows reasoning about block sizes.
77*49cdfc7eSAndroid Build Coastguard Worker 	 * Redirect stdout, to get rid of undefined uid messages
78*49cdfc7eSAndroid Build Coastguard Worker 	 */
79*49cdfc7eSAndroid Build Coastguard Worker 	const char *argv[] = {
80*49cdfc7eSAndroid Build Coastguard Worker 		"mksquashfs", DATA_DIR, tst_device->dev,
81*49cdfc7eSAndroid Build Coastguard Worker 		"-noappend", "-noI", "-noD", "-noX", "-noF", NULL
82*49cdfc7eSAndroid Build Coastguard Worker 	};
83*49cdfc7eSAndroid Build Coastguard Worker 	tst_cmd(argv, "/dev/null", NULL, 0);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(MOUNT_DIR, 0777);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
run(void)88*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	if (mount(tst_device->dev, MOUNT_DIR, "squashfs", 0, NULL) != 0)
91*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO, "Mount failed");
92*49cdfc7eSAndroid Build Coastguard Worker 	mounted = 1;
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UMOUNT("mnt");
95*49cdfc7eSAndroid Build Coastguard Worker 	mounted = 0;
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Regression not detected");
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
101*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
102*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
103*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
104*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
105*49cdfc7eSAndroid Build Coastguard Worker 	.needs_device = 1,
106*49cdfc7eSAndroid Build Coastguard Worker 	.dev_min_size = 1,
107*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cmds = (const char *const []) {
108*49cdfc7eSAndroid Build Coastguard Worker 		"mksquashfs",
109*49cdfc7eSAndroid Build Coastguard Worker 		NULL
110*49cdfc7eSAndroid Build Coastguard Worker 	},
111*49cdfc7eSAndroid Build Coastguard Worker 	.needs_drivers = (const char *const []) {
112*49cdfc7eSAndroid Build Coastguard Worker 		"squashfs",
113*49cdfc7eSAndroid Build Coastguard Worker 		NULL
114*49cdfc7eSAndroid Build Coastguard Worker 	},
115*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
116*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "c1b2028315c"},
117*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "8b44ca2b634"},
118*49cdfc7eSAndroid Build Coastguard Worker 		{}
119*49cdfc7eSAndroid Build Coastguard Worker 	},
120*49cdfc7eSAndroid Build Coastguard Worker };
121