1 /*
2 * csum.c --- checksumming of ext3 structures
3 *
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com>
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
10 * %End-Header%
11 */
12
13 #include "config.h"
14 #if HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif
17
18 #include "ext2_fs.h"
19 #include "ext2fs.h"
20 #include "crc16.h"
21 #include <assert.h>
22
23 #ifndef offsetof
24 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
25 #endif
26
27 #ifdef DEBUG
28 #define STATIC
29 #else
30 #define STATIC static
31 #endif
32
ext2fs_init_csum_seed(ext2_filsys fs)33 void ext2fs_init_csum_seed(ext2_filsys fs)
34 {
35 if (ext2fs_has_feature_csum_seed(fs->super))
36 fs->csum_seed = fs->super->s_checksum_seed;
37 else if (ext2fs_has_feature_metadata_csum(fs->super) ||
38 ext2fs_has_feature_ea_inode(fs->super))
39 fs->csum_seed = ext2fs_crc32c_le(~0, fs->super->s_uuid,
40 sizeof(fs->super->s_uuid));
41 }
42
ext2fs_mmp_csum(ext2_filsys fs,struct mmp_struct * mmp)43 static __u32 ext2fs_mmp_csum(ext2_filsys fs, struct mmp_struct *mmp)
44 {
45 int offset = offsetof(struct mmp_struct, mmp_checksum);
46
47 return ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)mmp, offset);
48 }
49
ext2fs_mmp_csum_verify(ext2_filsys fs,struct mmp_struct * mmp)50 int ext2fs_mmp_csum_verify(ext2_filsys fs, struct mmp_struct *mmp)
51 {
52 __u32 calculated;
53
54 if (!ext2fs_has_feature_metadata_csum(fs->super))
55 return 1;
56
57 calculated = ext2fs_mmp_csum(fs, mmp);
58
59 return ext2fs_le32_to_cpu(mmp->mmp_checksum) == calculated;
60 }
61
ext2fs_mmp_csum_set(ext2_filsys fs,struct mmp_struct * mmp)62 errcode_t ext2fs_mmp_csum_set(ext2_filsys fs, struct mmp_struct *mmp)
63 {
64 __u32 crc;
65
66 if (!ext2fs_has_feature_metadata_csum(fs->super))
67 return 0;
68
69 crc = ext2fs_mmp_csum(fs, mmp);
70 mmp->mmp_checksum = ext2fs_cpu_to_le32(crc);
71
72 return 0;
73 }
74
ext2fs_verify_csum_type(ext2_filsys fs,struct ext2_super_block * sb)75 int ext2fs_verify_csum_type(ext2_filsys fs, struct ext2_super_block *sb)
76 {
77 if (!ext2fs_has_feature_metadata_csum(fs->super))
78 return 1;
79
80 return sb->s_checksum_type == EXT2_CRC32C_CHKSUM;
81 }
82
ext2fs_superblock_csum(ext2_filsys fs EXT2FS_ATTR ((unused)),struct ext2_super_block * sb)83 static __u32 ext2fs_superblock_csum(ext2_filsys fs EXT2FS_ATTR((unused)),
84 struct ext2_super_block *sb)
85 {
86 int offset = offsetof(struct ext2_super_block, s_checksum);
87
88 return ext2fs_crc32c_le(~0, (unsigned char *)sb, offset);
89 }
90
91 /* NOTE: The input to this function MUST be in LE order */
ext2fs_superblock_csum_verify(ext2_filsys fs,struct ext2_super_block * sb)92 int ext2fs_superblock_csum_verify(ext2_filsys fs, struct ext2_super_block *sb)
93 {
94 __u32 flag, calculated;
95
96 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
97 flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
98 else
99 flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
100
101 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag))
102 return 1;
103
104 calculated = ext2fs_superblock_csum(fs, sb);
105
106 return ext2fs_le32_to_cpu(sb->s_checksum) == calculated;
107 }
108
109 /* NOTE: The input to this function MUST be in LE order */
ext2fs_superblock_csum_set(ext2_filsys fs,struct ext2_super_block * sb)110 errcode_t ext2fs_superblock_csum_set(ext2_filsys fs,
111 struct ext2_super_block *sb)
112 {
113 __u32 flag, crc;
114
115 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
116 flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
117 else
118 flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
119
120 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag))
121 return 0;
122
123 crc = ext2fs_superblock_csum(fs, sb);
124 sb->s_checksum = ext2fs_cpu_to_le32(crc);
125
126 return 0;
127 }
128
ext2fs_ext_attr_block_csum(ext2_filsys fs,ext2_ino_t inum EXT2FS_ATTR ((unused)),blk64_t block,struct ext2_ext_attr_header * hdr,__u32 * crc)129 static errcode_t ext2fs_ext_attr_block_csum(ext2_filsys fs,
130 ext2_ino_t inum EXT2FS_ATTR((unused)),
131 blk64_t block,
132 struct ext2_ext_attr_header *hdr,
133 __u32 *crc)
134 {
135 char *buf = (char *)hdr;
136 __u32 old_crc = hdr->h_checksum;
137
138 hdr->h_checksum = 0;
139 block = ext2fs_cpu_to_le64(block);
140 *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&block,
141 sizeof(block));
142 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, fs->blocksize);
143 hdr->h_checksum = old_crc;
144
145 return 0;
146 }
147
ext2fs_ext_attr_block_csum_verify(ext2_filsys fs,ext2_ino_t inum,blk64_t block,struct ext2_ext_attr_header * hdr)148 int ext2fs_ext_attr_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
149 blk64_t block,
150 struct ext2_ext_attr_header *hdr)
151 {
152 __u32 calculated;
153 errcode_t retval;
154
155 if (!ext2fs_has_feature_metadata_csum(fs->super))
156 return 1;
157
158 retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &calculated);
159 if (retval)
160 return 0;
161
162 return ext2fs_le32_to_cpu(hdr->h_checksum) == calculated;
163 }
164
ext2fs_ext_attr_block_csum_set(ext2_filsys fs,ext2_ino_t inum,blk64_t block,struct ext2_ext_attr_header * hdr)165 errcode_t ext2fs_ext_attr_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
166 blk64_t block,
167 struct ext2_ext_attr_header *hdr)
168 {
169 errcode_t retval;
170 __u32 crc;
171
172 if (!ext2fs_has_feature_metadata_csum(fs->super))
173 return 0;
174
175 retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &crc);
176 if (retval)
177 return retval;
178 hdr->h_checksum = ext2fs_cpu_to_le32(crc);
179 return 0;
180 }
181
do_nothing16(__u16 x)182 static __u16 do_nothing16(__u16 x)
183 {
184 return x;
185 }
186
disk_to_host16(__u16 x)187 static __u16 disk_to_host16(__u16 x)
188 {
189 return ext2fs_le16_to_cpu(x);
190 }
191
__get_dx_countlimit(ext2_filsys fs,struct ext2_dir_entry * dirent,struct ext2_dx_countlimit ** cc,int * offset,int need_swab)192 static errcode_t __get_dx_countlimit(ext2_filsys fs,
193 struct ext2_dir_entry *dirent,
194 struct ext2_dx_countlimit **cc,
195 int *offset,
196 int need_swab)
197 {
198 struct ext2_dir_entry *dp;
199 struct ext2_dx_root_info *root;
200 struct ext2_dx_countlimit *c;
201 int count_offset, max_sane_entries;
202 unsigned int rec_len;
203 __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16);
204
205 rec_len = translate(dirent->rec_len);
206
207 if (rec_len == fs->blocksize && translate(dirent->name_len) == 0)
208 count_offset = 8;
209 else if (rec_len == 12) {
210 dp = (struct ext2_dir_entry *)(((char *)dirent) + rec_len);
211 rec_len = translate(dp->rec_len);
212 if (rec_len != fs->blocksize - 12)
213 return EXT2_ET_DB_NOT_FOUND;
214 root = (struct ext2_dx_root_info *)(((char *)dp + 12));
215 if (root->reserved_zero ||
216 root->info_length != sizeof(struct ext2_dx_root_info))
217 return EXT2_ET_DB_NOT_FOUND;
218 count_offset = 32;
219 } else
220 return EXT2_ET_DB_NOT_FOUND;
221
222 c = (struct ext2_dx_countlimit *)(((char *)dirent) + count_offset);
223 max_sane_entries = (fs->blocksize - count_offset) /
224 sizeof(struct ext2_dx_entry);
225 if (ext2fs_le16_to_cpu(c->limit) > max_sane_entries ||
226 ext2fs_le16_to_cpu(c->count) > max_sane_entries)
227 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
228
229 if (offset)
230 *offset = count_offset;
231 if (cc)
232 *cc = c;
233
234 return 0;
235 }
236
ext2fs_get_dx_countlimit(ext2_filsys fs,struct ext2_dir_entry * dirent,struct ext2_dx_countlimit ** cc,int * offset)237 errcode_t ext2fs_get_dx_countlimit(ext2_filsys fs,
238 struct ext2_dir_entry *dirent,
239 struct ext2_dx_countlimit **cc,
240 int *offset)
241 {
242 return __get_dx_countlimit(fs, dirent, cc, offset, 0);
243 }
244
ext2fs_initialize_dirent_tail(ext2_filsys fs,struct ext2_dir_entry_tail * t)245 void ext2fs_initialize_dirent_tail(ext2_filsys fs,
246 struct ext2_dir_entry_tail *t)
247 {
248 memset(t, 0, sizeof(struct ext2_dir_entry_tail));
249 ext2fs_set_rec_len(fs, sizeof(struct ext2_dir_entry_tail),
250 (struct ext2_dir_entry *)t);
251 t->det_reserved_name_len = EXT2_DIR_NAME_LEN_CSUM;
252 }
253
__get_dirent_tail(ext2_filsys fs,struct ext2_dir_entry * dirent,struct ext2_dir_entry_tail ** tt,int need_swab)254 static errcode_t __get_dirent_tail(ext2_filsys fs,
255 struct ext2_dir_entry *dirent,
256 struct ext2_dir_entry_tail **tt,
257 int need_swab)
258 {
259 struct ext2_dir_entry *d;
260 void *top;
261 struct ext2_dir_entry_tail *t;
262 unsigned int rec_len;
263 errcode_t retval = 0;
264 __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16);
265
266 d = dirent;
267 top = EXT2_DIRENT_TAIL(dirent, fs->blocksize);
268
269 rec_len = translate(d->rec_len);
270 while ((void *) d < top) {
271 if ((rec_len < 8) || (rec_len & 0x03))
272 return EXT2_ET_DIR_CORRUPTED;
273 d = (struct ext2_dir_entry *)(((char *)d) + rec_len);
274 rec_len = translate(d->rec_len);
275 }
276
277 if ((void *)d > ((void *)dirent + fs->blocksize))
278 return EXT2_ET_DIR_CORRUPTED;
279 if (d != top)
280 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
281
282 t = (struct ext2_dir_entry_tail *)d;
283 if (t->det_reserved_zero1 ||
284 translate(t->det_rec_len) != sizeof(struct ext2_dir_entry_tail) ||
285 translate(t->det_reserved_name_len) != EXT2_DIR_NAME_LEN_CSUM)
286 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
287
288 if (tt)
289 *tt = t;
290 return retval;
291 }
292
ext2fs_dirent_has_tail(ext2_filsys fs,struct ext2_dir_entry * dirent)293 int ext2fs_dirent_has_tail(ext2_filsys fs, struct ext2_dir_entry *dirent)
294 {
295 return __get_dirent_tail(fs, dirent, NULL, 0) !=
296 EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
297 }
298
ext2fs_dirent_csum(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent,__u32 * crc,int size)299 static errcode_t ext2fs_dirent_csum(ext2_filsys fs, ext2_ino_t inum,
300 struct ext2_dir_entry *dirent, __u32 *crc,
301 int size)
302 {
303 errcode_t retval;
304 char *buf = (char *)dirent;
305 __u32 gen;
306 struct ext2_inode inode;
307
308 retval = ext2fs_read_inode(fs, inum, &inode);
309 if (retval)
310 return retval;
311
312 inum = ext2fs_cpu_to_le32(inum);
313 gen = ext2fs_cpu_to_le32(inode.i_generation);
314 *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
315 sizeof(inum));
316 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
317 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size);
318
319 return 0;
320 }
321
ext2fs_dirent_csum_verify(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)322 int ext2fs_dirent_csum_verify(ext2_filsys fs, ext2_ino_t inum,
323 struct ext2_dir_entry *dirent)
324 {
325 errcode_t retval;
326 __u32 calculated;
327 struct ext2_dir_entry_tail *t;
328
329 retval = __get_dirent_tail(fs, dirent, &t, 1);
330 if (retval)
331 return 1;
332
333 /*
334 * The checksum field is overlaid with the dirent->name field
335 * so the swapfs.c functions won't change the endianness.
336 */
337 retval = ext2fs_dirent_csum(fs, inum, dirent, &calculated,
338 (char *)t - (char *)dirent);
339 if (retval)
340 return 0;
341 return ext2fs_le32_to_cpu(t->det_checksum) == calculated;
342 }
343
ext2fs_dirent_csum_set(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)344 static errcode_t ext2fs_dirent_csum_set(ext2_filsys fs, ext2_ino_t inum,
345 struct ext2_dir_entry *dirent)
346 {
347 errcode_t retval;
348 __u32 crc;
349 struct ext2_dir_entry_tail *t;
350
351 retval = __get_dirent_tail(fs, dirent, &t, 1);
352 if (retval)
353 return retval;
354
355 /* swapfs.c functions don't change the checksum endianness */
356 retval = ext2fs_dirent_csum(fs, inum, dirent, &crc,
357 (char *)t - (char *)dirent);
358 if (retval)
359 return retval;
360 t->det_checksum = ext2fs_cpu_to_le32(crc);
361 return 0;
362 }
363
ext2fs_dx_csum(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent,__u32 * crc,int count_offset,int count,struct ext2_dx_tail * t)364 static errcode_t ext2fs_dx_csum(ext2_filsys fs, ext2_ino_t inum,
365 struct ext2_dir_entry *dirent,
366 __u32 *crc, int count_offset, int count,
367 struct ext2_dx_tail *t)
368 {
369 errcode_t retval;
370 char *buf = (char *)dirent;
371 int size;
372 __u32 old_csum, gen;
373 struct ext2_inode inode;
374
375 size = count_offset + (count * sizeof(struct ext2_dx_entry));
376 old_csum = t->dt_checksum;
377 t->dt_checksum = 0;
378
379 retval = ext2fs_read_inode(fs, inum, &inode);
380 if (retval)
381 return retval;
382
383 inum = ext2fs_cpu_to_le32(inum);
384 gen = ext2fs_cpu_to_le32(inode.i_generation);
385 *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
386 sizeof(inum));
387 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
388 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size);
389 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)t,
390 sizeof(struct ext2_dx_tail));
391 t->dt_checksum = old_csum;
392
393 return 0;
394 }
395
ext2fs_dx_csum_verify(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)396 static int ext2fs_dx_csum_verify(ext2_filsys fs, ext2_ino_t inum,
397 struct ext2_dir_entry *dirent)
398 {
399 __u32 calculated;
400 errcode_t retval;
401 struct ext2_dx_countlimit *c;
402 struct ext2_dx_tail *t;
403 int count_offset, limit, count;
404
405 retval = __get_dx_countlimit(fs, dirent, &c, &count_offset, 1);
406 if (retval)
407 return 1;
408 limit = ext2fs_le16_to_cpu(c->limit);
409 count = ext2fs_le16_to_cpu(c->count);
410 if (count_offset + (limit * sizeof(struct ext2_dx_entry)) >
411 fs->blocksize - sizeof(struct ext2_dx_tail))
412 return 0;
413 /* htree structs are accessed in LE order */
414 t = (struct ext2_dx_tail *)(((struct ext2_dx_entry *)c) + limit);
415 retval = ext2fs_dx_csum(fs, inum, dirent, &calculated, count_offset,
416 count, t);
417 if (retval)
418 return 0;
419
420 return ext2fs_le32_to_cpu(t->dt_checksum) == calculated;
421 }
422
ext2fs_dx_csum_set(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)423 static errcode_t ext2fs_dx_csum_set(ext2_filsys fs, ext2_ino_t inum,
424 struct ext2_dir_entry *dirent)
425 {
426 __u32 crc;
427 errcode_t retval = 0;
428 struct ext2_dx_countlimit *c;
429 struct ext2_dx_tail *t;
430 int count_offset, limit, count;
431
432 retval = __get_dx_countlimit(fs, dirent, &c, &count_offset, 1);
433 if (retval)
434 return retval;
435 limit = ext2fs_le16_to_cpu(c->limit);
436 count = ext2fs_le16_to_cpu(c->count);
437 if (count_offset + (limit * sizeof(struct ext2_dx_entry)) >
438 fs->blocksize - sizeof(struct ext2_dx_tail))
439 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
440 t = (struct ext2_dx_tail *)(((struct ext2_dx_entry *)c) + limit);
441
442 /* htree structs are accessed in LE order */
443 retval = ext2fs_dx_csum(fs, inum, dirent, &crc, count_offset, count, t);
444 if (retval)
445 return retval;
446 t->dt_checksum = ext2fs_cpu_to_le32(crc);
447 return retval;
448 }
449
ext2fs_dir_block_csum_verify(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)450 int ext2fs_dir_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
451 struct ext2_dir_entry *dirent)
452 {
453 if (!ext2fs_has_feature_metadata_csum(fs->super))
454 return 1;
455
456 if (__get_dirent_tail(fs, dirent, NULL, 1) == 0)
457 return ext2fs_dirent_csum_verify(fs, inum, dirent);
458 if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0)
459 return ext2fs_dx_csum_verify(fs, inum, dirent);
460
461 return 0;
462 }
463
ext2fs_dir_block_csum_set(ext2_filsys fs,ext2_ino_t inum,struct ext2_dir_entry * dirent)464 errcode_t ext2fs_dir_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
465 struct ext2_dir_entry *dirent)
466 {
467 if (!ext2fs_has_feature_metadata_csum(fs->super))
468 return 0;
469
470 if (__get_dirent_tail(fs, dirent, NULL, 1) == 0)
471 return ext2fs_dirent_csum_set(fs, inum, dirent);
472 if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0)
473 return ext2fs_dx_csum_set(fs, inum, dirent);
474
475 if (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)
476 return 0;
477 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
478 }
479
480 #define EXT3_EXTENT_TAIL_OFFSET(hdr) (sizeof(struct ext3_extent_header) + \
481 (sizeof(struct ext3_extent) * ext2fs_le16_to_cpu((hdr)->eh_max)))
482
get_extent_tail(struct ext3_extent_header * h)483 static struct ext3_extent_tail *get_extent_tail(struct ext3_extent_header *h)
484 {
485 return (struct ext3_extent_tail *)(((char *)h) +
486 EXT3_EXTENT_TAIL_OFFSET(h));
487 }
488
ext2fs_extent_block_csum(ext2_filsys fs,ext2_ino_t inum,struct ext3_extent_header * eh,__u32 * crc)489 static errcode_t ext2fs_extent_block_csum(ext2_filsys fs, ext2_ino_t inum,
490 struct ext3_extent_header *eh,
491 __u32 *crc)
492 {
493 int size;
494 __u32 gen;
495 errcode_t retval;
496 struct ext2_inode inode;
497
498 size = EXT3_EXTENT_TAIL_OFFSET(eh) + offsetof(struct ext3_extent_tail,
499 et_checksum);
500
501 retval = ext2fs_read_inode(fs, inum, &inode);
502 if (retval)
503 return retval;
504 inum = ext2fs_cpu_to_le32(inum);
505 gen = ext2fs_cpu_to_le32(inode.i_generation);
506 *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
507 sizeof(inum));
508 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
509 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)eh, size);
510
511 return 0;
512 }
513
ext2fs_extent_block_csum_verify(ext2_filsys fs,ext2_ino_t inum,struct ext3_extent_header * eh)514 int ext2fs_extent_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
515 struct ext3_extent_header *eh)
516 {
517 errcode_t retval;
518 __u32 provided, calculated;
519 struct ext3_extent_tail *t = get_extent_tail(eh);
520
521 /*
522 * The extent tree structures are accessed in LE order, so we must
523 * swap the checksum bytes here.
524 */
525 if (!ext2fs_has_feature_metadata_csum(fs->super))
526 return 1;
527
528 provided = ext2fs_le32_to_cpu(t->et_checksum);
529 retval = ext2fs_extent_block_csum(fs, inum, eh, &calculated);
530 if (retval)
531 return 0;
532
533 return provided == calculated;
534 }
535
ext2fs_extent_block_csum_set(ext2_filsys fs,ext2_ino_t inum,struct ext3_extent_header * eh)536 errcode_t ext2fs_extent_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
537 struct ext3_extent_header *eh)
538 {
539 errcode_t retval;
540 __u32 crc;
541 struct ext3_extent_tail *t = get_extent_tail(eh);
542
543 if (!ext2fs_has_feature_metadata_csum(fs->super))
544 return 0;
545
546 /*
547 * The extent tree structures are accessed in LE order, so we must
548 * swap the checksum bytes here.
549 */
550 retval = ext2fs_extent_block_csum(fs, inum, eh, &crc);
551 if (retval)
552 return retval;
553 t->et_checksum = ext2fs_cpu_to_le32(crc);
554 return retval;
555 }
556
ext2fs_inode_bitmap_csum_verify(ext2_filsys fs,dgrp_t group,char * bitmap,int size)557 int ext2fs_inode_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
558 char *bitmap, int size)
559 {
560 struct ext4_group_desc *gdp = (struct ext4_group_desc *)
561 ext2fs_group_desc(fs, fs->group_desc, group);
562 __u32 provided, calculated;
563
564 if (!ext2fs_has_feature_metadata_csum(fs->super))
565 return 1;
566 provided = gdp->bg_inode_bitmap_csum_lo;
567 calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap,
568 size);
569 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
570 provided |= (__u32)gdp->bg_inode_bitmap_csum_hi << 16;
571 else
572 calculated &= 0xFFFF;
573
574 return provided == calculated;
575 }
576
ext2fs_inode_bitmap_csum_set(ext2_filsys fs,dgrp_t group,char * bitmap,int size)577 errcode_t ext2fs_inode_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
578 char *bitmap, int size)
579 {
580 __u32 crc;
581 struct ext4_group_desc *gdp = (struct ext4_group_desc *)
582 ext2fs_group_desc(fs, fs->group_desc, group);
583
584 if (!ext2fs_has_feature_metadata_csum(fs->super))
585 return 0;
586
587 crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size);
588 gdp->bg_inode_bitmap_csum_lo = crc & 0xFFFF;
589 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
590 gdp->bg_inode_bitmap_csum_hi = crc >> 16;
591
592 return 0;
593 }
594
ext2fs_block_bitmap_csum_verify(ext2_filsys fs,dgrp_t group,char * bitmap,int size)595 int ext2fs_block_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
596 char *bitmap, int size)
597 {
598 struct ext4_group_desc *gdp = (struct ext4_group_desc *)
599 ext2fs_group_desc(fs, fs->group_desc, group);
600 __u32 provided, calculated;
601
602 if (!ext2fs_has_feature_metadata_csum(fs->super))
603 return 1;
604 provided = gdp->bg_block_bitmap_csum_lo;
605 calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap,
606 size);
607 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
608 provided |= (__u32)gdp->bg_block_bitmap_csum_hi << 16;
609 else
610 calculated &= 0xFFFF;
611
612 return provided == calculated;
613 }
614
ext2fs_block_bitmap_csum_set(ext2_filsys fs,dgrp_t group,char * bitmap,int size)615 errcode_t ext2fs_block_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
616 char *bitmap, int size)
617 {
618 __u32 crc;
619 struct ext4_group_desc *gdp = (struct ext4_group_desc *)
620 ext2fs_group_desc(fs, fs->group_desc, group);
621
622 if (!ext2fs_has_feature_metadata_csum(fs->super))
623 return 0;
624
625 crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size);
626 gdp->bg_block_bitmap_csum_lo = crc & 0xFFFF;
627 if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
628 gdp->bg_block_bitmap_csum_hi = crc >> 16;
629
630 return 0;
631 }
632
ext2fs_inode_csum(ext2_filsys fs,ext2_ino_t inum,struct ext2_inode_large * inode,__u32 * crc,int has_hi)633 static errcode_t ext2fs_inode_csum(ext2_filsys fs, ext2_ino_t inum,
634 struct ext2_inode_large *inode,
635 __u32 *crc, int has_hi)
636 {
637 __u32 gen;
638 struct ext2_inode_large *desc = inode;
639 size_t size = EXT2_INODE_SIZE(fs->super);
640 __u16 old_lo;
641 __u16 old_hi = 0;
642
643 old_lo = inode->i_checksum_lo;
644 inode->i_checksum_lo = 0;
645 if (has_hi) {
646 old_hi = inode->i_checksum_hi;
647 inode->i_checksum_hi = 0;
648 }
649
650 inum = ext2fs_cpu_to_le32(inum);
651 gen = inode->i_generation;
652 *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
653 sizeof(inum));
654 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
655 *crc = ext2fs_crc32c_le(*crc, (unsigned char *)desc, size);
656
657 inode->i_checksum_lo = old_lo;
658 if (has_hi)
659 inode->i_checksum_hi = old_hi;
660 return 0;
661 }
662
ext2fs_inode_csum_verify(ext2_filsys fs,ext2_ino_t inum,struct ext2_inode_large * inode)663 int ext2fs_inode_csum_verify(ext2_filsys fs, ext2_ino_t inum,
664 struct ext2_inode_large *inode)
665 {
666 errcode_t retval;
667 __u32 provided, calculated;
668 unsigned int i, has_hi;
669 char *cp;
670
671 if (!ext2fs_has_feature_metadata_csum(fs->super))
672 return 1;
673
674 has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE &&
675 inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END);
676
677 provided = ext2fs_le16_to_cpu(inode->i_checksum_lo);
678 retval = ext2fs_inode_csum(fs, inum, inode, &calculated, has_hi);
679 if (retval)
680 return 0;
681 if (has_hi) {
682 __u32 hi = ext2fs_le16_to_cpu(inode->i_checksum_hi);
683 provided |= hi << 16;
684 } else
685 calculated &= 0xFFFF;
686
687 if (provided == calculated)
688 return 1;
689
690 /*
691 * If the checksum didn't match, it's possible it was due to
692 * the inode being all zero's. It's unlikely this is the
693 * case, but it can happen. So check for it here. (We only
694 * check the base inode since that's good enough, and it's not
695 * worth the bother to figure out how much of the extended
696 * inode, if any, is present.)
697 */
698 for (cp = (char *) inode, i = 0;
699 i < sizeof(struct ext2_inode);
700 cp++, i++)
701 if (*cp)
702 return 0;
703 return 1; /* Inode must have been all zero's */
704 }
705
ext2fs_inode_csum_set(ext2_filsys fs,ext2_ino_t inum,struct ext2_inode_large * inode)706 errcode_t ext2fs_inode_csum_set(ext2_filsys fs, ext2_ino_t inum,
707 struct ext2_inode_large *inode)
708 {
709 errcode_t retval;
710 __u32 crc;
711 int has_hi;
712
713 if (!ext2fs_has_feature_metadata_csum(fs->super))
714 return 0;
715
716 has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE &&
717 inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END);
718
719 retval = ext2fs_inode_csum(fs, inum, inode, &crc, has_hi);
720 if (retval)
721 return retval;
722 inode->i_checksum_lo = ext2fs_cpu_to_le16(crc & 0xFFFF);
723 if (has_hi)
724 inode->i_checksum_hi = ext2fs_cpu_to_le16(crc >> 16);
725 return 0;
726 }
727
ext2fs_group_desc_csum(ext2_filsys fs,dgrp_t group)728 __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
729 {
730 struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
731 group);
732 size_t offset, size = EXT2_DESC_SIZE(fs->super);
733 __u16 crc = 0;
734 #ifdef WORDS_BIGENDIAN
735 struct ext4_group_desc swabdesc;
736 size_t save_size = size;
737 const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
738 struct ext2_group_desc *save_desc = desc;
739
740 /* Have to swab back to little-endian to do the checksum */
741 if (size > ext4_bg_size)
742 size = ext4_bg_size;
743 memcpy(&swabdesc, desc, size);
744 ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
745 desc = (struct ext2_group_desc *) &swabdesc;
746 group = ext2fs_swab32(group);
747 #endif
748
749 if (ext2fs_has_feature_metadata_csum(fs->super)) {
750 /* new metadata csum code */
751 __u16 old_crc;
752 __u32 crc32;
753
754 old_crc = desc->bg_checksum;
755 desc->bg_checksum = 0;
756 crc32 = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&group,
757 sizeof(group));
758 crc32 = ext2fs_crc32c_le(crc32, (unsigned char *)desc,
759 size);
760 desc->bg_checksum = old_crc;
761 #ifdef WORDS_BIGENDIAN
762 if (save_size > ext4_bg_size)
763 crc32 = ext2fs_crc32c_le(crc32,
764 (unsigned char *)save_desc + ext4_bg_size,
765 save_size - ext4_bg_size);
766 #endif
767 crc = crc32 & 0xFFFF;
768 goto out;
769 }
770
771 /* old crc16 code */
772 offset = offsetof(struct ext2_group_desc, bg_checksum);
773 crc = ext2fs_crc16(~0, fs->super->s_uuid,
774 sizeof(fs->super->s_uuid));
775 crc = ext2fs_crc16(crc, &group, sizeof(group));
776 crc = ext2fs_crc16(crc, desc, offset);
777 offset += sizeof(desc->bg_checksum); /* skip checksum */
778 /* for checksum of struct ext4_group_desc do the rest...*/
779 if (offset < size) {
780 crc = ext2fs_crc16(crc, (char *)desc + offset,
781 size - offset);
782 }
783 #ifdef WORDS_BIGENDIAN
784 /*
785 * If the size of the bg descriptor is greater than 64
786 * bytes, which is the size of the traditional ext4 bg
787 * descriptor, checksum the rest of the descriptor here
788 */
789 if (save_size > ext4_bg_size)
790 crc = ext2fs_crc16(crc, (char *)save_desc + ext4_bg_size,
791 save_size - ext4_bg_size);
792 #endif
793
794 out:
795 return crc;
796 }
797
ext2fs_group_desc_csum_verify(ext2_filsys fs,dgrp_t group)798 int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
799 {
800 if (ext2fs_has_group_desc_csum(fs) &&
801 (ext2fs_bg_checksum(fs, group) !=
802 ext2fs_group_desc_csum(fs, group)))
803 return 0;
804
805 return 1;
806 }
807
ext2fs_group_desc_csum_set(ext2_filsys fs,dgrp_t group)808 void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
809 {
810 if (!ext2fs_has_group_desc_csum(fs))
811 return;
812
813 /* ext2fs_bg_checksum_set() sets the actual checksum field but
814 * does not calculate the checksum itself. */
815 ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group));
816 }
817
find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,__u32 inodes_per_grp,dgrp_t grp_no)818 static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
819 __u32 inodes_per_grp, dgrp_t grp_no)
820 {
821 ext2_ino_t i, start_ino, end_ino;
822
823 start_ino = grp_no * inodes_per_grp + 1;
824 end_ino = start_ino + inodes_per_grp - 1;
825
826 for (i = end_ino; i >= start_ino; i--) {
827 if (ext2fs_fast_test_inode_bitmap2(bitmap, i))
828 return i - start_ino + 1;
829 }
830 return inodes_per_grp;
831 }
832
833 /* update the bitmap flags, set the itable high watermark, and calculate
834 * checksums for the group descriptors */
ext2fs_set_gdt_csum(ext2_filsys fs)835 errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
836 {
837 struct ext2_super_block *sb = fs->super;
838 int dirty = 0;
839 dgrp_t i;
840
841 if (!fs->inode_map)
842 return EXT2_ET_NO_INODE_BITMAP;
843
844 if (!ext2fs_has_group_desc_csum(fs))
845 return 0;
846
847 for (i = 0; i < fs->group_desc_count; i++) {
848 __u32 old_csum = ext2fs_bg_checksum(fs, i);
849 __u32 old_unused = ext2fs_bg_itable_unused(fs, i);
850 __u32 old_flags = ext2fs_bg_flags(fs, i);
851 __u32 old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i);
852 __u32 old_free_blocks_count = ext2fs_bg_free_blocks_count(fs, i);
853
854 if (old_free_blocks_count == sb->s_blocks_per_group &&
855 i != fs->group_desc_count - 1)
856 ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
857
858 if (old_free_inodes_count == sb->s_inodes_per_group) {
859 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
860 ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group);
861 } else {
862 int unused =
863 sb->s_inodes_per_group -
864 find_last_inode_ingrp(fs->inode_map,
865 sb->s_inodes_per_group, i);
866
867 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
868 ext2fs_bg_itable_unused_set(fs, i, unused);
869 }
870
871 ext2fs_group_desc_csum_set(fs, i);
872 if (old_flags != ext2fs_bg_flags(fs, i))
873 dirty = 1;
874 if (old_unused != ext2fs_bg_itable_unused(fs, i))
875 dirty = 1;
876 if (old_csum != ext2fs_bg_checksum(fs, i))
877 dirty = 1;
878 }
879 if (dirty)
880 ext2fs_mark_super_dirty(fs);
881 return 0;
882 }
883
884 #ifdef DEBUG
885 #include "e2p/e2p.h"
886
print_csum(const char * msg,ext2_filsys fs,dgrp_t group)887 void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
888 {
889 __u16 crc1, crc2, crc3;
890 dgrp_t swabgroup;
891 struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
892 group);
893 size_t size = EXT2_DESC_SIZE(fs->super);
894 struct ext2_super_block *sb = fs->super;
895 int offset = offsetof(struct ext2_group_desc, bg_checksum);
896 #ifdef WORDS_BIGENDIAN
897 struct ext4_group_desc swabdesc;
898 struct ext2_group_desc *save_desc = desc;
899 const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
900 size_t save_size = size;
901 #endif
902
903 #ifdef WORDS_BIGENDIAN
904 /* Have to swab back to little-endian to do the checksum */
905 if (size > ext4_bg_size)
906 size = ext4_bg_size;
907 memcpy(&swabdesc, desc, size);
908 ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
909 desc = (struct ext2_group_desc *) &swabdesc;
910
911 swabgroup = ext2fs_swab32(group);
912 #else
913 swabgroup = group;
914 #endif
915
916 crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
917 crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
918 crc3 = ext2fs_crc16(crc2, desc, offset);
919 offset += sizeof(desc->bg_checksum); /* skip checksum */
920 /* for checksum of struct ext4_group_desc do the rest...*/
921 if (offset < size)
922 crc3 = ext2fs_crc16(crc3, (char *)desc + offset, size - offset);
923 #ifdef WORDS_BIGENDIAN
924 if (save_size > ext4_bg_size)
925 crc3 = ext2fs_crc16(crc3, (char *)save_desc + ext4_bg_size,
926 save_size - ext4_bg_size);
927 #endif
928
929 printf("%s UUID %s=%04x, grp %u=%04x: %04x=%04x\n",
930 msg, e2p_uuid2str(sb->s_uuid), crc1, group, crc2, crc3,
931 ext2fs_group_desc_csum(fs, group));
932 }
933
934 unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
935 0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
936
main(int argc,char ** argv)937 int main(int argc, char **argv)
938 {
939 struct ext2_super_block param;
940 errcode_t retval;
941 ext2_filsys fs;
942 int i;
943 __u16 csum1, csum2, csum_known = 0xd3a4;
944
945 memset(¶m, 0, sizeof(param));
946 ext2fs_blocks_count_set(¶m, 32768);
947 #if 0
948 param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
949 param.s_desc_size = 128;
950 csum_known = 0x5b6e;
951 #endif
952
953 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m,
954 test_io_manager, &fs);
955 if (retval) {
956 com_err("setup", retval,
957 "While initializing filesystem");
958 exit(1);
959 }
960 memcpy(fs->super->s_uuid, sb_uuid, 16);
961 fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
962
963 for (i=0; i < fs->group_desc_count; i++) {
964 ext2fs_block_bitmap_loc_set(fs, i, 124);
965 ext2fs_inode_bitmap_loc_set(fs, i, 125);
966 ext2fs_inode_table_loc_set(fs, i, 126);
967 ext2fs_bg_free_blocks_count_set(fs, i, 31119);
968 ext2fs_bg_free_inodes_count_set(fs, i, 15701);
969 ext2fs_bg_used_dirs_count_set(fs, i, 2);
970 ext2fs_bg_flags_zap(fs, i);
971 };
972
973 csum1 = ext2fs_group_desc_csum(fs, 0);
974 print_csum("csum0000", fs, 0);
975
976 if (csum1 != csum_known) {
977 printf("checksum for group 0 should be %04x\n", csum_known);
978 exit(1);
979 }
980 csum2 = ext2fs_group_desc_csum(fs, 1);
981 print_csum("csum0001", fs, 1);
982 if (csum1 == csum2) {
983 printf("checksums for different groups shouldn't match\n");
984 exit(1);
985 }
986 csum2 = ext2fs_group_desc_csum(fs, 2);
987 print_csum("csumffff", fs, 2);
988 if (csum1 == csum2) {
989 printf("checksums for different groups shouldn't match\n");
990 exit(1);
991 }
992 ext2fs_bg_checksum_set(fs, 0, csum1);
993 csum2 = ext2fs_group_desc_csum(fs, 0);
994 print_csum("csum_set", fs, 0);
995 if (csum1 != csum2) {
996 printf("checksums should not depend on checksum field\n");
997 exit(1);
998 }
999 if (!ext2fs_group_desc_csum_verify(fs, 0)) {
1000 printf("checksums should verify against gd_checksum\n");
1001 exit(1);
1002 }
1003 memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
1004 print_csum("new_uuid", fs, 0);
1005 if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
1006 printf("checksums for different filesystems shouldn't match\n");
1007 exit(1);
1008 }
1009 csum1 = ext2fs_group_desc_csum(fs, 0);
1010 ext2fs_bg_checksum_set(fs, 0, csum1);
1011 print_csum("csum_new", fs, 0);
1012 ext2fs_bg_free_blocks_count_set(fs, 0, 1);
1013 csum2 = ext2fs_group_desc_csum(fs, 0);
1014 print_csum("csum_blk", fs, 0);
1015 if (csum1 == csum2) {
1016 printf("checksums for different data shouldn't match\n");
1017 exit(1);
1018 }
1019 ext2fs_free(fs);
1020
1021 return 0;
1022 }
1023 #endif
1024