File: ioctl_ubi.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (303 lines) | stat: -rw-r--r-- 8,906 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Check decoding of UBI ioctl commands.
 *
 * Copyright (c) 2016-2024 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <mtd/ubi-user.h>

static const unsigned long long llmagic = 0xdeadbeefbadc0dedULL;
static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
static const unsigned int imagic = 0xdeadbeef;

static const char *errstr;

static int
do_ioctl(kernel_ulong_t cmd, kernel_ulong_t arg)
{
	int rc = ioctl(-1, cmd, arg);
	errstr = sprintrc(rc);

#ifdef INJECT_RETVAL
	if (rc != INJECT_RETVAL)
		error_msg_and_fail("Return value [%d] does not match"
				   " expectations [%d]", rc, INJECT_RETVAL);

	static char inj_errstr[4096];

	snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr);
	errstr = inj_errstr;
#endif

	return rc;
}

static int
do_ioctl_ptr(kernel_ulong_t cmd, const void *arg)
{
	return do_ioctl(cmd, (uintptr_t) arg);
}

#ifdef INJECT_RETVAL
static void
skip_ioctls(int argc, const char *argv[])
{
	if (argc < 2)
		error_msg_and_fail("Usage: %s NUM_SKIP", argv[0]);

	unsigned long num_skip = strtoul(argv[1], NULL, 0);

	for (size_t i = 0; i < num_skip; ++i) {
		int rc = ioctl(-1, UBI_IOCATT, 0);
		printf("ioctl(-1, UBI_IOCATT, NULL) = %s%s\n", sprintrc(rc),
		       rc == INJECT_RETVAL ? " (INJECTED)" : "");
		if (rc == INJECT_RETVAL)
			return;
	}

	error_msg_and_fail("Issued %lu ioctl syscalls but failed"
			   " to detect an injected return code %d",
			   num_skip, INJECT_RETVAL);
}
#endif /* INJECT_RETVAL */

