]>
Commit | Line | Data |
---|---|---|
63cccae4 | 1 | /* |
9e95aa12 KZ |
2 | * SPDX-License-Identifier: GPL-2.0-or-later |
3 | * | |
63cccae4 KZ |
4 | * cramfsck - check a cramfs file system |
5 | * | |
19922f22 KZ |
6 | * Copyright (C) 2000-2002 Transmeta Corporation |
7 | * 2005 Adrian Bunk | |
63cccae4 KZ |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
7cebf0bb SK |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
63cccae4 KZ |
22 | * |
23 | * 1999/12/03: Linus Torvalds (cramfs tester and unarchive program) | |
24 | * 2000/06/03: Daniel Quinlan (CRC and length checking program) | |
25 | * 2000/06/04: Daniel Quinlan (merged programs, added options, support | |
26 | * for special files, preserve permissions and | |
27 | * ownership, cramfs superblock v2, bogus mode | |
28 | * test, pathname length test, etc.) | |
29 | * 2000/06/06: Daniel Quinlan (support for holes, pretty-printing, | |
30 | * symlink size test) | |
31 | * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512, | |
32 | * fsck-compatible exit codes) | |
33 | * 2000/07/15: Daniel Quinlan (initial support for block devices) | |
19922f22 KZ |
34 | * 2002/01/10: Daniel Quinlan (additional checks, test more return codes, |
35 | * use read if mmap fails, standardize messages) | |
63cccae4 KZ |
36 | */ |
37 | ||
63cccae4 | 38 | #include <stdio.h> |
19922f22 | 39 | #include <stdarg.h> |
d51f37a3 | 40 | #include <stdint.h> |
63cccae4 KZ |
41 | #include <unistd.h> |
42 | #include <dirent.h> | |
43 | #include <stdlib.h> | |
44 | #include <errno.h> | |
45 | #include <string.h> | |
46 | #include <getopt.h> | |
63cccae4 | 47 | #include <fcntl.h> |
2687686c KZ |
48 | |
49 | /* We don't use our include/crc32.h, but crc32 from zlib! | |
50 | * | |
73afd3f8 | 51 | * The zlib implementation performs pre/post-conditioning. The util-linux |
2687686c KZ |
52 | * imlemenation requires post-conditioning (xor) in the applications. |
53 | */ | |
63cccae4 KZ |
54 | #include <zlib.h> |
55 | ||
ad7ac3d5 | 56 | #include <sys/time.h> |
63cccae4 KZ |
57 | #include <sys/types.h> |
58 | #include <sys/stat.h> | |
59 | #include <sys/mman.h> | |
63cccae4 | 60 | |
075d2c07 | 61 | #include "c.h" |
63cccae4 | 62 | #include "cramfs.h" |
63cccae4 | 63 | #include "nls.h" |
098fa6b1 | 64 | #include "blkdev.h" |
5d6fb944 | 65 | #include "exitcodes.h" |
5cd50ebc | 66 | #include "strutils.h" |
45ca68ec | 67 | #include "closestream.h" |
63cccae4 | 68 | |
70b604b8 | 69 | #define XALLOC_EXIT_CODE FSCK_EX_ERROR |
fc8dc410 SK |
70 | #include "xalloc.h" |
71 | ||
63cccae4 KZ |
72 | static int fd; /* ROM image file descriptor */ |
73 | static char *filename; /* ROM image filename */ | |
2ba641e5 | 74 | static struct cramfs_super super; /* just find the cramfs superblock once */ |
fbaec83b | 75 | static int cramfs_is_big_endian = 0; /* source is big endian */ |
63cccae4 | 76 | static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */ |
34731f89 | 77 | static int opt_extract = 0; /* extract cramfs (-x) */ |
2ba641e5 | 78 | static char *extract_dir = ""; /* optional extraction directory (-x) */ |
63cccae4 | 79 | |
63cccae4 | 80 | #define PAD_SIZE 512 |
b22550fa | 81 | |
6ffd9b03 | 82 | static uid_t euid; /* effective UID */ |
19922f22 KZ |
83 | |
84 | /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */ | |
6ffd9b03 SK |
85 | static unsigned long start_dir = ~0UL; /* start of first non-root inode */ |
86 | static unsigned long end_dir = 0; /* end of the directory structure */ | |
87 | static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */ | |
88 | static unsigned long end_data = 0; /* end of the data */ | |
19922f22 | 89 | |
63cccae4 | 90 | |
deba6720 T |
91 | /* Guarantee access to at least 2 * blksize at a time */ |
92 | #define CRAMFS_ROMBUFFER_BITS 13 | |
93 | #define CRAMFS_ROMBUFFERSIZE (1 << CRAMFS_ROMBUFFER_BITS) | |
94 | #define CRAMFS_ROMBUFFERMASK (CRAMFS_ROMBUFFERSIZE - 1) | |
95 | ||
96 | /* Defaults, updated in main() according to block size */ | |
97 | static size_t rombufbits = CRAMFS_ROMBUFFER_BITS; | |
98 | static size_t rombufsize = CRAMFS_ROMBUFFERSIZE; | |
99 | static size_t rombufmask = CRAMFS_ROMBUFFERMASK; | |
100 | ||
101 | static char *read_buffer; | |
63cccae4 KZ |
102 | static unsigned long read_buffer_block = ~0UL; |
103 | ||
19922f22 | 104 | static z_stream stream; |
63cccae4 | 105 | |
19922f22 KZ |
106 | /* Prototypes */ |
107 | static void expand_fs(char *, struct cramfs_inode *); | |
63cccae4 | 108 | |
19922f22 KZ |
109 | static char *outbuffer; |
110 | ||
5cd50ebc | 111 | static size_t blksize = 0; |
19922f22 | 112 | |
5118d1be | 113 | static void __attribute__((__noreturn__)) usage(void) |
63cccae4 | 114 | { |
5118d1be | 115 | FILE *out = stdout; |
63cccae4 | 116 | |
7ee26cbf SK |
117 | fputs(USAGE_HEADER, out); |
118 | fprintf(out, | |
09af3db4 | 119 | _(" %s [options] <file>\n"), program_invocation_short_name); |
451dbcfa | 120 | |
7ee26cbf SK |
121 | fputs(USAGE_SEPARATOR, out); |
122 | fputs(_("Check and repair a compressed ROM filesystem.\n"), out); | |
123 | ||
124 | fputs(USAGE_OPTIONS, out); | |
125 | fputs(_(" -a for compatibility only, ignored\n"), out); | |
126 | fputs(_(" -v, --verbose be more verbose\n"), out); | |
127 | fputs(_(" -y for compatibility only, ignored\n"), out); | |
128 | fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out); | |
129 | fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out); | |
130 | fputs(USAGE_SEPARATOR, out); | |
bad4c729 | 131 | fprintf(out, USAGE_HELP_OPTIONS(26)); |
7ee26cbf | 132 | |
bad4c729 | 133 | fprintf(out, USAGE_MAN_TAIL("fsck.cramfs(8)")); |
5118d1be | 134 | exit(FSCK_EX_OK); |
63cccae4 KZ |
135 | } |
136 | ||
d63eb75e | 137 | static int get_superblock_endianness(uint32_t magic) |
fbaec83b SRP |
138 | { |
139 | if (magic == CRAMFS_MAGIC) { | |
140 | cramfs_is_big_endian = HOST_IS_BIG_ENDIAN; | |
141 | return 0; | |
042f62df RP |
142 | } |
143 | ||
144 | if (magic == | |
6ffd9b03 | 145 | u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) { |
fbaec83b SRP |
146 | cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN; |
147 | return 0; | |
042f62df RP |
148 | } |
149 | ||
150 | return -1; | |
fbaec83b SRP |
151 | } |
152 | ||
e0771b1b | 153 | static void test_super(int *start) |
6ffd9b03 | 154 | { |
19922f22 | 155 | struct stat st; |
e0771b1b | 156 | unsigned long long length; |
6ffd9b03 | 157 | |
19922f22 | 158 | fd = open(filename, O_RDONLY); |
6ffd9b03 | 159 | if (fd < 0) |
289dcc90 | 160 | err(FSCK_EX_ERROR, _("cannot open %s"), filename); |
6ffd9b03 | 161 | |
c6f36329 KZ |
162 | /* find the physical size of the file or block device */ |
163 | if (fstat(fd, &st) < 0) | |
164 | err(FSCK_EX_ERROR, _("stat of %s failed"), filename); | |
165 | ||
19922f22 | 166 | if (S_ISBLK(st.st_mode)) { |
e0771b1b | 167 | if (blkdev_get_size(fd, &length)) |
70b604b8 | 168 | err(FSCK_EX_ERROR, |
6ffd9b03 SK |
169 | _("ioctl failed: unable to determine device size: %s"), |
170 | filename); | |
6ffd9b03 | 171 | } else if (S_ISREG(st.st_mode)) |
e0771b1b | 172 | length = st.st_size; |
6ffd9b03 | 173 | else |
70b604b8 | 174 | errx(FSCK_EX_ERROR, _("not a block device or file: %s"), filename); |
19922f22 | 175 | |
e0771b1b | 176 | if (length < sizeof(struct cramfs_super)) |
70b604b8 | 177 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); |
19922f22 KZ |
178 | |
179 | /* find superblock */ | |
6ffd9b03 | 180 | if (read(fd, &super, sizeof(super)) != sizeof(super)) |
47481cbd | 181 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 182 | if (get_superblock_endianness(super.magic) != -1) |
19922f22 | 183 | *start = 0; |
e0771b1b | 184 | else if (length >= (PAD_SIZE + sizeof(super))) { |
b3f30f50 | 185 | if (lseek(fd, PAD_SIZE, SEEK_SET) == (off_t) -1) |
47481cbd | 186 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
6ffd9b03 | 187 | if (read(fd, &super, sizeof(super)) != sizeof(super)) |
47481cbd | 188 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 189 | if (get_superblock_endianness(super.magic) != -1) |
19922f22 | 190 | *start = PAD_SIZE; |
6ffd9b03 | 191 | else |
70b604b8 | 192 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); |
6ffd9b03 | 193 | } else |
70b604b8 | 194 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); |
fbaec83b | 195 | |
6ffd9b03 | 196 | if (opt_verbose) |
6de1396e SK |
197 | printf(_("cramfs endianness is %s\n"), |
198 | cramfs_is_big_endian ? _("big") : _("little")); | |
fbaec83b SRP |
199 | |
200 | super_toggle_endianness(cramfs_is_big_endian, &super); | |
6ffd9b03 | 201 | if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) |
70b604b8 | 202 | errx(FSCK_EX_ERROR, _("unsupported filesystem features")); |
6ffd9b03 | 203 | |
f991dbd3 | 204 | /* What are valid superblock sizes? */ |
2374b1ab | 205 | if (super.size < *start + sizeof(struct cramfs_super)) |
70b604b8 | 206 | errx(FSCK_EX_UNCORRECTED, _("superblock size (%d) too small"), |
6ffd9b03 SK |
207 | super.size); |
208 | ||
19922f22 | 209 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) { |
6ffd9b03 | 210 | if (super.fsid.files == 0) |
70b604b8 | 211 | errx(FSCK_EX_UNCORRECTED, _("zero file count")); |
e0771b1b | 212 | if (length < super.size) |
70b604b8 | 213 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); |
e0771b1b | 214 | else if (length > super.size) |
deedae5f | 215 | warnx(_("file extends past end of filesystem")); |
6ffd9b03 | 216 | } else |
deedae5f | 217 | warnx(_("old cramfs format")); |
19922f22 KZ |
218 | } |
219 | ||
220 | static void test_crc(int start) | |
221 | { | |
222 | void *buf; | |
d51f37a3 | 223 | uint32_t crc; |
19922f22 KZ |
224 | |
225 | if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) { | |
deedae5f | 226 | warnx(_("unable to test CRC: old cramfs format")); |
19922f22 | 227 | return; |
19922f22 KZ |
228 | } |
229 | ||
87918040 | 230 | crc = crc32(0L, NULL, 0); |
19922f22 | 231 | |
6ffd9b03 | 232 | buf = |
919e372d | 233 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
19922f22 | 234 | if (buf == MAP_FAILED) { |
6ffd9b03 | 235 | buf = |
919e372d | 236 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, |
6ffd9b03 | 237 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
19922f22 | 238 | if (buf != MAP_FAILED) { |
68ac4269 | 239 | ssize_t tmp; |
919e372d | 240 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) |
47481cbd | 241 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
68ac4269 RM |
242 | tmp = read(fd, buf, super.size); |
243 | if (tmp < 0) | |
47481cbd | 244 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
68ac4269 RM |
245 | if (tmp != (ssize_t) super.size) |
246 | errx(FSCK_EX_ERROR, _("failed to read %"PRIu32" bytes from file %s"), | |
247 | super.size, filename); | |
19922f22 KZ |
248 | } |
249 | } | |
250 | if (buf != MAP_FAILED) { | |
aa719998 | 251 | ((struct cramfs_super *)((unsigned char *) buf + start))->fsid.crc = |
87918040 | 252 | crc32(0L, NULL, 0); |
919e372d RM |
253 | crc = crc32(crc, (unsigned char *) buf + start, super.size - start); |
254 | munmap(buf, super.size); | |
6ffd9b03 | 255 | } else { |
19922f22 KZ |
256 | int retval; |
257 | size_t length = 0; | |
258 | ||
fc8dc410 | 259 | buf = xmalloc(4096); |
b3f30f50 | 260 | if (lseek(fd, start, SEEK_SET) == (off_t) -1) |
47481cbd | 261 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
19922f22 KZ |
262 | for (;;) { |
263 | retval = read(fd, buf, 4096); | |
6ffd9b03 | 264 | if (retval < 0) |
47481cbd | 265 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 266 | else if (retval == 0) |
19922f22 | 267 | break; |
6ffd9b03 SK |
268 | if (length == 0) |
269 | ((struct cramfs_super *)buf)->fsid.crc = | |
87918040 | 270 | crc32(0L, NULL, 0); |
19922f22 | 271 | length += retval; |
6ffd9b03 | 272 | if (length > (super.size - start)) { |
2687686c | 273 | crc = crc32(crc, buf, |
6ffd9b03 SK |
274 | retval - (length - |
275 | (super.size - start))); | |
19922f22 KZ |
276 | break; |
277 | } | |
2687686c | 278 | crc = crc32(crc, buf, retval); |
19922f22 KZ |
279 | } |
280 | free(buf); | |
281 | } | |
282 | ||
6ffd9b03 | 283 | if (crc != super.fsid.crc) |
70b604b8 | 284 | errx(FSCK_EX_UNCORRECTED, _("crc error")); |
19922f22 KZ |
285 | } |
286 | ||
63cccae4 KZ |
287 | static void print_node(char type, struct cramfs_inode *i, char *name) |
288 | { | |
289 | char info[10]; | |
290 | ||
6ffd9b03 | 291 | if (S_ISCHR(i->mode) || (S_ISBLK(i->mode))) |
63cccae4 KZ |
292 | /* major/minor numbers can be as high as 2^12 or 4096 */ |
293 | snprintf(info, 10, "%4d,%4d", major(i->size), minor(i->size)); | |
6ffd9b03 | 294 | else |
63cccae4 KZ |
295 | /* size be as high as 2^24 or 16777216 */ |
296 | snprintf(info, 10, "%9d", i->size); | |
63cccae4 KZ |
297 | |
298 | printf("%c %04o %s %5d:%-3d %s\n", | |
a6f72ab4 KZ |
299 | type, i->mode & ~S_IFMT, info, i->uid, i->gid, |
300 | !*name && type == 'd' ? "/" : name); | |
63cccae4 KZ |
301 | } |
302 | ||
303 | /* | |
304 | * Create a fake "blocked" access | |
305 | */ | |
306 | static void *romfs_read(unsigned long offset) | |
307 | { | |
deba6720 | 308 | unsigned int block = offset >> rombufbits; |
63cccae4 | 309 | if (block != read_buffer_block) { |
52848780 KZ |
310 | ssize_t x; |
311 | ||
63cccae4 | 312 | read_buffer_block = block; |
deba6720 | 313 | if (lseek(fd, block << rombufbits, SEEK_SET) == (off_t) -1) |
b3f30f50 | 314 | warn(_("seek failed")); |
52848780 | 315 | |
deba6720 | 316 | x = read(fd, read_buffer, rombufsize * 2); |
52848780 KZ |
317 | if (x < 0) |
318 | warn(_("read romfs failed")); | |
63cccae4 | 319 | } |
deba6720 | 320 | return read_buffer + (offset & rombufmask); |
63cccae4 KZ |
321 | } |
322 | ||
6ffd9b03 | 323 | static struct cramfs_inode *cramfs_iget(struct cramfs_inode *i) |
63cccae4 | 324 | { |
fc8dc410 | 325 | struct cramfs_inode *inode = xmalloc(sizeof(struct cramfs_inode)); |
19922f22 | 326 | |
fbaec83b | 327 | inode_to_host(cramfs_is_big_endian, i, inode); |
63cccae4 KZ |
328 | return inode; |
329 | } | |
330 | ||
331 | static struct cramfs_inode *iget(unsigned int ino) | |
332 | { | |
333 | return cramfs_iget(romfs_read(ino)); | |
334 | } | |
335 | ||
63cccae4 KZ |
336 | static void iput(struct cramfs_inode *inode) |
337 | { | |
338 | free(inode); | |
339 | } | |
63cccae4 KZ |
340 | |
341 | /* | |
19922f22 | 342 | * Return the offset of the root directory |
63cccae4 KZ |
343 | */ |
344 | static struct cramfs_inode *read_super(void) | |
345 | { | |
6ffd9b03 | 346 | struct cramfs_inode *root = cramfs_iget(&super.root); |
fbaec83b | 347 | unsigned long offset = root->offset << 2; |
19922f22 | 348 | |
fbaec83b | 349 | if (!S_ISDIR(root->mode)) |
70b604b8 | 350 | errx(FSCK_EX_UNCORRECTED, _("root inode is not directory")); |
19922f22 KZ |
351 | if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) && |
352 | ((offset != sizeof(struct cramfs_super)) && | |
6ffd9b03 | 353 | (offset != PAD_SIZE + sizeof(struct cramfs_super)))) { |
70b604b8 | 354 | errx(FSCK_EX_UNCORRECTED, _("bad root offset (%lu)"), offset); |
19922f22 | 355 | } |
fbaec83b | 356 | return root; |
63cccae4 KZ |
357 | } |
358 | ||
52848780 | 359 | static int uncompress_block(void *src, size_t len) |
63cccae4 KZ |
360 | { |
361 | int err; | |
362 | ||
363 | stream.next_in = src; | |
364 | stream.avail_in = len; | |
365 | ||
6ffd9b03 | 366 | stream.next_out = (unsigned char *)outbuffer; |
38141baf | 367 | stream.avail_out = blksize * 2; |
63cccae4 KZ |
368 | |
369 | inflateReset(&stream); | |
370 | ||
38141baf | 371 | if (len > blksize * 2) |
70b604b8 | 372 | errx(FSCK_EX_UNCORRECTED, _("data block too large")); |
6ffd9b03 | 373 | |
63cccae4 | 374 | err = inflate(&stream, Z_FINISH); |
6ffd9b03 | 375 | if (err != Z_STREAM_END) |
52848780 KZ |
376 | errx(FSCK_EX_UNCORRECTED, _("decompression error: %s"), |
377 | zError(err)); | |
63cccae4 KZ |
378 | return stream.total_out; |
379 | } | |
380 | ||
d52786aa | 381 | #ifndef HAVE_LCHOWN |
48d7b13a | 382 | #define lchown chown |
63cccae4 | 383 | #endif |
6ffd9b03 | 384 | |
7ee26cbf | 385 | static void do_uncompress(char *path, int outfd, unsigned long offset, |
6ffd9b03 | 386 | unsigned long size) |
19922f22 | 387 | { |
38141baf | 388 | unsigned long curr = offset + 4 * ((size + blksize - 1) / blksize); |
19922f22 KZ |
389 | |
390 | do { | |
38141baf | 391 | unsigned long out = blksize; |
6ffd9b03 SK |
392 | unsigned long next = u32_toggle_endianness(cramfs_is_big_endian, |
393 | *(uint32_t *) | |
394 | romfs_read(offset)); | |
19922f22 | 395 | |
6ffd9b03 | 396 | if (next > end_data) |
19922f22 | 397 | end_data = next; |
19922f22 KZ |
398 | |
399 | offset += 4; | |
400 | if (curr == next) { | |
6ffd9b03 | 401 | if (opt_verbose > 1) |
e3ca1312 | 402 | printf(_(" hole at %lu (%zu)\n"), curr, |
38141baf RM |
403 | blksize); |
404 | if (size < blksize) | |
19922f22 KZ |
405 | out = size; |
406 | memset(outbuffer, 0x00, out); | |
6ffd9b03 SK |
407 | } else { |
408 | if (opt_verbose > 1) | |
e3ca1312 | 409 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), |
6ffd9b03 | 410 | curr, next, next - curr); |
19922f22 KZ |
411 | out = uncompress_block(romfs_read(curr), next - curr); |
412 | } | |
38141baf RM |
413 | if (size >= blksize) { |
414 | if (out != blksize) | |
70b604b8 | 415 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 | 416 | _("non-block (%ld) bytes"), out); |
19922f22 | 417 | } else { |
6ffd9b03 | 418 | if (out != size) |
70b604b8 | 419 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
420 | _("non-size (%ld vs %ld) bytes"), out, |
421 | size); | |
19922f22 KZ |
422 | } |
423 | size -= out; | |
74ce680a SK |
424 | if (*extract_dir != '\0' && write(outfd, outbuffer, out) < 0) |
425 | err(FSCK_EX_ERROR, _("write failed: %s"), path); | |
19922f22 KZ |
426 | curr = next; |
427 | } while (size); | |
428 | } | |
63cccae4 KZ |
429 | static void change_file_status(char *path, struct cramfs_inode *i) |
430 | { | |
12d12102 | 431 | const struct timeval epoch[] = { {0,0}, {0,0} }; |
63cccae4 KZ |
432 | |
433 | if (euid == 0) { | |
6ffd9b03 | 434 | if (lchown(path, i->uid, i->gid) < 0) |
70b604b8 | 435 | err(FSCK_EX_ERROR, _("lchown failed: %s"), path); |
63cccae4 KZ |
436 | if (S_ISLNK(i->mode)) |
437 | return; | |
6ffd9b03 | 438 | if (((S_ISUID | S_ISGID) & i->mode) && chmod(path, i->mode) < 0) |
aab25aef | 439 | err(FSCK_EX_ERROR, _("chmod failed: %s"), path); |
63cccae4 KZ |
440 | } |
441 | if (S_ISLNK(i->mode)) | |
442 | return; | |
12d12102 | 443 | if (utimes(path, epoch) < 0) |
ad7ac3d5 | 444 | err(FSCK_EX_ERROR, _("utimes failed: %s"), path); |
19922f22 KZ |
445 | } |
446 | ||
18f96355 SN |
447 | static int is_dangerous_filename(char *name, int len) |
448 | { | |
449 | return (len == 1 && name[0] == '.') || | |
450 | (len == 2 && name[0] == '.' && name[1] == '.'); | |
451 | } | |
452 | ||
453 | static void __attribute__((__noreturn__)) | |
454 | errx_path(const char *mesg, const char *name, size_t namelen) | |
455 | { | |
456 | char buf[PATH_MAX] = { 0 }; | |
457 | ||
458 | namelen = min(namelen, sizeof(buf) - 1); | |
459 | memcpy(buf, name, namelen); | |
460 | ||
461 | errx(FSCK_EX_UNCORRECTED, "%s: %s", mesg, buf); | |
462 | } | |
463 | ||
19922f22 KZ |
464 | static void do_directory(char *path, struct cramfs_inode *i) |
465 | { | |
466 | int pathlen = strlen(path); | |
467 | int count = i->size; | |
468 | unsigned long offset = i->offset << 2; | |
fc8dc410 | 469 | char *newpath = xmalloc(pathlen + 256); |
19922f22 | 470 | |
6ffd9b03 | 471 | if (offset == 0 && count != 0) |
70b604b8 | 472 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
473 | _("directory inode has zero offset and non-zero size: %s"), |
474 | path); | |
475 | ||
476 | if (offset != 0 && offset < start_dir) | |
19922f22 | 477 | start_dir = offset; |
6ffd9b03 | 478 | |
19922f22 KZ |
479 | /* TODO: Do we need to check end_dir for empty case? */ |
480 | memcpy(newpath, path, pathlen); | |
481 | newpath[pathlen] = '/'; | |
482 | pathlen++; | |
6ffd9b03 | 483 | if (opt_verbose) |
19922f22 | 484 | print_node('d', i, path); |
6ffd9b03 | 485 | |
2d317f11 | 486 | if (*extract_dir != '\0') { |
6ffd9b03 | 487 | if (mkdir(path, i->mode) < 0) |
70b604b8 | 488 | err(FSCK_EX_ERROR, _("mkdir failed: %s"), path); |
19922f22 KZ |
489 | change_file_status(path, i); |
490 | } | |
491 | while (count > 0) { | |
492 | struct cramfs_inode *child = iget(offset); | |
18f96355 | 493 | char *name; |
19922f22 KZ |
494 | int size; |
495 | int newlen = child->namelen << 2; | |
496 | ||
497 | size = sizeof(struct cramfs_inode) + newlen; | |
498 | count -= size; | |
499 | ||
500 | offset += sizeof(struct cramfs_inode); | |
18f96355 | 501 | name = romfs_read(offset); |
19922f22 | 502 | |
18f96355 SN |
503 | if (memchr(name, '/', newlen) != NULL) |
504 | errx_path(_("illegal filename"), name, newlen); | |
505 | if (*extract_dir != '\0' && is_dangerous_filename(name, newlen)) | |
506 | errx_path(_("dangerous filename"), name, newlen); | |
507 | memcpy(newpath + pathlen, name, newlen); | |
19922f22 | 508 | newpath[pathlen + newlen] = 0; |
6ffd9b03 | 509 | if (newlen == 0) |
70b604b8 | 510 | errx(FSCK_EX_UNCORRECTED, _("filename length is zero")); |
6ffd9b03 | 511 | if ((pathlen + newlen) - strlen(newpath) > 3) |
70b604b8 | 512 | errx(FSCK_EX_UNCORRECTED, _("bad filename length")); |
19922f22 KZ |
513 | expand_fs(newpath, child); |
514 | ||
515 | offset += newlen; | |
516 | ||
6ffd9b03 | 517 | if (offset <= start_dir) |
70b604b8 | 518 | errx(FSCK_EX_UNCORRECTED, _("bad inode offset")); |
6ffd9b03 | 519 | if (offset > end_dir) |
19922f22 | 520 | end_dir = offset; |
6ffd9b03 | 521 | iput(child); /* free(child) */ |
19922f22 KZ |
522 | } |
523 | free(newpath); | |
524 | } | |
525 | ||
526 | static void do_file(char *path, struct cramfs_inode *i) | |
527 | { | |
528 | unsigned long offset = i->offset << 2; | |
7ee26cbf | 529 | int outfd = 0; |
19922f22 | 530 | |
6ffd9b03 | 531 | if (offset == 0 && i->size != 0) |
70b604b8 | 532 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
533 | _("file inode has zero offset and non-zero size")); |
534 | if (i->size == 0 && offset != 0) | |
70b604b8 | 535 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
536 | _("file inode has zero size and non-zero offset")); |
537 | if (offset != 0 && offset < start_data) | |
19922f22 | 538 | start_data = offset; |
6ffd9b03 | 539 | if (opt_verbose) |
19922f22 | 540 | print_node('f', i, path); |
2d317f11 | 541 | if (*extract_dir != '\0') { |
7ee26cbf SK |
542 | outfd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode); |
543 | if (outfd < 0) | |
289dcc90 | 544 | err(FSCK_EX_ERROR, _("cannot open %s"), path); |
19922f22 | 545 | } |
6ffd9b03 | 546 | if (i->size) |
7ee26cbf | 547 | do_uncompress(path, outfd, offset, i->size); |
2d317f11 | 548 | if ( *extract_dir != '\0') { |
7ee26cbf | 549 | if (close_fd(outfd) != 0) |
83f210e8 | 550 | err(FSCK_EX_ERROR, _("write failed: %s"), path); |
19922f22 | 551 | change_file_status(path, i); |
63cccae4 KZ |
552 | } |
553 | } | |
554 | ||
555 | static void do_symlink(char *path, struct cramfs_inode *i) | |
556 | { | |
557 | unsigned long offset = i->offset << 2; | |
558 | unsigned long curr = offset + 4; | |
6ffd9b03 SK |
559 | unsigned long next = |
560 | u32_toggle_endianness(cramfs_is_big_endian, | |
561 | *(uint32_t *) romfs_read(offset)); | |
63cccae4 KZ |
562 | unsigned long size; |
563 | ||
6ffd9b03 | 564 | if (offset == 0) |
70b604b8 | 565 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero offset")); |
6ffd9b03 | 566 | if (i->size == 0) |
70b604b8 | 567 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero size")); |
19922f22 | 568 | |
6ffd9b03 | 569 | if (offset < start_data) |
19922f22 | 570 | start_data = offset; |
6ffd9b03 | 571 | if (next > end_data) |
63cccae4 | 572 | end_data = next; |
63cccae4 KZ |
573 | |
574 | size = uncompress_block(romfs_read(curr), next - curr); | |
6ffd9b03 | 575 | if (size != i->size) |
70b604b8 | 576 | errx(FSCK_EX_UNCORRECTED, _("size error in symlink: %s"), path); |
63cccae4 KZ |
577 | outbuffer[size] = 0; |
578 | if (opt_verbose) { | |
579 | char *str; | |
580 | ||
6f312c89 | 581 | xasprintf(&str, "%s -> %s", path, outbuffer); |
63cccae4 | 582 | print_node('l', i, str); |
6ffd9b03 | 583 | if (opt_verbose > 1) |
e3ca1312 | 584 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), |
6ffd9b03 | 585 | curr, next, next - curr); |
19922f22 | 586 | free(str); |
63cccae4 | 587 | } |
2d317f11 | 588 | if (*extract_dir != '\0') { |
6ffd9b03 | 589 | if (symlink(outbuffer, path) < 0) |
70b604b8 | 590 | err(FSCK_EX_ERROR, _("symlink failed: %s"), path); |
63cccae4 KZ |
591 | change_file_status(path, i); |
592 | } | |
593 | } | |
594 | ||
595 | static void do_special_inode(char *path, struct cramfs_inode *i) | |
596 | { | |
597 | dev_t devtype = 0; | |
598 | char type; | |
599 | ||
6ffd9b03 SK |
600 | if (i->offset) |
601 | /* no need to shift offset */ | |
70b604b8 | 602 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
603 | _("special file has non-zero offset: %s"), path); |
604 | ||
63cccae4 KZ |
605 | if (S_ISCHR(i->mode)) { |
606 | devtype = i->size; | |
607 | type = 'c'; | |
6ffd9b03 | 608 | } else if (S_ISBLK(i->mode)) { |
63cccae4 KZ |
609 | devtype = i->size; |
610 | type = 'b'; | |
6ffd9b03 SK |
611 | } else if (S_ISFIFO(i->mode)) { |
612 | if (i->size != 0) | |
70b604b8 | 613 | errx(FSCK_EX_UNCORRECTED, _("fifo has non-zero size: %s"), |
6ffd9b03 | 614 | path); |
63cccae4 | 615 | type = 'p'; |
6ffd9b03 SK |
616 | } else if (S_ISSOCK(i->mode)) { |
617 | if (i->size != 0) | |
70b604b8 | 618 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 | 619 | _("socket has non-zero size: %s"), path); |
63cccae4 | 620 | type = 's'; |
6ffd9b03 | 621 | } else { |
70b604b8 | 622 | errx(FSCK_EX_UNCORRECTED, _("bogus mode: %s (%o)"), path, i->mode); |
19922f22 | 623 | return; /* not reached */ |
63cccae4 KZ |
624 | } |
625 | ||
6ffd9b03 | 626 | if (opt_verbose) |
63cccae4 | 627 | print_node(type, i, path); |
63cccae4 | 628 | |
2d317f11 | 629 | if (*extract_dir != '\0') { |
6ffd9b03 | 630 | if (mknod(path, i->mode, devtype) < 0) |
70b604b8 | 631 | err(FSCK_EX_ERROR, _("mknod failed: %s"), path); |
63cccae4 KZ |
632 | change_file_status(path, i); |
633 | } | |
634 | } | |
635 | ||
19922f22 | 636 | static void expand_fs(char *path, struct cramfs_inode *inode) |
63cccae4 | 637 | { |
6ffd9b03 | 638 | if (S_ISDIR(inode->mode)) |
19922f22 | 639 | do_directory(path, inode); |
6ffd9b03 | 640 | else if (S_ISREG(inode->mode)) |
19922f22 | 641 | do_file(path, inode); |
6ffd9b03 | 642 | else if (S_ISLNK(inode->mode)) |
19922f22 | 643 | do_symlink(path, inode); |
6ffd9b03 | 644 | else |
19922f22 | 645 | do_special_inode(path, inode); |
63cccae4 KZ |
646 | } |
647 | ||
19922f22 | 648 | static void test_fs(int start) |
63cccae4 | 649 | { |
19922f22 | 650 | struct cramfs_inode *root; |
63cccae4 | 651 | |
19922f22 KZ |
652 | root = read_super(); |
653 | umask(0); | |
654 | euid = geteuid(); | |
655 | stream.next_in = NULL; | |
656 | stream.avail_in = 0; | |
657 | inflateInit(&stream); | |
658 | expand_fs(extract_dir, root); | |
659 | inflateEnd(&stream); | |
660 | if (start_data != ~0UL) { | |
6ffd9b03 | 661 | if (start_data < (sizeof(struct cramfs_super) + start)) |
70b604b8 | 662 | errx(FSCK_EX_UNCORRECTED, |
60cb2c37 | 663 | _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"), |
6ffd9b03 SK |
664 | start_data, sizeof(struct cramfs_super) + start); |
665 | if (end_dir != start_data) | |
70b604b8 | 666 | errx(FSCK_EX_UNCORRECTED, |
60cb2c37 | 667 | _("directory data end (%lu) != file data start (%lu)"), |
6ffd9b03 SK |
668 | end_dir, start_data); |
669 | } | |
74ce680a SK |
670 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2 && end_data > super.size) |
671 | errx(FSCK_EX_UNCORRECTED, _("invalid file data offset")); | |
6ffd9b03 | 672 | |
19922f22 | 673 | iput(root); /* free(root) */ |
63cccae4 | 674 | } |
63cccae4 KZ |
675 | |
676 | int main(int argc, char **argv) | |
677 | { | |
63cccae4 KZ |
678 | int c; /* for getopt */ |
679 | int start = 0; | |
19922f22 | 680 | |
922ec175 | 681 | static const struct option longopts[] = { |
87918040 SK |
682 | {"verbose", no_argument, NULL, 'v'}, |
683 | {"version", no_argument, NULL, 'V'}, | |
684 | {"help", no_argument, NULL, 'h'}, | |
685 | {"blocksize", required_argument, NULL, 'b'}, | |
686 | {"extract", optional_argument, NULL, 'x'}, | |
687 | {NULL, 0, NULL, 0}, | |
922ec175 SK |
688 | }; |
689 | ||
e56644ca | 690 | setlocale(LC_MESSAGES, ""); |
5d4ece6e | 691 | setlocale(LC_CTYPE, ""); |
e56644ca PR |
692 | bindtextdomain(PACKAGE, LOCALEDIR); |
693 | textdomain(PACKAGE); | |
2c308875 | 694 | close_stdout_atexit(); |
e56644ca | 695 | |
0b2b32e8 RM |
696 | strutils_set_exitcode(FSCK_EX_USAGE); |
697 | ||
63cccae4 | 698 | /* command line options */ |
2d317f11 | 699 | while ((c = getopt_long(argc, argv, "ayvVhb:", longopts, NULL)) != EOF) |
63cccae4 | 700 | switch (c) { |
6bf463c5 KZ |
701 | case 'a': /* ignore */ |
702 | case 'y': | |
703 | break; | |
63cccae4 | 704 | case 'h': |
5118d1be | 705 | usage(); |
e4a36b8b | 706 | break; |
922ec175 | 707 | case 'V': |
2c308875 | 708 | print_version(FSCK_EX_OK); |
63cccae4 | 709 | case 'x': |
63cccae4 | 710 | opt_extract = 1; |
2d317f11 RM |
711 | if(optarg) |
712 | extract_dir = optarg; | |
63cccae4 | 713 | break; |
63cccae4 KZ |
714 | case 'v': |
715 | opt_verbose++; | |
716 | break; | |
5cd50ebc | 717 | case 'b': |
5cd50ebc | 718 | blksize = strtou32_or_err(optarg, _("invalid blocksize argument")); |
5cd50ebc | 719 | break; |
de173766 | 720 | default: |
677ec86c | 721 | errtryhelp(FSCK_EX_USAGE); |
63cccae4 | 722 | } |
63cccae4 | 723 | |
5118d1be RM |
724 | if ((argc - optind) != 1){ |
725 | warnx(_("bad usage")); | |
726 | errtryhelp(FSCK_EX_USAGE); | |
727 | } | |
63cccae4 KZ |
728 | filename = argv[optind]; |
729 | ||
e0771b1b | 730 | test_super(&start); |
19922f22 | 731 | test_crc(start); |
34731f89 | 732 | |
deba6720 T |
733 | if (opt_extract) { |
734 | size_t bufsize = 0; | |
735 | ||
2d317f11 RM |
736 | if (blksize == 0) |
737 | blksize = getpagesize(); | |
deba6720 T |
738 | |
739 | /* re-calculate according to blksize */ | |
740 | bufsize = rombufsize = blksize * 2; | |
741 | rombufbits = 0; | |
742 | while (bufsize >>= 1) | |
743 | rombufbits++; | |
744 | rombufmask = rombufsize - 1; | |
745 | ||
2d317f11 | 746 | outbuffer = xmalloc(blksize * 2); |
deba6720 | 747 | read_buffer = xmalloc(rombufsize * 2); |
2d317f11 RM |
748 | test_fs(start); |
749 | } | |
63cccae4 | 750 | |
6ffd9b03 | 751 | if (opt_verbose) |
6de1396e | 752 | printf(_("%s: OK\n"), filename); |
63cccae4 | 753 | |
70b604b8 | 754 | exit(FSCK_EX_OK); |
63cccae4 | 755 | } |