Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ext4: improve xattr consistency checking and error reporting

Refactor the in-inode and xattr block consistency checking, and report
more fine-grained reports of the consistency problems. Also add more
consistency checks for ea_inode number.

Reviewed-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Link: https://lore.kernel.org/r/20221214200818.870087-1-tytso@mit.edu
Signed-off-by: Theodore Ts'o <tytso@mit.edu>

+80 -46
+80 -46
fs/ext4/xattr.c
··· 184 184 } 185 185 186 186 static int 187 - ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end, 188 - void *value_start) 187 + check_xattrs(struct inode *inode, struct buffer_head *bh, 188 + struct ext4_xattr_entry *entry, void *end, void *value_start, 189 + const char *function, unsigned int line) 189 190 { 190 191 struct ext4_xattr_entry *e = entry; 192 + int err = -EFSCORRUPTED; 193 + char *err_str; 194 + 195 + if (bh) { 196 + if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || 197 + BHDR(bh)->h_blocks != cpu_to_le32(1)) { 198 + err_str = "invalid header"; 199 + goto errout; 200 + } 201 + if (buffer_verified(bh)) 202 + return 0; 203 + if (!ext4_xattr_block_csum_verify(inode, bh)) { 204 + err = -EFSBADCRC; 205 + err_str = "invalid checksum"; 206 + goto errout; 207 + } 208 + } else { 209 + struct ext4_xattr_ibody_header *header = value_start; 210 + 211 + header -= 1; 212 + if (end - (void *)header < sizeof(*header) + sizeof(u32)) { 213 + err_str = "in-inode xattr block too small"; 214 + goto errout; 215 + } 216 + if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 217 + err_str = "bad magic number in in-inode xattr"; 218 + goto errout; 219 + } 220 + } 191 221 192 222 /* Find the end of the names list */ 193 223 while (!IS_LAST_ENTRY(e)) { 194 224 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); 195 - if ((void *)next >= end) 196 - return -EFSCORRUPTED; 197 - if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) 198 - return -EFSCORRUPTED; 225 + if ((void *)next >= end) { 226 + err_str = "e_name out of bounds"; 227 + goto errout; 228 + } 229 + if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) { 230 + err_str = "bad e_name length"; 231 + goto errout; 232 + } 199 233 e = next; 200 234 } 201 235 202 236 /* Check the values */ 203 237 while (!IS_LAST_ENTRY(entry)) { 204 238 u32 size = le32_to_cpu(entry->e_value_size); 239 + unsigned long ea_ino = le32_to_cpu(entry->e_value_inum); 205 240 206 - if (size > EXT4_XATTR_SIZE_MAX) 207 - return -EFSCORRUPTED; 241 + if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) { 242 + err_str = "ea_inode specified without ea_inode feature enabled"; 243 + goto errout; 244 + } 245 + if (ea_ino && ((ea_ino == EXT4_ROOT_INO) || 246 + !ext4_valid_inum(inode->i_sb, ea_ino))) { 247 + err_str = "invalid ea_ino"; 248 + goto errout; 249 + } 250 + if (size > EXT4_XATTR_SIZE_MAX) { 251 + err_str = "e_value size too large"; 252 + goto errout; 253 + } 208 254 209 255 if (size != 0 && entry->e_value_inum == 0) { 210 256 u16 offs = le16_to_cpu(entry->e_value_offs); ··· 262 216 * the padded and unpadded sizes, since the size may 263 217 * overflow to 0 when adding padding. 264 218 */ 265 - if (offs > end - value_start) 266 - return -EFSCORRUPTED; 219 + if (offs > end - value_start) { 220 + err_str = "e_value out of bounds"; 221 + goto errout; 222 + } 267 223 value = value_start + offs; 268 224 if (value < (void *)e + sizeof(u32) || 269 225 size > end - value || 270 - EXT4_XATTR_SIZE(size) > end - value) 271 - return -EFSCORRUPTED; 226 + EXT4_XATTR_SIZE(size) > end - value) { 227 + err_str = "overlapping e_value "; 228 + goto errout; 229 + } 272 230 } 273 231 entry = EXT4_XATTR_NEXT(entry); 274 232 } 275 - 233 + if (bh) 234 + set_buffer_verified(bh); 276 235 return 0; 236 + 237 + errout: 238 + if (bh) 239 + __ext4_error_inode(inode, function, line, 0, -err, 240 + "corrupted xattr block %llu: %s", 241 + (unsigned long long) bh->b_blocknr, 242 + err_str); 243 + else 244 + __ext4_error_inode(inode, function, line, 0, -err, 245 + "corrupted in-inode xattr: %s", err_str); 246 + return err; 277 247 } 278 248 279 249 static inline int 280 250 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh, 281 251 const char *function, unsigned int line) 282 252 { 283 - int error = -EFSCORRUPTED; 284 - 285 - if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || 286 - BHDR(bh)->h_blocks != cpu_to_le32(1)) 287 - goto errout; 288 - if (buffer_verified(bh)) 289 - return 0; 290 - 291 - error = -EFSBADCRC; 292 - if (!ext4_xattr_block_csum_verify(inode, bh)) 293 - goto errout; 294 - error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size, 295 - bh->b_data); 296 - errout: 297 - if (error) 298 - __ext4_error_inode(inode, function, line, 0, -error, 299 - "corrupted xattr block %llu", 300 - (unsigned long long) bh->b_blocknr); 301 - else 302 - set_buffer_verified(bh); 303 - return error; 253 + return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size, 254 + bh->b_data, function, line); 304 255 } 305 256 306 257 #define ext4_xattr_check_block(inode, bh) \ 307 258 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__) 308 259 309 260 310 - static int 261 + static inline int 311 262 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, 312 263 void *end, const char *function, unsigned int line) 313 264 { 314 - int error = -EFSCORRUPTED; 315 - 316 - if (end - (void *)header < sizeof(*header) + sizeof(u32) || 317 - (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) 318 - goto errout; 319 - error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header)); 320 - errout: 321 - if (error) 322 - __ext4_error_inode(inode, function, line, 0, -error, 323 - "corrupted in-inode xattr"); 324 - return error; 265 + return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header), 266 + function, line); 325 267 } 326 268 327 269 #define xattr_check_inode(inode, header, end) \