• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "defs.h"
30 #include "nsig.h"
31 #include "number_set.h"
32 #include "filter.h"
33 
34 struct number_set *read_set;
35 struct number_set *write_set;
36 struct number_set *signal_set;
37 
38 static struct number_set *abbrev_set;
39 static struct number_set *inject_set;
40 static struct number_set *raw_set;
41 static struct number_set *trace_set;
42 static struct number_set *verbose_set;
43 
44 static int
sigstr_to_uint(const char * s)45 sigstr_to_uint(const char *s)
46 {
47 	if (*s >= '0' && *s <= '9')
48 		return string_to_uint_upto(s, 255);
49 
50 	if (strncasecmp(s, "SIG", 3) == 0)
51 		s += 3;
52 
53 	for (int i = 0; i <= 255; ++i) {
54 		const char *name = signame(i);
55 
56 		if (strncasecmp(name, "SIG", 3) != 0)
57 			continue;
58 
59 		name += 3;
60 
61 		if (strcasecmp(name, s) != 0)
62 			continue;
63 
64 		return i;
65 	}
66 
67 	return -1;
68 }
69 
70 static int
find_errno_by_name(const char * name)71 find_errno_by_name(const char *name)
72 {
73 	for (unsigned int i = 1; i < nerrnos; ++i) {
74 		if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
75 			return i;
76 	}
77 
78 	return -1;
79 }
80 
81 static bool
parse_inject_token(const char * const token,struct inject_opts * const fopts,const bool fault_tokens_only)82 parse_inject_token(const char *const token, struct inject_opts *const fopts,
83 		   const bool fault_tokens_only)
84 {
85 	const char *val;
86 	kernel_long_t intval;
87 
88 	if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
89 		/*
90 		 *	== 1+1
91 		 * F	== F+0
92 		 * F+	== F+1
93 		 * F+S
94 		 */
95 		char *end;
96 		intval = string_to_uint_ex(val, &end, 0xffff, "+");
97 		if (intval < 1)
98 			return false;
99 
100 		fopts->first = intval;
101 
102 		if (*end) {
103 			val = end + 1;
104 			if (*val) {
105 				/* F+S */
106 				intval = string_to_uint_upto(val, 0xffff);
107 				if (intval < 1)
108 					return false;
109 				fopts->step = intval;
110 			} else {
111 				/* F+ == F+1 */
112 				fopts->step = 1;
113 			}
114 		} else {
115 			/* F == F+0 */
116 			fopts->step = 0;
117 		}
118 	} else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
119 		if (fopts->data.flags & INJECT_F_RETVAL)
120 			return false;
121 		intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
122 		if (intval < 0)
123 			intval = find_errno_by_name(val);
124 		if (intval < 1)
125 			return false;
126 		fopts->data.rval = -intval;
127 		fopts->data.flags |= INJECT_F_RETVAL;
128 	} else if (!fault_tokens_only
129 		   && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
130 		if (fopts->data.flags & INJECT_F_RETVAL)
131 			return false;
132 		intval = string_to_kulong(val);
133 		if (intval < 0)
134 			return false;
135 
136 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG && !HAVE_ARCH_DEDICATED_ERR_REG
137 		if ((int) intval != intval)
138 			error_msg("Injected return value %" PRI_kld " will be"
139 				  " clipped to %d in compat personality",
140 				  intval, (int) intval);
141 
142 		if ((int) intval < 0 && (int) intval >= -MAX_ERRNO_VALUE)
143 			error_msg("Inadvertent injection of error %d is"
144 				  " possible in compat personality for"
145 				  " retval=%" PRI_kld,
146 				  -(int) intval, intval);
147 #endif
148 
149 		fopts->data.rval = intval;
150 		fopts->data.flags |= INJECT_F_RETVAL;
151 	} else if (!fault_tokens_only
152 		   && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
153 		if (fopts->data.flags & INJECT_F_SIGNAL)
154 			return false;
155 		intval = sigstr_to_uint(val);
156 		if (intval < 1 || intval > NSIG_BYTES * 8)
157 			return false;
158 		fopts->data.signo = intval;
159 		fopts->data.flags |= INJECT_F_SIGNAL;
160 	} else {
161 		return false;
162 	}
163 
164 	return true;
165 }
166 
167 static const char *
parse_inject_expression(char * const str,struct inject_opts * const fopts,const bool fault_tokens_only)168 parse_inject_expression(char *const str,
169 			struct inject_opts *const fopts,
170 			const bool fault_tokens_only)
171 {
172 	if (str[0] == '\0' || str[0] == ':')
173 		return "";
174 
175 	char *saveptr = NULL;
176 	const char *name = strtok_r(str, ":", &saveptr);
177 
178 	char *token;
179 	while ((token = strtok_r(NULL, ":", &saveptr))) {
180 		if (!parse_inject_token(token, fopts, fault_tokens_only))
181 			return NULL;
182 	}
183 
184 	return name;
185 }
186 
187 static void
qualify_read(const char * const str)188 qualify_read(const char *const str)
189 {
190 	if (!read_set)
191 		read_set = alloc_number_set_array(1);
192 	qualify_tokens(str, read_set, string_to_uint, "descriptor");
193 }
194 
195 static void
qualify_write(const char * const str)196 qualify_write(const char *const str)
197 {
198 	if (!write_set)
199 		write_set = alloc_number_set_array(1);
200 	qualify_tokens(str, write_set, string_to_uint, "descriptor");
201 }
202 
203 static void
qualify_signals(const char * const str)204 qualify_signals(const char *const str)
205 {
206 	if (!signal_set)
207 		signal_set = alloc_number_set_array(1);
208 	qualify_tokens(str, signal_set, sigstr_to_uint, "signal");
209 }
210 
211 static void
qualify_trace(const char * const str)212 qualify_trace(const char *const str)
213 {
214 	if (!trace_set)
215 		trace_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
216 	qualify_syscall_tokens(str, trace_set);
217 }
218 
219 static void
qualify_abbrev(const char * const str)220 qualify_abbrev(const char *const str)
221 {
222 	if (!abbrev_set)
223 		abbrev_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
224 	qualify_syscall_tokens(str, abbrev_set);
225 }
226 
227 static void
qualify_verbose(const char * const str)228 qualify_verbose(const char *const str)
229 {
230 	if (!verbose_set)
231 		verbose_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
232 	qualify_syscall_tokens(str, verbose_set);
233 }
234 
235 static void
qualify_raw(const char * const str)236 qualify_raw(const char *const str)
237 {
238 	if (!raw_set)
239 		raw_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
240 	qualify_syscall_tokens(str, raw_set);
241 }
242 
243 static void
qualify_inject_common(const char * const str,const bool fault_tokens_only,const char * const description)244 qualify_inject_common(const char *const str,
245 		      const bool fault_tokens_only,
246 		      const char *const description)
247 {
248 	struct inject_opts opts = {
249 		.first = 1,
250 		.step = 1
251 	};
252 	char *copy = xstrdup(str);
253 	const char *name =
254 		parse_inject_expression(copy, &opts, fault_tokens_only);
255 	if (!name)
256 		error_msg_and_die("invalid %s '%s'", description, str);
257 
258 	struct number_set *tmp_set =
259 		alloc_number_set_array(SUPPORTED_PERSONALITIES);
260 	qualify_syscall_tokens(name, tmp_set);
261 
262 	free(copy);
263 
264 	/* If neither of retval, error, or signal is specified, then ... */
265 	if (!opts.data.flags) {
266 		if (fault_tokens_only) {
267 			/* in fault= syntax the default error code is ENOSYS. */
268 			opts.data.rval = -ENOSYS;
269 			opts.data.flags |= INJECT_F_RETVAL;
270 		} else {
271 			/* in inject= syntax this is not allowed. */
272 			error_msg_and_die("invalid %s '%s'", description, str);
273 		}
274 	}
275 
276 	/*
277 	 * Initialize inject_vec according to tmp_set.
278 	 * Merge tmp_set into inject_set.
279 	 */
280 	for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
281 		if (number_set_array_is_empty(tmp_set, p))
282 			continue;
283 
284 		if (!inject_set) {
285 			inject_set =
286 				alloc_number_set_array(SUPPORTED_PERSONALITIES);
287 		}
288 		if (!inject_vec[p]) {
289 			inject_vec[p] = xcalloc(nsyscall_vec[p],
290 						sizeof(*inject_vec[p]));
291 		}
292 
293 		for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
294 			if (is_number_in_set_array(i, tmp_set, p)) {
295 				add_number_to_set_array(i, inject_set, p);
296 				inject_vec[p][i] = opts;
297 			}
298 		}
299 	}
300 
301 	free_number_set_array(tmp_set, SUPPORTED_PERSONALITIES);
302 }
303 
304 static void
qualify_fault(const char * const str)305 qualify_fault(const char *const str)
306 {
307 	qualify_inject_common(str, true, "fault argument");
308 }
309 
310 static void
qualify_inject(const char * const str)311 qualify_inject(const char *const str)
312 {
313 	qualify_inject_common(str, false, "inject argument");
314 }
315 
316 static const struct qual_options {
317 	const char *name;
318 	void (*qualify)(const char *);
319 } qual_options[] = {
320 	{ "trace",	qualify_trace	},
321 	{ "t",		qualify_trace	},
322 	{ "abbrev",	qualify_abbrev	},
323 	{ "a",		qualify_abbrev	},
324 	{ "verbose",	qualify_verbose	},
325 	{ "v",		qualify_verbose	},
326 	{ "raw",	qualify_raw	},
327 	{ "x",		qualify_raw	},
328 	{ "signal",	qualify_signals	},
329 	{ "signals",	qualify_signals	},
330 	{ "s",		qualify_signals	},
331 	{ "read",	qualify_read	},
332 	{ "reads",	qualify_read	},
333 	{ "r",		qualify_read	},
334 	{ "write",	qualify_write	},
335 	{ "writes",	qualify_write	},
336 	{ "w",		qualify_write	},
337 	{ "fault",	qualify_fault	},
338 	{ "inject",	qualify_inject	},
339 };
340 
341 void
qualify(const char * str)342 qualify(const char *str)
343 {
344 	const struct qual_options *opt = qual_options;
345 
346 	for (unsigned int i = 0; i < ARRAY_SIZE(qual_options); ++i) {
347 		const char *name = qual_options[i].name;
348 		const size_t len = strlen(name);
349 		const char *val = str_strip_prefix_len(str, name, len);
350 
351 		if (val == str || *val != '=')
352 			continue;
353 		str = val + 1;
354 		opt = &qual_options[i];
355 		break;
356 	}
357 
358 	opt->qualify(str);
359 }
360 
361 unsigned int
qual_flags(const unsigned int scno)362 qual_flags(const unsigned int scno)
363 {
364 	return	(is_number_in_set_array(scno, trace_set, current_personality)
365 		   ? QUAL_TRACE : 0)
366 		| (is_number_in_set_array(scno, abbrev_set, current_personality)
367 		   ? QUAL_ABBREV : 0)
368 		| (is_number_in_set_array(scno, verbose_set, current_personality)
369 		   ? QUAL_VERBOSE : 0)
370 		| (is_number_in_set_array(scno, raw_set, current_personality)
371 		   ? QUAL_RAW : 0)
372 		| (is_number_in_set_array(scno, inject_set, current_personality)
373 		   ? QUAL_INJECT : 0);
374 }
375