• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
3  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/msg.h>
34 
35 #include "xlat.h"
36 #include "xlat/resource_flags.h"
37 
38 /*
39  * Before glibc-2.22-122-gbe48165, ppc64 code tried to retrieve data
40  * provided in third argument of msgctl call (in case of IPC_SET cmd)
41  * which led to segmentation fault.
42  */
43 #undef TEST_MSGCTL_BOGUS_ADDR
44 #if defined __GLIBC__ && defined POWERPC64
45 # if !(defined __GLIBC_MINOR__) \
46    || ((__GLIBC__ << 16) + __GLIBC_MINOR__ < (2 << 16) + 23)
47 #  define TEST_MSGCTL_BOGUS_ADDR 0
48 # endif
49 #endif /* __GLIBC__ && POWERPC64 */
50 
51 #ifndef TEST_MSGCTL_BOGUS_ADDR
52 # define TEST_MSGCTL_BOGUS_ADDR 1
53 #endif
54 
55 static int id = -1;
56 
57 static void
cleanup(void)58 cleanup(void)
59 {
60 	msgctl(id, IPC_RMID, NULL);
61 	printf("msgctl\\(%d, (IPC_64\\|)?IPC_RMID, NULL\\) += 0\n", id);
62 	id = -1;
63 }
64 
65 int
main(void)66 main(void)
67 {
68 	static const key_t private_key =
69 		(key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
70 	static const key_t bogus_key = (key_t) 0xeca86420fdb97531ULL;
71 	static const int bogus_msgid = 0xfdb97531;
72 	static const int bogus_cmd = 0xdeadbeef;
73 #if TEST_MSGCTL_BOGUS_ADDR
74 	static void * const bogus_addr = (void *) -1L;
75 #endif
76 	static const int bogus_flags = 0xface1e55 & ~IPC_CREAT;
77 
78 	int rc;
79 	struct msqid_ds ds;
80 
81 	rc = msgget(bogus_key, bogus_flags);
82 	printf("msgget\\(%#llx, %s%s%s%#x\\|%#04o\\) += %s\n",
83 	       zero_extend_signed_to_ull(bogus_key),
84 	       IPC_CREAT & bogus_flags ? "IPC_CREAT\\|" : "",
85 	       IPC_EXCL & bogus_flags ? "IPC_EXCL\\|" : "",
86 	       IPC_NOWAIT & bogus_flags ? "IPC_NOWAIT\\|" : "",
87 	       bogus_flags & ~(0777 | IPC_CREAT | IPC_EXCL | IPC_NOWAIT),
88 	       bogus_flags & 0777, sprintrc_grep(rc));
89 
90 	id = msgget(private_key, 0600);
91 	if (id < 0)
92 		perror_msg_and_skip("msgget");
93 	printf("msgget\\(IPC_PRIVATE, 0600\\) += %d\n", id);
94 	atexit(cleanup);
95 
96 	rc = msgctl(bogus_msgid, bogus_cmd, NULL);
97 	printf("msgctl\\(%d, (IPC_64\\|)?%#x /\\* MSG_\\?\\?\\? \\*/, NULL\\)"
98 	       " += %s\n", bogus_msgid, bogus_cmd, sprintrc_grep(rc));
99 
100 #if TEST_MSGCTL_BOGUS_ADDR
101 	rc = msgctl(bogus_msgid, IPC_SET, bogus_addr);
102 	printf("msgctl\\(%d, (IPC_64\\|)?IPC_SET, %p\\) += %s\n",
103 	       bogus_msgid, bogus_addr, sprintrc_grep(rc));
104 #endif
105 
106 	if (msgctl(id, IPC_STAT, &ds))
107 		perror_msg_and_skip("msgctl IPC_STAT");
108 	printf("msgctl\\(%d, (IPC_64\\|)?IPC_STAT, \\{msg_perm=\\{uid=%u"
109 	       ", gid=%u, mode=%#o, key=%u, cuid=%u, cgid=%u\\}, msg_stime=%u"
110 	       ", msg_rtime=%u, msg_ctime=%u, msg_qnum=%u, msg_qbytes=%u"
111 	       ", msg_lspid=%u, msg_lrpid=%u\\}\\) += 0\n",
112 	       id, (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid,
113 	       (unsigned) ds.msg_perm.mode, (unsigned) ds.msg_perm.__key,
114 	       (unsigned) ds.msg_perm.cuid, (unsigned) ds.msg_perm.cgid,
115 	       (unsigned) ds.msg_stime, (unsigned) ds.msg_rtime,
116 	       (unsigned) ds.msg_ctime, (unsigned) ds.msg_qnum,
117 	       (unsigned) ds.msg_qbytes, (unsigned) ds.msg_lspid,
118 	       (unsigned) ds.msg_lrpid);
119 
120 	if (msgctl(id, IPC_SET, &ds))
121 		perror_msg_and_skip("msgctl IPC_SET");
122 	printf("msgctl\\(%d, (IPC_64\\|)?IPC_SET, \\{msg_perm=\\{uid=%u"
123 	       ", gid=%u, mode=%#o\\}, ...\\}\\) += 0\n",
124 	       id, (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid,
125 	       (unsigned) ds.msg_perm.mode);
126 
127 	rc = msgctl(0, MSG_INFO, &ds);
128 	printf("msgctl\\(0, (IPC_64\\|)?MSG_INFO, %p\\) += %s\n",
129 	       &ds, sprintrc_grep(rc));
130 
131 	rc = msgctl(id, MSG_STAT, &ds);
132 	printf("msgctl\\(%d, (IPC_64\\|)?MSG_STAT, %p\\) += %s\n",
133 	       id, &ds, sprintrc_grep(rc));
134 
135 	return 0;
136 }
137