aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Kara <jack@suse.cz>2021-11-04 15:22:35 +0100
committerJan Kara <jack@suse.cz>2021-11-08 13:00:49 +0100
commit39a464de961f256197934d36aa5dda546cba8ed0 (patch)
treeca35851d1ad3aa4f04a3da6c01f143a3b33dd5bd
parent6b75d88fa81b122cce37ebf17428a849ccd3d0f1 (diff)
downloadlinux-next-39a464de961f256197934d36aa5dda546cba8ed0.tar.gz
udf: Fix crash after seekdir
Notice: this object is not reachable from any branch.
udf_readdir() didn't validate the directory position it should start reading from. Thus when user uses lseek(2) on directory file descriptor it can trick udf_readdir() into reading from a position in the middle of directory entry which then upsets directory parsing code resulting in errors or even possible kernel crashes. Add code to validate current offset in the directory. This is actually rather expensive for UDF as we need to read from the beginning of the directory and parse all directory entries. This is because in UDF a directory is just a stream of data containing directory entries and since file names are fully under user's control we cannot depend on detecting magic numbers and checksums in the header of directory entry as a malicious attacker could fake them. We skip this step if we detect that current position is the same as where we previously stopped to avoid this expensive test for the common case of not using lseek on directories at all. Reported-by: Nathan Wilson <nate@chickenbrittle.com> CC: stable@vger.kernel.org Signed-off-by: Jan Kara <jack@suse.cz>
Notice: this object is not reachable from any branch.
-rw-r--r--fs/udf/dir.c25
1 files changed, 23 insertions, 2 deletions
diff --git a/fs/udf/dir.c b/fs/udf/dir.c
index 70abdfad2df17..8ae501d27eff1 100644
--- a/fs/udf/dir.c
+++ b/fs/udf/dir.c
@@ -43,7 +43,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
struct fileIdentDesc *fi = NULL;
struct fileIdentDesc cfi;
udf_pblk_t block, iblock;
- loff_t nf_pos;
+ loff_t nf_pos, emit_pos;
int flen;
unsigned char *fname = NULL, *copy_name = NULL;
unsigned char *nameptr;
@@ -67,6 +67,19 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
if (nf_pos >= size)
goto out;
+ /*
+ * Did our position change since last readdir (likely lseek was
+ * called)? We need to verify the position correctly points at the
+ * beginning of some dir entry so that the directory parsing code does
+ * not get confused. Since UDF does not have any reliable way of
+ * identifying beginning of dir entry (names are under user control),
+ * we need to scan the directory from the beginning.
+ */
+ if (ctx->pos != (loff_t)file->private_data) {
+ emit_pos = nf_pos;
+ nf_pos = 0;
+ }
+
fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
if (!fname) {
ret = -ENOMEM;
@@ -122,13 +135,19 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
while (nf_pos < size) {
struct kernel_lb_addr tloc;
+ loff_t cur_pos = nf_pos;
- ctx->pos = (nf_pos >> 2) + 1;
+ /* Update file position only if we got past the current one */
+ if (nf_pos >= emit_pos)
+ ctx->pos = (nf_pos >> 2) + 1;
fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
&elen, &offset);
if (!fi)
goto out;
+ /* Still not at offset where user asked us to read from? */
+ if (cur_pos < emit_pos)
+ continue;
liu = le16_to_cpu(cfi.lengthOfImpUse);
lfi = cfi.lengthFileIdent;
@@ -188,6 +207,8 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
ctx->pos = (nf_pos >> 2) + 1;
out:
+ /* Store position where we've ended */
+ file->private_data = (void *)ctx->pos;
if (fibh.sbh != fibh.ebh)
brelse(fibh.ebh);
brelse(fibh.sbh);