• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of setuid/setgid/setuid32/setgid32 syscalls.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
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 <errno.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 void
printuid(unsigned UGID_TYPE id)36 printuid(unsigned UGID_TYPE id)
37 {
38 	if (id == (unsigned UGID_TYPE) -1U)
39 		printf("-1");
40 	else
41 		printf("%u", id);
42 }
43 
44 int
main(void)45 main(void)
46 {
47 	unsigned int ugid = GETUGID;
48 	CHECK_OVERFLOWUGID(ugid);
49 
50 	const long tests[] = {
51 		ugid,
52 		0xffff0000U | ugid,
53 		(unsigned long) 0xffffffff00000000ULL | ugid,
54 		0xffffU,
55 		-1U,
56 		-1L
57 	};
58 
59 	unsigned int i;
60 
61 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
62 		const unsigned int num = (unsigned UGID_TYPE) tests[i];
63 		long expected;
64 
65 		errno = 0;
66 
67 		if (num == ugid)
68 			expected = 0;
69 		else if ((UGID_TYPE) num == (UGID_TYPE) -1U)
70 			expected = -1;
71 		else
72 			continue;
73 
74 		const long rc = syscall(SYSCALL_NR, tests[i]);
75 		const char *errstr = sprintrc(rc);
76 
77 		if (rc != expected) {
78 			if (!i && ENOSYS == errno) {
79 				printf("%s(%u) = %s\n",
80 				       SYSCALL_NAME, ugid, errstr);
81 				break;
82 			}
83 			perror_msg_and_fail("%s(%#lx) = %ld != %ld",
84 					    SYSCALL_NAME, tests[i],
85 					    rc, expected);
86 		}
87 
88 		printf("%s(", SYSCALL_NAME);
89 		printuid(num);
90 		printf(") = %s\n", errstr);
91 	}
92 
93 	puts("+++ exited with 0 +++");
94 	return 0;
95 }
96