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