Skip to content

Commit 09ebb17

Browse files
committed
udf: Fix data corruption on file type conversion
UDF has two types of files - files with data stored in inode (ICB in UDF terminology) and files with data stored in external data blocks. We convert file from in-inode format to external format in udf_file_aio_write() when we find out data won't fit into inode any longer. However the following race between two O_APPEND writes can happen: CPU1 CPU2 udf_file_aio_write() udf_file_aio_write() down_write(&iinfo->i_data_sem); checks that i_size + count1 fits within inode => no need to convert up_write(&iinfo->i_data_sem); down_write(&iinfo->i_data_sem); checks that i_size + count2 fits within inode => no need to convert up_write(&iinfo->i_data_sem); generic_file_aio_write() - extends file by count1 bytes generic_file_aio_write() - extends file by count2 bytes Clearly if count1 + count2 doesn't fit into the inode, we overwrite kernel buffers beyond inode, possibly corrupting the filesystem as well. Fix the problem by acquiring i_mutex before checking whether write fits into the inode and using __generic_file_aio_write() afterwards which puts check and write into one critical section. Reported-by: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 45a22f4 commit 09ebb17

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎fs/udf/file.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static ssize_t udf_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
144144
size_t count = iocb->ki_nbytes;
145145
struct udf_inode_info *iinfo = UDF_I(inode);
146146

147+
mutex_lock(&inode->i_mutex);
147148
down_write(&iinfo->i_data_sem);
148149
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
149150
if (file->f_flags & O_APPEND)
@@ -156,6 +157,7 @@ static ssize_t udf_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
156157
pos + count)) {
157158
err = udf_expand_file_adinicb(inode);
158159
if (err) {
160+
mutex_unlock(&inode->i_mutex);
159161
udf_debug("udf_expand_adinicb: err=%d\n", err);
160162
return err;
161163
}
@@ -169,9 +171,17 @@ static ssize_t udf_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
169171
} else
170172
up_write(&iinfo->i_data_sem);
171173

172-
retval = generic_file_aio_write(iocb, iov, nr_segs, ppos);
173-
if (retval > 0)
174+
retval = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
175+
mutex_unlock(&inode->i_mutex);
176+
177+
if (retval > 0) {
178+
ssize_t err;
179+
174180
mark_inode_dirty(inode);
181+
err = generic_write_sync(file, iocb->ki_pos - retval, retval);
182+
if (err < 0)
183+
retval = err;
184+
}
175185

176186
return retval;
177187
}

‎fs/udf/inode.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ int udf_expand_file_adinicb(struct inode *inode)
265265
.nr_to_write = 1,
266266
};
267267

268+
WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
268269
if (!iinfo->i_lenAlloc) {
269270
if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
270271
iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;

0 commit comments

Comments
 (0)