[PATCH] udf: reserve extent slots before insertion
From: Kyuwon Cho
Date: Thu Dec 03 2026 - 11:50:30 EST
udf_insert_aext() shifts allocation descriptors in place and only then
calls udf_add_aext() for the displaced tail. If the file entry is full
and allocating an indirect allocation extent fails, the old tail has
already been overwritten. inode_getblk() returns -ENOSPC with a
partially shifted extent list and also leaks the data block allocated for
the failed write. A later truncate observes the broken list:
WARNING: fs/udf/truncate.c:225 at udf_truncate_extents
RIP: udf_truncate_extents (fs/udf/truncate.c:225)
Call Trace:
udf_setsize (fs/udf/inode.c:1326)
udf_setattr (fs/udf/file.c:238)
notify_change (fs/attr.c:556)
do_truncate (fs/open.c:68)
do_ftruncate (fs/open.c:194)
ksys_ftruncate (fs/open.c:206)
Reserve all descriptor slots at the end of the list before changing
existing descriptors. Roll back any partial reservation on failure,
then rotate the new descriptors into place only after the reservation
succeeds. Also release a newly allocated data block when the reservation
fails with -ENOSPC.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
The issue was found by an LLM-guided test generator that we are
developing as a research prototype. The reproducer formats and mounts a
small UDF image, fills its allocation descriptors, and leaves one free
block before a write needs both a data block and an indirect allocation
extent. With the patch the write returns -ENOSPC without changing the
extent list or leaking the data block, and truncation completes cleanly.
Found and tested on mainline fe6ff8d5d675 (v7.4-rc5); the patch is
against mainline 19cc08c1a6a6.
fs/udf/inode.c | 128 ++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 101 insertions(+), 27 deletions(-)
diff --git a/fs/udf/inode.c b/fs/udf/inode.c
index 29900065673c..2c83c3be714d 100644
--- a/fs/udf/inode.c
+++ b/fs/udf/inode.c
@@ -53,8 +53,8 @@ struct udf_map_rq;
static umode_t udf_convert_permissions(struct fileEntry *);
static int udf_alloc_i_data(struct inode *inode, size_t size);
static int inode_getblk(struct inode *inode, struct udf_map_rq *map);
-static int udf_insert_aext(struct inode *, struct extent_position,
- struct kernel_lb_addr, uint32_t);
+static int udf_insert_aexts(struct inode *, struct extent_position,
+ struct kernel_long_ad *, unsigned int);
static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
struct kernel_long_ad *, int *);
static void udf_prealloc_extents(struct inode *, int, int,
@@ -743,6 +743,7 @@ static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
udf_pblk_t goal = 0, pgoal = 0;
int lastblock = 0;
bool isBeyondEOF = false;
+ bool newblock_allocated = false;
int ret = 0;
prev_epos.offset = udf_file_entry_alloc_offset(inode);
@@ -909,6 +910,7 @@ static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
goal, &ret);
if (!newblocknum)
goto out_free;
+ newblock_allocated = true;
if (isBeyondEOF)
iinfo->i_lenExtents += inode->i_sb->s_blocksize;
}
@@ -929,8 +931,20 @@ static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
* of extents is greater than the old number, and deleting extents if
* the new number of extents is less than the old number */
ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
- if (ret < 0)
+ if (ret < 0) {
+ if (ret == -ENOSPC && newblock_allocated) {
+ struct kernel_lb_addr newblock = {
+ .logicalBlockNum = newblocknum,
+ .partitionReferenceNum =
+ iinfo->i_location.partitionReferenceNum,
+ };
+
+ udf_free_blocks(inode->i_sb, inode, &newblock, 0, 1);
+ if (isBeyondEOF)
+ iinfo->i_lenExtents -= inode->i_sb->s_blocksize;
+ }
goto out_free;
+ }
map->pblk = udf_get_pblock(inode->i_sb, newblocknum,
iinfo->i_location.partitionReferenceNum, 0);
@@ -1210,17 +1224,11 @@ static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
for (i = 0; i < (startnum - endnum); i++)
udf_delete_aext(inode, *epos, NULL);
} else if (startnum < endnum) {
+ err = udf_insert_aexts(inode, *epos, laarr,
+ endnum - startnum);
+ if (err < 0)
+ return err;
for (i = 0; i < (endnum - startnum); i++) {
- err = udf_insert_aext(inode, *epos,
- laarr[i].extLocation,
- laarr[i].extLength);
- /*
- * If we fail here, we are likely corrupting the extent
- * list and leaking blocks. At least stop early to
- * limit the damage.
- */
- if (err < 0)
- return err;
err = udf_next_aext(inode, epos, &laarr[i].extLocation,
&laarr[i].extLength, &tmpetype, 1);
if (err < 0)
@@ -2367,27 +2375,93 @@ int udf_current_aext(struct inode *inode, struct extent_position *epos,
return 1;
}
-static int udf_insert_aext(struct inode *inode, struct extent_position epos,
- struct kernel_lb_addr neloc, uint32_t nelen)
+static int udf_insert_aexts(struct inode *inode, struct extent_position epos,
+ struct kernel_long_ad *extents,
+ unsigned int nr_insertions)
{
- struct kernel_lb_addr oeloc;
- uint32_t oelen;
+ struct extent_position end = epos;
+ struct extent_position reserved_pos = {};
+ struct kernel_long_ad carry[EXTENT_MERGE_SIZE];
+ struct kernel_long_ad old;
+ struct kernel_lb_addr tail_loc;
+ uint32_t tail_len;
+ unsigned int nr_extents = 0;
+ unsigned int reserved = 0;
+ unsigned int i;
+ int adsize;
int8_t etype;
int ret;
- if (epos.bh)
+ if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
+ adsize = sizeof(struct short_ad);
+ else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
+ adsize = sizeof(struct long_ad);
+ else
+ return -EIO;
+
+ if (epos.bh) {
get_bh(epos.bh);
+ get_bh(end.bh);
+ }
- while (1) {
- ret = udf_next_aext(inode, &epos, &oeloc, &oelen, &etype, 0);
- if (ret <= 0)
- break;
- udf_write_aext(inode, &epos, &neloc, nelen, 1);
- neloc = oeloc;
- nelen = (etype << 30) | oelen;
+ while ((ret = udf_next_aext(inode, &end, &tail_loc, &tail_len,
+ &etype, 1)) > 0) {
+ tail_len |= (uint32_t)etype << 30;
+ nr_extents++;
+ }
+ if (ret < 0)
+ goto out;
+
+ for (i = 0; i < nr_insertions; i++) {
+ struct kernel_long_ad *append = nr_extents ? NULL : &extents[i];
+
+ ret = udf_add_aext(inode, &end,
+ append ? &append->extLocation : &tail_loc,
+ append ? append->extLength : tail_len, 1);
+ if (ret < 0)
+ goto rollback;
+ if (!reserved) {
+ reserved_pos = end;
+ reserved_pos.offset -= adsize;
+ if (reserved_pos.bh)
+ get_bh(reserved_pos.bh);
+ }
+ reserved++;
}
- if (ret == 0)
- ret = udf_add_aext(inode, &epos, &neloc, nelen, 1);
+ if (!nr_extents)
+ goto out;
+
+ memcpy(carry, extents, nr_insertions * sizeof(*carry));
+ for (i = 0; i < nr_extents; i++) {
+ ret = udf_next_aext(inode, &epos, &old.extLocation,
+ &old.extLength, &etype, 0);
+ if (ret <= 0) {
+ if (!ret)
+ ret = -EFSCORRUPTED;
+ goto out;
+ }
+ old.extLength |= (uint32_t)etype << 30;
+ udf_write_aext(inode, &epos, &carry[0].extLocation,
+ carry[0].extLength, 1);
+ if (nr_insertions > 1)
+ memmove(&carry[0], &carry[1],
+ (nr_insertions - 1) * sizeof(*carry));
+ carry[nr_insertions - 1] = old;
+ }
+ for (i = 0; i < nr_insertions; i++)
+ udf_write_aext(inode, &epos, &carry[i].extLocation,
+ carry[i].extLength, 1);
+ ret = 0;
+ goto out;
+
+rollback:
+ while (reserved) {
+ reserved--;
+ udf_delete_aext(inode, reserved_pos, NULL);
+ }
+out:
+ brelse(reserved_pos.bh);
+ brelse(end.bh);
brelse(epos.bh);
return ret;
--
2.43.0