1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
* Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
* Copyright (c) 2015-2020 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#ifndef SHM_HUGE_SHIFT
# define SHM_HUGE_SHIFT 26
#endif
#ifndef SHM_HUGE_MASK
# define SHM_HUGE_MASK 0x3f
#endif
#ifndef SHM_STAT_ANY
# define SHM_STAT_ANY 15
#endif
#ifndef SHM_NORESERVE
# define SHM_NORESERVE 010000
#endif
#undef TEST_SHMCTL_BOGUS_ADDR
#undef TEST_SHMCTL_BOGUS_CMD
/*
* Starting with commit glibc-2.32~80, on every 32-bit architecture
* where 32-bit time_t support is enabled, glibc tries to retrieve
* the data provided in the third argument of shmctl call.
*/
#if GLIBC_PREREQ_GE(2, 32) && defined __TIMESIZE && __TIMESIZE != 64
# define TEST_SHMCTL_BOGUS_ADDR 0
#endif
/*
* Starting with commit glibc-2.32.9000-207-g9ebaabeaac1a96b0d91f,
* glibc skips shmctl syscall invocations and returns EINVAL
* for invalid shmctl commands.
*/
#if GLIBC_PREREQ_GE(2, 32)
# define TEST_SHMCTL_BOGUS_CMD 0
#endif
#ifndef TEST_SHMCTL_BOGUS_ADDR
# define TEST_SHMCTL_BOGUS_ADDR 1
#endif
#ifndef TEST_SHMCTL_BOGUS_CMD
# define TEST_SHMCTL_BOGUS_CMD 1
#endif
#include "xlat.h"
#include "xlat/shm_resource_flags.h"
#if XLAT_RAW
# define str_ipc_flags "0x2ce1e00"
# define str_shm_huge "21<<26"
# define str_ipc_private "0"
# define str_ipc_rmid "0"
# define str_ipc_set "0x1"
# define str_ipc_stat "0x2"
# define str_ipc_info "0x3"
# define str_shm_stat "0xd"
# define str_shm_info "0xe"
# define str_shm_stat_any "0xf"
# define str_ipc_64 "0x100"
# define str_bogus_cmd "0xdefaced2"
#elif XLAT_VERBOSE
# define str_ipc_flags \
"0x2ce1e00 /\\* IPC_CREAT\\|IPC_EXCL\\|SHM_HUGETLB\\|SHM_NORESERVE" \
"\\|0x2ce0000 \\*/"
# define str_shm_huge "21<<26 /\\* SHM_HUGE_SHIFT \\*/"
# define str_ipc_private "0 /\\* IPC_PRIVATE \\*/"
# define str_ipc_rmid "0 /\\* IPC_RMID \\*/"
# define str_ipc_set "0x1 /\\* IPC_SET \\*/"
# define str_ipc_stat "0x2 /\\* IPC_STAT \\*/"
# define str_ipc_info "0x3 /\\* IPC_INFO \\*/"
# define str_shm_stat "0xd /\\* SHM_STAT \\*/"
# define str_shm_info "0xe /\\* SHM_INFO \\*/"
# define str_shm_stat_any "0xf /\\* SHM_STAT_ANY \\*/"
# define str_ipc_64 "0x100 /\\* IPC_64 \\*/"
# define str_bogus_cmd "0xdefaced2 /\\* SHM_\\?\\?\\? \\*/"
#else
# define str_ipc_flags \
"IPC_CREAT\\|IPC_EXCL\\|SHM_HUGETLB\\|SHM_NORESERVE\\|0x2ce0000"
# define str_shm_huge "21<<SHM_HUGE_SHIFT"
# define str_ipc_private "IPC_PRIVATE"
# define str_ipc_rmid "IPC_RMID"
# define str_ipc_set "IPC_SET"
# define str_ipc_stat "IPC_STAT"
# define str_ipc_info "IPC_INFO"
# define str_shm_stat "SHM_STAT"
# define str_shm_info "SHM_INFO"
# define str_shm_stat_any "SHM_STAT_ANY"
# define str_ipc_64 "IPC_64"
# define str_bogus_cmd "0xdefaced2 /\\* SHM_\\?\\?\\? \\*/"
#endif
static int id = -1;
static void
cleanup(void)
{
shmctl(id, IPC_RMID, NULL);
printf("shmctl\\(%d, (%s\\|)?%s, NULL\\) = 0\n",
id, str_ipc_64, str_ipc_rmid);
id = -1;
}
static void
print_shmid_ds(const char *const str_ipc_cmd,
const struct shmid_ds *const ds,
const int rc)
{
if (rc < 0) {
printf("shmctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
id, str_ipc_64, str_ipc_cmd, ds, sprintrc_grep(rc));
return;
}
printf("shmctl\\(%d, (%s\\|)?%s, \\{shm_perm=\\{uid=%u, gid=%u"
", mode=%#o, key=%u, cuid=%u, cgid=%u\\}, shm_segsz=%u"
", shm_cpid=%d, shm_lpid=%d, shm_nattch=%u, shm_atime=%u"
", shm_dtime=%u, shm_ctime=%u\\}\\) = %d\n",
id,
str_ipc_64,
str_ipc_cmd,
(unsigned) ds->shm_perm.uid,
(unsigned) ds->shm_perm.gid,
(unsigned) ds->shm_perm.mode,
(unsigned) ds->shm_perm.__key,
(unsigned) ds->shm_perm.cuid,
(unsigned) ds->shm_perm.cgid,
(unsigned) ds->shm_segsz,
(int) ds->shm_cpid,
(int) ds->shm_lpid,
(unsigned) ds->shm_nattch,
(unsigned) ds->shm_atime,
(unsigned) ds->shm_dtime,
(unsigned) ds->shm_ctime,
rc);
}
static void
print_ipc_info(const char *const str_ipc_cmd,
const struct shminfo *const info,
const int rc)
{
if (rc < 0) {
printf("shmctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
id, str_ipc_64, str_ipc_cmd, info, sprintrc_grep(rc));
return;
}
printf("shmctl\\(%d, (%s\\|)?%s, \\{shmmax=%llu, shmmin=%llu"
", shmmni=%llu, shmseg=%llu, shmall=%llu\\}\\) = %d\n",
id,
str_ipc_64,
str_ipc_cmd,
(unsigned long long) info->shmmax,
(unsigned long long) info->shmmin,
(unsigned long long) info->shmmni,
(unsigned long long) info->shmseg,
(unsigned long long) info->shmall,
rc);
}
static void
print_shm_info(const char *const str_ipc_cmd,
const struct shm_info *const info,
const int rc)
{
if (rc < 0) {
printf("shmctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
id, str_ipc_64, str_ipc_cmd, info, sprintrc_grep(rc));
return;
}
printf("shmctl\\(%d, (%s\\|)?%s, \\{used_ids=%d, shm_tot=%llu"
", shm_rss=%llu, shm_swp=%llu, swap_attempts=%llu"
", swap_successes=%llu\\}\\) = %d\n",
id,
str_ipc_64,
str_ipc_cmd,
info->used_ids,
(unsigned long long) info->shm_tot,
(unsigned long long) info->shm_rss,
(unsigned long long) info->shm_swp,
(unsigned long long) info->swap_attempts,
(unsigned long long) info->swap_successes,
rc);
}
int
main(void)
{
static const key_t private_key =
(key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
#if TEST_SHMCTL_BOGUS_CMD || TEST_SHMCTL_BOGUS_ADDR
static const int bogus_id = 0xdefaced1;
#endif
#if TEST_SHMCTL_BOGUS_CMD
static const int bogus_cmd = 0xdefaced2;
#endif
#if TEST_SHMCTL_BOGUS_ADDR
static void * const bogus_addr = (void *) -1L;
#endif
static const size_t bogus_size =
/*
* musl sets size to SIZE_MAX if size argument is greater than
* PTRDIFF_MAX - musl/src/ipc/shmget.c
*/
#ifdef __GLIBC__
(size_t) 0xdec0ded1dec0ded2ULL;
#else
(size_t) 0x1e55c0de5dec0dedULL;
#endif
static const unsigned int bogus_ipc_shm_flags =
IPC_CREAT | IPC_EXCL | SHM_HUGETLB | SHM_NORESERVE;
static const unsigned int huge_mask = SHM_HUGE_MASK << SHM_HUGE_SHIFT;
static const unsigned int huge_flags = 21 << SHM_HUGE_SHIFT;
int bogus_flags;
int rc;
union {
struct shmid_ds ds;
struct shminfo ipc_info;
struct shm_info shm_info;
} buf;
rc = shmget(bogus_key, bogus_size, 0);
printf("shmget\\(%#llx, %zu, 000\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
sprintrc_grep(rc));
rc = shmget(bogus_key, bogus_size, huge_flags);
printf("shmget\\(%#llx, %zu, %s\\|%#03o\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
str_shm_huge, 0, sprintrc_grep(rc));
bogus_flags = 0xface1e55 & ~(bogus_ipc_shm_flags | huge_mask);
rc = shmget(bogus_key, bogus_size, bogus_flags);
printf("shmget\\(%#llx, %zu, %#x\\|%#03o\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
bogus_flags & ~0777,
bogus_flags & 0777, sprintrc_grep(rc));
bogus_flags |= bogus_ipc_shm_flags;
rc = shmget(bogus_key, bogus_size, bogus_flags);
printf("shmget\\(%#llx, %zu, %s\\|%#03o\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
str_ipc_flags,
bogus_flags & 0777, sprintrc_grep(rc));
bogus_flags |= huge_flags;
rc = shmget(bogus_key, bogus_size, bogus_flags);
printf("shmget\\(%#llx, %zu, %s\\|%s\\|%#03o\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
str_ipc_flags, str_shm_huge,
bogus_flags & 0777, sprintrc_grep(rc));
bogus_flags &= ~bogus_ipc_shm_flags;
rc = shmget(bogus_key, bogus_size, bogus_flags);
printf("shmget\\(%#llx, %zu, %#x\\|%s\\|%#03o\\) = %s\n",
zero_extend_signed_to_ull(bogus_key), bogus_size,
bogus_flags & ~(0777 | huge_mask),
str_shm_huge,
bogus_flags & 0777, sprintrc_grep(rc));
id = shmget(private_key, 1, 0600);
if (id < 0)
perror_msg_and_skip("shmget");
printf("shmget\\(%s, 1, 0600\\) = %d\n", str_ipc_private, id);
atexit(cleanup);
#if TEST_SHMCTL_BOGUS_CMD
rc = shmctl(bogus_id, bogus_cmd, NULL);
printf("shmctl\\(%d, (%s\\|)?%s, NULL\\) = %s\n",
bogus_id, str_ipc_64, str_bogus_cmd, sprintrc_grep(rc));
#endif
#if TEST_SHMCTL_BOGUS_ADDR
rc = shmctl(bogus_id, IPC_STAT, bogus_addr);
printf("shmctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
bogus_id, str_ipc_64, str_ipc_stat, bogus_addr,
sprintrc_grep(rc));
#endif
rc = shmctl(id, IPC_STAT, &buf.ds);
if (rc < 0)
perror_msg_and_skip("shmctl IPC_STAT");
print_shmid_ds(str_ipc_stat, &buf.ds, rc);
if (shmctl(id, IPC_SET, &buf.ds))
perror_msg_and_skip("shmctl IPC_SET");
printf("shmctl\\(%d, (%s\\|)?%s, \\{shm_perm=\\{uid=%u, gid=%u"
", mode=%#o\\}\\}\\) = 0\n",
id, str_ipc_64, str_ipc_set,
(unsigned) buf.ds.shm_perm.uid,
(unsigned) buf.ds.shm_perm.gid,
(unsigned) buf.ds.shm_perm.mode);
rc = shmctl(id, IPC_INFO, &buf.ds);
print_ipc_info(str_ipc_info, &buf.ipc_info, rc);
rc = shmctl(id, SHM_INFO, &buf.ds);
print_shm_info(str_shm_info, &buf.shm_info, rc);
rc = shmctl(id, SHM_STAT, &buf.ds);
print_shmid_ds(str_shm_stat, &buf.ds, rc);
rc = shmctl(id, SHM_STAT_ANY, &buf.ds);
print_shmid_ds(str_shm_stat_any, &buf.ds, rc);
return 0;
}
|