2 * Check decoding of getgroups/getgroups32 syscalls.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #ifdef __NR_getgroups32
32 # define SYSCALL_NR __NR_getgroups32
33 # define SYSCALL_NAME "getgroups32"
34 # define GID_TYPE unsigned int
39 # include <asm/unistd.h>
41 # ifdef __NR_getgroups
43 # define SYSCALL_NR __NR_getgroups
44 # define SYSCALL_NAME "getgroups"
45 # if defined __NR_getgroups32 && __NR_getgroups != __NR_getgroups32
46 # define GID_TYPE unsigned short
48 # define GID_TYPE unsigned int
64 get_groups(const long size, GID_TYPE *const g)
66 long i = syscall(SYSCALL_NR, size, g);
68 perror_msg_and_fail("%s(%#lx, %p)", SYSCALL_NAME, size, g);
70 printf("%s(%d, [", SYSCALL_NAME, (int) size);
71 for (i = 0; i < ngroups; ++i) {
74 if (i >= MAX_STRLEN) {
78 printf("%u", (unsigned int) g[i]);
80 printf("]) = %ld\n", ngroups);
88 /* check how the first argument is decoded */
89 ngroups = syscall(SYSCALL_NR, 0, 0);
90 printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, ngroups);
92 perror_msg_and_fail(SYSCALL_NAME);
94 rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK, 0);
95 printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, rc);
97 rc = syscall(SYSCALL_NR, -1U, 0);
98 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
100 rc = syscall(SYSCALL_NR, -1L, 0);
101 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
103 const unsigned int ngroups_max = sysconf(_SC_NGROUPS_MAX);
105 rc = syscall(SYSCALL_NR, ngroups_max, 0);
106 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc));
108 rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK | ngroups_max, 0);
109 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc));
111 /* check how the second argument is decoded */
113 tail_alloc(ngroups ? sizeof(*g1) * ngroups : 1);
114 GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * (ngroups + 1));
115 void *efault = g2 + ngroups + 1;
117 get_groups(ngroups, g1);
118 get_groups(ngroups + 1, g1);
119 get_groups(ngroups + 1, g2);
122 rc = syscall(SYSCALL_NR, ngroups, efault);
123 printf("%s(%d, %p) = %s\n",
124 SYSCALL_NAME, (unsigned) ngroups, efault, sprintrc(rc));
127 puts("+++ exited with 0 +++");
133 SKIP_MAIN_UNDEFINED("__NR_getgroups")