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