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