1 /* Copyright (C) 1995-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <sys/msg.h>
19 #include <ipc_priv.h>
20 #include <sysdep.h>
21 #include <shlib-compat.h>
22 #include <errno.h>
23 #include <linux/posix_types.h>  /* For __kernel_mode_t.  */
24 
25 /* POSIX states ipc_perm mode should have type of mode_t.  */
26 _Static_assert (sizeof ((struct msqid_ds){0}.msg_perm.mode)
27 		== sizeof (mode_t),
28 		"sizeof (msqid_ds.msg_perm.mode) != sizeof (mode_t)");
29 
30 #if __IPC_TIME64 == 0
31 typedef struct msqid_ds msgctl_arg_t;
32 #else
33 # include <struct_kernel_msqid64_ds.h>
34 
35 static void
msqid64_to_kmsqid64(const struct __msqid64_ds * msqid64,struct kernel_msqid64_ds * kmsqid)36 msqid64_to_kmsqid64 (const struct __msqid64_ds *msqid64,
37 		     struct kernel_msqid64_ds *kmsqid)
38 {
39   kmsqid->msg_perm       = msqid64->msg_perm;
40   kmsqid->msg_stime      = msqid64->msg_stime;
41   kmsqid->msg_stime_high = msqid64->msg_stime >> 32;
42   kmsqid->msg_rtime      = msqid64->msg_rtime;
43   kmsqid->msg_rtime_high = msqid64->msg_rtime >> 32;
44   kmsqid->msg_ctime      = msqid64->msg_ctime;
45   kmsqid->msg_ctime_high = msqid64->msg_ctime >> 32;
46   kmsqid->msg_cbytes     = msqid64->msg_cbytes;
47   kmsqid->msg_qnum       = msqid64->msg_qnum;
48   kmsqid->msg_qbytes     = msqid64->msg_qbytes;
49   kmsqid->msg_lspid      = msqid64->msg_lspid;
50   kmsqid->msg_lrpid      = msqid64->msg_lrpid;
51 }
52 
53 static void
kmsqid64_to_msqid64(const struct kernel_msqid64_ds * kmsqid,struct __msqid64_ds * msqid64)54 kmsqid64_to_msqid64 (const struct kernel_msqid64_ds *kmsqid,
55 		     struct __msqid64_ds *msqid64)
56 {
57   msqid64->msg_perm   = kmsqid->msg_perm;
58   msqid64->msg_stime  = kmsqid->msg_stime
59 		        | ((__time64_t) kmsqid->msg_stime_high << 32);
60   msqid64->msg_rtime  = kmsqid->msg_rtime
61 		        | ((__time64_t) kmsqid->msg_rtime_high << 32);
62   msqid64->msg_ctime  = kmsqid->msg_ctime
63 		        | ((__time64_t) kmsqid->msg_ctime_high << 32);
64   msqid64->msg_cbytes = kmsqid->msg_cbytes;
65   msqid64->msg_qnum   = kmsqid->msg_qnum;
66   msqid64->msg_qbytes = kmsqid->msg_qbytes;
67   msqid64->msg_lspid  = kmsqid->msg_lspid;
68   msqid64->msg_lrpid  = kmsqid->msg_lrpid;
69 }
70 
71 typedef struct kernel_msqid64_ds msgctl_arg_t;
72 #endif
73 
74 static int
msgctl_syscall(int msqid,int cmd,msgctl_arg_t * buf)75 msgctl_syscall (int msqid, int cmd, msgctl_arg_t *buf)
76 {
77 #ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS
78   return INLINE_SYSCALL_CALL (msgctl, msqid, cmd | __IPC_64, buf);
79 #else
80   return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd | __IPC_64, 0,
81 			      buf);
82 #endif
83 }
84 
85 int
__msgctl64(int msqid,int cmd,struct __msqid64_ds * buf)86 __msgctl64 (int msqid, int cmd, struct __msqid64_ds *buf)
87 {
88 #if __IPC_TIME64
89   struct kernel_msqid64_ds ksemid, *arg = NULL;
90 #else
91   msgctl_arg_t *arg;
92 #endif
93 
94   switch (cmd)
95     {
96     case IPC_RMID:
97       arg = NULL;
98       break;
99 
100     case IPC_SET:
101     case IPC_STAT:
102     case MSG_STAT:
103     case MSG_STAT_ANY:
104 #if __IPC_TIME64
105       if (buf != NULL)
106 	{
107 	  msqid64_to_kmsqid64 (buf, &ksemid);
108 	  arg = &ksemid;
109 	}
110 # ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T
111       if (cmd == IPC_SET)
112 	arg->msg_perm.mode *= 0x10000U;
113 # endif
114 #else
115       arg = buf;
116 #endif
117       break;
118 
119     case IPC_INFO:
120     case MSG_INFO:
121       /* This is a Linux extension where kernel returns a 'struct msginfo'
122 	 instead.  */
123       arg = (__typeof__ (arg)) buf;
124       break;
125 
126     default:
127       __set_errno (EINVAL);
128       return -1;
129     }
130 
131   int ret = msgctl_syscall (msqid, cmd, arg);
132   if (ret < 0)
133     return ret;
134 
135   switch (cmd)
136     {
137     case IPC_STAT:
138     case MSG_STAT:
139     case MSG_STAT_ANY:
140 #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T
141       arg->msg_perm.mode >>= 16;
142 #else
143       /* Old Linux kernel versions might not clear the mode padding.  */
144       if (sizeof ((struct msqid_ds){0}.msg_perm.mode)
145           != sizeof (__kernel_mode_t))
146 	arg->msg_perm.mode &= 0xFFFF;
147 #endif
148 
149 #if __IPC_TIME64
150       kmsqid64_to_msqid64 (arg, buf);
151 #endif
152     }
153 
154   return ret;
155 }
156 #if __TIMESIZE != 64
libc_hidden_def(__msgctl64)157 libc_hidden_def (__msgctl64)
158 
159 static void
160 msqid_to_msqid64 (struct __msqid64_ds *mq64, const struct msqid_ds *mq)
161 {
162   mq64->msg_perm   = mq->msg_perm;
163   mq64->msg_stime  = mq->msg_stime
164 		     | ((__time64_t) mq->__msg_stime_high << 32);
165   mq64->msg_rtime  = mq->msg_rtime
166 		     | ((__time64_t) mq->__msg_rtime_high << 32);
167   mq64->msg_ctime  = mq->msg_ctime
168 		     | ((__time64_t) mq->__msg_ctime_high << 32);
169   mq64->msg_cbytes = mq->msg_cbytes;
170   mq64->msg_qnum   = mq->msg_qnum;
171   mq64->msg_qbytes = mq->msg_qbytes;
172   mq64->msg_lspid  = mq->msg_lspid;
173   mq64->msg_lrpid  = mq->msg_lrpid;
174 }
175 
176 static void
msqid64_to_msqid(struct msqid_ds * mq,const struct __msqid64_ds * mq64)177 msqid64_to_msqid (struct msqid_ds *mq, const struct __msqid64_ds *mq64)
178 {
179   mq->msg_perm         = mq64->msg_perm;
180   mq->msg_stime        = mq64->msg_stime;
181   mq->__msg_stime_high = 0;
182   mq->msg_rtime        = mq64->msg_rtime;
183   mq->__msg_rtime_high = 0;
184   mq->msg_ctime        = mq64->msg_ctime;
185   mq->__msg_ctime_high = 0;
186   mq->msg_cbytes       = mq64->msg_cbytes;
187   mq->msg_qnum         = mq64->msg_qnum;
188   mq->msg_qbytes       = mq64->msg_qbytes;
189   mq->msg_lspid        = mq64->msg_lspid;
190   mq->msg_lrpid        = mq64->msg_lrpid;
191 }
192 
193 int
__msgctl(int msqid,int cmd,struct msqid_ds * buf)194 __msgctl (int msqid, int cmd, struct msqid_ds *buf)
195 {
196   struct __msqid64_ds msqid64, *buf64 = NULL;
197   if (buf != NULL)
198     {
199       /* This is a Linux extension where kernel returns a 'struct msginfo'
200 	 instead.  */
201       if (cmd == IPC_INFO || cmd == MSG_INFO)
202 	buf64 = (struct __msqid64_ds *) buf;
203       else
204 	{
205 	  msqid_to_msqid64 (&msqid64, buf);
206 	  buf64 = &msqid64;
207 	}
208     }
209 
210   int ret = __msgctl64 (msqid, cmd, buf64);
211   if (ret < 0)
212     return ret;
213 
214   switch (cmd)
215     {
216     case IPC_STAT:
217     case MSG_STAT:
218     case MSG_STAT_ANY:
219       msqid64_to_msqid (buf, buf64);
220     }
221 
222   return ret;
223 }
224 #endif
225 
226 #ifndef DEFAULT_VERSION
227 # ifndef __ASSUME_SYSVIPC_BROKEN_MODE_T
228 #  define DEFAULT_VERSION GLIBC_2_2
229 # else
230 #  define DEFAULT_VERSION GLIBC_2_31
231 # endif
232 #endif
233 versioned_symbol (libc, __msgctl, msgctl, DEFAULT_VERSION);
234 
235 #if defined __ASSUME_SYSVIPC_BROKEN_MODE_T \
236     && SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_31)
237 int
238 attribute_compat_text_section
__msgctl_mode16(int msqid,int cmd,struct msqid_ds * buf)239 __msgctl_mode16 (int msqid, int cmd, struct msqid_ds *buf)
240 {
241   return msgctl_syscall (msqid, cmd, (msgctl_arg_t *) buf);
242 }
243 compat_symbol (libc, __msgctl_mode16, msgctl, GLIBC_2_2);
244 #endif
245 
246 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
247 struct __old_msqid_ds
248 {
249   struct __old_ipc_perm msg_perm;	/* structure describing operation permission */
250   struct msg *__msg_first;		/* pointer to first message on queue */
251   struct msg *__msg_last;		/* pointer to last message on queue */
252   __time_t msg_stime;			/* time of last msgsnd command */
253   __time_t msg_rtime;			/* time of last msgrcv command */
254   __time_t msg_ctime;			/* time of last change */
255   struct wait_queue *__wwait;		/* ??? */
256   struct wait_queue *__rwait;		/* ??? */
257   unsigned short int __msg_cbytes;	/* current number of bytes on queue */
258   unsigned short int msg_qnum;		/* number of messages currently on queue */
259   unsigned short int msg_qbytes;	/* max number of bytes allowed on queue */
260   __ipc_pid_t msg_lspid;		/* pid of last msgsnd() */
261   __ipc_pid_t msg_lrpid;		/* pid of last msgrcv() */
262 };
263 
264 int
265 attribute_compat_text_section
__old_msgctl(int msqid,int cmd,struct __old_msqid_ds * buf)266 __old_msgctl (int msqid, int cmd, struct __old_msqid_ds *buf)
267 {
268 #if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \
269     && !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64
270   /* For architecture that have wire-up msgctl but also have __IPC_64 to a
271      value different than default (0x0) it means the compat symbol used the
272      __NR_ipc syscall.  */
273   return INLINE_SYSCALL_CALL (msgctl, msqid, cmd, buf);
274 #else
275   return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd, 0, buf);
276 #endif
277 }
278 compat_symbol (libc, __old_msgctl, msgctl, GLIBC_2_0);
279 #endif
280