Skip to content

Commit 8b9bf58

Browse files
tobluxtyhicks
authored andcommitted
ecryptfs: Use struct_size to improve process_response + send_miscdev
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>
1 parent f7a1c02 commit 8b9bf58

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

fs/ecryptfs/messaging.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
77
* Tyler Hicks <code@tyhicks.com>
88
*/
9+
#include <linux/overflow.h>
910
#include <linux/sched.h>
1011
#include <linux/slab.h>
1112
#include <linux/user_namespace.h>
@@ -232,7 +233,7 @@ int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
232233
msg_ctx->counter, seq);
233234
goto unlock;
234235
}
235-
msg_size = (sizeof(*msg) + msg->data_len);
236+
msg_size = struct_size(msg, data, msg->data_len);
236237
msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
237238
if (!msg_ctx->msg) {
238239
rc = -ENOMEM;

fs/ecryptfs/miscdev.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/hash.h>
1111
#include <linux/random.h>
1212
#include <linux/miscdevice.h>
13+
#include <linux/overflow.h>
1314
#include <linux/poll.h>
1415
#include <linux/slab.h>
1516
#include <linux/wait.h>
@@ -148,8 +149,10 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
148149
u16 msg_flags, struct ecryptfs_daemon *daemon)
149150
{
150151
struct ecryptfs_message *msg;
152+
size_t msg_size;
151153

152-
msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
154+
msg_size = struct_size(msg, data, data_size);
155+
msg = kmalloc(msg_size, GFP_KERNEL);
153156
if (!msg)
154157
return -ENOMEM;
155158

@@ -159,7 +162,7 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
159162
msg_ctx->msg->data_len = data_size;
160163
msg_ctx->type = msg_type;
161164
memcpy(msg_ctx->msg->data, data, data_size);
162-
msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
165+
msg_ctx->msg_size = msg_size;
163166
list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
164167
mutex_unlock(&msg_ctx->mux);
165168

0 commit comments

Comments
 (0)