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