blob: 61894f1d9b9a825db3dc41a879a0a417f33b6b5e [file] [log] [blame]
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +00001/*
2 * Check decoding of setgroups/setgroups32 syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifdef __NR_setgroups32
31
32# define SYSCALL_NR __NR_setgroups32
33# define SYSCALL_NAME "setgroups32"
34# define GID_TYPE unsigned int
35
36#else
37
38# include "tests.h"
Dmitry V. Levin6a2f43c2016-08-09 14:38:29 +000039# include <asm/unistd.h>
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000040
41# ifdef __NR_setgroups
42
43# define SYSCALL_NR __NR_setgroups
44# define SYSCALL_NAME "setgroups"
45# if defined __NR_setgroups32 && __NR_setgroups != __NR_setgroups32
46# define GID_TYPE unsigned short
47# else
48# define GID_TYPE unsigned int
49# endif
50
51# endif
52
53#endif
54
55#ifdef GID_TYPE
56
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000057# include <stdio.h>
58# include <unistd.h>
59
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000060int
61main(void)
62{
63 /* check how the first argument is decoded */
64 if (syscall(SYSCALL_NR, 0, 0))
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000065 printf("%s(0, NULL) = -1 %s (%m)\n", SYSCALL_NAME, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000066 else
67 printf("%s(0, NULL) = 0\n", SYSCALL_NAME);
68
69 if (syscall(SYSCALL_NR, (long) 0xffffffff00000000ULL, 0))
70 printf("%s(0, NULL) = -1 %s (%m)\n",
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000071 SYSCALL_NAME, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000072 else
73 printf("%s(0, NULL) = 0\n", SYSCALL_NAME);
74
75 syscall(SYSCALL_NR, 1, 0);
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000076 printf("%s(1, NULL) = -1 %s (%m)\n", SYSCALL_NAME, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000077
78 syscall(SYSCALL_NR, (long) 0xffffffff00000001ULL, 0);
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000079 printf("%s(1, NULL) = -1 %s (%m)\n", SYSCALL_NAME, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000080
81 syscall(SYSCALL_NR, -1U, 0);
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000082 printf("%s(%u, NULL) = -1 %s (%m)\n", SYSCALL_NAME, -1U, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000083
84 syscall(SYSCALL_NR, -1L, 0);
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000085 printf("%s(%u, NULL) = -1 %s (%m)\n", SYSCALL_NAME, -1U, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000086
87 /* check how the second argument is decoded */
88 const GID_TYPE *const g1 = tail_alloc(sizeof(*g1));
89 GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * 2);
90 GID_TYPE *const g3 = tail_alloc(sizeof(*g3) * 3);
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000091
92 if (syscall(SYSCALL_NR, 0, g1 + 1))
93 printf("%s(0, []) = -1 %s (%m)\n",
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +000094 SYSCALL_NAME, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +000095 else
96 printf("%s(0, []) = 0\n", SYSCALL_NAME);
97
98 if (syscall(SYSCALL_NR, 1, g1))
99 printf("%s(1, [%u]) = -1 %s (%m)\n",
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000100 SYSCALL_NAME, (unsigned) *g1, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000101 else
102 printf("%s(1, [%u]) = 0\n",
103 SYSCALL_NAME, (unsigned) *g1);
104
105 syscall(SYSCALL_NR, 1, g1 + 1);
106 printf("%s(1, %p) = -1 %s (%m)\n",
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000107 SYSCALL_NAME, g1 + 1, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000108
109 syscall(SYSCALL_NR, 1, -1L);
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000110 printf("%s(1, %#lx) = -1 %s (%m)\n", SYSCALL_NAME, -1L, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000111
112 syscall(SYSCALL_NR, 2, g1);
113 printf("%s(2, [%u, %p]) = -1 %s (%m)\n",
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000114 SYSCALL_NAME, (unsigned) *g1, g1 + 1, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000115
116 g2[0] = -2;
117 g2[1] = -3;
118 if (syscall(SYSCALL_NR, 2, g2))
119 printf("%s(2, [%u, %u]) = -1 %s (%m)\n", SYSCALL_NAME,
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000120 (unsigned) g2[0], (unsigned) g2[1], errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000121 else
122 printf("%s(2, [%u, %u]) = 0\n", SYSCALL_NAME,
123 (unsigned) g2[0], (unsigned) g2[1]);
124
125 syscall(SYSCALL_NR, 3, g2);
126 printf("%s(3, [%u, %u, %p]) = -1 %s (%m)\n", SYSCALL_NAME,
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000127 (unsigned) g2[0], (unsigned) g2[1], g2 + 2, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000128
129 g3[0] = 0;
130 g3[1] = 1;
131 if (syscall(SYSCALL_NR, 3, g3))
132 printf("%s(3, [%u, %u, ...]) = -1 %s (%m)\n", SYSCALL_NAME,
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000133 (unsigned) g3[0], (unsigned) g3[1], errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000134 else
135 printf("%s(3, [%u, %u]) = 0\n", SYSCALL_NAME,
136 (unsigned) g3[0], (unsigned) g3[1]);
137
138 syscall(SYSCALL_NR, 4, g3);
139 printf("%s(4, [%u, %u, ...]) = -1 %s (%m)\n", SYSCALL_NAME,
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000140 (unsigned) g3[0], (unsigned) g3[1], errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000141
142 long rc = sysconf(_SC_NGROUPS_MAX);
143 const unsigned ngroups_max = rc;
144
145 if ((unsigned long) rc == ngroups_max && (int) ngroups_max > 0) {
146 syscall(SYSCALL_NR, ngroups_max, g3);
147 printf("%s(%u, [%u, %u, ...]) = -1 %s (%m)\n", SYSCALL_NAME,
148 ngroups_max, (unsigned) g3[0], (unsigned) g3[1],
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000149 errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000150
151 const unsigned long size =
152 (unsigned long) 0xffffffff00000000ULL | ngroups_max;
153 syscall(SYSCALL_NR, size, g3);
154 printf("%s(%u, [%u, %u, ...]) = -1 %s (%m)\n", SYSCALL_NAME,
155 ngroups_max, (unsigned) g3[0], (unsigned) g3[1],
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000156 errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000157
158 syscall(SYSCALL_NR, ngroups_max + 1, g3);
159 printf("%s(%u, %p) = -1 %s (%m)\n", SYSCALL_NAME,
Dmitry V. Levinb255f6e2016-04-21 21:21:37 +0000160 ngroups_max + 1, g3, errno2name());
Dmitry V. Levin5e9944d2016-04-19 01:59:52 +0000161 }
162
163 puts("+++ exited with 0 +++");
164 return 0;
165}
166
167#else
168
169SKIP_MAIN_UNDEFINED("__NR_setgroups")
170
171#endif