2 * Check decoding of getgroups/getgroups32 syscalls.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2019 The strace developers.
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #ifdef __NR_getgroups32
13 # define SYSCALL_NR __NR_getgroups32
14 # define SYSCALL_NAME "getgroups32"
15 # define GID_TYPE unsigned int
22 # ifdef __NR_getgroups
24 # define SYSCALL_NR __NR_getgroups
25 # define SYSCALL_NAME "getgroups"
26 # if defined __NR_getgroups32 && __NR_getgroups != __NR_getgroups32
27 # define GID_TYPE unsigned short
29 # define GID_TYPE unsigned int
41 # define MAX_STRLEN 32
45 get_groups(const long size, GID_TYPE *const g)
47 long i = syscall(SYSCALL_NR, size, g);
49 perror_msg_and_fail("%s(%#lx, %p)", SYSCALL_NAME, size, g);
51 printf("%s(%d, [", SYSCALL_NAME, (int) size);
52 for (i = 0; i < ngroups; ++i) {
55 if (i >= MAX_STRLEN) {
59 printf("%u", (unsigned int) g[i]);
61 printf("]) = %ld\n", ngroups);
69 /* check how the first argument is decoded */
70 ngroups = syscall(SYSCALL_NR, 0, 0);
71 printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, ngroups);
73 perror_msg_and_fail(SYSCALL_NAME);
75 rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK, 0);
76 printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, rc);
78 rc = syscall(SYSCALL_NR, -1U, 0);
79 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
81 rc = syscall(SYSCALL_NR, -1L, 0);
82 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, -1, sprintrc(rc));
84 const unsigned int ngroups_max = sysconf(_SC_NGROUPS_MAX);
86 rc = syscall(SYSCALL_NR, ngroups_max, 0);
87 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc));
89 rc = syscall(SYSCALL_NR, F8ILL_KULONG_MASK | ngroups_max, 0);
90 printf("%s(%d, NULL) = %s\n", SYSCALL_NAME, ngroups_max, sprintrc(rc));
92 /* check how the second argument is decoded */
94 tail_alloc(ngroups ? sizeof(*g1) * ngroups : 1);
95 GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * (ngroups + 1));
96 void *efault = g2 + ngroups + 1;
98 get_groups(ngroups, g1);
99 get_groups(ngroups + 1, g1);
100 get_groups(ngroups + 1, g2);
103 rc = syscall(SYSCALL_NR, ngroups, efault);
104 printf("%s(%d, %p) = %s\n",
105 SYSCALL_NAME, (unsigned) ngroups, efault, sprintrc(rc));
108 puts("+++ exited with 0 +++");
114 SKIP_MAIN_UNDEFINED("__NR_getgroups")