Skip to content

Commit 936d6a3

Browse files
krisman-at-collaborajankara
authored andcommitted
fanotify: Report fid info for file related file system errors
Plumb the pieces to add a FID report to error records. Since all error event memory must be pre-allocated, we pre-allocate the maximum file handle size possible, such that it should always fit. For errors that don't expose a file handle, report it with an invalid FID. Internally we use zero-length FILEID_ROOT file handle for passing the information (which we report as zero-length FILEID_INVALID file handle to userspace) so we update the handle reporting code to deal with this case correctly. Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Gabriel Krisman Bertazi <[email protected]> Reviewed-by: Amir Goldstein <[email protected]> Reviewed-by: Jan Kara <[email protected]> [Folded two patches into 2 to make series bisectable] Signed-off-by: Jan Kara <[email protected]>
1 parent 572c28f commit 936d6a3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,9 @@ static struct fanotify_event *fanotify_alloc_error_event(
609609
{
610610
struct fs_error_report *report =
611611
fsnotify_data_error_report(data, data_type);
612+
struct inode *inode;
612613
struct fanotify_error_event *fee;
614+
int fh_len;
613615

614616
if (WARN_ON_ONCE(!report))
615617
return NULL;
@@ -622,6 +624,15 @@ static struct fanotify_event *fanotify_alloc_error_event(
622624
fee->err_count = 1;
623625
fee->fsid = *fsid;
624626

627+
inode = report->inode;
628+
fh_len = fanotify_encode_fh_len(inode);
629+
630+
/* Bad fh_len. Fallback to using an invalid fh. Should never happen. */
631+
if (!fh_len && inode)
632+
inode = NULL;
633+
634+
fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0);
635+
625636
*hash ^= fanotify_hash_fsid(fsid);
626637

627638
return &fee->fae;

fs/notify/fanotify/fanotify.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ struct fanotify_error_event {
208208
u32 err_count; /* Suppressed errors count */
209209

210210
__kernel_fsid_t fsid; /* FSID this error refers to. */
211+
212+
FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ);
211213
};
212214

213215
static inline struct fanotify_error_event *
@@ -222,6 +224,8 @@ static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
222224
return &FANOTIFY_FE(event)->fsid;
223225
else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
224226
return &FANOTIFY_NE(event)->fsid;
227+
else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
228+
return &FANOTIFY_EE(event)->fsid;
225229
else
226230
return NULL;
227231
}
@@ -233,6 +237,8 @@ static inline struct fanotify_fh *fanotify_event_object_fh(
233237
return &FANOTIFY_FE(event)->object_fh;
234238
else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
235239
return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
240+
else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
241+
return &FANOTIFY_EE(event)->object_fh;
236242
else
237243
return NULL;
238244
}
@@ -266,6 +272,9 @@ static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
266272

267273
static inline bool fanotify_event_has_object_fh(struct fanotify_event *event)
268274
{
275+
/* For error events, even zeroed fh are reported. */
276+
if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR)
277+
return true;
269278
return fanotify_event_object_fh_len(event) > 0;
270279
}
271280

fs/notify/fanotify/fanotify_user.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,6 @@ static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
339339
pr_debug("%s: fh_len=%zu name_len=%zu, info_len=%zu, count=%zu\n",
340340
__func__, fh_len, name_len, info_len, count);
341341

342-
if (!fh_len)
343-
return 0;
344-
345342
if (WARN_ON_ONCE(len < sizeof(info) || len > count))
346343
return -EFAULT;
347344

@@ -376,6 +373,11 @@ static int copy_fid_info_to_user(__kernel_fsid_t *fsid, struct fanotify_fh *fh,
376373

377374
handle.handle_type = fh->type;
378375
handle.handle_bytes = fh_len;
376+
377+
/* Mangle handle_type for bad file_handle */
378+
if (!fh_len)
379+
handle.handle_type = FILEID_INVALID;
380+
379381
if (copy_to_user(buf, &handle, sizeof(handle)))
380382
return -EFAULT;
381383

0 commit comments

Comments
 (0)