Skip to content

Commit 4bf798e

Browse files
Christophe Leroy (CS GROUP)brauner
authored andcommitted
readdir: Introduce dirent_size()
In several places in readdir.c there are calculations of the total size of a dirent, which contains a few fixed fields plus a name field with variable size. To add fun every dirent is of a slightly different type: - struct old_linux_dirent - struct linux_dirent - struct linux_dirent64 - struct compat_old_linux_dirent - struct compat_linux_dirent Replace ugly size calculation by a macro called dirent_size() which calculates the size of the structure based on the pointed type and the name field len. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org> Link: https://patch.msgid.link/c20d2f8f6817a39401155cfc80f0dff88df116e0.1774350128.git.chleroy@kernel.org Reviewed-by: David Laight <david.laight.linux@gmail.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent f30186b commit 4bf798e

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

fs/readdir.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/compat.h>
2323
#include <linux/uaccess.h>
2424

25+
#define dirent_size(dirent, len) offsetof(typeof(*(dirent)), d_name[len])
26+
2527
/*
2628
* Some filesystems were never converted to '->iterate_shared()'
2729
* and their directory iterators want the inode lock held for
@@ -198,9 +200,7 @@ static bool fillonedir(struct dir_context *ctx, const char *name, int namlen,
198200
}
199201
buf->result++;
200202
dirent = buf->dirent;
201-
if (!user_write_access_begin(dirent,
202-
(unsigned long)(dirent->d_name + namlen + 1) -
203-
(unsigned long)dirent))
203+
if (!user_write_access_begin(dirent, dirent_size(dirent, namlen + 1)))
204204
goto efault;
205205
unsafe_put_user(d_ino, &dirent->d_ino, efault_end);
206206
unsafe_put_user(offset, &dirent->d_offset, efault_end);
@@ -263,8 +263,7 @@ static bool filldir(struct dir_context *ctx, const char *name, int namlen,
263263
struct getdents_callback *buf =
264264
container_of(ctx, struct getdents_callback, ctx);
265265
unsigned long d_ino;
266-
int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
267-
sizeof(long));
266+
int reclen = ALIGN(dirent_size(dirent, namlen + 2), sizeof(long));
268267
int prev_reclen;
269268
unsigned int flags = d_type;
270269

@@ -352,8 +351,7 @@ static bool filldir64(struct dir_context *ctx, const char *name, int namlen,
352351
struct linux_dirent64 __user *dirent, *prev;
353352
struct getdents_callback64 *buf =
354353
container_of(ctx, struct getdents_callback64, ctx);
355-
int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
356-
sizeof(u64));
354+
int reclen = ALIGN(dirent_size(dirent, namlen + 1), sizeof(u64));
357355
int prev_reclen;
358356
unsigned int flags = d_type;
359357

@@ -460,9 +458,7 @@ static bool compat_fillonedir(struct dir_context *ctx, const char *name,
460458
}
461459
buf->result++;
462460
dirent = buf->dirent;
463-
if (!user_write_access_begin(dirent,
464-
(unsigned long)(dirent->d_name + namlen + 1) -
465-
(unsigned long)dirent))
461+
if (!user_write_access_begin(dirent, dirent_size(dirent, namlen + 1)))
466462
goto efault;
467463
unsafe_put_user(d_ino, &dirent->d_ino, efault_end);
468464
unsafe_put_user(offset, &dirent->d_offset, efault_end);
@@ -519,8 +515,7 @@ static bool compat_filldir(struct dir_context *ctx, const char *name, int namlen
519515
struct compat_getdents_callback *buf =
520516
container_of(ctx, struct compat_getdents_callback, ctx);
521517
compat_ulong_t d_ino;
522-
int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
523-
namlen + 2, sizeof(compat_long_t));
518+
int reclen = ALIGN(dirent_size(dirent, namlen + 2), sizeof(compat_long_t));
524519
int prev_reclen;
525520
unsigned int flags = d_type;
526521

0 commit comments

Comments
 (0)