udf: Implement searching for directory entry using new iteration code

Implement searching for directory entry - udf_fiiter_find_entry() -
using new directory iteration code.

Reported-by: syzbot+69c9fdccc6dd08961d34@syzkaller.appspotmail.com
Signed-off-by: Jan Kara <jack@suse.cz>
This commit is contained in:
Jan Kara 2022-10-05 18:28:33 +02:00
parent a27b2923de
commit 1c80afa04d

View File

@ -140,6 +140,73 @@ int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
return 0;
}
/**
* udf_fiiter_find_entry - find entry in given directory.
*
* @dir: directory inode to search in
* @child: qstr of the name
* @iter: iter to use for searching
*
* This function searches in the directory @dir for a file name @child. When
* found, @iter points to the position in the directory with given entry.
*
* Returns 0 on success, < 0 on error (including -ENOENT).
*/
static int udf_fiiter_find_entry(struct inode *dir, const struct qstr *child,
struct udf_fileident_iter *iter)
{
int flen;
unsigned char *fname = NULL;
struct super_block *sb = dir->i_sb;
int isdotdot = child->len == 2 &&
child->name[0] == '.' && child->name[1] == '.';
int ret;
fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
if (!fname)
return -ENOMEM;
for (ret = udf_fiiter_init(iter, dir, 0);
!ret && iter->pos < dir->i_size;
ret = udf_fiiter_advance(iter)) {
if (iter->fi.fileCharacteristics & FID_FILE_CHAR_DELETED) {
if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
continue;
}
if (iter->fi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) {
if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
continue;
}
if ((iter->fi.fileCharacteristics & FID_FILE_CHAR_PARENT) &&
isdotdot)
goto out_ok;
if (!iter->fi.lengthFileIdent)
continue;
flen = udf_get_filename(sb, iter->name,
iter->fi.lengthFileIdent, fname, UDF_NAME_LEN);
if (flen < 0) {
ret = flen;
goto out_err;
}
if (udf_match(flen, fname, child->len, child->name))
goto out_ok;
}
if (!ret)
ret = -ENOENT;
out_err:
udf_fiiter_release(iter);
out_ok:
kfree(fname);
return ret;
}
/**
* udf_find_entry - find entry in given directory.
*