Skip to content

Commit 39be79c

Browse files
jtlaytonChristoph Hellwig
authored andcommitted
vfs: iov_iter: have iov_iter_advance decrement nr_segs appropriately
Currently, when you call iov_iter_advance, then the pointer to the iovec array can be incremented, but it does not decrement the nr_segs value in the iov_iter struct. The result is a iov_iter struct with a nr_segs value that goes beyond the end of the array. While I'm not aware of anything that's specifically broken by this, it seems odd and a bit dangerous not to decrement that value. If someone were to trust the nr_segs value to be correct, then they could end up walking off the end of the array. Changing this might also provide some micro-optimization when dealing with the last iovec in an array. Many of the other routines that deal with iov_iter have optimized codepaths when nr_segs == 1. Cc: Nick Piggin <npiggin@suse.de> Signed-off-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent c3b92c8 commit 39be79c

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎mm/filemap.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,7 @@ void iov_iter_advance(struct iov_iter *i, size_t bytes)
21152115
} else {
21162116
const struct iovec *iov = i->iov;
21172117
size_t base = i->iov_offset;
2118+
unsigned long nr_segs = i->nr_segs;
21182119

21192120
/*
21202121
* The !iov->iov_len check ensures we skip over unlikely
@@ -2130,11 +2131,13 @@ void iov_iter_advance(struct iov_iter *i, size_t bytes)
21302131
base += copy;
21312132
if (iov->iov_len == base) {
21322133
iov++;
2134+
nr_segs--;
21332135
base = 0;
21342136
}
21352137
}
21362138
i->iov = iov;
21372139
i->iov_offset = base;
2140+
i->nr_segs = nr_segs;
21382141
}
21392142
}
21402143
EXPORT_SYMBOL(iov_iter_advance);

0 commit comments

Comments
 (0)