fscrypt: Replace 1-element array with flexible array
authorKees Cook <keescook@chromium.org>
Tue, 23 May 2023 16:55:02 +0000 (09:55 -0700)
committerEric Biggers <ebiggers@google.com>
Wed, 24 May 2023 02:46:09 +0000 (19:46 -0700)
1-element arrays are deprecated and are being replaced with C99
flexible arrays[1].

As sizes were being calculated with the extra byte intentionally,
propagate the difference so there is no change in binary output.

[1] https://github.com/KSPP/linux/issues/79

Cc: Eric Biggers <ebiggers@kernel.org>
Cc: "Theodore Y. Ts'o" <tytso@mit.edu>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: linux-fscrypt@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20230523165458.gonna.580-kees@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
fs/crypto/fscrypt_private.h
fs/crypto/hooks.c

index 7ab5a7b..2d63da4 100644 (file)
@@ -171,7 +171,7 @@ fscrypt_policy_flags(const union fscrypt_policy *policy)
  */
 struct fscrypt_symlink_data {
        __le16 len;
-       char encrypted_path[1];
+       char encrypted_path[];
 } __packed;
 
 /**
index 9e786ae..6238dbc 100644 (file)
@@ -255,10 +255,10 @@ int fscrypt_prepare_symlink(struct inode *dir, const char *target,
         * for now since filesystems will assume it is there and subtract it.
         */
        if (!__fscrypt_fname_encrypted_size(policy, len,
-                                           max_len - sizeof(struct fscrypt_symlink_data),
+                                           max_len - sizeof(struct fscrypt_symlink_data) - 1,
                                            &disk_link->len))
                return -ENAMETOOLONG;
-       disk_link->len += sizeof(struct fscrypt_symlink_data);
+       disk_link->len += sizeof(struct fscrypt_symlink_data) + 1;
 
        disk_link->name = NULL;
        return 0;
@@ -289,7 +289,7 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
                if (!sd)
                        return -ENOMEM;
        }
-       ciphertext_len = disk_link->len - sizeof(*sd);
+       ciphertext_len = disk_link->len - sizeof(*sd) - 1;
        sd->len = cpu_to_le16(ciphertext_len);
 
        err = fscrypt_fname_encrypt(inode, &iname, sd->encrypted_path,
@@ -367,7 +367,7 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
         * the ciphertext length, even though this is redundant with i_size.
         */
 
-       if (max_size < sizeof(*sd))
+       if (max_size < sizeof(*sd) + 1)
                return ERR_PTR(-EUCLEAN);
        sd = caddr;
        cstr.name = (unsigned char *)sd->encrypted_path;
@@ -376,7 +376,7 @@ const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
        if (cstr.len == 0)
                return ERR_PTR(-EUCLEAN);
 
-       if (cstr.len + sizeof(*sd) - 1 > max_size)
+       if (cstr.len + sizeof(*sd) > max_size)
                return ERR_PTR(-EUCLEAN);
 
        err = fscrypt_fname_alloc_buffer(cstr.len, &pstr);