Skip to content

Commit f35c415

Browse files
amir73iljankara
authored andcommitted
fanotify: no external fh buffer in fanotify_name_event
The fanotify_fh struct has an inline buffer of size 12 which is enough to store the most common local filesystem file handles (e.g. ext4, xfs). For file handles that do not fit in the inline buffer (e.g. btrfs), an external buffer is allocated to store the file handle. When allocating a variable size fanotify_name_event, there is no point in allocating also an external fh buffer when file handle does not fit in the inline buffer. Check required size for encoding fh, preallocate an event buffer sufficient to contain both file handle and name and store the name after the file handle. At this time, when not reporting name in event, we still allocate the fixed size fanotify_fid_event and an external buffer for large file handles, but fanotify_alloc_name_event() has already been prepared to accept a NULL file_name. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent f454fa6 commit f35c415

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -298,56 +298,79 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
298298
return test_mask & user_mask;
299299
}
300300

301+
/*
302+
* Check size needed to encode fanotify_fh.
303+
*
304+
* Return size of encoded fh without fanotify_fh header.
305+
* Return 0 on failure to encode.
306+
*/
307+
static int fanotify_encode_fh_len(struct inode *inode)
308+
{
309+
int dwords = 0;
310+
311+
if (!inode)
312+
return 0;
313+
314+
exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
315+
316+
return dwords << 2;
317+
}
318+
301319
/*
302320
* Encode fanotify_fh.
303321
*
304322
* Return total size of encoded fh including fanotify_fh header.
305323
* Return 0 on failure to encode.
306324
*/
307325
static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
308-
gfp_t gfp)
326+
unsigned int fh_len, gfp_t gfp)
309327
{
310-
int dwords, type, bytes = 0;
328+
int dwords, type = 0;
311329
char *ext_buf = NULL;
312330
void *buf = fh->buf;
313331
int err;
314332

315333
fh->type = FILEID_ROOT;
316334
fh->len = 0;
335+
fh->flags = 0;
317336
if (!inode)
318337
return 0;
319338

320-
dwords = 0;
339+
/*
340+
* !gpf means preallocated variable size fh, but fh_len could
341+
* be zero in that case if encoding fh len failed.
342+
*/
321343
err = -ENOENT;
322-
type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
323-
if (!dwords)
344+
if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
324345
goto out_err;
325346

326-
bytes = dwords << 2;
327-
if (bytes > FANOTIFY_INLINE_FH_LEN) {
328-
/* Treat failure to allocate fh as failure to allocate event */
347+
/* No external buffer in a variable size allocated fh */
348+
if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
349+
/* Treat failure to allocate fh as failure to encode fh */
329350
err = -ENOMEM;
330-
ext_buf = kmalloc(bytes, gfp);
351+
ext_buf = kmalloc(fh_len, gfp);
331352
if (!ext_buf)
332353
goto out_err;
333354

334355
*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
335356
buf = ext_buf;
357+
fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
336358
}
337359

360+
dwords = fh_len >> 2;
338361
type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
339362
err = -EINVAL;
340-
if (!type || type == FILEID_INVALID || bytes != dwords << 2)
363+
if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
341364
goto out_err;
342365

343366
fh->type = type;
344-
fh->len = bytes;
367+
fh->len = fh_len;
345368

346-
return FANOTIFY_FH_HDR_LEN + bytes;
369+
return FANOTIFY_FH_HDR_LEN + fh_len;
347370

348371
out_err:
349372
pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
350-
type, bytes, err);
373+
type, fh_len, err);
351374
kfree(ext_buf);
352375
*fanotify_fh_ext_buf_ptr(fh) = NULL;
353376
/* Report the event without a file identifier on encode error */
@@ -419,7 +442,8 @@ static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
419442

420443
ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
421444
ffe->fsid = *fsid;
422-
fanotify_encode_fh(&ffe->object_fh, id, gfp);
445+
fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
446+
gfp);
423447

424448
return &ffe->fae;
425449
}
@@ -432,8 +456,13 @@ static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
432456
struct fanotify_name_event *fne;
433457
struct fanotify_info *info;
434458
struct fanotify_fh *dfh;
459+
unsigned int dir_fh_len = fanotify_encode_fh_len(id);
460+
unsigned int size;
435461

436-
fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
462+
size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
463+
if (file_name)
464+
size += file_name->len + 1;
465+
fne = kmalloc(size, gfp);
437466
if (!fne)
438467
return NULL;
439468

@@ -442,8 +471,13 @@ static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
442471
info = &fne->info;
443472
fanotify_info_init(info);
444473
dfh = fanotify_info_dir_fh(info);
445-
info->dir_fh_totlen = fanotify_encode_fh(dfh, id, gfp);
446-
fanotify_info_copy_name(info, file_name);
474+
info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, 0);
475+
if (file_name)
476+
fanotify_info_copy_name(info, file_name);
477+
478+
pr_debug("%s: ino=%lu size=%u dir_fh_len=%u name_len=%u name='%.*s'\n",
479+
__func__, id->i_ino, size, dir_fh_len,
480+
info->name_len, info->name_len, fanotify_info_name(info));
447481

448482
return &fne->fae;
449483
}
@@ -658,12 +692,7 @@ static void fanotify_free_fid_event(struct fanotify_event *event)
658692

659693
static void fanotify_free_name_event(struct fanotify_event *event)
660694
{
661-
struct fanotify_name_event *fne = FANOTIFY_NE(event);
662-
struct fanotify_fh *dfh = fanotify_info_dir_fh(&fne->info);
663-
664-
if (fanotify_fh_has_ext_buf(dfh))
665-
kfree(fanotify_fh_ext_buf(dfh));
666-
kfree(fne);
695+
kfree(FANOTIFY_NE(event));
667696
}
668697

669698
static void fanotify_free_event(struct fsnotify_event *fsn_event)

fs/notify/fanotify/fanotify.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ enum {
2929
struct fanotify_fh {
3030
u8 type;
3131
u8 len;
32-
u8 pad[2];
33-
unsigned char buf[FANOTIFY_INLINE_FH_LEN];
32+
#define FANOTIFY_FH_FLAG_EXT_BUF 1
33+
u8 flags;
34+
u8 pad;
35+
unsigned char buf[];
3436
} __aligned(4);
3537

3638
/* Variable size struct for dir file handle + child file handle + name */
@@ -50,7 +52,7 @@ struct fanotify_info {
5052

5153
static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
5254
{
53-
return fh->len > FANOTIFY_INLINE_FH_LEN;
55+
return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
5456
}
5557

5658
static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
@@ -154,6 +156,8 @@ struct fanotify_fid_event {
154156
struct fanotify_event fae;
155157
__kernel_fsid_t fsid;
156158
struct fanotify_fh object_fh;
159+
/* Reserve space in object_fh.buf[] - access with fanotify_fh_buf() */
160+
unsigned char _inline_fh_buf[FANOTIFY_INLINE_FH_LEN];
157161
};
158162

159163
static inline struct fanotify_fid_event *
@@ -166,8 +170,6 @@ struct fanotify_name_event {
166170
struct fanotify_event fae;
167171
__kernel_fsid_t fsid;
168172
struct fanotify_info info;
169-
/* Reserve space in info.buf[] - access with fanotify_info_dir_fh() */
170-
struct fanotify_fh _dir_fh;
171173
};
172174

173175
static inline struct fanotify_name_event *

0 commit comments

Comments
 (0)