Skip to content

Commit fe4a6fc

Browse files
Al Virosashalevin
authored andcommitted
ioctx_alloc(): fix vma (and file) leak on failure
[ Upstream commit deeb852 ] If we fail past the aio_setup_ring(), we need to destroy the mapping. We don't need to care about anybody having found ctx, or added requests to it, since the last failure exit is exactly the failure to make ctx visible to lookups. Reproducer (based on one by Joe Mario <jmario@redhat.com>): void count(char *p) { char s[80]; printf("%s: ", p); fflush(stdout); sprintf(s, "/bin/cat /proc/%d/maps|/bin/fgrep -c '/[aio] (deleted)'", getpid()); system(s); } int main() { io_context_t *ctx; int created, limit, i, destroyed; FILE *f; count("before"); if ((f = fopen("/proc/sys/fs/aio-max-nr", "r")) == NULL) perror("opening aio-max-nr"); else if (fscanf(f, "%d", &limit) != 1) fprintf(stderr, "can't parse aio-max-nr\n"); else if ((ctx = calloc(limit, sizeof(io_context_t))) == NULL) perror("allocating aio_context_t array"); else { for (i = 0, created = 0; i < limit; i++) { if (io_setup(1000, ctx + created) == 0) created++; } for (i = 0, destroyed = 0; i < created; i++) if (io_destroy(ctx[i]) == 0) destroyed++; printf("created %d, failed %d, destroyed %d\n", created, limit - created, destroyed); count("after"); } } Found-by: Joe Mario <jmario@redhat.com> Cc: stable@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent 6704168 commit fe4a6fc

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

‎fs/aio.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,9 @@ static struct kioctx *ioctx_alloc(unsigned nr_events)
715715
err_cleanup:
716716
aio_nr_sub(ctx->max_reqs);
717717
err_ctx:
718+
atomic_set(&ctx->dead, 1);
719+
if (ctx->mmap_size)
720+
vm_munmap(ctx->mmap_base, ctx->mmap_size);
718721
aio_free_ring(ctx);
719722
err:
720723
mutex_unlock(&ctx->ring_lock);

0 commit comments

Comments
 (0)