]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ecryptfs: Use struct_size to improve process_response + send_miscdev
authorThorsten Blum <thorsten.blum@linux.dev>
Thu, 5 Feb 2026 13:24:51 +0000 (14:24 +0100)
committerTyler Hicks <code@tyhicks.com>
Thu, 26 Mar 2026 03:39:27 +0000 (22:39 -0500)
Use struct_size(), which provides additional compile-time checks for
structures with flexible array members (e.g., __must_be_array()), to
determine the allocation size for a new 'struct ecryptfs_message'.

In send_miscdev(), reuse 'msg_size' instead of recalculating it.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Signed-off-by: Tyler Hicks <code@tyhicks.com>
fs/ecryptfs/messaging.c
fs/ecryptfs/miscdev.c

index 30c8e15d87b5c85bfeb770738dd058224edd0d14..03c60f0850ca6178b415ad57b0791ad4a1e8e429 100644 (file)
@@ -6,6 +6,7 @@
  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  *             Tyler Hicks <code@tyhicks.com>
  */
+#include <linux/overflow.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/user_namespace.h>
@@ -232,7 +233,7 @@ int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
                       msg_ctx->counter, seq);
                goto unlock;
        }
-       msg_size = (sizeof(*msg) + msg->data_len);
+       msg_size = struct_size(msg, data, msg->data_len);
        msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
        if (!msg_ctx->msg) {
                rc = -ENOMEM;
index 4e62c3cef70fbb59b8ae0e14e2a4710b53e23011..5a7d081499227a715719853e84a3891b0ce4f79b 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/hash.h>
 #include <linux/random.h>
 #include <linux/miscdevice.h>
+#include <linux/overflow.h>
 #include <linux/poll.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
@@ -148,8 +149,10 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
                          u16 msg_flags, struct ecryptfs_daemon *daemon)
 {
        struct ecryptfs_message *msg;
+       size_t msg_size;
 
-       msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
+       msg_size = struct_size(msg, data, data_size);
+       msg = kmalloc(msg_size, GFP_KERNEL);
        if (!msg)
                return -ENOMEM;
 
@@ -159,7 +162,7 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
        msg_ctx->msg->data_len = data_size;
        msg_ctx->type = msg_type;
        memcpy(msg_ctx->msg->data, data, data_size);
-       msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
+       msg_ctx->msg_size = msg_size;
        list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
        mutex_unlock(&msg_ctx->mux);