Skip to content

Commit f9dd87f

Browse files
Stanislav Kinsburskytorvalds
authored andcommitted
ipc: message queue receive cleanup
Move all message related manipulation into one function msg_fill(). Actually, two functions because of the compat one. [akpm@linux-foundation.org: checkpatch fixes] Signed-off-by: Stanislav Kinsbursky <skinsbursky@parallels.com> Cc: Serge Hallyn <serge.hallyn@canonical.com> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Pavel Emelyanov <xemul@parallels.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 03f5956 commit f9dd87f

3 files changed

Lines changed: 45 additions & 49 deletions

File tree

‎include/linux/msg.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct msg_queue {
3434
/* Helper routines for sys_msgsnd and sys_msgrcv */
3535
extern long do_msgsnd(int msqid, long mtype, void __user *mtext,
3636
size_t msgsz, int msgflg);
37-
extern long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
38-
size_t msgsz, long msgtyp, int msgflg);
37+
extern long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
38+
int msgflg,
39+
long (*msg_fill)(void __user *, struct msg_msg *, size_t));
3940

4041
#endif /* _LINUX_MSG_H */

‎ipc/compat.c‎

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ static long do_compat_semctl(int first, int second, int third, u32 pad)
306306
return err;
307307
}
308308

309+
long compat_do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
310+
{
311+
struct compat_msgbuf __user *msgp = dest;
312+
size_t msgsz;
313+
314+
if (put_user(msg->m_type, &msgp->mtype))
315+
return -EFAULT;
316+
317+
msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
318+
if (store_msg(msgp->mtext, msg, msgsz))
319+
return -EFAULT;
320+
return msgsz;
321+
}
322+
309323
#ifdef CONFIG_ARCH_WANT_OLD_COMPAT_IPC
310324
long compat_sys_semctl(int first, int second, int third, void __user *uptr)
311325
{
@@ -337,34 +351,21 @@ long compat_sys_msgsnd(int first, int second, int third, void __user *uptr)
337351
long compat_sys_msgrcv(int first, int second, int msgtyp, int third,
338352
int version, void __user *uptr)
339353
{
340-
struct compat_msgbuf __user *up;
341-
long type;
342-
int err;
343-
344354
if (first < 0)
345355
return -EINVAL;
346356
if (second < 0)
347357
return -EINVAL;
348358

349359
if (!version) {
350360
struct compat_ipc_kludge ipck;
351-
err = -EINVAL;
352361
if (!uptr)
353-
goto out;
354-
err = -EFAULT;
362+
return -EINVAL;
355363
if (copy_from_user (&ipck, uptr, sizeof(ipck)))
356-
goto out;
364+
return -EFAULT;
357365
uptr = compat_ptr(ipck.msgp);
358366
msgtyp = ipck.msgtyp;
359367
}
360-
up = uptr;
361-
err = do_msgrcv(first, &type, up->mtext, second, msgtyp, third);
362-
if (err < 0)
363-
goto out;
364-
if (put_user(type, &up->mtype))
365-
err = -EFAULT;
366-
out:
367-
return err;
368+
return do_msgrcv(first, uptr, second, msgtyp, third, compat_do_msg_fill);
368369
}
369370
#else
370371
long compat_sys_semctl(int semid, int semnum, int cmd, int arg)
@@ -385,16 +386,8 @@ long compat_sys_msgsnd(int msqid, struct compat_msgbuf __user *msgp,
385386
long compat_sys_msgrcv(int msqid, struct compat_msgbuf __user *msgp,
386387
compat_ssize_t msgsz, long msgtyp, int msgflg)
387388
{
388-
long err, mtype;
389-
390-
err = do_msgrcv(msqid, &mtype, msgp->mtext, (ssize_t)msgsz, msgtyp, msgflg);
391-
if (err < 0)
392-
goto out;
393-
394-
if (put_user(mtype, &msgp->mtype))
395-
err = -EFAULT;
396-
out:
397-
return err;
389+
return do_msgrcv(msqid, msgp, (ssize_t)msgsz, msgtyp, msgflg,
390+
compat_do_msg_fill);
398391
}
399392
#endif
400393

‎ipc/msg.c‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,30 @@ static inline int convert_mode(long *msgtyp, int msgflg)
755755
return SEARCH_EQUAL;
756756
}
757757

758-
long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
759-
size_t msgsz, long msgtyp, int msgflg)
758+
static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
759+
{
760+
struct msgbuf __user *msgp = dest;
761+
size_t msgsz;
762+
763+
if (put_user(msg->m_type, &msgp->mtype))
764+
return -EFAULT;
765+
766+
msgsz = (bufsz > msg->m_ts) ? msg->m_ts : bufsz;
767+
if (store_msg(msgp->mtext, msg, msgsz))
768+
return -EFAULT;
769+
return msgsz;
770+
}
771+
772+
long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
773+
int msgflg,
774+
long (*msg_handler)(void __user *, struct msg_msg *, size_t))
760775
{
761776
struct msg_queue *msq;
762777
struct msg_msg *msg;
763778
int mode;
764779
struct ipc_namespace *ns;
765780

766-
if (msqid < 0 || (long) msgsz < 0)
781+
if (msqid < 0 || (long) bufsz < 0)
767782
return -EINVAL;
768783
mode = convert_mode(&msgtyp, msgflg);
769784
ns = current->nsproxy->ipc_ns;
@@ -804,7 +819,7 @@ long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
804819
* Found a suitable message.
805820
* Unlink it from the queue.
806821
*/
807-
if ((msgsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
822+
if ((bufsz < msg->m_ts) && !(msgflg & MSG_NOERROR)) {
808823
msg = ERR_PTR(-E2BIG);
809824
goto out_unlock;
810825
}
@@ -831,7 +846,7 @@ long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
831846
if (msgflg & MSG_NOERROR)
832847
msr_d.r_maxsize = INT_MAX;
833848
else
834-
msr_d.r_maxsize = msgsz;
849+
msr_d.r_maxsize = bufsz;
835850
msr_d.r_msg = ERR_PTR(-EAGAIN);
836851
current->state = TASK_INTERRUPTIBLE;
837852
msg_unlock(msq);
@@ -894,29 +909,16 @@ long do_msgrcv(int msqid, long *pmtype, void __user *mtext,
894909
if (IS_ERR(msg))
895910
return PTR_ERR(msg);
896911

897-
msgsz = (msgsz > msg->m_ts) ? msg->m_ts : msgsz;
898-
*pmtype = msg->m_type;
899-
if (store_msg(mtext, msg, msgsz))
900-
msgsz = -EFAULT;
901-
912+
bufsz = msg_handler(buf, msg, bufsz);
902913
free_msg(msg);
903914

904-
return msgsz;
915+
return bufsz;
905916
}
906917

907918
SYSCALL_DEFINE5(msgrcv, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
908919
long, msgtyp, int, msgflg)
909920
{
910-
long err, mtype;
911-
912-
err = do_msgrcv(msqid, &mtype, msgp->mtext, msgsz, msgtyp, msgflg);
913-
if (err < 0)
914-
goto out;
915-
916-
if (put_user(mtype, &msgp->mtype))
917-
err = -EFAULT;
918-
out:
919-
return err;
921+
return do_msgrcv(msqid, msgp, msgsz, msgtyp, msgflg, do_msg_fill);
920922
}
921923

922924
#ifdef CONFIG_PROC_FS

0 commit comments

Comments
 (0)