Skip to content

Commit c45beeb

Browse files
amir73ilbrauner
authored andcommitted
ovl: support encoding fid from inode with no alias
Dmitry Safonov reported that a WARN_ON() assertion can be trigered by userspace when calling inotify_show_fdinfo() for an overlayfs watched inode, whose dentry aliases were discarded with drop_caches. The WARN_ON() assertion in inotify_show_fdinfo() was removed, because it is possible for encoding file handle to fail for other reason, but the impact of failing to encode an overlayfs file handle goes beyond this assertion. As shown in the LTP test case mentioned in the link below, failure to encode an overlayfs file handle from a non-aliased inode also leads to failure to report an fid with FAN_DELETE_SELF fanotify events. As Dmitry notes in his analyzis of the problem, ovl_encode_fh() fails if it cannot find an alias for the inode, but this failure can be fixed. ovl_encode_fh() seldom uses the alias and in the case of non-decodable file handles, as is often the case with fanotify fid info, ovl_encode_fh() never needs to use the alias to encode a file handle. Defer finding an alias until it is actually needed so ovl_encode_fh() will not fail in the common case of FAN_DELETE_SELF fanotify events. Fixes: 16aac5a ("ovl: support encoding non-decodable file handles") Reported-by: Dmitry Safonov <dima@arista.com> Closes: https://lore.kernel.org/linux-fsdevel/CAOQ4uxiie81voLZZi2zXS1BziXZCM24nXqPAxbu8kxXCUWdwOg@mail.gmail.com/ Signed-off-by: Amir Goldstein <amir73il@gmail.com> Link: https://lore.kernel.org/r/20250105162404.357058-3-amir73il@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 07aeefa commit c45beeb

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

fs/overlayfs/export.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,37 @@ static int ovl_connect_layer(struct dentry *dentry)
176176
*
177177
* Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
178178
*/
179-
static int ovl_check_encode_origin(struct dentry *dentry)
179+
static int ovl_check_encode_origin(struct inode *inode)
180180
{
181-
struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
181+
struct ovl_fs *ofs = OVL_FS(inode->i_sb);
182182
bool decodable = ofs->config.nfs_export;
183+
struct dentry *dentry;
184+
int err;
183185

184186
/* No upper layer? */
185187
if (!ovl_upper_mnt(ofs))
186188
return 1;
187189

188190
/* Lower file handle for non-upper non-decodable */
189-
if (!ovl_dentry_upper(dentry) && !decodable)
191+
if (!ovl_inode_upper(inode) && !decodable)
190192
return 1;
191193

192194
/* Upper file handle for pure upper */
193-
if (!ovl_dentry_lower(dentry))
195+
if (!ovl_inode_lower(inode))
194196
return 0;
195197

196198
/*
197199
* Root is never indexed, so if there's an upper layer, encode upper for
198200
* root.
199201
*/
200-
if (dentry == dentry->d_sb->s_root)
202+
if (inode == d_inode(inode->i_sb->s_root))
201203
return 0;
202204

203205
/*
204206
* Upper decodable file handle for non-indexed upper.
205207
*/
206-
if (ovl_dentry_upper(dentry) && decodable &&
207-
!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
208+
if (ovl_inode_upper(inode) && decodable &&
209+
!ovl_test_flag(OVL_INDEX, inode))
208210
return 0;
209211

210212
/*
@@ -213,17 +215,25 @@ static int ovl_check_encode_origin(struct dentry *dentry)
213215
* ovl_connect_layer() will try to make origin's layer "connected" by
214216
* copying up a "connectable" ancestor.
215217
*/
216-
if (d_is_dir(dentry) && decodable)
217-
return ovl_connect_layer(dentry);
218+
if (!decodable || !S_ISDIR(inode->i_mode))
219+
return 1;
220+
221+
dentry = d_find_any_alias(inode);
222+
if (!dentry)
223+
return -ENOENT;
224+
225+
err = ovl_connect_layer(dentry);
226+
dput(dentry);
227+
if (err < 0)
228+
return err;
218229

219230
/* Lower file handle for indexed and non-upper dir/non-dir */
220231
return 1;
221232
}
222233

223-
static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry,
234+
static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct inode *inode,
224235
u32 *fid, int buflen)
225236
{
226-
struct inode *inode = d_inode(dentry);
227237
struct ovl_fh *fh = NULL;
228238
int err, enc_lower;
229239
int len;
@@ -232,7 +242,7 @@ static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry,
232242
* Check if we should encode a lower or upper file handle and maybe
233243
* copy up an ancestor to make lower file handle connectable.
234244
*/
235-
err = enc_lower = ovl_check_encode_origin(dentry);
245+
err = enc_lower = ovl_check_encode_origin(inode);
236246
if (enc_lower < 0)
237247
goto fail;
238248

@@ -252,28 +262,22 @@ static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct dentry *dentry,
252262
return err;
253263

254264
fail:
255-
pr_warn_ratelimited("failed to encode file handle (%pd2, err=%i)\n",
256-
dentry, err);
265+
pr_warn_ratelimited("failed to encode file handle (ino=%lu, err=%i)\n",
266+
inode->i_ino, err);
257267
goto out;
258268
}
259269

260270
static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
261271
struct inode *parent)
262272
{
263273
struct ovl_fs *ofs = OVL_FS(inode->i_sb);
264-
struct dentry *dentry;
265274
int bytes, buflen = *max_len << 2;
266275

267276
/* TODO: encode connectable file handles */
268277
if (parent)
269278
return FILEID_INVALID;
270279

271-
dentry = d_find_any_alias(inode);
272-
if (!dentry)
273-
return FILEID_INVALID;
274-
275-
bytes = ovl_dentry_to_fid(ofs, dentry, fid, buflen);
276-
dput(dentry);
280+
bytes = ovl_dentry_to_fid(ofs, inode, fid, buflen);
277281
if (bytes <= 0)
278282
return FILEID_INVALID;
279283

0 commit comments

Comments
 (0)