int
main(int argc, const char *argv[])
{
#ifdef INJECT_RETVAL
	skip_ioctls(argc, argv);
#endif

	static const struct {
		unsigned int cmd;
		const char *str;
	}
	noarg_cmds[] = {
		{ ARG_STR(UBI_IOCVOLCRBLK) },
		{ ARG_STR(UBI_IOCVOLRMBLK) },
	},
	pint_cmds[] = {
		{ ARG_STR(UBI_IOCDET) },
		{ ARG_STR(UBI_IOCEBER) },
		{ ARG_STR(UBI_IOCEBISMAP) },
		{ ARG_STR(UBI_IOCEBUNMAP) },
		{ ARG_STR(UBI_IOCRMVOL) },
		{ ARG_STR(UBI_IOCRPEB) },
		{ ARG_STR(UBI_IOCSPEB) },
	}, pint64_cmds[] = {
		{ ARG_STR(UBI_IOCVOLUP) },
	}, ptr_cmds[] = {
		{ ARG_STR(UBI_IOCATT) },
		{ ARG_STR(UBI_IOCEBCH) },
		{ ARG_STR(UBI_IOCEBMAP) },
		{ ARG_STR(UBI_IOCMKVOL) },
		{ ARG_STR(UBI_IOCRNVOL) },
		{ ARG_STR(UBI_IOCRSVOL) },
		{ ARG_STR(UBI_IOCSETVOLPROP) },
	}, attach_cmds[] = {
		{ ARG_STR(UBI_IOCATT) },
	}, leb_cmds[] = {
		{ ARG_STR(UBI_IOCEBCH) },
	}, map_cmds[] = {
		{ ARG_STR(UBI_IOCEBMAP) },
	}, mkvol_cmds[] = {
		{ ARG_STR(UBI_IOCMKVOL) },
	}, rnvol_cmds[] = {
		{ ARG_STR(UBI_IOCRNVOL) },
	}, rsvol_cmds[] = {
		{ ARG_STR(UBI_IOCRSVOL) },
	}, prop_cmds[] = {
		{ ARG_STR(UBI_IOCSETVOLPROP) },
	};

	for (size_t i = 0; i < ARRAY_SIZE(noarg_cmds); ++i) {
		do_ioctl(noarg_cmds[i].cmd, lmagic);
		printf("ioctl(-1, %s) = %s\n",
		       noarg_cmds[i].str, errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(int, pint);
	*pint = imagic;

	for (size_t i = 0; i < ARRAY_SIZE(pint_cmds); ++i) {
		do_ioctl_ptr(pint_cmds[i].cmd, pint);
		printf("ioctl(-1, %s, [%d]) = %s\n",
		       pint_cmds[i].str, *pint, errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(int64_t, pint64);
	*pint64 = (int64_t) llmagic;

	for (size_t i = 0; i < ARRAY_SIZE(pint64_cmds); ++i) {
		do_ioctl_ptr(pint64_cmds[i].cmd, pint64);
		printf("ioctl(-1, %s, [%jd]) = %s\n",
		       pint64_cmds[i].str, (intmax_t) *pint64, errstr);
	}

	void *const efault = tail_alloc(1);

	for (size_t i = 0; i < ARRAY_SIZE(ptr_cmds); ++i) {
		do_ioctl(ptr_cmds[i].cmd, 0);
		printf("ioctl(-1, %s, NULL) = %s\n",
		       ptr_cmds[i].str, errstr);
		do_ioctl_ptr(ptr_cmds[i].cmd, efault);
		printf("ioctl(-1, %s, %p) = %s\n",
		       ptr_cmds[i].str, efault, errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_attach_req, attach);
	fill_memory(attach, sizeof(*attach));

	for (size_t i = 0; i < ARRAY_SIZE(attach_cmds); ++i) {
		int rc = do_ioctl_ptr(attach_cmds[i].cmd, attach);
		printf("ioctl(-1, %s, {ubi_num=%d, mtd_num=%d"
		       ", vid_hdr_offset=%d, max_beb_per1024=%hd"
		       ", disable_fm=%hhd, need_resv_pool=%hhd}",
		       attach_cmds[i].str, attach->ubi_num,
		       attach->mtd_num, attach->vid_hdr_offset,
		       attach->max_beb_per1024, attach->disable_fm,
		       attach->need_resv_pool);
		if (rc >= 0)
			printf(" => [%d]", attach->ubi_num);
		printf(") = %s\n", errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_leb_change_req, leb);
	fill_memory(leb, sizeof(*leb));
	leb->dtype = 3;

	for (size_t i = 0; i < ARRAY_SIZE(leb_cmds); ++i) {
		do_ioctl_ptr(leb_cmds[i].cmd, leb);
		printf("ioctl(-1, %s, {lnum=%d, bytes=%d, dtype=%s}) = %s\n",
		       leb_cmds[i].str, leb->lnum, leb->bytes, "UBI_UNKNOWN",
		       errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_map_req, map);
	fill_memory(map, sizeof(*map));
	map->dtype = 3;

	for (size_t i = 0; i < ARRAY_SIZE(map_cmds); ++i) {
		do_ioctl_ptr(map_cmds[i].cmd, map);
		printf("ioctl(-1, %s, {lnum=%d, dtype=%s}) = %s\n",
		       map_cmds[i].str, map->lnum, "UBI_UNKNOWN", errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_mkvol_req, mkvol);
	fill_memory(mkvol, sizeof(*mkvol));
	mkvol->vol_type = 4;
	mkvol->flags = 1;
	fill_memory_ex(mkvol->name, sizeof(mkvol->name), '0', 10);

	for (size_t i = 0; i < ARRAY_SIZE(mkvol_cmds); ++i) {
		static const int lens[] = {
			-1, 0, 1, UBI_MAX_VOLUME_NAME,
			UBI_MAX_VOLUME_NAME + 1
		};
		for (size_t j = 0; j < ARRAY_SIZE(lens); ++j) {
			mkvol->name_len = lens[j];
			int len = CLAMP(mkvol->name_len, 0,
					UBI_MAX_VOLUME_NAME);
			int rc = do_ioctl_ptr(mkvol_cmds[i].cmd, mkvol);
			printf("ioctl(-1, %s, {vol_id=%d, alignment=%d"
			       ", bytes=%jd, vol_type=%s, flags=%s"
			       ", name_len=%hd, name=\"%.*s\"...}",
			       mkvol_cmds[i].str, mkvol->vol_id,
			       mkvol->alignment, (intmax_t) mkvol->bytes,
			       "UBI_STATIC_VOLUME",
			       "UBI_VOL_SKIP_CRC_CHECK_FLG",
			       mkvol->name_len, len, mkvol->name);
			if (rc >= 0)
				printf(" => [%d]", mkvol->vol_id);
			printf(") = %s\n", errstr);
		}
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_rnvol_req, rnvol);

	for (size_t i = 0; i < ARRAY_SIZE(rnvol_cmds); ++i) {
		fill_memory(rnvol, sizeof(*rnvol));
		do_ioctl_ptr(rnvol_cmds[i].cmd, rnvol);
		printf("ioctl(-1, %s, {count=%d, ents=[]}) = %s\n",
		       rnvol_cmds[i].str, rnvol->count, errstr);

		rnvol->count = 1;
		do_ioctl_ptr(rnvol_cmds[i].cmd, rnvol);
		printf("ioctl(-1, %s, {count=%d, ents=[{vol_id=%d"
		       ", name_len=%hd, name=\"\"...}]}) = %s\n",
		       rnvol_cmds[i].str, rnvol->count,
		       rnvol->ents[0].vol_id, rnvol->ents[0].name_len, errstr);

		rnvol->count = UBI_MAX_RNVOL + 1;
		for (size_t j = 0; j < UBI_MAX_RNVOL; ++j) {
			fill_memory_ex(rnvol->ents[j].name,
				       sizeof(rnvol->ents[j].name), '0', 10);
		}
		rnvol->ents[0].name_len = 0;
		rnvol->ents[1].name_len = 1;
		rnvol->ents[2].name_len = 1;
		rnvol->ents[2].name[1] = '\0';
		rnvol->ents[3].name_len = 2;
		rnvol->ents[3].name[1] = '\0';
		rnvol->ents[4].name_len = UBI_MAX_VOLUME_NAME;
		rnvol->ents[5].name_len = UBI_MAX_VOLUME_NAME;
		rnvol->ents[5].name[UBI_MAX_VOLUME_NAME] = '\0';
		rnvol->ents[6].name_len = UBI_MAX_VOLUME_NAME;
		rnvol->ents[6].name[UBI_MAX_VOLUME_NAME - 1] = '\0';
		rnvol->ents[7].name_len = UBI_MAX_VOLUME_NAME + 1;
		rnvol->ents[8].name_len = UBI_MAX_VOLUME_NAME + 1;
		rnvol->ents[8].name[UBI_MAX_VOLUME_NAME] = '\0';
		do_ioctl_ptr(rnvol_cmds[i].cmd, rnvol);
		printf("ioctl(-1, %s, {count=%d, ents=[",
		       rnvol_cmds[i].str, rnvol->count);
		for (size_t j = 0; j < UBI_MAX_RNVOL; ++j) {
			int len = CLAMP(rnvol->ents[j].name_len, 0,
					UBI_MAX_VOLUME_NAME);
			bool dots = rnvol->ents[j].name[len] &&
				    (len <= 0 || rnvol->ents[j].name[len - 1]);
			printf("%s{vol_id=%d, name_len=%hd, name=\"%.*s\"%s}",
			       j ? ", " : "",
			       rnvol->ents[j].vol_id,
			       rnvol->ents[j].name_len,
			       len, rnvol->ents[j].name,
			       dots ? "..." : "");
		}
		printf("]}) = %s\n", errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_rsvol_req, rsvol);
	fill_memory(rsvol, sizeof(*rsvol));

	for (size_t i = 0; i < ARRAY_SIZE(rsvol_cmds); ++i) {
		do_ioctl_ptr(rsvol_cmds[i].cmd, rsvol);
		printf("ioctl(-1, %s, {bytes=%jd, vol_id=%d}) = %s\n",
		       rsvol_cmds[i].str, (intmax_t) rsvol->bytes,
		       rsvol->vol_id, errstr);
	}

	TAIL_ALLOC_OBJECT_CONST_PTR(struct ubi_set_vol_prop_req, prop);
	fill_memory(prop, sizeof(*prop));
	prop->property = UBI_VOL_PROP_DIRECT_WRITE;

	for (size_t i = 0; i < ARRAY_SIZE(prop_cmds); ++i) {
		do_ioctl_ptr(prop_cmds[i].cmd, prop);
		printf("ioctl(-1, %s, {property=%s, value=%#jx}) = %s\n",
		       prop_cmds[i].str, "UBI_VOL_PROP_DIRECT_WRITE",
		       (uintmax_t) prop->value, errstr);
	}

	do_ioctl(_IO(0x6f, 0x35), lmagic);
	printf("ioctl(-1, %s, %#lx) = %s\n", "NET_REMOVE_IF", lmagic, errstr);

	puts("+++ exited with 0 +++");
	return 0;
}