1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*
7 * DESCRIPTION
8 *
9 * 1) shmat() chooses a suitable (unused) address when shmaddr is NULL.
10 * 2) shmat() attaches shm segment to the shmaddr when shmaddr is a
11 * page-aligned address.
12 * 3) shmat() attaches shm segment to the address equal to shmaddr rounded
13 * down to the nearest multiple of SHMLBA when shmaddr is a page-unaligned
14 * address and shmflg is set to SHM_RND.
15 * 4) shmat() attaches shm segment to the shmaddr for reading when shmflg
16 * is set to SHM_RDONLY.
17 */
18
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/ipc.h>
22 #include <sys/shm.h>
23 #include <sys/wait.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26
27 #include "tst_test.h"
28 #include "tst_safe_sysv_ipc.h"
29 #include "libnewipc.h"
30
31 #define ALIGN_DOWN(in_addr) ((void *)(((uintptr_t)in_addr / SHMLBA) * SHMLBA))
32
33 static int shm_id = -1;
34 static key_t shm_key;
35 static void *null_addr;
36 static void *aligned_addr;
37 static void *unaligned_addr;
38
39 static struct test_case_t {
40 void **shmaddr;
41 int flag;
42 int exp_status;
43 char *desp;
44 } tcases[] = {
45 {&null_addr, 0, 0, "NULL address"},
46 {&aligned_addr, 0, 0, "aligned address"},
47 {&unaligned_addr, SHM_RND, 0, "unaligned address with SHM_RND"},
48 {&aligned_addr, SHM_RDONLY, SIGSEGV,
49 "aligned address with SHM_READONLY, and got SIGSEGV on write"}
50 };
51
expected_addr(void * in_addr,void * out_addr)52 static void *expected_addr(void *in_addr, void *out_addr)
53 {
54 if (!in_addr)
55 return out_addr;
56
57 return ALIGN_DOWN(in_addr);
58 }
59
do_child(int * in_addr,int expect_crash)60 static void do_child(int *in_addr, int expect_crash)
61 {
62 if (expect_crash)
63 tst_no_corefile(1);
64
65 *in_addr = 10;
66
67 exit(0);
68 }
69
expected_status(int status,int exp_status)70 static int expected_status(int status, int exp_status)
71 {
72 if (!exp_status && WIFEXITED(status))
73 return 0;
74
75 if (exp_status && WIFSIGNALED(status) && WTERMSIG(status) == exp_status)
76 return 0;
77
78 return 1;
79 }
80
verify_shmat(unsigned int n)81 static void verify_shmat(unsigned int n)
82 {
83 int *addr;
84 pid_t pid;
85 int status = 0;
86 struct shmid_ds buf;
87
88 struct test_case_t *tc = &tcases[n];
89
90 addr = shmat(shm_id, *tc->shmaddr, tc->flag);
91 if (addr == (void *)-1) {
92 tst_res(TFAIL | TERRNO, "shmat() failed");
93 return;
94 }
95
96 SAFE_SHMCTL(shm_id, IPC_STAT, &buf);
97
98 if (buf.shm_nattch != 1) {
99 tst_res(TFAIL, "number of attaches was incorrect");
100 goto end;
101 }
102
103 if (buf.shm_segsz != INT_SIZE) {
104 tst_res(TFAIL, "segment size was incorrect");
105 goto end;
106 }
107
108 if (expected_addr(*tc->shmaddr, addr) != addr) {
109 tst_res(TFAIL,
110 "shared memory address %p is not correct, expected %p",
111 addr, expected_addr(*tc->shmaddr, addr));
112 goto end;
113 }
114
115 pid = SAFE_FORK();
116 if (!pid)
117 do_child(addr, tc->exp_status == SIGSEGV);
118 else
119 SAFE_WAITPID(pid, &status, 0);
120
121 if (expected_status(status, tc->exp_status))
122 tst_res(TFAIL, "shmat() failed to attach %s", tc->desp);
123 else
124 tst_res(TPASS, "shmat() succeeded to attach %s", tc->desp);
125
126 end:
127 SAFE_SHMDT(addr);
128 }
129
setup(void)130 static void setup(void)
131 {
132 aligned_addr = PROBE_FREE_ADDR();
133 unaligned_addr = aligned_addr + SHMLBA - 1;
134
135 shm_key = GETIPCKEY();
136
137 shm_id = SAFE_SHMGET(shm_key, INT_SIZE, SHM_RW | IPC_CREAT | IPC_EXCL);
138 }
139
cleanup(void)140 static void cleanup(void)
141 {
142 if (shm_id != -1)
143 SAFE_SHMCTL(shm_id, IPC_RMID, NULL);
144 }
145
146 static struct tst_test test = {
147 .needs_root = 1,
148 .forks_child = 1,
149 .setup = setup,
150 .cleanup = cleanup,
151 .test = verify_shmat,
152 .tcnt = ARRAY_SIZE(tcases)
153 };
154