1/*
2 * Check decoding of prctl operations which use arg2 as pointer to an integer
3 * value: PR_GET_CHILD_SUBREAPER, PR_GET_ENDIAN, PR_GET_FPEMU, and PR_GET_FPEXC.
4 *
5 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Copyright (c) 2016-2017 The strace developers.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "tests.h"
34#include <asm/unistd.h>
35
36#if defined __NR_prctl
37
38# include <stdint.h>
39# include <stdio.h>
40# include <unistd.h>
41# include <linux/prctl.h>
42
43static const char *errstr;
44
45static long
46prctl(kernel_ulong_t arg1, kernel_ulong_t arg2)
47{
48	static const kernel_ulong_t bogus_arg =
49		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;
50	long rc = syscall(__NR_prctl, arg1, arg2, bogus_arg);
51	errstr = sprintrc(rc);
52	return rc;
53}
54
55int
56main(void)
57{
58	static const kernel_ulong_t bogus_addr1 =
59		(kernel_ulong_t) 0x1e55c0de00000000ULL;
60	static const kernel_ulong_t bogus_addr2 =
61		(kernel_ulong_t) 0xfffffffffffffffdULL;
62	static const kernel_ulong_t bogus_op_bits =
63		(kernel_ulong_t) 0xbadc0ded00000000ULL;
64	static const struct {
65		kernel_ulong_t val;
66		const char *str;
67	} options[] = {
68		{ 37, "PR_GET_CHILD_SUBREAPER" },
69		{ 19, "PR_GET_ENDIAN" },
70		{  9, "PR_GET_FPEMU" },
71		{ 11, "PR_GET_FPEXC" },
72	};
73
74	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, ptr);
75	long rc;
76	unsigned int i;
77
78	for (i = 0; i < ARRAY_SIZE(options); ++i) {
79		prctl(options[i].val | bogus_op_bits, 0);
80		printf("prctl(%s, NULL) = %s\n", options[i].str, errstr);
81
82		if (bogus_addr1) {
83			prctl(options[i].val | bogus_op_bits, bogus_addr1);
84			printf("prctl(%s, %#llx) = %s\n", options[i].str,
85			       (unsigned long long) bogus_addr1, errstr);
86		}
87
88		prctl(options[i].val | bogus_op_bits, bogus_addr2);
89		printf("prctl(%s, %#llx) = %s\n", options[i].str,
90		       (unsigned long long) bogus_addr2, errstr);
91
92		prctl(options[i].val | bogus_op_bits, (uintptr_t) (ptr + 1));
93		printf("prctl(%s, %p) = %s\n", options[i].str,
94		       ptr + 1, errstr);
95
96		rc = prctl(options[i].val | bogus_op_bits, (uintptr_t) ptr);
97		if (!rc) {
98			printf("prctl(%s, [%u]) = %s\n",
99			       options[i].str, *ptr, errstr);
100		} else {
101			printf("prctl(%s, %p) = %s\n",
102			       options[i].str, ptr, errstr);
103		}
104
105		if (F8ILL_KULONG_SUPPORTED) {
106			kernel_ulong_t bogus_addr3 = f8ill_ptr_to_kulong(ptr);
107			prctl(options[i].val | bogus_op_bits, bogus_addr3);
108			printf("prctl(%s, %#llx) = %s\n", options[i].str,
109			       (unsigned long long) bogus_addr3, errstr);
110		}
111	}
112
113	puts("+++ exited with 0 +++");
114	return 0;
115}
116
117#else
118
119SKIP_MAIN_UNDEFINED("__NR_prctl")
120
121#endif
122