Dmitry V. Levin | 5e9944d | 2016-04-19 01:59:52 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Check decoding of getgroups/getgroups32 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_getgroups32 |
| 31 | |
| 32 | # define SYSCALL_NR __NR_getgroups32 |
| 33 | # define SYSCALL_NAME "getgroups32" |
| 34 | # define GID_TYPE unsigned int |
| 35 | |
| 36 | #else |
| 37 | |
| 38 | # include "tests.h" |
| 39 | # include <sys/syscall.h> |
| 40 | |
| 41 | # ifdef __NR_getgroups |
| 42 | |
| 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 |
| 47 | # else |
| 48 | # define GID_TYPE unsigned int |
| 49 | # endif |
| 50 | |
| 51 | # endif |
| 52 | |
| 53 | #endif |
| 54 | |
| 55 | #ifdef GID_TYPE |
| 56 | |
| 57 | # include <errno.h> |
| 58 | # include <stdio.h> |
| 59 | # include <unistd.h> |
| 60 | |
| 61 | static const char * |
| 62 | errno2str(void) |
| 63 | { |
| 64 | switch (errno) { |
| 65 | #define CASE(x) case x: return #x |
| 66 | CASE(EINVAL); |
| 67 | CASE(EFAULT); |
| 68 | default: perror_msg_and_fail(SYSCALL_NAME); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #define MAX_STRLEN 32 |
| 73 | static long ngroups; |
| 74 | |
| 75 | static void |
| 76 | get_groups(const long size, GID_TYPE *const g) |
| 77 | { |
| 78 | long i = syscall(SYSCALL_NR, size, g); |
| 79 | if (i != ngroups) |
| 80 | perror_msg_and_fail("%s(%#lx, %p)", SYSCALL_NAME, size, g); |
| 81 | |
| 82 | printf("%s(%u, [", SYSCALL_NAME, (unsigned) size); |
| 83 | for (i = 0; i < ngroups; ++i) { |
| 84 | if (i) |
| 85 | printf(", "); |
| 86 | if (i >= MAX_STRLEN) { |
| 87 | printf("..."); |
| 88 | break; |
| 89 | } |
| 90 | printf("%u", (unsigned int) g[i]); |
| 91 | } |
| 92 | printf("]) = %ld\n", ngroups); |
| 93 | } |
| 94 | |
| 95 | int |
| 96 | main(void) |
| 97 | { |
| 98 | long rc; |
| 99 | |
| 100 | /* check how the first argument is decoded */ |
| 101 | ngroups = syscall(SYSCALL_NR, 0, 0); |
| 102 | printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, ngroups); |
| 103 | if (ngroups < 0) |
| 104 | perror_msg_and_fail(SYSCALL_NAME); |
| 105 | |
| 106 | rc = syscall(SYSCALL_NR, (long) 0xffffffff00000000ULL, 0); |
| 107 | printf("%s(0, NULL) = %ld\n", SYSCALL_NAME, rc); |
| 108 | |
| 109 | rc = syscall(SYSCALL_NR, -1U, 0); |
| 110 | printf("%s(%u, NULL) = %ld %s (%m)\n", |
| 111 | SYSCALL_NAME, -1U, rc, errno2str()); |
| 112 | |
| 113 | rc = syscall(SYSCALL_NR, -1L, 0); |
| 114 | printf("%s(%u, NULL) = %ld %s (%m)\n", |
| 115 | SYSCALL_NAME, -1U, rc, errno2str()); |
| 116 | |
| 117 | const unsigned int ngroups_max = sysconf(_SC_NGROUPS_MAX); |
| 118 | |
| 119 | rc = syscall(SYSCALL_NR, ngroups_max, 0); |
| 120 | if (rc < 0) |
| 121 | printf("%s(%u, NULL) = %ld %s (%m)\n", |
| 122 | SYSCALL_NAME, ngroups_max, rc, errno2str()); |
| 123 | else |
| 124 | printf("%s(%u, NULL) = %ld\n", |
| 125 | SYSCALL_NAME, ngroups_max, rc); |
| 126 | |
| 127 | rc = syscall(SYSCALL_NR, (long) 0xffffffff00000000ULL | ngroups_max, 0); |
| 128 | if (rc < 0) |
| 129 | printf("%s(%u, NULL) = %ld %s (%m)\n", |
| 130 | SYSCALL_NAME, ngroups_max, rc, errno2str()); |
| 131 | else |
| 132 | printf("%s(%u, NULL) = %ld\n", |
| 133 | SYSCALL_NAME, ngroups_max, rc); |
| 134 | |
| 135 | /* check how the second argument is decoded */ |
| 136 | GID_TYPE *const g1 = |
| 137 | tail_alloc(ngroups ? sizeof(*g1) * ngroups : 1); |
| 138 | GID_TYPE *const g2 = tail_alloc(sizeof(*g2) * (ngroups + 1)); |
| 139 | void *efault = g2 + ngroups + 1; |
| 140 | (void) tail_alloc(1); |
| 141 | |
| 142 | get_groups(ngroups, g1); |
| 143 | get_groups(ngroups + 1, g1); |
| 144 | get_groups(ngroups + 1, g2); |
| 145 | |
| 146 | if (ngroups) { |
| 147 | rc = syscall(SYSCALL_NR, ngroups, efault); |
| 148 | printf("%s(%u, %p) = %ld %s (%m)\n", |
| 149 | SYSCALL_NAME, (unsigned) ngroups, efault, |
| 150 | rc, errno2str()); |
| 151 | } |
| 152 | |
| 153 | puts("+++ exited with 0 +++"); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | #else |
| 158 | |
| 159 | SKIP_MAIN_UNDEFINED("__NR_getgroups") |
| 160 | |
| 161 | #endif |