From: Jan Kara <jack@xxxxxxx> [ Upstream commit 1c80afa04db39c98aebea9aabfafa37a208cdfee ] Implement searching for directory entry - udf_fiiter_find_entry() - using new directory iteration code. Reported-by: syzbot+69c9fdccc6dd08961d34@xxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Jan Kara <jack@xxxxxxx> Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/udf/namei.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) --- a/fs/udf/namei.c +++ b/fs/udf/namei.c @@ -141,6 +141,73 @@ int udf_write_fi(struct inode *inode, st } /** + * 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. * * @dir: directory inode to search in Patches currently in stable-queue which might be from cascardo@xxxxxxxxxx are queue-5.15/udf-handle-error-when-expanding-directory.patch queue-5.15/udf-convert-empty_dir-to-new-directory-iteration-code.patch queue-5.15/udf-fix-bogus-checksum-computation-in-udf_rename.patch queue-5.15/udf-convert-udf_expand_dir_adinicb-to-new-directory-iteration.patch queue-5.15/udf-convert-udf_unlink-to-new-directory-iteration-code.patch queue-5.15/udf-convert-udf_add_nondir-to-new-directory-iteration.patch queue-5.15/udf-convert-udf_link-to-new-directory-iteration-code.patch queue-5.15/udf-remove-old-directory-iteration-code.patch queue-5.15/udf-don-t-return-bh-from-udf_expand_dir_adinicb.patch queue-5.15/udf-new-directory-iteration-code.patch queue-5.15/udf-convert-udf_rmdir-to-new-directory-iteration-code.patch queue-5.15/udf-move-udf_expand_dir_adinicb-to-its-callsite.patch queue-5.15/udf-convert-udf_readdir-to-new-directory-iteration.patch queue-5.15/udf-convert-udf_mkdir-to-new-directory-iteration-code.patch queue-5.15/udf-provide-function-to-mark-entry-as-deleted-using-new-directory-iteration-code.patch queue-5.15/udf-convert-udf_lookup-to-use-new-directory-iteration-code.patch queue-5.15/udf-implement-adding-of-dir-entries-using-new-iteration-code.patch queue-5.15/udf-convert-udf_rename-to-new-directory-iteration-code.patch queue-5.15/udf-convert-udf_get_parent-to-new-directory-iteration-code.patch queue-5.15/udf-implement-searching-for-directory-entry-using-new-iteration-code.patch