1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *  03/2001 - Written by Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test for EACCES error.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Create a shared memory segment without read or write permissions under
13f08c3bdfSopenharmony_ci * unpriviledged user and call shmget() with SHM_RD/SHM_WR/SHM_RW flag to
14f08c3bdfSopenharmony_ci * trigger EACCES error.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci#include <sys/ipc.h>
19f08c3bdfSopenharmony_ci#include <stdlib.h>
20f08c3bdfSopenharmony_ci#include <pwd.h>
21f08c3bdfSopenharmony_ci#include <sys/shm.h>
22f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "libnewipc.h"
25f08c3bdfSopenharmony_ci#include "lapi/shm.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int shm_id = -1;
28f08c3bdfSopenharmony_cistatic key_t shmkey;
29f08c3bdfSopenharmony_cistatic struct tcase {
30f08c3bdfSopenharmony_ci	char *message;
31f08c3bdfSopenharmony_ci	int flag;
32f08c3bdfSopenharmony_ci} tcases[] = {
33f08c3bdfSopenharmony_ci	{"Testing SHM_RD flag", SHM_RD},
34f08c3bdfSopenharmony_ci	{"Testing SHM_WR flag", SHM_WR},
35f08c3bdfSopenharmony_ci	{"Testing SHM_RW flag", SHM_RW},
36f08c3bdfSopenharmony_ci};
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void verify_shmget(unsigned int n)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s", tc->message);
43f08c3bdfSopenharmony_ci	TST_EXP_FAIL2(shmget(shmkey, SHM_SIZE, tc->flag), EACCES, "shmget(%i, %i, %i)",
44f08c3bdfSopenharmony_ci		shmkey, SHM_SIZE, tc->flag);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void setup(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	struct passwd *pw;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
52f08c3bdfSopenharmony_ci	SAFE_SETUID(pw->pw_uid);
53f08c3bdfSopenharmony_ci	shmkey = GETIPCKEY();
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	shm_id = SAFE_SHMGET(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void cleanup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	if (shm_id >= 0)
61f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
62f08c3bdfSopenharmony_ci}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cistatic struct tst_test test = {
65f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
66f08c3bdfSopenharmony_ci	.needs_root = 1,
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.cleanup = cleanup,
69f08c3bdfSopenharmony_ci	.test = verify_shmget,
70f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
71f08c3bdfSopenharmony_ci};
72