]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ext4: annotate struct ext4_xattr_inode_array with __counted_by()
authorThorsten Blum <thorsten.blum@toblux.com>
Tue, 30 Jul 2024 22:02:02 +0000 (00:02 +0200)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 27 Aug 2024 03:40:06 +0000 (23:40 -0400)
Add the __counted_by compiler attribute to the flexible array member
inodes to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Remove the now obsolete comment on the count field.

In ext4_expand_inode_array(), use struct_size() instead of offsetof()
and remove the local variable count. Increment the count field before
adding a new inode to the inodes array.

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
Link: https://patch.msgid.link/20240730220200.410939-3-thorsten.blum@toblux.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/xattr.c
fs/ext4/xattr.h

index 31c5189e131fec958d7caa15a61d6a0d59dee8b5..c14a674c3e3def8beb960d63f9bc9b9be556bda4 100644 (file)
@@ -2879,33 +2879,31 @@ ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
        if (*ea_inode_array == NULL) {
                /*
                 * Start with 15 inodes, so it fits into a power-of-two size.
-                * If *ea_inode_array is NULL, this is essentially offsetof()
                 */
-               (*ea_inode_array) =
-                       kmalloc(offsetof(struct ext4_xattr_inode_array,
-                                        inodes[EIA_MASK]),
-                               GFP_NOFS);
+               (*ea_inode_array) = kmalloc(
+                       struct_size(*ea_inode_array, inodes, EIA_MASK),
+                       GFP_NOFS);
                if (*ea_inode_array == NULL)
                        return -ENOMEM;
                (*ea_inode_array)->count = 0;
        } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
                /* expand the array once all 15 + n * 16 slots are full */
                struct ext4_xattr_inode_array *new_array = NULL;
-               int count = (*ea_inode_array)->count;
 
-               /* if new_array is NULL, this is essentially offsetof() */
                new_array = kmalloc(
-                               offsetof(struct ext4_xattr_inode_array,
-                                        inodes[count + EIA_INCR]),
-                               GFP_NOFS);
+                       struct_size(*ea_inode_array, inodes,
+                                   (*ea_inode_array)->count + EIA_INCR),
+                       GFP_NOFS);
                if (new_array == NULL)
                        return -ENOMEM;
                memcpy(new_array, *ea_inode_array,
-                      offsetof(struct ext4_xattr_inode_array, inodes[count]));
+                      struct_size(*ea_inode_array, inodes,
+                                  (*ea_inode_array)->count));
                kfree(*ea_inode_array);
                *ea_inode_array = new_array;
        }
-       (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
+       (*ea_inode_array)->count++;
+       (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode;
        return 0;
 }
 
index 7df0d77643c70fda86638a1a95b601b6f130b50e..b25c2d7b5f991563881e945d8e5e0d2030551790 100644 (file)
@@ -129,8 +129,8 @@ struct ext4_xattr_ibody_find {
 };
 
 struct ext4_xattr_inode_array {
-       unsigned int count;             /* # of used items in the array */
-       struct inode *inodes[];
+       unsigned int count;
+       struct inode *inodes[] __counted_by(count);
 };
 
 extern const struct xattr_handler ext4_xattr_user_handler;