f133fc0dfb800606a7c5275622b3ba9e190f9082
[xfstests-dev.git] / src / aio-dio-regress / aio-free-ring-with-bogus-nr-pages.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  */
20  
21 /*
22  *  Code taken from an example posted to Red Hat bugzilla #220971
23  *
24  *  Original Author: Kostantin Khorenko from OpenVZ/Virtuozzo
25  *  Munged by Jeff Moyer.
26  *
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."
31  *
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:
36  *
37  *      info->mmap_base = do_mmap(NULL, 0, info->mmap_size, 
38  *                                PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
39  *                                0);
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);    <---------
45  *              return -EAGAIN;
46  *      }
47  *
48  *  we end up calling aio_free_ring with a bogus array and cause an oops.
49  *
50  *  This is a destructive test.
51  */
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <sys/mman.h>
55 #include <errno.h>
56 #include <libgen.h>
57 #include <libaio.h>
58
59 int main(int __attribute__((unused)) argc, char **argv)
60 {
61         long res;
62         io_context_t ctx;
63         void* map;
64
65         while (1) {
66                 map = mmap(NULL, 100, PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE,
67                            0, 0);
68                 if (map == MAP_FAILED)
69                         break;
70                 map = mmap(NULL, 100, PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
71                            0, 0);
72                 if (map == MAP_FAILED)
73                         break;
74         }
75
76         memset(&ctx, 0, sizeof(ctx));
77         res = io_setup(10000, &ctx);
78         if (res != -ENOMEM) {
79                 printf("%s: Error: io_setup returned %ld, expected -ENOMEM\n",
80                        basename(argv[0]), res);
81                 return 1;
82         } else
83                 printf("%s: Success!\n", basename(argv[0]));
84         return 0;
85 }