• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 # include <sys/fanotify.h>
42 
43 /* Performs fanotify_mark call via the syscall interface. */
44 static void
do_call(kernel_ulong_t fd,kernel_ulong_t flags,const char * flags_str,uint64_t mask,const char * mask_str,kernel_ulong_t dirfd,const char * dirfd_str,kernel_ulong_t path,const char * path_str)45 do_call(kernel_ulong_t fd, kernel_ulong_t flags, const char *flags_str,
46 	uint64_t mask, const char *mask_str, kernel_ulong_t dirfd,
47 	const char *dirfd_str, kernel_ulong_t path, const char *path_str)
48 {
49 	long rc;
50 
51 	rc = syscall(__NR_fanotify_mark, fd, flags,
52 # if (LONG_MAX > INT_MAX) \
53   || (defined __x86_64__ && defined __ILP32__) \
54   || defined LINUX_MIPSN32
55 		mask,
56 # else
57 /* arch/parisc/kernel/sys_parisc32.c, commit ab8a261b */
58 #  ifdef HPPA
59 		LL_VAL_TO_PAIR((mask << 32) | (mask >> 32)),
60 #  else
61 		LL_VAL_TO_PAIR(mask),
62 #  endif
63 # endif
64 		dirfd, path);
65 
66 	printf("fanotify_mark(%d, %s, %s, %s, %s) = %s\n",
67 	       (int) fd, flags_str, mask_str, dirfd_str, path_str,
68 	       sprintrc(rc));
69 }
70 
71 struct strval {
72 	kernel_ulong_t val;
73 	const char *str;
74 };
75 
76 struct strval64 {
77 	uint64_t val;
78 	const char *str;
79 };
80 
81 #define STR16 "0123456789abcdef"
82 #define STR64 STR16 STR16 STR16 STR16
83 
84 int
main(void)85 main(void)
86 {
87 	enum {
88 		PATH1_SIZE = 64,
89 	};
90 
91 	static const kernel_ulong_t fds[] = {
92 		(kernel_ulong_t) 0xdeadfeed12345678ULL,
93 		(kernel_ulong_t) 0xffffffff00000000ULL,
94 		(kernel_ulong_t) 0xdeb0d1edffffffffULL,
95 	};
96 	static const struct strval flags[] = {
97 		{ (kernel_ulong_t) 0xffffffff00000000ULL, "0" },
98 		{ (kernel_ulong_t) 0xdec0deddefaced00ULL,
99 			"0xefaced00 /* FAN_MARK_??? */" },
100 		{ (kernel_ulong_t) 0xda7a105700000040ULL,
101 			"FAN_MARK_IGNORED_SURV_MODIFY" },
102 		{ (kernel_ulong_t) 0xbadc0deddeadfeedULL,
103 			"FAN_MARK_ADD|FAN_MARK_DONT_FOLLOW|FAN_MARK_ONLYDIR|"
104 			"FAN_MARK_IGNORED_MASK|FAN_MARK_IGNORED_SURV_MODIFY|"
105 			"FAN_MARK_FLUSH|0xdeadfe00" },
106 	};
107 	static const struct strval64 masks[] = {
108 		{ ARG_ULL_STR(0) },
109 		{ 0xdeadfeedfacebeefULL,
110 			"FAN_ACCESS|FAN_MODIFY|FAN_CLOSE_WRITE|FAN_OPEN|"
111 			"FAN_ACCESS_PERM|FAN_ONDIR|FAN_EVENT_ON_CHILD|"
112 			"0xdeadfeedb2ccbec4" },
113 		{ ARG_ULL_STR(0xffffffffb7fcbfc4) " /* FAN_??? */" },
114 	};
115 	static const struct strval dirfds[] = {
116 		{ (kernel_ulong_t) 0xfacefeed00000001ULL, "1" },
117 		{ (kernel_ulong_t) 0xdec0ded0ffffffffULL, "FAN_NOFD" },
118 		{ (kernel_ulong_t) 0xbadfacedffffff9cULL, "AT_FDCWD" },
119 		{ (kernel_ulong_t) 0xdefaced1beeff00dULL, "-1091571699" },
120 	};
121 	static const char str64[] = STR64;
122 
123 	static char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
124 	static char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
125 
126 	char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
127 	char *bogus_path2 = tail_memdup(str64, sizeof(str64));
128 
129 	struct strval paths[] = {
130 		{ (kernel_ulong_t) 0, "NULL" },
131 		{ (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE),
132 			bogus_path1_after_addr },
133 		{ (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr },
134 		{ (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" },
135 	};
136 
137 	unsigned int i;
138 	unsigned int j;
139 	unsigned int k;
140 	unsigned int l;
141 	unsigned int m;
142 	int rc;
143 
144 
145 	snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1);
146 	snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p",
147 		bogus_path1 + PATH1_SIZE);
148 
149 	rc = fanotify_mark(-1, FAN_MARK_ADD, FAN_MODIFY | FAN_ONDIR,
150 			       -100, ".");
151 	printf("fanotify_mark(-1, FAN_MARK_ADD, FAN_MODIFY|FAN_ONDIR"
152 	       ", AT_FDCWD, \".\") = %s\n", sprintrc(rc));
153 
154 	for (i = 0; i < ARRAY_SIZE(fds); i++) {
155 		for (j = 0; j < ARRAY_SIZE(flags); j++) {
156 			for (k = 0; k < ARRAY_SIZE(masks); k++) {
157 				for (l = 0; l < ARRAY_SIZE(dirfds); l++) {
158 					for (m = 0; m < ARRAY_SIZE(paths); m++)
159 						do_call(fds[i],
160 							flags[j].val,
161 							flags[j].str,
162 							masks[k].val,
163 							masks[k].str,
164 							dirfds[l].val,
165 							dirfds[l].str,
166 							paths[m].val,
167 							paths[m].str);
168 				}
169 			}
170 		}
171 	}
172 
173 	puts("+++ exited with 0 +++");
174 	return 0;
175 }
176 
177 #else
178 
179 SKIP_MAIN_UNDEFINED("HAVE_SYS_FANOTIFY_H && HAVE_FANOTIFY_MARK && "
180 		    "__NR_fanotify_mark")
181 
182 #endif
183