Skip to content

Commit 2d9374f

Browse files
amir73iljankara
authored andcommitted
fanotify: use macros to get the offset to fanotify_info buffer
The fanotify_info buffer contains up to two file handles and a name. Use macros to simplify the code that access the different items within the buffer. Add assertions to verify that stored fh len and name len do not overflow the u8 stored value in fanotify_info header. Remove the unused fanotify_info_len() helper. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent e54183f commit 2d9374f

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
411411
* be zero in that case if encoding fh len failed.
412412
*/
413413
err = -ENOENT;
414-
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
414+
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
415415
goto out_err;
416416

417417
/* No external buffer in a variable size allocated fh */

fs/notify/fanotify/fanotify.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ struct fanotify_info {
4949
* (optional) file_fh starts at buf[dir_fh_totlen]
5050
* name starts at buf[dir_fh_totlen + file_fh_totlen]
5151
*/
52+
#define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen)
53+
#define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen)
54+
#define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1)
55+
56+
#define FANOTIFY_DIR_FH_OFFSET(info) 0
57+
#define FANOTIFY_FILE_FH_OFFSET(info) \
58+
(FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info))
59+
#define FANOTIFY_NAME_OFFSET(info) \
60+
(FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info))
61+
62+
#define FANOTIFY_DIR_FH_BUF(info) \
63+
((info)->buf + FANOTIFY_DIR_FH_OFFSET(info))
64+
#define FANOTIFY_FILE_FH_BUF(info) \
65+
((info)->buf + FANOTIFY_FILE_FH_OFFSET(info))
66+
#define FANOTIFY_NAME_BUF(info) \
67+
((info)->buf + FANOTIFY_NAME_OFFSET(info))
5268
} __aligned(4);
5369

5470
static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
@@ -87,7 +103,7 @@ static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *inf
87103
{
88104
BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
89105

90-
return (struct fanotify_fh *)info->buf;
106+
return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info);
91107
}
92108

93109
static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
@@ -101,32 +117,35 @@ static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
101117

102118
static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
103119
{
104-
return (struct fanotify_fh *)(info->buf + info->dir_fh_totlen);
120+
return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info);
105121
}
106122

107-
static inline const char *fanotify_info_name(struct fanotify_info *info)
123+
static inline char *fanotify_info_name(struct fanotify_info *info)
108124
{
109-
return info->buf + info->dir_fh_totlen + info->file_fh_totlen;
125+
if (!info->name_len)
126+
return NULL;
127+
128+
return FANOTIFY_NAME_BUF(info);
110129
}
111130

112131
static inline void fanotify_info_init(struct fanotify_info *info)
113132
{
133+
BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX);
134+
BUILD_BUG_ON(NAME_MAX > U8_MAX);
135+
114136
info->dir_fh_totlen = 0;
115137
info->file_fh_totlen = 0;
116138
info->name_len = 0;
117139
}
118140

119-
static inline unsigned int fanotify_info_len(struct fanotify_info *info)
120-
{
121-
return info->dir_fh_totlen + info->file_fh_totlen + info->name_len;
122-
}
123-
124141
static inline void fanotify_info_copy_name(struct fanotify_info *info,
125142
const struct qstr *name)
126143
{
144+
if (WARN_ON_ONCE(name->len > NAME_MAX))
145+
return;
146+
127147
info->name_len = name->len;
128-
strcpy(info->buf + info->dir_fh_totlen + info->file_fh_totlen,
129-
name->name);
148+
strcpy(fanotify_info_name(info), name->name);
130149
}
131150

132151
/*

0 commit comments

Comments
 (0)