Skip to content

Commit 5875f87

Browse files
jankaragregkh
authored andcommitted
udf: Fix crash after seekdir
commit a48fc69 upstream. 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. Similarly when the directory is modified between two readdir calls, the directory position need not be valid anymore. 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 nothing changed since the last readdir call. Reported-by: Nathan Wilson <nate@chickenbrittle.com> CC: stable@vger.kernel.org Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6b43cf1 commit 5875f87

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎fs/udf/dir.c‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/mm.h>
3232
#include <linux/slab.h>
3333
#include <linux/bio.h>
34+
#include <linux/iversion.h>
3435

3536
#include "udf_i.h"
3637
#include "udf_sb.h"
@@ -44,7 +45,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
4445
struct fileIdentDesc *fi = NULL;
4546
struct fileIdentDesc cfi;
4647
udf_pblk_t block, iblock;
47-
loff_t nf_pos;
48+
loff_t nf_pos, emit_pos = 0;
4849
int flen;
4950
unsigned char *fname = NULL, *copy_name = NULL;
5051
unsigned char *nameptr;
@@ -58,6 +59,7 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
5859
int i, num, ret = 0;
5960
struct extent_position epos = { NULL, 0, {0, 0} };
6061
struct super_block *sb = dir->i_sb;
62+
bool pos_valid = false;
6163

6264
if (ctx->pos == 0) {
6365
if (!dir_emit_dot(file, ctx))
@@ -68,6 +70,21 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
6870
if (nf_pos >= size)
6971
goto out;
7072

73+
/*
74+
* Something changed since last readdir (either lseek was called or dir
75+
* changed)? We need to verify the position correctly points at the
76+
* beginning of some dir entry so that the directory parsing code does
77+
* not get confused. Since UDF does not have any reliable way of
78+
* identifying beginning of dir entry (names are under user control),
79+
* we need to scan the directory from the beginning.
80+
*/
81+
if (!inode_eq_iversion(dir, file->f_version)) {
82+
emit_pos = nf_pos;
83+
nf_pos = 0;
84+
} else {
85+
pos_valid = true;
86+
}
87+
7188
fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
7289
if (!fname) {
7390
ret = -ENOMEM;
@@ -123,13 +140,21 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
123140

124141
while (nf_pos < size) {
125142
struct kernel_lb_addr tloc;
143+
loff_t cur_pos = nf_pos;
126144

127-
ctx->pos = (nf_pos >> 2) + 1;
145+
/* Update file position only if we got past the current one */
146+
if (nf_pos >= emit_pos) {
147+
ctx->pos = (nf_pos >> 2) + 1;
148+
pos_valid = true;
149+
}
128150

129151
fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
130152
&elen, &offset);
131153
if (!fi)
132154
goto out;
155+
/* Still not at offset where user asked us to read from? */
156+
if (cur_pos < emit_pos)
157+
continue;
133158

134159
liu = le16_to_cpu(cfi.lengthOfImpUse);
135160
lfi = cfi.lengthFileIdent;
@@ -187,8 +212,11 @@ static int udf_readdir(struct file *file, struct dir_context *ctx)
187212
} /* end while */
188213

189214
ctx->pos = (nf_pos >> 2) + 1;
215+
pos_valid = true;
190216

191217
out:
218+
if (pos_valid)
219+
file->f_version = inode_query_iversion(dir);
192220
if (fibh.sbh != fibh.ebh)
193221
brelse(fibh.ebh);
194222
brelse(fibh.sbh);

‎fs/udf/namei.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/sched.h>
3131
#include <linux/crc-itu-t.h>
3232
#include <linux/exportfs.h>
33+
#include <linux/iversion.h>
3334

3435
static inline int udf_match(int len1, const unsigned char *name1, int len2,
3536
const unsigned char *name2)
@@ -135,6 +136,8 @@ int udf_write_fi(struct inode *inode, struct fileIdentDesc *cfi,
135136
mark_buffer_dirty_inode(fibh->ebh, inode);
136137
mark_buffer_dirty_inode(fibh->sbh, inode);
137138
}
139+
inode_inc_iversion(inode);
140+
138141
return 0;
139142
}
140143

‎fs/udf/super.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <linux/crc-itu-t.h>
5858
#include <linux/log2.h>
5959
#include <asm/byteorder.h>
60+
#include <linux/iversion.h>
6061

6162
#include "udf_sb.h"
6263
#include "udf_i.h"
@@ -149,6 +150,7 @@ static struct inode *udf_alloc_inode(struct super_block *sb)
149150
init_rwsem(&ei->i_data_sem);
150151
ei->cached_extent.lstart = -1;
151152
spin_lock_init(&ei->i_extent_cache_lock);
153+
inode_set_iversion(&ei->vfs_inode, 1);
152154

153155
return &ei->vfs_inode;
154156
}

0 commit comments

Comments
 (0)