2 * aio-free-ring-with-bogus-nr-pages - test aio_setup_ring with bad nr_pages
3 * Copyright (C) 2006 Kostantin Khorenko
4 * Copyright (C) 2006 Jeff Moyer
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Code taken from an example posted to Red Hat bugzilla #220971
24 * Original Author: Kostantin Khorenko from OpenVZ/Virtuozzo
25 * Munged by Jeff Moyer.
27 * Description: "aio_setup_ring() function initializes info->nr_pages
28 * variable incorrectly, then this variable can be used in error path
29 * to free the allocated resources. By this way an unprivileged user
30 * can crash the node."
32 * At the beginning of aio_setup_ring, info->nr_pages is initialized
33 * to the requested number of pages. However, it is supposed to
34 * indicate how many pages are mapped in info->ring_pages. Thus, if
35 * the call to do_mmap fails:
37 * info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
38 * PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
40 * if (IS_ERR((void *)info->mmap_base)) {
41 * up_write(&ctx->mm->mmap_sem);
42 * printk("mmap err: %ld\n", -info->mmap_base);
43 * info->mmap_size = 0;
44 * aio_free_ring(ctx); <---------
48 * we end up calling aio_free_ring with a bogus array and cause an oops.
50 * This is a destructive test.
59 int main(int __attribute__((unused)) argc, char **argv)
66 map = mmap(NULL, 100, PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE,
68 if (map == MAP_FAILED)
70 map = mmap(NULL, 100, PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
72 if (map == MAP_FAILED)
76 memset(&ctx, 0, sizeof(ctx));
77 res = io_setup(10000, &ctx);
79 printf("%s: Error: io_setup returned %ld, expected -ENOMEM\n",
80 basename(argv[0]), res);
83 printf("%s: Success!\n", basename(argv[0]));