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() fails and set errno to EINVAL when shmid is invalid.
10 * 2) shmat() fails and set errno to EINVAL when shmaddr is not page
11 * aligned and SHM_RND is not given
12 * 3) shmat() fails and set errno to EACCES when the shm resource has
13 * no read/write permission.
14 */
15
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/ipc.h>
20 #include <sys/shm.h>
21 #include <pwd.h>
22
23 #include "tst_test.h"
24 #include "tst_safe_sysv_ipc.h"
25 #include "libnewipc.h"
26
27 static int shm_id1 = -1;
28 static int shm_id2 = -1;
29 static void *aligned_addr;
30 static void *unaligned_addr;
31 static key_t shm_key1;
32 static struct passwd *pw;
33
34 static struct test_case_t {
35 int *shmid;
36 void **shmaddr;
37 int exp_err;
38 int exp_user;
39 } tcases[] = {
40 {&shm_id1, &aligned_addr, EINVAL, 0},
41 {&shm_id2, &unaligned_addr, EINVAL, 0},
42 {&shm_id2, &aligned_addr, EACCES, 1},
43 };
44
verify_shmat(struct test_case_t * tc)45 static void verify_shmat(struct test_case_t *tc)
46 {
47 TST_EXP_FAIL_PTR_VOID(shmat(*tc->shmid, *tc->shmaddr, 0), tc->exp_err);
48 }
49
do_shmat(unsigned int n)50 static void do_shmat(unsigned int n)
51 {
52 pid_t pid;
53
54 struct test_case_t *tc = &tcases[n];
55
56 if (!tc->exp_user) {
57 verify_shmat(tc);
58 } else {
59 pid = SAFE_FORK();
60 if (pid) {
61 tst_reap_children();
62 } else {
63 SAFE_SETUID(pw->pw_uid);
64 verify_shmat(tc);
65 exit(0);
66 }
67 }
68 }
69
setup(void)70 static void setup(void)
71 {
72 aligned_addr = PROBE_FREE_ADDR();
73 unaligned_addr = aligned_addr + SHMLBA - 1;
74
75 shm_key1 = GETIPCKEY();
76
77 shm_id2 = SAFE_SHMGET(shm_key1, INT_SIZE, SHM_RW | IPC_CREAT | IPC_EXCL);
78
79 pw = SAFE_GETPWNAM("nobody");
80 }
81
cleanup(void)82 static void cleanup(void)
83 {
84 if (shm_id2 != -1)
85 SAFE_SHMCTL(shm_id2, IPC_RMID, NULL);
86 }
87
88 static struct tst_test test = {
89 .needs_root = 1,
90 .forks_child = 1,
91 .test = do_shmat,
92 .tcnt = ARRAY_SIZE(tcases),
93 .setup = setup,
94 .cleanup = cleanup
95 };
96