diff options
author | Theodore Ts'o <tytso@mit.edu> | 2024-04-18 10:04:27 -0400 |
---|---|---|
committer | Theodore Ts'o <tytso@mit.edu> | 2024-04-18 10:23:56 -0400 |
commit | 9103e1e792170a836884db4ee9f2762bf1684f09 (patch) | |
tree | 827b2fefc58b9a2a5c670d662ea77b4a8d23b36d | |
parent | 5182bd6826c6ec5aab2b3cb90816d6d480d8e574 (diff) | |
download | e2fsprogs-9103e1e792170a836884db4ee9f2762bf1684f09.tar.gz |
debugfs: fix set_field's handling of timestamps
How timestamps are encoded in inodes and superblocks are different.
Unfortunately, commit ca8bc9240a00 which added post-2038 timestamps
was (a) overwriting adjacent superblock fields and/or attempting
unaligned writes to a 8-bit field from a 32-bit pointer, and (b) using
the incorrect encoding for timestamps stored in inodes. Fix both of
these issues, which were found thanks to UBSAN.
Fixes: ca8bc9240a00 ("Add post-2038 timestamp support to e2fsprogs")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r-- | debugfs/set_fields.c | 66 |
1 files changed, 49 insertions, 17 deletions
diff --git a/debugfs/set_fields.c b/debugfs/set_fields.c index 6755f8802..4980a63d8 100644 --- a/debugfs/set_fields.c +++ b/debugfs/set_fields.c @@ -71,7 +71,9 @@ static errcode_t parse_string(struct field_set_info *info, char *field, char *ar static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg); static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg); static errcode_t parse_encoding(struct field_set_info *info, char *field, char *arg); -static errcode_t parse_time(struct field_set_info *info, char *field, char *arg); +static errcode_t parse_sb_time(struct field_set_info *info, char *field, char *arg); +static errcode_t parse_ino_time(struct field_set_info *info, char *field, char *arg); + static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg); static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg); static errcode_t parse_inode_csum(struct field_set_info *info, char *field, @@ -99,8 +101,8 @@ static struct field_set_info super_fields[] = { { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint }, { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint }, { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint }, - { "mtime", &set_sb.s_mtime, &set_sb.s_mtime_hi, 5, parse_time }, - { "wtime", &set_sb.s_wtime, &set_sb.s_wtime_hi, 5, parse_time }, + { "mtime", &set_sb.s_mtime, &set_sb.s_mtime_hi, 5, parse_sb_time }, + { "wtime", &set_sb.s_wtime, &set_sb.s_wtime_hi, 5, parse_sb_time }, { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint }, { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int }, /* s_magic */ @@ -108,7 +110,7 @@ static struct field_set_info super_fields[] = { { "errors", &set_sb.s_errors, NULL, 2, parse_uint }, { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint }, { "lastcheck", &set_sb.s_lastcheck, &set_sb.s_lastcheck_hi, 5, - parse_time }, + parse_sb_time }, { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint }, { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint }, { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint }, @@ -141,7 +143,7 @@ static struct field_set_info super_fields[] = { { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint }, { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint }, { "mkfs_time", &set_sb.s_mkfs_time, &set_sb.s_mkfs_time_hi, 5, - parse_time }, + parse_sb_time }, { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY, 17 }, { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint }, @@ -170,13 +172,13 @@ static struct field_set_info super_fields[] = { { "encryption_level", &set_sb.s_encryption_level, NULL, 1, parse_uint }, { "error_count", &set_sb.s_error_count, NULL, 4, parse_uint }, { "first_error_time", &set_sb.s_first_error_time, - &set_sb.s_first_error_time_hi, 5, parse_time }, + &set_sb.s_first_error_time_hi, 5, parse_sb_time }, { "first_error_ino", &set_sb.s_first_error_ino, NULL, 4, parse_uint }, { "first_error_block", &set_sb.s_first_error_block, NULL, 8, parse_uint }, { "first_error_func", &set_sb.s_first_error_func, NULL, 32, parse_string }, { "first_error_line", &set_sb.s_first_error_line, NULL, 4, parse_uint }, { "last_error_time", &set_sb.s_last_error_time, - &set_sb.s_last_error_time_hi, 5, parse_time }, + &set_sb.s_last_error_time_hi, 5, parse_sb_time }, { "last_error_ino", &set_sb.s_last_error_ino, NULL, 4, parse_uint }, { "last_error_block", &set_sb.s_last_error_block, NULL, 8, parse_uint }, { "last_error_func", &set_sb.s_last_error_func, NULL, 32, parse_string }, @@ -197,13 +199,13 @@ static struct field_set_info inode_fields[] = { 2, parse_uint }, { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint }, { "atime", &set_inode.i_atime, &set_inode.i_atime_extra, - 4, parse_time }, + 4, parse_ino_time }, { "ctime", &set_inode.i_ctime, &set_inode.i_ctime_extra, - 4, parse_time }, + 4, parse_ino_time }, { "mtime", &set_inode.i_mtime, &set_inode.i_mtime_extra, - 4, parse_time }, + 4, parse_ino_time }, { "dtime", &set_inode.i_dtime, NULL, - 4, parse_time }, + 4, parse_ino_time }, { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high, 2, parse_uint }, { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint }, @@ -240,7 +242,7 @@ static struct field_set_info inode_fields[] = { { "atime_extra", &set_inode.i_atime_extra, NULL, 4, parse_uint, FLAG_ALIAS }, { "crtime", &set_inode.i_crtime, &set_inode.i_crtime_extra, - 4, parse_time }, + 4, parse_ino_time }, { "crtime_extra", &set_inode.i_crtime_extra, NULL, 4, parse_uint, FLAG_ALIAS }, { "projid", &set_inode.i_projid, NULL, 4, parse_uint }, @@ -581,18 +583,19 @@ static errcode_t parse_string(struct field_set_info *info, return 0; } -static errcode_t parse_time(struct field_set_info *info, - char *field, char *arg) +static errcode_t parse_sb_time(struct field_set_info *info, + char *field, char *arg) { __s64 t; __u32 t_low, t_high; - __u32 *ptr_low, *ptr_high; + __u32 *ptr_low; + __u8 *ptr_high; if (check_suffix(field)) return parse_uint(info, field, arg); ptr_low = (__u32 *) info->ptr; - ptr_high = (__u32 *) info->ptr2; + ptr_high = (__u8 *) info->ptr2; t = string_to_time(arg); @@ -609,6 +612,34 @@ static errcode_t parse_time(struct field_set_info *info, return 0; } +static errcode_t parse_ino_time(struct field_set_info *info, + char *field, char *arg) +{ + __s64 t; + __u32 t_low, t_high; + __u32 *ptr_low, *ptr_high; + + if (check_suffix(field)) + return parse_uint(info, field, arg); + + ptr_low = (__u32 *) info->ptr; + ptr_high = (__u32 *) info->ptr2; + + t = string_to_time(arg); + + if (t == -1) { + fprintf(stderr, "Couldn't parse '%s' for field %s.\n", + arg, info->name); + return EINVAL; + } + t_low = (__u32) t; + t_high = __encode_extra_time(t, 0); + *ptr_low = t_low; + if (ptr_high) + *ptr_high = t_high; + return 0; +} + static errcode_t parse_uuid(struct field_set_info *info, char *field EXT2FS_ATTR((unused)), char *arg) { @@ -790,7 +821,8 @@ static void print_possible_fields(struct field_set_info *fields) type = "UUID"; else if (ss->func == parse_hashalg) type = "hash algorithm"; - else if (ss->func == parse_time) + else if ((ss->func == parse_sb_time) || + (ss->func == parse_ino_time)) type = "date/time"; else if (ss->func == parse_bmap) type = "set physical->logical block map"; |