]> granicus.if.org Git - strace/blob - tests/fanotify_mark.c
tests: some additional checks for fanotify_mark
[strace] / tests / fanotify_mark.c
1 /*
2  * Check decoding of fanotify_mark syscall.
3  *
4  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "tests.h"
32
33 #include <asm/unistd.h>
34
35 #if defined HAVE_SYS_FANOTIFY_H && defined HAVE_FANOTIFY_MARK && \
36         defined __NR_fanotify_mark
37
38 # include <limits.h>
39 # include <stdio.h>
40 # include <unistd.h>
41
42 # include <sys/fanotify.h>
43
44 # include "kernel_types.h"
45
46 /* Performs fanotify_mark call via the syscall interface. */
47 static void
48 do_call(kernel_ulong_t fd, kernel_ulong_t flags, const char *flags_str,
49         uint64_t mask, const char *mask_str, kernel_ulong_t dirfd,
50         const char *dirfd_str, kernel_ulong_t path, const char *path_str)
51 {
52         long rc;
53
54         rc = syscall(__NR_fanotify_mark, fd, flags,
55 # if (LONG_MAX > INT_MAX) \
56   || (defined __x86_64__ && defined __ILP32__) \
57   || defined LINUX_MIPSN32
58                 mask,
59 # else
60 /* arch/parisc/kernel/sys_parisc32.c, commit ab8a261b */
61 #  ifdef HPPA
62                 LL_VAL_TO_PAIR((mask << 32) | (mask >> 32)),
63 #  else
64                 LL_VAL_TO_PAIR(mask),
65 #  endif
66 # endif
67                 dirfd, path);
68
69         printf("fanotify_mark(%d, %s, %s, %s, %s) = %s\n",
70                (int) fd, flags_str, mask_str, dirfd_str, path_str,
71                sprintrc(rc));
72 }
73
74 struct strval {
75         kernel_ulong_t val;
76         const char *str;
77 };
78
79 struct strval64 {
80         uint64_t val;
81         const char *str;
82 };
83
84 #define STR16 "0123456789abcdef"
85 #define STR64 STR16 STR16 STR16 STR16
86
87 int
88 main(void)
89 {
90         enum {
91                 PATH1_SIZE = 64,
92         };
93
94         static const kernel_ulong_t fds[] = {
95                 (kernel_ulong_t) 0xdeadfeed12345678ULL,
96                 (kernel_ulong_t) 0xffffffff00000000ULL,
97                 (kernel_ulong_t) 0xdeb0d1edffffffffULL,
98         };
99         static const struct strval flags[] = {
100                 { (kernel_ulong_t) 0xffffffff00000000ULL, "0" },
101                 { (kernel_ulong_t) 0xdec0deddefaced00ULL,
102                         "0xefaced00 /* FAN_MARK_??? */" },
103                 { (kernel_ulong_t) 0xda7a105700000040ULL,
104                         "FAN_MARK_IGNORED_SURV_MODIFY" },
105                 { (kernel_ulong_t) 0xbadc0deddeadfeedULL,
106                         "FAN_MARK_ADD|FAN_MARK_DONT_FOLLOW|FAN_MARK_ONLYDIR|"
107                         "FAN_MARK_IGNORED_MASK|FAN_MARK_IGNORED_SURV_MODIFY|"
108                         "FAN_MARK_FLUSH|0xdeadfe00" },
109         };
110         static const struct strval64 masks[] = {
111                 { ARG_ULL_STR(0) },
112                 { 0xdeadfeedfacebeefULL,
113                         "FAN_ACCESS|FAN_MODIFY|FAN_CLOSE_WRITE|FAN_OPEN|"
114                         "FAN_ACCESS_PERM|FAN_ONDIR|FAN_EVENT_ON_CHILD|"
115                         "0xdeadfeedb2ccbec4" },
116                 { ARG_ULL_STR(0xffffffffb7fcbfc4) " /* FAN_??? */" },
117         };
118         static const struct strval dirfds[] = {
119                 { (kernel_ulong_t) 0xfacefeed00000001ULL, "1" },
120                 { (kernel_ulong_t) 0xdec0ded0ffffffffULL, "FAN_NOFD" },
121                 { (kernel_ulong_t) 0xbadfacedffffff9cULL, "AT_FDCWD" },
122                 { (kernel_ulong_t) 0xdefaced1beeff00dULL, "-1091571699" },
123         };
124         static const char str64[] = STR64;
125
126         static char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
127         static char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
128
129         char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
130         char *bogus_path2 = tail_memdup(str64, sizeof(str64));
131
132         struct strval paths[] = {
133                 { (kernel_ulong_t) 0, "NULL" },
134                 { (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE),
135                         bogus_path1_after_addr },
136                 { (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr },
137                 { (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" },
138         };
139
140         unsigned int i;
141         unsigned int j;
142         unsigned int k;
143         unsigned int l;
144         unsigned int m;
145         int rc;
146
147
148         snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1);
149         snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p",
150                 bogus_path1 + PATH1_SIZE);
151
152         rc = fanotify_mark(-1, FAN_MARK_ADD, FAN_MODIFY | FAN_ONDIR,
153                                -100, ".");
154         printf("fanotify_mark(-1, FAN_MARK_ADD, FAN_MODIFY|FAN_ONDIR"
155                ", AT_FDCWD, \".\") = %s\n", sprintrc(rc));
156
157         for (i = 0; i < ARRAY_SIZE(fds); i++) {
158                 for (j = 0; j < ARRAY_SIZE(flags); j++) {
159                         for (k = 0; k < ARRAY_SIZE(masks); k++) {
160                                 for (l = 0; l < ARRAY_SIZE(dirfds); l++) {
161                                         for (m = 0; m < ARRAY_SIZE(paths); m++)
162                                                 do_call(fds[i],
163                                                         flags[j].val,
164                                                         flags[j].str,
165                                                         masks[k].val,
166                                                         masks[k].str,
167                                                         dirfds[l].val,
168                                                         dirfds[l].str,
169                                                         paths[m].val,
170                                                         paths[m].str);
171                                 }
172                         }
173                 }
174         }
175
176         puts("+++ exited with 0 +++");
177         return 0;
178 }
179
180 #else
181
182 SKIP_MAIN_UNDEFINED("HAVE_SYS_FANOTIFY_H && HAVE_FANOTIFY_MARK && "
183                     "__NR_fanotify_mark")
184
185 #endif