fanotify: use macros to get the offset to fanotify_info buffer

[ Upstream commit 2d9374f095 ]

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/20211129201537.1932819-6-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
This commit is contained in:
Amir Goldstein 2021-11-29 22:15:31 +02:00 committed by Greg Kroah-Hartman
parent 932090b8ab
commit 3eec21b660
2 changed files with 31 additions and 12 deletions

View file

@ -411,7 +411,7 @@ static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
* be zero in that case if encoding fh len failed.
*/
err = -ENOENT;
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
goto out_err;
/* No external buffer in a variable size allocated fh */

View file

@ -49,6 +49,22 @@ struct fanotify_info {
* (optional) file_fh starts at buf[dir_fh_totlen]
* name starts at buf[dir_fh_totlen + file_fh_totlen]
*/
#define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen)
#define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen)
#define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1)
#define FANOTIFY_DIR_FH_OFFSET(info) 0
#define FANOTIFY_FILE_FH_OFFSET(info) \
(FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info))
#define FANOTIFY_NAME_OFFSET(info) \
(FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info))
#define FANOTIFY_DIR_FH_BUF(info) \
((info)->buf + FANOTIFY_DIR_FH_OFFSET(info))
#define FANOTIFY_FILE_FH_BUF(info) \
((info)->buf + FANOTIFY_FILE_FH_OFFSET(info))
#define FANOTIFY_NAME_BUF(info) \
((info)->buf + FANOTIFY_NAME_OFFSET(info))
} __aligned(4);
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
{
BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
return (struct fanotify_fh *)info->buf;
return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info);
}
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)
static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
{
return (struct fanotify_fh *)(info->buf + info->dir_fh_totlen);
return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info);
}
static inline const char *fanotify_info_name(struct fanotify_info *info)
static inline char *fanotify_info_name(struct fanotify_info *info)
{
return info->buf + info->dir_fh_totlen + info->file_fh_totlen;
if (!info->name_len)
return NULL;
return FANOTIFY_NAME_BUF(info);
}
static inline void fanotify_info_init(struct fanotify_info *info)
{
BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX);
BUILD_BUG_ON(NAME_MAX > U8_MAX);
info->dir_fh_totlen = 0;
info->file_fh_totlen = 0;
info->name_len = 0;
}
static inline unsigned int fanotify_info_len(struct fanotify_info *info)
{
return info->dir_fh_totlen + info->file_fh_totlen + info->name_len;
}
static inline void fanotify_info_copy_name(struct fanotify_info *info,
const struct qstr *name)
{
if (WARN_ON_ONCE(name->len > NAME_MAX))
return;
info->name_len = name->len;
strcpy(info->buf + info->dir_fh_totlen + info->file_fh_totlen,
name->name);
strcpy(fanotify_info_name(info), name->name);
}
/*